]> git.donarmstrong.com Git - lilypond.git/blob - lily/unfolded-repeat-iterator.cc
* flower
[lilypond.git] / lily / unfolded-repeat-iterator.cc
1 /*
2   unfolded-repeat-iterator.cc -- implement Unfolded_repeat_iterator, Volta_repeat_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6
7   (c) 2002--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "music.hh"
11 #include "sequential-iterator.hh"
12 #include "context.hh"
13
14 class Unfolded_repeat_iterator : public Sequential_iterator
15 {
16 public:
17   DECLARE_SCHEME_CALLBACK (constructor, ());
18 protected:
19   virtual SCM get_music_list () const;
20 };
21
22 SCM
23 Unfolded_repeat_iterator::get_music_list () const
24 {
25   SCM l = SCM_EOL;
26   SCM *tail = &l;
27
28   SCM body = get_music ()->get_property ("element");
29   SCM alts = get_music ()->get_property ("elements");
30   int alt_count = scm_ilength (alts);
31   int rep_count = scm_to_int (get_music ()->get_property ("repeat-count"));
32
33   for (int i = 0; i < rep_count; i++)
34     {
35       if (unsmob_music (body))
36         *tail = scm_cons (body, SCM_EOL);
37
38       tail = SCM_CDRLOC (*tail);
39
40       if (alt_count)
41         {
42           *tail = scm_cons (scm_car (alts), SCM_EOL);
43           tail = SCM_CDRLOC (*tail);
44           if (i >= rep_count - alt_count)
45
46             alts = scm_cdr (alts);
47         }
48     }
49
50   return l;
51 }
52
53 class Volta_repeat_iterator : public Sequential_iterator
54 {
55 public:
56   DECLARE_SCHEME_CALLBACK (constructor, ());
57   Volta_repeat_iterator ();
58
59   void add_repeat_command (SCM);
60 protected:
61   virtual SCM get_music_list () const;
62   virtual void next_element (bool);
63   virtual void construct_children ();
64   virtual void process (Moment);
65
66   bool first_time_;
67   int alt_count_;
68   int rep_count_;
69   int done_count_;
70 };
71
72 Volta_repeat_iterator::Volta_repeat_iterator ()
73 {
74   done_count_ = alt_count_ = rep_count_ = 0;
75   first_time_ = true;
76 }
77
78 SCM
79 Volta_repeat_iterator::get_music_list ()const
80 {
81   return scm_cons (get_music ()->get_property ("element"),
82                    get_music ()->get_property ("elements"));
83 }
84
85 void
86 Volta_repeat_iterator::construct_children ()
87 {
88   Sequential_iterator::construct_children ();
89
90   SCM alts = get_music ()->get_property ("elements");
91
92   alt_count_ = scm_ilength (alts);
93   rep_count_ = scm_to_int (get_music ()->get_property ("repeat-count"));
94   done_count_ = 0;
95 }
96
97 /*
98   TODO: add source information for debugging
99 */
100 void
101 Volta_repeat_iterator::add_repeat_command (SCM what)
102 {
103   SCM reps = ly_symbol2scm ("repeatCommands");
104   SCM current_reps = get_outlet ()->internal_get_property (reps);
105
106   Context *where = get_outlet ()->where_defined (reps);
107   if (where
108       && current_reps == SCM_EOL || scm_is_pair (current_reps))
109     {
110       current_reps = scm_cons (what, current_reps);
111       where->internal_set_property (reps, current_reps);
112     }
113 }
114
115 void
116 Volta_repeat_iterator::next_element (bool side_effect)
117 {
118   done_count_++;
119
120   Sequential_iterator::next_element (side_effect);
121
122   if (side_effect)
123     {
124       if (alt_count_)
125         {
126           String repstr = to_string (rep_count_ - alt_count_ + done_count_) + ".";
127           if (done_count_ > 1)
128             {
129               add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
130
131               if (done_count_ - 1 < alt_count_)
132                 add_repeat_command (ly_symbol2scm ("end-repeat"));
133             }
134
135           if (done_count_ == 1 && alt_count_ < rep_count_)
136             {
137               repstr = "1.--" + to_string (rep_count_ - alt_count_ + done_count_) + ".";
138             }
139
140           if (done_count_ <= alt_count_)
141             add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
142                                             scm_makfrom0str (repstr.to_str0 ()), SCM_UNDEFINED));
143         }
144       else
145         {
146           add_repeat_command (ly_symbol2scm ("end-repeat"));
147         }
148     }
149 }
150
151 void
152 Volta_repeat_iterator::process (Moment m)
153 {
154   if (first_time_)
155     {
156       add_repeat_command (ly_symbol2scm ("start-repeat"));
157       first_time_ = false;
158     }
159   Sequential_iterator::process (m);
160 }
161
162 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);
163 IMPLEMENT_CTOR_CALLBACK (Unfolded_repeat_iterator);