]> git.lyx.org Git - features.git/blob - src/CoordCache.h
a93696df1e7a9dd0cbd4021b82957d5722ad9985
[features.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
49
50 template <class T> class CoordCacheBase {
51 public:
52         void clear()
53         {
54                 data_.clear();
55         }
56
57         bool empty() const
58         {
59                 return data_.empty();
60         }
61
62         void add(T const * thing, int x, int y)
63         {
64                 data_[thing].pos = Point(x, y);
65         }
66
67         void add(T const * thing, Dimension const & dim)
68         {
69                 data_[thing].dim = dim;
70         }
71
72         Dimension const & dim(T const * thing) const
73         {
74                 //check(thing, "dim");
75                 return data_.find(thing)->second.dim;
76         }
77
78         int x(T const * thing) const
79         {
80                 check(thing, "x");
81                 return data_.find(thing)->second.pos.x_;
82         }
83
84         int y(T const * thing) const
85         {
86                 check(thing, "y");
87                 return data_.find(thing)->second.pos.y_;
88         }
89
90         Point xy(T const * thing) const
91         {
92                 check(thing, "xy");
93                 return data_.find(thing)->second.pos;
94         }
95
96         bool has(T const * thing) const
97         {
98                 return data_.find(thing) != data_.end();
99         }
100
101 //      T * find(int x, int y) const
102 //      {
103 //              T *
104 //              cache_type iter
105 //      }
106
107 private:
108         friend class CoordCache;
109
110         void check(T const * thing, char const * hint) const
111         {
112                 if (!has(thing))
113                         lyxbreaker(thing, hint, data_.size());
114         }
115
116         typedef std::map<T const *, Geometry> cache_type;
117         cache_type data_;
118
119 public:
120         cache_type const & getData() const { return data_; }
121 };
122
123 /**
124  * A global cache that allows us to come from a paragraph in a document
125  * to a position point on the screen.
126  * All points cached in this cache are only valid between subsequent
127  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
128  * to the right, y increases downwords.
129  * The cache is built in BufferView::updateMetrics which is called
130  * from BufferView::update. The individual points are added
131  * while we paint them. See for instance paintPar in RowPainter.C.
132  */
133 class CoordCache {
134 public:
135         void clear();
136
137         /// A map from MathData to position on the screen
138         CoordCacheBase<MathData> & arrays() { return arrays_; }
139         CoordCacheBase<MathData> const & getArrays() const { return arrays_; }
140         /// A map from insets to positions on the screen
141         CoordCacheBase<Inset> & insets() { return insets_; }
142         CoordCacheBase<Inset> const & getInsets() const { return insets_; }
143
144         /// Dump the contents of the cache to lyxerr in debugging form
145         void dump() const;
146 private:
147         /// MathDatas
148         CoordCacheBase<MathData> arrays_;
149         // All insets
150         CoordCacheBase<Inset> insets_;
151 };
152
153 } // namespace lyx
154
155 #endif