]> 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", ""))
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   if (get_score_context ())
84     get_score_context ()->prepare (m);
85 }
86
87 Moment
88 Global_context::now_mom () const
89 {
90   return now_mom_;
91 }
92
93 Score_context*
94 Global_context::get_score_context () const
95 {
96   return (scm_is_pair (context_list_))
97     ? dynamic_cast<Score_context*> (unsmob_context (scm_car (context_list_)))
98     : 0;
99 }
100
101 Music_output*
102 Global_context::get_output ()
103 {
104   return get_score_context ()->get_output ();
105 }
106
107 void
108 Global_context::one_time_step ()
109 {
110   get_score_context ()->one_time_step ();
111   apply_finalizations ();
112   check_removal ();
113 }
114
115 void
116 Global_context::finish ()
117 {
118   if (get_score_context ())
119     get_score_context ()->finish ();
120 }
121
122 void
123 Global_context::run_iterator_on_me (Music_iterator * iter)
124 {
125   if (iter-> ok ())
126     prev_mom_ = now_mom_ = iter->pending_moment ();
127
128   bool first = true;
129   while (iter->ok () || get_moments_left ())
130     {
131       Moment w;
132       w.set_infinite (1);
133       if (iter->ok ())
134         {
135           w = iter->pending_moment ();
136         }
137
138       w = sneaky_insert_extra_moment (w);
139       if (w.main_part_.is_infinity ())
140         break ;
141       
142       if (first)
143         {
144           /*
145             Need this to get grace notes at start of a piece correct.
146            */
147           first = false;
148           set_property ("measurePosition", w.smobbed_copy ());
149         }
150
151
152       prepare (w);
153
154       if (iter->ok ())
155         iter->process (w);
156
157       if (!get_score_context ()) 
158         {
159           SCM sym = ly_symbol2scm ("Score");
160           Context_def * t = unsmob_context_def (find_context_def (get_output_def (), sym));
161           if (!t)
162             error (_f ("can't find `%s' context", "Score"));
163
164           Object_key *key = new Lilypond_context_key (get_key(), now_mom(),
165                                                       "Score", "");
166           Context *c = t->instantiate (SCM_EOL, key);
167           add_context (c);
168           scm_gc_unprotect_object (key->self_scm());
169
170           Score_context *sc = dynamic_cast<Score_context*> (c);
171           sc->prepare (w);
172         }
173       
174       one_time_step ();
175     }
176 }
177
178 void
179 Global_context::apply_finalizations ()
180 {
181   SCM lst = get_property ("finalizations");
182   set_property ("finalizations", SCM_EOL);
183   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
184     /* TODO: make safe.  */
185     scm_primitive_eval (scm_car (s));
186 }
187
188 /* Add a function to execute before stepping to the next time step.  */
189 void
190 Global_context::add_finalization (SCM x)
191 {
192   SCM lst = get_property ("finalizations");
193   lst = scm_cons (x, lst);
194   set_property ("finalizations", lst); 
195 }
196
197 Moment
198 Global_context::previous_moment () const
199 {
200   return prev_mom_;
201 }
202
203 Context *
204 Global_context::get_default_interpreter ()
205 {
206   if (get_score_context ())
207     return get_score_context ()->get_default_interpreter ();
208   else
209     return Context::get_default_interpreter ();
210 }