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