]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-interface.cc
842641d18166b176d2d85d715e60e6f1cb671698
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "axis-group-interface.hh"
11 #include "hara-kiri-group-spanner.hh"
12
13 void
14 Axis_group_interface::add_element (Grob*me, Grob *e)
15 {
16   for (SCM ax = me->get_property ("axes"); ax != SCM_EOL ; ax = scm_cdr (ax))
17     {
18       Axis a = (Axis) scm_to_int (scm_car (ax));
19       
20       if (!e->get_parent (a))
21         e->set_parent (me, a);
22
23       e->internal_set_property ((a == X_AXIS)
24                                 ? ly_symbol2scm ("axis-group-parent-X")
25                                 : ly_symbol2scm ("axis-group-parent-Y"),
26                                 me->self_scm ()
27                                 );
28     }
29     
30   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), e);
31   me->add_dependency (e);
32 }
33
34 bool
35 Axis_group_interface::has_axis (Grob*me, Axis a)
36 {
37   /*
38     urg. FIXME, check for Hara_kiri_group_spanner shouldn't be necessary?
39    */
40   return me->has_extent_callback (group_extent_callback_proc, a) ||
41  (me->has_extent_callback (Hara_kiri_group_spanner::y_extent_proc, a));
42 }
43
44 Interval
45 Axis_group_interface::relative_group_extent (Axis a, Grob *common, SCM elts)
46 {
47   Interval r;
48   for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
49     {
50       Grob * se = unsmob_grob (scm_car (s));
51       Interval dims = se->extent (common, a);
52       if (!dims.is_empty ())
53         r.unite (dims);
54     }
55   return r;
56 }
57
58 MAKE_SCHEME_CALLBACK (Axis_group_interface, group_extent_callback, 2);
59 SCM
60 Axis_group_interface::group_extent_callback (SCM element_smob, SCM scm_axis)
61 {
62   Grob *me = unsmob_grob (element_smob);
63   Axis a = (Axis) scm_to_int (scm_axis);
64
65   SCM elts = me->get_property ("elements");
66   Grob * common = common_refpoint_of_list (elts, me, a);
67
68   Real my_coord = me->relative_coordinate (common, a);
69   Interval r (relative_group_extent (a, common, elts));
70
71   return ly_interval2scm (r - my_coord);
72 }
73
74 void
75 Axis_group_interface::set_axes (Grob*me, Axis a1, Axis a2)
76 {
77   SCM sa1= scm_int2num (a1);
78   SCM sa2 = scm_int2num (a2);
79
80   SCM axes = me->get_property ("axes");
81   
82   if (!scm_is_pair (axes)
83       || scm_c_memq (sa1, axes) == SCM_BOOL_F
84       || scm_c_memq (sa2, axes) == SCM_BOOL_F)
85     {
86       SCM ax = scm_cons (sa1, SCM_EOL);
87       if (a1 != a2)
88         ax = scm_cons (sa2, ax);
89       me->set_property ("axes", ax);
90     }
91
92   if (a1 != X_AXIS && a2 != X_AXIS)
93     me->set_extent (SCM_EOL, X_AXIS);
94   if (a1 != Y_AXIS && a2 != Y_AXIS)
95     me->set_extent (SCM_EOL, Y_AXIS);
96
97   /*
98     why so convoluted ? (fixme/documentme?) 
99    */
100   if (me->has_extent_callback (Grob::stencil_extent_proc, a1))
101     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc, a1);
102   if (me->has_extent_callback (Grob::stencil_extent_proc, a2))
103     me->set_extent_callback (Axis_group_interface::group_extent_callback_proc, a2);
104 }
105
106 Link_array<Grob> 
107 Axis_group_interface::get_children (Grob*me)
108 {
109   Link_array<Grob> childs;
110   childs.push (me) ;
111
112   if (!has_interface (me))
113     return childs;
114   
115   for (SCM ep = me->get_property ("elements"); scm_is_pair (ep); ep = scm_cdr (ep))
116     {
117       Grob* e = unsmob_grob (scm_car (ep));
118       if (e)
119         childs.concat (Axis_group_interface::get_children (e));
120     }
121   
122   return childs;
123 }
124
125
126
127 ADD_INTERFACE (Axis_group_interface, "axis-group-interface",
128   "An object that groups other layout objects.",
129   "axes elements");