]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-engraver.cc
add vcs lines to debian/control
[lilypond.git] / lily / percent-repeat-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>, Erik Sandberg <mandolaerik@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "engraver.hh"
22 #include "global-context.hh"
23 #include "international.hh"
24 #include "item.hh"
25 #include "side-position-interface.hh"
26 #include "spanner.hh"
27 #include "stream-event.hh"
28 #include "warn.hh"
29
30 #include "translator.icc"
31
32 class Percent_repeat_engraver : public Engraver
33 {
34   void typeset_perc ();
35
36 public:
37   TRANSLATOR_DECLARATIONS (Percent_repeat_engraver);
38   
39 protected:
40   Stream_event *percent_event_;
41
42   // moment (global time) where percent started
43   Moment start_mom_;
44   // moment (global time) where percent should end
45   Moment stop_mom_;
46
47   Spanner *percent_;
48   Spanner *percent_counter_;
49
50   Grob *first_command_column_;
51   Moment command_moment_;
52
53   virtual void finalize ();
54   DECLARE_TRANSLATOR_LISTENER (percent);
55
56   void start_translation_timestep ();
57   void stop_translation_timestep ();
58   void process_music ();
59 };
60
61 Percent_repeat_engraver::Percent_repeat_engraver ()
62 {
63   percent_ = 0;
64   percent_counter_ = 0;
65   percent_event_ = 0;
66
67   first_command_column_ = 0;
68   command_moment_ = Moment (-1);
69 }
70
71 void
72 Percent_repeat_engraver::start_translation_timestep ()
73 {
74   if (now_mom ().main_part_ != command_moment_.main_part_)
75     {
76       first_command_column_
77         = unsmob_grob (get_property ("currentCommandColumn"));
78       command_moment_ = now_mom ();
79     }
80
81   if (stop_mom_.main_part_ == now_mom ().main_part_)
82     {
83       if (percent_)
84         typeset_perc ();
85       percent_event_ = 0;
86     }
87 }
88
89 IMPLEMENT_TRANSLATOR_LISTENER (Percent_repeat_engraver, percent);
90 void
91 Percent_repeat_engraver::listen_percent (Stream_event *ev)
92 {
93   if (!percent_event_)
94     {
95       Moment body_length = get_event_length (ev);
96       start_mom_ = now_mom ();
97       stop_mom_ = now_mom () + body_length;
98       get_global_context ()->add_moment_to_process (stop_mom_);
99       percent_event_ = ev;
100     }
101   else
102     {
103       /*
104         print a warning: no assignment happens because
105         percent_event_ != 0
106       */
107       ASSIGN_EVENT_ONCE (percent_event_, ev);
108     }
109 }
110
111 void
112 Percent_repeat_engraver::process_music ()
113 {
114   if (percent_event_
115       && now_mom ().main_part_ == start_mom_.main_part_)
116     {
117       if (percent_)
118         typeset_perc ();
119
120       percent_ = make_spanner ("PercentRepeat", percent_event_->self_scm ());
121
122       Grob *col = first_command_column_;
123       percent_->set_bound (LEFT, col);
124
125       SCM count = percent_event_->get_property ("repeat-count");
126       if (count != SCM_EOL && to_boolean (get_property ("countPercentRepeats"))
127           && check_repeat_count_visibility (context (), count))
128         {
129           percent_counter_ = make_spanner ("PercentRepeatCounter",
130                                            percent_event_->self_scm ());
131
132           SCM text = scm_number_to_string (count, scm_from_int (10));
133           percent_counter_->set_property ("text", text);
134           percent_counter_->set_bound (LEFT, col);
135           Side_position_interface::add_support (percent_counter_, percent_);
136           percent_counter_->set_parent (percent_, Y_AXIS);
137         }
138       else
139         percent_counter_ = 0;
140     }
141 }
142
143 void
144 Percent_repeat_engraver::finalize ()
145 {
146   if (percent_)
147     {
148       percent_event_->origin ()->warning (_ ("unterminated percent repeat"));
149       percent_->suicide ();
150       percent_counter_->suicide ();
151     }
152 }
153
154 void
155 Percent_repeat_engraver::typeset_perc ()
156 {
157   Grob *col = first_command_column_;
158
159   percent_->set_bound (RIGHT, col);
160   percent_ = 0;
161
162   if (percent_counter_)
163     percent_counter_->set_bound (RIGHT, col);
164   percent_counter_ = 0;
165 }
166
167 void
168 Percent_repeat_engraver::stop_translation_timestep ()
169 {
170 }
171
172 ADD_TRANSLATOR (Percent_repeat_engraver,
173                 /* doc */
174                 "Make whole measure repeats.",
175                 
176                 /* create */
177                 "PercentRepeat "
178                 "PercentRepeatCounter ",
179
180                 /* read */
181                 "countPercentRepeats "
182                 "currentCommandColumn "
183                 "repeatCountVisibility ",
184
185                 /* write */
186                 ""
187                 );