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)
27 definition_ = find_context_def (o, ly_symbol2scm ("Global"));
29 now_mom_.set_infinite (-1);
30 prev_mom_.set_infinite (-1);
32 /* We only need the most basic stuff to bootstrap the context tree */
33 event_source ()->add_listener (GET_LISTENER (create_context_from_event),
34 ly_symbol2scm ("CreateContext"));
35 event_source ()->add_listener (GET_LISTENER (prepare),
36 ly_symbol2scm ("Prepare"));
37 events_below ()->register_as_listener (event_source_);
39 Context_def *globaldef = unsmob_context_def (definition_);
41 programming_error ("no `Global' context found");
43 globaldef->apply_default_property_operations (this);
44 accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
48 Global_context::get_output_def () const
54 Global_context::add_moment_to_process (Moment m)
57 programming_error ("trying to freeze in time");
59 for (vsize i = 0; i < extra_mom_pq_.size (); i++)
60 if (extra_mom_pq_[i] == m)
62 extra_mom_pq_.insert (m);
66 Global_context::sneaky_insert_extra_moment (Moment w)
68 while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
69 w = extra_mom_pq_.get ();
74 Global_context::get_moments_left () const
76 return extra_mom_pq_.size ();
79 IMPLEMENT_LISTENER (Global_context, prepare);
81 Global_context::prepare (SCM sev)
83 Stream_event *ev = unsmob_stream_event (sev);
84 Moment *mom = unsmob_moment (ev->get_property ("moment"));
88 if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
96 Global_context::now_mom () const
102 Global_context::get_score_context () const
104 return (scm_is_pair (context_list_))
105 ? unsmob_context (scm_car (context_list_))
110 Global_context::get_output ()
112 Context * c = get_score_context ();
114 return c->get_property ("output");
120 Global_context::run_iterator_on_me (Music_iterator *iter)
122 prev_mom_.set_infinite (-1);
123 now_mom_.set_infinite (-1);
124 Moment final_mom = iter->get_music ()->get_length ();
127 while (iter->ok () || get_moments_left ())
132 w = iter->pending_moment ();
134 w = sneaky_insert_extra_moment (w);
135 if (w.main_part_.is_infinity () || w > final_mom)
140 programming_error ("Moment is not increasing. Aborting interpretation.");
148 Need this to get grace notes at start of a piece correct.
151 set_property ("measurePosition", w.smobbed_copy ());
154 send_stream_event (this, "Prepare", 0,
155 ly_symbol2scm ("moment"), w.smobbed_copy ());
160 send_stream_event (this, "OneTimeStep", 0, 0);
161 apply_finalizations ();
167 Global_context::apply_finalizations ()
169 SCM lst = get_property ("finalizations");
170 set_property ("finalizations", SCM_EOL);
171 for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
173 /* TODO: make safe. */
174 scm_primitive_eval (scm_car (s));
177 /* Add a function to execute before stepping to the next time step. */
179 Global_context::add_finalization (SCM x)
181 SCM lst = get_property ("finalizations");
182 lst = scm_cons (x, lst);
183 set_property ("finalizations", lst);
187 Global_context::previous_moment () const
193 Global_context::get_default_interpreter ()
195 if (get_score_context ())
196 return get_score_context ()->get_default_interpreter ();
198 return Context::get_default_interpreter ();