]> git.lyx.org Git - lyx.git/blob - src/CoordCache.h
\cprotect amendment
[lyx.git] / src / CoordCache.h
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #ifndef COORDCACHE_H
12 #define COORDCACHE_H
13
14 // It seems that MacOSX define the check macro.
15 #undef check
16
17 #include "Dimension.h"
18
19 #include <unordered_map>
20
21 namespace lyx {
22
23 class Inset;
24 class MathData;
25
26 void lyxbreaker(void const * data, const char * hint, int size);
27
28 struct Geometry {
29         Point pos = {-10000, -10000 };
30         Dimension dim;
31
32         bool covers(int x, int y) const
33         {
34                 return x >= pos.x
35                         && x <= pos.x + dim.wid
36                         && y >= pos.y - dim.asc
37                         && y <= pos.y + dim.des;
38         }
39
40         int squareDistance(int x, int y) const
41         {
42                 int xx = 0;
43                 int yy = 0;
44
45                 if (x < pos.x)
46                         xx = pos.x - x;
47                 else if (x > pos.x + dim.wid)
48                         xx = x - pos.x - dim.wid;
49
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;
54
55                 // Optimisation: We avoid to compute the sqrt on purpose.
56                 return xx*xx + yy*yy;
57         }
58 };
59
60
61 template <class T> class CoordCacheBase {
62 public:
63         void clear()
64         {
65                 data_.clear();
66         }
67
68         bool empty() const
69         {
70                 return data_.empty();
71         }
72
73         void add(T const * thing, int x, int y)
74         {
75                 data_[thing].pos = Point(x, y);
76         }
77
78         // this returns true if a change is done
79         bool add(T const * thing, Dimension const & dim)
80         {
81                 Geometry g;
82                 g.dim = dim;
83                 auto const result = data_.insert(std::make_pair(thing, g));
84                 // did a new entry get inserted?
85                 if (result.second)
86                         return true;
87                 // otherwise, if the existing value is different, update it
88                 else if (result.first->second.dim != dim) {
89                         result.first->second.dim = dim;
90                         return true;
91                 }
92                 return false;
93         }
94
95         Geometry & geometry(T const * thing)
96         {
97                 checkDim(thing, "geometry");
98                 return data_.find(thing)->second;
99         }
100
101         Geometry const & geometry(T const * thing) const
102         {
103                 checkDim(thing, "geometry");
104                 return data_.find(thing)->second;
105         }
106
107         Dimension const & dim(T const * thing) const
108         {
109                 checkDim(thing, "dim");
110                 return data_.find(thing)->second.dim;
111         }
112
113         int x(T const * thing) const
114         {
115                 check(thing, "x");
116                 return data_.find(thing)->second.pos.x;
117         }
118
119         int y(T const * thing) const
120         {
121                 check(thing, "y");
122                 return data_.find(thing)->second.pos.y;
123         }
124
125         Point xy(T const * thing) const
126         {
127                 check(thing, "xy");
128                 return data_.find(thing)->second.pos;
129         }
130
131         bool has(T const * thing) const
132         {
133                 typename cache_type::const_iterator it = data_.find(thing);
134
135                 if (it == data_.end())
136                         return false;
137                 return it->second.pos.x != -10000;
138         }
139
140         bool hasDim(T const * thing) const
141         {
142                 return data_.find(thing) != data_.end();
143         }
144
145         bool covers(T const * thing, int x, int y) const
146         {
147                 typename cache_type::const_iterator it = data_.find(thing);
148                 return it != data_.end() && it->second.covers(x, y);
149         }
150
151         int squareDistance(T const * thing, int x, int y) const
152         {
153                 typename cache_type::const_iterator it = data_.find(thing);
154                 if (it == data_.end())
155                         return 1000000;
156                 return it->second.squareDistance(x, y);
157         }
158
159 private:
160         friend class CoordCache;
161
162 #ifdef ENABLE_ASSERTIONS
163         void checkDim(T const * thing, char const * hint) const
164         {
165                 if (!hasDim(thing))
166                         lyxbreaker(thing, hint, data_.size());
167         }
168
169         void check(T const * thing, char const * hint) const
170         {
171                 if (!has(thing))
172                         lyxbreaker(thing, hint, data_.size());
173         }
174 #else
175         void checkDim(T const *, char const * const ) const {}
176
177         void check(T const *, char const *) const {}
178 #endif
179
180         typedef std::unordered_map<T const *, Geometry> cache_type;
181         cache_type data_;
182 };
183
184 /**
185  * A BufferView dependent cache that allows us to come from an inset in
186  * a document to a position point and dimension on the screen.
187  * All points cached in this cache are only valid between subsequent
188  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
189  * to the right, y increases downwords.
190  * The dimension part is built in BufferView::updateMetrics() and the
191  * diverse Inset::metrics() calls.
192  * The individual points are added at drawing time in
193  * BufferView::draw(). The math inset position are cached in
194  * the diverse InsetMathXXX::draw() calls and the in-text inset position
195  * are cached in RowPainter::paintInset().
196  * FIXME: For mathed, it would be nice if the insets did not saves their
197  * position themselves. That should be the duty of the containing math
198  * array.
199  */
200 class CoordCache {
201 public:
202         void clear();
203
204         /// A map from MathData to position on the screen
205         typedef CoordCacheBase<MathData> Arrays;
206         Arrays & arrays() { return arrays_; }
207         Arrays const & getArrays() const { return arrays_; }
208         /// A map from insets to positions on the screen
209         typedef CoordCacheBase<Inset> Insets;
210         Insets & insets() { return insets_; }
211         Insets const & getInsets() const { return insets_; }
212
213         /// Dump the contents of the cache to lyxerr in debugging form
214         void dump() const;
215 private:
216         /// MathDatas
217         Arrays arrays_;
218         // All insets
219         Insets insets_;
220 };
221
222 } // namespace lyx
223
224 #endif