]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
* scm/music-functions.scm (has-request-chord): don't use
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   */
8
9 #include "item.hh"
10 #include "bar-line.hh"
11 #include "staff-symbol-referencer.hh"
12 #include "context.hh"
13 #include "engraver.hh"
14 #include "protected-scm.hh"
15 #include "clef.hh"
16
17 /*
18   TODO: The representation  of key sigs is all fucked.
19  */
20
21 /**
22   Make the key signature.
23  */
24 class Key_engraver : public Engraver
25 {
26   void create_key (bool);
27   void read_ev (Music const * r);
28
29   Music *key_ev_;
30   Item *item_;
31   Item *cancellation_;
32 public:
33   TRANSLATOR_DECLARATIONS (Key_engraver);
34
35 protected:
36   virtual void initialize ();
37   virtual void finalize ();
38   virtual bool try_music (Music *ev);
39   virtual void stop_translation_timestep ();
40   virtual void start_translation_timestep ();
41   virtual void process_music ();
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   key_ev_ = 0;
55   item_ = 0;
56   cancellation_ = 0;
57 }
58
59
60 void
61 Key_engraver::create_key (bool def)
62 {
63   if (!item_) 
64     {
65       item_ = make_item ("KeySignature", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
66
67       item_->set_property ("c0-position",
68                            get_property ("middleCPosition"));
69
70       SCM last = get_property ("lastKeySignature");
71       SCM key = get_property ("keySignature");
72       if (to_boolean (get_property ("printKeyCancellation"))
73           && !scm_is_eq (last, key))
74         {
75           cancellation_ = make_item ("KeyCancellation", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
76           cancellation_->set_property ("old-accidentals", last);
77           cancellation_->set_property ("c0-position",
78                            get_property ("middleCPosition"));
79       
80         }
81       item_->set_property ("new-accidentals", key);
82     }
83
84   if (!def)
85     {
86       SCM vis = get_property ("explicitKeySignatureVisibility"); 
87       if (ly_c_procedure_p (vis))
88         item_->set_property ("break-visibility", vis);
89     }
90 }      
91
92
93 bool
94 Key_engraver::try_music (Music * ev)
95 {
96   if (ev->is_mus_type ("key-change-event"))
97     {
98       /* do this only once, just to be on the safe side.  */
99       if (!key_ev_)
100         {
101           key_ev_ = ev;
102           read_ev (key_ev_);
103         }
104       return true;
105     }   
106   return  false;
107 }
108
109
110 void
111 Key_engraver::acknowledge_grob (Grob_info info)
112 {
113   if (Clef::has_interface (info.grob_))
114     {
115       SCM c =  get_property ("createKeyOnClefChange");
116       if (to_boolean (c))
117         {
118           create_key (false);
119         }
120     }
121   else if (Bar_line::has_interface (info.grob_)
122            && scm_is_pair (get_property ("keySignature")))
123     {
124       create_key (true);
125     }
126 }
127
128
129 void
130 Key_engraver::process_music ()
131 {
132   if (key_ev_ ||
133       get_property ("lastKeySignature") != get_property ("keySignature"))
134     create_key (false);
135 }
136
137
138 void
139 Key_engraver::stop_translation_timestep ()
140 {
141   item_ = 0;
142   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
143   cancellation_ = 0; 
144 }
145
146
147 void
148 Key_engraver::read_ev (Music const * r)
149 {
150   SCM p = r->get_property ("pitch-alist");
151   if (!scm_is_pair (p))
152     return;
153
154   SCM n = scm_list_copy (p);
155   SCM accs = SCM_EOL;
156   for (SCM s = get_property ("keyAccidentalOrder");
157        scm_is_pair (s); s = scm_cdr (s))
158     {
159       if (scm_is_pair (scm_member (scm_car (s), n)))
160         {
161           accs = scm_cons (scm_car (s), accs);
162           n = scm_delete_x (scm_car (s), n);
163         }
164     }
165   
166   for (SCM s = n ; scm_is_pair (s); s = scm_cdr (s))
167     if (scm_to_int (scm_cdar (s)))
168       accs = scm_cons (scm_car (s), accs);
169
170   context ()->set_property ("keySignature", accs);
171   context ()->set_property ("tonic" ,
172                             r->get_property ("tonic"));
173 }
174
175
176 void
177 Key_engraver::start_translation_timestep ()
178 {
179   key_ev_ = 0;
180 }
181
182
183 void
184 Key_engraver::initialize ()
185 {
186   context ()->set_property ("keySignature", SCM_EOL);
187   context ()->set_property ("lastKeySignature", SCM_EOL);
188
189   Pitch p (0, 0, 0);
190   context ()->set_property ("tonic", p.smobbed_copy ());
191
192 }
193
194
195 ADD_TRANSLATOR (Key_engraver,
196 /* descr */       "",
197 /* creats*/       "KeySignature",
198 /* accepts */     "key-change-event",
199 /* acks  */      "bar-line-interface clef-interface",
200 /* reads */       "keySignature printKeyCancellation lastKeySignature explicitKeySignatureVisibility createKeyOnClefChange keyAccidentalOrder keySignature",
201 /* write */       "lastKeySignature tonic keySignature");