]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
accept coordinates up to a million...
[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
46 template <class T> class CoordCacheBase {
47 public:
48         void clear()
49         {
50                 data_.clear();
51         }
52
53         void add(T const * thing, int x, int y)
54         {
55                 data_[thing] = Point(x, y);
56         }
57
58         int x(T const * thing) const
59         {
60                 check(thing, "x");
61                 return data_.find(thing)->second.x_;
62         }
63
64         int y(T const * thing) const
65         {
66                 check(thing, "y");
67                 return data_.find(thing)->second.y_;
68         }
69
70         Point xy(T const * thing) const
71         {
72                 check(thing, "xy");
73                 return data_.find(thing)->second;
74         }
75
76         bool has(T const * thing) const
77         {
78                 return data_.find(thing) != data_.end();
79         }
80
81 //      T * find(int x, int y) const
82 //      {
83 //              T *
84 //              cache_type iter
85 //      }
86
87 private:
88         friend class CoordCache;
89
90         void check(T const * thing, char const * hint) const
91         {
92                 if (!has(thing))
93                         lyxbreaker(thing, hint, data_.size());
94         }
95
96         typedef std::map<T const *, Point> cache_type;
97         cache_type data_;
98 };
99
100 /**
101  * A global cache that allows us to come from a paragraph in a document
102  * to a position point on the screen.
103  * All points cached in this cache are only valid between subsequent
104  * updated. (x,y) == (0,0) is the upper left screen corner, x increases
105  * to the right, y increases downwords.
106  * The cache is built in BufferView::Pimpl::metrics which is called
107  * from BufferView::Pimpl::update. The individual points are added
108  * while we paint them. See for instance paintPar in RowPainter.C.
109  */
110 class CoordCache {
111 public:
112         CoordCache() : updating(false) { }
113         /// In order to find bugs, we record when we start updating the cache
114         void startUpdating();
115         /// When we are done, we record that to help find bugs
116         void doneUpdating();
117
118         void clear();
119         Point get(LyXText const *, lyx::pit_type);
120
121         /// A map from paragraph index number to screen point
122         typedef std::map<lyx::pit_type, Point> InnerParPosCache;
123         /// A map from a LyXText to the map of paragraphs to screen points
124         typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
125
126         /// A map from MathArray to position on the screen
127         CoordCacheBase<MathArray> & arrays() { BOOST_ASSERT(updating); return arrays_; }
128         CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
129         /// A map from insets to positions on the screen
130         CoordCacheBase<InsetBase> & insets() { BOOST_ASSERT(updating); return insets_; }
131         CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
132         /// A map from (LyXText, paragraph) pair to screen positions
133         ParPosCache & parPos() { BOOST_ASSERT(updating); return pars_; }
134         ParPosCache const & getParPos() const { return pars_; }
135 private:
136         CoordCacheBase<MathArray> arrays_;
137
138         // all insets
139         CoordCacheBase<InsetBase> insets_;
140
141         // paragraph grouped by owning text
142         ParPosCache pars_;
143
144         /**
145          * Debugging flag only: Set to true while the cache is being built.
146          * No changes to the structure are allowed unless we are updating.
147          */
148         bool updating;
149 };
150
151 extern CoordCache theCoords;
152
153 #endif