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