2 global-context.cc -- implement Global_context
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "global-context.hh"
15 #include "music-iterator.hh"
16 #include "score-context.hh"
17 #include "context-def.hh"
18 #include "output-def.hh"
19 #include "lilypond-key.hh"
21 Global_context::Global_context (Output_def
*o
, Moment final
, Object_key
*key
)
22 : Context (new Lilypond_context_key (key
,
28 definition_
= find_context_def (o
, ly_symbol2scm ("Global"));
30 Context_def
*globaldef
= unsmob_context_def (definition_
);
33 programming_error ("no `Global' context found");
36 globaldef
->apply_default_property_operations (this);
37 accepts_list_
= scm_list_1 (ly_symbol2scm ("Score"));
41 Global_context::get_output_def () const
47 Global_context::add_moment_to_process (Moment m
)
53 programming_error ("trying to freeze in time");
55 for (int i
= 0; i
< extra_mom_pq_
.size (); i
++)
56 if (extra_mom_pq_
[i
] == m
)
58 extra_mom_pq_
.insert (m
);
62 Global_context::sneaky_insert_extra_moment (Moment w
)
64 while (extra_mom_pq_
.size () && extra_mom_pq_
.front () <= w
)
65 w
= extra_mom_pq_
.get ();
70 Global_context::get_moments_left () const
72 return extra_mom_pq_
.size ();
76 Global_context::prepare (Moment m
)
81 clear_key_disambiguations ();
82 if (get_score_context ())
83 get_score_context ()->prepare (m
);
87 Global_context::now_mom () const
93 Global_context::get_score_context () const
95 return (scm_is_pair (context_list_
))
96 ? dynamic_cast<Score_context
*> (unsmob_context (scm_car (context_list_
)))
101 Global_context::get_output ()
103 return get_score_context ()->get_output ();
107 Global_context::one_time_step ()
109 get_score_context ()->one_time_step ();
110 apply_finalizations ();
115 Global_context::finish ()
117 if (get_score_context ())
118 get_score_context ()->finish ();
122 Global_context::run_iterator_on_me (Music_iterator
*iter
)
125 prev_mom_
= now_mom_
= iter
->pending_moment ();
128 while (iter
->ok () || get_moments_left ())
134 w
= iter
->pending_moment ();
137 w
= sneaky_insert_extra_moment (w
);
138 if (w
.main_part_
.is_infinity ())
144 Need this to get grace notes at start of a piece correct.
147 set_property ("measurePosition", w
.smobbed_copy ());
155 if (!get_score_context ())
157 SCM sym
= ly_symbol2scm ("Score");
158 Context_def
*t
= unsmob_context_def (find_context_def (get_output_def (),
161 error (_f ("can't find `%s' context", "Score"));
163 Object_key
const *key
= get_context_key ("Score", "");
164 Context
*c
= t
->instantiate (SCM_EOL
, key
);
167 Score_context
*sc
= dynamic_cast<Score_context
*> (c
);
176 Global_context::apply_finalizations ()
178 SCM lst
= get_property ("finalizations");
179 set_property ("finalizations", SCM_EOL
);
180 for (SCM s
= lst
; scm_is_pair (s
); s
= scm_cdr (s
))
182 /* TODO: make safe. */
183 scm_primitive_eval (scm_car (s
));
186 /* Add a function to execute before stepping to the next time step. */
188 Global_context::add_finalization (SCM x
)
190 SCM lst
= get_property ("finalizations");
191 lst
= scm_cons (x
, lst
);
192 set_property ("finalizations", lst
);
196 Global_context::previous_moment () const
202 Global_context::get_default_interpreter ()
204 if (get_score_context ())
205 return get_score_context ()->get_default_interpreter ();
207 return Context::get_default_interpreter ();