can now map windows and render textures

This commit is contained in:
Marius Nita 2002-11-11 03:46:25 +00:00
parent 6852efaa9e
commit f0e2abf573
2 changed files with 102 additions and 34 deletions

View file

@ -5,22 +5,27 @@
namespace otk {
OtkWidget::OtkWidget(OtkWidget *parent)
: _parent(parent), _visible(false), _focused(false), _grabbed_mouse(false),
OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
: _parent(parent), _style(parent->getStyle()), _direction(direction),
_cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
_visible(false), _focused(false), _grabbed_mouse(false),
_grabbed_keyboard(false), _stretchable_vert(false),
_stretchable_horz(false), _texture(NULL), _screen(parent->getScreen()),
_cursor(parent->getCursor())
_stretchable_horz(false), _texture(0), _bg_pixmap(0),
_screen(parent->getScreen())
{
parent->addChild(this);
create();
}
OtkWidget::OtkWidget(unsigned int screen, Cursor cursor = 0)
: _parent(NULL), _visible(false), _focused(false), _grabbed_mouse(false),
_grabbed_keyboard(false), _stretchable_vert(false),
_stretchable_horz(false), _texture(NULL), _screen(screen),
_cursor(cursor)
OtkWidget::OtkWidget(Style *style, Direction direction,
Cursor cursor, int bevel_width)
: _parent(0), _style(style), _direction(direction), _cursor(cursor),
_bevel_width(bevel_width), _visible(false),
_focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
_stretchable_vert(false), _stretchable_horz(false), _texture(0),
_bg_pixmap(0), _screen(style->getScreen())
{
assert(style);
create();
}
@ -42,8 +47,7 @@ void OtkWidget::create(void)
const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
_rect.setRect(10, 10, 20, 20);
_rect.setRect(0, 0, 1, 1); // just some initial values
XSetWindowAttributes attrib_create;
unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
@ -51,7 +55,7 @@ void OtkWidget::create(void)
attrib_create.background_pixmap = None;
attrib_create.colormap = scr_info->getColormap();
attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | ExposureMask;
ButtonMotionMask | ExposureMask | StructureNotifyMask;
if (_cursor) {
create_mask |= CWCursor;
@ -84,13 +88,12 @@ void OtkWidget::resize(int x, int y)
{
assert(x >= _rect.x() && y >= _rect.y());
_rect.setWidth(x - _rect.x());
_rect.setHeight(y - _rect.y());
setGeometry(_rect.x(), _rect.y(), x - _rect.x(), y - _rect.y());
}
void OtkWidget::setGeometry(const Rect &new_geom)
{
setGeometry(new_geom.x(), new_geom.y(), new_geom.height(), new_geom.width());
setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
}
void OtkWidget::setGeometry(const Point &topleft, int width, int height)
@ -101,7 +104,12 @@ void OtkWidget::setGeometry(const Point &topleft, int width, int height)
void OtkWidget::setGeometry(int x, int y, int width, int height)
{
_rect = Rect(x, y, width, height);
fprintf(stderr, "Resizing to x: %d, y: %d, width: %d, height: %d\n",
x, y, width, height);
XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
setTexture();
}
void OtkWidget::show(void)
@ -110,8 +118,13 @@ void OtkWidget::show(void)
return;
OtkWidgetList::iterator it = _children.begin(), end = _children.end();
for (; it != end; ++it)
for (; it != end; ++it) {
fprintf(stderr, "showing child\n");
(*it)->show();
}
fprintf(stderr, "x: %d, y: %d, width: %d, height: %d\n",
_rect.x(), _rect.y(), _rect.width(), _rect.height());
XMapWindow(otk::OBDisplay::display, _window);
_visible = true;
@ -139,44 +152,82 @@ void OtkWidget::focus(void)
CurrentTime);
}
void OtkWidget::blur(void)
{
// ?
}
bool OtkWidget::grabMouse(void)
{
return true;
Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
(ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | EnterWindowMask |
LeaveWindowMask | PointerMotionMask),
GrabModeSync, GrabModeAsync, None, None,
CurrentTime);
_grabbed_mouse = (ret == GrabSuccess);
return _grabbed_mouse;
}
void OtkWidget::ungrabMouse(void)
{
if (! _grabbed_mouse)
return;
XUngrabPointer(otk::OBDisplay::display, CurrentTime);
_grabbed_mouse = false;
}
bool OtkWidget::grabKeyboard(void)
{
return true;
Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
GrabModeSync, GrabModeAsync, CurrentTime);
_grabbed_keyboard = (ret == GrabSuccess);
return _grabbed_keyboard;
}
void OtkWidget::ungrabKeyboard(void)
{
if (! _grabbed_keyboard)
return;
XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
_grabbed_keyboard = false;
}
void OtkWidget::setTexture(BTexture *texture)
{
texture = texture;
if (!texture && !_texture)
return;
Pixmap old = _bg_pixmap;
if (texture)
_texture = texture;
_bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
if (_bg_pixmap != old)
XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
//XSetWindowBackground(otk::OBDisplay::display, win, pix);
}
void OtkWidget::addChild(OtkWidget *child)
void OtkWidget::addChild(OtkWidget *child, bool front)
{
child = child;
assert(child);
if (front)
_children.push_front(child);
else
_children.push_back(child);
}
void OtkWidget::removeChild(OtkWidget *child)
{
child = child;
OtkWidgetList::iterator it, end = _children.end();
for (; it != end; ++it) {
if ((*it) == child)
break;
}
if (it != _children.end())
_children.erase(it);
}
}

