]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
* lily/parser.yy (Music_list): add error-found to music with errors.
[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 do_quit ();
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   Moment start_moment_;
33
34   Interpretation_context_handle up_;
35   Interpretation_context_handle down_;
36
37 };
38
39
40
41 void
42 Auto_change_iterator::change_to (Music_iterator *it, SCM to_type_sym,
43                                  String to_id)
44 {
45   Context * current = it->get_outlet ();
46   Context * last = 0;
47
48   /*
49     Cut & Paste from Change_iterator (ugh).
50
51     TODO: abstract this function 
52    */
53   
54   /* find the type  of translator that we're changing.
55      
56      If \translator Staff = bass, then look for Staff = *
57    */
58   while (current && !current->is_alias (to_type_sym))
59     {
60       last = current;
61       current = current->get_parent_context ();
62     }
63
64   if (current && current->id_string () == to_id)
65     {
66       String msg;
67       msg += _ ("Can't switch translators, I'm there already");
68     }
69   
70   if (current) 
71     if (last)
72       {
73         Context * dest = 
74           it->get_outlet ()->find_create_context (to_type_sym, to_id, SCM_EOL);
75         current->remove_context (last);
76         dest->add_context (last);
77       }
78     else
79       {
80         /*
81           We could change the current translator's id, but that would make 
82           errors hard to catch
83           
84         */
85       }
86   else
87     ;
88
89 }
90
91 void
92 Auto_change_iterator::process (Moment m)
93 {
94   Music_wrapper_iterator::process (m);
95   
96   Moment now = get_outlet ()->now_mom ();
97   Moment *splitm = 0;
98   
99   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
100     {
101       splitm = unsmob_moment (scm_caar (split_list_));
102       if ((*splitm + start_moment_) > now)
103         break ;
104
105       SCM tag = scm_cdar (split_list_);
106       Direction d = to_dir  (tag);
107       
108       if (d && d != where_dir_)
109         {
110           where_dir_ = d;
111           String to_id = (d >= 0) ?  "up" : "down";
112           change_to (child_iter_,
113                      ly_symbol2scm ("Staff"),
114                      to_id);
115         }
116     }
117 }
118
119 Auto_change_iterator::Auto_change_iterator ()
120 {
121   where_dir_ = CENTER;
122   split_list_ = SCM_EOL;
123 }
124
125 void
126 Auto_change_iterator::construct_children ()
127 {
128   split_list_ =  get_music ()->get_property ("split-list");
129   start_moment_ = get_outlet ()->now_mom ();
130
131   SCM props = get_outlet()->get_property ("trebleStaffProperties");
132   Context *up = get_outlet()->find_create_context (ly_symbol2scm ("Staff"),
133                                                    "up", props);
134
135   props = get_outlet()->get_property ("bassStaffProperties");
136   Context *down = get_outlet()->find_create_context (ly_symbol2scm ("Staff"),
137                                                    "down", props);
138   
139   up_.set_translator (up);
140   down_.set_translator (down);
141
142   Context *voice = up->find_create_context (ly_symbol2scm ("Voice"),
143                                            "", SCM_EOL);
144   set_translator (voice);
145   Music_wrapper_iterator::construct_children ();
146
147 }
148
149 void
150 Auto_change_iterator::do_quit()
151 {
152   up_.set_translator (0);
153   down_.set_translator (0);
154   
155 }
156 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);