]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
Run grand-replace for 2010.
[lilypond.git] / lily / clef-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Mats Bengtsson <matsb@s3.kth.se>
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 <cctype>
22 using namespace std;
23
24 #include "item.hh"
25 #include "context.hh"
26 #include "bar-line.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "engraver.hh"
29 #include "direction.hh"
30 #include "side-position-interface.hh"
31
32 #include "translator.icc"
33
34 class Clef_engraver : public Engraver
35 {
36 public:
37   TRANSLATOR_DECLARATIONS (Clef_engraver);
38
39   Direction octave_dir_;
40
41 protected:
42   void stop_translation_timestep ();
43   void process_music ();
44   DECLARE_ACKNOWLEDGER (bar_line);
45
46   virtual void derived_mark () const;
47 private:
48   Item *clef_;
49   Item *octavate_;
50
51   SCM prev_glyph_;
52   SCM prev_cpos_;
53   SCM prev_octavation_;
54   void create_clef ();
55   void set_glyph ();
56   void inspect_clef_properties ();
57 };
58
59 void
60 Clef_engraver::derived_mark () const
61 {
62   scm_gc_mark (prev_octavation_);
63   scm_gc_mark (prev_cpos_);
64   scm_gc_mark (prev_glyph_);
65 }
66
67 Clef_engraver::Clef_engraver ()
68 {
69   clef_ = 0;
70   octave_dir_ = CENTER;
71   octavate_ = 0;
72
73   /*
74     will trigger a clef at the start since #f != ' ()
75   */
76   prev_octavation_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
77 }
78
79 void
80 Clef_engraver::set_glyph ()
81 {
82   SCM glyph_sym = ly_symbol2scm ("glyph");
83   SCM basic = ly_symbol2scm ("Clef");
84
85   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
86   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
87 }
88
89 /**
90    Generate a clef at the start of a measure. (when you see a Bar,
91    ie. a breakpoint)
92 */
93 void
94 Clef_engraver::acknowledge_bar_line (Grob_info info)
95 {
96   Item *item = dynamic_cast<Item *> (info.grob ());
97   if (item && scm_is_string (get_property ("clefGlyph")))
98     create_clef ();
99 }
100
101 void
102 Clef_engraver::create_clef ()
103 {
104   if (!clef_)
105     {
106       Item *c = make_item ("Clef", SCM_EOL);
107
108       clef_ = c;
109       SCM cpos = get_property ("clefPosition");
110
111       if (scm_is_number (cpos))
112         clef_->set_property ("staff-position", cpos);
113
114       SCM oct = get_property ("clefOctavation");
115       if (scm_is_number (oct) && scm_to_int (oct))
116         {
117           Item *g = make_item ("OctavateEight", SCM_EOL);
118
119           int abs_oct = scm_to_int (oct);
120           int dir = sign (abs_oct);
121           abs_oct = abs (abs_oct) + 1;
122
123           SCM txt = scm_number_to_string (scm_from_int (abs_oct),
124                                           scm_from_int (10));
125
126           g->set_property ("text",
127                            scm_list_n (ly_lily_module_constant ("vcenter-markup"),
128                                        txt, SCM_UNDEFINED));
129           Side_position_interface::add_support (g, clef_);
130
131           g->set_parent (clef_, Y_AXIS);
132           g->set_parent (clef_, X_AXIS);
133           g->set_property ("direction", scm_from_int (dir));
134           octavate_ = g;
135         }
136     }
137 }
138 void
139 Clef_engraver::process_music ()
140 {
141   inspect_clef_properties ();
142 }
143
144 void
145 Clef_engraver::inspect_clef_properties ()
146 {
147   SCM glyph = get_property ("clefGlyph");
148   SCM clefpos = get_property ("clefPosition");
149   SCM octavation = get_property ("clefOctavation");
150   SCM force_clef = get_property ("forceClef");
151
152   if (clefpos == SCM_EOL
153       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
154       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
155       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
156       || to_boolean (force_clef))
157     {
158       set_context_property_on_children (context (),
159                                         ly_symbol2scm ("localKeySignature"),
160                                         get_property ("keySignature"));
161       
162       set_glyph ();
163       if (prev_cpos_ != SCM_BOOL_F || to_boolean (get_property ("firstClef")))
164         create_clef ();
165
166       if (clef_)
167         clef_->set_property ("non-default", SCM_BOOL_T);
168
169       prev_cpos_ = clefpos;
170       prev_glyph_ = glyph;
171       prev_octavation_ = octavation;
172     }
173
174   if (to_boolean (force_clef))
175     {
176       SCM prev;
177       Context *w = context ()->where_defined (ly_symbol2scm ("forceClef"), &prev);
178       w->set_property ("forceClef", SCM_EOL);
179     }
180 }
181
182 void
183 Clef_engraver::stop_translation_timestep ()
184 {
185   if (clef_)
186     {
187       SCM vis = 0;
188       if (to_boolean (clef_->get_property ("non-default")))
189         vis = get_property ("explicitClefVisibility");
190
191       if (vis)
192         {
193           clef_->set_property ("break-visibility", vis);
194           if (octavate_)
195             octavate_->set_property ("break-visibility", vis);
196         }
197
198       clef_ = 0;
199
200       octavate_ = 0;
201     }
202 }
203
204 ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
205 ADD_TRANSLATOR (Clef_engraver,
206                 /* doc */
207                 "Determine and set reference point for pitches.",
208
209                 /* create */
210                 "Clef "
211                 "OctavateEight ",
212
213                 /* read */
214                 "clefGlyph "
215                 "clefOctavation "
216                 "clefPosition "
217                 "explicitClefVisibility "
218                 "forceClef ",
219
220                 /* write */
221                 ""
222                 );