]> git.donarmstrong.com Git - lilypond.git/blob - lily/change-iterator.cc
* lily/include/context.hh (class Context): make members protected.
[lilypond.git] / lily / change-iterator.cc
1 /*
2   change-iterator.cc -- implement Change_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "change-iterator.hh"
10 #include "context.hh"
11 #include "music.hh"
12 #include "warn.hh"
13 #include "input.hh"
14
15
16 void
17 Change_iterator::error (String reason)
18 {
19   String to_type = ly_symbol2string (get_music ()->get_property ("change-to-type"));
20   String to_id = ly_scm2string (get_music ()->get_property ("change-to-id"));
21
22   String warn1 = _f ("can't change `%s' to `%s'", to_type, to_id) 
23     + ": " + reason;
24
25   /*
26     GUHG!
27    */
28   String warn2= "Change_iterator::process (): " 
29     + get_outlet ()->context_name () + " = `"
30     + get_outlet ()->id_string () + "': ";
31   warning (warn2);
32   get_music ()->origin ()->warning (warn1);
33 }
34
35 /*
36   move to construct_children ?
37  */
38 void
39 Change_iterator::process (Moment m)
40 {
41   Context * current = get_outlet ();
42   Context * last = 0;
43
44   SCM to_type = get_music ()->get_property ("change-to-type");
45   String to_id =  ly_scm2string (get_music ()->get_property ("change-to-id"));
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))
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 += _ ("Can't switch translators, I'm there already");
62     }
63   
64   if (current) 
65     if (last)
66       {
67         Context * dest = 0;
68         Context * where = get_outlet ();
69         while (!dest && where)
70           {
71             dest = find_context_below (where, to_type, to_id);
72             where = where->get_parent_context ();
73           }
74         
75         if (dest)
76           {
77             current->remove_context (last);
78             dest->add_context (last);
79           }
80         else
81           {
82             get_music ()->origin ()->warning  ("could not find context to switch to.");
83           }
84       }
85     else
86       {
87         /*
88           We could change the current translator's id, but that would make 
89           errors hard to catch
90           
91            last->translator_id_string ()  = get_change ()->change_to_id_string ();
92         */
93         error (_ ("I'm one myself"));
94       }
95   else
96     error (_ ("none of these in my family"));
97
98   Simple_music_iterator::process (m);
99 }
100
101
102
103 IMPLEMENT_CTOR_CALLBACK (Change_iterator);