1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1999--2021 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 
22 #include "axis-group-interface.hh"
23 #include "hara-kiri-group-spanner.hh"
24 #include "pointer-group-interface.hh"
25 #include "context.hh"
26 #include "international.hh"
27 #include "spanner.hh"
28 #include "warn.hh"
29 
30 #include "translator.icc"
31 
32 using std::vector;
33 
34 /**
35    Put stuff in a Spanner with an Axis_group_interface.
36    Use as last element of a context.
37 */
38 class Axis_group_engraver : public Engraver
39 {
40 protected:
41   bool active_;
42   Spanner *staffline_;
43   SCM interesting_;
44   vector<Grob *> elts_;
45   void process_music ();
46   void initialize () override;
47   void finalize () override;
48   void acknowledge_grob (Grob_info) override;
49   void process_acknowledged ();
50   virtual Spanner *get_spanner ();
51   virtual void add_element (Grob *);
52   bool must_be_last () const override;
53   void derived_mark () const override;
54 
55 public:
56   TRANSLATOR_DECLARATIONS (Axis_group_engraver);
57 };
58 
Axis_group_engraver(Context * c)59 Axis_group_engraver::Axis_group_engraver (Context *c)
60   : Engraver (c)
61 {
62   staffline_ = 0;
63   interesting_ = SCM_EOL;
64   active_ = false;
65 }
66 
67 void
initialize()68 Axis_group_engraver::initialize ()
69 {
70   active_ = !from_scm<bool> (get_property (this, "hasAxisGroup"));
71   if (active_)
72     set_property (context (), "hasAxisGroup", SCM_BOOL_T);
73 }
74 
75 void
derived_mark() const76 Axis_group_engraver::derived_mark () const
77 {
78   scm_gc_mark (interesting_);
79 }
80 
81 bool
must_be_last() const82 Axis_group_engraver::must_be_last () const
83 {
84   return true;
85 }
86 
87 void
process_music()88 Axis_group_engraver::process_music ()
89 {
90   if (!staffline_ && active_)
91     {
92       staffline_ = get_spanner ();
93       Grob *it = unsmob<Grob> (get_property (this, "currentCommandColumn"));
94       staffline_->set_bound (LEFT, it);
95     }
96   interesting_ = get_property (this, "keepAliveInterfaces");
97 }
98 
99 Spanner *
get_spanner()100 Axis_group_engraver::get_spanner ()
101 {
102   return make_spanner ("VerticalAxisGroup", SCM_EOL);
103 }
104 
105 void
finalize()106 Axis_group_engraver::finalize ()
107 {
108   if (staffline_)
109     {
110       Grob *it = unsmob<Grob> (get_property (this, "currentCommandColumn"));
111       staffline_->set_bound (RIGHT, it);
112 
113       Pointer_group_interface::set_ordered (staffline_, ly_symbol2scm ("elements"), false);
114     }
115 }
116 
117 void
acknowledge_grob(Grob_info i)118 Axis_group_engraver::acknowledge_grob (Grob_info i)
119 {
120   if (!staffline_)
121     return;
122 
123   elts_.push_back (i.grob ());
124 
125   if (from_scm<bool> (get_property (staffline_, "remove-empty")))
126     {
127       for (SCM s = interesting_; scm_is_pair (s); s = scm_cdr (s))
128         {
129           if (i.grob ()->internal_has_interface (scm_car (s)))
130             {
131               Hara_kiri_group_spanner::add_interesting_item (staffline_, i.grob ());
132               break;
133             }
134         }
135     }
136 }
137 
138 /*
139   maybe should check if our parent is set, because we now get a
140   cyclic parent relationship if we have two Axis_group_engravers in
141   the context.  */
142 void
process_acknowledged()143 Axis_group_engraver::process_acknowledged ()
144 {
145   if (!staffline_)
146     return;
147 
148   for (vsize i = 0; i < elts_.size (); i++)
149     {
150       if (!unsmob<Grob> (get_object (elts_[i], "axis-group-parent-Y")))
151         {
152           if (staffline_->get_y_parent ()
153               && staffline_->get_y_parent () == elts_[i])
154             {
155               staffline_->warning (_ ("Axis_group_engraver: vertical group already has a parent"));
156               staffline_->warning (_ ("are there two Axis_group_engravers?"));
157               staffline_->warning (_ ("removing this vertical group"));
158               staffline_->suicide ();
159               staffline_ = 0;
160               break;
161             }
162           add_element (elts_[i]);
163         }
164     }
165   elts_.clear ();
166 }
167 
168 void
add_element(Grob * e)169 Axis_group_engraver::add_element (Grob *e)
170 {
171   Axis_group_interface::add_element (staffline_, e);
172 }
173 
174 void
boot()175 Axis_group_engraver::boot ()
176 {
177   ADD_ACKNOWLEDGER (Axis_group_engraver, grob);
178 }
179 
180 ADD_TRANSLATOR (Axis_group_engraver,
181                 /* doc */
182                 "Group all objects created in this context in a"
183                 " @code{VerticalAxisGroup} spanner.",
184 
185                 /* create */
186                 "VerticalAxisGroup ",
187 
188                 /* read */
189                 "currentCommandColumn "
190                 "keepAliveInterfaces "
191                 "hasAxisGroup ",
192 
193                 /* write */
194                 "hasAxisGroup "
195                );
196