]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
- Dump parpos cache if you use -dbg workarea on command line
[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 namespace lyx {
29
30 void lyxbreaker(void const * data, const char * hint, int size);
31
32 class Point {
33 public:
34         Point()
35                 : x_(0), y_(0)
36         {}
37
38         Point(int x, int y)
39                 : x_(x), y_(y)
40         {
41                 BOOST_ASSERT(x > -1000000);
42                 BOOST_ASSERT(x <  1000000);
43                 BOOST_ASSERT(y > -1000000);
44                 BOOST_ASSERT(y <  1000000);
45         }
46
47         int x_, y_;
48 };
49
50 template <class T> class CoordCacheBase {
51 public:
52         void clear()
53         {
54                 data_.clear();
55         }
56
57         void add(T const * thing, int x, int y)
58         {
59                 data_[thing] = Point(x, y);
60         }
61
62         int x(T const * thing) const
63         {
64                 check(thing, "x");
65                 return data_.find(thing)->second.x_;
66         }
67
68         int y(T const * thing) const
69         {
70                 check(thing, "y");
71                 return data_.find(thing)->second.y_;
72         }
73
74         Point xy(T const * thing) const
75         {
76                 check(thing, "xy");
77                 return data_.find(thing)->second;
78         }
79
80         bool has(T const * thing) const
81         {
82                 return data_.find(thing) != data_.end();
83         }
84
85 //      T * find(int x, int y) const
86 //      {
87 //              T *
88 //              cache_type iter
89 //      }
90
91 private:
92         friend class CoordCache;
93
94         void check(T const * thing, char const * hint) const
95         {
96                 if (!has(thing))
97                         lyxbreaker(thing, hint, data_.size());
98         }
99
100         typedef std::map<T const *, Point> cache_type;
101         cache_type data_;
102 };
103
104 /**
105  * A global cache that allows us to come from a paragraph in a document
106  * to a position point on the screen.
107  * All points cached in this cache are only valid between subsequent
108  * updates. (x,y) == (0,0) is the upper left screen corner, x increases
109  * to the right, y increases downwords.
110  * The cache is built in BufferView::updateMetrics which is called
111  * from BufferView::Pimpl::update. The individual points are added
112  * while we paint them. See for instance paintPar in RowPainter.C.
113  */
114 class CoordCache {
115 public:
116         void clear();
117         Point get(LyXText const *, lyx::pit_type);
118
119         /// A map from paragraph index number to screen point
120         typedef std::map<lyx::pit_type, Point> InnerParPosCache;
121         /// A map from a LyXText to the map of paragraphs to screen points
122         typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
123         /// A map from a CursorSlice to screen points
124         typedef std::map<LyXText const *, InnerParPosCache> SliceCache;
125
126         /// A map from MathArray to position on the screen
127         CoordCacheBase<MathArray> & arrays() { return arrays_; }
128         CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
129         /// A map from insets to positions on the screen
130         CoordCacheBase<InsetBase> & insets() { return insets_; }
131         CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
132         /// A map from (LyXText, paragraph) pair to screen positions
133         ParPosCache & parPos() { return pars_; }
134         ParPosCache const & getParPos() const { return pars_; }
135         ///
136         SliceCache & slice(bool boundary)
137         {
138                 return boundary ? slices1_ : slices0_;
139         }
140         SliceCache const & getSlice(bool boundary) const
141         {
142                 return boundary ? slices1_ : slices0_;
143         }
144
145         /// Dump the contents of the cache to lyxerr in debugging form
146         void dump() const;
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 } // namespace lyx
161
162 #endif