]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/repeated-music-iterator.cc
release: 1.1.42
[lilypond.git] / lily / repeated-music-iterator.cc
index 5faf0880e7fcf3166b1c2fd60579c09034bc90af..80007e5b131874673eaf172fdbc812b898ed1e3a 100644 (file)
@@ -3,7 +3,7 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
   
  */
 
@@ -17,6 +17,8 @@ Repeated_music_iterator::Repeated_music_iterator ()
 {
   repeat_iter_p_ = 0;
   alternative_iter_p_ = 0;
+  here_mom_ = 0;
+  unfold_i_ = -1; 
 }
 
 Repeated_music_iterator::~Repeated_music_iterator ()
@@ -35,7 +37,7 @@ Repeated_music_iterator::do_print () const
 void
 Repeated_music_iterator::construct_children ()
 {
-  repeat_iter_p_ = get_iterator_p (repeated_music_l ()->repeat_p_);  
+  repeat_iter_p_ = get_iterator_p (dynamic_cast<Repeated_music const*> (music_l_)->repeat_p_);
 }
 
 void
@@ -43,70 +45,92 @@ Repeated_music_iterator::do_process_and_next (Moment m)
 {
   if (first_b_)
     {
-      bool success = report_to_l ()->try_music (repeated_music_l ());
+      bool success = report_to_l ()->try_music (dynamic_cast<Repeated_music const*> (music_l_));
       if (!success)
        music_l_->warning ( _("No one to print a volta bracket"));
     }
   if (repeat_iter_p_ && repeat_iter_p_->ok ())
-    repeat_iter_p_->process_and_next (m);
+    repeat_iter_p_->process_and_next (m - here_mom_);
   else
-    alternative_iter_p_->process_and_next (m - 
-      repeated_music_l ()->repeat_p_->duration ());
+    alternative_iter_p_->process_and_next (m - here_mom_);
   Music_iterator::do_process_and_next (m);
 }
 
 Moment
 Repeated_music_iterator::next_moment () const
 {
+  
   if (repeat_iter_p_)
-    return repeat_iter_p_->next_moment ();
+    return repeat_iter_p_->next_moment () + here_mom_;
   else if (alternative_iter_p_)
-//    return alternative_iter_p_->next_moment ();
-    return alternative_iter_p_->next_moment () + 
-      repeated_music_l ()->repeat_p_->duration ();
-// return 0;
-  return repeated_music_l ()->repeat_p_->duration ();
+    return alternative_iter_p_->next_moment () + here_mom_;
+
+  Repeated_music const*r = dynamic_cast<Repeated_music const*>(music_l_);
+  return r->alternative_p_->length_mom () + here_mom_;
 }
 
+/*
+  FIXME
+ */
 bool
 Repeated_music_iterator::ok () const
 {
-#if 0
-  if (repeat_iter_p_)
-    return repeat_iter_p_->ok ();
-  else if (alternative_iter_p_)
-    return alternative_iter_p_->ok ();
-  return false;
-#elif 0
-  if (repeat_iter_p_ && repeat_iter_p_->ok ())
-    return true;
-  else if (!alternative_iter_p_)
+  if (!repeat_iter_p_ && !alternative_iter_p_)
+    return false;
+
+  if ((repeat_iter_p_ && repeat_iter_p_->ok ())
+    || (alternative_iter_p_ && alternative_iter_p_->ok ()))
     return true;
-  return alternative_iter_p_->ok ();
-#else // perhaps iterating stops because we return false on repeat_iter...
-  if (repeat_iter_p_)
+
+  Repeated_music_iterator *urg = (Repeated_music_iterator*)this;
+  // urg, we're const
+  urg->start_next_element ();
+
+  return ok ();
+}
+
+
+void
+Repeated_music_iterator::start_next_element ()
+{
+  Repeated_music const*rep =dynamic_cast<Repeated_music const*> (music_l_);
+
+
+ if (repeat_iter_p_)
+    {
+      assert (!repeat_iter_p_->ok ());
+      assert (!alternative_iter_p_);
+      delete repeat_iter_p_;
+      repeat_iter_p_ = 0;
+      alternative_iter_p_ = dynamic_cast<Music_list_iterator*>
+       (get_iterator_p ((Music*)rep->alternative_p_));  
+      here_mom_ += rep->repeat_p_->length_mom ();
+    }
+  else if (alternative_iter_p_)
     {
-      if (repeat_iter_p_->ok ())
-        return true;
-      else
+      assert (!alternative_iter_p_->ok ());
+      assert (!repeat_iter_p_);
+      delete alternative_iter_p_;
+      alternative_iter_p_ = 0;
+      if (unfold_i_ < 0)
+       unfold_i_ = rep->unfold_b_ ? 
+         rep->repeats_i_ - 1 : 0;
+      if (unfold_i_)
         {
-         // urg, we're const
-         Repeated_music_iterator *urg = (Repeated_music_iterator*)this;
-         delete urg->repeat_iter_p_;
-         urg->repeat_iter_p_ = 0;
-         urg->alternative_iter_p_ = dynamic_cast<Music_list_iterator*>
-           (get_iterator_p ((Music*)repeated_music_l ()->alternative_p_));  
+         unfold_i_--;
+         repeat_iter_p_ = get_iterator_p (rep->repeat_p_);
+         // urg, assume same length alternatives for now...
+//       here_mom_ += rep->alternative_p_->music_p_list_p_->top ()->length_mom ();
+         /*
+           URG
+           this is *wrong* but at least it doesn't dump core
+           when unfolding, the alternative (sequential) music 
+           shouldn't automatically move to the next alternative
+
+           how to intercept this...
+          */
+         here_mom_ += rep->alternative_p_->length_mom ();
        }
     }
-  if (alternative_iter_p_)
-    return alternative_iter_p_->ok ();
-  return false;
-#endif
-}
-
-Repeated_music*
-Repeated_music_iterator::repeated_music_l () const
-{
-  return (Repeated_music*)Music_iterator::music_l_;
 }