]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver-group.cc
Web-ja: update introduction
[lilypond.git] / lily / engraver-group.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "context.hh"
21 #include "dispatcher.hh"
22 #include "engraver-group.hh"
23 #include "global-context.hh"
24 #include "grob.hh"
25 #include "grob-properties.hh"
26 #include "paper-score.hh"
27 #include "translator-dispatch-list.hh"
28 #include "warn.hh"
29
30 void
31 Engraver_group::override (SCM sev)
32 {
33   Stream_event *ev = unsmob<Stream_event> (sev);
34   SCM sym = ev->get_property ("symbol");
35   Grob_property_info gpi (context (), sym);
36
37   if (to_boolean (ev->get_property ("once")))
38     {
39       SCM token = gpi.temporary_override (ev->get_property ("property-path"),
40                                           ev->get_property ("value"));
41       if (scm_is_pair (token))
42         if (Global_context *g = context ()->get_global_context ())
43           {
44             g->add_finalization (scm_list_4 (ly_context_matched_pop_property_proc,
45                                              context ()->self_scm (),
46                                              sym,
47                                              token));
48           }
49     }
50   else
51     gpi.push (ev->get_property ("property-path"),
52               ev->get_property ("value"));
53 }
54
55 void
56 Engraver_group::revert (SCM sev)
57 {
58   Stream_event *ev = unsmob<Stream_event> (sev);
59   SCM sym = ev->get_property ("symbol");
60   Grob_property_info gpi (context (), sym);
61
62   if (to_boolean (ev->get_property ("once")))
63     {
64       SCM token = gpi.temporary_revert (ev->get_property ("property-path"));
65       if (scm_is_pair (token))
66         if (Global_context *g = context ()->get_global_context ())
67           {
68             g->add_finalization (scm_list_4 (ly_context_matched_pop_property_proc,
69                                              context ()->self_scm (),
70                                              sym,
71                                              token));
72           }
73     }
74   else
75     gpi.pop (ev->get_property ("property-path"));
76 }
77
78 void
79 Engraver_group::connect_to_context (Context *c)
80 {
81   Translator_group::connect_to_context (c);
82   c->event_source ()->add_listener (GET_LISTENER (Engraver_group, override), ly_symbol2scm ("Override"));
83   c->event_source ()->add_listener (GET_LISTENER (Engraver_group, revert), ly_symbol2scm ("Revert"));
84 }
85
86 void
87 Engraver_group::disconnect_from_context ()
88 {
89   context ()->event_source ()->remove_listener (GET_LISTENER (Engraver_group, override), ly_symbol2scm ("Override"));
90   context ()->event_source ()->remove_listener (GET_LISTENER (Engraver_group, revert), ly_symbol2scm ("Revert"));
91   Translator_group::disconnect_from_context ();
92 }
93
94 void
95 Engraver_group::announce_grob (Grob_info info, Direction dir,
96                                Context *reroute_context)
97 {
98   announce_infos_.push_back (Announce_grob_info (info, dir));
99
100   Context *dad_con = reroute_context ? reroute_context
101     : context_->get_parent_context ();
102
103   Engraver_group *dad_eng
104     = dad_con
105       ? dynamic_cast<Engraver_group *> (dad_con->implementation ())
106       : 0;
107
108   if (dad_eng)
109     dad_eng->announce_grob (info, dir);
110 }
111
112 void
113 Engraver_group::acknowledge_grobs ()
114 {
115   if (!announce_infos_.size ())
116     return;
117
118   SCM name_sym = ly_symbol2scm ("name");
119
120   for (vsize j = 0; j < announce_infos_.size (); j++)
121     {
122       Announce_grob_info info = announce_infos_[j];
123
124       SCM meta = info.grob ()->get_property ("meta");
125       SCM nm = scm_assoc (name_sym, meta);
126       if (scm_is_pair (nm))
127         nm = scm_cdr (nm);
128       else
129         continue;
130
131       SCM ackhandle = scm_hashq_create_handle_x (acknowledge_hash_table_drul_[info.start_end ()],
132                                                  nm, SCM_BOOL_F);
133
134       SCM acklist = scm_cdr (ackhandle);
135
136       if (scm_is_false (acklist))
137         {
138           SCM ifaces
139             = scm_cdr (scm_assoc (ly_symbol2scm ("interfaces"), meta));
140           acklist = Engraver_dispatch_list::create (get_simple_trans_list (),
141                                                     ifaces, info.start_end ());
142
143           scm_set_cdr_x (ackhandle, acklist);
144         }
145
146       Engraver_dispatch_list *dispatch
147         = unsmob<Engraver_dispatch_list> (acklist);
148
149       if (dispatch)
150         dispatch->apply (info);
151     }
152 }
153
154 /*
155   Ugh. This is slightly expensive. We could/should cache the value of
156   the group count?
157 */
158 bool
159 Engraver_group::pending_grobs () const
160 {
161   if (!announce_infos_.empty ())
162     return true;
163   for (SCM s = context_->children_contexts ();
164        scm_is_pair (s); s = scm_cdr (s))
165     {
166       Context *c = unsmob<Context> (scm_car (s));
167       Engraver_group *group
168         = dynamic_cast<Engraver_group *> (c->implementation ());
169
170       if (group && group->pending_grobs ())
171         return true;
172     }
173   return false;
174 }
175
176 void
177 Engraver_group::do_announces ()
178 {
179   do
180     {
181       /*
182         DOCME: why is this inside the loop?
183        */
184       for (SCM s = context ()->children_contexts ();
185            scm_is_pair (s); s = scm_cdr (s))
186         {
187           Context *c = unsmob<Context> (scm_car (s));
188           Engraver_group *group
189             = dynamic_cast<Engraver_group *> (c->implementation ());
190           if (group)
191             group->do_announces ();
192         }
193
194       while (1)
195         {
196           precomputed_translator_foreach (PROCESS_ACKNOWLEDGED);
197           if (announce_infos_.size () == 0)
198             break;
199
200           acknowledge_grobs ();
201           announce_infos_.clear ();
202         }
203     }
204   while (pending_grobs ());
205 }
206
207 Preinit_Engraver_group::Preinit_Engraver_group ()
208 {
209   acknowledge_hash_table_drul_.set (SCM_EOL, SCM_EOL);
210 }
211
212 Engraver_group::Engraver_group ()
213 {
214   acknowledge_hash_table_drul_[LEFT] = scm_c_make_hash_table (61);
215   acknowledge_hash_table_drul_[RIGHT] = scm_c_make_hash_table (61);
216 }
217
218 #include "translator.icc"
219
220 ADD_TRANSLATOR_GROUP (Engraver_group,
221                       /* doc */
222                       "A group of engravers taken together.",
223
224                       /* create */
225                       "",
226
227                       /* read */
228                       "",
229
230                       /* write */
231                       ""
232                      );
233
234 void
235 Engraver_group::derived_mark () const
236 {
237   scm_gc_mark (acknowledge_hash_table_drul_[LEFT]);
238   scm_gc_mark (acknowledge_hash_table_drul_[RIGHT]);
239 }