]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-repeat-iterator.cc
* scm/define-music-types.scm (music-descriptions): don't use
[lilypond.git] / lily / volta-repeat-iterator.cc
1 /*
2   volta-repeat-iterator.cc -- implement 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
11 #include "music.hh"
12 #include "sequential-iterator.hh"
13 #include "context.hh"
14
15 class Volta_repeat_iterator : public Sequential_iterator
16 {
17 public:
18   DECLARE_SCHEME_CALLBACK (constructor, ());
19   Volta_repeat_iterator ();
20
21   void add_repeat_command (SCM);
22 protected:
23   virtual SCM get_music_list () const;
24   virtual void next_element (bool);
25   virtual void construct_children ();
26   virtual void process (Moment);
27
28   bool first_time_;
29   int alt_count_;
30   int rep_count_;
31   int done_count_;
32 };
33
34 Volta_repeat_iterator::Volta_repeat_iterator ()
35 {
36   done_count_ = alt_count_ = rep_count_ = 0;
37   first_time_ = true;
38 }
39
40 SCM
41 Volta_repeat_iterator::get_music_list ()const
42 {
43   return scm_cons (get_music ()->get_property ("element"),
44                    get_music ()->get_property ("elements"));
45 }
46
47 void
48 Volta_repeat_iterator::construct_children ()
49 {
50   Sequential_iterator::construct_children ();
51
52   SCM alts = get_music ()->get_property ("elements");
53
54   alt_count_ = scm_ilength (alts);
55   rep_count_ = scm_to_int (get_music ()->get_property ("repeat-count"));
56   done_count_ = 0;
57 }
58
59 /*
60   TODO: add source information for debugging
61 */
62 void
63 Volta_repeat_iterator::add_repeat_command (SCM what)
64 {
65   SCM reps = ly_symbol2scm ("repeatCommands");
66   SCM current_reps = get_outlet ()->internal_get_property (reps);
67
68   Context *where = get_outlet ()->where_defined (reps);
69   if (where
70       && current_reps == SCM_EOL || scm_is_pair (current_reps))
71     {
72       current_reps = scm_cons (what, current_reps);
73       where->internal_set_property (reps, current_reps);
74     }
75 }
76
77 void
78 Volta_repeat_iterator::next_element (bool side_effect)
79 {
80   done_count_++;
81
82   Sequential_iterator::next_element (side_effect);
83
84   if (side_effect)
85     {
86       if (alt_count_)
87         {
88           String repstr = to_string (rep_count_ - alt_count_ + done_count_) + ".";
89           if (done_count_ > 1)
90             {
91               add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
92
93               if (done_count_ - 1 < alt_count_)
94                 add_repeat_command (ly_symbol2scm ("end-repeat"));
95             }
96
97           if (done_count_ == 1 && alt_count_ < rep_count_)
98             {
99               repstr = "1.--" + to_string (rep_count_ - alt_count_ + done_count_) + ".";
100             }
101
102           if (done_count_ <= alt_count_)
103             add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
104                                             scm_makfrom0str (repstr.to_str0 ()), SCM_UNDEFINED));
105         }
106       else
107         {
108           add_repeat_command (ly_symbol2scm ("end-repeat"));
109         }
110     }
111 }
112
113 void
114 Volta_repeat_iterator::process (Moment m)
115 {
116   if (first_time_)
117     {
118       add_repeat_command (ly_symbol2scm ("start-repeat"));
119       first_time_ = false;
120     }
121   Sequential_iterator::process (m);
122 }
123
124 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);