]> icculus.org git repositories - dana/openbox.git/blob - otk/gccache.hh
adding toolkit beginnings
[dana/openbox.git] / otk / gccache.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef GCCACHE_HH
3 #define GCCACHE_HH
4
5 extern "C" {
6 #include <X11/Xlib.h>
7 }
8
9 #include "basedisplay.hh"
10 #include "color.hh"
11
12 class BGCCacheItem;
13
14 class BGCCacheContext {
15 public:
16   void set(const BColor &_color, const XFontStruct * const _font,
17            const int _function, const int _subwindow, const int _linewidth);
18   void set(const XFontStruct * const _font);
19
20   ~BGCCacheContext(void);
21
22 private:
23   BGCCacheContext(const BaseDisplay * const _display)
24     : display(_display), gc(0), pixel(0ul), fontid(0ul),
25       function(0), subwindow(0), used(false), screen(~(0u)), linewidth(0) {}
26
27   const BaseDisplay *display;
28   GC gc;
29   unsigned long pixel;
30   unsigned long fontid;
31   int function;
32   int subwindow;
33   bool used;
34   unsigned int screen;
35   int linewidth;
36
37   BGCCacheContext(const BGCCacheContext &_nocopy);
38   BGCCacheContext &operator=(const BGCCacheContext &_nocopy);
39
40   friend class BGCCache;
41   friend class BGCCacheItem;
42 };
43
44 class BGCCacheItem {
45 public:
46   inline const GC &gc(void) const { return ctx->gc; }
47
48 private:
49   BGCCacheItem(void) : ctx(0), count(0), hits(0), fault(false) { }
50
51   BGCCacheContext *ctx;
52   unsigned int count;
53   unsigned int hits;
54   bool fault;
55
56   BGCCacheItem(const BGCCacheItem &_nocopy);
57   BGCCacheItem &operator=(const BGCCacheItem &_nocopy);
58
59   friend class BGCCache;
60 };
61
62 class BGCCache {
63 public:
64   BGCCache(const BaseDisplay * const _display, unsigned int screen_count);
65   ~BGCCache(void);
66
67   // cleans up the cache
68   void purge(void);
69
70   BGCCacheItem *find(const BColor &_color, const XFontStruct * const _font = 0,
71                      int _function = GXcopy, int _subwindow = ClipByChildren,
72                      int _linewidth = 0);
73   void release(BGCCacheItem *_item);
74
75 private:
76   BGCCacheContext *nextContext(unsigned int _screen);
77   void release(BGCCacheContext *ctx);
78
79   // this is closely modelled after the Qt GC cache, but with some of the
80   // complexity stripped out
81   const BaseDisplay *display;
82
83   const unsigned int context_count;
84   const unsigned int cache_size;
85   const unsigned int cache_buckets;
86   const unsigned int cache_total_size;
87   BGCCacheContext **contexts;
88   BGCCacheItem **cache;
89 };
90
91 class BPen {
92 public:
93   inline BPen(const BColor &_color,  const XFontStruct * const _font = 0,
94               int _linewidth = 0, int _function = GXcopy,
95               int _subwindow = ClipByChildren)
96     : color(_color), font(_font), linewidth(_linewidth), function(_function),
97       subwindow(_subwindow), cache(_color.display()->gcCache()), item(0) { }
98
99   inline ~BPen(void) { if (item) cache->release(item); }
100
101   inline const GC &gc(void) const {
102     if (! item) item = cache->find(color, font, function, subwindow,
103                                    linewidth);
104     return item->gc();
105   }
106
107 private:
108   const BColor &color;
109   const XFontStruct *font;
110   int linewidth;
111   int function;
112   int subwindow;
113
114   mutable BGCCache *cache;
115   mutable BGCCacheItem *item;
116 };
117
118
119 #endif // GCCACHE_HH