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