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