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"
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)
96 clear_key_disambiguations ();
100 Global_context::now_mom () const
106 Global_context::get_score_context () const
108 return (scm_is_pair (context_list_))
109 ? unsmob_context (scm_car (context_list_))
114 Global_context::get_output ()
116 Context * c = get_score_context ();
118 return c->get_property ("output");
124 Global_context::run_iterator_on_me (Music_iterator *iter)
126 prev_mom_.set_infinite (-1);
127 now_mom_.set_infinite (-1);
128 Moment final_mom = iter->get_music ()->get_length ();
131 while (iter->ok () || get_moments_left ())
136 w = iter->pending_moment ();
138 w = sneaky_insert_extra_moment (w);
139 if (w.main_part_.is_infinity () || w > final_mom)
144 programming_error ("Moment is not increasing. Aborting interpretation.");
152 Need this to get grace notes at start of a piece correct.
155 set_property ("measurePosition", w.smobbed_copy ());
158 send_stream_event (this, "Prepare", 0,
159 ly_symbol2scm ("moment"), w.smobbed_copy ());
164 send_stream_event (this, "OneTimeStep", 0, 0);
165 apply_finalizations ();
171 Global_context::apply_finalizations ()
173 SCM lst = get_property ("finalizations");
174 set_property ("finalizations", SCM_EOL);
175 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
177 /* TODO: make safe. */
178 scm_primitive_eval (scm_car (s));
181 /* Add a function to execute before stepping to the next time step. */
183 Global_context::add_finalization (SCM x)
185 SCM lst = get_property ("finalizations");
186 lst = scm_cons (x, lst);
187 set_property ("finalizations", lst);
191 Global_context::previous_moment () const
197 Global_context::get_default_interpreter ()
199 if (get_score_context ())
200 return get_score_context ()->get_default_interpreter ();
202 return Context::get_default_interpreter ();