lilypond-1.3.28
[lilypond.git] / lily / clef-engraver.cc
blob4dd5889831c5503a32d09c7f7d9f1763bec90a9b
1 /*
2 clef-engraver.cc -- implement Clef_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>,
8 Mats Bengtsson <matsb@s3.kth.se>
9 */
11 #include <ctype.h>
12 #include "staff-symbol-referencer.hh"
13 #include "bar.hh"
14 #include "clef-item.hh"
15 #include "debug.hh"
16 #include "command-request.hh"
17 #include "timing-translator.hh"
18 #include "note-head.hh"
19 #include "key-item.hh"
20 #include "local-key-item.hh"
21 #include "array.hh"
22 #include "engraver.hh"
23 #include "direction.hh"
25 /// where is c-0 in the staff?
26 class Clef_engraver : public Engraver {
27 Clef_item * clef_p_;
28 Clef_change_req * clef_req_l_;
29 void create_clef();
30 bool set_type (String);
31 protected:
32 virtual void do_process_requests();
33 virtual void do_pre_move_processing();
34 virtual void do_creation_processing();
35 virtual void do_post_move_processing();
36 virtual bool do_try_music (Music*);
37 virtual void acknowledge_element (Score_element_info);
38 public:
39 VIRTUAL_COPY_CONS(Translator);
40 int c0_position_i_;
41 int clef_position_i_;
42 // junkme.
43 Direction octave_dir_;
44 SCM clef_glyph_; // no need for protection. Always referenced somewhere else.
46 Clef_engraver();
50 Clef_engraver::Clef_engraver()
52 clef_glyph_ = SCM_EOL;
53 clef_p_ = 0;
54 clef_req_l_ = 0;
55 c0_position_i_ = 0;
56 clef_position_i_ = 0;
57 octave_dir_ = CENTER;
60 bool
61 Clef_engraver::set_type (String s)
63 if (s.right_str(2) == "_8") // Down one octave
65 octave_dir_ = DOWN;
66 s = s.left_str(s.length_i() - 2);
68 else if (s.right_str(2) == "^8") // Up one octave
70 octave_dir_ = UP;
71 s = s.left_str(s.length_i() - 2);
73 else
74 octave_dir_ = CENTER;
76 SCM c = get_property ("supportedClefTypes",0);
77 SCM p = get_property ("clefPitches", 0);
79 if (gh_list_p (c))
81 SCM found = scm_assoc (ly_str02scm (s.ch_C()), c);
82 if (found == SCM_BOOL_F)
83 return false;
85 clef_glyph_ = gh_cadr (found);
86 SCM pos = gh_caddr (found);
88 clef_position_i_ = gh_scm2int (pos);
90 found = scm_assoc (clef_glyph_, p);
91 if (found == SCM_BOOL_F)
92 return false;
94 c0_position_i_ = clef_position_i_ + gh_scm2int (gh_cdr (found));
97 c0_position_i_ -= (int) octave_dir_ * 7;
98 return true;
102 /**
103 Generate a clef at the start of a measure. (when you see a Bar,
104 ie. a breakpoint)
106 void
107 Clef_engraver::acknowledge_element (Score_element_info info)
109 if (dynamic_cast<Bar*>(info.elem_l_)
110 && gh_string_p (clef_glyph_))
111 create_clef();
113 /* ugh; should make Clef_referenced baseclass */
114 Item * it_l =dynamic_cast <Item *> (info.elem_l_);
115 if (it_l)
117 if (dynamic_cast<Note_head*>(it_l)
118 || dynamic_cast<Local_key_item*> (it_l)
122 Staff_symbol_referencer_interface si (it_l);
123 si.set_position (int (si.position_f ()) + c0_position_i_);
125 else if (Key_item *k = dynamic_cast<Key_item*>(it_l))
127 k-> set_c_position (c0_position_i_);
132 void
133 Clef_engraver::do_creation_processing()
135 SCM def = get_property ("defaultClef", 0);
136 if (gh_string_p (def))
138 set_type (ly_scm2string (def));
139 create_clef ();
140 clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
144 bool
145 Clef_engraver::do_try_music (Music * r_l)
147 if (Clef_change_req *cl = dynamic_cast <Clef_change_req *> (r_l))
149 clef_req_l_ = cl;
150 if (!set_type (cl->clef_str_))
151 cl->error (_ ("unknown clef type"));
153 return true;
155 else
156 return false;
160 void
161 Clef_engraver::create_clef()
163 if (!clef_p_)
165 Clef_item *c= new Clef_item;
166 c->set_elt_property ("breakable", SCM_BOOL_T);
167 c->set_elt_property ("break-aligned", SCM_BOOL_T);
168 announce_element (Score_element_info (c, clef_req_l_));
170 Staff_symbol_referencer_interface si(c);
171 si.set_interface ();
173 clef_p_ = c;
175 Staff_symbol_referencer_interface si(clef_p_);
176 clef_p_->set_elt_property ("glyph", clef_glyph_);
177 si.set_position (clef_position_i_);
178 if (octave_dir_)
180 clef_p_->set_elt_property ("octave-dir", gh_int2scm (octave_dir_));
185 void
186 Clef_engraver::do_process_requests()
188 if (clef_req_l_)
190 create_clef();
191 clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
195 void
196 Clef_engraver::do_pre_move_processing()
198 if (clef_p_)
200 if(to_boolean (clef_p_->remove_elt_property("non-default")))
201 clef_p_->set_elt_property("visibility-lambda",
202 ly_eval_str ("all-visibility"));
204 typeset_element (clef_p_);
205 clef_p_ =0;
209 void
210 Clef_engraver::do_post_move_processing()
212 clef_req_l_ = 0;
218 ADD_THIS_TRANSLATOR(Clef_engraver);