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