]> icculus.org git repositories - mikachu/openbox.git/blob - otk/surface.cc
add rendercolor class, with a cache of gcs for the colors
[mikachu/openbox.git] / otk / surface.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 "surface.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10 #include "rendercolor.hh"
11
12 extern "C" {
13 #include <X11/Xutil.h>
14 }
15
16 namespace otk {
17
18 Surface::Surface(int screen, const Point &size)
19   : _screen(screen),
20     _size(size),
21     _pixmap(None),
22     _xftdraw(0)
23 {
24 }
25
26 Surface::~Surface()
27 {
28   destroyObjects();
29 }
30
31 void Surface::setPixmap(const RenderColor &color)
32 {
33   if (_pixmap == None)
34     createObjects();
35
36   XFillRectangle(**display, _pixmap, color.gc(), 0, 0,
37                  _size.x(), _size.y());
38 }
39
40 void Surface::setPixmap(XImage *image)
41 {
42   printf("SET PIXMAP\n");
43   assert(image->width == _size.x());
44   assert(image->height == _size.y());
45   
46   if (_pixmap == None)
47     createObjects();
48
49   XPutImage(**display, _pixmap, DefaultGC(**display, _screen),
50             image, 0, 0, 0, 0, _size.x(), _size.y());
51 }
52
53 void Surface::createObjects()
54 {
55   assert(_pixmap == None); assert(!_xftdraw);
56
57   const ScreenInfo *info = display->screenInfo(_screen);
58   
59   _pixmap = XCreatePixmap(**display, info->rootWindow(),
60                           _size.x(), _size.y(), info->depth());
61     
62   _xftdraw = XftDrawCreate(**display, _pixmap,
63                            info->visual(), info->colormap());
64 }
65
66 void Surface::destroyObjects()
67 {
68   if (_xftdraw) {
69     XftDrawDestroy(_xftdraw);
70     _xftdraw = 0;
71   }
72
73   if (_pixmap != None) {
74     XFreePixmap(**display, _pixmap);
75     _pixmap = None;
76   }
77 }
78
79 }