2002->2003
[lilypond.git] / lily / auto-change-iterator.cc
blobd57960f9bba1ac799af0f34573f7bdd661864025
1 /*
2 auto-change-iterator.cc -- implement Auto_change_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "music.hh"
11 #include "auto-change-iterator.hh"
12 #include "translator-group.hh"
13 #include "event.hh"
17 void
18 Auto_change_iterator::change_to (Music_iterator *it, String to_type,
19 String to_id)
21 Translator_group * current = it->report_to ();
22 Translator_group * last = 0;
25 Cut & Paste from Change_iterator (ugh).
27 TODO: abstract this function
30 /* find the type of translator that we're changing.
32 If \translator Staff = bass, then look for Staff = *
34 while (current && current->type_string_ != to_type)
36 last = current;
37 current = current->daddy_trans_;
40 if (current && current->id_string_ == to_id)
42 String msg;
43 msg += _ ("Can't switch translators, I'm there already");
46 if (current)
47 if (last)
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);
54 else
57 We could change the current translator's id, but that would make
58 errors hard to catch
60 last->translator_id_string_ = get_change ()->change_to_id_string_;
62 // error (_ ("I'm one myself"));
64 else
65 ; // error (_ ("none of these in my family"));
70 Look ahead to find first pitches to determine staff position.
71 WARNING: this means that
73 \autochange Staff \notes { .... \context Staff = otherstaff { .. } .. }
75 will confuse the autochanger, since it will not notice that the
76 music for OTHERSTAFF is not his.
78 PRECONDITION: this->ok () holds.
80 Array<Pitch>
81 Auto_change_iterator::pending_pitch (Moment m) const
83 Music_iterator * iter = child_iter_ ->clone ();
84 Array<Pitch> ps;
85 while (1)
87 SCM muses = iter->get_pending_events (m);
88 for (SCM s = muses; gh_pair_p (s); s=ly_cdr (s))
90 Music * m = unsmob_music (ly_car (s));
91 if (m && m->is_mus_type ("note-event"))
93 ps.push (*unsmob_pitch (m->get_mus_property ("pitch")));
97 if (ps.size ())
98 break;
100 iter->skip (m);
101 if (!iter->ok ())
102 break;
104 m = iter->pending_moment ();
107 scm_gc_unprotect_object (iter->self_scm());
109 return ps;
112 void
113 Auto_change_iterator::process (Moment m)
116 first we get the pitches, then we do the real work.
117 Music_wrapper_iterator::process () might process (and throw away)
118 pitches we need. */
119 Array<Pitch> ps = pending_pitch (m);
121 Music_wrapper_iterator::process (m);
122 if (ps.size ())
124 Pitch p = ps[0];
125 Direction s = Direction (sign (p.steps ()));
127 Don't change for central C.
129 TODO: make this tunable somehow. Sometimes, you'd want to
130 switch for C.C. as well.
133 if (s && s != where_dir_)
135 where_dir_ = s;
136 String to_id = (s >= 0) ? "up" : "down";
137 String wh = ly_scm2string (get_music ()->get_mus_property ("what"));
138 change_to (child_iter_, wh, to_id);
143 Auto_change_iterator::Auto_change_iterator ()
145 where_dir_ = CENTER;
148 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);