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