]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / staff-symbol-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "engraver.hh"
21 #include "international.hh"
22 #include "spanner.hh"
23 #include "stream-event.hh"
24 #include "warn.hh"
25
26 #include "translator.icc"
27
28 class Staff_symbol_engraver : public Engraver
29 {
30 public:
31   TRANSLATOR_DECLARATIONS (Staff_symbol_engraver);
32
33 protected:
34   Drul_array<Stream_event *> span_events_;
35   Spanner *span_;
36   Spanner *finished_span_;
37   bool first_start_;
38
39 protected:
40   virtual void start_spanner ();
41   virtual void stop_spanner ();
42
43   void stop_translation_timestep ();
44   virtual ~Staff_symbol_engraver ();
45   void acknowledge_grob (Grob_info);
46   void listen_staff_span (Stream_event *);
47   virtual void finalize ();
48   void process_music ();
49   virtual void derived_mark () const;
50 };
51
52 void
53 Staff_symbol_engraver::derived_mark () const
54 {
55   for (LEFT_and_RIGHT (d))
56     {
57       if (span_events_[d])
58         scm_gc_mark (span_events_[d]->self_scm ());
59     }
60 }
61
62 Staff_symbol_engraver::~Staff_symbol_engraver ()
63 {
64   if (span_)
65     {
66       // Somehow finalize() was not called?
67       programming_error ("Have a pending spanner in destructor.");
68     }
69 }
70
71 Staff_symbol_engraver::Staff_symbol_engraver (Context *c)
72   : Engraver (c)
73 {
74   finished_span_ = 0;
75   first_start_ = true;
76   span_ = 0;
77   span_events_.set (0, 0);
78 }
79
80 void
81 Staff_symbol_engraver::listen_staff_span (Stream_event *ev)
82 {
83   Direction d = to_dir (ev->get_property ("span-direction"));
84   if (d)
85     ASSIGN_EVENT_ONCE (span_events_[d], ev);
86   else
87     programming_error ("staff-span event has no direction");
88 }
89
90 void
91 Staff_symbol_engraver::process_music ()
92 {
93   if (span_events_[STOP])
94     {
95       finished_span_ = span_;
96       span_ = 0;
97       if (first_start_)
98         first_start_ = false;
99     }
100
101   if (span_events_[START]
102       || (first_start_ && !span_events_[STOP]))
103     start_spanner ();
104 }
105
106 void
107 Staff_symbol_engraver::start_spanner ()
108 {
109   if (!span_)
110     {
111       span_ = make_spanner ("StaffSymbol", SCM_EOL);
112       span_->set_bound (LEFT,
113                         unsmob<Grob> (get_property ("currentCommandColumn")));
114     }
115 }
116
117 void
118 Staff_symbol_engraver::stop_spanner ()
119 {
120   if (!finished_span_)
121     return;
122
123   if (!finished_span_->get_bound (RIGHT))
124     finished_span_->set_bound (RIGHT, unsmob<Grob> (get_property ("currentCommandColumn")));
125
126   announce_end_grob (finished_span_,
127                      span_events_[STOP]
128                      ? span_events_[STOP]->self_scm ()
129                      : SCM_EOL);
130
131   finished_span_ = 0;
132 }
133
134 void
135 Staff_symbol_engraver::stop_translation_timestep ()
136 {
137   if ((span_events_[START] || first_start_)
138       && span_)
139     first_start_ = false;
140
141   span_events_.set (0, 0);
142   stop_spanner ();
143 }
144
145 void
146 Staff_symbol_engraver::finalize ()
147 {
148   finished_span_ = span_;
149   span_ = 0;
150   stop_spanner ();
151 }
152
153 /*
154   Todo: staff-symbol-referencer iface.
155 */
156 void
157 Staff_symbol_engraver::acknowledge_grob (Grob_info s)
158 {
159   if (span_ || finished_span_)
160     {
161       Spanner *my = span_ ? span_ : finished_span_;
162       s.grob ()->set_object ("staff-symbol", my->self_scm ());
163     }
164 }
165
166
167 void
168 Staff_symbol_engraver::boot ()
169 {
170   ADD_LISTENER (Staff_symbol_engraver, staff_span);
171   ADD_ACKNOWLEDGER (Staff_symbol_engraver, grob);
172 }
173
174 ADD_TRANSLATOR (Staff_symbol_engraver,
175                 /* doc */
176                 "Create the constellation of five (default) staff lines.",
177
178                 /* create */
179                 "StaffSymbol ",
180
181                 /* read */
182                 "",
183
184                 /* write */
185                 ""
186                );