]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
* configure.in (--enable-std-vector): New option.
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "global-context.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "context-def.hh"
15 #include "international.hh"
16 #include "lilypond-key.hh"
17 #include "music-iterator.hh"
18 #include "music.hh"
19 #include "output-def.hh"
20 #include "score-context.hh"
21 #include "warn.hh"
22
23 Global_context::Global_context (Output_def *o, Moment final, Object_key *key)
24   : Context (new Lilypond_context_key (key,
25                                        Moment (0),
26                                        "Global", "", 0))
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     programming_error ("no `Global' context found");
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 (vsize 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 Moment
87 Global_context::now_mom () const
88 {
89   return now_mom_;
90 }
91
92 Score_context *
93 Global_context::get_score_context () const
94 {
95   return (scm_is_pair (context_list_))
96     ? dynamic_cast<Score_context *> (unsmob_context (scm_car (context_list_)))
97     : 0;
98 }
99
100 SCM
101 Global_context::get_output ()
102 {
103   return get_score_context ()->get_output ();
104 }
105
106 void
107 Global_context::one_time_step ()
108 {
109   get_score_context ()->one_time_step ();
110   apply_finalizations ();
111   check_removal ();
112 }
113
114 void
115 Global_context::finish ()
116 {
117   if (get_score_context ())
118     get_score_context ()->finish ();
119 }
120
121 void
122 Global_context::run_iterator_on_me (Music_iterator *iter)
123 {
124   if (iter->ok ())
125     prev_mom_ = now_mom_ = iter->pending_moment ();
126
127   bool first = true;
128   while (iter->ok () || get_moments_left ())
129     {
130       Moment w;
131       w.set_infinite (1);
132       if (iter->ok ())
133         w = iter->pending_moment ();
134
135       w = sneaky_insert_extra_moment (w);
136       if (w.main_part_.is_infinity ())
137         break;
138
139       if (first)
140         {
141           /*
142             Need this to get grace notes at start of a piece correct.
143           */
144           first = false;
145           set_property ("measurePosition", w.smobbed_copy ());
146         }
147
148       prepare (w);
149
150       if (iter->ok ())
151         iter->process (w);
152
153       if (!get_score_context ())
154         {
155           SCM sym = ly_symbol2scm ("Score");
156           Context_def *t = unsmob_context_def (find_context_def (get_output_def (),
157                                                                  sym));
158           if (!t)
159             error (_f ("can't find `%s' context", "Score"));
160
161           Object_key const *key = get_context_key ("Score", "");
162           Context *c = t->instantiate (SCM_EOL, key);
163           add_context (c);
164
165           Score_context *sc = dynamic_cast<Score_context *> (c);
166           sc->prepare (w);
167         }
168
169       one_time_step ();
170     }
171 }
172
173 void
174 Global_context::apply_finalizations ()
175 {
176   SCM lst = get_property ("finalizations");
177   set_property ("finalizations", SCM_EOL);
178   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
179
180     /* TODO: make safe.  */
181     scm_primitive_eval (scm_car (s));
182 }
183
184 /* Add a function to execute before stepping to the next time step.  */
185 void
186 Global_context::add_finalization (SCM x)
187 {
188   SCM lst = get_property ("finalizations");
189   lst = scm_cons (x, lst);
190   set_property ("finalizations", lst);
191 }
192
193 Moment
194 Global_context::previous_moment () const
195 {
196   return prev_mom_;
197 }
198
199 Context *
200 Global_context::get_default_interpreter ()
201 {
202   if (get_score_context ())
203     return get_score_context ()->get_default_interpreter ();
204   else
205     return Context::get_default_interpreter ();
206 }