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