]> git.lyx.org Git - lyx.git/blob - src/CoordCache.h
Fixed some lines that were too long. It compiled afterwards.
[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 // 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         Point get(Text const *, pit_type) const;
117
118         /// A map from paragraph index number to screen point
119         typedef std::map<pit_type, Point> InnerParPosCache;
120         /// A map from a Text to the map of paragraphs to screen points
121         typedef std::map<Text const *, InnerParPosCache> ParPosCache;
122         /// A map from a CursorSlice to screen points
123         typedef std::map<Text const *, InnerParPosCache> SliceCache;
124
125         /// A map from MathData to position on the screen
126         CoordCacheBase<MathData> & arrays() { return arrays_; }
127         CoordCacheBase<MathData> const & getArrays() const { return arrays_; }
128         /// A map from insets to positions on the screen
129         CoordCacheBase<Inset> & insets() { return insets_; }
130         CoordCacheBase<Inset> const & getInsets() const { return insets_; }
131         /// A map from (Text, paragraph) pair to screen positions
132         ParPosCache & parPos() { return pars_; }
133         ParPosCache const & getParPos() const { return pars_; }
134         ///
135         SliceCache & slice(bool boundary)
136         {
137                 return boundary ? slices1_ : slices0_;
138         }
139         SliceCache const & getSlice(bool boundary) const
140         {
141                 return boundary ? slices1_ : slices0_;
142         }
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         /// Paragraph grouped by owning text
152         ParPosCache pars_;
153         /// Used with boundary == 0
154         SliceCache slices0_;
155         /// Used with boundary == 1
156         SliceCache slices1_;
157 };
158
159 } // namespace lyx
160
161 #endif