]> git.donarmstrong.com Git - lilypond.git/blob - lily/folded-repeat-iterator.cc
(Invoking lilypond-book):
[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 (main_iter_)
60     {
61       main_iter_->process (m);
62       if (!main_iter_->ok ())
63         leave_body ();
64     }
65
66   if (!main_iter_ && !alternative_iter_)
67     enter_alternative ();
68
69   if (alternative_iter_)
70     {
71       alternative_iter_->process (m - main_length_mom_);
72       if (!alternative_iter_->ok ())
73         {
74           alternative_iter_->quit ();
75           alternative_iter_ = 0;
76         }
77     }
78 }
79
80 void
81 Folded_repeat_iterator::leave_body ()
82 {
83   Music *mus = get_music ();
84
85   main_iter_->quit ();
86   main_iter_ = 0;
87   main_length_mom_ += Repeated_music::body (mus)->get_length ();
88 }
89
90 void
91 Folded_repeat_iterator::enter_alternative ()
92 {
93   Music *mus = get_music ();
94   if (scm_is_pair (Repeated_music::alternatives (mus)))
95     {
96       /*
97         ugh.
98       */
99       Simultaneous_music_iterator *s = new Simultaneous_music_iterator;
100       s->create_separate_contexts_ = true;
101       s->init_context (mus, get_outlet ());
102
103       alternative_iter_ = s;
104       alternative_iter_->construct_children ();
105
106       s->unprotect ();
107     }
108 }
109
110 void
111 Folded_repeat_iterator::derived_mark () const
112 {
113   if (main_iter_)
114     scm_gc_mark (main_iter_->self_scm ());
115   if (alternative_iter_)
116     scm_gc_mark (alternative_iter_->self_scm ());
117 }
118
119 void
120 Folded_repeat_iterator::derived_substitute (Context *f, Context *t)
121 {
122   if (main_iter_)
123     main_iter_->substitute_outlet (f, t);
124   if (alternative_iter_)
125     alternative_iter_->substitute_outlet (f, t);
126 }
127
128 IMPLEMENT_CTOR_CALLBACK (Folded_repeat_iterator);