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 "stream-event.hh"
24 Global_context::Global_context (Output_def *o, Object_key *key)
25 : Context (new Lilypond_context_key (key,
30 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
32 now_mom_.set_infinite (-1);
33 prev_mom_.set_infinite (-1);
35 /* We only need the most basic stuff to bootstrap the context tree */
36 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
37 ly_symbol2scm ("CreateContext"));
38 event_source ()->add_listener (GET_LISTENER (prepare),
39 ly_symbol2scm ("Prepare"));
40 events_below ()->register_as_listener (event_source_);
42 Context_def *globaldef = unsmob_context_def (definition_);
44 programming_error ("no `Global' context found");
46 globaldef->apply_default_property_operations (this);
47 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
51 Global_context::get_output_def () const
57 Global_context::add_moment_to_process (Moment m)
60 programming_error ("trying to freeze in time");
62 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
63 if (extra_mom_pq_[i] == m)
65 extra_mom_pq_.insert (m);
69 Global_context::sneaky_insert_extra_moment (Moment w)
71 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
72 w = extra_mom_pq_.get ();
77 Global_context::get_moments_left () const
79 return extra_mom_pq_.size ();
82 IMPLEMENT_LISTENER (Global_context, prepare);
84 Global_context::prepare (SCM sev)
86 Stream_event *ev = unsmob_stream_event (sev);
87 Moment *mom = unsmob_moment (ev->get_property ("moment"));
91 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
97 clear_key_disambiguations ();
101 Global_context::now_mom () const
107 Global_context::get_score_context () const
109 return (scm_is_pair (context_list_))
110 ? unsmob_context (scm_car (context_list_))
115 Global_context::get_output ()
117 Context * c = get_score_context ();
119 return c->get_property ("output");
125 Global_context::run_iterator_on_me (Music_iterator *iter)
127 prev_mom_.set_infinite (-1);
128 now_mom_.set_infinite (-1);
129 Moment final_mom = iter->get_music ()->get_length ();
132 while (iter->ok () || get_moments_left ())
137 w = iter->pending_moment ();
139 w = sneaky_insert_extra_moment (w);
140 if (w.main_part_.is_infinity () || w > final_mom)
146 Need this to get grace notes at start of a piece correct.
149 set_property ("measurePosition", w.smobbed_copy ());
152 send_stream_event (this, "Prepare", 0,
153 ly_symbol2scm ("moment"), w.smobbed_copy ());
158 send_stream_event (this, "OneTimeStep", 0, 0);
159 apply_finalizations ();
165 Global_context::apply_finalizations ()
167 SCM lst = get_property ("finalizations");
168 set_property ("finalizations", SCM_EOL);
169 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
171 /* TODO: make safe. */
172 scm_primitive_eval (scm_car (s));
175 /* Add a function to execute before stepping to the next time step. */
177 Global_context::add_finalization (SCM x)
179 SCM lst = get_property ("finalizations");
180 lst = scm_cons (x, lst);
181 set_property ("finalizations", lst);
185 Global_context::previous_moment () const
191 Global_context::get_default_interpreter ()
193 if (get_score_context ())
194 return get_score_context ()->get_default_interpreter ();
196 return Context::get_default_interpreter ();