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,
29 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
31 Context_def *globaldef = unsmob_context_def (definition_);
34 programming_error ("No `Global' context found.");
37 globaldef->apply_default_property_operations (this);
38 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
42 Global_context::get_output_def () const
48 Global_context::add_moment_to_process (Moment m)
54 programming_error ("Trying to freeze in time.");
56 for (int i = 0; i < extra_mom_pq_.size (); i++)
57 if (extra_mom_pq_[i] == m)
59 extra_mom_pq_.insert (m);
63 Global_context::sneaky_insert_extra_moment (Moment w)
65 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
66 w = extra_mom_pq_.get ();
71 Global_context::get_moments_left () const
73 return extra_mom_pq_.size ();
77 Global_context::prepare (Moment m)
82 clear_key_disambiguations ();
83 if (get_score_context ())
84 get_score_context ()->prepare (m);
89 Global_context::now_mom () const
95 Global_context::get_score_context () const
97 return (scm_is_pair (context_list_))
98 ? dynamic_cast<Score_context*> (unsmob_context (scm_car (context_list_)))
103 Global_context::get_output ()
105 return get_score_context ()->get_output ();
109 Global_context::one_time_step ()
111 get_score_context ()->one_time_step ();
112 apply_finalizations ();
117 Global_context::finish ()
119 if (get_score_context ())
120 get_score_context ()->finish ();
124 Global_context::run_iterator_on_me (Music_iterator * iter)
127 prev_mom_ = now_mom_ = iter->pending_moment ();
130 while (iter->ok () || get_moments_left ())
136 w = iter->pending_moment ();
139 w = sneaky_insert_extra_moment (w);
140 if (w.main_part_.is_infinity ())
146 Need this to get grace notes at start of a piece correct.
149 set_property ("measurePosition", w.smobbed_copy ());
158 if (!get_score_context ())
160 SCM sym = ly_symbol2scm ("Score");
161 Context_def * t = unsmob_context_def (find_context_def (get_output_def (), sym));
163 error (_f ("can't find `%s' context", "Score"));
165 Object_key const *key = get_context_key ("Score", "");
166 Context *c = t->instantiate (SCM_EOL, key);
169 Score_context *sc = dynamic_cast<Score_context*> (c);
178 Global_context::apply_finalizations ()
180 SCM lst = get_property ("finalizations");
181 set_property ("finalizations", SCM_EOL);
182 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
183 /* TODO: make safe. */
184 scm_primitive_eval (scm_car (s));
187 /* Add a function to execute before stepping to the next time step. */
189 Global_context::add_finalization (SCM x)
191 SCM lst = get_property ("finalizations");
192 lst = scm_cons (x, lst);
193 set_property ("finalizations", lst);
197 Global_context::previous_moment () const
203 Global_context::get_default_interpreter ()
205 if (get_score_context ())
206 return get_score_context ()->get_default_interpreter ();
208 return Context::get_default_interpreter ();