]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
ca3c938ed85733ebf423d246035af23056143841
[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   if (!gh_number_p (elt_l_->get_elt_property ("staff-position")))
25       elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
26       
27   elt_l_->add_offset_callback (callback, Y_AXIS);
28 }
29
30 bool
31 Staff_symbol_referencer_interface::has_interface_b ()
32 {
33   return unsmob_element (elt_l_->get_elt_pointer ("staff-symbol"))
34     || gh_number_p (elt_l_->get_elt_property ("staff-position"));
35 }
36
37
38 int
39 Staff_symbol_referencer_interface::line_count () const
40 {
41   Staff_symbol *st = staff_symbol_l ();
42   return st  ?  st->line_count () : 0;
43 }
44
45 Staff_symbol*
46 Staff_symbol_referencer_interface::staff_symbol_l () const
47 {
48   SCM st = elt_l_->get_elt_pointer ("staff-symbol");
49   return dynamic_cast<Staff_symbol* > (unsmob_element(st));
50 }
51
52 Real
53 Staff_symbol_referencer_interface::staff_space () const
54 {
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");
60  
61   return 0.0;
62 }
63
64
65 Real
66 Staff_symbol_referencer_interface::position_f () const
67 {
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)
72     {
73       Real y = elt_l_->relative_coordinate (c, Y_AXIS)
74         - st->relative_coordinate (c, Y_AXIS);
75
76       p += 2.0 * y / st->staff_space ();
77     }
78   else
79     {
80       SCM pos = elt_l_->get_elt_property ("staff-position");
81       if (gh_number_p (pos))
82         return gh_scm2double (pos);
83     }
84   
85   return  p;
86 }
87
88
89
90 /*
91   should use offset callback!
92  */
93 Real
94 Staff_symbol_referencer_interface::callback (Score_element const* sc,Axis )
95 {
96   Score_element* me = (Score_element*)sc; // UGH.
97   
98   SCM pos = sc->get_elt_property ("staff-position");
99   Real off =0.0;
100   if (gh_number_p (pos))
101     {
102       Real space = Staff_symbol_referencer_interface (sc).staff_space ();
103       off = gh_scm2double (pos) * space/2.0;
104     }
105
106   me->set_elt_property ("staff-position", gh_double2scm (0.0));
107
108   return off;
109 }
110
111 /*
112   
113   This sets the position relative to the center of the staff symbol.
114
115   The function is hairy, because it can be callled in two situations:
116
117   1.  There is no staff yet; we must set staff-position
118
119   2.  There is a staff, and perhaps someone even applied a
120   translate_axis (). Then we must compensate for the translation
121   
122   In either case, we set a callback to be sure that our new position
123   will be extracted from staff-position
124   
125  */
126 void
127 Staff_symbol_referencer_interface::set_position (Real p)
128 {
129   Staff_symbol * st = staff_symbol_l ();
130   if (st && elt_l_->common_refpoint(st, Y_AXIS))
131     {
132       Real oldpos = position_f ();
133       elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
134     }
135   else
136     {
137       elt_l_->set_elt_property ("staff-position",
138                                 gh_double2scm (p));
139
140     }
141
142   if (elt_l_->has_offset_callback_b (callback, Y_AXIS))
143     return ; 
144
145   elt_l_->add_offset_callback (callback, Y_AXIS);
146 }
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 }