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