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"
14 #include "context-def.hh"
15 #include "dispatcher.hh"
16 #include "international.hh"
17 #include "lilypond-key.hh"
18 #include "music-iterator.hh"
20 #include "output-def.hh"
21 #include "score-context.hh"
22 #include "stream-event.hh"
25 Global_context::Global_context (Output_def *o, Object_key *key)
26 : Context (new Lilypond_context_key (key,
31 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
33 now_mom_.set_infinite (-1);
34 prev_mom_.set_infinite (-1);
36 /* We only need the most basic stuff to bootstrap the context tree */
37 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
38 ly_symbol2scm ("CreateContext"));
39 event_source ()->add_listener (GET_LISTENER (prepare),
40 ly_symbol2scm ("Prepare"));
41 events_below ()->register_as_listener (event_source_);
43 Context_def *globaldef = unsmob_context_def (definition_);
45 programming_error ("no `Global' context found");
47 globaldef->apply_default_property_operations (this);
48 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
52 Global_context::get_output_def () const
58 Global_context::add_moment_to_process (Moment m)
61 programming_error ("trying to freeze in time");
63 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
64 if (extra_mom_pq_[i] == m)
66 extra_mom_pq_.insert (m);
70 Global_context::sneaky_insert_extra_moment (Moment w)
72 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
73 w = extra_mom_pq_.get ();
78 Global_context::get_moments_left () const
80 return extra_mom_pq_.size ();
83 IMPLEMENT_LISTENER (Global_context, prepare);
85 Global_context::prepare (SCM sev)
87 Stream_event *ev = unsmob_stream_event (sev);
88 Moment *mom = unsmob_moment (ev->get_property ("moment"));
92 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
98 clear_key_disambiguations ();
100 // should do nothing now
101 if (get_score_context ())
102 get_score_context ()->prepare (now_mom_);
106 Global_context::now_mom () const
112 Global_context::get_score_context () const
114 return (scm_is_pair (context_list_))
115 ? dynamic_cast<Score_context *> (unsmob_context (scm_car (context_list_)))
120 Global_context::get_output ()
122 return get_score_context ()->get_output ();
126 Global_context::one_time_step ()
128 get_score_context ()->one_time_step ();
132 Global_context::finish ()
134 if (get_score_context ())
135 get_score_context ()->finish ();
139 Global_context::run_iterator_on_me (Music_iterator *iter)
141 prev_mom_.set_infinite (-1);
142 now_mom_.set_infinite (-1);
143 Moment final_mom = iter->get_music ()->get_length ();
146 while (iter->ok () || get_moments_left ())
151 w = iter->pending_moment ();
153 w = sneaky_insert_extra_moment (w);
154 if (w.main_part_.is_infinity () || w > final_mom)
160 Need this to get grace notes at start of a piece correct.
163 set_property ("measurePosition", w.smobbed_copy ());
166 send_stream_event (this, "Prepare", 0,
167 ly_symbol2scm ("moment"), w.smobbed_copy ());
172 if (!get_score_context ())
174 error ("ES TODO: no score context, this shouldn't happen");
175 SCM sym = ly_symbol2scm ("Score");
176 Context_def *t = unsmob_context_def (find_context_def (get_output_def (),
179 error (_f ("can't find `%s' context", "Score"));
181 Object_key const *key = get_context_key ("Score", "");
182 Context *c = t->instantiate (SCM_EOL, key);
185 Score_context *sc = dynamic_cast<Score_context *> (c);
189 send_stream_event (this, "OneTimeStep", 0, 0);
190 apply_finalizations ();
196 Global_context::apply_finalizations ()
198 SCM lst = get_property ("finalizations");
199 set_property ("finalizations", SCM_EOL);
200 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
202 /* TODO: make safe. */
203 scm_primitive_eval (scm_car (s));
206 /* Add a function to execute before stepping to the next time step. */
208 Global_context::add_finalization (SCM x)
210 SCM lst = get_property ("finalizations");
211 lst = scm_cons (x, lst);
212 set_property ("finalizations", lst);
216 Global_context::previous_moment () const
222 Global_context::get_default_interpreter ()
224 if (get_score_context ())
225 return get_score_context ()->get_default_interpreter ();
227 return Context::get_default_interpreter ();