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