]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
f31a1283afa7fe1e4862736bf07630d503ff4172
[lilypond.git] / lily / global-context.cc
1 /*
2   global-context.cc -- implement Global_context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "global-context.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "context-def.hh"
15 #include "dispatcher.hh"
16 #include "international.hh"
17 #include "music-iterator.hh"
18 #include "music.hh"
19 #include "output-def.hh"
20 #include "warn.hh"
21
22 Global_context::Global_context (Output_def *o)
23   : Context ()
24 {
25   output_def_ = o;
26   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
27
28   now_mom_.set_infinite (-1);
29   prev_mom_.set_infinite (-1);
30
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_);
37
38   Context_def *globaldef = unsmob_context_def (definition_);
39   if (!globaldef)
40     programming_error ("no `Global' context found");
41   else
42     globaldef->apply_default_property_operations (this);
43   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
44 }
45
46 Output_def *
47 Global_context::get_output_def () const
48 {
49   return output_def_;
50 }
51
52 void
53 Global_context::add_moment_to_process (Moment m)
54 {
55   if (m < now_mom_)
56     programming_error ("trying to freeze in time");
57
58   for (vsize i = 0; i < extra_mom_pq_.size (); i++)
59     if (extra_mom_pq_[i] == m)
60       return;
61   extra_mom_pq_.insert (m);
62 }
63
64 Moment
65 Global_context::sneaky_insert_extra_moment (Moment w)
66 {
67   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
68     w = extra_mom_pq_.get ();
69   return w;
70 }
71
72 int
73 Global_context::get_moments_left () const
74 {
75   return extra_mom_pq_.size ();
76 }
77
78 IMPLEMENT_LISTENER (Global_context, prepare);
79 void
80 Global_context::prepare (SCM sev)
81 {
82   Stream_event *ev = unsmob_stream_event (sev);
83   Moment *mom = unsmob_moment (ev->get_property ("moment"));
84
85   assert (mom);
86
87   if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
88     prev_mom_ = *mom;
89   else
90     prev_mom_ = now_mom_;
91   now_mom_ = *mom;
92 }
93
94 Moment
95 Global_context::now_mom () const
96 {
97   return now_mom_;
98 }
99
100 Context *
101 Global_context::get_score_context () const
102 {
103   return (scm_is_pair (context_list_))
104     ? unsmob_context (scm_car (context_list_))
105     : 0;
106 }
107
108 SCM
109 Global_context::get_output ()
110 {
111   Context * c = get_score_context ();
112   if (c)
113     return c->get_property ("output");
114   else
115     return SCM_EOL;
116 }
117
118 void
119 Global_context::run_iterator_on_me (Music_iterator *iter)
120 {
121   prev_mom_.set_infinite (-1);
122   now_mom_.set_infinite (-1);
123   Moment final_mom = iter->get_music ()->get_length ();
124
125   bool first = true;
126   while (iter->ok () || get_moments_left ())
127     {
128       Moment w;
129       w.set_infinite (1);
130       if (iter->ok ())
131         w = iter->pending_moment ();
132
133       w = sneaky_insert_extra_moment (w);
134       if (w.main_part_.is_infinity () || w > final_mom)
135         break;
136
137       if (w == prev_mom_)
138         {
139           programming_error ("Moment is not increasing. Aborting interpretation.");
140           break ;
141         }
142
143       
144       if (first)
145         {
146           /*
147             Need this to get grace notes at start of a piece correct.
148           */
149           first = false;
150           set_property ("measurePosition", w.smobbed_copy ());
151         }
152
153       send_stream_event (this, "Prepare", 0,
154                          ly_symbol2scm ("moment"), w.smobbed_copy ());
155
156       if (iter->ok ())
157         iter->process (w);
158
159       send_stream_event (this, "OneTimeStep", 0, 0);
160       apply_finalizations ();
161       check_removal ();
162     }
163 }
164
165 void
166 Global_context::apply_finalizations ()
167 {
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))
171
172     /* TODO: make safe.  */
173     scm_primitive_eval (scm_car (s));
174 }
175
176 /* Add a function to execute before stepping to the next time step.  */
177 void
178 Global_context::add_finalization (SCM x)
179 {
180   SCM lst = get_property ("finalizations");
181   lst = scm_cons (x, lst);
182   set_property ("finalizations", lst);
183 }
184
185 Moment
186 Global_context::previous_moment () const
187 {
188   return prev_mom_;
189 }
190
191 Context *
192 Global_context::get_default_interpreter ()
193 {
194   if (get_score_context ())
195     return get_score_context ()->get_default_interpreter ();
196   else
197     return Context::get_default_interpreter ();
198 }
199
200 Global_context *
201 unsmob_global_context (SCM x)
202 {
203   return dynamic_cast<Global_context *> (unsmob_context (x));
204 }