whitespace.
[lyx.git] / src / CoordCache.h
bloba666a4ecb025bb291b1eecc6ec8d3f5b5c279107
1 // -*- C++ -*-
2 /* \file CoordCache.h
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #ifndef COORDCACHE_H
12 #define COORDCACHE_H
14 // It seems that MacOSX define the check macro.
15 #undef check
17 #include "Dimension.h"
19 #include "support/types.h"
21 #include <map>
23 namespace lyx {
25 class Inset;
26 class Text;
27 class MathData;
28 class Paragraph;
30 void lyxbreaker(void const * data, const char * hint, int size);
32 struct Geometry {
33 Point pos;
34 Dimension dim;
36 bool covers(int x, int y) const
38 return x >= pos.x_
39 && x <= pos.x_ + dim.wid
40 && y >= pos.y_ - dim.asc
41 && y <= pos.y_ + dim.des;
44 int squareDistance(int x, int y) const
46 int xx = 0;
47 int yy = 0;
49 if (x < pos.x_)
50 xx = pos.x_ - x;
51 else if (x > pos.x_ + dim.wid)
52 xx = x - pos.x_ - dim.wid;
54 if (y < pos.y_ - dim.asc)
55 yy = pos.y_ - dim.asc - y;
56 else if (y > pos.y_ + dim.des)
57 yy = y - pos.y_ - dim.des;
59 // Optimisation: We avoid to compute the sqrt on purpose.
60 return xx*xx + yy*yy;
65 template <class T> class CoordCacheBase {
66 public:
67 void clear()
69 data_.clear();
72 bool empty() const
74 return data_.empty();
77 void add(T const * thing, int x, int y)
79 data_[thing].pos = Point(x, y);
82 void add(T const * thing, Dimension const & dim)
84 data_[thing].dim = dim;
87 Geometry const & geometry(T const * thing) const
89 check(thing, "geometry");
90 return data_.find(thing)->second;
93 Dimension const & dim(T const * thing) const
95 check(thing, "dim");
96 return data_.find(thing)->second.dim;
99 int x(T const * thing) const
101 check(thing, "x");
102 return data_.find(thing)->second.pos.x_;
105 int y(T const * thing) const
107 check(thing, "y");
108 return data_.find(thing)->second.pos.y_;
111 Point xy(T const * thing) const
113 check(thing, "xy");
114 return data_.find(thing)->second.pos;
117 bool has(T const * thing) const
119 return data_.find(thing) != data_.end();
122 bool covers(T const * thing, int x, int y) const
124 typename cache_type::const_iterator it = data_.find(thing);
125 return it != data_.end() && it->second.covers(x, y);
128 int squareDistance(T const * thing, int x, int y) const
130 typename cache_type::const_iterator it = data_.find(thing);
131 if (it == data_.end())
132 return 1000000;
133 return it->second.squareDistance(x, y);
136 private:
137 friend class CoordCache;
139 void check(T const * thing, char const * hint) const
141 if (!has(thing))
142 lyxbreaker(thing, hint, data_.size());
145 typedef std::map<T const *, Geometry> cache_type;
146 cache_type data_;
148 public:
149 cache_type const & getData() const { return data_; }
153 * A BufferView dependent cache that allows us to come from an inset in
154 * a document to a position point and dimension on the screen.
155 * All points cached in this cache are only valid between subsequent
156 * updates. (x,y) == (0,0) is the upper left screen corner, x increases
157 * to the right, y increases downwords.
158 * The dimension part is built in BufferView::updateMetrics() and the
159 * diverse Inset::metrics() calls.
160 * The individual points are added at drawing time in
161 * BufferView::updateMetrics(). The math inset position are cached in
162 * the diverse InsetMathXXX::draw() calls and the in-text inset position
163 * are cached in RowPainter::paintInset().
164 * FIXME: For mathed, it would be nice if the insets did not saves their
165 * position themselves. That should be the duty of the containing math
166 * array.
168 class CoordCache {
169 public:
170 void clear();
172 /// A map from MathData to position on the screen
173 CoordCacheBase<MathData> & arrays() { return arrays_; }
174 CoordCacheBase<MathData> const & getArrays() const { return arrays_; }
175 /// A map from insets to positions on the screen
176 CoordCacheBase<Inset> & insets() { return insets_; }
177 CoordCacheBase<Inset> const & getInsets() const { return insets_; }
179 /// Dump the contents of the cache to lyxerr in debugging form
180 void dump() const;
181 private:
182 /// MathDatas
183 CoordCacheBase<MathData> arrays_;
184 // All insets
185 CoordCacheBase<Inset> insets_;
188 } // namespace lyx
190 #endif