diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-01-23 20:08:12 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-01-23 20:08:12 +0100 |
commit | af9c9091ed69394171485aa4c4814504f86f2004 (patch) | |
tree | 070b911dc8faed486c8e8829f0f41aec7d5b4350 /plugingui/widget.cc | |
parent | 5e4cb10f72ef69d73fbd2c1bc42465c9111a4c41 (diff) |
Simple image blit. New slider class. New filenamelineedit. New pixelbuffer used for drawing everything but the root window - with alpha blending...
Diffstat (limited to 'plugingui/widget.cc')
-rw-r--r-- | plugingui/widget.cc | 219 |
1 files changed, 98 insertions, 121 deletions
diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 04af3c2..4b09732 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -27,166 +27,143 @@ #include "widget.h" #include "globalcontext.h" -#include "widgetcontext.h" -#include <stdio.h> +#include "painter.h" + +#include "window.h" -#include <X11/Xatom.h> +#include <stdio.h> -Widget::Widget(GlobalContext *gctx, Widget *parent) +GUI::Widget::Widget(Widget *parent) + : pixbuf(1, 1) { - this->gctx = gctx; this->parent = parent; - wctx = new WidgetContext(); - - _x = _y = _width = _height = 10; - -#ifdef X11 - - // Get some colors - int blackColor = BlackPixel(gctx->display, DefaultScreen(gctx->display)); - - Window w; - if(parent == NULL) w = DefaultRootWindow(gctx->display); - else w = parent->wctx->window; - - - // Create the window - wctx->window = XCreateSimpleWindow(gctx->display, - w, _x, _y, _width, _height, 0, - blackColor, blackColor); - - gctx->widgets[wctx->window] = this; - - // We want to get MapNotify events - XSelectInput(gctx->display, wctx->window, - StructureNotifyMask | - PointerMotionMask | - ButtonPressMask | - ButtonReleaseMask | - KeyPressMask | - KeyReleaseMask| - ExposureMask | - StructureNotifyMask); - - /* - // register interest in the delete window message - wdg->wmDeleteMessage = XInternAtom(wdg->dpy, "WM_DELETE_WINDOW", false); - XSetWMProtocols(wdg->dpy, wdg->w, &wdg->wmDeleteMessage, 1); - */ - - // "Map" the window (that is, make it appear on the screen) - XMapWindow(gctx->display, wctx->window); - - // Create a "Graphics Context" - wctx->gc = XCreateGC(gctx->display, wctx->window, 0, NULL); - - // Wait for the MapNotify event - for(;;) { - XEvent e; - XNextEvent(gctx->display, &e); - if(e.type == MapNotify) break; + if(parent) { + parent->addChild(this); + _window = parent->window(); } -#endif/*X11*/ - -#ifdef WIN32 - WNDCLASSEX wcex; - WNDID wndId; - - wctx->m_hwnd = 0; - wctx->m_className = NULL; - - memset(&wcex, 0, sizeof(wcex)); - - /* - Time to register a window class. Generic flags and everything. cbWndExtra is the - size of a pointer to an object - we need this in the wndproc handler. - */ - wcex.cbSize = sizeof(WNDCLASSEX); - wcex.style = class_style; - wcex.lpfnWndProc = (WNDPROC) data; - wcex.cbWndExtra = sizeof(CWindow *); - wcex.hInstance = GetModuleHandle(NULL); - - // if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) { - // wcex.hbrBackground = NULL; - // } else { - wcex.hbrBackground = (HBRUSH) COLOR_BACKGROUND + 1; - // } - - wcex.lpszClassName = wctx->m_className = strdup(className); - - RegisterClassEx(&wcex); - - if(parent) { - style = style | WS_CHILD; - wndId = parent->getWndId(); - } else { - style = style | WS_OVERLAPPEDWINDOW; - wndId = 0; - } - - wctx->m_hwnd = CreateWindowEx(ex_style, wctx->m_className, "DGBasisWidget", style, x, y, w, h, - wndId, NULL, GetModuleHandle(NULL), NULL); -#endif/*WIN32*/ + _width = _height = 0; } -Widget::~Widget() +GUI::Widget::~Widget() { -#ifdef X11 - gctx->widgets.erase(wctx->window); -#endif/*X11*/ - delete wctx; } -void Widget::show() +void GUI::Widget::show() { } -void Widget::hide() +void GUI::Widget::hide() { } -void Widget::addChild(Widget *widget) +void GUI::Widget::addChild(GUI::Widget *widget) { children.push_back(widget); } -void Widget::setSize(size_t width, size_t height) +void GUI::Widget::resize(size_t width, size_t height) { _width = width; _height = height; -#ifdef X11 - XResizeWindow(gctx->display, wctx->window, width, height); -#endif/*X11*/ + pixbuf.realloc(width, height); } -void Widget::move(size_t x, size_t y) +void GUI::Widget::move(size_t x, size_t y) { _x = x; _y = y; -#ifdef X11 - XMoveWindow(gctx->display, wctx->window, x, y); -#endif/*X11*/ + pixbuf.x = x; + pixbuf.y = y; } -size_t Widget::x() { return _x; } -size_t Widget::y() { return _y; } -size_t Widget::width() { return _width; } -size_t Widget::height() { return _height; } +size_t GUI::Widget::x() { return _x; } +size_t GUI::Widget::y() { return _y; } +size_t GUI::Widget::width() { return _width; } +size_t GUI::Widget::height() { return _height; } + +GUI::Widget *GUI::Widget::find(size_t x, size_t y) +{ + std::vector<Widget*>::iterator i = children.begin(); + while(i != children.end()) { + Widget *w = *i; + if(w->x() <= x && (w->x() + w->width()) >= x && + w->y() <= y && w->y() + w->height() >= y) + return w->find(x - w->x(), y - w->y()); + i++; + } + + if(x > width() || x < 0 || y > height() || y < 0) return NULL; + return this; +} + +GUI::Window *GUI::Widget::window() +{ + return _window; +} + +void GUI::Widget::repaint_r(GUI::RepaintEvent *e) +{ + Painter p(this); + repaintEvent(e); + std::vector<Widget*>::iterator i = children.begin(); + while(i != children.end()) { + Widget *w = *i; + w->repaint_r(e); + i++; + } +} + +std::vector<GUI::PixelBufferAlpha *> GUI::Widget::getPixelBuffers() +{ + std::vector<PixelBufferAlpha *> pbs; + + pbs.push_back(&pixbuf); + + std::vector<Widget*>::iterator i = children.begin(); + while(i != children.end()) { + Widget *w = *i; + std::vector<PixelBufferAlpha *> pbs0 = w->getPixelBuffers(); + pbs.insert(pbs.end(), pbs0.begin(), pbs0.end()); + i++; + } + + return pbs; +} + +bool GUI::Widget::hasKeyboardFocus() +{ + return window()->keyboardFocus() == this; +} #ifdef TEST_WIDGET -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) +//deps: window.cc globalcontext.cc //cflags: -//Required link options (autoconf vars may be used) //libs: #include "test.h" +#include "window.h" + TEST_BEGIN; -// TODO: Put some testcode here (see test.h for usable macros). +GUI::Window w1(NULL); +w1.move(0,0); +w1.resize(100, 100); + +GUI::Widget w2(&w1); +w2.resize(40,40); +w2.move(10,10); + +GUI::Widget w3(&w2); +w3.resize(20,20); +w3.move(10,10); + +TEST_EQUAL_PTR(w1.find(101,0), NULL, "Miss?"); +TEST_EQUAL_PTR(w1.find(0,0), &w1, "Hit w1?"); +TEST_EQUAL_PTR(w1.find(100,100), &w1, "Hit w1?"); +TEST_EQUAL_PTR(w1.find(0,0), &w1, "Hit w1?"); +TEST_EQUAL_PTR(w1.find(11,11), &w2, "Hit w2?"); +TEST_EQUAL_PTR(w1.find(22,22), &w3, "Hit w3?"); TEST_END; |