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