*** empty log message ***
[lilypond.git] / lily / vertical-align-engraver.cc
blob7b0b9900f69ab5808d0aebc08c8a2558ad1f83a1
1 /*
2 vertical-align-engraver.cc -- implement Vertical_align_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "context.hh"
10 #include "paper-column.hh"
11 #include "align-interface.hh"
12 #include "span-bar.hh"
13 #include "axis-group-interface.hh"
14 #include "engraver.hh"
15 #include "spanner.hh"
17 class Vertical_align_engraver : public Engraver
19 Spanner *valign_;
20 bool qualifies (Grob_info) const;
21 SCM id_to_group_hashtab_;
23 public:
24 TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
26 protected:
27 virtual void derived_mark () const;
28 virtual void acknowledge_grob (Grob_info);
29 virtual void process_music ();
30 virtual void finalize ();
31 virtual void initialize ();
35 ADD_TRANSLATOR (Vertical_align_engraver,
36 "Catch groups (staffs, lyrics lines, etc.) and stack "
37 "them vertically.",
38 /* creats*/ "VerticalAlignment",
39 /* accepts */ "",
40 /* acks */ "axis-group-interface",
41 /* reads */ "",
42 /* write */ "");
45 Vertical_align_engraver::Vertical_align_engraver ()
47 valign_ = 0;
48 id_to_group_hashtab_ = SCM_EOL;
51 void
52 Vertical_align_engraver::derived_mark () const
54 scm_gc_mark (id_to_group_hashtab_);
57 void
58 Vertical_align_engraver::initialize ()
60 id_to_group_hashtab_ = scm_c_make_hash_table (11);
64 void
65 Vertical_align_engraver::process_music ()
67 if (!valign_)
69 valign_ = make_spanner ("VerticalAlignment", SCM_EOL);
70 valign_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
74 void
75 Vertical_align_engraver::finalize ()
77 if (valign_)
79 valign_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
80 valign_ = 0;
84 bool
85 Vertical_align_engraver::qualifies (Grob_info i) const
87 int sz = i.origin_contexts ((Translator *)this).size ();
89 return sz > 0 && Axis_group_interface::has_interface (i.grob ())
90 && !i.grob ()->get_parent (Y_AXIS) && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
93 void
94 Vertical_align_engraver::acknowledge_grob (Grob_info i)
96 if (qualifies (i))
98 String id = i.context ()->id_string ();
100 scm_hash_set_x (id_to_group_hashtab_, scm_makfrom0str (id.to_str0 ()),
101 i.grob ()->self_scm ());
104 SCM before_id = i.context ()->get_property ("alignAboveContext");
105 SCM after_id = i.context ()->get_property ("alignBelowContext");
107 SCM before = scm_hash_ref (id_to_group_hashtab_, before_id, SCM_BOOL_F);
108 SCM after = scm_hash_ref (id_to_group_hashtab_, after_id, SCM_BOOL_F);
111 Align_interface::add_element (valign_, i.grob (),
112 get_property ("verticalAlignmentChildCallback"));
114 if (unsmob_grob (before) || unsmob_grob (after))
116 SCM elts = valign_->get_property ("elements");
117 SCM new_order = scm_cdr (elts);
118 SCM *current = &new_order;
120 for (SCM s = new_order; scm_is_pair (s); s = scm_cdr (s))
122 if (scm_car (s) == after)
124 *current = scm_cons (i.grob ()->self_scm(), s);
125 break;
127 else if (scm_car (s) == before)
129 scm_set_cdr_x (s, scm_cons (i.grob ()->self_scm (),
130 scm_cdr (s)));
131 break;
134 current = SCM_CDRLOC (s);
137 valign_->set_property ("elements", new_order);