]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-translator.cc
eded9743bcd17b6dc9da549f515c3828e9a619d1
[lilypond.git] / lily / global-translator.cc
1 /*
2   global-translator.cc -- implement Global_translator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2003 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-translator.hh"
16
17 Global_translator::Global_translator ()
18 {
19 }
20
21 void
22 Global_translator::add_moment_to_process (Moment m)
23 {
24   if (m  > final_mom_)
25     return;
26
27   if (m < now_mom_)
28     programming_error ("Trying to freeze in time.");
29   
30   for (int i=0; i <  extra_mom_pq_.size (); i++)
31     if (extra_mom_pq_[i] == m)
32       return;
33   extra_mom_pq_.insert (m);
34 }
35
36 Moment
37 Global_translator::sneaky_insert_extra_moment (Moment w)
38 {
39   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
40     w = extra_mom_pq_.get ();
41   return w;
42 }
43
44 int
45 Global_translator::get_moments_left () const
46 {
47   return extra_mom_pq_.size ();
48 }
49
50 void
51 Global_translator::prepare (Moment m)
52 {
53   prev_mom_  = now_mom_;
54   now_mom_ = m;
55 }
56
57 Moment
58 Global_translator::now_mom () const
59 {
60   return now_mom_;
61 }
62
63
64
65 Music_output*
66 Global_translator::get_output ()
67 {
68   return 0;
69 }
70
71 void
72 Global_translator::one_time_step ()
73 {
74 }
75
76 void
77 Global_translator::start ()
78 {
79 }
80 void
81 Global_translator::finish ()
82 {
83 }
84
85 void
86 Global_translator::run_iterator_on_me (Music_iterator * iter)
87 {
88   if (iter-> ok ())
89     prev_mom_ = now_mom_ = iter->pending_moment ();
90
91   bool first = true;
92   while (iter->ok () || get_moments_left ())
93     {
94       Moment w;
95       w.set_infinite (1);
96       if (iter->ok ())
97         {
98           w = iter->pending_moment ();
99         }
100
101       w = sneaky_insert_extra_moment (w);
102       if (w.main_part_.is_infinity ())
103         break ;
104       
105       
106       //      printf ("proccing %s\n ",       w.to_string ().to_str0 ());
107       if (first)
108         {
109           first = false;
110           set_property ("measurePosition", w.smobbed_copy ());
111         }
112
113       prepare (w);
114       if (iter->ok ())
115         iter->process (w);
116       
117       one_time_step ();
118     }
119 }
120
121 void
122 Global_translator::apply_finalizations ()
123 {
124   SCM lst = get_property ("finalizations");
125   set_property ("finalizations" , SCM_EOL); 
126   for (SCM s = lst ; gh_pair_p (s); s = gh_cdr (s))
127     {
128       scm_primitive_eval (gh_car (s));
129     }
130 }
131
132 /*
133    Add a function to execute before stepping to the next time step.
134  */
135 void
136 Global_translator::add_finalization (SCM x)
137 {
138   SCM lst = get_property ("finalizations");
139   lst = scm_cons (x, lst);
140   set_property ("finalizations" ,lst); 
141 }
142
143
144 Global_translator *
145 Translator::top_translator()const
146 {
147   if (dynamic_cast<Global_translator*>((Translator*)this))
148     return dynamic_cast<Global_translator*> ((Translator*)this);
149
150   if (daddy_trans_)
151     return daddy_trans_->top_translator ();
152
153   programming_error ("No top translator!");
154   return 0;
155 }