]> git.donarmstrong.com Git - lilypond.git/blob - lily/property-iterator.cc
076bda4f00477df838e13ff8dc656e08ce936d11
[lilypond.git] / lily / property-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "property-iterator.hh"
21
22 #include "context-def.hh"
23 #include "global-context.hh"
24 #include "international.hh"
25 #include "music.hh"
26
27 bool check_grob (Music *mus, SCM sym);
28
29 /**
30    There is no real processing to a property: just lookup the
31    translation unit, and set the property.
32 */
33 void
34 Property_iterator::process (Moment mom)
35 {
36   Context *o = get_outlet ();
37   Music *m = get_music ();
38   bool once = to_boolean (m->get_property ("once"));
39   SCM symbol = m->get_property ("symbol");
40   SCM previous_value = SCM_UNDEFINED;
41   if (once) {
42     Context *w = o->where_defined (symbol, &previous_value);
43     if (o != w)
44       previous_value = SCM_UNDEFINED;
45   }
46   send_stream_event (o, "SetProperty", m->origin (),
47                      ly_symbol2scm ("symbol"), symbol,
48                      ly_symbol2scm ("value"), m->get_property ("value"));
49
50   /* For \once \set install a finalization hook to reset the property to the
51    * previous value after the timestep */
52   if (once)
53     {
54       Global_context *tg = get_outlet ()->get_global_context ();
55       tg->add_finalization (scm_list_4 (once_finalization_proc,
56                                         o->self_scm (), m->self_scm (),
57                                         previous_value));
58     }
59
60   Simple_music_iterator::process (mom);
61 }
62
63 void
64 Property_unset_iterator::process (Moment mom)
65 {
66   Context *o = get_outlet ();
67   Music *m = get_music ();
68   bool once = to_boolean (m->get_property ("once"));
69   SCM symbol = m->get_property ("symbol");
70   SCM previous_value = SCM_UNDEFINED;
71   if (once) {
72     Context *w = o->where_defined (symbol, &previous_value);
73     if (o != w)
74       previous_value = SCM_UNDEFINED;
75   }
76   send_stream_event (o, "UnsetProperty", m->origin (),
77                      ly_symbol2scm ("symbol"), symbol);
78
79   /* For \once \unset install a finalization hook to reset the property to the
80    * previous value after the timestep */
81   if (once && !SCM_UNBNDP (previous_value))
82     {
83       Global_context *tg = get_outlet ()->get_global_context ();
84       tg->add_finalization (scm_list_4 (Property_iterator::once_finalization_proc,
85                                         o->self_scm (), m->self_scm (),
86                                         previous_value));
87     }
88
89   Simple_music_iterator::process (mom);
90 }
91
92 MAKE_SCHEME_CALLBACK (Property_iterator, once_finalization, 3);
93 SCM
94 Property_iterator::once_finalization (SCM ctx, SCM music, SCM previous_value)
95 {
96   Music *m = unsmob_music (music);
97   Context *c = unsmob_context (ctx);
98
99   // Do not use UnsetProperty, which sets the default, but rather
100   // cache the value before the \once \set command and restore it now
101   send_stream_event (c, "SetProperty", m->origin (),
102                      ly_symbol2scm ("symbol"), m->get_property ("symbol"),
103                      ly_symbol2scm ("value"), previous_value);
104
105   return SCM_UNSPECIFIED;
106 }
107
108 void
109 Property_iterator::do_quit ()
110 {
111 }
112
113 bool
114 check_grob (Music *mus, SCM sym)
115 {
116   bool g = to_boolean (scm_object_property (sym, ly_symbol2scm ("is-grob?")));
117
118   if (!g)
119     mus->origin ()->warning (_f ("not a grob name, `%s'",
120                                  ly_symbol2string (sym)));
121
122   return g;
123 }
124
125 SCM
126 get_property_path (Music *m)
127 {
128   SCM grob_property_path = m->get_property ("grob-property-path");
129
130   SCM eprop = m->get_property ("grob-property");
131   if (scm_is_symbol (eprop))
132     {
133       grob_property_path = scm_list_1 (eprop);
134     }
135
136   return grob_property_path;
137 }
138
139 void
140 Push_property_iterator::process (Moment m)
141 {
142   SCM sym = get_music ()->get_property ("symbol");
143   if (check_grob (get_music (), sym))
144     {
145       SCM grob_property_path = get_property_path (get_music ());
146       SCM val = get_music ()->get_property ("grob-value");
147
148       if (to_boolean (get_music ()->get_property ("pop-first"))
149           && !to_boolean (get_music ()->get_property ("once")))
150         send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
151                            ly_symbol2scm ("symbol"), sym,
152                            ly_symbol2scm ("property-path"), grob_property_path);
153
154       send_stream_event (get_outlet (), "Override", get_music ()->origin (),
155                          ly_symbol2scm ("symbol"), sym,
156                          ly_symbol2scm ("property-path"), grob_property_path,
157                          ly_symbol2scm ("value"), val);
158     }
159   Simple_music_iterator::process (m);
160 }
161
162 MAKE_SCHEME_CALLBACK (Push_property_iterator, once_finalization, 2);
163 SCM
164 Push_property_iterator::once_finalization (SCM ctx, SCM music)
165 {
166   Music *mus = unsmob_music (music);
167   Context *c = unsmob_context (ctx);
168
169   SCM sym = mus->get_property ("symbol");
170   if (check_grob (mus, sym))
171     {
172       SCM grob_property_path = get_property_path (mus);
173
174       send_stream_event (c, "Revert", mus->origin (),
175                          ly_symbol2scm ("symbol"), sym,
176                          ly_symbol2scm ("property-path"), grob_property_path);
177     }
178   return SCM_UNSPECIFIED;
179 }
180
181 void
182 Push_property_iterator::do_quit ()
183 {
184   if (to_boolean (get_music ()->get_property ("once")))
185     {
186       SCM trans = get_outlet ()->self_scm ();
187       SCM music = get_music ()->self_scm ();
188
189       Global_context *tg = get_outlet ()->get_global_context ();
190       tg->add_finalization (scm_list_3 (once_finalization_proc,
191                                         trans, music));
192     }
193 }
194
195 void
196 Pop_property_iterator::process (Moment m)
197 {
198   SCM sym = get_music ()->get_property ("symbol");
199
200   if (check_grob (get_music (), sym))
201     {
202       SCM grob_property_path = get_property_path (get_music ());
203
204       send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
205                          ly_symbol2scm ("symbol"), sym,
206                          ly_symbol2scm ("property-path"), grob_property_path);
207     }
208   Simple_music_iterator::process (m);
209 }
210
211 IMPLEMENT_CTOR_CALLBACK (Pop_property_iterator);
212 IMPLEMENT_CTOR_CALLBACK (Push_property_iterator);
213 IMPLEMENT_CTOR_CALLBACK (Property_iterator);
214 IMPLEMENT_CTOR_CALLBACK (Property_unset_iterator);