diff options
Diffstat (limited to 'plugingui/widget.cc')
-rw-r--r-- | plugingui/widget.cc | 244 |
1 files changed, 123 insertions, 121 deletions
diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 05966aa..4b92e65 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -31,188 +31,190 @@ #include <stdio.h> -GUI::Widget::Widget(Widget *parent) - : pixbuf(1, 1) +namespace GUI { + +Widget::Widget(Widget* parent) + : parent(parent) { - _width = _height = 10; + if(parent) + { + parent->addChild(this); + _window = parent->window(); + } - this->parent = parent; - if(parent) { - parent->addChild(this); - _window = parent->window(); - } - _width = _height = 0; - _visible = true; + _width = _height = 0; + _visible = true; } -GUI::Widget::~Widget() +Widget::~Widget() { - if(parent) parent->removeChild(this); + if(parent) + { + parent->removeChild(this); + } } -void GUI::Widget::show() +void Widget::show() { - setVisible(true); + setVisible(true); } -void GUI::Widget::hide() +void Widget::hide() { - setVisible(false); + setVisible(false); } -void GUI::Widget::setVisible(bool v) +void Widget::setVisible(bool visible) { - _visible = v; - repaintEvent(NULL); + _visible = visible; + repaintEvent(nullptr); } -bool GUI::Widget::visible() +bool Widget::visible() { - return _visible; + return _visible; } -void GUI::Widget::addChild(GUI::Widget *widget) +void Widget::addChild(Widget* widget) { - children.push_back(widget); + children.push_back(widget); } -void GUI::Widget::removeChild(GUI::Widget *widget) +void Widget::removeChild(Widget* widget) { - std::vector<Widget *>::iterator i = children.begin(); - while(i != children.end()) { - if(*i == widget) { - children.erase(i); - return; - } - i++; - } + for(auto i = children.begin(); i != children.end(); ++i) + { + if(*i == widget) + { + children.erase(i); + return; + } + } } -void GUI::Widget::resize(int width, int height) +void Widget::resize(int width, int height) { - if(width < 1 || height < 1) return; - _width = width; - _height = height; - pixbuf.realloc(width, height); + if((width < 1) || (height < 1) || + (((size_t)width == _width) && ((size_t)height == _height))) + { + return; + } + + _width = width; + _height = height; + pixbuf.realloc(width, height); + + sizeChangeNotifier(width, height); } -void GUI::Widget::move(size_t x, size_t y) +void Widget::move(size_t x, size_t y) { - _x = x; - _y = y; + _x = x; + _y = y; } -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; } - -size_t GUI::Widget::windowX() +int Widget::x() { - size_t window_x = x(); - if(parent) window_x += parent->windowX(); - return window_x; + return _x; } -size_t GUI::Widget::windowY() +int Widget::y() { - size_t window_y = y(); - if(parent) window_y += parent->windowY(); - return window_y; + return _y; } -GUI::Widget *GUI::Widget::find(size_t x, size_t y) +size_t Widget::width() { - std::vector<Widget*>::reverse_iterator i = children.rbegin(); - while(i != children.rend()) { - Widget *w = *i; - if(w->visible()) { - 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; + return _width; } -GUI::Window *GUI::Widget::window() +size_t Widget::height() { - return _window; + return _height; } -void GUI::Widget::repaint_r(GUI::RepaintEvent *e) +size_t Widget::windowX() { - Painter p(this); // make sure pixbuf refcount is incremented. + size_t window_x = x(); + if(parent) + { + window_x += parent->windowX(); + } - repaintEvent(e); - - std::vector<Widget*>::iterator i = children.begin(); - while(i != children.end()) { - Widget *w = *i; - w->repaint_r(e); - i++; - } + return window_x; } -std::vector<GUI::PixelBufferAlpha *> GUI::Widget::getPixelBuffers() +size_t Widget::windowY() { - std::vector<PixelBufferAlpha *> pbs; - - pixbuf.x = windowX(); - pixbuf.y = windowY(); + size_t window_y = y(); + if(parent) + { + window_y += parent->windowY(); + } - pbs.push_back(&pixbuf); + return window_y; +} - std::vector<Widget*>::iterator i = children.begin(); - while(i != children.end()) { - Widget *w = *i; - if(w->visible()) { - std::vector<PixelBufferAlpha *> pbs0 = w->getPixelBuffers(); - pbs.insert(pbs.end(), pbs0.begin(), pbs0.end()); - } - i++; - } +Widget* Widget::find(int x, int y) +{ + for(auto i = children.rbegin(); i != children.rend(); ++i) + { + Widget* widget = *i; + if(widget->visible()) + { + if((x >= widget->x()) && (x < (widget->x() + (int)widget->width())) && + (y >= widget->y()) && (y < (widget->y() + (int)widget->height()))) + { + return widget->find(x - widget->x(), y - widget->y()); + } + } + } - return pbs; + return this; } -bool GUI::Widget::hasKeyboardFocus() +Window* Widget::window() { - return window()->keyboardFocus() == this; + return _window; } -#ifdef TEST_WIDGET -//deps: window.cc globalcontext.cc -//cflags: -//libs: -#include "test.h" +std::vector<PixelBufferAlpha*> Widget::getPixelBuffers() +{ + std::vector<PixelBufferAlpha*> pixelBuffers; -#include "window.h" + pixbuf.x = windowX(); + pixbuf.y = windowY(); -TEST_BEGIN; + pixelBuffers.push_back(&pixbuf); -GUI::Window w1(NULL); -w1.move(0,0); -w1.resize(100, 100); + for(auto child : children) + { + if(child->visible()) + { + auto childPixelBuffers = child->getPixelBuffers(); + pixelBuffers.insert(pixelBuffers.end(), + childPixelBuffers.begin(), childPixelBuffers.end()); + } + } + + return pixelBuffers; +} -GUI::Widget w2(&w1); -w2.resize(40,40); -w2.move(10,10); +bool Widget::hasKeyboardFocus() +{ + return window()->keyboardFocus() == this; +} -GUI::Widget w3(&w2); -w3.resize(20,20); -w3.move(10,10); +void Widget::repaintChildren(RepaintEvent* repaintEvent) +{ + Painter p(*this); // make sure pixbuf refcount is incremented. -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?"); + this->repaintEvent(repaintEvent); -TEST_END; + for(auto child : children) + { + child->repaintChildren(repaintEvent); + } +} -#endif/*TEST_WIDGET*/ +} // GUI:: |