]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
add
[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   if (req->is_mus_type ("key-change-event"))
89     {
90       if (!keyreq_)
91         {
92           /*
93             do this only once, just to be on the safe side.
94             */      
95           keyreq_ = dynamic_cast<Key_change_req*> (req); // UGH.
96           read_req (keyreq_);
97         }
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_))
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_)
117            && gh_pair_p (get_property ("keySignature")))
118     {
119       create_key (true);
120     }
121 }
122
123
124 void
125 Key_engraver::process_music ()
126 {
127   if (keyreq_ ||
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_) 
137     {
138       typeset_grob (item_);
139       item_ = 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 #if 0
168   daddy_trans_->set_property ("lastKeySignature",
169                                 get_property ("keySignature"));
170 #endif
171   
172   daddy_trans_->set_property ("keySignature", accs);
173 }
174
175
176 void
177 Key_engraver::start_translation_timestep ()
178 {
179   keyreq_ = 0;
180   daddy_trans_->set_property ("lastKeySignature", get_property ("keySignature"));
181 }
182
183
184 void
185 Key_engraver::initialize ()
186 {
187   daddy_trans_->set_property ("keySignature", SCM_EOL);
188   daddy_trans_->set_property ("lastKeySignature", SCM_EOL);
189 }
190
191
192 ENTER_DESCRIPTION(Key_engraver,
193 /* descr */       "",
194 /* creats*/       "KeySignature",
195 /* accepts */     "key-change-event",
196 /* acks  */      "bar-line-interface clef-interface",
197 /* reads */       "keySignature lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
198 /* write */       "lastKeySignature");