lilypond-0.1.59
[lilypond.git] / lily / graphical-element.cc
blob79c298928b158af5f34a631161a149d56776d713
1 /*
2 graphical-element.cc -- implement Graphical_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "graphical-element.hh"
10 #include "axis-group-element.hh"
11 #include "debug.hh"
13 bool
14 Graphical_element::empty_b () const
16 return empty_b_;
19 Graphical_element::Graphical_element ()
21 init ();
24 Graphical_element::Graphical_element (Graphical_element const &s)
26 init ();
27 empty_b_ = s.empty_b_;
28 axis_group_l_a_[0] = axis_group_l_a_[1] =0;
29 offset_ = Offset (0,0);
32 Graphical_element::~Graphical_element ()
37 void
38 Graphical_element::init ()
40 empty_b_ = false;
41 axis_group_l_a_[X_AXIS] = axis_group_l_a_[Y_AXIS] =0;
42 offset_ = Offset (0,0);
43 cached_valid_b_a_ [X_AXIS] = cached_valid_b_a_[Y_AXIS] = false;
46 void
47 Graphical_element::invalidate_cache (Axis a)
49 Graphical_element * g = this;
50 while (g && g->cached_valid_b_a_[a])
52 g->cached_valid_b_a_ [a] = false;
53 g = g->axis_group_l_a_[a];
57 Real
58 Graphical_element::absolute_coordinate (Axis a) const
60 Real r = offset_[a];
61 for (Axis_group_element * axis_group_l = axis_group_l_a_[a];
62 axis_group_l; axis_group_l = axis_group_l->axis_group_l_a_[a])
64 r += axis_group_l->offset_[a];
65 return r;
69 Offset
70 Graphical_element::absolute_offset() const
72 return Offset (absolute_coordinate (X_AXIS), absolute_coordinate (Y_AXIS));
75 void
76 Graphical_element::translate_axis (Real y, Axis a)
78 if (axis_group_l_a_[a])
79 axis_group_l_a_[a]->invalidate_cache (a);
80 offset_[a] += y;
83 Real
84 Graphical_element::relative_coordinate (Axis_group_element*e, Axis a) const
86 Real r =0.0;
87 for (Axis_group_element * axis_group_l = axis_group_l_a_[a];
88 axis_group_l != e;
89 axis_group_l = axis_group_l->axis_group_l_a_[a])
90 r += axis_group_l->offset_[a];
92 return r;
95 Axis_group_element*
96 Graphical_element::common_group (Graphical_element const* s, Axis a) const
98 Link_array<Axis_group_element> my_groups;
99 for (Axis_group_element * axis_group_l = axis_group_l_a_[a];
100 axis_group_l;
101 axis_group_l = axis_group_l->axis_group_l_a_[a])
102 my_groups.push (axis_group_l);
104 Axis_group_element* common_l=0;
105 for (Axis_group_element * axis_group_l = s->axis_group_l_a_[a];
106 !common_l && axis_group_l;
107 axis_group_l = axis_group_l->axis_group_l_a_[a])
108 common_l = my_groups.find_l (axis_group_l);
110 return common_l;
115 void
116 Graphical_element::translate (Offset offset)
118 translate_axis (offset[Y_AXIS], Y_AXIS);
119 translate_axis (offset[X_AXIS], X_AXIS);
122 Interval
123 Graphical_element::width() const
125 return extent (X_AXIS);
128 void
129 Graphical_element::set_empty (bool b)
131 if (empty_b_ != b)
133 empty_b_ = b;
134 if (!empty_b_)
136 invalidate_cache (X_AXIS);
137 invalidate_cache (Y_AXIS);
143 Interval
144 Graphical_element::extent (Axis a) const
146 if (empty_b_)
147 return Interval ();
149 if (!cached_valid_b_a_[a])
151 Graphical_element *self = (Graphical_element*)this;
152 self->cached_dimension_a_[a] = (a == X_AXIS)? do_width(): do_height ();
153 self->cached_valid_b_a_[a] = true;
156 Interval r(cached_dimension_a_[a]);
157 if (!r.empty_b()) // float exception on DEC Alpha
158 r+=offset_[a];
160 return r;
163 Interval
164 Graphical_element::height() const
166 return extent (Y_AXIS);
169 void
170 Graphical_element::unlink ()
172 for (int j=0; j < 2; j++)
173 if (axis_group_l_a_[j])
174 axis_group_l_a_[j]->remove_element (this);
177 void
178 Graphical_element::junk_links ()
180 axis_group_l_a_[X_AXIS] = axis_group_l_a_[Y_AXIS] =0;
183 void
184 Graphical_element::print () const
186 #ifndef NPRINT
187 if (offset_.x() || offset_.y ())
188 DOUT << "offset: " << offset_.str() ;
189 DOUT << "\n";
190 #endif
193 IMPLEMENT_IS_TYPE_B(Graphical_element);