]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-iterator.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / music-iterator.cc
1 /*
2   music-iterator.cc -- implement Music_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include "music-iterator.hh"
11
12 #include <cstdio>
13 using namespace std;
14
15 #include "warn.hh"
16 #include "context.hh"
17 #include "event-iterator.hh"
18 #include "input.hh"
19 #include "international.hh"
20 #include "music-wrapper.hh"
21 #include "music-wrapper-iterator.hh"
22 #include "simple-music-iterator.hh"
23
24 #include "ly-smobs.icc"
25
26 Music_iterator::Music_iterator ()
27 {
28   music_ = 0;
29   smobify_self ();
30 }
31
32 Music_iterator::Music_iterator (Music_iterator const &)
33 {
34   assert (false);
35 }
36
37 Music_iterator::~Music_iterator ()
38 {
39 }
40
41 Context *
42 Music_iterator::get_outlet () const
43 {
44   return handle_.get_outlet ();
45 }
46
47 void
48 Music_iterator::set_context (Context *trans)
49 {
50   handle_.set_context (trans);
51 }
52
53 void
54 Music_iterator::construct_children ()
55 {
56 }
57
58 Moment
59 Music_iterator::pending_moment () const
60 {
61   return 0;
62 }
63
64 void
65 Music_iterator::process (Moment)
66 {
67 }
68
69 bool
70 Music_iterator::ok () const
71 {
72   return false;
73 }
74
75 SCM
76 Music_iterator::get_static_get_iterator (Music *m)
77 {
78   Music_iterator *p = 0;
79
80   SCM ctor = m->get_property ("iterator-ctor");
81   SCM iter = SCM_EOL;
82   if (ly_is_procedure (ctor))
83     {
84       iter = scm_call_0 (ctor);
85       p = unsmob_iterator (iter);
86     }
87   else
88     {
89       if (dynamic_cast<Music_wrapper *> (m))
90         p = new Music_wrapper_iterator;
91       else if (m->is_mus_type ("event"))
92         p = new Event_iterator;
93       else
94         p = new Simple_music_iterator;
95
96       iter = p->self_scm ();
97       p->unprotect ();
98     }
99
100   p->music_ = m;
101   assert (m);
102   p->music_length_ = m->get_length ();
103   p->start_mom_ = m->start_mom ();
104
105   return iter;
106 }
107
108 Moment
109 Music_iterator::music_get_length () const
110 {
111   return music_length_;
112 }
113
114 Moment
115 Music_iterator::music_start_mom ()const
116 {
117   return start_mom_;
118 }
119
120 void
121 Music_iterator::init_translator (Music *m, Context *report)
122 {
123   music_ = m;
124   assert (m);
125   if (! get_outlet ())
126     set_context (report);
127 }
128
129 void
130 Music_iterator::substitute_outlet (Context *f, Context *t)
131 {
132   if (get_outlet () == f)
133     set_context (t);
134   derived_substitute (f, t);
135 }
136
137 void
138 Music_iterator::derived_substitute (Context *, Context *)
139 {
140 }
141
142 SCM
143 Music_iterator::get_iterator (Music *m) const
144 {
145   SCM ip = get_static_get_iterator (m);
146   Music_iterator *p = unsmob_iterator (ip);
147
148   p->init_translator (m, get_outlet ());
149
150   p->construct_children ();
151   return ip;
152 }
153
154 /* Descend to a bottom context; implicitly create a new one if necessary */
155 void
156 Music_iterator::descend_to_bottom_context ()
157 {
158   assert (get_outlet ());
159   if (!get_outlet ()->is_bottom_context ())
160     set_context (get_outlet ()->get_default_interpreter ());
161 }
162
163 void 
164 Music_iterator::report_event (Music *m)
165 {
166   descend_to_bottom_context ();
167
168   if (!m->is_mus_type ("event"))
169     m->origin ()->warning (_f ("Sending non-event to context"));
170
171   m->send_to_context (get_outlet ());
172 }
173
174 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
175
176 Music *
177 Music_iterator::get_music () const
178 {
179   return music_;
180 }
181
182 /****************************************************************/
183
184 IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
185 IMPLEMENT_SMOBS (Music_iterator);
186 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
187
188 SCM
189 Music_iterator::mark_smob (SCM smob)
190 {
191   Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
192
193   mus->derived_mark ();
194   /*
195     Careful with GC, although we intend the following as pointers
196     only, we _must_ mark them.
197   */
198   if (mus->get_outlet ())
199     scm_gc_mark (mus->get_outlet ()->self_scm ());
200   if (mus->music_)
201     scm_gc_mark (mus->music_->self_scm ());
202
203   return SCM_EOL;
204 }
205
206 int
207 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
208 {
209   char s[1000];
210
211   Music_iterator *iter = unsmob_iterator (sm);
212   sprintf (s, "#<%s>", iter->class_name ());
213   scm_puts (s, port);
214   return 1;
215 }
216
217 void
218 Music_iterator::derived_mark ()const
219 {
220 }
221
222 void
223 Music_iterator::quit ()
224 {
225   do_quit ();
226   handle_.set_context (0);
227 }
228
229 void
230 Music_iterator::do_quit ()
231 {
232 }
233
234 bool
235 Music_iterator::run_always ()const
236 {
237   return false;
238 }
239
240 bool
241 is_child_context (Context *me, Context *child)
242 {
243   while (child && child != me)
244     child = child->get_parent_context ();
245
246   return child == me;
247 }
248
249 /*
250   move to context of child iterator if it is deeper down in the
251   hierarchy.
252 */
253 void
254 Music_iterator::descend_to_child (Context *child_report)
255 {
256   Context *me_report = get_outlet ();
257   if (is_child_context (me_report, child_report))
258     set_context (child_report);
259 }