]> git.donarmstrong.com Git - lilypond.git/blob - src/inputcommands.cc
ef2014604c2a5a1f025189b580986f271c272077
[lilypond.git] / src / inputcommands.cc
1 #include "inputcommands.hh"
2 #include "inputcommand.hh"
3 #include "debug.hh"
4 #include "staffcommands.hh"
5 #include "getcommand.hh"
6 #include "command.hh"
7 #include "stcol.hh"
8 #include "staff.hh"
9 #include "assoc.hh"
10
11 void
12 Commands_at::print() const
13 {
14 #ifndef NPRINT
15     mtor << "Commands_at {";
16     tdescription_.print();
17     for (iter_top(*this,cc); cc.ok(); cc++) 
18         cc->print();
19     mtor << "}\n";
20 #endif
21 }
22
23 Moment
24 Commands_at::when()
25 {
26     return tdescription_.when;
27 }
28
29 Commands_at::Commands_at(Moment dt, Commands_at* prev)
30     : tdescription_(dt, (prev)? &prev->tdescription_ : 0)
31 {
32     if (prev && !tdescription_.whole_in_measure) {
33         bottom().add(get_newmeasure_command());
34     }
35 }
36
37 void
38 Commands_at::add(Input_command *i)
39 {
40     bottom().add(i);
41
42     // should check for other meterchanges here.
43     if (i->args[0] == "METER") { 
44         int l = i->args[1];
45         int o = i->args[2];
46         tdescription_.set_meter(l,o);
47         bottom().add(get_grouping_command( get_default_grouping(l)));
48     }
49 }
50
51 Commands_at::Commands_at(Commands_at const&src) :
52     tdescription_(src.tdescription_)
53 {
54     IPointerList<Input_command*> &me(*this);
55     const IPointerList<Input_command*> &that(src);
56     
57     PL_copy(me, that);
58 }
59
60 void
61 Commands_at::setpartial(Moment p)
62 {
63     tdescription_.setpartial(p);
64 }
65
66 Moment
67 Commands_at::barleft()
68 {
69     return  tdescription_.barleft();
70 }
71
72 void
73 Commands_at::parse(Staff_commands_at*s)
74 {
75     s->tdescription_ = tdescription_;
76     for (iter_top(*this,cc); cc.ok(); cc++) {
77         if (cc->args.size() &&  cc->args[0] !="") {
78             Command c = **cc;
79             s->add(c);
80             
81         }
82     }
83 }
84 /****************/
85
86 void
87 Input_cursor::find_moment(Moment w)
88 {
89     Moment last = when();
90     while  (1) {
91         if (! ok() ) {
92             *this = list().bottom();
93             
94             Moment dt = (w - when());
95             if ( !ptr()->tdescription_.cadenza_b_ )
96                 dt = dt <? ptr()->barleft();
97
98             Commands_at * c = new Commands_at(dt, *this);
99             assert(c->when() <= w);
100             add(c);
101         } else if (when() == w ) {
102             return ;
103         } else if (when() > w )
104             break;
105         
106         
107         last = when();
108         next();
109     }
110
111     prev();
112     Moment dt = (w - when());
113     Commands_at * c = new Commands_at(dt, *this);
114     add(c);
115     next();
116 }
117
118
119
120 /****************/
121 void
122 Input_commands::find_moment(Moment m)
123 {
124     ptr.find_moment(m);
125 }
126
127 Input_commands::Input_commands(Input_commands const&src)
128     : ptr(src.ptr)
129 {
130     IPointerList<Commands_at*> &me(*this);
131     const IPointerList<Commands_at*> &that(src);
132     
133     PL_copy(me, that);    
134 }
135
136 Input_commands::Input_commands()
137     :    ptr (bottom())
138 {
139     Commands_at * p = new Commands_at(0,0);    
140     bottom().add(p);    
141     ptr = bottom();    
142 }
143
144 void
145 Input_commands::do_skip(int bars, Moment wholes)
146 {
147     while (bars > 0) {
148         Moment b = ptr->barleft();
149         ptr.find_moment(ptr->when() + b);
150         bars --;        
151     }
152     if (wholes) {
153         ptr.find_moment(ptr->when() + wholes);
154     }
155 }
156
157
158 void
159 Input_commands::add(Input_command c, Assoc<String,Moment> &marks_assoc_r)
160 {
161     String s(c.args[0]);
162     if (s == "CADENZA") {
163         ptr->tdescription_.set_cadenza((int)c.args[1]);
164     } if (s == "PARTIAL") {     
165         ptr->setpartial(c.args[1]);
166     } else if (s == "GROUPING") {
167         Input_command *ic = new Input_command(c);
168         ic->args.insert(ptr->tdescription_.one_beat, 1);
169         ptr->add(ic);
170     } else if (s == "METER") {
171         int beats_per_meas = c.args[1];
172         int one_beat = c.args[2];
173         Input_command *ch = get_meterchange_command(beats_per_meas, one_beat);
174         ptr->add(ch);           
175     } else if (s == "SKIP") {
176         int bars = c.args[1] ;
177         Moment wholes= c.args[2];
178         do_skip(bars, wholes);
179     } else if (s == "RESET") {
180         ptr= top();
181     } else if (s=="GOTO") {
182         ptr = top();
183         String m(c.args[1]);
184         if (!marks_assoc_r.elt_query(m))
185             error("Unknown marker: `" +m + "\'");
186         
187         ptr.find_moment(marks_assoc_r[m]);
188     } else {
189         Input_command *ic = new Input_command(c);
190         ptr->add(ic);
191     } 
192     
193 }
194
195 void
196 Input_commands::parse(Staff * staff_l) const
197 {
198     print();
199     for (iter_top(*this,i); i.ok(); i++) {
200
201         Staff_column* col_l = staff_l->get_col(i->when(), false);
202         if (!col_l->staff_commands_p_)
203             col_l->staff_commands_p_ = new Staff_commands_at(i->tdescription_);
204         
205         Staff_commands_at * com_l = col_l->staff_commands_p_;
206         
207         if (!i->when()) {   /* all pieces should start with a breakable. */
208             com_l->set_breakable();
209         }
210
211         i->parse(com_l);
212     }
213 }
214
215
216 void
217 Input_commands::print() const
218 {
219 #ifndef NPRINT
220     for (iter_top(*this,cc); cc.ok() ; cc++) {
221         cc->print();
222     }
223 #endif
224 }
225 /****************/
226
227 Moment
228 Input_cursor::when()const
229 {
230     return (*this)->when(); 
231 }
232 Input_cursor::Input_cursor(PCursor<Commands_at *>c)
233     : PCursor<Commands_at*>(c)
234 {
235 }