]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
* lily/auto-change-iterator.cc: move contents from
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "music.hh"
11 #include "translator-group.hh"
12 #include "event.hh"
13 #include "music-wrapper-iterator.hh"
14 #include "direction.hh"
15
16 class Auto_change_iterator : public Music_wrapper_iterator
17 {
18 public:
19   VIRTUAL_COPY_CONS (Music_iterator);
20   DECLARE_SCHEME_CALLBACK(constructor, ());
21   
22   Auto_change_iterator ();
23
24 protected:
25   virtual void construct_children ();
26   virtual void process (Moment);  
27   Array<Pitch> pending_pitch (Moment)const;
28 private:
29   SCM split_list_;
30   Direction where_dir_;
31   void change_to (Music_iterator* , SCM, String);
32 };
33
34
35
36 void
37 Auto_change_iterator::change_to (Music_iterator *it, SCM to_type_sym,
38                                  String to_id)
39 {
40   Translator_group * current = it->report_to ();
41   Translator_group * 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->daddy_trans_;
57     }
58
59   if (current && current->id_string_ == to_id)
60     {
61       String msg;
62       msg += _ ("Can't switch translators, I'm there already");
63     }
64   
65   if (current) 
66     if (last)
67       {
68         Translator_group * dest = 
69           it->report_to ()->find_create_translator (to_type_sym, to_id, SCM_EOL);
70         current->remove_translator (last);
71         dest->add_used_group_translator (last);
72       }
73     else
74       {
75         /*
76           We could change the current translator's id, but that would make 
77           errors hard to catch
78           
79         */
80       }
81   else
82     ;
83
84 }
85
86 void
87 Auto_change_iterator::process (Moment m)
88 {
89   Music_wrapper_iterator::process (m);
90
91   
92   Moment now = report_to ()->now_mom ();
93   Moment *splitm = 0;
94   
95   for (; gh_pair_p (split_list_); split_list_ = gh_cdr (split_list_))
96     {
97       splitm = unsmob_moment (gh_caar (split_list_));
98       if (*splitm > now)
99         break ;
100
101       SCM tag = gh_cdar (split_list_);
102       Direction d = to_dir  (tag);
103       
104       if (d && d != where_dir_)
105         {
106           where_dir_ = d;
107           String to_id = (d >= 0) ?  "up" : "down";
108           change_to (child_iter_,
109                      ly_symbol2scm ("Staff"),
110                      to_id);
111         }
112     }
113 }
114
115 Auto_change_iterator::Auto_change_iterator ()
116 {
117   where_dir_ = CENTER;
118   split_list_ = SCM_EOL;
119 }
120
121 void
122 Auto_change_iterator::construct_children ()
123 {
124   Music_wrapper_iterator::construct_children ();
125   split_list_ =  get_music ()->get_mus_property ("split-list");
126 }
127
128 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);