]> git.donarmstrong.com Git - lilypond.git/blob - lily/folded-repeat-iterator.cc
b8b4e1603373f49aeffdb1517eaf7b71c15d5f9f
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "folded-repeat-iterator.hh"
11 #include "repeated-music.hh"
12 #include "music-list.hh"
13 #include "simultaneous-music-iterator.hh"
14 #include "translator-group.hh"
15
16 Folded_repeat_iterator::Folded_repeat_iterator ()
17 {
18   main_iter_p_ = 0;
19   alternative_iter_p_ = 0;
20 }
21
22 bool
23 Folded_repeat_iterator::ok () const
24 {
25   return main_iter_p_ || alternative_iter_p_;
26 }
27
28 Folded_repeat_iterator::~Folded_repeat_iterator ()
29 {
30   delete main_iter_p_;
31   delete alternative_iter_p_;
32 }
33
34 Moment
35 Folded_repeat_iterator::pending_moment () const
36 {
37   if (main_iter_p_)
38     {
39       return main_iter_p_->pending_moment ();
40     }
41   else
42     return main_length_mom_ + alternative_iter_p_->pending_moment ();
43 }
44
45 void
46 Folded_repeat_iterator::construct_children ()
47 {
48   Repeated_music  *  mus = dynamic_cast<Repeated_music*> (music_l_);
49   main_iter_p_ = get_iterator_p (mus->body ());
50   if (!main_iter_p_->ok())
51     {
52       leave_body ();
53       enter_alternative ();
54     }
55 }
56
57 void
58 Folded_repeat_iterator::process (Moment m)
59 {
60   if (!m)
61     {
62       bool success = try_music (music_l_);
63       if (!success)
64         music_l_->origin ()->warning ( _("no one to print a repeat brace"));
65     }
66   
67   if (main_iter_p_)
68     {
69       main_iter_p_->process (m);
70       if (!main_iter_p_->ok ())
71         leave_body ();
72     }
73
74   if (!main_iter_p_ && !alternative_iter_p_)
75     {
76       enter_alternative ();
77     }
78   
79   if (alternative_iter_p_)
80     {
81       alternative_iter_p_->process (m - main_length_mom_);
82       if (!alternative_iter_p_->ok ())
83         {
84           delete alternative_iter_p_;
85           alternative_iter_p_ =0;
86         }
87     }
88 }
89
90 void
91 Folded_repeat_iterator::leave_body ()
92 {
93   Repeated_music *  mus = dynamic_cast<Repeated_music *> (music_l_);
94   delete main_iter_p_;
95   main_iter_p_ = 0;
96   main_length_mom_ +=  mus->body ()->length_mom ();
97 }
98
99 void
100 Folded_repeat_iterator::enter_alternative ()
101 {
102   Repeated_music *  mus = dynamic_cast<Repeated_music *> (music_l_);  
103   if (mus->alternatives ())
104     {
105       Simultaneous_music_iterator * s = new Simultaneous_music_iterator;
106       s->separate_contexts_b_ = true;
107       s->init_translator (mus->alternatives (), report_to_l ());
108   
109       alternative_iter_p_ = s;
110       alternative_iter_p_->construct_children ();
111     }
112 }
113
114
115 Music_iterator*
116 Folded_repeat_iterator::try_music_in_children (Music * m) const
117 {
118   if (main_iter_p_)
119     {
120       return main_iter_p_->try_music (m);
121     }
122   if (alternative_iter_p_)
123     return alternative_iter_p_->try_music (m);
124   return 0;
125 }