]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
* lily/ : rename Request to Event
[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
10
11 #include "event.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_;
29   Item * item_;
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);
38   virtual void stop_translation_timestep ();
39   virtual void start_translation_timestep ();
40   virtual void process_music ();
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_ = 0;
54   item_ = 0;
55 }
56
57
58 void
59 Key_engraver::create_key (bool def)
60 {
61   if (!item_) 
62     {
63       item_ = new Item (get_property ("KeySignature"));
64
65       item_->set_grob_property ("c0-position",
66                                 get_property ("centralCPosition"));
67       
68       // todo: put this in basic props.
69       item_->set_grob_property ("old-accidentals", get_property ("lastKeySignature"));
70       item_->set_grob_property ("new-accidentals", get_property ("keySignature"));
71
72       announce_grob(item_, keyreq_ ? keyreq_->self_scm() : SCM_EOL);
73     }
74
75   if (!def)
76     {
77       SCM vis = get_property ("explicitKeySignatureVisibility"); 
78       if (gh_procedure_p (vis))
79         item_->set_grob_property ("break-visibility",vis);
80     }
81 }      
82
83
84 bool
85 Key_engraver::try_music (Music * req)
86 {
87   if (Key_change_req *kc = dynamic_cast <Key_change_req *> (req))
88     {
89       if (!keyreq_)
90         {
91           /*
92             do this only once, just to be on the safe side.
93             */      
94           keyreq_ = kc;
95           read_req (keyreq_);
96         }
97       
98       return true;
99     }   
100   return  false;
101 }
102
103
104 void
105 Key_engraver::acknowledge_grob (Grob_info info)
106 {
107   if (Clef::has_interface (info.grob_))
108     {
109       SCM c =  get_property ("createKeyOnClefChange");
110       if (to_boolean (c))
111         {
112           create_key (false);
113         }
114     }
115   else if (Bar_line::has_interface (info.grob_)
116            && gh_pair_p (get_property ("keySignature")))
117     {
118       create_key (true);
119     }
120 }
121
122
123 void
124 Key_engraver::process_music ()
125 {
126   if (keyreq_ ||
127       get_property ("lastKeySignature") != get_property ("keySignature"))
128     create_key (false);
129 }
130
131
132 void
133 Key_engraver::stop_translation_timestep ()
134 {
135   if (item_) 
136     {
137       typeset_grob (item_);
138       item_ = 0;
139     }
140 }
141
142
143 void
144 Key_engraver::read_req (Key_change_req const * r)
145 {
146   SCM p = r->get_mus_property ("pitch-alist");
147   if (!gh_pair_p (p))
148     return;
149
150   SCM n = scm_list_copy (p);
151   SCM accs = SCM_EOL;
152   for (SCM s = get_property ("keyAccidentalOrder");
153        gh_pair_p (s); s = ly_cdr (s))
154     {
155       if (gh_pair_p (scm_member (ly_car (s), n)))
156         {
157           accs = gh_cons (ly_car (s), accs);
158           n = scm_delete_x (ly_car (s), n);
159         }
160     }
161   
162   for (SCM s = n ; gh_pair_p (s); s = ly_cdr (s))
163     if (gh_scm2int (ly_cdar (s)))
164       accs = gh_cons (ly_car (s), accs);
165
166 #if 0
167   daddy_trans_->set_property ("lastKeySignature",
168                                 get_property ("keySignature"));
169 #endif
170   
171   daddy_trans_->set_property ("keySignature", accs);
172 }
173
174
175 void
176 Key_engraver::start_translation_timestep ()
177 {
178   keyreq_ = 0;
179   daddy_trans_->set_property ("lastKeySignature", get_property ("keySignature"));
180 }
181
182
183 void
184 Key_engraver::initialize ()
185 {
186   daddy_trans_->set_property ("keySignature", SCM_EOL);
187   daddy_trans_->set_property ("lastKeySignature", SCM_EOL);
188 }
189
190
191 ENTER_DESCRIPTION(Key_engraver,
192 /* descr */       "",
193 /* creats*/       "KeySignature",
194 /* accepts */     "key-change-event",
195 /* acks  */      "bar-line-interface clef-interface",
196 /* reads */       "keySignature lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
197 /* write */       "lastKeySignature");