initial commit

This commit is contained in:
Marius Nita 2002-11-10 13:33:34 +00:00
parent e828f1404f
commit 18378aaba1
2 changed files with 275 additions and 0 deletions

182
otk/widget.cc Normal file
View file

@ -0,0 +1,182 @@
#include "widget.hh"
#include "display.hh"
#include "assassin.hh"
#include "screeninfo.hh"
namespace otk {
OtkWidget::OtkWidget(OtkWidget *parent)
: _parent(parent), _visible(false), _focused(false), _grabbed_mouse(false),
_grabbed_keyboard(false), _stretchable_vert(false),
_stretchable_horz(false), _texture(NULL), _screen(parent->getScreen()),
_cursor(parent->getCursor())
{
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)
{
create();
}
OtkWidget::~OtkWidget()
{
if (_visible)
hide();
std::for_each(_children.begin(), _children.end(), PointerAssassin());
if (_parent)
_parent->removeChild(this);
XDestroyWindow(otk::OBDisplay::display, _window);
}
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);
XSetWindowAttributes attrib_create;
unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
attrib_create.background_pixmap = None;
attrib_create.colormap = scr_info->getColormap();
attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | ExposureMask;
if (_cursor) {
create_mask |= CWCursor;
attrib_create.cursor = _cursor;
}
_window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
_rect.y(), _rect.width(), _rect.height(), 0,
scr_info->getDepth(), InputOutput,
scr_info->getVisual(), create_mask, &attrib_create);
}
void OtkWidget::move(const Point &to)
{
move(to.x(), to.y());
}
void OtkWidget::move(int x, int y)
{
_rect.setPos(x, y);
XMoveWindow(otk::OBDisplay::display, _window, x, y);
}
void OtkWidget::resize(const Point &to)
{
resize(to.x(), to.y());
}
void OtkWidget::resize(int x, int y)
{
assert(x >= _rect.x() && y >= _rect.y());
_rect.setWidth(x - _rect.x());
_rect.setHeight(y - _rect.y());
}
void OtkWidget::setGeometry(const Rect &new_geom)
{
setGeometry(new_geom.x(), new_geom.y(), new_geom.height(), new_geom.width());
}
void OtkWidget::setGeometry(const Point &topleft, int width, int height)
{
setGeometry(topleft.x(), topleft.y(), width, height);
}
void OtkWidget::setGeometry(int x, int y, int width, int height)
{
_rect = Rect(x, y, width, height);
XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
}
void OtkWidget::show(void)
{
if (_visible)
return;
OtkWidgetList::iterator it = _children.begin(), end = _children.end();
for (; it != end; ++it)
(*it)->show();
XMapWindow(otk::OBDisplay::display, _window);
_visible = true;
}
void OtkWidget::hide(void)
{
if (! _visible)
return;
OtkWidgetList::iterator it = _children.begin(), end = _children.end();
for (; it != end; ++it)
(*it)->hide();
XUnmapWindow(otk::OBDisplay::display, _window);
_visible = false;
}
void OtkWidget::focus(void)
{
if (! _visible)
return;
XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
CurrentTime);
}
void OtkWidget::blur(void)
{
// ?
}
bool OtkWidget::grabMouse(void)
{
return true;
}
void OtkWidget::ungrabMouse(void)
{
}
bool OtkWidget::grabKeyboard(void)
{
return true;
}
void OtkWidget::ungrabKeyboard(void)
{
}
void OtkWidget::setTexture(BTexture *texture)
{
texture = texture;
}
void OtkWidget::addChild(OtkWidget *child)
{
child = child;
}
void OtkWidget::removeChild(OtkWidget *child)
{
child = child;
}
}

93
otk/widget.hh Normal file
View file

@ -0,0 +1,93 @@
#include <string>
#include <list>
#include "rect.hh"
#include "point.hh"
#include "texture.hh"
namespace otk {
class OtkWidget {
public:
typedef std::list<OtkWidget *> OtkWidgetList;
OtkWidget(OtkWidget *parent);
OtkWidget(unsigned int screen, Cursor);
virtual ~OtkWidget();
inline Window getWindow(void) const { return _window; }
inline const OtkWidget *getParent(void) const { return _parent; }
inline const OtkWidgetList &getChildren(void) const { return _children; }
inline unsigned int getScreen(void) const { return _screen; }
inline const Rect &getRect(void) const { return _rect; }
void move(const Point &to);
void move(int x, int y);
virtual void resize(const Point &to);
virtual void resize(int x, int y);
virtual void setGeometry(const Rect &new_geom);
virtual void setGeometry(const Point &topleft, int width, int height);
virtual void setGeometry(int x, int y, int width, int height);
inline bool isVisible(void) const { return _visible; };
virtual void show(void);
virtual void hide(void);
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);
void ungrabMouse(void);
inline bool hasGrabbedKeyboard(void) const { return _grabbed_keyboard; }
bool grabKeyboard(void);
void ungrabKeyboard(void);
inline const BTexture *getTexture(void) const { return _texture; }
virtual void setTexture(BTexture *texture);
virtual void addChild(OtkWidget *child);
virtual void removeChild(OtkWidget *child);
inline bool getStretchableHorz(void) const { return _stretchable_horz; }
void setStretchableHorz(bool s_horz) { _stretchable_horz = s_horz; }
inline bool getStretchableVert(void) const { return _stretchable_vert; }
void setStretchableVert(bool s_vert) { _stretchable_vert = s_vert; }
inline Cursor getCursor(void) const { return _cursor; }
private:
void create(void);
Window _window;
OtkWidget *_parent;
OtkWidgetList _children;
bool _visible;
bool _focused;
bool _grabbed_mouse;
bool _grabbed_keyboard;
bool _stretchable_vert;
bool _stretchable_horz;
BTexture *_texture;
Rect _rect;
unsigned int _screen;
Cursor _cursor;
};
}