]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.3.54
[lilypond.git] / lily / staff-symbol-referencer.cc
1 /*   
2   staff-symbol-referencer.cc -- implement Staff_symbol_referencer
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7  */
8
9 #include <math.h>
10
11 #include "staff-symbol-referencer.hh"
12 #include "staff-symbol.hh"
13 #include "paper-def.hh"
14 #include "dimension-cache.hh"
15
16 Staff_symbol_referencer_interface::Staff_symbol_referencer_interface (Score_element const *sc)
17 {
18   elt_l_ = (Score_element*)sc;
19 }
20
21 void
22 Staff_symbol_referencer_interface::set_interface ()
23 {
24   elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
25   elt_l_->add_offset_callback (callback, Y_AXIS);
26 }
27
28 bool
29 Staff_symbol_referencer_interface::has_interface_b ()
30 {
31   return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
32     || gh_number_p (elt_l_->get_elt_property ("staff-position"));
33 }
34
35
36 int
37 Staff_symbol_referencer_interface::line_count () const
38 {
39   Staff_symbol *st = staff_symbol_l ();
40   return st  ?  st->line_count () : 0;
41 }
42
43 Staff_symbol*
44 Staff_symbol_referencer_interface::staff_symbol_l () const
45 {
46   SCM st = elt_l_->get_elt_property ("staff-symbol");
47   return dynamic_cast<Staff_symbol* > (unsmob_element(st));
48 }
49
50 Real
51 Staff_symbol_referencer_interface::staff_space () const
52 {
53   Staff_symbol * st = staff_symbol_l ();
54   if (st)
55     return st->staff_space ();
56   else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
57     return elt_l_->paper_l ()->get_var ("interline");
58  
59   return 0.0;
60 }
61
62
63 Real
64 Staff_symbol_referencer_interface::position_f () const
65 {
66   Real p =0.0;
67   Staff_symbol * st = staff_symbol_l ();
68   Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
69   if (st && c)
70     {
71       Real y = elt_l_->relative_coordinate (c, Y_AXIS)
72         - st->relative_coordinate (c, Y_AXIS);
73
74       p += 2.0 * y / st->staff_space ();
75     }
76   else
77     {
78       SCM pos = elt_l_->get_elt_property ("staff-position");
79       if (gh_number_p (pos))
80         return gh_scm2double (pos);
81     }
82   
83   return  p;
84 }
85
86
87
88 /*
89   should use offset callback!
90  */
91 Real
92 Staff_symbol_referencer_interface::callback (Score_element const* sc,Axis )
93 {
94   SCM pos = sc->get_elt_property ("staff-position");
95   Real off =0.0;
96   if (gh_number_p (pos))
97     {
98       Real space = staff_symbol_referencer (sc).staff_space ();
99       off = gh_scm2double (pos) * space/2.0;
100     }
101   sc->set_elt_property ("staff-position", gh_double2scm (0.0));
102
103   return off;
104 }
105
106 /*
107   
108   This sets the position relative to the center of the staff symbol.
109
110   The function is hairy, because it can be callled in two situations:
111
112   1.  There is no staff yet; we must set staff-position
113
114   2.  There is a staff, and perhaps someone even applied a
115   translate_axis (). Then we must compensate for the translation
116   
117   In either case, we set a callback to be sure that our new position
118   will be extracted from staff-position
119   
120  */
121 void
122 Staff_symbol_referencer_interface::set_position (Real p)
123 {
124   Staff_symbol * st = staff_symbol_l ();
125   if (st && elt_l_->common_refpoint(st, Y_AXIS))
126     {
127       Real oldpos = position_f ();
128       elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
129     }
130   else
131     {
132       elt_l_->set_elt_property ("staff-position",
133                                 gh_double2scm (p));
134
135     }
136
137   if (elt_l_->has_offset_callback_b (callback, Y_AXIS))
138     return ; 
139
140   elt_l_->add_offset_callback (callback, Y_AXIS);
141 }
142
143 Staff_symbol_referencer_interface
144 staff_symbol_referencer (Score_element const*e)
145 {
146   return e;                     // gee, I'm so smart!
147 }
148
149 int
150 compare_position (Score_element *const  &a, Score_element * const &b)
151 {
152   Staff_symbol_referencer_interface s1(a);
153   Staff_symbol_referencer_interface s2(b);      
154
155   return sign(s1.position_f () - s2.position_f ());
156 }