]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
104687dbd5ef2046e18932904a97d9dc88fb28dd
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   */
8
9 #include "key-item.hh"
10 #include "command-request.hh"
11 #include "musical-request.hh"
12 #include "item.hh"
13 #include "bar.hh"
14 #include "timing-translator.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "translator-group.hh"
17 #include "engraver.hh"
18 #include "musical-pitch.hh"
19 #include "protected-scm.hh"
20 #include "clef.hh"
21
22 /**
23   Make the key signature.
24  */
25 class Key_engraver : public Engraver
26 {
27   void create_key(bool);
28   void read_req (Key_change_req const * r);
29
30 public:
31   Key_engraver();
32   
33   VIRTUAL_COPY_CONS(Translator);
34
35   Key_change_req * keyreq_l_;
36   Item * item_p_;
37   Protected_scm old_accs_;      // ugh. -> property
38     
39 protected:
40   virtual void do_creation_processing();
41   virtual void do_removal_processing ();
42   virtual bool do_try_music (Music *req_l);
43   virtual void do_process_music();
44   virtual void do_pre_move_processing();
45   virtual void do_post_move_processing();
46   virtual void acknowledge_element (Score_element_info);
47 };
48
49
50 void
51 Key_engraver::do_removal_processing ()
52 {
53   old_accs_ = SCM_EOL;          // unprotect can not  be called from dtor.
54 }
55
56 Key_engraver::Key_engraver ()
57 {
58   keyreq_l_ = 0;
59   item_p_ = 0;
60 }
61
62 void
63 Key_engraver::create_key (bool def)
64 {
65   if (!item_p_) 
66     {
67       item_p_ = new Item ( get_property ("basicKeyProperties"));
68
69       item_p_->set_elt_property ("c0-position", gh_int2scm (0));
70
71       // todo: put this in basic props.
72       item_p_->set_elt_property ("old-accidentals", old_accs_);
73       item_p_->set_elt_property ("new-accidentals", get_property ("keySignature"));
74
75       Staff_symbol_referencer::set_interface (item_p_);
76       Key_item::set_interface (item_p_);
77
78       SCM prop = get_property ("keyOctaviation");
79       bool multi = to_boolean (prop);
80       
81       if (multi)
82         item_p_->set_elt_property ("multi-octave", gh_bool2scm (multi));
83       
84       announce_element (item_p_,keyreq_l_);
85     }
86
87   if (!def)
88     item_p_->set_elt_property ("visibility-lambda",
89                                scm_eval2 (ly_symbol2scm  ("all-visible"),
90                                           SCM_EOL));
91
92 }      
93
94
95 bool
96 Key_engraver::do_try_music (Music * req_l)
97 {
98   if (Key_change_req *kc = dynamic_cast <Key_change_req *> (req_l))
99     {
100       if (keyreq_l_)
101         warning (_ ("FIXME: key change merge"));
102       keyreq_l_ = kc;
103       read_req (keyreq_l_);
104       return true;
105     }   
106   return  false;
107 }
108
109 void
110 Key_engraver::acknowledge_element (Score_element_info info)
111 {
112   if (Clef::has_interface (info.elem_l_))
113     {
114       SCM c =  get_property ("createKeyOnClefChange");
115       if (to_boolean (c))
116         {
117           create_key (false);
118         }
119     }
120   else if (Bar::has_interface (info.elem_l_)
121            && gh_pair_p (get_property ("keySignature")))
122     {
123       create_key (true);
124     }
125
126 }
127
128 void
129 Key_engraver::do_process_music ()
130 {
131   if (keyreq_l_ || old_accs_ != get_property ("keySignature"))
132     {
133       create_key (false);
134     }
135 }
136
137 void
138 Key_engraver::do_pre_move_processing ()
139
140   if (item_p_) 
141     {
142       typeset_element (item_p_);
143       item_p_ = 0;
144     }
145 }
146
147 void
148 Key_engraver::read_req (Key_change_req const * r)
149 {
150   SCM p = r->get_mus_property ("pitch-alist");
151   if (p == SCM_UNDEFINED)
152     return;
153
154   SCM n = scm_list_copy (p);
155   SCM accs = SCM_EOL;
156   for (SCM s = get_property ("keyAccidentalOrder");
157        gh_pair_p (s); s = gh_cdr (s))
158     {
159       if (gh_pair_p (scm_member (gh_car (s), n)))
160         {
161           accs = gh_cons (gh_car (s), accs);
162           n = scm_delete_x (gh_car (s), n);
163         }
164     }
165   for (SCM s = n ; gh_pair_p (s); s = gh_cdr (s))
166     if (gh_scm2int (gh_cdar (s)))
167       accs = gh_cons (gh_car (s), accs);
168
169   old_accs_ = get_property ("keySignature");
170   daddy_trans_l_->set_property ("keySignature", accs);
171 }
172
173 void
174 Key_engraver::do_post_move_processing ()
175 {
176   keyreq_l_ = 0;
177   old_accs_ = get_property ("keySignature");
178 }
179
180 void
181 Key_engraver::do_creation_processing ()
182 {
183   daddy_trans_l_->set_property ("keySignature", SCM_EOL);
184   old_accs_ = SCM_EOL;
185 }
186
187
188 ADD_THIS_TRANSLATOR (Key_engraver);
189