]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
update for the lily-wins.py script.
[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--2004 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 "context.hh"
16 #include "engraver.hh"
17 #include "pitch.hh"
18 #include "protected-scm.hh"
19 #include "clef.hh"
20
21 /*
22   TODO: The representation  of key sigs is all fucked.
23  */
24
25 /**
26   Make the key signature.
27  */
28 class Key_engraver : public Engraver
29 {
30   void create_key (bool);
31   void read_ev (Music const * r);
32   Music * key_ev_;
33   Item * item_;
34
35 public:
36   TRANSLATOR_DECLARATIONS (Key_engraver);
37
38 protected:
39   virtual void initialize ();
40   virtual void finalize ();
41   virtual bool try_music (Music *ev);
42   virtual void stop_translation_timestep ();
43   virtual void start_translation_timestep ();
44   virtual void process_music ();
45   virtual void acknowledge_grob (Grob_info);
46 };
47
48
49 void
50 Key_engraver::finalize ()
51 {
52 }
53
54
55 Key_engraver::Key_engraver ()
56 {
57   key_ev_ = 0;
58   item_ = 0;
59 }
60
61
62 void
63 Key_engraver::create_key (bool def)
64 {
65   if (!item_) 
66     {
67       item_ = make_item ("KeySignature", key_ev_ ? key_ev_->self_scm () : SCM_EOL);
68
69       item_->set_property ("c0-position",
70                            get_property ("middleCPosition"));
71       
72       if (to_boolean (get_property ("printKeyCancellation")))
73         item_->set_property ("old-accidentals", get_property ("lastKeySignature"));
74       item_->set_property ("new-accidentals", get_property ("keySignature"));
75
76     }
77
78   if (!def)
79     {
80       SCM vis = get_property ("explicitKeySignatureVisibility"); 
81       if (ly_c_procedure_p (vis))
82         item_->set_property ("break-visibility",vis);
83     }
84 }      
85
86
87 bool
88 Key_engraver::try_music (Music * ev)
89 {
90   if (ev->is_mus_type ("key-change-event"))
91     {
92       if (!key_ev_)
93         {
94           /*
95             do this only once, just to be on the safe side.
96             */      
97           key_ev_ = ev;
98           read_ev (key_ev_);
99         }
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_))
111     {
112       SCM c =  get_property ("createKeyOnClefChange");
113       if (to_boolean (c))
114         {
115           create_key (false);
116         }
117     }
118   else if (Bar_line::has_interface (info.grob_)
119            && ly_c_pair_p (get_property ("keySignature")))
120     {
121       create_key (true);
122     }
123 }
124
125
126 void
127 Key_engraver::process_music ()
128 {
129   if (key_ev_ ||
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_) 
139     {
140       typeset_grob (item_);
141       item_ = 0;
142     }
143 }
144
145
146 void
147 Key_engraver::read_ev (Music const * r)
148 {
149   SCM p = r->get_property ("pitch-alist");
150   if (!ly_c_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        ly_c_pair_p (s); s = ly_cdr (s))
157     {
158       if (ly_c_pair_p (scm_member (ly_car (s), n)))
159         {
160           accs = scm_cons (ly_car (s), accs);
161           n = scm_delete_x (ly_car (s), n);
162         }
163     }
164   
165   for (SCM s = n ; ly_c_pair_p (s); s = ly_cdr (s))
166     if (ly_scm2int (ly_cdar (s)))
167       accs = scm_cons (ly_car (s), accs);
168
169   context ()->set_property ("keySignature", accs);
170   context ()->set_property ("tonic" ,
171                               r->get_property ("tonic"));
172 }
173
174
175 void
176 Key_engraver::start_translation_timestep ()
177 {
178   key_ev_ = 0;
179   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
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 ENTER_DESCRIPTION (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");