* The grand 2005-2006 replace.
[lilypond/patrick.git] / lily / folded-repeat-iterator.cc
blob51df070893d32e6de10766a332d3fb9caffc1873
1 /*
2 folded-repeat-iterator.cc -- implement Folded_repeat_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "folded-repeat-iterator.hh"
11 #include "input.hh"
12 #include "repeated-music.hh"
13 #include "simultaneous-music-iterator.hh"
14 #include "context.hh"
16 Folded_repeat_iterator::Folded_repeat_iterator ()
18 main_iter_ = 0;
19 alternative_iter_ = 0;
22 bool
23 Folded_repeat_iterator::ok () const
25 return main_iter_ || alternative_iter_;
27 void
28 Folded_repeat_iterator::do_quit ()
30 if (main_iter_)main_iter_->quit ();
31 if (alternative_iter_)alternative_iter_->quit ();
34 Moment
35 Folded_repeat_iterator::pending_moment () const
37 if (main_iter_)
38 return main_iter_->pending_moment ();
39 else
40 return main_length_mom_ + alternative_iter_->pending_moment ();
43 void
44 Folded_repeat_iterator::construct_children ()
46 Music *mus = get_music ();
47 main_iter_ = unsmob_iterator (get_iterator (Repeated_music::body (mus)));
48 if (!main_iter_->ok ())
50 leave_body ();
51 enter_alternative ();
55 void
56 Folded_repeat_iterator::process (Moment m)
58 if (!m.to_bool ())
60 bool success = try_music (get_music ());
61 if (!success)
62 get_music ()->origin ()->warning (_ ("no one to print a repeat brace"));
65 if (main_iter_)
67 main_iter_->process (m);
68 if (!main_iter_->ok ())
69 leave_body ();
72 if (!main_iter_ && !alternative_iter_)
73 enter_alternative ();
75 if (alternative_iter_)
77 alternative_iter_->process (m - main_length_mom_);
78 if (!alternative_iter_->ok ())
80 alternative_iter_->quit ();
81 alternative_iter_ = 0;
86 void
87 Folded_repeat_iterator::leave_body ()
89 Music *mus = get_music ();
91 main_iter_->quit ();
92 main_iter_ = 0;
93 main_length_mom_ += Repeated_music::body (mus)->get_length ();
96 void
97 Folded_repeat_iterator::enter_alternative ()
99 Music *mus = get_music ();
100 if (scm_is_pair (Repeated_music::alternatives (mus)))
103 ugh.
105 Simultaneous_music_iterator *s = new Simultaneous_music_iterator;
106 s->create_separate_contexts_ = true;
107 s->init_translator (mus, get_outlet ());
109 alternative_iter_ = s;
110 alternative_iter_->construct_children ();
112 s->unprotect ();
116 Music_iterator *
117 Folded_repeat_iterator::try_music_in_children (Music *m) const
119 if (main_iter_)
120 return main_iter_->try_music (m);
121 if (alternative_iter_)
122 return alternative_iter_->try_music (m);
123 return 0;
125 void
126 Folded_repeat_iterator::derived_mark () const
128 if (main_iter_)
129 scm_gc_mark (main_iter_->self_scm ());
130 if (alternative_iter_)
131 scm_gc_mark (alternative_iter_->self_scm ());
134 void
135 Folded_repeat_iterator::derived_substitute (Context *f, Context *t)
137 if (main_iter_)
138 main_iter_->substitute_outlet (f, t);
139 if (alternative_iter_)
140 alternative_iter_->substitute_outlet (f, t);
143 IMPLEMENT_CTOR_CALLBACK (Folded_repeat_iterator);