]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter-engraver.cc
b8770feebbd4f7b5ac9d90ce3351daa4503cd3d1
[lilypond.git] / lily / system-start-delimiter-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2012 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 "engraver.hh"
21 #include "output-def.hh"
22 #include "paper-column.hh"
23 #include "pointer-group-interface.hh"
24 #include "side-position-interface.hh"
25 #include "spanner.hh"
26 #include "staff-symbol.hh"
27 #include "system-start-delimiter.hh"
28
29 struct Bracket_nesting_node
30 {
31 public:
32   virtual ~Bracket_nesting_node () {}
33   virtual bool add_staff (Grob *) { return false; }
34   virtual void add_support (Grob *) {}
35   virtual void set_bound (Direction, Grob *) {}
36   virtual void set_nesting_support (Grob *) {}
37   virtual void create_grobs (Engraver *, SCM) {}
38 };
39
40 struct Bracket_nesting_group : public Bracket_nesting_node
41 {
42   Spanner *delimiter_;
43   vector<Bracket_nesting_node *> children_;
44   SCM symbol_;
45
46   void from_list (SCM);
47   virtual void add_support (Grob *grob);
48   virtual bool add_staff (Grob *grob);
49   virtual void set_nesting_support (Grob *);
50   virtual void set_bound (Direction, Grob *grob);
51   virtual void create_grobs (Engraver *, SCM);
52   ~Bracket_nesting_group ();
53   Bracket_nesting_group ();
54 };
55
56 struct Bracket_nesting_staff : public Bracket_nesting_node
57 {
58   Grob *staff_;
59
60   Bracket_nesting_staff (Grob *s) { staff_ = s; }
61   virtual bool add_staff (Grob *);
62 };
63
64 Bracket_nesting_group::Bracket_nesting_group ()
65 {
66   symbol_ = SCM_EOL;
67   delimiter_ = 0;
68 }
69
70 bool
71 Bracket_nesting_staff::add_staff (Grob *g)
72 {
73   if (!staff_)
74     {
75       staff_ = g;
76       return true;
77     }
78   return false;
79 }
80
81 void
82 Bracket_nesting_group::create_grobs (Engraver *engraver, SCM default_type)
83 {
84   SCM type = scm_is_symbol (symbol_) ? symbol_ : default_type;
85   delimiter_ = engraver->make_spanner (ly_symbol2string (type).c_str (),
86                                        SCM_EOL);
87
88   for (vsize i = 0; i < children_.size (); i++)
89     children_[i]->create_grobs (engraver, default_type);
90 }
91
92 void
93 Bracket_nesting_group::add_support (Grob *g)
94 {
95   Side_position_interface::add_support (g, delimiter_);
96   for (vsize i = 0; i < children_.size (); i++)
97     children_[i]->add_support (g);
98 }
99
100 Bracket_nesting_group::~Bracket_nesting_group ()
101 {
102   junk_pointers (children_);
103 }
104
105 void
106 Bracket_nesting_group::set_bound (Direction d, Grob *g)
107 {
108   delimiter_->set_bound (d, g);
109   for (vsize i = 0; i < children_.size (); i++)
110     children_[i]->set_bound (d, g);
111 }
112
113 void
114 Bracket_nesting_group::set_nesting_support (Grob *parent)
115 {
116   if (parent)
117     Side_position_interface::add_support (delimiter_, parent);
118
119   for (vsize i = 0; i < children_.size (); i++)
120     children_[i]->set_nesting_support (delimiter_);
121 }
122
123 void
124 Bracket_nesting_group::from_list (SCM x)
125 {
126   for (SCM s = x; scm_is_pair (s); s = scm_cdr (s))
127     {
128       SCM entry = scm_car (s);
129       if (scm_is_pair (entry))
130         {
131           Bracket_nesting_group *node = new Bracket_nesting_group;
132           node->from_list (entry);
133           children_.push_back (node);
134         }
135       else if (entry == ly_symbol2scm ("SystemStartBrace")
136                || entry == ly_symbol2scm ("SystemStartBracket")
137                || entry == ly_symbol2scm ("SystemStartBar")
138                || entry == ly_symbol2scm ("SystemStartSquare"))
139         symbol_ = entry;
140       else
141         children_.push_back (new Bracket_nesting_staff (0));
142     }
143 }
144
145 bool
146 Bracket_nesting_group::add_staff (Grob *grob)
147 {
148   for (vsize i = 0; i < children_.size (); i++)
149     {
150       if (children_[i]->add_staff (grob))
151         {
152           Pointer_group_interface::add_grob (delimiter_,
153                                              ly_symbol2scm ("elements"), grob);
154           return true;
155         }
156     }
157   return false;
158 }
159
160 /****************/
161
162 class System_start_delimiter_engraver : public Engraver
163 {
164 public:
165   TRANSLATOR_DECLARATIONS (System_start_delimiter_engraver);
166
167 protected:
168   Bracket_nesting_group *nesting_;
169
170   DECLARE_ACKNOWLEDGER (system_start_delimiter);
171   DECLARE_ACKNOWLEDGER (staff_symbol);
172
173   void process_music ();
174   virtual void finalize ();
175 };
176
177 System_start_delimiter_engraver::System_start_delimiter_engraver ()
178 {
179   nesting_ = 0;
180 }
181
182 void
183 System_start_delimiter_engraver::process_music ()
184 {
185   if (!nesting_)
186     {
187       nesting_ = new Bracket_nesting_group ();
188       SCM hierarchy = get_property ("systemStartDelimiterHierarchy");
189       SCM delimiter_name = get_property ("systemStartDelimiter");
190
191       nesting_->from_list (hierarchy);
192       nesting_->create_grobs (this, delimiter_name);
193       nesting_->set_bound (LEFT,
194                            unsmob_grob (get_property ("currentCommandColumn")));
195     }
196 }
197
198 void
199 System_start_delimiter_engraver::finalize ()
200 {
201   if (nesting_)
202     {
203       nesting_->set_bound (RIGHT,
204                            unsmob_grob (get_property ("currentCommandColumn")));
205       nesting_->set_nesting_support (0);
206
207       delete nesting_;
208     }
209 }
210
211 void
212 System_start_delimiter_engraver::acknowledge_staff_symbol (Grob_info inf)
213 {
214   Grob *staff = inf.grob ();
215   bool succ = nesting_->add_staff (staff);
216
217   if (!succ)
218     {
219       nesting_->children_.push_back (new Bracket_nesting_staff (0));
220       nesting_->add_staff (staff);
221     }
222 }
223
224 void
225 System_start_delimiter_engraver::acknowledge_system_start_delimiter (Grob_info inf)
226 {
227   nesting_->add_support (inf.grob ());
228 }
229
230 #include "translator.icc"
231
232 ADD_ACKNOWLEDGER (System_start_delimiter_engraver, staff_symbol);
233 ADD_ACKNOWLEDGER (System_start_delimiter_engraver, system_start_delimiter);
234
235 ADD_TRANSLATOR (System_start_delimiter_engraver,
236                 /* doc */
237                 "Create a system start delimiter (i.e., a"
238                 " @code{SystemStartBar}, @code{SystemStartBrace},"
239                 " @code{SystemStartBracket} or @code{SystemStartSquare}"
240                 " spanner).",
241
242                 /* create */
243                 "SystemStartSquare "
244                 "SystemStartBrace "
245                 "SystemStartBracket "
246                 "SystemStartBar ",
247
248                 /* read */
249                 "systemStartDelimiter "
250                 "systemStartDelimiterHierarchy "
251                 "currentCommandColumn ",
252
253                 /* write */
254                 ""
255                );