View file

@ -4,6 +4,7 @@
#include "rect.hh"
#include "point.hh"
#include "texture.hh"
#include "style.hh"
namespace otk {
@ -11,10 +12,13 @@ class OtkWidget {
public:
enum Direction { Horizontal, Vertical };
typedef std::list<OtkWidget *> OtkWidgetList;
OtkWidget(OtkWidget *parent);
OtkWidget(unsigned int screen, Cursor);
OtkWidget(OtkWidget *parent, Direction = Horizontal);
OtkWidget(Style *style, Direction direction = Horizontal,
Cursor cursor = 0, int bevel_width = 1);
virtual ~OtkWidget();
@ -40,7 +44,6 @@ public:
inline bool isFocused(void) const { return _focused; };
virtual void focus(void);
virtual void blur(void);
inline bool hasGrabbedMouse(void) const { return _grabbed_mouse; }
bool grabMouse(void);
@ -51,9 +54,9 @@ public:
void ungrabKeyboard(void);
inline const BTexture *getTexture(void) const { return _texture; }
virtual void setTexture(BTexture *texture);
virtual void setTexture(BTexture *texture = 0);
virtual void addChild(OtkWidget *child);
virtual void addChild(OtkWidget *child, bool front = false);
virtual void removeChild(OtkWidget *child);
inline bool getStretchableHorz(void) const { return _stretchable_horz; }
@ -64,6 +67,16 @@ public:
inline Cursor getCursor(void) const { return _cursor; }
inline int getBevelWidth(void) const { return _bevel_width; }
void setBevelWidth(int bevel_width)
{ assert(bevel_width > 0); _bevel_width = bevel_width; }
inline Direction getDirection(void) const { return _direction; }
void setDirection(Direction dir) { _direction = dir; }
inline Style *getStyle(void) const { return _style; }
void setStyle(Style *style) { _style = style; }
private:
void create(void);
@ -73,6 +86,11 @@ private:
OtkWidget *_parent;
OtkWidgetList _children;
Style *_style;
Direction _direction;
Cursor _cursor;
int _bevel_width;
bool _visible;
bool _focused;
@ -83,11 +101,10 @@ private:
bool _stretchable_horz;
BTexture *_texture;
Pixmap _bg_pixmap;
Rect _rect;
unsigned int _screen;
Cursor _cursor;
};
}