#include "display.hh"
#include "screeninfo.hh"
#include "gccache.hh"
+#include "rendercontrol.hh"
#include "util.hh"
extern "C" {
_scroll_lock_mask(0),
_grab_count(0),
_screenInfoList(),
+ _renderControlList(),
_gccache((GCCache*) 0)
{
int junk;
// Get information on all the screens which are available.
_screenInfoList.reserve(ScreenCount(_display));
for (int i = 0; i < ScreenCount(_display); ++i)
- _screenInfoList.push_back(ScreenInfo(i));
+ _screenInfoList.push_back(i);
+
+ _renderControlList.reserve(ScreenCount(_display));
+ for (int i = 0; i < ScreenCount(_display); ++i)
+ _renderControlList.push_back(RenderControl::getRenderControl(i));
_gccache = new GCCache(_screenInfoList.size());
}
const ScreenInfo* Display::findScreen(Window root)
{
- ScreenInfoList::iterator it, end = _screenInfoList.end();
+ std::vector<ScreenInfo>::iterator it, end = _screenInfoList.end();
for (it = _screenInfoList.begin(); it != end; ++it)
if (it->rootWindow() == root)
return &(*it);
}
+const RenderControl *Display::renderControl(int snum)
+{
+ assert(snum >= 0);
+ assert(snum < (signed) _renderControlList.size());
+ return _renderControlList[snum];
+}
+
+
void Display::grab()
{
if (_grab_count == 0)
class ScreenInfo;
class GCCache;
+class RenderControl;
class Display;
//! Manages a single X11 display.
class Display
{
-public:
- //! A List of ScreenInfo instances
- typedef std::vector<ScreenInfo> ScreenInfoList;
-
private:
//! The X display
::Display *_display;
int _grab_count;
//! A list of information for all screens on the display
- ScreenInfoList _screenInfoList;
+ std::vector<ScreenInfo> _screenInfoList;
+
+ //! A list of RenderControl objects, which are used for all graphics on a
+ //! screen
+ std::vector<RenderControl*> _renderControlList;
//! A cache for re-using GCs, used by the drawing objects
/*!
//! Find a ScreenInfo based on a root window
const ScreenInfo* findScreen(Window root);
+ //! Gets the RenderControl for a screen
+ const RenderControl *renderControl(int snum);
+
//! Returns if the display has the xkb extension available
inline bool xkb() const { return _xkb; }
//! Returns the xkb extension's event base
FocusLabel::FocusLabel(Widget *parent)
: FocusWidget(parent), _text("")
{
- const ScreenInfo *info = display->screenInfo(screen());
- _xftdraw = XftDrawCreate(**display, window(), info->visual(),
- info->colormap());
}
FocusLabel::~FocusLabel()
{
- XftDrawDestroy(_xftdraw);
}
FocusWidget::update();
- ft->drawString(_xftdraw, x, 0, *text_color, t);
+ display->renderControl(_screen)->
+ drawString(this, *ft, x, 0, *text_color, t);
} else
FocusWidget::update();
}
#define __label_hh
#include "focuswidget.hh"
-#include "font.hh"
namespace otk {
virtual void setStyle(Style *style);
private:
- //! Object used by Xft to render to the drawable
- XftDraw *_xftdraw;
//! Text displayed in the label
ustring _text;
};
#include <algorithm>
#include "font.hh"
+#include "surface.hh"
#include "util.hh"
#include "display.hh"
-#include "color.hh"
#include "screeninfo.hh"
extern "C" {
}
-void Font::drawString(XftDraw *d, int x, int y, const Color &color,
- const ustring &string) const
-{
- assert(d);
-
- if (_shadow) {
- XftColor c;
- c.color.red = 0;
- c.color.green = 0;
- c.color.blue = 0;
- c.color.alpha = _tint | _tint << 8; // transparent shadow
- c.pixel = BlackPixel(**display, _screen_num);
-
- if (string.utf8())
- XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
- _xftfont->ascent + y + _offset,
- (FcChar8*)string.c_str(), string.bytes());
- else
- XftDrawString8(d, &c, _xftfont, x + _offset,
- _xftfont->ascent + y + _offset,
- (FcChar8*)string.c_str(), string.bytes());
- }
-
- XftColor c;
- c.color.red = color.red() | color.red() << 8;
- c.color.green = color.green() | color.green() << 8;
- c.color.blue = color.blue() | color.blue() << 8;
- c.pixel = color.pixel();
- c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
-
- if (string.utf8())
- XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
- (FcChar8*)string.c_str(), string.bytes());
- else
- XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y,
- (FcChar8*)string.c_str(), string.bytes());
-
- return;
-}
-
-
unsigned int Font::measureString(const ustring &string) const
{
XGlyphInfo info;
#define __font_hh
#include "ustring.hh"
+#include "truerendercontrol.hh"
extern "C" {
#include <X11/Xlib.h>
namespace otk {
class Color;
+class Surface;
class Font {
/*
unsigned int measureString(const ustring &string) const;
- //! Draws a string into an XftDraw object
- /*!
- Be Warned: If you use an XftDraw object and a color, or a font from
- different screens, you WILL have unpredictable results! :)
- */
- void drawString(XftDraw *d, int x, int y, const Color &color,
- const ustring &string) const;
+ // The RenderControl classes use the internal data to render the fonts, but
+ // noone else needs it, so its private.
+ friend class RenderControl;
};
}
Label::Label(Widget *parent)
: Widget(parent), _text("")
{
- const ScreenInfo *info = display->screenInfo(screen());
- _xftdraw = XftDrawCreate(**display, window(), info->visual(),
- info->colormap());
}
Label::~Label()
{
- XftDrawDestroy(_xftdraw);
}
void Label::setStyle(Style *style)
Widget::update();
- ft->drawString(_xftdraw, x, 0, *style()->getTextUnfocus(), t);
+ display->renderControl(_screen)->
+ drawString(this, *ft, x, 0, *style()->getTextUnfocus(), t);
} else
Widget::update();
}
#define __label_hh
#include "widget.hh"
-#include "font.hh"
namespace otk {
virtual void setStyle(Style *style);
private:
- //! Object used by Xft to render to the drawable
- XftDraw *_xftdraw;
//! Text displayed in the label
ustring _text;
};
#include "rendertexture.hh"
#include "display.hh"
#include "screeninfo.hh"
+#include "surface.hh"
+#include "color.hh"
+#include "font.hh"
+#include "ustring.hh"
extern "C" {
#ifdef HAVE_STDLIB_H
RenderControl *RenderControl::getRenderControl(int screen)
{
- const ScreenInfo *info = display->screenInfo(screen);
-
// get the visual on the screen and return the correct type of RenderControl
- int vclass = info->visual()->c_class;
+ int vclass = display->screenInfo(screen)->visual()->c_class;
switch (vclass) {
case TrueColor:
- return new TrueRenderControl(info);
+ return new TrueRenderControl(screen);
case PseudoColor:
case StaticColor:
-// return new PseudoRenderControl(info);
+// return new PseudoRenderControl(screen);
case GrayScale:
case StaticGray:
-// return new GrayRenderControl(info);
+// return new GrayRenderControl(screen);
default:
printf(_("RenderControl: Unsupported visual %d specified. Aborting.\n"),
vclass);
}
}
-RenderControl::RenderControl(const ScreenInfo *screen)
+RenderControl::RenderControl(int screen)
: _screen(screen)
{
printf("Initializing RenderControl\n");
}
+void RenderControl::drawString(Surface *sf, const Font &font, int x, int y,
+ const Color &color, const ustring &string) const
+{
+ assert(sf);
+ assert(sf->_screen == _screen);
+ XftDraw *d = sf->_xftdraw;
+ assert(d);
+
+ if (font._shadow) {
+ XftColor c;
+ c.color.red = 0;
+ c.color.green = 0;
+ c.color.blue = 0;
+ c.color.alpha = font._tint | font._tint << 8; // transparent shadow
+ c.pixel = BlackPixel(**display, _screen);
+
+ if (string.utf8())
+ XftDrawStringUtf8(d, &c, font._xftfont, x + font._offset,
+ font._xftfont->ascent + y + font._offset,
+ (FcChar8*)string.c_str(), string.bytes());
+ else
+ XftDrawString8(d, &c, font._xftfont, x + font._offset,
+ font._xftfont->ascent + y + font._offset,
+ (FcChar8*)string.c_str(), string.bytes());
+ }
+
+ XftColor c;
+ c.color.red = color.red() | color.red() << 8;
+ c.color.green = color.green() | color.green() << 8;
+ c.color.blue = color.blue() | color.blue() << 8;
+ c.pixel = color.pixel();
+ c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
+
+ if (string.utf8())
+ XftDrawStringUtf8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
+ (FcChar8*)string.c_str(), string.bytes());
+ else
+ XftDrawString8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
+ (FcChar8*)string.c_str(), string.bytes());
+
+ return;
+}
+
}
class ScreenInfo;
class Surface;
class RenderTexture;
+class Font;
+class Color;
+class ustring;
class RenderControl {
protected:
- const ScreenInfo *_screen;
+ int _screen;
/*
// color tables, meaning, 256 (possibly) different shades of each color,
int _ncolors;
*/
- RenderControl(const ScreenInfo *screen);
+ RenderControl(int screen);
public:
virtual ~RenderControl();
static RenderControl *getRenderControl(int screen);
+ //! Draws a string onto a Surface
+ virtual void drawString(Surface *sf, const Font &font, int x, int y,
+ const Color &color, const ustring &string) const;
+
+ //! Draws a background onto a Surface, as specified by a RenderTexture
virtual void drawBackground(Surface *sf,
const RenderTexture &texture) const = 0;
};
#include "surface.hh"
#include "display.hh"
+#include "screeninfo.hh"
+
+extern "C" {
+#include <X11/Xutil.h>
+}
namespace otk {
-Surface::Surface()
- : _size(1, 1),
- _pm(None)
+Surface::Surface(int screen)
+ : _screen(screen),
+ _size(1, 1),
+ _im(0),
+ _pm(None),
+ _xftdraw(0)
{
+ createObjects();
}
-Surface::Surface(const Point &size)
- : _size(size),
- _pm(None)
+Surface::Surface(int screen, const Point &size)
+ : _screen(screen),
+ _size(size),
+ _im(0),
+ _pm(None),
+ _xftdraw(0)
{
+ createObjects();
}
Surface::~Surface()
{
- if (_pm != None) XFreePixmap(**display, _pm);
+ destroyObjects();
+}
+
+void Surface::createObjects()
+{
+ assert(!_im); assert(_pm == None); assert(!_xftdraw);
+
+ const ScreenInfo *info = display->screenInfo(_screen);
+
+ _im = XCreateImage(**display, info->visual(), info->depth(),
+ ZPixmap, 0, NULL, _size.x(), _size.y(), 32, 0);
+
+ _pm = XCreatePixmap(**display, info->rootWindow(), _size.x(), _size.y(),
+ info->depth());
+
+ _xftdraw = XftDrawCreate(**display, _pm, info->visual(), info->colormap());
+}
+
+void Surface::destroyObjects()
+{
+ assert(_im); assert(_pm != None); assert(_xftdraw);
+
+ // do the delete ourselves cuz we alloc it with new not malloc
+ delete [] _im->data;
+ _im->data = NULL;
+ XDestroyImage(_im);
+ _im = 0;
+
+ XFreePixmap(**display, _pm);
+ _pm = None;
+
+ XftDrawDestroy(_xftdraw);
+ _xftdraw = 0;
}
void Surface::setSize(int w, int h)
{
+ if (w == _size.x() && h == _size.y()) return; // no change
+
_size.setPoint(w, h);
+ destroyObjects();
+ createObjects();
}
}
extern "C" {
#include <X11/Xlib.h>
+#define _XFT_NO_COMPAT_ // no Xft 1 API
+#include <X11/Xft/Xft.h>
}
namespace otk {
+class ScreenInfo;
+
class Surface {
+ int _screen;
Point _size;
+ XImage *_im;
Pixmap _pm;
+ XftDraw *_xftdraw;
+ void createObjects();
+ void destroyObjects();
+
protected:
- Surface();
- Surface(const Point &size);
+ Surface(int screen);
+ Surface(int screen, const Point &size);
virtual void setSize(int w, int h);
public:
virtual ~Surface();
+ inline int screen(void) const { return _screen; }
virtual const Point& size() const { return _size; }
virtual int width() const { return _size.x(); }
virtual int height() const { return _size.y(); }
virtual Pixmap pixmap() const { return _pm; } // TEMP
+ // The RenderControl classes use the internal objects in this class to render
+ // to it. Noone else needs them tho, so they are private.
+ friend class RenderControl;
friend class TrueRenderControl;
};
namespace otk {
-TrueRenderControl::TrueRenderControl(const ScreenInfo *screen)
+TrueRenderControl::TrueRenderControl(int screen)
: RenderControl(screen),
_red_offset(0),
_green_offset(0),
{
printf("Initializing TrueColor RenderControl\n");
+ Visual *visual = display->screenInfo(_screen)->visual();
unsigned long red_mask, green_mask, blue_mask;
// find the offsets for each color in the visual's masks
- red_mask = screen->visual()->red_mask;
- green_mask = screen->visual()->green_mask;
- blue_mask = screen->visual()->blue_mask;
+ red_mask = visual->red_mask;
+ green_mask = visual->green_mask;
+ blue_mask = visual->blue_mask;
while (! (red_mask & 1)) { _red_offset++; red_mask >>= 1; }
while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
}
+
static inline void renderPixel(XImage *im, unsigned char *dp,
unsigned long pixel)
{
assert(sf);
int w = sf->width(), h = sf->height();
-
- XImage *im = XCreateImage(**display, _screen->visual(), _screen->depth(),
- ZPixmap, 0, NULL, w, h, 32, 0);
+ XImage *im = sf->_im;
+ Pixmap pm = sf->_pm;
+ assert(im); assert(pm != None);
unsigned char *data = new unsigned char[im->bytes_per_line * h];
unsigned char *dp = data;
+ unsigned int bytes_per_pixel = im->bits_per_pixel/8;
for (int y = 0; y < h/3; ++y)
- for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
- renderPixel(im, dp, (255*x/w) << _red_offset << _red_shift);
+ for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
+ renderPixel(im, dp, (255*x/w) >> _red_shift << _red_offset);
for (int y = 0; y < h/3; ++y)
- for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
- renderPixel(im, dp, (255*x/w) << _green_offset << _green_shift);
+ for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
+ renderPixel(im, dp, (255*x/w) >> _green_shift << _green_offset);
for (int y = 0; y < h/3; ++y)
- for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
- renderPixel(im, dp, (255*x/w) << _blue_offset << _blue_shift);
+ for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
+ renderPixel(im, dp, (255*x/w) >> _blue_shift << _blue_offset);
im->data = (char*) data;
- if (!sf->_pm)
- sf->_pm = XCreatePixmap(**display, _screen->rootWindow(), w, h,
- _screen->depth());
- XPutImage(**display, sf->_pm, DefaultGC(**display, _screen->screen()),
- im, 0, 0, 0, 0, w, h);
-
- //delete [] image->data;
- //image->data = NULL;
- XDestroyImage(im);
+ XPutImage(**display, pm, DefaultGC(**display, _screen),
+ sf->_im, 0, 0, 0, 0, w, h);
}
}
#include "rendercontrol.hh"
+#include <vector>
+
namespace otk {
class TrueRenderControl : public RenderControl {
private:
- // the offset of each color in a color mask
- int _red_offset;
- int _green_offset;
- int _blue_offset;
-
- // the number of bits to shift a color value (from 0-255) to fit it into the
- // the color mask
+ // the number of bits to shift a color value (from 0-255) to the right, to
+ // fit it into the the color mask (do this before the offset)
int _red_shift;
int _green_shift;
int _blue_shift;
+ // the offset of each color in a color mask
+ int _red_offset;
+ int _green_offset;
+ int _blue_offset;
public:
- TrueRenderControl(const ScreenInfo *screen);
+ TrueRenderControl(int screen);
virtual ~TrueRenderControl();
virtual void drawBackground(Surface *sf, const RenderTexture &texture) const;
namespace otk {
Widget::Widget(Widget *parent, Direction direction)
- : EventHandler(),
+ : Surface(parent->screen()),
+ EventHandler(),
_dirty(false), _focused(false),
_parent(parent), _style(parent->style()), _direction(direction),
_cursor(parent->cursor()), _bevel_width(parent->bevelWidth()),
Widget::Widget(EventDispatcher *event_dispatcher, Style *style,
Direction direction, Cursor cursor, int bevel_width,
bool override_redirect)
- : EventHandler(),
+ : Surface(style->getScreen()),
+ EventHandler(),
_dirty(false),_focused(false),
_parent(0), _style(style), _direction(direction), _cursor(cursor),
_bevel_width(bevel_width), _ignore_config(0), _visible(false),
inline Window window(void) const { return _window; }
inline const Widget *parent(void) const { return _parent; }
inline const WidgetList &children(void) const { return _children; }
- inline unsigned int screen(void) const { return _screen; }
inline Rect rect(void) const { return Rect(_pos, size()); }
void move(const Point &to);