3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 // It seems that MacOSX define the check macro.
17 #include "Dimension.h"
19 #include <unordered_map>
26 void lyxbreaker(void const * data, const char * hint, int size);
29 Point pos = {-10000, -10000 };
32 bool covers(int x, int y) const
34 return x >= pos.x_ && x < pos.x_ + dim.wid
35 && y >= pos.y_ - dim.asc && y <= pos.y_ + dim.des;
38 int squareDistance(int x, int y) const
45 else if (x > pos.x_ + dim.wid)
46 xx = x - pos.x_ - dim.wid;
48 if (y < pos.y_ - dim.asc)
49 yy = pos.y_ - dim.asc - y;
50 else if (y > pos.y_ + dim.des)
51 yy = y - pos.y_ - dim.des;
53 // Optimisation: We avoid to compute the sqrt on purpose.
59 template <class T> class CoordCacheBase {
71 void add(T const * thing, int x, int y)
73 data_[thing].pos = Point(x, y);
76 void add(T const * thing, Dimension const & dim)
78 data_[thing].dim = dim;
81 Geometry & geometry(T const * thing)
83 checkDim(thing, "geometry");
84 return data_.find(thing)->second;
87 Geometry const & geometry(T const * thing) const
89 checkDim(thing, "geometry");
90 return data_.find(thing)->second;
93 Dimension const & dim(T const * thing) const
95 checkDim(thing, "dim");
96 return data_.find(thing)->second.dim;
99 int x(T const * thing) const
102 return data_.find(thing)->second.pos.x_;
105 int y(T const * thing) const
108 return data_.find(thing)->second.pos.y_;
111 Point xy(T const * thing) const
114 return data_.find(thing)->second.pos;
117 bool has(T const * thing) const
119 typename cache_type::const_iterator it = data_.find(thing);
121 if (it == data_.end())
123 return it->second.pos.x_ != -10000;
126 bool hasDim(T const * thing) const
128 return data_.find(thing) != data_.end();
131 bool covers(T const * thing, int x, int y) const
133 typename cache_type::const_iterator it = data_.find(thing);
134 return it != data_.end() && it->second.covers(x, y);
137 int squareDistance(T const * thing, int x, int y) const
139 typename cache_type::const_iterator it = data_.find(thing);
140 if (it == data_.end())
142 return it->second.squareDistance(x, y);
146 friend class CoordCache;
148 #ifdef ENABLE_ASSERTIONS
149 void checkDim(T const * thing, char const * hint) const
152 lyxbreaker(thing, hint, data_.size());
155 void check(T const * thing, char const * hint) const
158 lyxbreaker(thing, hint, data_.size());
161 void checkDim(T const *, char const * const ) const {}
163 void check(T const *, char const *) const {}
166 typedef std::unordered_map<T const *, Geometry> cache_type;
171 * A BufferView dependent cache that allows us to come from an inset in
172 * a document to a position point and dimension on the screen.
173 * All points cached in this cache are only valid between subsequent
174 * updates. (x,y) == (0,0) is the upper left screen corner, x increases
175 * to the right, y increases downwords.
176 * The dimension part is built in BufferView::updateMetrics() and the
177 * diverse Inset::metrics() calls.
178 * The individual points are added at drawing time in
179 * BufferView::draw(). The math inset position are cached in
180 * the diverse InsetMathXXX::draw() calls and the in-text inset position
181 * are cached in RowPainter::paintInset().
182 * FIXME: For mathed, it would be nice if the insets did not saves their
183 * position themselves. That should be the duty of the containing math
190 /// A map from MathData to position on the screen
191 typedef CoordCacheBase<MathData> Arrays;
192 Arrays & arrays() { return arrays_; }
193 Arrays const & getArrays() const { return arrays_; }
194 /// A map from insets to positions on the screen
195 typedef CoordCacheBase<Inset> Insets;
196 Insets & insets() { return insets_; }
197 Insets const & getInsets() const { return insets_; }
199 /// Dump the contents of the cache to lyxerr in debugging form