lilypond-1.3.69
[lilypond.git] / lily / local-key-engraver.cc
blob2067092594fb7ab094f0e0afd95575e22b95fec1
1 /*
2 local-key-engraver.cc -- implement Local_key_engraver
4 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
7 #include "musical-request.hh"
8 #include "command-request.hh"
9 #include "local-key-item.hh"
10 #include "key-item.hh"
11 #include "tie.hh"
12 #include "rhythmic-head.hh"
13 #include "timing-translator.hh"
14 #include "engraver-group-engraver.hh"
15 #include "grace-align-item.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "side-position-interface.hh"
18 #include "engraver.hh"
21 /**
22 Make accidentals. Catches note heads, ties and notices key-change
23 events. Due to interaction with ties (which don't come together
24 with note heads), this needs to be in a context higher than Tie_engraver.
25 (FIXME).
27 struct Local_key_engraver : Engraver {
28 Local_key_item *key_item_p_;
29 protected:
30 VIRTUAL_COPY_CONS(Translator);
31 virtual void do_process_music();
32 virtual void acknowledge_element (Score_element_info);
33 virtual void do_pre_move_processing();
34 virtual void do_creation_processing ();
35 virtual void process_acknowledged ();
36 virtual void do_removal_processing ();
37 public:
39 // todo -> property
40 SCM last_keysig_;
42 Link_array<Note_req> mel_l_arr_;
43 Link_array<Score_element> support_l_arr_;
44 Link_array<Item> forced_l_arr_;
45 Link_array<Score_element> tied_l_arr_;
46 Local_key_engraver();
48 Item * grace_align_l_;
51 Local_key_engraver::Local_key_engraver()
53 key_item_p_ =0;
54 grace_align_l_ =0;
55 last_keysig_ = SCM_EOL;
58 void
59 Local_key_engraver::do_creation_processing ()
61 last_keysig_ = get_property ("keySignature");
62 daddy_trans_l_->set_property ("localKeySignature", last_keysig_);
65 void
66 Local_key_engraver::process_acknowledged ()
68 if (!key_item_p_ && mel_l_arr_.size())
70 SCM localsig = get_property ("localKeySignature");
72 SCM f = get_property ("forgetAccidentals");
73 bool forget = to_boolean (f);
74 for (int i=0; i < mel_l_arr_.size(); i++)
76 Score_element * support_l = support_l_arr_[i];
77 Note_req * note_l = mel_l_arr_[i];
79 int n = note_l->pitch_.notename_i_;
80 int o = note_l->pitch_.octave_i_;
81 int a = note_l->pitch_.accidental_i_;
83 /* see if there's a tie that "changes" the accidental */
84 /* works because if there's a tie, the note to the left
85 is of the same pitch as the actual note */
87 SCM prev = scm_assoc (gh_cons (gh_int2scm (o), gh_int2scm (n)), localsig);
88 if (prev == SCM_BOOL_F)
89 prev = scm_assoc (gh_int2scm (n), localsig);
90 int prev_acc = (prev == SCM_BOOL_F) ? 0 : gh_scm2int (gh_cdr (prev));
91 bool different = prev_acc != a;
93 bool tie_changes = tied_l_arr_.find_l (support_l) && different;
94 if (!forget
95 && (note_l->forceacc_b_ || different)
96 && !tie_changes)
98 if (!key_item_p_)
100 key_item_p_ = new Local_key_item (get_property ("basicLocalKeyProperties"));
101 Side_position::set_axis (key_item_p_, X_AXIS);
102 Side_position::set_direction (key_item_p_, LEFT);
103 Staff_symbol_referencer::set_interface (key_item_p_);
105 announce_element (Score_element_info (key_item_p_, 0));
109 bool extra_natural =
110 sign (prev_acc) * (prev_acc - a) == 1
111 && abs(prev_acc) == 2;
113 key_item_p_->add_pitch (note_l->pitch_,
114 note_l->cautionary_b_,
115 extra_natural);
116 Side_position::add_support (key_item_p_,support_l);
119 if (!forget)
121 localsig = scm_assoc_set_x (localsig, gh_cons (gh_int2scm (o),
122 gh_int2scm (n)),
123 gh_int2scm (a));
125 #if 0
127 TESTME!
129 if (!tied_l_arr_.find_l (support_l))
131 local_key_.clear_internal_forceacc (note_l->pitch_);
133 else if (tie_changes)
135 local_key_.set_internal_forceacc (note_l->pitch_);
137 #endif
144 daddy_trans_l_->set_property ("localKeySignature", localsig);
149 if (key_item_p_ && grace_align_l_)
151 Side_position::add_support (grace_align_l_,key_item_p_);
152 grace_align_l_ =0;
157 void
158 Local_key_engraver::do_removal_processing ()
160 // TODO: if grace ? signal accidentals to Local_key_engraver the
163 void
164 Local_key_engraver::do_pre_move_processing()
166 if (key_item_p_)
168 for (int i=0; i < support_l_arr_.size(); i++)
169 Side_position::add_support (key_item_p_,support_l_arr_[i]);
171 typeset_element (key_item_p_);
172 key_item_p_ =0;
175 grace_align_l_ = 0;
176 mel_l_arr_.clear();
177 tied_l_arr_.clear();
178 support_l_arr_.clear();
179 forced_l_arr_.clear();
182 void
183 Local_key_engraver::acknowledge_element (Score_element_info info)
185 SCM wg= get_property ("weAreGraceContext");
187 bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
188 bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
190 Item * item = dynamic_cast<Item*> (info.elem_l_);
191 if (he_gr && !selfgr && item && Grace_align_item::has_interface (item))
193 grace_align_l_ = item;
195 if (he_gr != selfgr)
196 return;
198 Note_req * note_l = dynamic_cast <Note_req *> (info.req_l_);
200 if (note_l && Rhythmic_head::has_interface (info.elem_l_))
202 mel_l_arr_.push (note_l);
203 support_l_arr_.push (info.elem_l_);
205 else if (Tie::has_interface (info.elem_l_))
207 tied_l_arr_.push (Tie::head (info.elem_l_, RIGHT));
212 ugh. repeated deep_copy generates lots of garbage.
214 void
215 Local_key_engraver::do_process_music()
217 SCM smp = get_property ("measurePosition");
218 Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
220 SCM sig = get_property ("keySignature");
221 if (!mp)
223 if (!to_boolean (get_property ("noResetKey")))
224 daddy_trans_l_->set_property ("localKeySignature", ly_deep_copy (sig));
226 else if (last_keysig_ != sig)
228 daddy_trans_l_->set_property ("localKeySignature", ly_deep_copy (sig));
229 last_keysig_ = sig;
235 ADD_THIS_TRANSLATOR(Local_key_engraver);