]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
1efa379e9d2cc2194e57c97756a7e932ad0466c3
[lilypond.git] / lily / auto-change-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "context.hh"
21 #include "direction.hh"
22 #include "international.hh"
23 #include "music.hh"
24 #include "music-wrapper-iterator.hh"
25
26 class Auto_change_iterator : public Music_wrapper_iterator
27 {
28 public:
29   DECLARE_SCHEME_CALLBACK (constructor, ());
30
31   Auto_change_iterator ();
32
33 protected:
34   virtual void do_quit ();
35   virtual void construct_children ();
36   virtual void process (Moment);
37   vector<Pitch> pending_pitch (Moment) const;
38 private:
39   SCM split_list_;
40   Direction where_dir_;
41   void change_to (Music_iterator *, SCM, const string&);
42   Moment start_moment_;
43
44   Context_handle up_;
45   Context_handle down_;
46 };
47
48 void
49 Auto_change_iterator::change_to (Music_iterator *it, SCM to_type_sym,
50                                  const string &to_id)
51 {
52   Context *current = it->get_outlet ();
53   Context *last = 0;
54
55   /*
56     Cut & Paste from Change_iterator (ugh).
57
58     TODO: abstract this function
59   */
60
61   /* find the type  of translator that we're changing.
62
63   If \translator Staff = bass, then look for Staff = *
64   */
65   while (current && !current->is_alias (to_type_sym))
66     {
67       last = current;
68       current = current->get_parent_context ();
69     }
70
71   if (current && current->id_string () == to_id)
72     {
73       string msg;
74       msg += _f ("cannot change, already in translator: %s", to_id);
75     }
76
77   if (current)
78     {
79       if (last)
80         {
81           Context *dest
82             = it->get_outlet ()->find_create_context (to_type_sym, to_id, SCM_EOL);
83
84           send_stream_event (last, "ChangeParent", get_music ()->origin (),
85                              ly_symbol2scm ("context"), dest->self_scm ());
86         }
87       else
88         {
89           /*
90             We could change the current translator's id, but that would make
91             errors hard to catch
92
93           */
94           ;
95         }
96     }
97 }
98
99 void
100 Auto_change_iterator::process (Moment m)
101 {
102   Music_wrapper_iterator::process (m);
103
104   Moment now = get_outlet ()->now_mom ();
105   Moment *splitm = 0;
106   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
107     start_moment_ = now;
108
109   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
110     {
111       splitm = unsmob_moment (scm_caar (split_list_));
112       if ((*splitm + start_moment_) > now)
113         break;
114
115       SCM tag = scm_cdar (split_list_);
116       Direction d = to_dir (tag);
117
118       if (d && d != where_dir_)
119         {
120           where_dir_ = d;
121           string to_id = (d >= 0) ? "up" : "down";
122           change_to (child_iter_,
123                      ly_symbol2scm ("Staff"),
124                      to_id);
125         }
126     }
127 }
128
129 Auto_change_iterator::Auto_change_iterator ()
130 {
131   where_dir_ = CENTER;
132   split_list_ = SCM_EOL;
133 }
134
135 void
136 Auto_change_iterator::construct_children ()
137 {
138   split_list_ = get_music ()->get_property ("split-list");
139   start_moment_ = get_outlet ()->now_mom ();
140
141   SCM props = get_outlet ()->get_property ("trebleStaffProperties");
142   Context *up = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
143                                                     "up", props);
144
145   props = get_outlet ()->get_property ("bassStaffProperties");
146   Context *down = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
147                                                       "down", props);
148
149   up_.set_context (up);
150   down_.set_context (down);
151
152   Context *voice = up->find_create_context (ly_symbol2scm ("Voice"),
153                                             "", SCM_EOL);
154   set_context (voice);
155   Music_wrapper_iterator::construct_children ();
156 }
157
158 void
159 Auto_change_iterator::do_quit ()
160 {
161   up_.set_context (0);
162   down_.set_context (0);
163 }
164
165 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);