2 global-context.cc -- implement Global_context
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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"
23 Global_context::Global_context (Output_def *o, Object_key *key)
24 : Context (new Lilypond_context_key (key,
29 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
31 now_mom_.set_infinite (-1);
32 prev_mom_.set_infinite (-1);
34 /* We only need the most basic stuff to bootstrap the context tree */
35 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
36 ly_symbol2scm ("CreateContext"));
37 event_source ()->add_listener (GET_LISTENER (prepare),
38 ly_symbol2scm ("Prepare"));
39 events_below ()->register_as_listener (event_source_);
41 Context_def *globaldef = unsmob_context_def (definition_);
43 programming_error ("no `Global' context found");
45 globaldef->apply_default_property_operations (this);
46 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
50 Global_context::get_output_def () const
56 Global_context::add_moment_to_process (Moment m)
59 programming_error ("trying to freeze in time");
61 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
62 if (extra_mom_pq_[i] == m)
64 extra_mom_pq_.insert (m);
68 Global_context::sneaky_insert_extra_moment (Moment w)
70 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
71 w = extra_mom_pq_.get ();
76 Global_context::get_moments_left () const
78 return extra_mom_pq_.size ();
81 IMPLEMENT_LISTENER (Global_context, prepare);
83 Global_context::prepare (SCM sev)
85 Stream_event *ev = unsmob_stream_event (sev);
86 Moment *mom = unsmob_moment (ev->get_property ("moment"));
90 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
98 Global_context::now_mom () const
104 Global_context::get_score_context () const
106 return (scm_is_pair (context_list_))
107 ? unsmob_context (scm_car (context_list_))
112 Global_context::get_output ()
114 Context * c = get_score_context ();
116 return c->get_property ("output");
122 Global_context::run_iterator_on_me (Music_iterator *iter)
124 prev_mom_.set_infinite (-1);
125 now_mom_.set_infinite (-1);
126 Moment final_mom = iter->get_music ()->get_length ();
129 while (iter->ok () || get_moments_left ())
134 w = iter->pending_moment ();
136 w = sneaky_insert_extra_moment (w);
137 if (w.main_part_.is_infinity () || w > final_mom)
142 programming_error ("Moment is not increasing. Aborting interpretation.");
150 Need this to get grace notes at start of a piece correct.
153 set_property ("measurePosition", w.smobbed_copy ());
156 send_stream_event (this, "Prepare", 0,
157 ly_symbol2scm ("moment"), w.smobbed_copy ());
162 send_stream_event (this, "OneTimeStep", 0, 0);
163 apply_finalizations ();
169 Global_context::apply_finalizations ()
171 SCM lst = get_property ("finalizations");
172 set_property ("finalizations", SCM_EOL);
173 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
175 /* TODO: make safe. */
176 scm_primitive_eval (scm_car (s));
179 /* Add a function to execute before stepping to the next time step. */
181 Global_context::add_finalization (SCM x)
183 SCM lst = get_property ("finalizations");
184 lst = scm_cons (x, lst);
185 set_property ("finalizations", lst);
189 Global_context::previous_moment () const
195 Global_context::get_default_interpreter ()
197 if (get_score_context ())
198 return get_score_context ()->get_default_interpreter ();
200 return Context::get_default_interpreter ();