]> git.donarmstrong.com Git - lilypond.git/blob - lily/metronome-engraver.cc
Break-alignable metronome marks.
[lilypond.git] / lily / metronome-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
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 #include <cctype>
21 using namespace std;
22
23 #include "engraver.hh"
24
25 #include "context.hh"
26 #include "duration.hh"
27 #include "grob-array.hh"
28 #include "item.hh"
29 #include "stream-event.hh"
30 #include "text-interface.hh"
31
32 #include "translator.icc"
33
34 class Metronome_mark_engraver : public Engraver
35 {
36 public:
37   TRANSLATOR_DECLARATIONS (Metronome_mark_engraver);
38 protected:
39   Item *text_;
40   Grob *bar_line_;
41
42   SCM last_duration_;
43   SCM last_count_;
44   SCM last_text_;
45
46   DECLARE_ACKNOWLEDGER (break_aligned);
47   DECLARE_ACKNOWLEDGER (break_alignment);
48   
49 protected:
50   virtual void derived_mark () const;
51   void stop_translation_timestep ();
52   void process_music ();
53 };
54
55 Metronome_mark_engraver::Metronome_mark_engraver ()
56 {
57   text_ = 0;
58   last_duration_ = SCM_EOL;
59   last_count_ = SCM_EOL;
60   last_text_ = SCM_EOL;
61 }
62
63 void
64 Metronome_mark_engraver::derived_mark () const
65 {
66   scm_gc_mark (last_count_);
67   scm_gc_mark (last_duration_);
68   scm_gc_mark (last_text_);
69 }
70
71 void
72 Metronome_mark_engraver::acknowledge_break_aligned (Grob_info inf)
73 {
74   Grob *s = inf.grob ();
75   if (text_
76       && !text_->get_parent (X_AXIS)
77       && dynamic_cast<Item *> (s)
78       && (s->get_property_data ("break-align-symbol")
79           == text_->get_property_data ("break-align-symbol")))
80     text_->set_parent (s, X_AXIS);
81 }
82
83 void
84 Metronome_mark_engraver::acknowledge_break_alignment (Grob_info inf)
85 {
86   Grob *s = inf.grob ();
87   if (text_
88       && dynamic_cast<Item *> (s))
89     {
90       text_->set_parent (s, X_AXIS);
91     }
92 }
93
94
95 void
96 Metronome_mark_engraver::stop_translation_timestep ()
97 {
98   if (text_)
99     {
100       /*
101         Problem: how to set musical columns as parent
102         when there's no breakable object of interest nearby?
103         We don't want metronome marks aligned to paper columns.
104        
105         Grob *mc = unsmob_grob (get_property ("currentMusicalColumn"));
106         text_->set_parent (mc, X_AXIS);
107       */
108       text_->set_object ("side-support-elements",
109                          grob_list_to_grob_array (get_property ("stavesFound")));
110       text_ = 0;
111     }
112 }
113
114 void
115 Metronome_mark_engraver::process_music ()
116 {
117   SCM count = get_property ("tempoUnitCount");
118   SCM duration = get_property ("tempoUnitDuration");
119   SCM text = get_property ("tempoText");
120
121   if ( ( (unsmob_duration (duration) && scm_is_number (count))
122         || Text_interface::is_markup (text) )
123       && !(ly_is_equal (count, last_count_)
124            && ly_is_equal (duration, last_duration_)
125            && ly_is_equal (text, last_text_)))
126     {
127       text_ = make_item ("MetronomeMark", SCM_EOL);
128
129       SCM proc = get_property ("metronomeMarkFormatter");
130       SCM result = scm_call_4 (proc,
131                                text,
132                                duration,
133                                count,
134                                context ()->self_scm ());
135
136       text_->set_property ("text", result);
137     }
138
139   last_duration_ = duration;
140   last_count_ = count;
141   last_text_ = text;
142 }
143
144
145
146 ADD_ACKNOWLEDGER (Metronome_mark_engraver, break_aligned);
147 ADD_ACKNOWLEDGER (Metronome_mark_engraver, break_alignment);
148
149 ADD_TRANSLATOR (Metronome_mark_engraver,
150                 /* doc */
151                 "Engrave metronome marking.  This delegates the formatting"
152                 " work to the function in the @code{metronomeMarkFormatter}"
153                 " property.  The mark is put over all staves.  The staves are"
154                 " taken from the @code{stavesFound} property, which is"
155                 " maintained by @ref{Staff_collecting_engraver}.",
156
157                 /* create */
158                 "MetronomeMark ",
159
160                 /* read */
161                 "stavesFound "
162                 "metronomeMarkFormatter "
163                 "tempoUnitDuration "
164                 "tempoUnitCount "
165                 "tempoText "
166                 "tempoHideNote ",
167
168                 /* write */
169                 ""
170                 );