lilypond-1.3.65
[lilypond.git] / lily / staff-symbol-referencer.cc
blobba6d15a7d8b75a838164a87960442bcf0c85be49
1 /*
2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 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 if (!gh_number_p (elt_l_->get_elt_property ("staff-position")))
25 elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
27 elt_l_->add_offset_callback (callback, Y_AXIS);
30 bool
31 Staff_symbol_referencer_interface::has_interface_b ()
33 return unsmob_element (elt_l_->get_elt_pointer ("staff-symbol"))
34 || gh_number_p (elt_l_->get_elt_property ("staff-position"));
38 int
39 Staff_symbol_referencer_interface::line_count () const
41 Staff_symbol *st = staff_symbol_l ();
42 return st ? st->line_count () : 0;
45 Staff_symbol*
46 Staff_symbol_referencer_interface::staff_symbol_l () const
48 SCM st = elt_l_->get_elt_pointer ("staff-symbol");
49 return dynamic_cast<Staff_symbol* > (unsmob_element(st));
52 Real
53 Staff_symbol_referencer_interface::staff_space () const
55 Staff_symbol * st = staff_symbol_l ();
56 if (st)
57 return st->staff_space ();
58 else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
59 return elt_l_->paper_l ()->get_var ("interline");
61 return 0.0;
65 Real
66 Staff_symbol_referencer_interface::position_f () const
68 Real p =0.0;
69 Staff_symbol * st = staff_symbol_l ();
70 Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
71 if (st && c)
73 Real y = elt_l_->relative_coordinate (c, Y_AXIS)
74 - st->relative_coordinate (c, Y_AXIS);
76 p += 2.0 * y / st->staff_space ();
78 else
80 SCM pos = elt_l_->get_elt_property ("staff-position");
81 if (gh_number_p (pos))
82 return gh_scm2double (pos);
85 return p;
91 should use offset callback!
93 Real
94 Staff_symbol_referencer_interface::callback (Score_element const* sc,Axis )
96 Score_element* me = (Score_element*)sc; // UGH.
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 (sc).staff_space ();
103 off = gh_scm2double (pos) * space/2.0;
106 me->set_elt_property ("staff-position", gh_double2scm (0.0));
108 return off;
113 This sets the position relative to the center of the staff symbol.
115 The function is hairy, because it can be callled in two situations:
117 1. There is no staff yet; we must set staff-position
119 2. There is a staff, and perhaps someone even applied a
120 translate_axis (). Then we must compensate for the translation
122 In either case, we set a callback to be sure that our new position
123 will be extracted from staff-position
126 void
127 Staff_symbol_referencer_interface::set_position (Real p)
129 Staff_symbol * st = staff_symbol_l ();
130 if (st && elt_l_->common_refpoint(st, Y_AXIS))
132 Real oldpos = position_f ();
133 elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
135 else
137 elt_l_->set_elt_property ("staff-position",
138 gh_double2scm (p));
142 if (elt_l_->has_offset_callback_b (callback, Y_AXIS))
143 return ;
145 elt_l_->add_offset_callback (callback, Y_AXIS);
148 Staff_symbol_referencer_interface
149 staff_symbol_referencer (Score_element const*e)
151 return e; // gee, I'm so smart!
155 compare_position (Score_element *const &a, Score_element * const &b)
157 Staff_symbol_referencer_interface s1(a);
158 Staff_symbol_referencer_interface s2(b);
160 return sign(s1.position_f () - s2.position_f ());