]> git.donarmstrong.com Git - lilypond.git/blob - lily/mark-engraver.cc
25b19bd18a607b2ae34e50e9c7e677242dbcb922
[lilypond.git] / lily / mark-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 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 "axis-group-interface.hh"
26 #include "context.hh"
27 #include "grob-array.hh"
28 #include "international.hh"
29 #include "item.hh"
30 #include "stream-event.hh"
31 #include "text-interface.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 /**
37    put stuff over or next to  bars.  Examples: bar numbers, marginal notes,
38    rehearsal marks.
39 */
40 class Mark_engraver : public Engraver
41 {
42
43   void create_items (Stream_event *);
44   Item *text_;
45   Item *final_text_;
46   Stream_event *mark_ev_;
47
48 public:
49   TRANSLATOR_DECLARATIONS (Mark_engraver);
50
51 protected:
52   void process_music ();
53   void start_translation_timestep ();
54   void stop_translation_timestep ();
55   virtual void finalize ();
56
57   DECLARE_TRANSLATOR_LISTENER (mark);
58   DECLARE_ACKNOWLEDGER (break_alignment);
59 };
60
61 Mark_engraver::Mark_engraver ()
62 {
63   text_ = 0;
64   final_text_ = 0;
65   mark_ev_ = 0;
66 }
67
68 void
69 Mark_engraver::acknowledge_break_alignment (Grob_info inf)
70 {
71   Grob *s = inf.grob ();
72   if (text_
73       && dynamic_cast<Item *> (s))
74     text_->set_parent (s, X_AXIS);
75 }
76
77 void
78 Mark_engraver::start_translation_timestep ()
79 {
80   final_text_ = 0;
81 }
82
83 void
84 Mark_engraver::stop_translation_timestep ()
85 {
86   if (text_)
87     {
88       text_->set_object ("side-support-elements",
89                          grob_list_to_grob_array (get_property ("stavesFound")));
90       final_text_ = text_;
91       text_ = 0;
92     }
93   mark_ev_ = 0;
94 }
95
96 void
97 Mark_engraver::finalize ()
98 {
99   if (final_text_)
100     final_text_->set_property ("break-visibility",
101                                scm_c_make_vector (3, SCM_BOOL_T));
102   final_text_ = 0;
103 }
104
105 void
106 Mark_engraver::create_items (Stream_event *ev)
107 {
108   if (text_)
109     return;
110
111   text_ = make_item ("RehearsalMark", ev->self_scm ());
112 }
113
114 IMPLEMENT_TRANSLATOR_LISTENER (Mark_engraver, mark);
115 void
116 Mark_engraver::listen_mark (Stream_event *ev)
117 {
118   ASSIGN_EVENT_ONCE (mark_ev_, ev);
119 }
120
121 /*
122   TODO: make the increment function in Scheme.
123 */
124 void
125 Mark_engraver::process_music ()
126 {
127   if (mark_ev_)
128     {
129       create_items (mark_ev_);
130
131       /*
132         automatic marks.
133       */
134
135       SCM m = mark_ev_->get_property ("label");
136       SCM proc = get_property ("markFormatter");
137       if (!Text_interface::is_markup (m)
138           && ly_is_procedure (proc))
139         {
140           if (!scm_is_number (m))
141             m = get_property ("rehearsalMark");
142
143           if (scm_integer_p (m) == SCM_BOOL_T
144               && scm_exact_p (m) == SCM_BOOL_T)
145             {
146               int mark_count = scm_to_int (m);
147               mark_count++;
148               context ()->set_property ("rehearsalMark",
149                                         scm_from_int (mark_count));
150             }
151
152           if (scm_is_number (m))
153             m = scm_call_2 (proc, m, context ()->self_scm ());
154           else
155             /* FIXME: constant error message.  */
156             warning (_ ("rehearsalMark must have integer value"));
157         }
158
159       if (Text_interface::is_markup (m))
160         text_->set_property ("text", m);
161       else
162         warning (_ ("mark label must be a markup object"));
163     }
164 }
165
166 ADD_ACKNOWLEDGER (Mark_engraver, break_alignment);
167
168 ADD_TRANSLATOR (Mark_engraver,
169                 /* doc */
170                 "Create @code{RehearsalMark} objects.  It puts them on top of"
171                 " all staves (which is taken from the property"
172                 " @code{stavesFound}).  If moving this engraver to a different"
173                 " context, @ref{Staff_collecting_engraver} must move along,"
174                 " otherwise all marks end up on the same Y@tie{}location.",
175
176                 /* create */
177                 "RehearsalMark ",
178
179                 /* read */
180                 "markFormatter "
181                 "rehearsalMark "
182                 "stavesFound ",
183
184                 /* write */
185                 ""
186                );