From 44fa03c1bf8eb60d38256b48720f9958c735aadc Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 20 Nov 2015 21:45:41 +0100 Subject: Refactored Window. --- plugingui/window.cc | 303 +++++++++++++++++++++++++++++----------------------- plugingui/window.h | 101 +++++++++--------- 2 files changed, 220 insertions(+), 184 deletions(-) (limited to 'plugingui') diff --git a/plugingui/window.cc b/plugingui/window.cc index d1cce76..30e90ee 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -27,11 +27,6 @@ #include "window.h" #include "painter.h" - -#include -#include - -#include #include #ifndef PUGL @@ -45,212 +40,252 @@ #include "nativewindow_pugl.h" #endif -GUI::Window::Window() - : Widget(NULL), wpixbuf(100, 100), back(":bg.png"), logo(":logo.png") -{ - _x = _y = 100; - _width = wpixbuf.width; - _height = wpixbuf.height; +namespace GUI { - refcount = 0; - max_refcount = 0; - _keyboardFocus = this; - _buttonDownFocus = NULL; - _mouseFocus = NULL; +Window::Window() + : Widget(nullptr) + , wpixbuf(100, 100) +{ + // Make sure we have a valid size when initialising the NativeWindow + _width = wpixbuf.width; + _height = wpixbuf.height; #ifndef PUGL #ifdef X11 - native = new NativeWindowX11(*this); + native = new NativeWindowX11(*this); #endif/*X11*/ #ifdef WIN32 - native = new NativeWindowWin32(*this); + native = new NativeWindowWin32(*this); #endif/*WIN32*/ #else/*Use pugl*/ - native = new NativeWindowPugl(this); + native = new NativeWindowPugl(this); #endif - eventhandler = new GUI::EventHandler(*native, *this); + eventhandler = new EventHandler(*native, *this); } -GUI::Window::~Window() +Window::~Window() { - delete native; - delete eventhandler; + delete native; + delete eventhandler; } -GUI::EventHandler *GUI::Window::eventHandler() +void Window::setFixedSize(int w, int h) { - return eventhandler; + native->setFixedSize(w, h); + resize(w,h); } -void GUI::Window::setCaption(std::string caption) +void Window::setCaption(const std::string& caption) { - native->setCaption(caption); + native->setCaption(caption); } -void GUI::Window::repaintEvent(GUI::RepaintEvent* repaintEvent) +void Window::resize(int width, int height) { - if(!visible()) return; + if((width < 1) || (height < 1)) + { + return; + } + + resized(width, height); + Widget::resize(width, height); + native->resize(width, height); +} - Painter p(*this); - p.drawImageStretched(0,0, back, width(), height()); - p.drawImage(width() - logo.width(), height() - logo.height(), logo); +void Window::move(size_t x, size_t y) +{ + native->move(x, y); + + // Make sure widget corrdinates are updated. + Widget::move(x, y); } -void GUI::Window::setFixedSize(int w, int h) +size_t Window::x() { - native->setFixedSize(w, h); - resize(w,h); + return _x; } -void GUI::Window::resize(int width, int height) +size_t Window::y() { - if(width < 1 || height < 1) return; - - // This needs to be done on all platoforms when setFixedSize is introduced. - //#ifdef WIN32 - // Fix to force buffer size reallocation - // FIXME: This should've been done indirectly through a WM_SIZE message in the - // EventHandler... - resized(width, height); - //#endif - - Widget::resize(width, height); - native->resize(width, height); + return _y; } -void GUI::Window::move(size_t x, size_t y) +size_t Window::width() { - native->move(x, y); + return _width; +} - // Make sure widget corrds are updated. - Widget::move(x, y); +size_t Window::height() +{ + return _height; } -size_t GUI::Window::x() { return _x; } -size_t GUI::Window::y() { return _y; } -size_t GUI::Window::width() { return _width; } -size_t GUI::Window::height() { return _height; } -size_t GUI::Window::windowX() { return 0; } -size_t GUI::Window::windowY() { return 0; } +size_t Window::windowX() +{ + return 0; +} -void GUI::Window::show() +size_t Window::windowY() { - repaintChildren(NULL); - native->show(); + return 0; } -void GUI::Window::hide() +void Window::show() { - native->hide(); + repaintChildren(nullptr); + native->show(); } -GUI::Window *GUI::Window::window() +void Window::hide() { - return this; + native->hide(); } -void GUI::Window::beginPaint() +Window* Window::window() { - refcount++; - if(refcount > max_refcount) max_refcount = refcount; + return this; } -void GUI::Window::endPaint() +EventHandler* Window::eventHandler() { - if(refcount) refcount--; - - if(!refcount) { - if(max_refcount > 1) { // Did we go deep enough for a buffer update? - updateBuffer(); - redraw(); - } - max_refcount = 0; - } + return eventhandler; } -void GUI::Window::updateBuffer() +Widget* Window::keyboardFocus() { - DEBUG(window, "Updating buffer\n"); - memset(wpixbuf.buf, 0, wpixbuf.width * wpixbuf.height * 3); - - std::vector pl = getPixelBuffers(); - std::vector::iterator pli = pl.begin(); - while(pli != pl.end()) { - PixelBufferAlpha *pb = *pli; - size_t updateWidth = pb->width; - size_t updateHeight = pb->height; - if(updateWidth > (wpixbuf.width - pb->x)) - { - updateWidth = (wpixbuf.width - pb->x); - } - - if(updateHeight > (wpixbuf.height - pb->y)) - { - updateHeight = (wpixbuf.height - pb->y); - } - - for(size_t x = 0; x < updateWidth; x++) { - for(size_t y = 0; y < updateHeight; y++) { - unsigned char r,g,b,a; - pb->pixel(x,y,&r,&g,&b,&a); - wpixbuf.setPixel(x + pb->x, y + pb->y, r, g, b, a); - } - } - pli++; - } - native->handleBuffer(); + return _keyboardFocus; } -void GUI::Window::resized(size_t w, size_t h) +void Window::setKeyboardFocus(Widget* widget) { - if(_width == w && _height == h) return; + auto oldFocusWidget = _keyboardFocus; + _keyboardFocus = widget; + + if(oldFocusWidget) + { + oldFocusWidget->repaintEvent(nullptr); + } + + if(_keyboardFocus) + { + _keyboardFocus->repaintEvent(nullptr); + } +} - _width = w; - _height = h; - wpixbuf.realloc(w, h); - updateBuffer(); +Widget* Window::buttonDownFocus() +{ + return _buttonDownFocus; +} + +void Window::setButtonDownFocus(Widget* widget) +{ + _buttonDownFocus = widget; + native->grabMouse(widget != nullptr); +} - pixbuf.realloc(w, h); - repaintEvent(NULL); +Widget* Window::mouseFocus() +{ + return _mouseFocus; } -void GUI::Window::redraw() +void Window::setMouseFocus(Widget* widget) { - native->redraw(); + _mouseFocus = widget; } -GUI::Widget *GUI::Window::keyboardFocus() +void Window::redraw() { - return _keyboardFocus; + native->redraw(); } -void GUI::Window::setKeyboardFocus(GUI::Widget *widget) +void Window::resized(size_t width, size_t height) { - GUI::Widget *old_focus = _keyboardFocus; - _keyboardFocus = widget; + if((_width == width) && (_height == height)) + { + return; + } + + _width = width; + _height = height; - if(old_focus) old_focus->repaintEvent(NULL); - if(_keyboardFocus) _keyboardFocus->repaintEvent(NULL); + wpixbuf.realloc(width, height); + updateBuffer(); + + pixbuf.realloc(width, height); + repaintEvent(nullptr); } -GUI::Widget *GUI::Window::buttonDownFocus() +void Window::updateBuffer() { - return _buttonDownFocus; + //DEBUG(window, "Updating buffer\n"); + for(auto pixelBuffer : getPixelBuffers()) + { + size_t updateWidth = pixelBuffer->width; + size_t updateHeight = pixelBuffer->height; + + if(updateWidth > (wpixbuf.width - pixelBuffer->x)) + { + updateWidth = (wpixbuf.width - pixelBuffer->x); + } + + if(updateHeight > (wpixbuf.height - pixelBuffer->y)) + { + updateHeight = (wpixbuf.height - pixelBuffer->y); + } + + unsigned char r,g,b,a; + for(size_t y = 0; y < updateHeight; y++) + { + for(size_t x = 0; x < updateWidth; x++) + { + pixelBuffer->pixel(x, y, &r, &g, &b, &a); + wpixbuf.setPixel(x + pixelBuffer->x, y + pixelBuffer->y, r, g, b, a); + } + } + } + + native->handleBuffer(); } -void GUI::Window::setButtonDownFocus(GUI::Widget *widget) +void Window::beginPaint() { - _buttonDownFocus = widget; - native->grabMouse(widget != NULL); + ++refcount; + if(refcount > maxRefcount) + { + maxRefcount = refcount; + } } -GUI::Widget *GUI::Window::mouseFocus() +void Window::endPaint() { - return _mouseFocus; + if(refcount) + { + --refcount; + } + + if(!refcount) + { + // Did we go deep enough for a buffer update? + if(maxRefcount > 1) + { + updateBuffer(); + redraw(); + } + maxRefcount = 0; + } } -void GUI::Window::setMouseFocus(GUI::Widget *widget) +void Window::repaintEvent(RepaintEvent* repaintEvent) { - _mouseFocus = widget; + if(!visible()) + { + return; + } + + Painter p(*this); + p.drawImageStretched(0,0, back, width(), height()); + p.drawImage(width() - logo.width(), height() - logo.height(), logo); } + +} // GUI:: diff --git a/plugingui/window.h b/plugingui/window.h index 23d91e0..7701ad9 100644 --- a/plugingui/window.h +++ b/plugingui/window.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_WINDOW_H__ -#define __DRUMGIZMO_WINDOW_H__ +#pragma once #include "widget.h" @@ -38,68 +37,70 @@ namespace GUI { class Window : public Widget { public: - Window(); - ~Window(); + Window(); + ~Window(); - void show(); - void hide(); + void setFixedSize(int width, int height); + void setCaption(const std::string& caption); - void setFixedSize(int width, int height); - void resize(int width, int height); - void move(size_t x, size_t y); + // From Widget: + void resize(int width, int height) override; + void move(size_t x, size_t y) override; + size_t x() override; + size_t y() override; + size_t width() override; + size_t height() override; + size_t windowX() override; + size_t windowY() override; + void show() override; + void hide() override; + Window* window() override; - size_t x(); - size_t y(); - size_t windowX(); - size_t windowY(); - size_t width(); - size_t height(); + EventHandler* eventHandler(); - void setCaption(std::string caption); + Widget* keyboardFocus(); + void setKeyboardFocus(Widget* widget); - void addChild(Widget *widget); + Widget* buttonDownFocus(); + void setButtonDownFocus(Widget* widget); - void repaintEvent(GUI::RepaintEvent* repaintEvent); + Widget* mouseFocus(); + void setMouseFocus(Widget* widget); - void beginPaint(); - void endPaint(); - - Window *window(); - - EventHandler *eventHandler(); - - // handlers - virtual void redraw(); - void resized(size_t w, size_t h); - - Widget *keyboardFocus(); - void setKeyboardFocus(Widget *widget); - - Widget *buttonDownFocus(); - void setButtonDownFocus(Widget *widget); +protected: + // For the EventHandler + friend class EventHandler; + void redraw(); + void resized(size_t w, size_t h); + void updateBuffer(); - Widget *mouseFocus(); - void setMouseFocus(Widget *widget); + // For the Painter + friend class Painter; + void beginPaint(); + void endPaint(); - PixelBuffer wpixbuf; - void updateBuffer(); + // From Widget: + void repaintEvent(RepaintEvent* repaintEvent) override; -protected: - size_t refcount; + // For the NativweWindow + friend class NativeWindowX11; + friend class NativeWindowWin32; + friend class NativeWindowPugl; + PixelBuffer wpixbuf; - Widget *_keyboardFocus; - Widget *_buttonDownFocus; - Widget *_mouseFocus; + size_t refcount{0}; - NativeWindow *native; - EventHandler *eventhandler; + Widget* _keyboardFocus{nullptr}; + Widget* _buttonDownFocus{nullptr}; + Widget* _mouseFocus{nullptr}; - Image back; - Image logo; + NativeWindow* native{nullptr}; + EventHandler* eventhandler{nullptr}; - size_t max_refcount; -}; + Image back{":bg.png"}; + Image logo{":logo.png"}; + size_t maxRefcount{0}; }; -#endif/*__DRUMGIZMO_WINDOW_H__*/ +} // GUI:: -- cgit v1.2.3