2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "staff-symbol-referencer.hh"
12 #include "staff-symbol.hh"
13 #include "paper-def.hh"
14 #include "dimension-cache.hh"
16 Staff_symbol_referencer_interface::Staff_symbol_referencer_interface (Score_element const *sc)
18 elt_l_ = (Score_element*)sc;
22 Staff_symbol_referencer_interface::set_interface ()
24 elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
25 elt_l_->dim_cache_[Y_AXIS]->off_callbacks_.push (callback);
30 Staff_symbol_referencer_interface::has_interface_b ()
32 return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
33 || gh_number_p (elt_l_->get_elt_property ("staff-position"));
38 Staff_symbol_referencer_interface::line_count () const
40 Staff_symbol *st = staff_symbol_l ();
41 return st ? st->line_count () : 0;
45 Staff_symbol_referencer_interface::staff_symbol_l () const
47 SCM st = elt_l_->get_elt_property ("staff-symbol");
48 return dynamic_cast<Staff_symbol* > (unsmob_element(st));
52 Staff_symbol_referencer_interface::staff_space () const
54 Staff_symbol * st = staff_symbol_l ();
56 return st->staff_space ();
57 else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
58 return elt_l_->paper_l ()->get_var ("interline");
65 Staff_symbol_referencer_interface::position_f () const
68 Staff_symbol * st = staff_symbol_l ();
69 Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
72 Real y = elt_l_->relative_coordinate (c, Y_AXIS)
73 - st->relative_coordinate (c, Y_AXIS);
75 p += 2.0 * y / st->staff_space ();
79 SCM pos = elt_l_->get_elt_property ("staff-position");
80 if (gh_number_p (pos))
81 return gh_scm2double (pos);
90 should use offset callback!
93 Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
95 Score_element * sc = dynamic_cast<Score_element*> (c->element_l ());
98 SCM pos = sc->get_elt_property ("staff-position");
100 if (gh_number_p (pos))
102 Real space = staff_symbol_referencer (sc).staff_space ();
103 off = gh_scm2double (pos) * space/2.0;
105 sc->set_elt_property ("staff-position", gh_double2scm (0.0));
112 This sets the position relative to the center of the staff symbol.
114 The function is hairy, because it can be callled in two situations:
116 1. There is no staff yet; we must set staff-position
118 2. There is a staff, and perhaps someone even applied a
119 translate_axis (). Then we must compensate for the translation
121 In either case, we set a callback to be sure that our new position
122 will be extracted from staff-position
126 Staff_symbol_referencer_interface::set_position (Real p)
128 Staff_symbol * st = staff_symbol_l ();
129 if (st && elt_l_->common_refpoint(st, Y_AXIS))
131 Real oldpos = position_f ();
132 elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
136 elt_l_->set_elt_property ("staff-position",
141 Array<Offset_cache_callback> &callbacks (elt_l_->dim_cache_[Y_AXIS]->off_callbacks_);
142 for (int i=0; i < callbacks.size ();i++)
143 if (callbacks[i] == callback)
146 callbacks.push (callback);
149 Staff_symbol_referencer_interface
150 staff_symbol_referencer (Score_element const*e)
152 return e; // gee, I'm so smart!
156 compare_position (Score_element *const &a, Score_element * const &b)
158 Staff_symbol_referencer_interface s1(a);
159 Staff_symbol_referencer_interface s2(b);
161 return sign(s1.position_f () - s2.position_f ());