]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-iterator.cc
Web-ja: update introduction
[lilypond.git] / lily / percent-repeat-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2015  Han-Wen Nienhuys <hanwen@xs4all.nl>
5                   Erik Sandberg <mandolaerik@gmail.com>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "context.hh"
22 #include "input.hh"
23 #include "repeated-music.hh"
24 #include "sequential-iterator.hh"
25 #include "lily-imports.hh"
26
27 class Percent_repeat_iterator : public Sequential_iterator
28 {
29 public:
30   DECLARE_CLASSNAME (Percent_repeat_iterator);
31   DECLARE_SCHEME_CALLBACK (constructor, ());
32   Percent_repeat_iterator ();
33 protected:
34   virtual SCM get_music_list () const;
35   virtual void construct_children ();
36   virtual void next_element (bool);
37   virtual void derived_mark () const;
38 private:
39   SCM child_list_;
40   int starting_bar_;
41 };
42
43 IMPLEMENT_CTOR_CALLBACK (Percent_repeat_iterator);
44
45 Percent_repeat_iterator::Percent_repeat_iterator ()
46   : child_list_ (SCM_UNDEFINED), starting_bar_ (-1)
47 {
48 }
49
50 void
51 Percent_repeat_iterator::derived_mark () const
52 {
53   scm_gc_mark (child_list_);
54   Sequential_iterator::derived_mark ();
55 }
56
57 void
58 Percent_repeat_iterator::construct_children ()
59 {
60   Music *mus = get_music ();
61
62   Music *child = Repeated_music::body (mus);
63   child_list_ = scm_list_1 (child->self_scm ());
64
65   Sequential_iterator::construct_children ();
66
67   descend_to_bottom_context ();
68   if (!measure_position (get_outlet ()).main_part_)
69     starting_bar_ =
70       robust_scm2int (get_outlet ()->get_property ("internalBarNumber"), 0);
71 }
72
73 SCM
74 Percent_repeat_iterator::get_music_list () const
75 {
76   return child_list_;
77 }
78
79 // Arrive here when child processing has been completed.  At that
80 // point of time, we can determine what kind of percent expression we
81 // are dealing with and extend the child list just in time before the
82 // next iterator is getting fetched.
83 void
84 Percent_repeat_iterator::next_element (bool side_effect)
85 {
86   // Do nothing if we already did our processing here.
87   if (!scm_is_pair (child_list_))
88     {
89       Sequential_iterator::next_element (side_effect);
90       return;
91     }
92
93   Music *mus = get_music ();
94
95   Music *child = Repeated_music::body (mus);
96   SCM length = child->get_length ().smobbed_copy ();
97
98   int current_bar = -1;
99   if (!measure_position (get_outlet ()).main_part_)
100     current_bar =
101       robust_scm2int (get_outlet ()->get_property ("internalBarNumber"), 0);
102
103   SCM child_list = SCM_EOL;
104
105   string event_type;
106   SCM slash_count = SCM_EOL;
107
108   if (starting_bar_ >= 0 && current_bar == starting_bar_ + 1)
109     event_type = "PercentEvent";
110   else if (starting_bar_ >=0 && current_bar == starting_bar_ + 2)
111     event_type = "DoublePercentEvent";
112   else
113     {
114       slash_count = Lily::calc_repeat_slash_count (child->self_scm ());
115       event_type = "RepeatSlashEvent";
116     }
117
118   int repeats = scm_to_int (mus->get_property ("repeat-count"));
119   for (int i = repeats; i > 1; i--)
120     {
121       Music *percent = make_music_by_name (ly_symbol2scm (event_type.c_str ()));
122       percent->set_spot (*mus->origin ());
123       percent->set_property ("length", length);
124       if (repeats > 1)
125         {
126           percent->set_property ("repeat-count", scm_from_int (i));
127           if (event_type == "RepeatSlashEvent")
128             percent->set_property ("slash-count", slash_count);
129         }
130
131       child_list = scm_cons (percent->unprotect (), child_list);
132     }
133
134   scm_set_cdr_x (child_list_, child_list);
135   child_list_ = SCM_EOL;
136
137   Sequential_iterator::next_element (side_effect);
138 }