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