]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-side.cc
release: 1.3.9
[lilypond.git] / lily / staff-side.cc
1 /*   
2   staff-side.cc --  implement Staff_side_element
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "staff-side.hh"
11 #include "staff-symbol.hh"
12 #include "debug.hh"
13 #include "warn.hh"
14 #include "dimensions.hh"
15 #include "dimension-cache.hh"
16
17 Side_position_interface::Side_position_interface (Score_element *e)
18 {
19   elt_l_ = e;
20 }
21
22
23 void
24 Side_position_interface::add_support (Score_element*e)
25 {
26   SCM sup = elt_l_->get_elt_property ("side-support");
27   elt_l_->set_elt_property ("side-support",
28                             gh_cons (e->self_scm_,sup));
29 }
30
31
32
33 Direction
34 Side_position_interface::get_direction () const
35 {
36   SCM d = elt_l_->get_elt_property ("direction");
37   if (isdir_b (d))
38     return to_dir (d) ? to_dir (d) : DOWN;
39
40   Direction relative_dir = UP;
41   SCM reldir = elt_l_->get_elt_property ("side-relative-direction");    // should use a lambda.
42   if (isdir_b (d))
43     {
44       relative_dir = to_dir (reldir);
45     }
46   
47   SCM other_elt = elt_l_->get_elt_property ("direction-source");
48   if (SMOB_IS_TYPE_B (Score_element, other_elt))
49     {
50       Score_element * e = SMOB_TO_TYPE(Score_element,other_elt);
51
52       return relative_dir * Side_position_interface (e).get_direction ();
53     }
54   
55   return DOWN;
56 }
57   
58 /**
59    Callback that does the aligning.
60  */
61 Real
62 Side_position_interface::side_position (Dimension_cache const * c)
63 {
64   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
65
66   Interval dim;
67   Axis  axis = c->axis ();
68   Graphical_element *common = me->parent_l (axis);
69   SCM support = me->get_elt_property ("side-support");
70   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
71     {
72       if (!SMOB_IS_TYPE_B (Score_element, gh_car (s)))
73         continue;
74       
75       Score_element * e  = SMOB_TO_TYPE(Score_element, gh_car (s));
76       common = common->common_refpoint (e, axis);
77     }
78   
79   for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
80     {
81       if (!SMOB_IS_TYPE_B (Score_element, gh_car (s)))
82         continue;
83
84       Score_element * e  = SMOB_TO_TYPE(Score_element, gh_car (s));
85       Real coord = e->relative_coordinate (common, axis);
86
87       dim.unite (coord + e->extent (axis));
88     }
89
90
91   if (dim.empty_b ())
92     {
93       dim = Interval(0,0);
94     }
95
96   Real off =  me->parent_l (axis)->relative_coordinate (common, axis);
97
98
99   Direction dir = Side_position_interface (me).get_direction ();
100     
101   SCM pad = me->remove_elt_property ("padding");
102   if (pad != SCM_UNDEFINED)
103     {
104       off += gh_scm2double (pad) * dir;
105     }
106   Real total_off = dim[dir] + off;
107
108   if (fabs (total_off) > 100 CM)
109     programming_error ("Huh ? Improbable staff side dim.");
110
111   return total_off;
112 }
113
114 Real
115 Side_position_interface::self_alignment (Dimension_cache const *c)
116 {
117   String s ("self-alignment-");
118   Axis ax = c->axis ();
119   s +=  (ax == X_AXIS) ? "X" : "Y";
120   Score_element *elm = dynamic_cast<Score_element*> (c->element_l ());
121   SCM align (elm->get_elt_property (s));
122   if (isdir_b (align))
123     {
124       Direction d = to_dir (align);
125       Interval ext(elm->extent (ax));
126       if (d)
127         {
128           return - ext[d];
129         }
130       return - ext.center ();
131     }
132   else
133     return 0.0;
134 }
135
136
137 Real
138 Side_position_interface::aligned_side (Dimension_cache const *c)
139 {
140   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
141   Side_position_interface s(me);
142   Direction d = s.get_direction ();
143   Axis ax = c->axis ();
144   Real o = side_position (c);
145
146   Interval iv =  me->extent (ax);
147
148   if (!iv.empty_b ())
149     {
150       o += - iv[-d];
151
152       SCM pad = me->get_elt_property ("padding");
153       if (gh_number_p (pad))
154         o += d *gh_scm2double (pad) ; 
155     }
156   return o;
157 }
158
159 #if 0
160
161 /*
162   need cascading off callbacks for this.
163  */
164 Side_position_interface::quantised_side (Dimension_cache const *c)
165 {
166   Score_element * me = dynamic_cast<Score_element*> (c->element_l ());
167   Side_position_interface s(me);
168   Direction d = s.get_direction ();
169   Axis ax = c->axis ();
170   Real o = side_position (c);
171   Staff_symbol_referencer * st = dynamic_cast (me);
172
173   if (st && ax == Y_AXIS)
174     {
175       st->translate_axis (o, Y_AXIS);
176       st->set_position ();
177
178       st->lines_i ();
179       st->quantise_to_next_line (d);
180       st->translate_axis (o, -Y_AXIS);
181     }
182
183   return o;
184 }
185 #endif
186   
187
188
189 void
190 Side_position_interface::set_axis (Axis a)
191 {
192   // prop transparent ? 
193   if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
194     elt_l_->set_elt_property ("side-support" ,SCM_EOL);
195
196   Axis other = Axis ((a +1)%2);
197   elt_l_->dim_cache_[a]->off_callbacks_.push (aligned_side);
198 }
199
200
201 Axis
202 Side_position_interface::get_axis () const
203 {
204   Dimension_cache * c =  elt_l_->dim_cache_[X_AXIS];
205   for (int i=0 ; i < c->off_callbacks_.size();i ++)
206     if (c->off_callbacks_[i] == side_position
207         ||c->off_callbacks_[i] == aligned_side)
208       return X_AXIS;
209
210   
211   return Y_AXIS;
212 }
213
214 void
215 Side_position_interface::set_direction (Direction d) 
216 {
217   elt_l_->set_elt_property ("direction", gh_int2scm (d));
218 }
219
220 bool
221 Side_position_interface::is_staff_side_b () const
222 {
223   return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
224 }
225
226 bool
227 Side_position_interface::supported_b () const
228 {
229   SCM s =elt_l_->get_elt_property  ("side-support"); 
230   return s != SCM_UNDEFINED && s != SCM_EOL;
231 }