]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
* lily/grob.cc (Grob): idem. Plugs mem leaks.
[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
9 #include "global-context.hh"
10
11 #include <cstdio>
12
13 #include "warn.hh"
14 #include "event.hh"
15 #include "music-list.hh"
16 #include "music-iterator.hh"
17 #include "score-context.hh"
18 #include "context-def.hh"
19 #include "output-def.hh"
20 #include "lilypond-key.hh"
21
22 Global_context::Global_context (Output_def *o, Moment final, Object_key *key)
23   : Context (new Lilypond_context_key(key,
24                                       Moment(0),
25                                       "Global", "", 0))
26 {
27   output_def_ = o;
28   final_mom_ = final;
29   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
30
31   Context_def *globaldef  =  unsmob_context_def (definition_);
32   if (!globaldef)
33     {
34       programming_error ("No `Global' context found.");
35     }
36   else
37     globaldef->apply_default_property_operations (this);
38   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
39 }
40
41 Output_def* 
42 Global_context::get_output_def () const
43 {
44   return output_def_;
45 }
46
47 void
48 Global_context::add_moment_to_process (Moment m)
49 {
50   if (m > final_mom_)
51     return;
52
53   if (m < now_mom_)
54     programming_error ("Trying to freeze in time.");
55   
56   for (int i=0; i <  extra_mom_pq_.size (); i++)
57     if (extra_mom_pq_[i] == m)
58       return;
59   extra_mom_pq_.insert (m);
60 }
61
62 Moment
63 Global_context::sneaky_insert_extra_moment (Moment w)
64 {
65   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
66     w = extra_mom_pq_.get ();
67   return w;
68 }
69
70 int
71 Global_context::get_moments_left () const
72 {
73   return extra_mom_pq_.size ();
74 }
75
76 void
77 Global_context::prepare (Moment m)
78 {
79   prev_mom_  = now_mom_;
80   now_mom_ = m;
81
82   clear_key_disambiguations ();
83   if (get_score_context ())
84     get_score_context ()->prepare (m);
85   
86 }
87
88 Moment
89 Global_context::now_mom () const
90 {
91   return now_mom_;
92 }
93
94 Score_context*
95 Global_context::get_score_context () const
96 {
97   return (scm_is_pair (context_list_))
98     ? dynamic_cast<Score_context*> (unsmob_context (scm_car (context_list_)))
99     : 0;
100 }
101
102 Music_output*
103 Global_context::get_output ()
104 {
105   return get_score_context ()->get_output ();
106 }
107
108 void
109 Global_context::one_time_step ()
110 {
111   get_score_context ()->one_time_step ();
112   apply_finalizations ();
113   check_removal ();
114 }
115
116 void
117 Global_context::finish ()
118 {
119   if (get_score_context ())
120     get_score_context ()->finish ();
121 }
122
123 void
124 Global_context::run_iterator_on_me (Music_iterator * iter)
125 {
126   if (iter-> ok ())
127     prev_mom_ = now_mom_ = iter->pending_moment ();
128
129   bool first = true;
130   while (iter->ok () || get_moments_left ())
131     {
132       Moment w;
133       w.set_infinite (1);
134       if (iter->ok ())
135         {
136           w = iter->pending_moment ();
137         }
138
139       w = sneaky_insert_extra_moment (w);
140       if (w.main_part_.is_infinity ())
141         break ;
142       
143       if (first)
144         {
145           /*
146             Need this to get grace notes at start of a piece correct.
147            */
148           first = false;
149           set_property ("measurePosition", w.smobbed_copy ());
150         }
151
152
153       prepare (w);
154
155       if (iter->ok ())
156         iter->process (w);
157
158       if (!get_score_context ()) 
159         {
160           SCM sym = ly_symbol2scm ("Score");
161           Context_def * t = unsmob_context_def (find_context_def (get_output_def (), sym));
162           if (!t)
163             error (_f ("can't find `%s' context", "Score"));
164
165           Object_key const *key = get_context_key ("Score", "");
166           Context *c = t->instantiate (SCM_EOL, key);
167           add_context (c);
168
169           Score_context *sc = dynamic_cast<Score_context*> (c);
170           sc->prepare (w);
171         }
172       
173       one_time_step ();
174     }
175 }
176
177 void
178 Global_context::apply_finalizations ()
179 {
180   SCM lst = get_property ("finalizations");
181   set_property ("finalizations", SCM_EOL);
182   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
183     /* TODO: make safe.  */
184     scm_primitive_eval (scm_car (s));
185 }
186
187 /* Add a function to execute before stepping to the next time step.  */
188 void
189 Global_context::add_finalization (SCM x)
190 {
191   SCM lst = get_property ("finalizations");
192   lst = scm_cons (x, lst);
193   set_property ("finalizations", lst); 
194 }
195
196 Moment
197 Global_context::previous_moment () const
198 {
199   return prev_mom_;
200 }
201
202 Context *
203 Global_context::get_default_interpreter ()
204 {
205   if (get_score_context ())
206     return get_score_context ()->get_default_interpreter ();
207   else
208     return Context::get_default_interpreter ();
209 }