]> git.donarmstrong.com Git - lilypond.git/blob - lily/property-iterator.cc
Imported Upstream version 2.16.0
[lilypond.git] / lily / property-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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   SCM previous_value = o->get_property (m->get_property ("symbol"));
39   send_stream_event (o, "SetProperty", m->origin (),
40                      ly_symbol2scm ("symbol"), m->get_property ("symbol"),
41                      ly_symbol2scm ("value"), m->get_property ("value"));
42
43   /* For \once \set install a finalization hook to reset the property to the
44    * previous value after the timestep */
45   if (to_boolean (m->get_property ("once")))
46     {
47       Global_context *tg = get_outlet ()->get_global_context ();
48       tg->add_finalization (scm_list_n (once_finalization_proc,
49                                         o->self_scm (), m->self_scm (),
50                                         ly_quote_scm (previous_value), SCM_UNDEFINED));
51     }
52
53   Simple_music_iterator::process (mom);
54 }
55
56 void
57 Property_unset_iterator::process (Moment m)
58 {
59   SCM sym = get_music ()->get_property ("symbol");
60   send_stream_event (get_outlet (), "UnsetProperty", get_music ()->origin (),
61                      ly_symbol2scm ("symbol"), sym);
62
63   Simple_music_iterator::process (m);
64 }
65
66 MAKE_SCHEME_CALLBACK (Property_iterator, once_finalization, 3);
67 SCM
68 Property_iterator::once_finalization (SCM ctx, SCM music, SCM previous_value)
69 {
70   Music *m = unsmob_music (music);
71   Context *c = unsmob_context (ctx);
72
73   // Do not use UnsetProperty, which sets the default, but rather
74   // cache the value before the \once \set command and restore it now
75   send_stream_event (c, "SetProperty", m->origin (),
76                      ly_symbol2scm ("symbol"), m->get_property ("symbol"),
77                      ly_symbol2scm ("value"), previous_value);
78
79   return SCM_UNSPECIFIED;
80 }
81
82 void
83 Property_iterator::do_quit ()
84 {
85 }
86
87 bool
88 check_grob (Music *mus, SCM sym)
89 {
90   bool g = to_boolean (scm_object_property (sym, ly_symbol2scm ("is-grob?")));
91
92   if (!g)
93     mus->origin ()->warning (_f ("not a grob name, `%s'",
94                                  ly_symbol2string (sym)));
95
96   return g;
97 }
98
99 SCM
100 get_property_path (Music *m)
101 {
102   SCM grob_property_path = m->get_property ("grob-property-path");
103
104   SCM eprop = m->get_property ("grob-property");
105   if (scm_is_symbol (eprop))
106     {
107       grob_property_path = scm_list_1 (eprop);
108     }
109
110   return grob_property_path;
111 }
112
113 void
114 Push_property_iterator::process (Moment m)
115 {
116   SCM sym = get_music ()->get_property ("symbol");
117   if (check_grob (get_music (), sym))
118     {
119       SCM grob_property_path = get_property_path (get_music ());
120       SCM val = get_music ()->get_property ("grob-value");
121
122       if (to_boolean (get_music ()->get_property ("pop-first"))
123           && !to_boolean (get_music ()->get_property ("once")))
124         send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
125                            ly_symbol2scm ("symbol"), sym,
126                            ly_symbol2scm ("property-path"), grob_property_path);
127
128       send_stream_event (get_outlet (), "Override", get_music ()->origin (),
129                          ly_symbol2scm ("symbol"), sym,
130                          ly_symbol2scm ("property-path"), grob_property_path,
131                          ly_symbol2scm ("value"), val);
132     }
133   Simple_music_iterator::process (m);
134 }
135
136 MAKE_SCHEME_CALLBACK (Push_property_iterator, once_finalization, 2);
137 SCM
138 Push_property_iterator::once_finalization (SCM ctx, SCM music)
139 {
140   Music *mus = unsmob_music (music);
141   Context *c = unsmob_context (ctx);
142
143   SCM sym = mus->get_property ("symbol");
144   if (check_grob (mus, sym))
145     {
146       SCM grob_property_path = get_property_path (mus);
147
148       send_stream_event (c, "Revert", mus->origin (),
149                          ly_symbol2scm ("symbol"), sym,
150                          ly_symbol2scm ("property-path"), grob_property_path);
151     }
152   return SCM_UNSPECIFIED;
153 }
154
155 void
156 Push_property_iterator::do_quit ()
157 {
158   if (to_boolean (get_music ()->get_property ("once")))
159     {
160       SCM trans = get_outlet ()->self_scm ();
161       SCM music = get_music ()->self_scm ();
162
163       Global_context *tg = get_outlet ()->get_global_context ();
164       tg->add_finalization (scm_list_n (once_finalization_proc,
165                                         trans, music, SCM_UNDEFINED));
166     }
167 }
168
169 void
170 Pop_property_iterator::process (Moment m)
171 {
172   SCM sym = get_music ()->get_property ("symbol");
173
174   if (check_grob (get_music (), sym))
175     {
176       SCM grob_property_path = get_property_path (get_music ());
177
178       send_stream_event (get_outlet (), "Revert", get_music ()->origin (),
179                          ly_symbol2scm ("symbol"), sym,
180                          ly_symbol2scm ("property-path"), grob_property_path);
181     }
182   Simple_music_iterator::process (m);
183 }
184
185 IMPLEMENT_CTOR_CALLBACK (Pop_property_iterator);
186 IMPLEMENT_CTOR_CALLBACK (Push_property_iterator);
187 IMPLEMENT_CTOR_CALLBACK (Property_iterator);
188 IMPLEMENT_CTOR_CALLBACK (Property_unset_iterator);