]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.3.27
[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 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_->dim_cache_[Y_AXIS]->off_callbacks_.push (callback);
26 }
27
28
29 bool
30 Staff_symbol_referencer_interface::has_interface_b ()
31 {
32   return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
33     || gh_number_p (elt_l_->get_elt_property ("staff-position"));
34 }
35
36
37 int
38 Staff_symbol_referencer_interface::line_count () const
39 {
40   Staff_symbol *st = staff_symbol_l ();
41   return st  ?  st->line_count () : 0;
42 }
43
44 Staff_symbol*
45 Staff_symbol_referencer_interface::staff_symbol_l () const
46 {
47   SCM st = elt_l_->get_elt_property ("staff-symbol");
48   return dynamic_cast<Staff_symbol* > (unsmob_element(st));
49 }
50
51 Real
52 Staff_symbol_referencer_interface::staff_space () const
53 {
54   Staff_symbol * st = staff_symbol_l ();
55   if (st)
56     return st->staff_space ();
57   else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
58     return elt_l_->paper_l ()->get_var ("interline");
59  
60   return 0.0;
61 }
62
63
64 Real
65 Staff_symbol_referencer_interface::position_f () const
66 {
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)
71     {
72       Real y = elt_l_->relative_coordinate (c, Y_AXIS)
73         - st->relative_coordinate (c, Y_AXIS);
74
75       p += 2.0 * y / st->staff_space ();
76     }
77   else
78     {
79       SCM pos = elt_l_->get_elt_property ("staff-position");
80       if (gh_number_p (pos))
81         return gh_scm2double (pos);
82     }
83   
84   return  p;
85 }
86
87
88
89 /*
90   should use offset callback!
91  */
92 Real
93 Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
94 {
95   Score_element * sc = dynamic_cast<Score_element*> (c->element_l ());
96
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 (sc).staff_space ();
103       off = gh_scm2double (pos) * space/2.0;
104     }
105   sc->set_elt_property ("staff-position", gh_double2scm (0.0));
106
107   return off;
108 }
109
110
111 void
112 Staff_symbol_referencer_interface::set_position (Real p)
113 {
114   Staff_symbol * st = staff_symbol_l ();
115   if (st && elt_l_->common_refpoint(st, Y_AXIS))
116     {
117       Real oldpos = position_f ();
118       elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
119     }
120   else
121     {
122       elt_l_->set_elt_property ("staff-position",
123                                 gh_double2scm (p));
124
125     }
126
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 ;
131
132   callbacks.push (callback);
133 }
134
135 Staff_symbol_referencer_interface
136 staff_symbol_referencer (Score_element const*e)
137 {
138   return e;                     // gee, I'm so smart!
139 }
140
141 int
142 compare_position (Score_element *const  &a, Score_element * const &b)
143 {
144   Staff_symbol_referencer_interface s1(a);
145   Staff_symbol_referencer_interface s2(b);      
146
147   return sign(s1.position_f () - s2.position_f ());
148 }