]> git.donarmstrong.com Git - lilypond.git/blob - lily/vertical-align-engraver.cc
(measure_position): measure_position() is now a
[lilypond.git] / lily / vertical-align-engraver.cc
1 /*
2   vertical-align-engraver.cc -- implement Vertical_align_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
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"
16 #include "pointer-group-interface.hh"
17 #include "grob-array.hh"
18
19 #include "translator.icc"
20
21 class Vertical_align_engraver : public Engraver
22 {
23   Spanner *valign_;
24   bool qualifies (Grob_info) const;
25   SCM id_to_group_hashtab_;  
26   
27 public:
28   TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
29
30 protected:
31   virtual void derived_mark () const;
32   virtual void acknowledge_grob (Grob_info);
33   PRECOMPUTED_VIRTUAL void process_music ();
34   virtual void finalize ();
35   virtual void initialize ();
36 };
37
38
39 ADD_TRANSLATOR (Vertical_align_engraver,
40                 "Catch groups (staffs, lyrics lines, etc.) and stack "
41                 "them vertically.",
42                 /* creats*/ "VerticalAlignment",
43                 /* accepts */ "",
44                 /* acks  */ "axis-group-interface",
45                 /* reads */ "",
46                 /* write */ "");
47
48
49 Vertical_align_engraver::Vertical_align_engraver ()
50 {
51   valign_ = 0;
52   id_to_group_hashtab_ = SCM_EOL;
53 }
54
55 void
56 Vertical_align_engraver::derived_mark () const
57 {
58   scm_gc_mark (id_to_group_hashtab_); 
59 }
60
61 void
62 Vertical_align_engraver::initialize ()
63 {
64   id_to_group_hashtab_ = scm_c_make_hash_table (11);
65 }
66
67
68 void
69 Vertical_align_engraver::process_music ()
70 {
71   if (!valign_)
72     {
73       valign_ = make_spanner ("VerticalAlignment", SCM_EOL);
74       valign_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
75     }
76 }
77
78 void
79 Vertical_align_engraver::finalize ()
80 {
81   if (valign_)
82     {
83       valign_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
84       valign_ = 0;
85     }
86 }
87
88 bool
89 Vertical_align_engraver::qualifies (Grob_info i) const
90 {
91   int sz = i.origin_contexts ((Translator *)this).size ();
92
93   return sz > 0 && Axis_group_interface::has_interface (i.grob ())
94     && !i.grob ()->get_parent (Y_AXIS) && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
95 }
96
97 void
98 Vertical_align_engraver::acknowledge_grob (Grob_info i)
99 {
100   if (qualifies (i))
101     {
102       String id = i.context ()->id_string ();
103
104       scm_hash_set_x (id_to_group_hashtab_, scm_makfrom0str (id.to_str0 ()),
105                       i.grob ()->self_scm ());
106
107
108       SCM before_id = i.context ()->get_property ("alignAboveContext");
109       SCM after_id = i.context ()->get_property ("alignBelowContext");
110       
111       SCM before = scm_hash_ref (id_to_group_hashtab_,  before_id, SCM_BOOL_F);
112       SCM after = scm_hash_ref (id_to_group_hashtab_,  after_id, SCM_BOOL_F);
113
114       Grob * before_grob = unsmob_grob (before);
115       Grob * after_grob = unsmob_grob (after);
116       
117       Align_interface::add_element (valign_, i.grob (),
118                                     get_property ("verticalAlignmentChildCallback"));
119
120       if (before_grob || after_grob)
121         {
122           Grob_array * ga = unsmob_grob_array (valign_->get_object ("elements"));
123           Link_array<Grob> &arr = ga->array_reference ();
124           
125           Grob *added = arr.pop();
126           for (int i = 0 ; i < arr.size (); i++)
127             {
128               if (arr[i] == before_grob)
129                 {
130                   arr.insert (added, i);
131                   break ; 
132                 }
133               else if (arr[i] == after_grob)
134                 {
135                   arr.insert (added, i + 1);
136                   break ;
137                 }
138             }
139         }
140     }
141 }