]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
* lily/include/grob-info.hh: origin_contexts() now does not
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "music.hh"
11 #include "context.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   DECLARE_SCHEME_CALLBACK(constructor, ());
20   
21   Auto_change_iterator ();
22
23 protected:
24   virtual void construct_children ();
25   virtual void process (Moment);  
26   Array<Pitch> pending_pitch (Moment)const;
27 private:
28   SCM split_list_;
29   Direction where_dir_;
30   void change_to (Music_iterator* , SCM, String);
31 };
32
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->daddy_context_;
56     }
57
58   if (current && current->id_string_ == to_id)
59     {
60       String msg;
61       msg += _ ("Can't switch translators, I'm there already");
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 }
84
85 void
86 Auto_change_iterator::process (Moment m)
87 {
88   Music_wrapper_iterator::process (m);
89   
90   Moment now = get_outlet ()->now_mom ();
91   Moment *splitm = 0;
92   
93   for (; gh_pair_p (split_list_); split_list_ = gh_cdr (split_list_))
94     {
95       splitm = unsmob_moment (gh_caar (split_list_));
96       if (*splitm > now)
97         break ;
98
99       SCM tag = gh_cdar (split_list_);
100       Direction d = to_dir  (tag);
101       
102       if (d && d != where_dir_)
103         {
104           where_dir_ = d;
105           String to_id = (d >= 0) ?  "up" : "down";
106           change_to (child_iter_,
107                      ly_symbol2scm ("Staff"),
108                      to_id);
109         }
110     }
111 }
112
113 Auto_change_iterator::Auto_change_iterator ()
114 {
115   where_dir_ = CENTER;
116   split_list_ = SCM_EOL;
117 }
118
119 void
120 Auto_change_iterator::construct_children ()
121 {
122   Music_wrapper_iterator::construct_children ();
123   split_list_ =  get_music ()->get_mus_property ("split-list");
124 }
125
126 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);