]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
*** empty log message ***
[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--2005 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-iterator.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, Object_key *key)
22   : Context (new Lilypond_context_key(key,
23                                       Moment(0),
24                                       "Global", "", 0))
25 {
26   output_def_ = o;
27   final_mom_ = final;
28   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
29
30   Context_def *globaldef  =  unsmob_context_def (definition_);
31   if (!globaldef)
32     {
33       programming_error ("No `Global' context found.");
34     }
35   else
36     globaldef->apply_default_property_operations (this);
37   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
38 }
39
40 Output_def* 
41 Global_context::get_output_def () const
42 {
43   return output_def_;
44 }
45
46 void
47 Global_context::add_moment_to_process (Moment m)
48 {
49   if (m > final_mom_)
50     return;
51
52   if (m < now_mom_)
53     programming_error ("Trying to freeze in time.");
54   
55   for (int i = 0; i <  extra_mom_pq_.size (); i++)
56     if (extra_mom_pq_[i] == m)
57       return;
58   extra_mom_pq_.insert (m);
59 }
60
61 Moment
62 Global_context::sneaky_insert_extra_moment (Moment w)
63 {
64   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
65     w = extra_mom_pq_.get ();
66   return w;
67 }
68
69 int
70 Global_context::get_moments_left () const
71 {
72   return extra_mom_pq_.size ();
73 }
74
75 void
76 Global_context::prepare (Moment m)
77 {
78   prev_mom_  = now_mom_;
79   now_mom_ = m;
80
81   clear_key_disambiguations ();
82   if (get_score_context ())
83     get_score_context ()->prepare (m);
84   
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 const *key = get_context_key ("Score", "");
165           Context *c = t->instantiate (SCM_EOL, key);
166           add_context (c);
167
168           Score_context *sc = dynamic_cast<Score_context*> (c);
169           sc->prepare (w);
170         }
171       
172       one_time_step ();
173     }
174 }
175
176 void
177 Global_context::apply_finalizations ()
178 {
179   SCM lst = get_property ("finalizations");
180   set_property ("finalizations", SCM_EOL);
181   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
182     /* TODO: make safe.  */
183     scm_primitive_eval (scm_car (s));
184 }
185
186 /* Add a function to execute before stepping to the next time step.  */
187 void
188 Global_context::add_finalization (SCM x)
189 {
190   SCM lst = get_property ("finalizations");
191   lst = scm_cons (x, lst);
192   set_property ("finalizations", lst); 
193 }
194
195 Moment
196 Global_context::previous_moment () const
197 {
198   return prev_mom_;
199 }
200
201 Context *
202 Global_context::get_default_interpreter ()
203 {
204   if (get_score_context ())
205     return get_score_context ()->get_default_interpreter ();
206   else
207     return Context::get_default_interpreter ();
208 }