]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.3.55
[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_pointer ("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_pointer ("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   Score_element* me = (Score_element*)sc; // UGH.
95   
96   SCM pos = sc->get_elt_property ("staff-position");
97   Real off =0.0;
98   if (gh_number_p (pos))
99     {
100       Real space = staff_symbol_referencer (sc).staff_space ();
101       off = gh_scm2double (pos) * space/2.0;
102     }
103
104   me->set_elt_property ("staff-position", gh_double2scm (0.0));
105
106   return off;
107 }
108
109 /*
110   
111   This sets the position relative to the center of the staff symbol.
112
113   The function is hairy, because it can be callled in two situations:
114
115   1.  There is no staff yet; we must set staff-position
116
117   2.  There is a staff, and perhaps someone even applied a
118   translate_axis (). Then we must compensate for the translation
119   
120   In either case, we set a callback to be sure that our new position
121   will be extracted from staff-position
122   
123  */
124 void
125 Staff_symbol_referencer_interface::set_position (Real p)
126 {
127   Staff_symbol * st = staff_symbol_l ();
128   if (st && elt_l_->common_refpoint(st, Y_AXIS))
129     {
130       Real oldpos = position_f ();
131       elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
132     }
133   else
134     {
135       elt_l_->set_elt_property ("staff-position",
136                                 gh_double2scm (p));
137
138     }
139
140   if (elt_l_->has_offset_callback_b (callback, Y_AXIS))
141     return ; 
142
143   elt_l_->add_offset_callback (callback, Y_AXIS);
144 }
145
146 Staff_symbol_referencer_interface
147 staff_symbol_referencer (Score_element const*e)
148 {
149   return e;                     // gee, I'm so smart!
150 }
151
152 int
153 compare_position (Score_element *const  &a, Score_element * const &b)
154 {
155   Staff_symbol_referencer_interface s1(a);
156   Staff_symbol_referencer_interface s2(b);      
157
158   return sign(s1.position_f () - s2.position_f ());
159 }