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 "music-iterator.hh"
19 #include "output-def.hh"
22 Global_context::Global_context (Output_def *o)
26 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
28 now_mom_.set_infinite (-1);
29 prev_mom_.set_infinite (-1);
31 /* We only need the most basic stuff to bootstrap the context tree */
32 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
33 ly_symbol2scm ("CreateContext"));
34 event_source ()->add_listener (GET_LISTENER (prepare),
35 ly_symbol2scm ("Prepare"));
36 events_below ()->register_as_listener (event_source_);
38 Context_def *globaldef = unsmob_context_def (definition_);
40 programming_error ("no `Global' context found");
42 globaldef->apply_default_property_operations (this);
43 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
47 Global_context::get_output_def () const
53 Global_context::add_moment_to_process (Moment m)
56 programming_error ("trying to freeze in time");
58 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
59 if (extra_mom_pq_[i] == m)
61 extra_mom_pq_.insert (m);
65 Global_context::sneaky_insert_extra_moment (Moment w)
67 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
68 w = extra_mom_pq_.get ();
73 Global_context::get_moments_left () const
75 return extra_mom_pq_.size ();
78 IMPLEMENT_LISTENER (Global_context, prepare);
80 Global_context::prepare (SCM sev)
82 Stream_event *ev = unsmob_stream_event (sev);
83 Moment *mom = unsmob_moment (ev->get_property ("moment"));
87 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
95 Global_context::now_mom () const
101 Global_context::get_score_context () const
103 return (scm_is_pair (context_list_))
104 ? unsmob_context (scm_car (context_list_))
109 Global_context::get_output ()
111 Context * c = get_score_context ();
113 return c->get_property ("output");
119 Global_context::run_iterator_on_me (Music_iterator *iter)
121 prev_mom_.set_infinite (-1);
122 now_mom_.set_infinite (-1);
123 Moment final_mom = iter->get_music ()->get_length ();
126 while (iter->ok () || get_moments_left ())
131 w = iter->pending_moment ();
133 w = sneaky_insert_extra_moment (w);
134 if (w.main_part_.is_infinity () || w > final_mom)
139 programming_error ("Moment is not increasing. Aborting interpretation.");
147 Need this to get grace notes at start of a piece correct.
150 set_property ("measurePosition", w.smobbed_copy ());
153 send_stream_event (this, "Prepare", 0,
154 ly_symbol2scm ("moment"), w.smobbed_copy ());
159 send_stream_event (this, "OneTimeStep", 0, 0);
160 apply_finalizations ();
166 Global_context::apply_finalizations ()
168 SCM lst = get_property ("finalizations");
169 set_property ("finalizations", SCM_EOL);
170 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
172 /* TODO: make safe. */
173 scm_primitive_eval (scm_car (s));
176 /* Add a function to execute before stepping to the next time step. */
178 Global_context::add_finalization (SCM x)
180 SCM lst = get_property ("finalizations");
181 lst = scm_cons (x, lst);
182 set_property ("finalizations", lst);
186 Global_context::previous_moment () const
192 Global_context::get_default_interpreter ()
194 if (get_score_context ())
195 return get_score_context ()->get_default_interpreter ();
197 return Context::get_default_interpreter ();
201 unsmob_global_context (SCM x)
203 return dynamic_cast<Global_context *> (unsmob_context (x));