]> git.lyx.org Git - features.git/blob - src/CoordCache.h
Transfer the Paragraph position cache from CoordCache to ParagraphMetrics and TextMet...
[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 "support/types.h"
18
19 #include <map>
20
21 namespace lyx {
22
23 class Inset;
24 class Text;
25 class MathData;
26 class Paragraph;
27
28 void lyxbreaker(void const * data, const char * hint, int size);
29
30 class Point {
31 public:
32         Point()
33                 : x_(0), y_(0)
34         {}
35
36         Point(int x, int y);
37
38         int x_, y_;
39 };
40
41 template <class T> class CoordCacheBase {
42 public:
43         void clear()
44         {
45                 data_.clear();
46         }
47
48         bool const empty() const
49         {
50                 return data_.empty();
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 public:
100         cache_type const & getData() const { return data_; }
101 };
102
103 /**
104  * A global cache that allows us to come from a paragraph in a document
105  * to a position point on the screen.
106  * All points cached in this cache are only valid between subsequent
107  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
108  * to the right, y increases downwords.
109  * The cache is built in BufferView::updateMetrics which is called
110  * from BufferView::update. The individual points are added
111  * while we paint them. See for instance paintPar in RowPainter.C.
112  */
113 class CoordCache {
114 public:
115         void clear();
116
117         /// A map from paragraph index number to screen point
118         typedef std::map<pit_type, Point> InnerParPosCache;
119         /// A map from a CursorSlice to screen points
120         typedef std::map<Text const *, InnerParPosCache> SliceCache;
121
122         /// A map from MathData to position on the screen
123         CoordCacheBase<MathData> & arrays() { return arrays_; }
124         CoordCacheBase<MathData> const & getArrays() const { return arrays_; }
125         /// A map from insets to positions on the screen
126         CoordCacheBase<Inset> & insets() { return insets_; }
127         CoordCacheBase<Inset> const & getInsets() const { return insets_; }
128         ///
129         SliceCache & slice(bool boundary)
130         {
131                 return boundary ? slices1_ : slices0_;
132         }
133         SliceCache const & getSlice(bool boundary) const
134         {
135                 return boundary ? slices1_ : slices0_;
136         }
137
138         /// Dump the contents of the cache to lyxerr in debugging form
139         void dump() const;
140 private:
141         /// MathDatas
142         CoordCacheBase<MathData> arrays_;
143         // All insets
144         CoordCacheBase<Inset> insets_;
145         /// Used with boundary == 0
146         SliceCache slices0_;
147         /// Used with boundary == 1
148         SliceCache slices1_;
149 };
150
151 } // namespace lyx
152
153 #endif