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);
36 BOOST_ASSERT(x > -1000000);
37 BOOST_ASSERT(x < 1000000);
38 BOOST_ASSERT(y > -1000000);
39 BOOST_ASSERT(y < 1000000);
46 template <class T> class CoordCacheBase {
53 void add(T const * thing, int x, int y)
55 data_[thing] = Point(x, y);
58 int x(T const * thing) const
61 return data_.find(thing)->second.x_;
64 int y(T const * thing) const
67 return data_.find(thing)->second.y_;
70 Point xy(T const * thing) const
73 return data_.find(thing)->second;
76 bool has(T const * thing) const
78 return data_.find(thing) != data_.end();
81 // T * find(int x, int y) const
88 friend class CoordCache;
90 void check(T const * thing, char const * hint) const
93 lyxbreaker(thing, hint, data_.size());
96 typedef std::map<T const *, Point> cache_type;
101 * A global cache that allows us to come from a paragraph in a document
102 * to a position point on the screen.
103 * All points cached in this cache are only valid between subsequent
104 * updated. (x,y) == (0,0) is the upper left screen corner, x increases
105 * to the right, y increases downwords.
106 * The cache is built in BufferView::Pimpl::metrics which is called
107 * from BufferView::Pimpl::update. The individual points are added
108 * while we paint them. See for instance paintPar in RowPainter.C.
112 CoordCache() : updating(false) { }
113 /// In order to find bugs, we record when we start updating the cache
114 void startUpdating();
115 /// When we are done, we record that to help find bugs
119 Point get(LyXText const *, lyx::pit_type);
121 /// A map from paragraph index number to screen point
122 typedef std::map<lyx::pit_type, Point> InnerParPosCache;
123 /// A map from a LyXText to the map of paragraphs to screen points
124 typedef std::map<LyXText const *, InnerParPosCache> ParPosCache;
126 /// A map from MathArray to position on the screen
127 CoordCacheBase<MathArray> & arrays() { BOOST_ASSERT(updating); return arrays_; }
128 CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
129 /// A map from insets to positions on the screen
130 CoordCacheBase<InsetBase> & insets() { BOOST_ASSERT(updating); return insets_; }
131 CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
132 /// A map from (LyXText, paragraph) pair to screen positions
133 ParPosCache & parPos() { BOOST_ASSERT(updating); return pars_; }
134 ParPosCache const & getParPos() const { return pars_; }
136 CoordCacheBase<MathArray> arrays_;
139 CoordCacheBase<InsetBase> insets_;
141 // paragraph grouped by owning text
145 * Debugging flag only: Set to true while the cache is being built.
146 * No changes to the structure are allowed unless we are updating.
151 extern CoordCache theCoords;