]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
release: 1.3.81
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   */
8
9 #include "key-item.hh"
10 #include "command-request.hh"
11 #include "musical-request.hh"
12 #include "item.hh"
13 #include "bar.hh"
14 #include "timing-translator.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "translator-group.hh"
17 #include "engraver.hh"
18 #include "musical-pitch.hh"
19 #include "protected-scm.hh"
20 #include "clef.hh"
21
22 /**
23   Make the key signature.
24  */
25 class Key_engraver : public Engraver {
26   void create_key(bool);
27   void read_req (Key_change_req const * r);
28
29 public:
30   Key_engraver();
31   
32   VIRTUAL_COPY_CONS(Translator);
33
34   Key_change_req * keyreq_l_;
35   Item * item_p_;
36   Protected_scm old_accs_;      // ugh. -> property
37     
38 protected:
39   virtual void do_creation_processing();
40   virtual bool do_try_music (Music *req_l);
41   virtual void do_process_music();
42   virtual void do_pre_move_processing();
43   virtual void do_post_move_processing();
44   virtual void acknowledge_element (Score_element_info);
45 };
46
47
48 Key_engraver::Key_engraver ()
49 {
50   keyreq_l_ = 0;
51   item_p_ = 0;
52 }
53
54 void
55 Key_engraver::create_key (bool def)
56 {
57   if (!item_p_) 
58     {
59       item_p_ = new Item ( get_property ("basicKeyProperties"));
60
61       item_p_->set_elt_property ("c0-position", gh_int2scm (0));
62
63       // todo: put this in basic props.
64       item_p_->set_elt_property ("old-accidentals", old_accs_);
65       item_p_->set_elt_property ("new-accidentals", get_property ("keySignature"));
66
67       Staff_symbol_referencer::set_interface (item_p_);
68       Key_item::set_interface (item_p_);
69
70       SCM prop = get_property ("keyOctaviation");
71       bool multi = to_boolean (prop);
72       
73       if (multi)
74         item_p_->set_elt_property ("multi-octave", gh_bool2scm (multi));
75       
76       announce_element (item_p_,keyreq_l_);
77     }
78
79   if (!def)
80     item_p_->set_elt_property ("visibility-lambda",
81                                scm_eval2 (ly_symbol2scm  ("all-visible"),
82                                           SCM_EOL));
83
84 }      
85
86
87 bool
88 Key_engraver::do_try_music (Music * req_l)
89 {
90   if (Key_change_req *kc = dynamic_cast <Key_change_req *> (req_l))
91     {
92       if (keyreq_l_)
93         warning (_ ("FIXME: key change merge"));
94       keyreq_l_ = kc;
95       read_req (keyreq_l_);
96       return true;
97     }   
98   return  false;
99 }
100
101 void
102 Key_engraver::acknowledge_element (Score_element_info info)
103 {
104   if (Clef::has_interface (info.elem_l_))
105     {
106       SCM c =  get_property ("createKeyOnClefChange");
107       if (to_boolean (c))
108         {
109           create_key (false);
110         }
111     }
112   else if (Bar::has_interface (info.elem_l_)
113            && gh_pair_p (get_property ("keySignature")))
114     {
115       create_key (true);
116     }
117
118 }
119
120 void
121 Key_engraver::do_process_music ()
122 {
123   if (keyreq_l_ || old_accs_ != get_property ("keySignature"))
124     {
125       create_key (false);
126     }
127 }
128
129 void
130 Key_engraver::do_pre_move_processing ()
131
132   if (item_p_) 
133     {
134       typeset_element (item_p_);
135       item_p_ = 0;
136     }
137 }
138
139 void
140 Key_engraver::read_req (Key_change_req const * r)
141 {
142   SCM p = r->get_mus_property ("pitch-alist");
143   if (p == SCM_UNDEFINED)
144     return;
145
146   SCM n = scm_list_copy (p);
147   SCM accs = SCM_EOL;
148   for (SCM s = get_property ("keyAccidentalOrder");
149        gh_pair_p (s); s = gh_cdr (s))
150     {
151       if (gh_pair_p (scm_member (gh_car (s), n)))
152         {
153           accs = gh_cons (gh_car (s), accs);
154           n = scm_delete_x (gh_car (s), n);
155         }
156     }
157   for (SCM s = n ; gh_pair_p (s); s = gh_cdr (s))
158     if (gh_scm2int (gh_cdar (s)))
159       accs = gh_cons (gh_car (s), accs);
160
161   old_accs_ = get_property ("keySignature");
162   daddy_trans_l_->set_property ("keySignature", accs);
163 }
164
165 void
166 Key_engraver::do_post_move_processing ()
167 {
168   keyreq_l_ = 0;
169   old_accs_ = get_property ("keySignature");
170 }
171
172 void
173 Key_engraver::do_creation_processing ()
174 {
175   daddy_trans_l_->set_property ("keySignature", SCM_EOL);
176   old_accs_ = SCM_EOL;
177 }
178
179
180 ADD_THIS_TRANSLATOR (Key_engraver);
181