2 global-translator.cc -- implement global_context
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "global-context.hh"
15 #include "music-list.hh"
16 #include "music-iterator.hh"
17 #include "score-context.hh"
18 #include "context-def.hh"
19 #include "output-def.hh"
20 #include "lilypond-key.hh"
22 Global_context::Global_context (Output_def *o, Moment final, Object_key *key)
23 : Context (new Lilypond_context_key(key,
27 scm_gc_unprotect_object (key_->self_scm());
31 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
33 Context_def *globaldef = unsmob_context_def (definition_);
36 programming_error ("No `Global' context found.");
39 globaldef->apply_default_property_operations (this);
40 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
44 Global_context::get_output_def () const
50 Global_context::add_moment_to_process (Moment m)
56 programming_error ("Trying to freeze in time.");
58 for (int i=0; i < extra_mom_pq_.size (); i++)
59 if (extra_mom_pq_[i] == m)
61 extra_mom_pq_.insert (m);
65 Global_context::sneaky_insert_extra_moment (Moment w)
67 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
68 w = extra_mom_pq_.get ();
73 Global_context::get_moments_left () const
75 return extra_mom_pq_.size ();
79 Global_context::prepare (Moment m)
84 clear_key_disambiguations ();
85 if (get_score_context ())
86 get_score_context ()->prepare (m);
91 Global_context::now_mom () const
97 Global_context::get_score_context () const
99 return (scm_is_pair (context_list_))
100 ? dynamic_cast<Score_context*> (unsmob_context (scm_car (context_list_)))
105 Global_context::get_output ()
107 return get_score_context ()->get_output ();
111 Global_context::one_time_step ()
113 get_score_context ()->one_time_step ();
114 apply_finalizations ();
119 Global_context::finish ()
121 if (get_score_context ())
122 get_score_context ()->finish ();
126 Global_context::run_iterator_on_me (Music_iterator * iter)
129 prev_mom_ = now_mom_ = iter->pending_moment ();
132 while (iter->ok () || get_moments_left ())
138 w = iter->pending_moment ();
141 w = sneaky_insert_extra_moment (w);
142 if (w.main_part_.is_infinity ())
148 Need this to get grace notes at start of a piece correct.
151 set_property ("measurePosition", w.smobbed_copy ());
160 if (!get_score_context ())
162 SCM sym = ly_symbol2scm ("Score");
163 Context_def * t = unsmob_context_def (find_context_def (get_output_def (), sym));
165 error (_f ("can't find `%s' context", "Score"));
167 Object_key const *key = get_context_key ("Score", "");
168 Context *c = t->instantiate (SCM_EOL, key);
170 scm_gc_unprotect_object (key->self_scm());
172 Score_context *sc = dynamic_cast<Score_context*> (c);
181 Global_context::apply_finalizations ()
183 SCM lst = get_property ("finalizations");
184 set_property ("finalizations", SCM_EOL);
185 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
186 /* TODO: make safe. */
187 scm_primitive_eval (scm_car (s));
190 /* Add a function to execute before stepping to the next time step. */
192 Global_context::add_finalization (SCM x)
194 SCM lst = get_property ("finalizations");
195 lst = scm_cons (x, lst);
196 set_property ("finalizations", lst);
200 Global_context::previous_moment () const
206 Global_context::get_default_interpreter ()
208 if (get_score_context ())
209 return get_score_context ()->get_default_interpreter ();
211 return Context::get_default_interpreter ();