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