]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-interface.cc
''
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "hara-kiri-group-spanner.hh"
10 #include "axis-group-interface.hh"
11 #include "grob.hh"
12
13 void
14 Axis_group_interface::add_element (Grob*me,Grob *e)
15 {
16   for (SCM ax = me->get_grob_property ("axes"); ax != SCM_EOL ; ax = ly_cdr (ax))
17     {
18       Axis a = (Axis) gh_scm2int (ly_car (ax));
19       
20       if (!e->get_parent (a))
21         e->set_parent (me, a);
22     }
23
24   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), e);
25   me->add_dependency (e);
26 }
27
28 bool
29 Axis_group_interface::axis_b (Grob*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, Grob *common, SCM elts)
42 {
43   Interval r;
44   for (SCM s = elts; gh_pair_p (s); s = ly_cdr (s))
45     {
46       Grob * se = unsmob_grob (ly_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   Grob *me = unsmob_grob (element_smob);
59   Axis a = (Axis) gh_scm2int (scm_axis);
60
61   Grob * common = (Grob*) me;
62
63   for (SCM s = me->get_grob_property ("elements"); gh_pair_p (s); s = ly_cdr (s))
64     {
65       Grob * se = unsmob_grob (ly_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_grob_property ("elements")));
71
72   return ly_interval2scm (r - my_coord);
73 }
74
75 void
76 Axis_group_interface::set_axes (Grob*me,Axis a1, Axis a2)
77 {
78   SCM sa1= gh_int2scm (a1);
79   SCM sa2 = gh_int2scm (a2);
80
81   SCM axes = me->get_grob_property ("axes");
82   
83   if (!gh_pair_p (axes)
84       || scm_memq (sa1, axes) == SCM_BOOL_F
85       || scm_memq (sa2, axes) == SCM_BOOL_F)
86     {
87       SCM ax = gh_cons (sa1, SCM_EOL);
88       if (a1 != a2)
89         ax= gh_cons (sa2, ax);
90       me->set_grob_property ("axes", ax);
91     }
92
93   if (a1 != X_AXIS && a2 != X_AXIS)
94     me->set_extent_callback (SCM_EOL, X_AXIS);
95   if (a1 != Y_AXIS && a2 != Y_AXIS)
96     me->set_extent_callback (SCM_EOL, Y_AXIS);
97
98   /*
99     why so convoluted ? (fixme/documentme?) 
100    */
101   if (me->has_extent_callback_b (Grob::molecule_extent_proc, a1))
102     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc,a1);
103   if (me->has_extent_callback_b (Grob::molecule_extent_proc, a2))
104     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc,a2);
105 }
106
107 Link_array<Grob> 
108 Axis_group_interface::get_children (Grob*me)
109 {
110   Link_array<Grob> childs;
111   childs.push (me) ;
112
113   if (!has_interface (me))
114     return childs;
115   
116   for (SCM ep = me->get_grob_property ("elements"); gh_pair_p (ep); ep = ly_cdr (ep))
117     {
118       Grob* e = unsmob_grob (ly_car (ep));
119       if (e)
120         childs.concat (Axis_group_interface::get_children (e));
121     }
122   
123   return childs;
124 }
125
126
127
128 ADD_INTERFACE (Axis_group_interface, "axis-group-interface",
129   "a group of coupled grobs",
130   "axes");