]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-engraver.cc
release: 1.3.57
[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 #include "key.hh"
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   Key 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           bool tie_changes = tied_l_arr_.find_l (support_l)
115             && !local_key_.different_acc (note_l->pitch_);
116
117           if (!forget
118
119               && ((note_l->forceacc_b_
120                    || !local_key_.different_acc (note_l->pitch_)
121                    || local_key_.internal_forceacc (note_l->pitch_)))
122
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               key_item_p_->add_pitch (note_l->pitch_,
136                                       note_l->cautionary_b_,
137                                       local_key_.double_to_single_acc(note_l->pitch_));
138               Side_position_interface (key_item_p_).add_support (support_l);
139             }
140           
141           if (!forget)
142             {
143               local_key_.set (note_l->pitch_);
144               if (!tied_l_arr_.find_l (support_l))
145                 {
146                   local_key_.clear_internal_forceacc (note_l->pitch_);
147                 }
148               else if (tie_changes)
149                 {
150                   local_key_.set_internal_forceacc (note_l->pitch_);
151                 }
152             }
153         }
154     }
155   if (key_item_p_ && grace_align_l_)
156     {
157       Side_position_interface (grace_align_l_).add_support (key_item_p_);
158       grace_align_l_ =0;
159     }
160   
161 }
162
163 void
164 Local_key_engraver::do_removal_processing ()
165 {
166   // TODO: signal accidentals to Local_key_engraver the 
167 }
168
169 void
170 Local_key_engraver::do_pre_move_processing()
171 {
172   if (key_item_p_)
173     {
174       for (int i=0; i < support_l_arr_.size(); i++)
175         Side_position_interface (key_item_p_).add_support (support_l_arr_[i]);
176
177       typeset_element (key_item_p_);
178       key_item_p_ =0;
179     }
180
181   grace_align_l_ = 0;
182   mel_l_arr_.clear();
183   tied_l_arr_.clear();
184   support_l_arr_.clear();
185   forced_l_arr_.clear();        
186 }
187
188 void
189 Local_key_engraver::acknowledge_element (Score_element_info info)
190 {
191   SCM wg= get_property ("weAreGraceContext");
192   
193   bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
194   bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
195
196   Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);  
197   if (he_gr && !selfgr && gai)
198     {
199       grace_align_l_ = gai;
200     }
201   Note_req * note_l =  dynamic_cast <Note_req *> (info.req_l_);
202   Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
203
204
205   
206   if (he_gr != selfgr)
207     return;
208   
209   if (note_l && note_head)
210     {
211       mel_l_arr_.push (note_l);
212       support_l_arr_.push (note_head);
213     }
214   else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
215     {
216       tied_l_arr_.push (tie_l->head (RIGHT));
217     }
218 }
219
220 void
221 Local_key_engraver::do_process_music()
222 {
223
224   SCM smp = get_property ("measurePosition");
225   Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
226
227   if (!mp)
228     {
229       if (!to_boolean (get_property ("noResetKey")) && key_grav_l_)
230         local_key_= key_grav_l_->key_;
231     }
232   else if (key_grav_l_ && key_grav_l_->key_changed_b ())
233     {
234       local_key_ = key_grav_l_->key_;
235     }
236 }
237
238
239
240 ADD_THIS_TRANSLATOR(Local_key_engraver);
241