]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.3.11
[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::lines_i () const
39 {
40   Staff_symbol *st = staff_symbol_l ();
41   return st  ?  st->no_lines_i_ : 5;
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_line_leading_f () const
53 {
54   Staff_symbol * st = staff_symbol_l ();
55   if (st)
56     return st->staff_line_leading_f_;
57   else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
58     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   SCM pos = elt_l_->get_elt_property ("staff-position");
69   if (gh_number_p (pos))
70     p = gh_scm2double (pos);
71
72   Staff_symbol * st = staff_symbol_l ();
73   if (st)
74     {
75       Score_element * c = elt_l_->common_refpoint (st, Y_AXIS);
76       Real y = elt_l_->relative_coordinate (c, Y_AXIS)
77         - st->relative_coordinate (c, Y_AXIS);
78
79       p += 2.0 * y / st->staff_line_leading_f ();
80     }
81   return  p;
82 }
83
84
85
86 /*
87   should use offset callback!
88  */
89 Real
90 Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
91 {
92   Score_element * sc = dynamic_cast<Score_element*> (c->element_l ());
93
94   
95   SCM pos = sc->get_elt_property ("staff-position");
96   Real off =0.0;
97   if (gh_number_p (pos))
98     {
99       Real space = staff_symbol_referencer_interface (sc).staff_line_leading_f ();
100       off = gh_scm2double (pos) * space/2.0;
101     }
102   sc->set_elt_property ("staff-position", gh_double2scm (0.0));
103
104   return off;
105 }
106
107
108 void
109 Staff_symbol_referencer_interface::set_position (Real p)
110 {
111   Staff_symbol * st = staff_symbol_l ();
112   if (st && elt_l_->common_refpoint(st, Y_AXIS))
113     {
114       Real oldpos = position_f ();
115       elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
116     }
117   else
118     {
119       elt_l_->set_elt_property ("staff-position",
120                                 gh_double2scm (p));
121
122     }
123
124   Array<Offset_cache_callback> &callbacks (elt_l_->dim_cache_[Y_AXIS]->off_callbacks_);
125   for (int i=0; i < callbacks.size ();i++)
126     if (callbacks[i] == callback)
127       return ;
128
129   callbacks.push (callback);
130 }
131
132 Staff_symbol_referencer_interface
133 staff_symbol_referencer_interface (Score_element const*e)
134 {
135   return e;                     // gee, I'm so smart!
136 }