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