]> git.donarmstrong.com Git - lilypond.git/blob - lily/context.cc
* input/test/piano-staff-distance.ly: new file.
[lilypond.git] / lily / context.cc
1 /*   
2   context.cc --  implement Context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "translator-group.hh"
11 #include "context-def.hh"
12 #include "context.hh"
13 #include "warn.hh"
14 #include "music-output-def.hh"
15 #include "scm-hash.hh"
16 #include "main.hh"
17 #include "ly-smobs.icc"
18 #include "score-context.hh"
19
20 bool
21 Context::is_removable () const
22 {
23   return context_list_ == SCM_EOL && ! iterator_count_ &&
24           !dynamic_cast<Score_context const*> (this)
25     ;
26 }
27
28 void
29 Context::check_removal ()
30 {
31   for (SCM p = context_list_; gh_pair_p (p); p = gh_cdr (p))
32     {
33       Context *trg =  unsmob_context (ly_car (p));
34
35       trg->check_removal ();
36       if (trg->is_removable ())
37         {
38           recurse_down_translators (trg, &Translator::finalize, DOWN);
39           remove_context (trg);
40         }
41     }
42 }
43
44 Context::Context (Context const&)
45 {
46   assert (false);
47 }
48
49 Scheme_hash_table*
50 Context::properties_dict () const
51 {
52   return Scheme_hash_table::unsmob (properties_scm_);
53 }
54
55 void
56 Context::add_context (Context*t)
57 {
58   SCM ts = t->self_scm ();
59   context_list_ = gh_append2 (context_list_,
60                               gh_cons (ts, SCM_EOL));
61   
62   t->daddy_context_ = this;
63   if (!t->init_)
64     {
65       t->init_ = true;
66       
67       scm_gc_unprotect_object (ts);
68       Context_def * td = unsmob_context_def (t->definition_);
69
70       /*
71         this can not move before add_context(), because \override
72         operations require that we are in the hierarchy.
73       */
74       td->apply_default_property_operations (t);
75
76       recurse_down_translators (t, &Translator::initialize, DOWN);
77     }
78 }
79
80 Context::Context ()
81 {
82   daddy_context_ = 0;
83   init_ = false;
84   iterator_count_  = 0;
85   implementation_ = SCM_EOL;
86   properties_scm_ = SCM_EOL;
87   accepts_list_ = SCM_EOL;
88   context_list_ = SCM_EOL;
89   definition_ = SCM_EOL;
90   
91   smobify_self ();
92
93   properties_scm_ = (new Scheme_hash_table)->self_scm ();
94   scm_gc_unprotect_object (properties_scm_);
95 }
96
97 Context *
98 Context::find_existing_context (SCM n, String id)
99 {
100   if ((is_alias (n) && (id_string_ == id || id.is_empty ()))
101       || n == ly_symbol2scm ("Current"))
102     return this;
103
104   Context* r = 0;
105   for (SCM p = context_list_; !r && gh_pair_p (p); p = ly_cdr (p))
106     {
107       Context *  t = unsmob_context (ly_car (p));
108       
109       r = dynamic_cast<Context*> (t)->find_existing_context (n, id);
110     }
111
112   return r;
113 }
114
115
116 Context*
117 Context::find_create_context (SCM n, String id,
118                               SCM operations)
119 {
120   /*
121     Don't create multiple score contexts.
122    */
123   if (dynamic_cast<Global_context*> (this)
124       && dynamic_cast<Global_context*> (this)->get_score_context ())
125     return get_score_context ()->find_create_context (n, id, operations);
126     
127   
128   Context * existing = find_existing_context (n,id);
129   if (existing)
130     return existing;
131
132   if (n == ly_symbol2scm ("Bottom"))
133     {
134       Context* tg = get_default_interpreter ();
135       tg->id_string_ = id;
136       return tg;
137     }
138
139   /*
140     TODO: use accepts_list_.
141    */
142   Link_array<Context_def> path
143     = unsmob_context_def (definition_)->path_to_acceptable_context (n, get_output_def ());
144
145   if (path.size ())
146     {
147       Context * current = this;
148
149       // start at 1.  The first one (index 0) will be us.
150       for (int i=0; i < path.size (); i++)
151         {
152           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
153
154           Context * new_group
155             = path[i]->instantiate (ops);
156
157           if (i == path.size () -1)
158             {
159               new_group->id_string_ = id;
160             }
161
162           current->add_context (new_group);
163           apply_property_operations (new_group, ops);
164           
165           current = new_group;
166         }
167
168       return current;
169     }
170
171   Context *ret = 0;
172   if (daddy_context_)
173     ret = daddy_context_->find_create_context (n, id, operations);
174   else
175     {
176       warning (_f ("Cannot find or create `%s' called `%s'",
177                    ly_symbol2string (n).to_str0 (), id));
178       ret =0;
179     }
180   return ret;
181 }
182
183 /*
184   Default child context as a SCM string, or something else if there is
185   none.
186 */
187 SCM
188 default_child_context_name (Context const *tg)
189 {
190   return gh_pair_p (tg->accepts_list_)
191     ? ly_car (scm_last_pair (tg->accepts_list_))
192     : SCM_EOL;
193 }
194
195
196 bool
197 Context::is_bottom_context () const
198 {
199   return !gh_symbol_p (default_child_context_name (this));
200 }
201
202 Context*
203 Context::get_default_interpreter ()
204 {
205   if (!is_bottom_context ())
206     {
207       SCM nm = default_child_context_name (this);
208       SCM st = get_output_def ()->find_context_def (nm);
209
210       Context_def *t = unsmob_context_def (st);
211       if (!t)
212         {
213           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
214           t = unsmob_context_def (this->definition_);
215         }
216       Context *tg = t->instantiate (SCM_EOL);
217       add_context (tg);
218       if (!tg->is_bottom_context ())
219         return tg->get_default_interpreter ();
220       else
221         return tg;
222     }
223   return this;
224 }
225
226 /*
227   PROPERTIES
228  */
229 Context*
230 Context::where_defined (SCM sym) const
231 {
232   if (properties_dict ()->contains (sym))
233     {
234       return (Context*)this;
235     }
236
237   return (daddy_context_) ? daddy_context_->where_defined (sym) : 0;
238 }
239
240 /*
241   return SCM_EOL when not found.
242 */
243 SCM
244 Context::internal_get_property (SCM sym) const
245 {
246   SCM val =SCM_EOL;
247   if (properties_dict ()->try_retrieve (sym, &val))
248     return val;
249
250   if (daddy_context_)
251     return daddy_context_->internal_get_property (sym);
252   
253   return val;
254 }
255
256 bool
257 Context::is_alias (SCM sym) const
258 {
259   if (sym == ly_symbol2scm ("Bottom")
260       && !gh_pair_p (accepts_list_))
261     return true;
262   return unsmob_context_def (definition_)->is_alias (sym);
263 }
264
265 void
266 Context::internal_set_property (SCM sym, SCM val)
267 {
268 #ifndef NDEBUG
269   if (internal_type_checking_global_b)
270     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
271 #endif
272   
273   properties_dict ()->set (sym, val);
274 }
275
276 /*
277   TODO: look up to check whether we have inherited var? 
278  */
279 void
280 Context::unset_property (SCM sym)
281 {
282   properties_dict ()->remove (sym);
283 }
284
285 /**
286    Remove a context from the hierarchy.
287  */
288 Context *
289 Context::remove_context (Context*trans)
290 {
291   assert (trans);
292
293   context_list_ = scm_delq_x (trans->self_scm (), context_list_);
294   trans->daddy_context_ = 0;
295   return trans;
296 }
297
298 /*
299   ID == "" means accept any ID.
300  */
301 Context *
302 find_context_below (Context * where,
303                     String type, String id)
304 {
305   if (where->is_alias (ly_symbol2scm (type.to_str0 ())))
306     {
307       if (id == "" || where->id_string_ == id)
308         return where;
309     }
310   
311   Context * found = 0;
312   for (SCM s = where->context_list_;
313        !found && gh_pair_p (s); s = gh_cdr (s))
314     {
315       Context * tr = unsmob_context (gh_car (s));
316
317       found = find_context_below (tr, type, id);
318     }
319
320   return found; 
321 }
322
323 SCM
324 Context::properties_as_alist () const
325 {
326   return properties_dict()->to_alist();
327 }
328
329 String
330 Context::context_name () const
331 {
332   Context_def * td = unsmob_context_def (definition_ );
333   return ly_symbol2string (td->get_context_name ());
334 }
335
336
337 Score_context*
338 Context::get_score_context () const
339 {
340   if (Score_context *sc =dynamic_cast<Score_context*> ((Context*)this))
341     return sc;
342   else if (daddy_context_)
343     return daddy_context_->get_score_context ();
344   else
345     return 0;
346 }
347
348 Music_output_def *
349 Context::get_output_def () const
350 {
351   return  (daddy_context_)
352     ? daddy_context_->get_output_def () : 0;
353 }
354
355 Context::~Context()
356 {
357   
358 }
359
360 Moment
361 Context::now_mom () const
362 {
363   return daddy_context_->now_mom();
364 }
365
366 int
367 Context::print_smob (SCM s, SCM port, scm_print_state *)
368 {
369   Context *sc = (Context *) ly_cdr (s);
370      
371   scm_puts ("#<", port);
372   scm_puts (classname (sc), port);
373   if (Context_def *d=unsmob_context_def (sc->definition_))
374     {
375       scm_puts (" ", port);
376       scm_display (d->get_context_name (), port);
377     }
378
379   if (Context *td=dynamic_cast<Context *> (sc))
380     {
381       scm_puts ("=", port);
382       scm_puts (td->id_string_.to_str0 (), port);
383     }
384   
385
386   scm_puts (" ", port);
387
388   scm_display (sc->context_list_, port);
389   scm_puts (" >", port);
390   
391   return 1;
392 }
393
394 SCM
395 Context::mark_smob (SCM sm)
396 {
397   Context * me = (Context*) SCM_CELL_WORD_1 (sm);
398   
399   scm_gc_mark (me->context_list_);
400   scm_gc_mark (me->definition_);  
401   scm_gc_mark (me->properties_scm_);  
402   scm_gc_mark (me->accepts_list_);
403   scm_gc_mark (me->implementation_);
404
405   return me->properties_scm_;
406 }
407
408 IMPLEMENT_SMOBS (Context);
409 IMPLEMENT_DEFAULT_EQUAL_P (Context);
410 IMPLEMENT_TYPE_P(Context,"ly:context?");
411
412 bool
413 Context::try_music (Music* m)
414 {
415   Translator*  t = unsmob_translator (implementation_);
416   if (!t)
417     return false;
418   
419   bool b = t->try_music (m);
420   if (!b && daddy_context_)
421     b = daddy_context_->try_music (m);
422
423   return b;
424 }
425
426
427 Global_context*
428 Context::get_global_context () const
429 {
430   if (dynamic_cast<Global_context *>((Context*) this))
431     return dynamic_cast<Global_context *> ((Context*) this);
432
433   if (daddy_context_)
434     return daddy_context_->get_global_context ();
435
436   programming_error ("No Global context!");
437   return 0;
438 }