]> icculus.org git repositories - mikachu/openbox.git/blob - src/GCCache.hh
add some new styles for 2.0 from miklos
[mikachu/openbox.git] / src / GCCache.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // GCCache.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef GCCACHE_HH
25 #define GCCACHE_HH
26
27 extern "C" {
28 #include <X11/Xlib.h>
29 }
30
31 #include "BaseDisplay.hh"
32 #include "Color.hh"
33
34 class BGCCacheItem;
35
36 class BGCCacheContext {
37 public:
38   void set(const BColor &_color, const XFontStruct * const _font,
39            const int _function, const int _subwindow);
40   void set(const XFontStruct * const _font);
41
42   ~BGCCacheContext(void);
43
44 private:
45   BGCCacheContext(const BaseDisplay * const _display)
46     : display(_display), gc(0), pixel(0ul), fontid(0ul),
47       function(0), subwindow(0), used(false), screen(~(0u)) {}
48
49   const BaseDisplay *display;
50   GC gc;
51   unsigned long pixel;
52   unsigned long fontid;
53   int function;
54   int subwindow;
55   bool used;
56   unsigned int screen;
57
58   BGCCacheContext(const BGCCacheContext &_nocopy);
59   BGCCacheContext &operator=(const BGCCacheContext &_nocopy);
60
61   friend class BGCCache;
62   friend class BGCCacheItem;
63 };
64
65 class BGCCacheItem {
66 public:
67   inline const GC &gc(void) const { return ctx->gc; }
68
69 private:
70   BGCCacheItem(void) : ctx(0), count(0), hits(0), fault(false) { }
71
72   BGCCacheContext *ctx;
73   unsigned int count;
74   unsigned int hits;
75   bool fault;
76
77   BGCCacheItem(const BGCCacheItem &_nocopy);
78   BGCCacheItem &operator=(const BGCCacheItem &_nocopy);
79
80   friend class BGCCache;
81 };
82
83 class BGCCache {
84 public:
85   explicit BGCCache(const BaseDisplay * const _display);
86   ~BGCCache(void);
87
88   // cleans up the cache
89   void purge(void);
90
91   BGCCacheItem *find(const BColor &_color, const XFontStruct * const _font = 0,
92                      int _function = GXcopy, int _subwindow = ClipByChildren);
93   void release(BGCCacheItem *_item);
94
95 private:
96   BGCCacheContext *nextContext(unsigned int _screen);
97   void release(BGCCacheContext *ctx);
98
99   // this is closely modelled after the Qt GC cache, but with some of the
100   // complexity stripped out
101   const BaseDisplay *display;
102
103   const unsigned int context_count;
104   const unsigned int cache_size;
105   const unsigned int cache_buckets;
106   const unsigned int cache_total_size;
107   BGCCacheContext **contexts;
108   BGCCacheItem **cache;
109 };
110
111 class BPen {
112 public:
113   inline BPen(const BColor &_color,  const XFontStruct * const _font = 0,
114               int _function = GXcopy, int _subwindow = ClipByChildren)
115     : color(_color), font(_font), function(_function), subwindow(_subwindow),
116       cache(_color.display()->gcCache()), item(0) { }
117   inline ~BPen(void) { if (item) cache->release(item); }
118
119   inline const GC &gc(void) const {
120     if (! item) item = cache->find(color, font, function, subwindow);
121     return item->gc();
122   }
123
124 private:
125   const BColor &color;
126   const XFontStruct *font;
127   int function;
128   int subwindow;
129
130   mutable BGCCache *cache;
131   mutable BGCCacheItem *item;
132 };
133
134
135 #endif // GCCACHE_HH