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