]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
ws changes mostly
[lyx.git] / src / coordcache.h
1 #ifndef COORDCACHE_H
2 #define COORDCACHE_H
3
4 class InsetBase;
5 class LyXText;
6 class MathArray;
7 class Paragraph;
8
9 #include "support/types.h"
10
11 #include <boost/assert.hpp>
12
13 #include <map>
14
15 void lyxbreaker(void const * data, const char * hint, int size);
16
17 class Point {
18 public:
19         Point()
20                 : x_(0), y_(0)
21         {}
22
23         Point(int x, int y) : x_(x), y_(y)
24         {
25                 BOOST_ASSERT(x > -3000);
26                 BOOST_ASSERT(x <  4000);
27                 BOOST_ASSERT(y > -3000);
28                 BOOST_ASSERT(y <  4000);
29         }
30
31         int x_, y_;
32 };
33
34
35 template <class T> class CoordCacheBase {
36 public:
37         void clear()
38         {
39                 data_.clear();
40         }
41
42         void add(T const * thing, int x, int y)
43         {
44                 data_[thing] = Point(x, y);
45         }
46
47         int x(T const * thing) const
48         {
49                 check(thing, "x");
50                 return data_.find(thing)->second.x_;
51         }
52
53         int y(T const * thing) const
54         {
55                 check(thing, "y");
56                 return data_.find(thing)->second.y_;
57         }
58
59         Point xy(T const * thing) const
60         {
61                 check(thing, "xy");
62                 return data_.find(thing)->second;
63         }
64
65         bool has(T const * thing) const
66         {
67                 return data_.find(thing) != data_.end();
68         }
69
70 //      T * find(int x, int y) const
71 //      {
72 //              T *
73 //              cache_type iter
74 //      }
75
76 private:
77         friend class CoordCache;
78
79         void check(T const * thing, char const * hint) const
80         {
81                 if (!has(thing))
82                         lyxbreaker(thing, hint, data_.size());
83         }
84
85         typedef std::map<T const *, Point> cache_type;
86         cache_type data_;
87 };
88
89 /**
90  * A global cache that allows us to come from a paragraph in a document
91  * to a position point on the screen.
92  * All points cached in this cache are only valid between subsequent
93  * updated. (x,y) == (0,0) is the upper left screen corner, x increases
94  * to the right, y increases downwords.
95  * The cache is built in BufferView::Pimpl::metrics which is called
96  * from BufferView::Pimpl::update. The individual points are added
97  * while we paint them. See for instance paintPar in RowPainter.C.
98  */
99 class CoordCache {
100 public:
101         CoordCache() : updating(false) { }
102         /// In order to find bugs, we record when we start updating the cache
103         void startUpdating();
104         /// When we are done, we record that to help find bugs
105         void doneUpdating();
106
107         void clear();
108         Point get(LyXText const *, lyx::pit_type);
109
110         /// A map from paragraph index number to screen point
111         typedef std::map<lyx::pit_type, Point> InnerParPosCache;
112         /// A map from a LyXText to the map of paragraphs to screen points
113         typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
114
115         /// A map from MathArray to position on the screen
116         CoordCacheBase<MathArray> & arrays() { BOOST_ASSERT(updating); return arrays_; }
117         CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
118         /// A map from insets to positions on the screen
119         CoordCacheBase<InsetBase> & insets() { BOOST_ASSERT(updating); return insets_; }
120         CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
121         /// A map from (LyXText, paragraph) pair to screen positions
122         ParPosCache & parPos() { BOOST_ASSERT(updating); return pars_; }
123         ParPosCache const & getParPos() const { return pars_; }
124 private:
125         CoordCacheBase<MathArray> arrays_;
126         
127         // all insets
128         CoordCacheBase<InsetBase> insets_;
129
130         // paragraph grouped by owning text
131         ParPosCache pars_;
132
133         /**
134          * Debugging flag only: Set to true while the cache is being built.
135          * No changes to the structure are allowed unless we are updating.
136          */
137         bool updating;
138 };
139
140 extern CoordCache theCoords;
141
142 #endif