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
35 && x <= pos.x + dim.wid
36 && y >= pos.y - dim.asc
37 && y <= pos.y + dim.des;
40 int squareDistance(int x, int y) const
47 else if (x > pos.x + dim.wid)
48 xx = x - pos.x - dim.wid;
50 if (y < pos.y - dim.asc)
51 yy = pos.y - dim.asc - y;
52 else if (y > pos.y + dim.des)
53 yy = y - pos.y - dim.des;
55 // Optimisation: We avoid to compute the sqrt on purpose.
61 template <class T> class CoordCacheBase {
73 void add(T const * thing, int x, int y)
75 data_[thing].pos = Point(x, y);
78 void add(T const * thing, Dimension const & dim)
80 data_[thing].dim = dim;
83 Geometry & geometry(T const * thing)
85 checkDim(thing, "geometry");
86 return data_.find(thing)->second;
89 Geometry const & geometry(T const * thing) const
91 checkDim(thing, "geometry");
92 return data_.find(thing)->second;
95 Dimension const & dim(T const * thing) const
97 checkDim(thing, "dim");
98 return data_.find(thing)->second.dim;
101 int x(T const * thing) const
104 return data_.find(thing)->second.pos.x;
107 int y(T const * thing) const
110 return data_.find(thing)->second.pos.y;
113 Point xy(T const * thing) const
116 return data_.find(thing)->second.pos;
119 bool has(T const * thing) const
121 typename cache_type::const_iterator it = data_.find(thing);
123 if (it == data_.end())
125 return it->second.pos.x != -10000;
128 bool hasDim(T const * thing) const
130 return data_.find(thing) != data_.end();
133 bool covers(T const * thing, int x, int y) const
135 typename cache_type::const_iterator it = data_.find(thing);
136 return it != data_.end() && it->second.covers(x, y);
139 int squareDistance(T const * thing, int x, int y) const
141 typename cache_type::const_iterator it = data_.find(thing);
142 if (it == data_.end())
144 return it->second.squareDistance(x, y);
148 friend class CoordCache;
150 #ifdef ENABLE_ASSERTIONS
151 void checkDim(T const * thing, char const * hint) const
154 lyxbreaker(thing, hint, data_.size());
157 void check(T const * thing, char const * hint) const
160 lyxbreaker(thing, hint, data_.size());
163 void checkDim(T const *, char const * const ) const {}
165 void check(T const *, char const *) const {}
168 typedef std::unordered_map<T const *, Geometry> cache_type;
173 * A BufferView dependent cache that allows us to come from an inset in
174 * a document to a position point and dimension on the screen.
175 * All points cached in this cache are only valid between subsequent
176 * updates. (x,y) == (0,0) is the upper left screen corner, x increases
177 * to the right, y increases downwords.
178 * The dimension part is built in BufferView::updateMetrics() and the
179 * diverse Inset::metrics() calls.
180 * The individual points are added at drawing time in
181 * BufferView::draw(). The math inset position are cached in
182 * the diverse InsetMathXXX::draw() calls and the in-text inset position
183 * are cached in RowPainter::paintInset().
184 * FIXME: For mathed, it would be nice if the insets did not saves their
185 * position themselves. That should be the duty of the containing math
192 /// A map from MathData to position on the screen
193 typedef CoordCacheBase<MathData> Arrays;
194 Arrays & arrays() { return arrays_; }
195 Arrays const & getArrays() const { return arrays_; }
196 /// A map from insets to positions on the screen
197 typedef CoordCacheBase<Inset> Insets;
198 Insets & insets() { return insets_; }
199 Insets const & getInsets() const { return insets_; }
201 /// Dump the contents of the cache to lyxerr in debugging form