]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-interface.cc
release: 1.3.96
[lilypond.git] / lily / axis-group-interface.cc
1 /*   
2   axis-group-interface.cc --  implement Axis_group_interface
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "hara-kiri-group-spanner.hh"
10 #include "axis-group-interface.hh"
11 #include "score-element.hh"
12
13 void
14 Axis_group_interface::add_element (Score_element*me,Score_element *e)
15 {
16   for (SCM ax = me->get_elt_property ("axes"); ax != SCM_EOL ; ax = gh_cdr (ax))
17     {
18       Axis a = (Axis) gh_scm2int (gh_car (ax));
19       
20       if (!e->parent_l (a))
21         e->set_parent (me, a);
22     }
23
24   Pointer_group_interface::add_element (me, "elements", e);
25   me->add_dependency (e);
26 }
27
28 bool
29 Axis_group_interface::axis_b (Score_element*me,Axis a )
30 {
31   /*
32     urg. FIXME, check for Hara_kiri_group_spanner shouldn't be necessary?
33
34     
35    */
36   return me->has_extent_callback_b (group_extent_callback_proc, a) ||
37     (me->has_extent_callback_b (Hara_kiri_group_spanner::y_extent_proc, a));
38 }
39
40 Interval
41 Axis_group_interface::relative_group_extent (Axis a, Score_element *common, SCM elts)
42 {
43   Interval r;
44   for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
45     {
46       Score_element * se = unsmob_element (gh_car (s));
47       Interval dims = se->extent (common, a);
48       if (!dims.empty_b ())
49         r.unite (dims);
50     }
51   return r;
52 }
53
54 MAKE_SCHEME_CALLBACK(Axis_group_interface,group_extent_callback,2);
55 SCM
56 Axis_group_interface::group_extent_callback (SCM element_smob, SCM scm_axis)
57 {
58   Score_element *me = unsmob_element (element_smob);
59   Axis a = (Axis) gh_scm2int (scm_axis);
60
61   Score_element * common =(Score_element*) me;
62
63   for (SCM s = me->get_elt_property ("elements"); gh_pair_p (s); s = gh_cdr (s))
64     {
65       Score_element * se = unsmob_element (gh_car (s));
66       common = se->common_refpoint (common, a);
67     }
68
69   Real my_coord = me->relative_coordinate (common, a);
70   Interval r (relative_group_extent (a, common, me->get_elt_property ("elements")));
71
72   return ly_interval2scm (r - my_coord);
73 }
74
75
76
77 void
78 Axis_group_interface::set_axes (Score_element*me,Axis a1, Axis a2)
79 {
80   // set_interface () ?
81   SCM sa1= gh_int2scm (a1);
82   SCM sa2 = gh_int2scm (a2);
83
84   SCM axes = me->get_elt_property ("axes");
85   
86   if (!gh_pair_p (axes)
87       || scm_memq (sa1, axes) == SCM_BOOL_F
88       || scm_memq (sa2, axes) == SCM_BOOL_F)
89     {
90       SCM ax = gh_cons (sa1, SCM_EOL);
91       if (a1 != a2)
92         ax= gh_cons (sa2, ax);
93       me->set_elt_property ("axes", ax);
94     }
95
96   if (a1 != X_AXIS && a2 != X_AXIS)
97     me->set_extent_callback (SCM_EOL, X_AXIS);
98   if (a1 != Y_AXIS && a2 != Y_AXIS)
99     me->set_extent_callback (SCM_EOL, Y_AXIS);
100
101   /*
102     why so convoluted ? (fixme/documentme?) 
103    */
104   if (me->has_extent_callback_b (Score_element::molecule_extent_proc, a1))
105     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc,a1);
106   if (me->has_extent_callback_b (Score_element::molecule_extent_proc, a2))
107     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc,a2);
108 }
109
110 Link_array<Score_element> 
111 Axis_group_interface::get_children (Score_element*me)
112 {
113   Link_array<Score_element> childs;
114   childs.push (me) ;
115
116   if (!has_interface (me))
117     return childs;
118   
119   for (SCM ep = me->get_elt_property ("elements"); gh_pair_p (ep); ep = gh_cdr (ep))
120     {
121       Score_element* e = unsmob_element (gh_car (ep));
122       if (e)
123         childs.concat (Axis_group_interface::get_children (e));
124     }
125   
126   return childs;
127 }
128
129 bool
130 Axis_group_interface::has_interface (Score_element*me)
131 {
132   return me && me->has_interface (ly_symbol2scm ("axis-group-interface"));
133 }
134
135
136
137 void
138 Axis_group_interface::set_interface (Score_element*me)
139 {
140   if (!has_interface (me))
141     {
142       me->set_interface (ly_symbol2scm ("axis-group-interface"));      
143     }
144 }