openbox/otk/rendercolor.cc

102 lines
2.1 KiB
C++
Raw Normal View History

// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2003-02-09 23:07:54 +00:00
#include "config.h"
#include "rendercolor.hh"
#include "display.hh"
#include "screeninfo.hh"
#include "rendercontrol.hh"
2003-02-09 23:07:54 +00:00
#include <cstdio>
2003-01-23 02:16:07 +00:00
namespace otk {
std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
void RenderColor::initialize()
{
_cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
}
void RenderColor::destroy()
{
delete [] _cache;
}
RenderColor::RenderColor(int screen, unsigned char red,
unsigned char green, unsigned char blue)
: _screen(screen),
_red(red),
_green(green),
_blue(blue)
2003-01-22 20:14:28 +00:00
{
create();
2003-01-22 20:14:28 +00:00
}
RenderColor::RenderColor(int screen, RGB rgb)
: _screen(screen),
_red(rgb.r),
_green(rgb.g),
_blue(rgb.b)
2003-01-22 20:14:28 +00:00
{
create();
2003-01-22 20:14:28 +00:00
}
void RenderColor::create()
{
unsigned long color = _blue | _green << 8 | _red << 16;
// try get a gc from the cache
CacheItem *item = _cache[_screen][color];
if (item) {
_gc = item->gc;
2003-01-22 23:16:49 +00:00
_pixel = item->pixel;
++item->count;
} else {
XGCValues gcv;
// allocate a color and GC from the server
const ScreenInfo *info = display->screenInfo(_screen);
XColor xcol; // convert from 0-0xff to 0-0xffff
2003-02-12 01:22:51 +00:00
xcol.red = (_red << 8) | _red;
xcol.green = (_green << 8) | _green;
xcol.blue = (_blue << 8) | _blue;
display->renderControl(_screen)->allocateColor(&xcol);
2003-01-22 22:46:16 +00:00
_pixel = xcol.pixel;
gcv.foreground = _pixel;
2003-01-20 17:07:42 +00:00
gcv.cap_style = CapProjecting;
_gc = XCreateGC(**display, info->rootWindow(),
GCForeground | GCCapStyle, &gcv);
assert(_gc);
// insert into the cache
2003-01-22 23:16:49 +00:00
item = new CacheItem(_gc, _pixel);
_cache[_screen][color] = item;
++item->count;
}
}
RenderColor::~RenderColor()
{
unsigned long color = _blue | _green << 8 | _red << 16;
CacheItem *item = _cache[_screen][color];
assert(item); // better be...
if (--item->count <= 0) {
// remove from the cache
XFreeGC(**display, _gc);
_cache[_screen][color] = 0;
delete item;
const ScreenInfo *info = display->screenInfo(_screen);
XFreeColors(**display, info->colormap(), &_pixel, 1, 0);
}
}
}