]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
new file, move from
[lilypond.git] / lily / global-context.cc
1 /*
2   global-translator.cc -- implement global_context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <stdio.h>
9
10 #include "warn.hh"
11 #include "music.hh"
12 #include "event.hh"
13 #include "music-list.hh"
14 #include "music-iterator.hh"
15 #include "global-context.hh"
16 #include "score-context.hh"
17 #include "context-def.hh"
18 #include "output-def.hh"
19 #include "grace-fixup.hh"
20
21 Global_context::Global_context (Output_def *o, Moment final)
22 {
23   output_def_ = o;
24   final_mom_ = final;
25   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
26   unsmob_context_def (definition_)->apply_default_property_operations (this);
27   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
28 }
29
30 Output_def* 
31 Global_context::get_output_def () const
32 {
33   return output_def_;
34 }
35
36 void
37 Global_context::add_moment_to_process (Moment m)
38 {
39   if (m > final_mom_)
40     return;
41
42   if (m < now_mom_)
43     programming_error ("Trying to freeze in time.");
44   
45   for (int i=0; i <  extra_mom_pq_.size (); i++)
46     if (extra_mom_pq_[i] == m)
47       return;
48   extra_mom_pq_.insert (m);
49 }
50
51 Moment
52 Global_context::sneaky_insert_extra_moment (Moment w)
53 {
54   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
55     w = extra_mom_pq_.get ();
56   return w;
57 }
58
59 int
60 Global_context::get_moments_left () const
61 {
62   return extra_mom_pq_.size ();
63 }
64
65 void
66 Global_context::prepare (Moment m)
67 {
68   prev_mom_  = now_mom_;
69   now_mom_ = m;
70
71   if (get_score_context ())
72     get_score_context ()->prepare (m);
73 }
74
75 Moment
76 Global_context::now_mom () const
77 {
78   return now_mom_;
79 }
80
81 Score_context*
82 Global_context::get_score_context () const
83 {
84   return (ly_c_pair_p (context_list_))
85     ? dynamic_cast<Score_context*> (unsmob_context (ly_car (context_list_)))
86     : 0;
87 }
88
89 Music_output*
90 Global_context::get_output ()
91 {
92   return get_score_context ()->get_output ();
93 }
94
95 void
96 Global_context::one_time_step ()
97 {
98   get_score_context ()->one_time_step ();
99   apply_finalizations ();
100   check_removal ();
101 }
102
103 void
104 Global_context::finish ()
105 {
106   if (get_score_context ())
107     get_score_context ()->finish ();
108 }
109
110 void
111 Global_context::run_iterator_on_me (Music_iterator * iter)
112 {
113   if (iter-> ok ())
114     prev_mom_ = now_mom_ = iter->pending_moment ();
115
116   bool first = true;
117   while (iter->ok () || get_moments_left ())
118     {
119       Moment w;
120       w.set_infinite (1);
121       if (iter->ok ())
122         {
123           w = iter->pending_moment ();
124         }
125
126       w = sneaky_insert_extra_moment (w);
127       if (w.main_part_.is_infinity ())
128         break ;
129       
130       if (first)
131         {
132           /*
133             Need this to get grace notes at start of a piece correct.
134            */
135           first = false;
136           set_property ("measurePosition", w.smobbed_copy ());
137         }
138
139
140       prepare (w);
141
142       if (iter->ok ())
143         iter->process (w);
144
145       if (!get_score_context ()) 
146         {
147           SCM key = ly_symbol2scm ("Score");
148           Context_def * t = unsmob_context_def (find_context_def (get_output_def (), key));
149           if (!t)
150             error (_f ("can't find `%s' context", "Score"));
151
152           Context *c = t->instantiate (SCM_EOL);
153           add_context (c);
154
155           Score_context *sc = dynamic_cast<Score_context*> (c);
156           sc->prepare (w);
157         }
158       
159       one_time_step ();
160     }
161 }
162
163 void
164 Global_context::apply_finalizations ()
165 {
166   SCM lst = get_property ("finalizations");
167   set_property ("finalizations" , SCM_EOL); 
168   for (SCM s = lst ; ly_c_pair_p (s); s = ly_cdr (s))
169     {
170       scm_primitive_eval (ly_car (s)); // TODO: make safe.
171     }
172 }
173
174 /*
175   Add a function to execute before stepping to the next time step.
176 */
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 }