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