From eec019ec3e94d5aee7f7d423a3a43f63237b6e87 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 20 Nov 2015 20:37:00 +0100 Subject: Refactored Widget. --- plugingui/widget.cc | 113 +++++++++++++++++++++++++++------------------------- plugingui/widget.h | 30 ++++++++------ plugingui/window.cc | 2 +- 3 files changed, 76 insertions(+), 69 deletions(-) (limited to 'plugingui') diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 942337f..9b766df 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -33,13 +33,9 @@ namespace GUI { -Widget::Widget(Widget *parent) - : pixbuf(1, 1) - , parent(parent) - , _window(nullptr) +Widget::Widget(Widget* parent) + : parent(parent) { - _width = _height = 10; - if(parent) { parent->addChild(this); @@ -68,9 +64,9 @@ void Widget::hide() setVisible(false); } -void Widget::setVisible(bool v) +void Widget::setVisible(bool visible) { - _visible = v; + _visible = visible; repaintEvent(nullptr); } @@ -79,28 +75,27 @@ bool Widget::visible() return _visible; } -void Widget::addChild(Widget *widget) +void Widget::addChild(Widget* widget) { children.push_back(widget); } -void Widget::removeChild(Widget *widget) +void Widget::removeChild(Widget* widget) { - std::vector::iterator i = children.begin(); - while(i != children.end()) + for(auto i = children.begin(); i != children.end(); ++i) { if(*i == widget) { children.erase(i); return; } - i++; } } void Widget::resize(int width, int height) { - if(width < 1 || height < 1) + if((width < 1) || (height < 1) || + (((size_t)width == _width) && ((size_t)height == _height))) { return; } @@ -116,10 +111,25 @@ void Widget::move(size_t x, size_t y) _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 Widget::x() +{ + return _x; +} + +size_t Widget::y() +{ + return _y; +} + +size_t Widget::width() +{ + return _width; +} + +size_t Widget::height() +{ + return _height; +} size_t Widget::windowX() { @@ -143,24 +153,22 @@ size_t Widget::windowY() return window_y; } -Widget *Widget::find(size_t x, size_t y) +Widget* Widget::find(size_t x, size_t y) { - std::vector::reverse_iterator i = children.rbegin(); - while(i != children.rend()) + for(auto i = children.rbegin(); i != children.rend(); ++i) { - Widget *w = *i; - if(w->visible()) + Widget* widget = *i; + if(widget->visible()) { - if(w->x() <= x && (w->x() + w->width()) >= x && - w->y() <= y && w->y() + w->height() >= y) + if((widget->x() <= x) && ((widget->x() + widget->width()) >= x) && + (widget->y() <= y) && ((widget->y() + widget->height()) >= y)) { - return w->find(x - w->x(), y - w->y()); + return widget->find(x - widget->x(), y - widget->y()); } } - i++; } - if(x > width() || x < 0 || y > height() || y < 0) + if((x > width()) || (y > height())) { return nullptr; } @@ -168,48 +176,31 @@ Widget *Widget::find(size_t x, size_t y) return this; } -Window *Widget::window() +Window* Widget::window() { return _window; } -void Widget::repaint_r(RepaintEvent* repaintEvent) -{ - Painter p(*this); // make sure pixbuf refcount is incremented. - - this->repaintEvent(repaintEvent); - - std::vector::iterator i = children.begin(); - while(i != children.end()) - { - Widget *w = *i; - w->repaint_r(repaintEvent); - i++; - } -} - -std::vector Widget::getPixelBuffers() +std::vector Widget::getPixelBuffers() { - std::vector pbs; + std::vector pixelBuffers; pixbuf.x = windowX(); pixbuf.y = windowY(); - pbs.push_back(&pixbuf); + pixelBuffers.push_back(&pixbuf); - std::vector::iterator i = children.begin(); - while(i != children.end()) + for(auto child : children) { - Widget *w = *i; - if(w->visible()) + if(child->visible()) { - std::vector pbs0 = w->getPixelBuffers(); - pbs.insert(pbs.end(), pbs0.begin(), pbs0.end()); + auto childPixelBuffers = child->getPixelBuffers(); + pixelBuffers.insert(pixelBuffers.end(), + childPixelBuffers.begin(), childPixelBuffers.end()); } - i++; } - return pbs; + return pixelBuffers; } bool Widget::hasKeyboardFocus() @@ -217,4 +208,16 @@ bool Widget::hasKeyboardFocus() return window()->keyboardFocus() == this; } +void Widget::repaintChildren(RepaintEvent* repaintEvent) +{ + Painter p(*this); // make sure pixbuf refcount is incremented. + + this->repaintEvent(repaintEvent); + + for(auto child : children) + { + child->repaintChildren(repaintEvent); + } +} + } // GUI:: diff --git a/plugingui/widget.h b/plugingui/widget.h index 806ee5c..9827641 100644 --- a/plugingui/widget.h +++ b/plugingui/widget.h @@ -38,8 +38,9 @@ namespace GUI { class Window; class Widget : public Listener, LayoutItem { + friend class Painter; public: - Widget(Widget *parent); + Widget(Widget* parent); virtual ~Widget(); virtual void show(); @@ -59,15 +60,14 @@ public: virtual bool isFocusable() { return false; } virtual bool catchMouse() { return false; } - void addChild(Widget *widget); - void removeChild(Widget *widget); + void addChild(Widget* widget); + void removeChild(Widget* widget); virtual void repaintEvent(RepaintEvent* repaintEvent) {} virtual void mouseMoveEvent(MouseMoveEvent* mouseMoveEvent) {} virtual void buttonEvent(ButtonEvent* buttonEvent) {} virtual void scrollEvent(ScrollEvent* scrollEvent) {} virtual void keyEvent(KeyEvent* keyEvent) {} - virtual void mouseLeaveEvent() {} virtual void mouseEnterEvent() {} @@ -75,25 +75,29 @@ public: virtual Window* window(); - void repaint_r(RepaintEvent* repaintEvent); - - PixelBufferAlpha pixbuf; std::vector getPixelBuffers(); bool hasKeyboardFocus(); - Widget *parent; - bool visible(); void setVisible(bool visible); protected: + void repaintChildren(RepaintEvent* repaintEvent); + + PixelBufferAlpha pixbuf{0,0}; + std::vector children; - Window *_window; - size_t _x, _y, _width, _height; -private: - bool _visible; + Widget* parent = nullptr; + Window* _window = nullptr; + + size_t _x{0}; + size_t _y{0}; + size_t _width{0}; + size_t _height{0}; + + bool _visible = true; }; } // GUI:: diff --git a/plugingui/window.cc b/plugingui/window.cc index b54bc3c..d1cce76 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -136,7 +136,7 @@ size_t GUI::Window::windowY() { return 0; } void GUI::Window::show() { - repaint_r(NULL); + repaintChildren(NULL); native->show(); } -- cgit v1.2.3