]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
make all cached positions screen-absolute
[lyx.git] / src / coordcache.h
1 #ifndef COORDCACHE_H
2 #define COORDCACHE_H
3
4 #include "mathed/math_data.h"
5 #include "insets/insetbase.h"
6 #include "lyxtext.h"
7
8 #include <boost/assert.hpp>
9
10 #include <map>
11
12 void lyxbreaker(void const * data, const char * hint, int size);
13
14 struct Point {
15         Point()
16                 : x_(0), y_(0)
17         {}      
18
19         Point(int x, int y) : x_(x), y_(y)
20         {
21                 BOOST_ASSERT(x > -3000);
22                 BOOST_ASSERT(x <  4000);
23                 BOOST_ASSERT(y > -3000);
24                 BOOST_ASSERT(y <  4000);
25         }       
26
27         int x_, y_;
28 };
29
30
31 template <class T> class CoordCacheBase {
32 public:
33         void clear()
34         {
35                 data_.clear();
36         }
37
38         void add(T const * thing, int x, int y)
39         {
40                 data_[thing] = Point(x, y);
41         }
42
43         int x(T const * thing) const
44         {
45                 check(thing, "x");
46                 return data_.find(thing)->second.x_;
47         }
48
49         int y(T const * thing) const
50         {
51                 check(thing, "y");
52                 return data_.find(thing)->second.y_;
53         }
54
55         Point xy(T const * thing) const
56         {
57                 check(thing, "xy");
58                 return data_.find(thing)->second;
59         }
60
61         bool has(T const * thing) const
62         {
63                 return data_.find(thing) != data_.end();
64         }
65
66 //      T * find(int x, int y) const
67 //      {
68 //              T * 
69 //              cache_type iter
70 //      } 
71
72 private:
73         friend class CoordCache;
74
75         void check(T const * thing, char const * hint) const
76         {
77                 if (!has(thing)) {
78                         lyxbreaker(thing, hint, data_.size());
79                         BOOST_ASSERT(false);
80                 }
81         }
82
83         typedef std::map<T const *, Point> cache_type;
84         cache_type data_;
85 };
86
87
88 class CoordCache {
89 public:
90         void clear();
91
92         CoordCacheBase<MathArray> arrays_;
93         CoordCacheBase<InsetBase> insets_;
94 };
95
96 extern CoordCache theCoords;
97
98 #endif