]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / auto-change-iterator.cc
1 /*
2   auto-change-iterator.cc -- implement  Auto_change_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10 #include "direction.hh"
11 #include "international.hh"
12 #include "music.hh"
13 #include "music-wrapper-iterator.hh"
14
15 class Auto_change_iterator : public Music_wrapper_iterator
16 {
17 public:
18   DECLARE_SCHEME_CALLBACK (constructor, ());
19
20   Auto_change_iterator ();
21
22 protected:
23   virtual void do_quit ();
24   virtual void construct_children ();
25   virtual void process (Moment);
26   vector<Pitch> pending_pitch (Moment) const;
27 private:
28   SCM split_list_;
29   Direction where_dir_;
30   void change_to (Music_iterator *, SCM, string);
31   Moment start_moment_;
32
33   Context_handle up_;
34   Context_handle down_;
35 };
36
37 void
38 Auto_change_iterator::change_to (Music_iterator *it, SCM to_type_sym,
39                                  string to_id)
40 {
41   Context *current = it->get_outlet ();
42   Context *last = 0;
43
44   /*
45     Cut & Paste from Change_iterator (ugh).
46
47     TODO: abstract this function
48   */
49
50   /* find the type  of translator that we're changing.
51
52   If \translator Staff = bass, then look for Staff = *
53   */
54   while (current && !current->is_alias (to_type_sym))
55     {
56       last = current;
57       current = current->get_parent_context ();
58     }
59
60   if (current && current->id_string () == to_id)
61     {
62       string msg;
63       msg += _f ("can't change, already in translator: %s", to_id);
64     }
65
66   if (current)
67     {
68       if (last)
69         {
70           Context *dest
71             = it->get_outlet ()->find_create_context (to_type_sym, to_id, SCM_EOL);
72           
73           send_stream_event (last, "ChangeParent", get_music ()->origin (),
74                                ly_symbol2scm ("context"), dest->self_scm ());
75         }
76       else
77         {
78           /*
79             We could change the current translator's id, but that would make
80             errors hard to catch
81
82           */
83           ;
84         }
85     }
86 }
87
88 void
89 Auto_change_iterator::process (Moment m)
90 {
91   Music_wrapper_iterator::process (m);
92
93   Moment now = get_outlet ()->now_mom ();
94   Moment *splitm = 0;
95   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
96     start_moment_ = now;
97
98   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
99     {
100       splitm = unsmob_moment (scm_caar (split_list_));
101       if ((*splitm + start_moment_) > now)
102         break;
103
104       SCM tag = scm_cdar (split_list_);
105       Direction d = to_dir (tag);
106
107       if (d && d != where_dir_)
108         {
109           where_dir_ = d;
110           string to_id = (d >= 0) ? "up" : "down";
111           change_to (child_iter_,
112                      ly_symbol2scm ("Staff"),
113                      to_id);
114         }
115     }
116 }
117
118 Auto_change_iterator::Auto_change_iterator ()
119 {
120   where_dir_ = CENTER;
121   split_list_ = SCM_EOL;
122 }
123
124 void
125 Auto_change_iterator::construct_children ()
126 {
127   split_list_ = get_music ()->get_property ("split-list");
128   start_moment_ = get_outlet ()->now_mom ();
129
130   SCM props = get_outlet ()->get_property ("trebleStaffProperties");
131   Context *up = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
132                                                     "up", props);
133
134   props = get_outlet ()->get_property ("bassStaffProperties");
135   Context *down = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
136                                                       "down", props);
137
138   up_.set_context (up);
139   down_.set_context (down);
140
141   Context *voice = up->find_create_context (ly_symbol2scm ("Voice"),
142                                             "", SCM_EOL);
143   set_context (voice);
144   Music_wrapper_iterator::construct_children ();
145 }
146
147 void
148 Auto_change_iterator::do_quit ()
149 {
150   up_.set_context (0);
151   down_.set_context (0);
152 }
153
154 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);