]> git.donarmstrong.com Git - lilypond.git/blob - lily/folded-repeat-iterator.cc
016db26f4059b170b7b671038b8c99f7cab512d6
[lilypond.git] / lily / folded-repeat-iterator.cc
1 /*   
2   repeated-music-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
34 Folded_repeat_iterator::~Folded_repeat_iterator ()
35 {
36   delete main_iter_;
37   delete alternative_iter_;
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 }
47
48 Moment
49 Folded_repeat_iterator::pending_moment () const
50 {
51   if (main_iter_)
52     {
53       return main_iter_->pending_moment ();
54     }
55   else
56     return main_length_mom_ + alternative_iter_->pending_moment ();
57 }
58
59 void
60 Folded_repeat_iterator::construct_children ()
61 {
62   Repeated_music  *  mus = dynamic_cast<Repeated_music*> (get_music ());
63   main_iter_ = get_iterator (mus->body ());
64   if (!main_iter_->ok ())
65     {
66      leave_body ();
67       enter_alternative ();
68     }
69 }
70
71 void
72 Folded_repeat_iterator::process (Moment m)
73 {
74   if (!m.to_bool () )
75     {
76       bool success = try_music (get_music ());
77       if (!success)
78         get_music ()->origin ()->warning (_ ("no one to print a repeat brace"));
79     }
80   
81   if (main_iter_)
82     {
83       main_iter_->process (m);
84       if (!main_iter_->ok ())
85         leave_body ();
86     }
87
88   if (!main_iter_ && !alternative_iter_)
89     {
90       enter_alternative ();
91     }
92   
93   if (alternative_iter_)
94     {
95       alternative_iter_->process (m - main_length_mom_);
96       if (!alternative_iter_->ok ())
97         {
98           delete alternative_iter_;
99           alternative_iter_ =0;
100         }
101     }
102 }
103
104 void
105 Folded_repeat_iterator::leave_body ()
106 {
107   Repeated_music *  mus = dynamic_cast<Repeated_music *> (get_music ());
108   delete main_iter_;
109   main_iter_ = 0;
110   main_length_mom_ +=  mus->body ()->length_mom ();
111 }
112
113 void
114 Folded_repeat_iterator::enter_alternative ()
115 {
116   Repeated_music *  mus = dynamic_cast<Repeated_music *> (get_music ());  
117   if (mus->alternatives ())
118     {
119       Simultaneous_music_iterator * s = new Simultaneous_music_iterator;
120       s->separate_contexts_b_ = true;
121       s->init_translator (mus, report_to ());
122       
123       alternative_iter_ = s;
124       alternative_iter_->construct_children ();
125     }
126 }
127
128
129 Music_iterator*
130 Folded_repeat_iterator::try_music_in_children (Music * m) const
131 {
132   if (main_iter_)
133     {
134       return main_iter_->try_music (m);
135     }
136   if (alternative_iter_)
137     return alternative_iter_->try_music (m);
138   return 0;
139 }
140
141 IMPLEMENT_CTOR_CALLBACK (Folded_repeat_iterator);