]> git.lyx.org Git - lyx.git/blob - src/CoordCache.h
2b63e6d340a2c5430c45c217355aa28febe62792
[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
57
58 template <class T> class CoordCacheBase {
59 public:
60         void clear()
61         {
62                 data_.clear();
63         }
64
65         bool empty() const
66         {
67                 return data_.empty();
68         }
69
70         void add(T const * thing, int x, int y)
71         {
72                 data_[thing].pos = Point(x, y);
73         }
74
75         void add(T const * thing, Dimension const & dim)
76         {
77                 data_[thing].dim = dim;
78         }
79
80         Dimension const & dim(T const * thing) const
81         {
82                 check(thing, "dim");
83                 return data_.find(thing)->second.dim;
84         }
85
86         int x(T const * thing) const
87         {
88                 check(thing, "x");
89                 return data_.find(thing)->second.pos.x_;
90         }
91
92         int y(T const * thing) const
93         {
94                 check(thing, "y");
95                 return data_.find(thing)->second.pos.y_;
96         }
97
98         Point xy(T const * thing) const
99         {
100                 check(thing, "xy");
101                 return data_.find(thing)->second.pos;
102         }
103
104         bool has(T const * thing) const
105         {
106                 return data_.find(thing) != data_.end();
107         }
108
109         bool covers(T const * thing, int x, int y) const
110         {
111                 cache_type::const_iterator it = data_.find(thing);
112                 return it != data_.end() && it->second.covers(x, y);
113         }
114
115 private:
116         friend class CoordCache;
117
118         void check(T const * thing, char const * hint) const
119         {
120                 if (!has(thing))
121                         lyxbreaker(thing, hint, data_.size());
122         }
123
124         typedef std::map<T const *, Geometry> cache_type;
125         cache_type data_;
126
127 public:
128         cache_type const & getData() const { return data_; }
129 };
130
131 /**
132  * A BufferView dependent cache that allows us to come from an inset in
133  * a document to a position point and dimension on the screen.
134  * All points cached in this cache are only valid between subsequent
135  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
136  * to the right, y increases downwords.
137  * The dimension part is built in BufferView::updateMetrics() and the 
138  * diverse Inset::metrics() calls.
139  * The individual points are added at drawing time in
140  * BufferView::updateMetrics(). The math inset position are cached in
141  * the diverse InsetMathXXX::draw() calls and the in-text inset position
142  * are cached in RowPainter::paintInset().
143  * FIXME: For mathed, it would be nice if the insets did not saves their
144  * position themselves. That should be the duty of the containing math
145  * array.
146  */
147 class CoordCache {
148 public:
149         void clear();
150
151         /// A map from MathData to position on the screen
152         CoordCacheBase<MathData> & arrays() { return arrays_; }
153         CoordCacheBase<MathData> const & getArrays() const { return arrays_; }
154         /// A map from insets to positions on the screen
155         CoordCacheBase<Inset> & insets() { return insets_; }
156         CoordCacheBase<Inset> const & getInsets() const { return insets_; }
157
158         /// Dump the contents of the cache to lyxerr in debugging form
159         void dump() const;
160 private:
161         /// MathDatas
162         CoordCacheBase<MathData> arrays_;
163         // All insets
164         CoordCacheBase<Inset> insets_;
165 };
166
167 } // namespace lyx
168
169 #endif