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