]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music-iterator.cc
80007e5b131874673eaf172fdbc812b898ed1e3a
[lilypond.git] / lily / repeated-music-iterator.cc
1 /*   
2   repeated-music-iterator.cc --  implement Repeated_music_iterator
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--1999 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "repeated-music-iterator.hh"
11 #include "repeated-music.hh"
12 #include "musical-request.hh"
13 #include "translator-group.hh"
14 #include "command-request.hh"
15
16 Repeated_music_iterator::Repeated_music_iterator ()
17 {
18   repeat_iter_p_ = 0;
19   alternative_iter_p_ = 0;
20   here_mom_ = 0;
21   unfold_i_ = -1; 
22 }
23
24 Repeated_music_iterator::~Repeated_music_iterator ()
25 {
26   delete repeat_iter_p_;
27   delete alternative_iter_p_;
28 }
29
30 void
31 Repeated_music_iterator::do_print () const
32 {
33   if (repeat_iter_p_) repeat_iter_p_->print ();
34   if (alternative_iter_p_) alternative_iter_p_->print ();
35 }
36
37 void
38 Repeated_music_iterator::construct_children ()
39 {
40   repeat_iter_p_ = get_iterator_p (dynamic_cast<Repeated_music const*> (music_l_)->repeat_p_);
41 }
42
43 void
44 Repeated_music_iterator::do_process_and_next (Moment m)
45 {
46   if (first_b_)
47     {
48       bool success = report_to_l ()->try_music (dynamic_cast<Repeated_music const*> (music_l_));
49       if (!success)
50         music_l_->warning ( _("No one to print a volta bracket"));
51     }
52   if (repeat_iter_p_ && repeat_iter_p_->ok ())
53     repeat_iter_p_->process_and_next (m - here_mom_);
54   else
55     alternative_iter_p_->process_and_next (m - here_mom_);
56   Music_iterator::do_process_and_next (m);
57 }
58
59 Moment
60 Repeated_music_iterator::next_moment () const
61 {
62   
63   if (repeat_iter_p_)
64     return repeat_iter_p_->next_moment () + here_mom_;
65   else if (alternative_iter_p_)
66     return alternative_iter_p_->next_moment () + here_mom_;
67
68   Repeated_music const*r = dynamic_cast<Repeated_music const*>(music_l_);
69   return r->alternative_p_->length_mom () + here_mom_;
70 }
71
72 /*
73   FIXME
74  */
75 bool
76 Repeated_music_iterator::ok () const
77 {
78   if (!repeat_iter_p_ && !alternative_iter_p_)
79     return false;
80
81   if ((repeat_iter_p_ && repeat_iter_p_->ok ())
82     || (alternative_iter_p_ && alternative_iter_p_->ok ()))
83     return true;
84
85   Repeated_music_iterator *urg = (Repeated_music_iterator*)this;
86   // urg, we're const
87   urg->start_next_element ();
88
89   return ok ();
90 }
91
92
93 void
94 Repeated_music_iterator::start_next_element ()
95 {
96   Repeated_music const*rep =dynamic_cast<Repeated_music const*> (music_l_);
97
98
99  if (repeat_iter_p_)
100     {
101       assert (!repeat_iter_p_->ok ());
102       assert (!alternative_iter_p_);
103       delete repeat_iter_p_;
104       repeat_iter_p_ = 0;
105       alternative_iter_p_ = dynamic_cast<Music_list_iterator*>
106         (get_iterator_p ((Music*)rep->alternative_p_));  
107       here_mom_ += rep->repeat_p_->length_mom ();
108     }
109   else if (alternative_iter_p_)
110     {
111       assert (!alternative_iter_p_->ok ());
112       assert (!repeat_iter_p_);
113       delete alternative_iter_p_;
114       alternative_iter_p_ = 0;
115       if (unfold_i_ < 0)
116         unfold_i_ = rep->unfold_b_ ? 
117           rep->repeats_i_ - 1 : 0;
118       if (unfold_i_)
119         {
120           unfold_i_--;
121           repeat_iter_p_ = get_iterator_p (rep->repeat_p_);
122           // urg, assume same length alternatives for now...
123 //        here_mom_ += rep->alternative_p_->music_p_list_p_->top ()->length_mom ();
124           /*
125             URG
126             this is *wrong* but at least it doesn't dump core
127             when unfolding, the alternative (sequential) music 
128             shouldn't automatically move to the next alternative
129
130             how to intercept this...
131            */
132           here_mom_ += rep->alternative_p_->length_mom ();
133         }
134     }
135 }
136