]> icculus.org git repositories - mikachu/openbox.git/blob - otk/rendercolor.cc
part of a hardcoded style done
[mikachu/openbox.git] / otk / rendercolor.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "rendercolor.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 namespace otk {
12
13 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
14
15 void RenderColor::initialize()
16 {
17   _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
18 }
19
20 void RenderColor::destroy()
21 {
22   delete [] _cache;
23 }
24   
25 RenderColor::RenderColor(int screen, unsigned char red,
26                          unsigned char green, unsigned char blue)
27   : _screen(screen),
28     _red(red),
29     _green(green),
30     _blue(blue),
31     _gc(0)
32 {
33   create();
34 }
35
36 RenderColor::RenderColor(int screen, unsigned char red,
37                          unsigned char green, unsigned char blue)
38   : _screen(screen),
39     _red(red),
40     _green(green),
41     _blue(blue),
42     _gc(0)
43 {
44   create();
45 }
46
47 RenderColor::RenderColor(int screen, RGB rgb)
48   : _screen(screen),
49     _red(rgb.r),
50     _green(rgb.g),
51     _blue(rgb.b),
52     _gc(0)
53 {
54   create();
55 }
56
57 void RenderColor::create()
58 {
59   unsigned long color = _blue | _green << 8 | _red << 16;
60   
61   // try get a gc from the cache
62   CacheItem *item = _cache[_screen][color];
63
64   if (item) {
65     _gc = item->gc;
66     ++item->count;
67   } else {
68     XGCValues gcv;
69
70     // allocate a color and GC from the server
71     const ScreenInfo *info = display->screenInfo(_screen);
72
73     XColor xcol;    // convert from 0-0xff to 0-0xffff
74     xcol.red = _red; xcol.red |= xcol.red << 8;
75     xcol.green = _green; xcol.green |= xcol.green << 8;
76     xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
77     xcol.pixel = 0;
78
79     if (! XAllocColor(**display, info->colormap(), &xcol)) {
80       fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
81               _red, _green, _blue);
82       xcol.pixel = 0;
83     }
84
85     gcv.foreground = xcol.pixel;
86     gcv.cap_style = CapProjecting;
87     _gc = XCreateGC(**display, info->rootWindow(),
88                     GCForeground | GCCapStyle, &gcv);
89     assert(_gc);
90
91     // insert into the cache
92     _cache[_screen][color] = new CacheItem(_gc);
93   }
94 }
95
96 RenderColor::~RenderColor()
97 {
98   unsigned long color = _blue | _green << 8 | _red << 16;
99   
100   CacheItem *item = _cache[_screen][color];
101   assert(item); // it better be in the cache ...
102
103   if (--item->count <= 0) {
104     // remove from the cache
105     XFreeGC(**display, _gc);
106     _cache[_screen][color] = 0;
107     delete item;
108   }
109 }
110
111 }