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