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