9 #include "support/types.h"
11 #include <boost/assert.hpp>
15 void lyxbreaker(void const * data, const char * hint, int size);
23 Point(int x, int y) : x_(x), y_(y)
25 BOOST_ASSERT(x > -3000);
26 BOOST_ASSERT(x < 4000);
27 BOOST_ASSERT(y > -3000);
28 BOOST_ASSERT(y < 4000);
35 template <class T> class CoordCacheBase {
42 void add(T const * thing, int x, int y)
44 data_[thing] = Point(x, y);
47 int x(T const * thing) const
50 return data_.find(thing)->second.x_;
53 int y(T const * thing) const
56 return data_.find(thing)->second.y_;
59 Point xy(T const * thing) const
62 return data_.find(thing)->second;
65 bool has(T const * thing) const
67 return data_.find(thing) != data_.end();
70 // T * find(int x, int y) const
77 friend class CoordCache;
79 void check(T const * thing, char const * hint) const
82 lyxbreaker(thing, hint, data_.size());
85 typedef std::map<T const *, Point> cache_type;
90 * A global cache that allows us to come from a paragraph in a document
91 * to a position point on the screen.
92 * All points cached in this cache are only valid between subsequent
93 * updated. (x,y) == (0,0) is the upper left screen corner, x increases
94 * to the right, y increases downwords.
95 * The cache is built in BufferView::Pimpl::metrics which is called
96 * from BufferView::Pimpl::update. The individual points are added
97 * while we paint them. See for instance paintPar in RowPainter.C.
101 CoordCache() : updating(false) { }
102 /// In order to find bugs, we record when we start updating the cache
103 void startUpdating();
104 /// When we are done, we record that to help find bugs
108 Point get(LyXText const *, lyx::pit_type);
110 /// A map from paragraph index number to screen point
111 typedef std::map<lyx::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;
115 /// A map from MathArray to position on the screen
116 CoordCacheBase<MathArray> & arrays() { BOOST_ASSERT(updating); return arrays_; }
117 CoordCacheBase<MathArray> const & getArrays() const { return arrays_; }
118 /// A map from insets to positions on the screen
119 CoordCacheBase<InsetBase> & insets() { BOOST_ASSERT(updating); return insets_; }
120 CoordCacheBase<InsetBase> const & getInsets() const { return insets_; }
121 /// A map from (LyXText, paragraph) pair to screen positions
122 ParPosCache & parPos() { BOOST_ASSERT(updating); return pars_; }
123 ParPosCache const & getParPos() const { return pars_; }
125 CoordCacheBase<MathArray> arrays_;
128 CoordCacheBase<InsetBase> insets_;
130 // paragraph grouped by owning text
134 * Debugging flag only: Set to true while the cache is being built.
135 * No changes to the structure are allowed unless we are updating.
140 extern CoordCache theCoords;