]> git.donarmstrong.com Git - lilypond.git/blob - lily/vertical-align-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / vertical-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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 "context.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "engraver.hh"
25 #include "international.hh"
26 #include "spanner.hh"
27 #include "pointer-group-interface.hh"
28 #include "grob-array.hh"
29
30 #include "translator.icc"
31
32 using std::string;
33 using std::vector;
34
35 class Vertical_align_engraver : public Engraver
36 {
37   Spanner *valign_;
38   bool qualifies (Grob_info) const;
39   SCM id_to_group_hashtab_;
40
41 public:
42   TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
43   DECLARE_ACKNOWLEDGER (axis_group);
44
45 protected:
46   virtual void derived_mark () const;
47   void process_music ();
48   virtual void finalize ();
49   virtual void initialize ();
50
51   bool top_level_;
52 };
53
54 ADD_ACKNOWLEDGER (Vertical_align_engraver, axis_group);
55 ADD_TRANSLATOR (Vertical_align_engraver,
56                 /* doc */
57                 "Catch groups (staves, lyrics lines, etc.) and stack them"
58                 " vertically.",
59
60                 /* create */
61                 "VerticalAlignment ",
62
63                 /* read */
64                 "alignAboveContext "
65                 "alignBelowContext "
66                 "hasAxisGroup ",
67
68                 /* write */
69                 ""
70                );
71
72 Vertical_align_engraver::Vertical_align_engraver ()
73 {
74   valign_ = 0;
75   id_to_group_hashtab_ = SCM_EOL;
76   top_level_ = false;
77 }
78
79 void
80 Vertical_align_engraver::derived_mark () const
81 {
82   scm_gc_mark (id_to_group_hashtab_);
83 }
84
85 void
86 Vertical_align_engraver::initialize ()
87 {
88   id_to_group_hashtab_ = scm_c_make_hash_table (11);
89 }
90
91 void
92 Vertical_align_engraver::process_music ()
93 {
94   if (!valign_ && !scm_is_null (id_to_group_hashtab_))
95     {
96       if (to_boolean (get_property ("hasAxisGroup")))
97         {
98           warning (_ ("Ignoring Vertical_align_engraver in VerticalAxisGroup"));
99           id_to_group_hashtab_ = SCM_EOL;
100           return;
101         }
102       
103       top_level_ = to_boolean (get_property ("topLevelAlignment"));
104
105       valign_ = make_spanner (top_level_ ? "VerticalAlignment" : "StaffGrouper", SCM_EOL);
106       valign_->set_bound (LEFT, unsmob<Grob> (get_property ("currentCommandColumn")));
107       Align_interface::set_ordered (valign_);
108     }
109 }
110
111 void
112 Vertical_align_engraver::finalize ()
113 {
114   if (valign_)
115     {
116       valign_->set_bound (RIGHT, unsmob<Grob> (get_property ("currentCommandColumn")));
117       valign_ = 0;
118     }
119 }
120
121 bool
122 Vertical_align_engraver::qualifies (Grob_info i) const
123 {
124   int sz = i.origin_contexts ((Translator *)this).size ();
125
126   return sz > 0 && has_interface<Axis_group_interface> (i.grob ())
127          && !i.grob ()->get_parent (Y_AXIS)
128          && !to_boolean (i.grob ()->get_property ("no-alignment"))
129          && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
130 }
131
132 void
133 Vertical_align_engraver::acknowledge_axis_group (Grob_info i)
134 {
135   if (scm_is_null (id_to_group_hashtab_))
136     return;
137
138   if (top_level_ && qualifies (i))
139     {
140       string id = i.context ()->id_string ();
141
142       scm_hash_set_x (id_to_group_hashtab_, ly_string2scm (id),
143                       i.grob ()->self_scm ());
144
145       SCM before_id = i.context ()->get_property ("alignAboveContext");
146       SCM after_id = i.context ()->get_property ("alignBelowContext");
147
148       SCM before = scm_hash_ref (id_to_group_hashtab_, before_id, SCM_BOOL_F);
149       SCM after = scm_hash_ref (id_to_group_hashtab_, after_id, SCM_BOOL_F);
150
151       Grob *before_grob = unsmob<Grob> (before);
152       Grob *after_grob = unsmob<Grob> (after);
153
154       Align_interface::add_element (valign_, i.grob ());
155
156       if (before_grob || after_grob)
157         {
158           Grob_array *ga = unsmob<Grob_array> (valign_->get_object ("elements"));
159           vector<Grob *> &arr = ga->array_reference ();
160
161           Grob *added = arr.back ();
162           arr.pop_back ();
163           for (vsize i = 0; i < arr.size (); i++)
164             {
165               if (arr[i] == before_grob)
166                 {
167                   arr.insert (arr.begin () + i, added);
168
169                   /* Only set staff affinity if it already has one.  That way we won't
170                      set staff-affinity on things that don't want it (like staves). */
171                   if (scm_is_number (added->get_property ("staff-affinity")))
172                     added->set_property ("staff-affinity", scm_from_int (DOWN));
173                   break;
174                 }
175               else if (arr[i] == after_grob)
176                 {
177                   arr.insert (arr.begin () + i + 1, added);
178                   if (scm_is_number (added->get_property ("staff-affinity")))
179                     added->set_property ("staff-affinity", scm_from_int (UP));
180                   break;
181                 }
182             }
183         }
184     }
185   else if (qualifies (i))
186     {
187       Pointer_group_interface::add_grob (valign_, ly_symbol2scm ("elements"), i.grob ());
188       if (!unsmob<Grob> (i.grob ()->get_object ("staff-grouper")))
189         i.grob ()->set_object ("staff-grouper", valign_->self_scm ());
190     }
191 }