]> git.donarmstrong.com Git - lilypond.git/blob - lily/sequential-music-iterator.cc
8ba2fa20db418aeca4c9880c98fae5f94deec8f3
[lilypond.git] / lily / sequential-music-iterator.cc
1 /*
2   Sequential_music_iterator.cc -- implement Sequential_music_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "grace-iterator.hh"
9 #include "translator-group.hh"
10 #include "debug.hh"
11 #include "sequential-music-iterator.hh"
12 #include "music-list.hh"
13 #include "request-chord-iterator.hh"
14
15 Sequential_music_iterator::Sequential_music_iterator ()
16 {
17   cursor_ = 0;
18   here_mom_ = 0;
19   iter_p_ =0;
20 }
21
22 Sequential_music_iterator::Sequential_music_iterator (Sequential_music_iterator const &src)
23   : Music_iterator (src)
24 {
25   cursor_ = src.cursor_;
26   here_mom_ = src.here_mom_;
27   iter_p_ = src.iter_p_->clone ();
28 }
29
30 Sequential_music_iterator::~Sequential_music_iterator()
31 {
32   if (iter_p_)
33     {
34       if (iter_p_->ok ())
35         music_l_->origin ()->warning (_ ("Must stop before this music ends"));
36       delete iter_p_;
37       iter_p_ = 0;
38     }
39 }
40
41 void
42 Sequential_music_iterator::do_print() const
43 {
44   if (iter_p_)
45     iter_p_->print();
46 }
47
48 void
49 Sequential_music_iterator::construct_children()
50 {
51   cursor_ = dynamic_cast<Music_sequence const*> (music_l_)->music_list ();
52   
53   while (gh_pair_p (cursor_ ))
54     {
55       start_next_element();
56       if (!iter_p_->ok()) 
57         {
58           leave_element();
59         }
60       else 
61         {
62           set_sequential_music_translator();
63           break;
64         }
65     }
66 }
67
68 void 
69 Sequential_music_iterator::leave_element()
70 {
71   delete iter_p_;
72   iter_p_ =0;
73   Moment elt_time = unsmob_music (gh_car (cursor_))->length_mom ();
74   here_mom_ += elt_time;
75   cursor_ =gh_cdr (cursor_);
76 }
77
78 void
79 Sequential_music_iterator::start_next_element()
80 {
81   assert (!iter_p_);
82   iter_p_ = get_iterator_p (unsmob_music (gh_car (cursor_)));
83 }
84
85 void
86 Sequential_music_iterator::set_sequential_music_translator()
87 {
88   Translator_group  * child_report = child_report = iter_p_->report_to_l ();
89   if (dynamic_cast<Grace_iterator*> (iter_p_))
90     child_report = child_report->daddy_trans_l_;
91     
92   if (report_to_l()->depth_i () < child_report->depth_i ())
93     set_translator (child_report);
94 }
95
96 Music*
97 Sequential_music_iterator::get_music ()
98 {
99   if (ok ())
100     return unsmob_music (gh_car (cursor_));
101       
102   return 0;
103 }
104   
105 bool
106 Sequential_music_iterator::next ()
107 {
108   if (ok ())
109     {
110       bool b = false;
111       if (iter_p_->ok ())
112         b = iter_p_->next ();
113       if (!b)
114         {
115           set_sequential_music_translator ();
116           leave_element ();
117           if (gh_pair_p (cursor_))
118             start_next_element ();
119           b = ok ();
120         }
121       return b;
122     }
123   return false;
124 }
125
126 /*
127   This should use get_music () and next ()
128  */
129 void
130 Sequential_music_iterator::do_process_and_next (Moment until)
131 {
132   if (ok ())
133     {
134       while (1) 
135         {
136           Moment local_until = until - here_mom_;
137           while (iter_p_->ok ()) 
138             {
139               Moment here = iter_p_->next_moment ();
140               if (here != local_until)
141                 return Music_iterator::do_process_and_next (until);
142               
143               iter_p_->process_and_next (local_until);
144             }
145           
146           if (!iter_p_->ok ()) 
147             {
148               set_sequential_music_translator ();
149               leave_element ();
150               
151               if (gh_pair_p (cursor_))
152                 start_next_element ();
153               else 
154                 return Music_iterator::do_process_and_next (until);
155             }
156         }
157     }
158 }
159
160 Moment
161 Sequential_music_iterator::next_moment() const
162 {
163   return iter_p_->next_moment() + here_mom_;
164 }
165
166
167 bool
168 Sequential_music_iterator::ok() const
169 {
170   return iter_p_;
171 }
172
173 Music_iterator*
174 Sequential_music_iterator::try_music_in_children (Music *m) const
175
176   return iter_p_ ? iter_p_->try_music (m) : 0;
177 }