]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
* lily/include/global-context.hh (Context): clean-up data hiding.
[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 "music-output-def.hh"
19 #include "grace-fixup.hh"
20
21 Global_context::Global_context (Music_output_def *o, Moment final)
22 {
23   output_def_ = o;
24   final_mom_ = final;
25   definition_ = o->find_context_def (ly_symbol2scm ("Global"));
26   unsmob_context_def (definition_)->apply_default_property_operations (this);
27   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
28 }
29
30 Music_output_def* 
31 Global_context::get_output_def () const
32 {
33   return output_def_;
34 }
35
36 void
37 Global_context::add_moment_to_process (Moment m)
38 {
39   if (m > final_mom_)
40     return;
41
42   if (m < now_mom_)
43     programming_error ("Trying to freeze in time.");
44   
45   for (int i=0; i <  extra_mom_pq_.size (); i++)
46     if (extra_mom_pq_[i] == m)
47       return;
48   extra_mom_pq_.insert (m);
49 }
50
51 Moment
52 Global_context::sneaky_insert_extra_moment (Moment w)
53 {
54   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
55     w = extra_mom_pq_.get ();
56   return w;
57 }
58
59 int
60 Global_context::get_moments_left () const
61 {
62   return extra_mom_pq_.size ();
63 }
64
65 void
66 Global_context::prepare (Moment m)
67 {
68   prev_mom_  = now_mom_;
69   now_mom_ = m;
70
71   if (get_score_context ())
72     get_score_context ()->prepare (m);
73 }
74
75 Moment
76 Global_context::now_mom () const
77 {
78   return now_mom_;
79 }
80
81 Score_context*
82 Global_context::get_score_context () const
83 {
84   return (gh_pair_p (context_list_))
85     ? dynamic_cast<Score_context*> (unsmob_context (gh_car (context_list_)))
86     : 0;
87 }
88
89 Music_output*
90 Global_context::get_output ()
91 {
92   return get_score_context ()->get_output ();
93 }
94
95 void
96 Global_context::one_time_step ()
97 {
98   get_score_context ()->one_time_step ();
99   apply_finalizations ();
100   check_removal ();
101 }
102
103 void
104 Global_context::finish ()
105 {
106   if (get_score_context ())
107     get_score_context ()->finish ();
108 }
109
110 void
111 Global_context::run_iterator_on_me (Music_iterator * iter)
112 {
113   if (iter-> ok ())
114     prev_mom_ = now_mom_ = iter->pending_moment ();
115
116   bool first = true;
117   while (iter->ok () || get_moments_left ())
118     {
119       Moment w;
120       w.set_infinite (1);
121       if (iter->ok ())
122         {
123           w = iter->pending_moment ();
124         }
125
126       w = sneaky_insert_extra_moment (w);
127       if (w.main_part_.is_infinity ())
128         break ;
129       
130       prepare (w);
131
132       if (iter->ok ())
133         iter->process (w);
134
135       if (!get_score_context ()) 
136         {
137           SCM key = ly_symbol2scm ("Score");
138           Context_def * t = unsmob_context_def (get_output_def ()
139                                                 ->find_context_def (key));
140           if (!t)
141             error (_f ("can't find `%s' context", "Score"));
142
143           Context *c = t->instantiate (SCM_EOL);
144           add_context (c);
145
146           Score_context *sc = dynamic_cast<Score_context*> (c);
147           sc->prepare (w);
148         }
149       
150       one_time_step ();
151       first = false;
152     }
153 }
154
155 void
156 Global_context::apply_finalizations ()
157 {
158   SCM lst = get_property ("finalizations");
159   set_property ("finalizations" , SCM_EOL); 
160   for (SCM s = lst ; gh_pair_p (s); s = gh_cdr (s))
161     {
162       scm_primitive_eval (gh_car (s));
163     }
164 }
165
166 /*
167   Add a function to execute before stepping to the next time step.
168 */
169 void
170 Global_context::add_finalization (SCM x)
171 {
172   SCM lst = get_property ("finalizations");
173   lst = scm_cons (x, lst);
174   set_property ("finalizations" ,lst); 
175 }
176
177 Moment
178 Global_context::previous_moment () const
179 {
180   return prev_mom_;
181 }