Consider the case where there is not any layout name.
[lyx.git] / src / coordcache.h
blob4fe2c006d1ad33e9d9f1f078cdfc86d3a9bb0e6c
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.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #ifndef COORDCACHE_H
12 #define COORDCACHE_H
14 class InsetBase;
15 class LyXText;
16 class MathArray;
17 class Paragraph;
19 #include "support/types.h"
21 #include <boost/assert.hpp>
23 #include <map>
25 void lyxbreaker(void const * data, const char * hint, int size);
27 class Point {
28 public:
29 Point()
30 : x_(0), y_(0)
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);
41 int x_, y_;
45 template <class T> class CoordCacheBase {
46 public:
47 void clear()
49 data_.clear();
52 void add(T const * thing, int x, int y)
54 data_[thing] = Point(x, y);
57 int x(T const * thing) const
59 check(thing, "x");
60 return data_.find(thing)->second.x_;
63 int y(T const * thing) const
65 check(thing, "y");
66 return data_.find(thing)->second.y_;
69 Point xy(T const * thing) const
71 check(thing, "xy");
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
81 // {
82 // T *
83 // cache_type iter
84 // }
86 private:
87 friend class CoordCache;
89 void check(T const * thing, char const * hint) const
91 if (!has(thing))
92 lyxbreaker(thing, hint, data_.size());
95 typedef std::map<T const *, Point> cache_type;
96 cache_type data_;
99 /**
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.
109 class CoordCache {
110 public:
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
115 void doneUpdating();
117 void clear();
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_; }
134 private:
135 CoordCacheBase<MathArray> arrays_;
137 // all insets
138 CoordCacheBase<InsetBase> insets_;
140 // paragraph grouped by owning text
141 ParPosCache pars_;
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.
147 bool updating;
150 extern CoordCache theCoords;
152 #endif