]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "music.hh"
11 #include "auto-change-iterator.hh"
12 #include "translator-group.hh"
13 #include "musical-request.hh"
14
15
16
17 void
18 Auto_change_iterator::change_to (Music_iterator *it, String to_type,
19                                  String to_id)
20 {
21   Translator_group * current = it->report_to ();
22   Translator_group * last = 0;
23
24   /*
25     Cut & Paste from Change_iterator (ugh).
26
27     TODO: abstract this function 
28    */
29   
30   /* find the type  of translator that we're changing.
31      
32      If \translator Staff = bass, then look for Staff = *
33    */
34   while (current && current->type_string_ != to_type)
35     {
36       last = current;
37       current = current->daddy_trans_;
38     }
39
40   if (current && current->id_string_ == to_id)
41     {
42       String msg;
43       msg += _ ("Can't switch translators, I'm there already");
44     }
45   
46   if (current) 
47     if (last)
48       {
49         Translator_group * dest = 
50           it->report_to ()->find_create_translator (to_type, to_id);
51         current->remove_translator (last);
52         dest->add_used_group_translator (last);
53       }
54     else
55       {
56         /*
57           We could change the current translator's id, but that would make 
58           errors hard to catch
59           
60            last->translator_id_string_  = get_change ()->change_to_id_string_;
61         */
62         //      error (_ ("I'm one myself"));
63       }
64   else
65     ; //    error (_ ("none of these in my family"));
66
67 }
68
69 /*
70   Look ahead to find first pitches to determine staff position.
71   WARNING: this means that
72
73   \autochange Staff \notes { .... \context Staff = otherstaff { .. } .. }
74
75   will confuse the autochanger, since it will not notice that the
76   music for OTHERSTAFF is not his.
77
78   PRECONDITION: this->ok () holds.
79 */
80 Array<Pitch>
81 Auto_change_iterator::pending_pitch (Moment m) const
82 {
83   Music_iterator * iter = child_iter_ ->clone ();
84   Array<Pitch> ps;
85   while (1)
86     {
87       SCM muses = iter->get_pending_events (m);
88       for (SCM s = muses; gh_pair_p (s); s=ly_cdr (s))
89         if (Note_req* nr = dynamic_cast<Note_req*> (unsmob_music (ly_car (s))))
90           {
91             ps.push (*unsmob_pitch (nr->get_mus_property ("pitch")));
92           }
93
94       if (ps.size ())
95         break;
96
97       iter->skip (m);
98       if (!iter->ok ())
99         break;
100       
101       m = iter->pending_moment ();
102     }
103
104   scm_gc_unprotect_object (iter->self_scm());
105
106   return ps;
107 }
108
109 void
110 Auto_change_iterator::process (Moment m)
111 {
112   /*
113     first we get the pitches, then we do the real work.
114     Music_wrapper_iterator::process () might process (and throw away)
115     pitches we need.  */
116   Array<Pitch> ps = pending_pitch (m);
117
118   Music_wrapper_iterator::process (m);
119   if (ps.size ())
120     {
121       Pitch p = ps[0];
122       Direction s = Direction (sign (p.steps ()));
123       /*
124         Don't change for central C.
125
126         TODO: make this tunable somehow. Sometimes, you'd want to
127         switch for C.C. as well.
128
129       */
130       if (s && s != where_dir_)
131         {
132           where_dir_ = s;
133           String to_id = (s >= 0) ?  "up" : "down";
134           String wh = ly_scm2string (get_music ()->get_mus_property ("what"));
135           change_to (child_iter_, wh, to_id);     
136         }
137     }
138 }
139
140 Auto_change_iterator::Auto_change_iterator ()
141 {
142   where_dir_ = CENTER;
143 }
144
145 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);