]> git.donarmstrong.com Git - lilypond.git/blob - lily/folded-repeat-iterator.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 /*
12    Folded repeats are a stupid idea at this point, so we refrain from
13    implementing get_pending_events () and skip ().
14 */
15
16 #include "folded-repeat-iterator.hh"
17 #include "repeated-music.hh"
18 #include "music-list.hh"
19 #include "simultaneous-music-iterator.hh"
20 #include "translator-group.hh"
21
22 Folded_repeat_iterator::Folded_repeat_iterator ()
23 {
24   main_iter_ = 0;
25   alternative_iter_ = 0;
26 }
27
28 bool
29 Folded_repeat_iterator::ok () const
30 {
31   return main_iter_ || alternative_iter_;
32 }
33 void
34 Folded_repeat_iterator::do_quit()
35 {
36   if (main_iter_)main_iter_->quit();
37   if (alternative_iter_)alternative_iter_->quit();
38 }
39
40 Folded_repeat_iterator::Folded_repeat_iterator (Folded_repeat_iterator const &src)
41   : Music_iterator (src)
42 {
43   main_iter_ = src.main_iter_ ? src.main_iter_->clone () : 0;
44   alternative_iter_ = src.alternative_iter_ ? src.alternative_iter_->clone () : 0;
45   main_length_mom_ = src.main_length_mom_;
46   if (main_iter_)
47     scm_gc_unprotect_object (main_iter_->self_scm());
48   if (alternative_iter_)
49     scm_gc_unprotect_object (alternative_iter_->self_scm());
50 }
51
52 Moment
53 Folded_repeat_iterator::pending_moment () const
54 {
55   if (main_iter_)
56     {
57       return main_iter_->pending_moment ();
58     }
59   else
60     return main_length_mom_ + alternative_iter_->pending_moment ();
61 }
62
63 void
64 Folded_repeat_iterator::construct_children ()
65 {
66   Repeated_music  *  mus = dynamic_cast<Repeated_music*> (get_music ());
67   main_iter_ = unsmob_iterator (get_iterator (mus->body ()));
68   if (!main_iter_->ok ())
69     {
70       leave_body ();
71       enter_alternative ();
72     }
73 }
74
75 void
76 Folded_repeat_iterator::process (Moment m)
77 {
78   if (!m.to_bool () )
79     {
80       bool success = try_music (get_music ());
81       if (!success)
82         get_music ()->origin ()->warning (_ ("no one to print a repeat brace"));
83     }
84   
85   if (main_iter_)
86     {
87       main_iter_->process (m);
88       if (!main_iter_->ok ())
89         leave_body ();
90     }
91
92   if (!main_iter_ && !alternative_iter_)
93     {
94       enter_alternative ();
95     }
96   
97   if (alternative_iter_)
98     {
99       alternative_iter_->process (m - main_length_mom_);
100       if (!alternative_iter_->ok ())
101         {
102           alternative_iter_->quit();
103           alternative_iter_ =0;
104         }
105     }
106 }
107
108 void
109 Folded_repeat_iterator::leave_body ()
110 {
111   Repeated_music *  mus = dynamic_cast<Repeated_music *> (get_music ());
112
113   main_iter_->quit ();
114   main_iter_ = 0;
115   main_length_mom_ +=  mus->body ()->length_mom ();
116 }
117
118 void
119 Folded_repeat_iterator::enter_alternative ()
120 {
121   Repeated_music *  mus = dynamic_cast<Repeated_music *> (get_music ());  
122   if (mus->alternatives ())
123     {
124   /*
125     ugh.
126    */ 
127       Simultaneous_music_iterator * s = new Simultaneous_music_iterator;
128       s->separate_contexts_b_ = true;
129       s->init_translator (mus, report_to ());
130       
131       alternative_iter_ = s;
132       alternative_iter_->construct_children ();
133
134       scm_gc_unprotect_object (s->self_scm());
135     }
136 }
137
138
139 Music_iterator*
140 Folded_repeat_iterator::try_music_in_children (Music * m) const
141 {
142   if (main_iter_)
143     {
144       return main_iter_->try_music (m);
145     }
146   if (alternative_iter_)
147     return alternative_iter_->try_music (m);
148   return 0;
149 }
150 void
151 Folded_repeat_iterator::derived_mark()const
152 {
153   if (main_iter_)
154     scm_gc_mark (main_iter_->self_scm());
155   if (alternative_iter_)
156     scm_gc_mark (alternative_iter_->self_scm());
157 }
158 IMPLEMENT_CTOR_CALLBACK (Folded_repeat_iterator);