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