]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
release: 1.5.14
[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--2001 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 "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   Key_change_req * keyreq_l_;
30   Item * item_p_;
31
32 public:
33   TRANSLATOR_DECLARATIONS(Key_engraver);
34
35 protected:
36   virtual void initialize ();
37   virtual void finalize ();
38   virtual bool try_music (Music *req_l);
39   virtual void stop_translation_timestep ();
40   virtual void start_translation_timestep ();
41   virtual void create_grobs ();
42   virtual void acknowledge_grob (Grob_info);
43 };
44
45
46 void
47 Key_engraver::finalize ()
48 {
49 }
50
51
52 Key_engraver::Key_engraver ()
53 {
54   keyreq_l_ = 0;
55   item_p_ = 0;
56 }
57
58
59 void
60 Key_engraver::create_key (bool def)
61 {
62   if (!item_p_) 
63     {
64       item_p_ = new Item (get_property ("KeySignature"));
65
66       item_p_->set_grob_property ("c0-position",
67                                   get_property ("centralCPosition"));
68       
69       // todo: put this in basic props.
70       item_p_->set_grob_property ("old-accidentals", get_property ("lastKeySignature"));
71       item_p_->set_grob_property ("new-accidentals", get_property ("keySignature"));
72
73       Staff_symbol_referencer::set_interface (item_p_);
74       Key_item::set_interface (item_p_);
75       announce_grob (item_p_,keyreq_l_);
76     }
77
78   if (!def)
79     {
80       SCM vis = get_property ("explicitKeySignatureVisibility"); 
81       if (gh_procedure_p (vis))
82         item_p_->set_grob_property ("visibility-lambda",vis);
83     }
84 }      
85
86
87 bool
88 Key_engraver::try_music (Music * req_l)
89 {
90   if (Key_change_req *kc = dynamic_cast <Key_change_req *> (req_l))
91     {
92       if (keyreq_l_ && !keyreq_l_->equal_b (kc))
93         {
94           kc->origin ()->warning (_ ("Conflicting key signatures found."));
95           keyreq_l_->origin ()->warning (_ ("This was the other key definition."));       
96           return false;
97         }
98       keyreq_l_ = kc;
99       read_req (keyreq_l_);
100
101       return true;
102     }   
103   return  false;
104 }
105
106
107 void
108 Key_engraver::acknowledge_grob (Grob_info info)
109 {
110   if (Clef::has_interface (info.grob_l_))
111     {
112       SCM c =  get_property ("createKeyOnClefChange");
113       if (to_boolean (c))
114         {
115           create_key (false);
116         }
117     }
118   else if (Bar::has_interface (info.grob_l_)
119            && gh_pair_p (get_property ("keySignature")))
120     {
121       create_key (true);
122     }
123 }
124
125
126 void
127 Key_engraver::create_grobs ()
128 {
129   if (keyreq_l_ ||
130       get_property ("lastKeySignature") != get_property ("keySignature"))
131     create_key (false);
132 }
133
134
135 void
136 Key_engraver::stop_translation_timestep ()
137 {
138   if (item_p_) 
139     {
140       typeset_grob (item_p_);
141       item_p_ = 0;
142     }
143 }
144
145
146 void
147 Key_engraver::read_req (Key_change_req const * r)
148 {
149   SCM p = r->get_mus_property ("pitch-alist");
150   if (!gh_pair_p (p))
151     return;
152
153   SCM n = scm_list_copy (p);
154   SCM accs = SCM_EOL;
155   for (SCM s = get_property ("keyAccidentalOrder");
156        gh_pair_p (s); s = ly_cdr (s))
157     {
158       if (gh_pair_p (scm_member (ly_car (s), n)))
159         {
160           accs = gh_cons (ly_car (s), accs);
161           n = scm_delete_x (ly_car (s), n);
162         }
163     }
164   
165   for (SCM s = n ; gh_pair_p (s); s = ly_cdr (s))
166     if (gh_scm2int (ly_cdar (s)))
167       accs = gh_cons (ly_car (s), accs);
168
169   daddy_trans_l_->set_property ("lastKeySignature",
170                                 get_property ("keySignature"));
171   daddy_trans_l_->set_property ("keySignature", accs);
172 }
173
174
175 void
176 Key_engraver::start_translation_timestep ()
177 {
178   keyreq_l_ = 0;
179   daddy_trans_l_->set_property ("lastKeySignature", get_property ("keySignature"));
180 }
181
182
183 void
184 Key_engraver::initialize ()
185 {
186   daddy_trans_l_->set_property ("keySignature", SCM_EOL);
187   daddy_trans_l_->set_property ("lastKeySignature", SCM_EOL);
188 }
189
190
191 ENTER_DESCRIPTION(Key_engraver,
192 /* descr */       "",
193 /* creats*/       "KeySignature",
194 /* acks  */       "bar-line-interface clef-interface",
195 /* reads */       "keySignature lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
196 /* write */       "lastKeySignature");