]> git.donarmstrong.com Git - lilypond.git/blob - lily/folded-repeat-iterator.cc
17ef87275e1dc2d17342e8326790760253c9321d
[lilypond.git] / lily / folded-repeat-iterator.cc
1 /*
2   folded-repeat-iterator.cc -- implement Folded_repeat_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "folded-repeat-iterator.hh"
10
11 #include "input.hh"
12 #include "repeated-music.hh"
13 #include "simultaneous-music-iterator.hh"
14 #include "context.hh"
15
16 Folded_repeat_iterator::Folded_repeat_iterator ()
17 {
18   main_iter_ = 0;
19   alternative_iter_ = 0;
20 }
21
22 bool
23 Folded_repeat_iterator::ok () const
24 {
25   return main_iter_ || alternative_iter_;
26 }
27 void
28 Folded_repeat_iterator::do_quit ()
29 {
30   if (main_iter_)main_iter_->quit ();
31   if (alternative_iter_)alternative_iter_->quit ();
32 }
33
34 Moment
35 Folded_repeat_iterator::pending_moment () const
36 {
37   if (main_iter_)
38     return main_iter_->pending_moment ();
39   else
40     return main_length_mom_ + alternative_iter_->pending_moment ();
41 }
42
43 void
44 Folded_repeat_iterator::construct_children ()
45 {
46   Music *mus = get_music ();
47   main_iter_ = unsmob_iterator (get_iterator (Repeated_music::body (mus)));
48   if (!main_iter_->ok ())
49     {
50       leave_body ();
51       enter_alternative ();
52     }
53 }
54
55 void
56 Folded_repeat_iterator::process (Moment m)
57 {
58   if (!m.to_bool ())
59     {
60       bool success = try_music (get_music ());
61       if (!success)
62         get_music ()->origin ()->warning (_ ("no one to print a repeat brace"));
63     }
64
65   if (main_iter_)
66     {
67       main_iter_->process (m);
68       if (!main_iter_->ok ())
69         leave_body ();
70     }
71
72   if (!main_iter_ && !alternative_iter_)
73     enter_alternative ();
74
75   if (alternative_iter_)
76     {
77       alternative_iter_->process (m - main_length_mom_);
78       if (!alternative_iter_->ok ())
79         {
80           alternative_iter_->quit ();
81           alternative_iter_ = 0;
82         }
83     }
84 }
85
86 void
87 Folded_repeat_iterator::leave_body ()
88 {
89   Music *mus = get_music ();
90
91   main_iter_->quit ();
92   main_iter_ = 0;
93   main_length_mom_ += Repeated_music::body (mus)->get_length ();
94 }
95
96 void
97 Folded_repeat_iterator::enter_alternative ()
98 {
99   Music *mus = get_music ();
100   if (scm_is_pair (Repeated_music::alternatives (mus)))
101     {
102       /*
103         ugh.
104       */
105       Simultaneous_music_iterator *s = new Simultaneous_music_iterator;
106       s->create_separate_contexts_ = true;
107       s->init_translator (mus, get_outlet ());
108
109       alternative_iter_ = s;
110       alternative_iter_->construct_children ();
111
112       s->unprotect ();
113     }
114 }
115
116 Music_iterator *
117 Folded_repeat_iterator::try_music_in_children (Music *m) const
118 {
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;
124 }
125 void
126 Folded_repeat_iterator::derived_mark () const
127 {
128   if (main_iter_)
129     scm_gc_mark (main_iter_->self_scm ());
130   if (alternative_iter_)
131     scm_gc_mark (alternative_iter_->self_scm ());
132 }
133
134 void
135 Folded_repeat_iterator::derived_substitute (Context *f, Context *t)
136 {
137   if (main_iter_)
138     main_iter_->substitute_outlet (f, t);
139   if (alternative_iter_)
140     alternative_iter_->substitute_outlet (f, t);
141 }
142
143 IMPLEMENT_CTOR_CALLBACK (Folded_repeat_iterator);