]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-change-iterator.cc
4a34540de96dfb6cc67735ed0a484f406ce13945
[lilypond.git] / lily / auto-change-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "change-iterator.hh"
21 #include "context.hh"
22 #include "direction.hh"
23 #include "international.hh"
24 #include "music.hh"
25 #include "music-wrapper-iterator.hh"
26
27 class Auto_change_iterator : public Music_wrapper_iterator
28 {
29 public:
30   DECLARE_SCHEME_CALLBACK (constructor, ());
31
32   Auto_change_iterator ();
33
34 protected:
35   virtual void do_quit ();
36   virtual void construct_children ();
37   virtual void process (Moment);
38   vector<Pitch> pending_pitch (Moment) const;
39 private:
40   SCM split_list_;
41   Direction where_dir_;
42   Moment start_moment_;
43
44   Context_handle up_;
45   Context_handle down_;
46 };
47
48 void
49 Auto_change_iterator::process (Moment m)
50 {
51   Music_wrapper_iterator::process (m);
52
53   Moment now = get_outlet ()->now_mom ();
54   Moment *splitm = 0;
55   if (start_moment_.main_part_.is_infinity () && start_moment_ < 0)
56     start_moment_ = now;
57
58   for (; scm_is_pair (split_list_); split_list_ = scm_cdr (split_list_))
59     {
60       splitm = unsmob<Moment> (scm_caar (split_list_));
61       if ((*splitm + start_moment_) > now)
62         break;
63
64       SCM tag = scm_cdar (split_list_);
65       Direction d = to_dir (tag);
66
67       if (d && d != where_dir_)
68         {
69           where_dir_ = d;
70           string to_id = (d >= 0) ? "up" : "down";
71           // N.B. change_to() returns an error message. Silence is the legacy
72           // behavior here, but maybe that should be changed.
73           Change_iterator::change_to (*child_iter_,
74                                       ly_symbol2scm ("Staff"),
75                                       to_id);
76         }
77     }
78 }
79
80 Auto_change_iterator::Auto_change_iterator ()
81 {
82   where_dir_ = CENTER;
83   split_list_ = SCM_EOL;
84 }
85
86 void
87 Auto_change_iterator::construct_children ()
88 {
89   split_list_ = get_music ()->get_property ("split-list");
90   start_moment_ = get_outlet ()->now_mom ();
91
92   SCM props = get_outlet ()->get_property ("trebleStaffProperties");
93   Context *up = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
94                                                     "up", props);
95
96   props = get_outlet ()->get_property ("bassStaffProperties");
97   Context *down = get_outlet ()->find_create_context (ly_symbol2scm ("Staff"),
98                                                       "down", props);
99
100   up_.set_context (up);
101   down_.set_context (down);
102
103   Context *voice = up->find_create_context (ly_symbol2scm ("Voice"),
104                                             "", SCM_EOL);
105   set_context (voice);
106   Music_wrapper_iterator::construct_children ();
107 }
108
109 void
110 Auto_change_iterator::do_quit ()
111 {
112   up_.set_context (0);
113   down_.set_context (0);
114 }
115
116 IMPLEMENT_CTOR_CALLBACK (Auto_change_iterator);