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