]> git.lyx.org Git - lyx.git/blob - src/CoordCache.h
Capitalize labels of floats, etc. Fixes #11993.
[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 <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_
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         void add(T const * thing, Dimension const & dim)
79         {
80                 if (!has(thing))
81                         data_[thing].pos = Point(-10000, -10000);
82                 data_[thing].dim = dim;
83         }
84
85         Geometry const & geometry(T const * thing) const
86         {
87                 check(thing, "geometry");
88                 return data_.find(thing)->second;
89         }
90
91         Dimension const & dim(T const * thing) const
92         {
93                 checkDim(thing, "dim");
94                 return data_.find(thing)->second.dim;
95         }
96
97         int x(T const * thing) const
98         {
99                 check(thing, "x");
100                 return data_.find(thing)->second.pos.x_;
101         }
102
103         int y(T const * thing) const
104         {
105                 check(thing, "y");
106                 return data_.find(thing)->second.pos.y_;
107         }
108
109         Point xy(T const * thing) const
110         {
111                 check(thing, "xy");
112                 return data_.find(thing)->second.pos;
113         }
114
115         bool has(T const * thing) const
116         {
117                 typename cache_type::const_iterator it = data_.find(thing);
118
119                 if (it == data_.end())
120                         return false;
121                 return it->second.pos.x_ != -10000;
122         }
123
124         bool hasDim(T const * thing) const
125         {
126                 return data_.find(thing) != data_.end();
127         }
128
129         bool covers(T const * thing, int x, int y) const
130         {
131                 typename cache_type::const_iterator it = data_.find(thing);
132                 return it != data_.end() && it->second.covers(x, y);
133         }
134
135         int squareDistance(T const * thing, int x, int y) const
136         {
137                 typename cache_type::const_iterator it = data_.find(thing);
138                 if (it == data_.end())
139                         return 1000000;
140                 return it->second.squareDistance(x, y);
141         }
142
143 private:
144         friend class CoordCache;
145
146         void checkDim(T const * thing, char const * hint) const
147         {
148                 if (!hasDim(thing))
149                         lyxbreaker(thing, hint, data_.size());
150         }
151
152         void check(T const * thing, char const * hint) const
153         {
154                 if (!has(thing))
155                         lyxbreaker(thing, hint, data_.size());
156         }
157
158         typedef std::map<T const *, Geometry> cache_type;
159         cache_type data_;
160 };
161
162 /**
163  * A BufferView dependent cache that allows us to come from an inset in
164  * a document to a position point and dimension on the screen.
165  * All points cached in this cache are only valid between subsequent
166  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
167  * to the right, y increases downwords.
168  * The dimension part is built in BufferView::updateMetrics() and the
169  * diverse Inset::metrics() calls.
170  * The individual points are added at drawing time in
171  * BufferView::draw(). The math inset position are cached in
172  * the diverse InsetMathXXX::draw() calls and the in-text inset position
173  * are cached in RowPainter::paintInset().
174  * FIXME: For mathed, it would be nice if the insets did not saves their
175  * position themselves. That should be the duty of the containing math
176  * array.
177  */
178 class CoordCache {
179 public:
180         void clear();
181
182         /// A map from MathData to position on the screen
183         typedef CoordCacheBase<MathData> Arrays;
184         Arrays & arrays() { return arrays_; }
185         Arrays const & getArrays() const { return arrays_; }
186         /// A map from insets to positions on the screen
187         typedef CoordCacheBase<Inset> Insets;
188         Insets & insets() { return insets_; }
189         Insets 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         Arrays arrays_;
196         // All insets
197         Insets insets_;
198 };
199
200 } // namespace lyx
201
202 #endif