]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music-iterator.cc
patch::: 1.1.29.jcn4: repeat-iter: snapnie
[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   done_b_ = false;
23 }
24
25 Repeated_music_iterator::~Repeated_music_iterator ()
26 {
27   delete repeat_iter_p_;
28   delete alternative_iter_p_;
29 }
30
31 void
32 Repeated_music_iterator::do_print () const
33 {
34   if (repeat_iter_p_) repeat_iter_p_->print ();
35   if (alternative_iter_p_) alternative_iter_p_->print ();
36 }
37
38 void
39 Repeated_music_iterator::construct_children ()
40 {
41   done_b_ = false;
42   repeat_iter_p_ = get_iterator_p (dynamic_cast<Repeated_music const*> (music_l_)->repeat_p_);
43 }
44
45 void
46 Repeated_music_iterator::do_process_and_next (Moment m)
47 {
48   if (first_b_)
49     {
50       bool success = report_to_l ()->try_music (dynamic_cast<Repeated_music const*> (music_l_));
51       if (!success)
52         music_l_->warning ( _("No one to print a volta bracket"));
53     }
54
55   if (!ok ())
56     start_next_element ();
57
58   //  if (!ok())
59      // return;
60       
61   if (repeat_iter_p_ && repeat_iter_p_->ok ())
62     repeat_iter_p_->process_and_next (m - here_mom_);
63   else if (alternative_iter_p_ && alternative_iter_p_->ok ())
64     alternative_iter_p_->process_and_next (m - here_mom_);
65   else
66     return;
67   
68   Music_iterator::do_process_and_next (m);
69 }
70
71 Moment
72 Repeated_music_iterator::next_moment () const
73 {
74   
75   if (repeat_iter_p_)
76     return repeat_iter_p_->next_moment () + here_mom_;
77   else if (alternative_iter_p_)
78     return alternative_iter_p_->next_moment () + here_mom_;
79
80   Repeated_music const*r = dynamic_cast<Repeated_music const*>(music_l_);
81   return r->alternative_p_->length_mom () + here_mom_;
82 }
83
84 bool
85 Repeated_music_iterator::ok () const
86 {
87   return !done_b_;
88 }
89
90 void
91 Repeated_music_iterator::start_next_element ()
92 {
93   Repeated_music const*rep =dynamic_cast<Repeated_music const*> (music_l_);
94
95   if (repeat_iter_p_)
96     {
97       assert (!repeat_iter_p_->ok ());
98       assert (!alternative_iter_p_);
99       delete repeat_iter_p_;
100       repeat_iter_p_ = 0;
101       alternative_iter_p_ = dynamic_cast<Music_list_iterator*>
102         (get_iterator_p ((Music*)rep->alternative_p_));  
103       here_mom_ += rep->repeat_p_->length_mom ();
104     }
105   else if (alternative_iter_p_)
106     {
107       assert (!alternative_iter_p_->ok ());
108       assert (!repeat_iter_p_);
109       delete alternative_iter_p_;
110       alternative_iter_p_ = 0;
111       if (unfold_i_ < 0)
112         unfold_i_ = rep->unfold_b_ ? 
113           rep->repeats_i_ - 1 : 0;
114       if (unfold_i_)
115         {
116           unfold_i_--;
117           repeat_iter_p_ = get_iterator_p (rep->repeat_p_);
118           // urg, assume same length alternatives for now...
119           //      here_mom_ += rep->alternative_p_->music_p_list_p_->top ()->length_mom ();
120           /*
121             URG
122             this is *wrong* but at least it doesn't dump core
123             when unfolding, the alternative (sequential) music 
124             shouldn't automatically move to the next alternative
125             
126             how to intercept this...
127           */
128           here_mom_ += rep->alternative_p_->length_mom ();
129         }
130     }
131
132   if ((!repeat_iter_p_ || !repeat_iter_p_->ok ())
133       && (!alternative_iter_p_ || !alternative_iter_p_->ok ()))
134     done_b_ = true;
135 }
136