]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
(updated_grob_properties): new
[lilypond.git] / lily / translator-group.cc
1 /*
2   Translator_group.cc -- implement Translator_group
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
12 #include "warn.hh"
13 #include "moment.hh"
14 #include "scm-hash.hh"
15 #include "context-def.hh"
16 #include "main.hh"
17 #include "music.hh"
18
19 Translator_group::Translator_group (Translator_group const&s)
20   : Translator (s)
21 {
22   iterator_count_ =0;
23   
24   Scheme_hash_table * tab =  new Scheme_hash_table (*s.properties_dict ());
25   properties_scm_ = tab->self_scm ();
26   scm_gc_unprotect_object (tab->self_scm ());
27 }
28
29 Scheme_hash_table*
30 Translator_group::properties_dict () const
31 {
32   return Scheme_hash_table::unsmob (properties_scm_);
33 }
34
35 Translator_group::~Translator_group ()
36 {
37   
38   //assert (is_removable ());
39 }
40
41
42 Translator_group::Translator_group ()
43 {
44   iterator_count_  = 0;
45   Scheme_hash_table *tab = new Scheme_hash_table ;
46   properties_scm_ = tab->self_scm ();
47
48   scm_gc_unprotect_object (tab->self_scm ());
49 }
50
51 void
52 Translator_group::check_removal ()
53 {
54   SCM next = SCM_EOL; 
55   for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
56     {
57       next = ly_cdr (p);
58
59       Translator_group *trg =  dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
60
61       trg->check_removal ();
62       if (trg->is_removable ())
63         terminate_translator (trg);
64     }
65 }
66
67 SCM
68 Translator_group::add_translator (SCM list, Translator *t)
69 {
70   /*
71     Must append, since list ordering must be preserved.
72    */
73   list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
74   t->daddy_trans_ = this;
75   t->output_def_ = output_def_;
76
77   return list;
78 }
79
80
81 void
82 Translator_group::add_used_group_translator (Translator *t)
83 {
84   trans_group_list_ = add_translator (trans_group_list_,t);
85 }
86
87
88 void
89 Translator_group::add_fresh_group_translator (Translator*t)
90 {
91   Translator_group*tg = dynamic_cast<Translator_group*> (t);
92   trans_group_list_ = add_translator (trans_group_list_,t);
93   scm_gc_unprotect_object (t->self_scm ());
94
95   Context_def * td = unsmob_context_def (tg->definition_);
96
97   /*
98     this can not move before add_translator(), because \override
99     operations require that we are in the hierarchy.
100    */
101   td->apply_default_property_operations (tg);
102
103   t->initialize ();
104 }
105
106 bool
107 Translator_group::try_music (Music* m)
108 {
109   bool hebbes_b = try_music_on_nongroup_children (m);
110   
111   if (!hebbes_b && daddy_trans_)
112     hebbes_b = daddy_trans_->try_music (m);
113   
114   return hebbes_b ;
115 }
116
117 void
118 Translator_group::terminate_translator (Translator*r)
119 {
120   r->finalize ();
121   /*
122     Return value ignored. GC does the rest.
123    */
124   remove_translator (r);
125 }
126
127
128 /**
129    Remove a translator from the hierarchy.
130  */
131 Translator *
132 Translator_group::remove_translator (Translator*trans)
133 {
134   assert (trans);
135
136   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
137   trans->daddy_trans_ = 0;
138   return trans;
139 }
140
141
142 static void
143 static_each (SCM list, Method_pointer method)
144 {
145   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
146     (unsmob_translator (ly_car (p))->*method) ();
147   
148 }
149
150 void
151 Translator_group::each (Method_pointer method) 
152 {
153   static_each (get_simple_trans_list (), method);
154   static_each (trans_group_list_, method);
155 }
156
157
158
159
160 /*
161   STUBS
162 */
163 void
164 Translator_group::stop_translation_timestep ()
165 {
166   each (&Translator::stop_translation_timestep);
167 }
168
169 void
170 Translator_group::start_translation_timestep ()
171 {
172   each (&Translator::start_translation_timestep);
173 }
174
175 void
176 Translator_group::do_announces ()
177 {
178   each (&Translator::do_announces);
179 }
180
181 void
182 Translator_group::initialize ()
183 {
184   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
185   set_property ("acceptHashTable", tab);
186   each (&Translator::initialize);
187 }
188
189 void
190 Translator_group::finalize ()
191 {
192   each (&Translator::finalize);
193 }
194
195 bool
196 translator_accepts_any_of (Translator*tr, SCM ifaces)
197 {
198   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
199                            tr->translator_description());
200   ack_ifs = gh_cdr (ack_ifs);
201   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
202     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
203       return true;
204   return false;
205 }
206
207 SCM
208 find_accept_translators (SCM gravlist, SCM ifaces)
209 {
210   SCM l = SCM_EOL;
211   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
212     {
213       Translator* tr = unsmob_translator (ly_car (s));
214       if (translator_accepts_any_of (tr, ifaces))
215         l = scm_cons (tr->self_scm (), l); 
216     }
217   l = scm_reverse_x (l, SCM_EOL);
218
219   return l;
220 }
221
222 bool
223 Translator_group::try_music_on_nongroup_children (Music *m )
224 {
225   SCM tab = get_property ("acceptHashTable");
226   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
227                               m->get_property_alist (false));
228
229   if (!gh_pair_p (name))
230     return false;
231
232   name = gh_cdr (name);
233   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
234   if (accept_list == SCM_BOOL_F)
235     {
236       accept_list = find_accept_translators (get_simple_trans_list (),
237                                              m->get_mus_property ("types"));
238       scm_hashq_set_x (tab, name, accept_list);
239     }
240
241   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
242     {
243       Translator * t = unsmob_translator (ly_car (p));
244       if (t && t->try_music (m))
245         return true;
246     }
247   return false;
248 }
249
250 SCM
251 Translator_group::properties_as_alist () const
252 {
253   return properties_dict()->to_alist();
254 }
255
256 String
257 Translator_group::context_name () const
258 {
259   Context_def * td = unsmob_context_def (definition_ );
260   return ly_symbol2string (td->get_context_name ());
261 }
262
263
264
265 SCM
266 names_to_translators (SCM namelist, Translator_group*tg)
267 {
268   SCM l = SCM_EOL;
269   for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
270     {
271       Translator * t = get_translator (ly_car (s));
272       if (!t)
273         warning (_f ("can't find: `%s'", s));
274       else
275         {
276           Translator * tr = t->clone ();
277           SCM str = tr->self_scm ();
278           l = gh_cons (str, l);
279
280           tr->daddy_trans_ = tg;
281           tr->output_def_  = tg->output_def_;
282
283           scm_gc_unprotect_object (str);
284         }
285     }
286   return l;
287 }
288
289
290 SCM
291 Translator_group::get_simple_trans_list ()
292 {
293   return simple_trans_list_;
294 }
295