]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
release: 1.3.18
[lilypond.git] / lily / key-engraver.cc
1 /*
2   key-engraver.cc -- implement Key_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   */
8 // clean me up 
9 #include "key-engraver.hh"
10 #include "key-item.hh"
11 #include "command-request.hh"
12 #include "local-key-engraver.hh"
13 #include "musical-request.hh"
14 #include "local-key-item.hh"
15 #include "bar.hh"
16 #include "timing-translator.hh"
17 #include "staff-symbol-referencer.hh"
18
19 Key_engraver::Key_engraver ()
20 {
21   item_p_ = 0;
22   do_post_move_processing ();
23 }
24
25 bool
26 Key_engraver::key_changed_b () const
27 {
28   return keyreq_l_ ;
29 }
30
31 void
32 Key_engraver::create_key ()
33 {
34   if (!item_p_) 
35     {
36       item_p_ = new Key_item;
37       Staff_symbol_referencer_interface st (item_p_);
38       st.set_interface ();
39       
40       item_p_->set_elt_property ("break-aligned", SCM_BOOL_T); // ugh
41       
42       item_p_->set_elt_property ("multi-octave",
43                                  gh_bool2scm (key_.multi_octave_b_));
44       
45       announce_element (Score_element_info (item_p_,keyreq_l_));
46       
47
48       for (int i = 0; i < accidental_idx_arr_.size(); i++) 
49         {
50           Musical_pitch m_l =accidental_idx_arr_[i];
51           int a =m_l.accidental_i_;      
52           if (key_.multi_octave_b_)
53             item_p_->add (m_l.steps (), a);
54           else
55             item_p_->add (m_l.notename_i_, a);
56         }
57
58       for (int i = 0 ; i < old_accidental_idx_arr_.size(); i++) 
59         {
60           Musical_pitch m_l =old_accidental_idx_arr_[i];
61           int a =m_l.accidental_i_;
62           if (key_.multi_octave_b_)
63             item_p_->add_old (m_l.steps  (), a);
64           else
65             item_p_->add_old (m_l.notename_i_, a);
66         }
67     }
68 }      
69
70
71 bool
72 Key_engraver::do_try_music (Music * req_l)
73 {
74   if (Key_change_req *kc = dynamic_cast <Key_change_req *> (req_l))
75     {
76       if (keyreq_l_)
77         warning (_ ("FIXME: key change merge"));
78       keyreq_l_ = kc;
79       read_req (keyreq_l_);
80       return true;
81     }   
82   return  false;
83 }
84
85 void
86 Key_engraver::acknowledge_element (Score_element_info info)
87 {
88   if (dynamic_cast <Clef_change_req *> (info.req_l_)) 
89     {
90       SCM c =  get_property ("createKeyOnClefChange", 0);
91       if (to_boolean (c))
92         create_key ();
93     }
94   else if (dynamic_cast<Bar *> (info.elem_l_)
95            && accidental_idx_arr_.size ()) 
96     {
97       bool def =  (!item_p_);
98       create_key ();
99       if (!def)
100         {
101           item_p_->set_elt_property ("visibility-lambda",
102                                     ly_eval_str ("all-visibility"));
103         }
104     }
105
106 }
107
108 void
109 Key_engraver::do_process_requests ()
110 {
111   if (keyreq_l_) 
112     {
113       create_key ();
114     }
115 }
116
117 void
118 Key_engraver::do_pre_move_processing ()
119
120   if (item_p_) 
121     {
122       typeset_element (item_p_);
123       item_p_ = 0;
124     }
125 }
126
127
128 /*
129   TODO Slightly hairy.  
130  */
131 void
132 Key_engraver::read_req (Key_change_req const * r)
133 {
134   old_accidental_idx_arr_ = accidental_idx_arr_;
135   key_.clear ();
136   SCM prop = get_property ("keyOctaviation", 0);
137
138   key_.multi_octave_b_ = to_boolean (prop);
139   
140   accidental_idx_arr_.clear ();
141
142   if (r->key_.ordinary_key_b_) 
143     {
144       int no_of_acc = r->key_.ordinary_accidentals_i ();
145
146       // Hmm, can't these be handled/constructed by Key_change_req?
147       if (no_of_acc < 0) 
148         {
149           int accidental = 6 ; // First accidental: bes
150           for ( ; no_of_acc < 0 ; no_of_acc++ ) 
151             {
152               Musical_pitch m;
153               m.accidental_i_ = -1;
154               m.notename_i_ = accidental;
155               if (key_.multi_octave_b_)
156                 key_.set (m);
157               else
158                 key_.set (m.notename_i_, m.accidental_i_);
159               accidental_idx_arr_.push (m);
160               
161               accidental = (accidental + 3) % 7 ;
162             }
163         }
164       else 
165         { 
166           int accidental = 3 ; // First accidental: fis
167           for ( ; no_of_acc > 0 ; no_of_acc-- ) 
168             {
169               Musical_pitch m;
170               m.accidental_i_ = 1;
171               m.notename_i_ = accidental;
172               if (key_.multi_octave_b_)
173                 key_.set (m);
174               else
175                 key_.set (m.notename_i_, m.accidental_i_);
176               accidental_idx_arr_.push (m);
177               
178               accidental = (accidental + 4) % 7 ;
179             }
180         }
181     }
182   else // Special key
183     {
184       for (int i = 0; i < r->key_.pitch_arr_.size (); i ++) 
185         {
186           Musical_pitch m_l =r->key_.pitch_arr_[i];
187           if (key_.multi_octave_b_)
188             key_.set (m_l);
189           else
190             key_.set (m_l.notename_i_, m_l.accidental_i_);
191           
192           accidental_idx_arr_.push (m_l);
193         }
194     }
195 }
196
197 void
198 Key_engraver::do_post_move_processing ()
199 {
200   keyreq_l_ = 0;
201   old_accidental_idx_arr_.clear ();
202 }
203
204 ADD_THIS_TRANSLATOR (Key_engraver);
205