]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
release: 1.3.94
[lilypond.git] / lily / dot-column.cc
1 /*
2   dot-column.cc -- implement Dot_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "dots.hh"
10 #include "dot-column.hh"
11 #include "rhythmic-head.hh"
12 #include "group-interface.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "directional-element-interface.hh"
15 #include "side-position-interface.hh"
16 #include "axis-group-interface.hh"
17
18
19
20
21
22 void
23 Dot_column::set_interface (Score_element* me)
24 {
25
26   Directional_element_interface::set (me, RIGHT);
27   
28   Axis_group_interface::set_interface (me);
29   Axis_group_interface::set_axes (me, X_AXIS,X_AXIS);
30 }
31
32 /*
33   Will fuck up in this case.
34
35   X.  .
36   X.X .
37    |X .
38    |
39    |
40    |X .
41    |
42    |
43
44
45    Should be smarter.
46  */
47
48
49 MAKE_SCHEME_CALLBACK(Dot_column,force_shift_callback,2);
50 SCM
51 Dot_column::force_shift_callback (SCM element_smob, SCM axis)
52 {
53   Score_element *me = unsmob_element (element_smob);
54   Axis a = (Axis) gh_scm2int (axis);
55   assert (a == Y_AXIS);
56  me = me->parent_l (X_AXIS);
57   SCM dots = me->get_elt_property ("dots");
58   do_shifts (dots);
59   return gh_double2scm (0.0);
60 }
61
62 SCM
63 Dot_column::do_shifts (SCM l)
64 {
65   Link_array<Score_element> dots;
66   while (gh_pair_p (l))
67     {
68       dots.push (unsmob_element (gh_car (l)));
69       l = gh_cdr (l);
70     }
71   
72   dots.sort (compare_position);
73   
74   if (dots.size () < 2)
75     return SCM_UNSPECIFIED;
76   Slice s;
77   s.set_empty ();
78
79   Array<int> taken_posns;
80   int conflicts = 0;
81   for (int i=0; i < dots.size (); i++)
82     {
83       Real p = Staff_symbol_referencer::position_f (dots[i]);
84       for (int j=0; j < taken_posns.size (); j++)
85         {
86           if (taken_posns[j] == (int) p)
87             conflicts++;
88         }
89       taken_posns.push ((int)p);
90       s.unite (Slice ((int)p,
91                       (int)p));      
92     }
93
94   if (!conflicts)
95     return SCM_UNSPECIFIED;
96   
97   int  middle = s.center ();
98   /*
99     +1 -> off by one 
100    */
101   int pos = middle - dots.size () + 1;
102   if (!(pos % 2))
103     pos ++;                     // center () rounds down.
104
105   for (int i=0; i < dots.size (); pos += 2, i++)
106     {
107       Score_element * d = dots[i];
108       Staff_symbol_referencer::set_position (d,pos);
109     }
110
111   return SCM_UNSPECIFIED;
112 }
113
114 bool
115 Dot_column::has_interface (Score_element*m)
116 {
117   return m && m->has_interface (ly_symbol2scm ("dot-column-interface"));
118 }
119
120
121 void
122 Dot_column::add_head (Score_element * me, Score_element *rh)
123 {
124   Score_element * d = unsmob_element (rh->get_elt_property ("dot"));
125   if (d)
126     {
127       Side_position::add_support (me,rh);
128
129       Pointer_group_interface gi (me, "dots");
130       gi.add_element (d);
131       
132       d->add_offset_callback (Dot_column::force_shift_callback_proc , Y_AXIS);
133       Axis_group_interface::add_element (me, d);
134     }
135 }
136