]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-engraver.cc
release: 1.3.58
[lilypond.git] / lily / local-key-engraver.cc
1 /*
2   local-key-engraver.cc -- implement Local_key_engraver
3
4   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6 // clean up!
7
8 #include "musical-request.hh"
9 #include "command-request.hh"
10 #include "local-key-engraver.hh"
11 #include "local-key-item.hh"
12 #include "key-engraver.hh"
13 #include "debug.hh"
14 #include "key-item.hh"
15 #include "tie.hh"
16 #include "note-head.hh"
17 #include "timing-translator.hh"
18 #include "engraver-group-engraver.hh"
19 #include "grace-align-item.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "side-position-interface.hh"
22 #include "engraver.hh"
23
24 #include "parray.hh"
25
26
27 /**
28    Make accidentals.  Catches note heads, ties and notices key-change
29    events.  Due to interaction with ties (which don't come together
30    with note heads), this needs to be in a context higher than Tie_engraver.
31    (FIXME).
32  */
33 struct Local_key_engraver : Engraver {
34   Local_key_item *key_item_p_;
35 protected:
36   VIRTUAL_COPY_CONS(Translator);
37   virtual void do_process_music();
38   virtual void acknowledge_element (Score_element_info);
39   virtual void do_pre_move_processing();
40   virtual void do_creation_processing ();
41   virtual void process_acknowledged ();
42   virtual void do_removal_processing ();
43 public:
44   
45   Newkey local_key_;
46   Key_engraver *key_grav_l_;
47   Array<Note_req* > mel_l_arr_;
48   Array<Item*> support_l_arr_;
49   Link_array<Item  > forced_l_arr_;
50   Link_array<Item > tied_l_arr_;
51   Local_key_engraver();
52   bool self_grace_b_;
53   Grace_align_item * grace_align_l_;
54   Timing_translator * time_trans_l_  ;
55 };
56
57
58
59 Local_key_engraver::Local_key_engraver()
60 {
61   key_grav_l_ = 0;
62   key_item_p_ =0;
63   grace_align_l_ =0;
64   time_trans_l_ = 0;
65 }
66
67 void
68 Local_key_engraver::do_creation_processing ()
69 {
70   /*
71     UGHGUHGUH.
72
73     Breaks if Key_engraver is removed from under us.
74   */
75   Translator * result =
76     daddy_grav_l()->get_simple_translator ("Key_engraver");
77
78   key_grav_l_ = dynamic_cast<Key_engraver *> (result);
79
80   if (!key_grav_l_)
81     {
82       warning (_ ("out of tune:"));
83       warning (_f ("can't find: `%s'", "Key_engraver"));
84     }
85   else
86     {
87       local_key_ = key_grav_l_->key_;
88     }
89
90   /*
91     TODO
92     (if we are grace) get key info from parent Local_key_engraver
93   */
94
95   Translator * tr = daddy_grav_l()->get_simple_translator ("Timing_engraver");  // ugh
96   time_trans_l_ = dynamic_cast<Timing_translator*> (tr);
97 }
98
99 void
100 Local_key_engraver::process_acknowledged ()
101 {
102   if (!key_item_p_ && mel_l_arr_.size()) 
103     {
104       SCM f = get_property ("forgetAccidentals");
105       bool forget = to_boolean (f);
106       for (int i=0; i  < mel_l_arr_.size(); i++) 
107         {
108           Item * support_l = support_l_arr_[i];
109           Note_req * note_l = mel_l_arr_[i];
110
111           /* see if there's a tie that "changes" the accidental */
112           /* works because if there's a tie, the note to the left
113              is of the same pitch as the actual note */
114
115           int prev_acc =local_key_.get (note_l->pitch_.octave_i_,
116                                         note_l->pitch_.notename_i_);
117           bool different = prev_acc != note_l->pitch_.accidental_i_;
118           
119           bool tie_changes = tied_l_arr_.find_l (support_l) && different;
120           if (!forget
121               && (note_l->forceacc_b_
122                   || !different)
123               && !tie_changes)
124             {
125               if (!key_item_p_) 
126                 {
127                   key_item_p_ = new Local_key_item (get_property ("basicLocalKeyProperties"));
128                   Side_position_interface (key_item_p_).set_axis (X_AXIS);
129                   Side_position_interface (key_item_p_).set_direction (LEFT);
130                   staff_symbol_referencer(key_item_p_).set_interface ();
131                          
132                   announce_element (Score_element_info (key_item_p_, 0));
133                 }
134
135               
136               bool extra_natural
137                 = sign (prev_acc) * (prev_acc - note_l->pitch_.accidental_i_) == 1 ;
138
139               key_item_p_->add_pitch (note_l->pitch_,
140                                       note_l->cautionary_b_,
141                                       extra_natural);
142               Side_position_interface (key_item_p_).add_support (support_l);
143             }
144           
145           if (!forget)
146             {
147               local_key_.set (note_l->pitch_.octave_i_, note_l->pitch_.notename_i_,
148                               note_l->pitch_.accidental_i_);
149 #if 0
150               if (!tied_l_arr_.find_l (support_l))
151                 {
152                   local_key_.clear_internal_forceacc (note_l->pitch_);
153                 }
154               else if (tie_changes)
155                 {
156                   local_key_.set_internal_forceacc (note_l->pitch_);
157                 }
158 #endif
159             }
160         }
161     }
162   if (key_item_p_ && grace_align_l_)
163     {
164       Side_position_interface (grace_align_l_).add_support (key_item_p_);
165       grace_align_l_ =0;
166     }
167   
168 }
169
170 void
171 Local_key_engraver::do_removal_processing ()
172 {
173   // TODO: signal accidentals to Local_key_engraver the 
174 }
175
176 void
177 Local_key_engraver::do_pre_move_processing()
178 {
179   if (key_item_p_)
180     {
181       for (int i=0; i < support_l_arr_.size(); i++)
182         Side_position_interface (key_item_p_).add_support (support_l_arr_[i]);
183
184       typeset_element (key_item_p_);
185       key_item_p_ =0;
186     }
187
188   grace_align_l_ = 0;
189   mel_l_arr_.clear();
190   tied_l_arr_.clear();
191   support_l_arr_.clear();
192   forced_l_arr_.clear();        
193 }
194
195 void
196 Local_key_engraver::acknowledge_element (Score_element_info info)
197 {
198   SCM wg= get_property ("weAreGraceContext");
199   
200   bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
201   bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
202
203   Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);  
204   if (he_gr && !selfgr && gai)
205     {
206       grace_align_l_ = gai;
207     }
208   Note_req * note_l =  dynamic_cast <Note_req *> (info.req_l_);
209   Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
210
211
212   
213   if (he_gr != selfgr)
214     return;
215   
216   if (note_l && note_head)
217     {
218       mel_l_arr_.push (note_l);
219       support_l_arr_.push (note_head);
220     }
221   else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
222     {
223       tied_l_arr_.push (tie_l->head (RIGHT));
224     }
225 }
226
227 void
228 Local_key_engraver::do_process_music()
229 {
230
231   SCM smp = get_property ("measurePosition");
232   Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
233
234   if (!mp)
235     {
236       if (!to_boolean (get_property ("noResetKey")) && key_grav_l_)
237         local_key_= key_grav_l_->key_;
238     }
239   else if (key_grav_l_ && key_grav_l_->key_changed_b ())
240     {
241       local_key_ = key_grav_l_->key_;
242     }
243 }
244
245
246
247 ADD_THIS_TRANSLATOR(Local_key_engraver);
248