]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-engraver.cc
release: 1.3.19
[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
22 Local_key_engraver::Local_key_engraver()
23 {
24   key_grav_l_ = 0;
25   key_item_p_ =0;
26   grace_align_l_ =0;
27 }
28
29 void
30 Local_key_engraver::do_creation_processing ()
31 {
32   /*
33     UGHGUHGUH.
34
35     Breaks if Key_engraver is removed from under us.
36    */
37   Translator * result =
38     daddy_grav_l()->get_simple_translator ("Key_engraver");
39
40   key_grav_l_ = dynamic_cast<Key_engraver *> (result);
41
42   if (!key_grav_l_)
43     {
44       warning (_ ("out of tune:"));
45       warning (_f ("Can't find: `%s'", "Key_engraver"));
46     }
47   else
48     {
49       local_key_ = key_grav_l_->key_;
50     }
51
52   /*
53     TODO
54     (if we are grace) get key info from parent Local_key_engraver
55   */
56 }
57
58 void
59 Local_key_engraver::process_acknowledged ()
60 {
61     if (!key_item_p_ && mel_l_arr_.size()) 
62     {
63         SCM f = get_property ("forgetAccidentals",0);
64         bool forget = to_boolean (f);
65         for (int i=0; i  < mel_l_arr_.size(); i++) 
66             {
67                 Item * support_l = support_l_arr_[i];
68                 Note_req * note_l = mel_l_arr_[i];
69
70             /* see if there's a tie that "changes" the accidental */
71             /* works because if there's a tie, the note to the left
72                is of the same pitch as the actual note */
73             bool tie_changes = tied_l_arr_.find_l (support_l)
74                   && !local_key_.different_acc (note_l->pitch_);
75
76             if (!forget
77
78                  && ((note_l->forceacc_b_
79                  || !local_key_.different_acc (note_l->pitch_)
80                          || local_key_.internal_forceacc (note_l->pitch_)))
81
82              && !tie_changes)
83              {
84                  if (!key_item_p_) 
85                      {
86                          key_item_p_ = new Local_key_item;
87                          Staff_symbol_referencer_interface si(key_item_p_);
88                          si.set_interface ();
89                          
90                          announce_element (Score_element_info (key_item_p_, 0));
91                      }
92
93                      key_item_p_->add_pitch (note_l->pitch_,
94                                       note_l->cautionary_b_,
95                                           local_key_.double_to_single_acc(note_l->pitch_));
96                      key_item_p_->add_support (support_l);
97              }
98           
99                  if (!forget)
100                          {
101                                  local_key_.set (note_l->pitch_);
102                  if (!tied_l_arr_.find_l (support_l))
103                              {
104                                  local_key_.clear_internal_forceacc (note_l->pitch_);
105                              }
106                  else if (tie_changes)
107                                  {
108                      local_key_.set_internal_forceacc (note_l->pitch_);
109                              }
110                         }
111         }
112     }
113     if (key_item_p_ && grace_align_l_)
114     {
115         grace_align_l_->add_support (key_item_p_);
116         grace_align_l_ =0;
117     }
118   
119 }
120
121 void
122 Local_key_engraver::do_removal_processing ()
123 {
124   // TODO: signal accidentals to Local_key_engraver the 
125 }
126
127 void
128 Local_key_engraver::do_pre_move_processing()
129 {
130   if (key_item_p_)
131     {
132       for (int i=0; i < support_l_arr_.size(); i++)
133         key_item_p_->add_support (support_l_arr_[i]);
134
135       typeset_element (key_item_p_);
136       key_item_p_ =0;
137     }
138
139   grace_align_l_ = 0;
140   mel_l_arr_.clear();
141   tied_l_arr_.clear();
142   support_l_arr_.clear();
143   forced_l_arr_.clear();        
144 }
145
146 void
147 Local_key_engraver::acknowledge_element (Score_element_info info)
148 {
149   SCM wg= get_property ("weAreGraceContext", 0);
150   
151   bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
152   bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
153
154   Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);  
155   if (he_gr && !selfgr && gai)
156     {
157       grace_align_l_ = gai;
158     }
159   Note_req * note_l =  dynamic_cast <Note_req *> (info.req_l_);
160   Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
161
162
163   
164   if (he_gr != selfgr)
165     return;
166   
167   if (note_l && note_head)
168     {
169       mel_l_arr_.push (note_l);
170       support_l_arr_.push (note_head);
171     }
172  else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
173     {
174       tied_l_arr_.push (tie_l->head (RIGHT));
175     }
176 }
177
178 void
179 Local_key_engraver::do_process_requests()
180 {
181   Translator * tr = daddy_grav_l()->get_simple_translator ("Timing_engraver");  // ugh
182   Timing_translator * time_C_  = dynamic_cast<Timing_translator*> (tr);
183   
184   if (time_C_ && !time_C_->measure_position ())
185     {
186       SCM n =  get_property ("noResetKey",0);
187       bool no_res = to_boolean (n);
188       if (!no_res && key_grav_l_)
189         local_key_= key_grav_l_->key_;
190     }
191   else if (key_grav_l_ && key_grav_l_->key_changed_b ())
192     {
193       local_key_ = key_grav_l_->key_;
194     }
195 }
196
197
198
199 ADD_THIS_TRANSLATOR(Local_key_engraver);
200