]> git.donarmstrong.com Git - lilypond.git/blob - lily/vertical-align-engraver.cc
* lily/include/grob-info.hh (class Grob_info): make data member
[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
17 class Vertical_align_engraver : public Engraver
18 {
19   Spanner *valign_;
20   bool qualifies (Grob_info) const;
21   SCM id_to_group_hashtab_;  
22   
23 public:
24   TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
25
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 ();
32 };
33
34
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 */ "");
43
44
45 Vertical_align_engraver::Vertical_align_engraver ()
46 {
47   valign_ = 0;
48   id_to_group_hashtab_ = SCM_EOL;
49 }
50
51 void
52 Vertical_align_engraver::derived_mark () const
53 {
54   scm_gc_mark (id_to_group_hashtab_); 
55 }
56
57 void
58 Vertical_align_engraver::initialize ()
59 {
60   id_to_group_hashtab_ = scm_c_make_hash_table (11);
61 }
62
63
64 void
65 Vertical_align_engraver::process_music ()
66 {
67   if (!valign_)
68     {
69       valign_ = make_spanner ("VerticalAlignment", SCM_EOL);
70       valign_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
71     }
72 }
73
74 void
75 Vertical_align_engraver::finalize ()
76 {
77   if (valign_)
78     {
79       valign_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
80       valign_ = 0;
81     }
82 }
83
84 bool
85 Vertical_align_engraver::qualifies (Grob_info i) const
86 {
87   int sz = i.origin_contexts ((Translator *)this).size ();
88
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);
91 }
92
93 void
94 Vertical_align_engraver::acknowledge_grob (Grob_info i)
95 {
96   if (qualifies (i))
97     {
98       String id = i.context ()->id_string ();
99
100       scm_hash_set_x (id_to_group_hashtab_, scm_makfrom0str (id.to_str0 ()),
101                       i.grob ()->self_scm ());
102
103
104       SCM before_id = i.context ()->get_property ("alignAboveContext");
105       SCM after_id = i.context ()->get_property ("alignBelowContext");
106       
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);
109
110
111       Align_interface::add_element (valign_, i.grob (),
112                                     get_property ("verticalAlignmentChildCallback"));
113
114       if (unsmob_grob (before) || unsmob_grob (after))
115         {
116           SCM elts = valign_->get_property ("elements");
117           SCM new_order = scm_cdr (elts);
118           SCM *current = &new_order;
119
120           for (SCM s = new_order;  scm_is_pair (s); s = scm_cdr (s))
121             {
122               if (scm_car (s) == after)
123                 {
124                   *current = scm_cons (i.grob ()->self_scm(), s);
125                   break;
126                 }
127               else if (scm_car (s) == before)
128                 {
129                   scm_set_cdr_x (s, scm_cons (i.grob ()->self_scm (),
130                                               scm_cdr (s)));
131                   break;
132                 }
133
134               current = SCM_CDRLOC (s);
135             }
136
137           valign_->set_property ("elements", new_order);
138         }
139     }
140 }