2 global-context.cc -- implement Global_context
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "global-context.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_);
33 programming_error ("no `Global' context found");
35 globaldef->apply_default_property_operations (this);
36 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
40 Global_context::get_output_def () const
46 Global_context::add_moment_to_process (Moment m)
52 programming_error ("trying to freeze in time");
54 for (int i = 0; i < extra_mom_pq_.size (); i++)
55 if (extra_mom_pq_[i] == m)
57 extra_mom_pq_.insert (m);
61 Global_context::sneaky_insert_extra_moment (Moment w)
63 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
64 w = extra_mom_pq_.get ();
69 Global_context::get_moments_left () const
71 return extra_mom_pq_.size ();
75 Global_context::prepare (Moment m)
80 clear_key_disambiguations ();
81 if (get_score_context ())
82 get_score_context ()->prepare (m);
86 Global_context::now_mom () const
92 Global_context::get_score_context () const
94 return (scm_is_pair (context_list_))
95 ? dynamic_cast<Score_context *> (unsmob_context (scm_car (context_list_)))
100 Global_context::get_output ()
102 return get_score_context ()->get_output ();
106 Global_context::one_time_step ()
108 get_score_context ()->one_time_step ();
109 apply_finalizations ();
114 Global_context::finish ()
116 if (get_score_context ())
117 get_score_context ()->finish ();
121 Global_context::run_iterator_on_me (Music_iterator *iter)
124 prev_mom_ = now_mom_ = iter->pending_moment ();
127 while (iter->ok () || get_moments_left ())
132 w = iter->pending_moment ();
134 w = sneaky_insert_extra_moment (w);
135 if (w.main_part_.is_infinity ())
141 Need this to get grace notes at start of a piece correct.
144 set_property ("measurePosition", w.smobbed_copy ());
152 if (!get_score_context ())
154 SCM sym = ly_symbol2scm ("Score");
155 Context_def *t = unsmob_context_def (find_context_def (get_output_def (),
158 error (_f ("can't find `%s' context", "Score"));
160 Object_key const *key = get_context_key ("Score", "");
161 Context *c = t->instantiate (SCM_EOL, key);
164 Score_context *sc = dynamic_cast<Score_context *> (c);
173 Global_context::apply_finalizations ()
175 SCM lst = get_property ("finalizations");
176 set_property ("finalizations", SCM_EOL);
177 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
179 /* TODO: make safe. */
180 scm_primitive_eval (scm_car (s));
183 /* Add a function to execute before stepping to the next time step. */
185 Global_context::add_finalization (SCM x)
187 SCM lst = get_property ("finalizations");
188 lst = scm_cons (x, lst);
189 set_property ("finalizations", lst);
193 Global_context::previous_moment () const
199 Global_context::get_default_interpreter ()
201 if (get_score_context ())
202 return get_score_context ()->get_default_interpreter ();
204 return Context::get_default_interpreter ();