3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
19 #include "support/types.h"
21 #include <boost/assert.hpp>
25 void lyxbreaker(void const * data, const char * hint, int size);
33 Point(int x, int y) : x_(x), y_(y)
35 BOOST_ASSERT(x > -3000);
36 BOOST_ASSERT(x < 4000);
37 BOOST_ASSERT(y > -3000);
38 BOOST_ASSERT(y < 4000);
45 template <class T> class CoordCacheBase {
52 void add(T const * thing, int x, int y)
54 data_[thing] = Point(x, y);
57 int x(T const * thing) const
60 return data_.find(thing)->second.x_;
63 int y(T const * thing) const
66 return data_.find(thing)->second.y_;
69 Point xy(T const * thing) const
72 return data_.find(thing)->second;
75 bool has(T const * thing) const
77 return data_.find(thing) != data_.end();
80 // T * find(int x, int y) const
87 friend class CoordCache;
89 void check(T const * thing, char const * hint) const
92 lyxbreaker(thing, hint, data_.size());
95 typedef std::map<T const *, Point> cache_type;
100 * A global cache that allows us to come from a paragraph in a document
101 * to a position point on the screen.
102 * All points cached in this cache are only valid between subsequent
103 * updated. (x,y) == (0,0) is the upper left screen corner, x increases
104 * to the right, y increases downwords.
105 * The cache is built in BufferView::Pimpl::metrics which is called
106 * from BufferView::Pimpl::update. The individual points are added
107 * while we paint them. See for instance paintPar in RowPainter.C.
111 CoordCache() : updating(false) { }
112 /// In order to find bugs, we record when we start updating the cache
113 void startUpdating();
114 /// When we are done, we record that to help find bugs
118 Point get(LyXText const *, lyx::pit_type);
120 /// A map from paragraph index number to screen point
121 typedef std::map<lyx::pit_type, Point> InnerParPosCache;
122 /// A map from a LyXText to the map of paragraphs to screen points
123 typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
125 /// A map from MathArray to position on the screen
126 CoordCacheBase<MathArray> & arrays() { BOOST_ASSERT(updating); return arrays_; }
127 CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
128 /// A map from insets to positions on the screen
129 CoordCacheBase<InsetBase> & insets() { BOOST_ASSERT(updating); return insets_; }
130 CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
131 /// A map from (LyXText, paragraph) pair to screen positions
132 ParPosCache & parPos() { BOOST_ASSERT(updating); return pars_; }
133 ParPosCache const & getParPos() const { return pars_; }
135 CoordCacheBase<MathArray> arrays_;
138 CoordCacheBase<InsetBase> insets_;
140 // paragraph grouped by owning text
144 * Debugging flag only: Set to true while the cache is being built.
145 * No changes to the structure are allowed unless we are updating.
150 extern CoordCache theCoords;