]> git.lyx.org Git - lyx.git/blob - src/coordcache.h
make all cached coordinates mean the same (0,0) == upper left screen
[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
13 // All positions cached in this cache are only valid between subsequent
14 // updated. (x,y) == (0,0) is the upper left screen corner, x increases
15 // to the right, y increases downwords.
16
17 void lyxbreaker(void const * data, const char * hint, int size);
18
19 struct Point {
20         Point()
21                 : x_(0), y_(0)
22         {}      
23
24         Point(int x, int y) : x_(x), y_(y)
25         {
26                 BOOST_ASSERT(x > -3000);
27                 BOOST_ASSERT(x <  4000);
28                 BOOST_ASSERT(y > -3000);
29                 BOOST_ASSERT(y <  4000);
30         }       
31
32         int x_, y_;
33 };
34
35
36 template <class T> class CoordCacheBase {
37 public:
38         void clear()
39         {
40                 data_.clear();
41         }
42
43         void add(T const * thing, int x, int y)
44         {
45                 data_[thing] = Point(x, y);
46         }
47
48         int x(T const * thing) const
49         {
50                 check(thing, "x");
51                 return data_.find(thing)->second.x_;
52         }
53
54         int y(T const * thing) const
55         {
56                 check(thing, "y");
57                 return data_.find(thing)->second.y_;
58         }
59
60         Point xy(T const * thing) const
61         {
62                 check(thing, "xy");
63                 return data_.find(thing)->second;
64         }
65
66         bool has(T const * thing) const
67         {
68                 return data_.find(thing) != data_.end();
69         }
70
71 //      T * find(int x, int y) const
72 //      {
73 //              T * 
74 //              cache_type iter
75 //      } 
76
77 private:
78         friend class CoordCache;
79
80         void check(T const * thing, char const * hint) const
81         {
82                 if (!has(thing)) {
83                         lyxbreaker(thing, hint, data_.size());
84                         BOOST_ASSERT(false);
85                 }
86         }
87
88         typedef std::map<T const *, Point> cache_type;
89         cache_type data_;
90 };
91
92
93 class CoordCache {
94 public:
95         void clear();
96
97         CoordCacheBase<MathArray> arrays_;
98         CoordCacheBase<InsetBase> insets_;
99 };
100
101 extern CoordCache theCoords;
102
103 #endif