]> git.lyx.org Git - lyx.git/blob - src/CoordCache.h
44ac26c66de5232a4714cf2d7af49a8e737eab76
[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;
30         Dimension dim;
31
32         bool covers(int x, int y) const
33         {
34                 return x >= pos.x_ && x < pos.x_ + dim.wid
35                        && y >= pos.y_ - dim.asc && y <= pos.y_ + dim.des;
36         }
37
38         int squareDistance(int x, int y) const
39         {
40                 int xx = 0;
41                 int yy = 0;
42
43                 if (x < pos.x_)
44                         xx = pos.x_ - x;
45                 else if (x > pos.x_ + dim.wid)
46                         xx = x - pos.x_ - dim.wid;
47
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;
52
53                 // Optimisation: We avoid to compute the sqrt on purpose.
54                 return xx*xx + yy*yy;
55         }
56 };
57
58
59 template <class T> class CoordCacheBase {
60 public:
61         void clear()
62         {
63                 data_.clear();
64         }
65
66         bool empty() const
67         {
68                 return data_.empty();
69         }
70
71         void add(T const * thing, int x, int y)
72         {
73                 data_[thing].pos = Point(x, y);
74         }
75
76         void add(T const * thing, Dimension const & dim)
77         {
78                 if (!has(thing))
79                         data_[thing].pos = Point(-10000, -10000);
80                 data_[thing].dim = dim;
81         }
82
83         Geometry & geometry(T const * thing)
84         {
85                 check(thing, "geometry");
86                 return data_.find(thing)->second;
87         }
88
89         Geometry const & geometry(T const * thing) const
90         {
91                 check(thing, "geometry");
92                 return data_.find(thing)->second;
93         }
94
95         Dimension const & dim(T const * thing) const
96         {
97                 checkDim(thing, "dim");
98                 return data_.find(thing)->second.dim;
99         }
100
101         int x(T const * thing) const
102         {
103                 check(thing, "x");
104                 return data_.find(thing)->second.pos.x_;
105         }
106
107         int y(T const * thing) const
108         {
109                 check(thing, "y");
110                 return data_.find(thing)->second.pos.y_;
111         }
112
113         Point xy(T const * thing) const
114         {
115                 check(thing, "xy");
116                 return data_.find(thing)->second.pos;
117         }
118
119         bool has(T const * thing) const
120         {
121                 typename cache_type::const_iterator it = data_.find(thing);
122
123                 if (it == data_.end())
124                         return false;
125                 return it->second.pos.x_ != -10000;
126         }
127
128         bool hasDim(T const * thing) const
129         {
130                 return data_.find(thing) != data_.end();
131         }
132
133         bool covers(T const * thing, int x, int y) const
134         {
135                 typename cache_type::const_iterator it = data_.find(thing);
136                 return it != data_.end() && it->second.covers(x, y);
137         }
138
139         int squareDistance(T const * thing, int x, int y) const
140         {
141                 typename cache_type::const_iterator it = data_.find(thing);
142                 if (it == data_.end())
143                         return 1000000;
144                 return it->second.squareDistance(x, y);
145         }
146
147 private:
148         friend class CoordCache;
149
150         void checkDim(T const * thing, char const * hint) const
151         {
152                 if (!hasDim(thing))
153                         lyxbreaker(thing, hint, data_.size());
154         }
155
156         void check(T const * thing, char const * hint) const
157         {
158                 if (!has(thing))
159                         lyxbreaker(thing, hint, data_.size());
160         }
161
162         typedef std::unordered_map<T const *, Geometry> cache_type;
163         cache_type data_;
164 };
165
166 /**
167  * A BufferView dependent cache that allows us to come from an inset in
168  * a document to a position point and dimension on the screen.
169  * All points cached in this cache are only valid between subsequent
170  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
171  * to the right, y increases downwords.
172  * The dimension part is built in BufferView::updateMetrics() and the
173  * diverse Inset::metrics() calls.
174  * The individual points are added at drawing time in
175  * BufferView::draw(). The math inset position are cached in
176  * the diverse InsetMathXXX::draw() calls and the in-text inset position
177  * are cached in RowPainter::paintInset().
178  * FIXME: For mathed, it would be nice if the insets did not saves their
179  * position themselves. That should be the duty of the containing math
180  * array.
181  */
182 class CoordCache {
183 public:
184         void clear();
185
186         /// A map from MathData to position on the screen
187         typedef CoordCacheBase<MathData> Arrays;
188         Arrays & arrays() { return arrays_; }
189         Arrays const & getArrays() const { return arrays_; }
190         /// A map from insets to positions on the screen
191         typedef CoordCacheBase<Inset> Insets;
192         Insets & insets() { return insets_; }
193         Insets const & getInsets() const { return insets_; }
194
195         /// Dump the contents of the cache to lyxerr in debugging form
196         void dump() const;
197 private:
198         /// MathDatas
199         Arrays arrays_;
200         // All insets
201         Insets insets_;
202 };
203
204 } // namespace lyx
205
206 #endif