lilypond-1.3.12
[lilypond.git] / lily / staff-symbol-referencer.cc
blob64a46b81170a231df59025f876e80ec34235f27d
1 /*
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>
7 */
9 #include <math.h>
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;
21 void
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);
29 bool
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"));
37 int
38 Staff_symbol_referencer_interface::lines_i () const
40 Staff_symbol *st = staff_symbol_l ();
41 return st ? st->no_lines_i_ : 5;
44 Staff_symbol*
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));
51 Real
52 Staff_symbol_referencer_interface::staff_line_leading_f () const
54 Staff_symbol * st = staff_symbol_l ();
55 if (st)
56 return st->staff_line_leading_f_;
57 else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
58 elt_l_->paper_l ()->get_var ("interline");
60 return 0.0;
64 Real
65 Staff_symbol_referencer_interface::position_f () const
67 Real p =0.0;
68 Staff_symbol * st = staff_symbol_l ();
69 Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
70 if (st && c)
72 Real y = elt_l_->relative_coordinate (c, Y_AXIS)
73 - st->relative_coordinate (c, Y_AXIS);
75 p += 2.0 * y / st->staff_line_leading_f ();
77 else
79 SCM pos = elt_l_->get_elt_property ("staff-position");
80 if (gh_number_p (pos))
81 return gh_scm2double (pos);
84 return p;
90 should use offset callback!
92 Real
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");
99 Real off =0.0;
100 if (gh_number_p (pos))
102 Real space = staff_symbol_referencer_interface (sc).staff_line_leading_f ();
103 off = gh_scm2double (pos) * space/2.0;
105 sc->set_elt_property ("staff-position", gh_double2scm (0.0));
107 return off;
111 void
112 Staff_symbol_referencer_interface::set_position (Real p)
114 Staff_symbol * st = staff_symbol_l ();
115 if (st && elt_l_->common_refpoint(st, Y_AXIS))
117 Real oldpos = position_f ();
118 elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
120 else
122 elt_l_->set_elt_property ("staff-position",
123 gh_double2scm (p));
127 Array<Offset_cache_callback> &callbacks (elt_l_->dim_cache_[Y_AXIS]->off_callbacks_);
128 for (int i=0; i < callbacks.size ();i++)
129 if (callbacks[i] == callback)
130 return ;
132 callbacks.push (callback);
135 Staff_symbol_referencer_interface
136 staff_symbol_referencer_interface (Score_element const*e)
138 return e; // gee, I'm so smart!
142 compare_position (Score_element *const &a, Score_element * const &b)
144 Staff_symbol_referencer_interface s1(a);
145 Staff_symbol_referencer_interface s2(b);
147 return sign(s1.position_f () - s2.position_f ());