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