From 728abec962f993309acf3ebb1317b5f3773f18c7 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 8 Feb 2017 22:22:00 +0100 Subject: UI resize refactoring part 1. --- configure.ac | 1 + plugingui/filebrowser.cc | 16 ++--- plugingui/filebrowser.h | 2 +- plugingui/layout.h | 29 ++++++--- plugingui/listbox.cc | 2 +- plugingui/listbox.h | 2 +- plugingui/listboxbasic.cc | 8 +-- plugingui/listboxbasic.h | 2 +- plugingui/listboxthin.cc | 2 +- plugingui/listboxthin.h | 2 +- plugingui/mainwindow.cc | 3 +- plugingui/mainwindow.h | 2 +- plugingui/nativewindow.h | 41 +++++++----- plugingui/nativewindow_win32.cc | 26 ++++++-- plugingui/nativewindow_win32.h | 6 +- plugingui/nativewindow_x11.cc | 42 ++++++++++-- plugingui/nativewindow_x11.h | 6 +- plugingui/testmain.cc | 1 + plugingui/tests/Makefile.am | 10 +++ plugingui/tests/resizetest.cc | 141 ++++++++++++++++++++++++++++++++++++++++ plugingui/textedit.cc | 2 +- plugingui/textedit.h | 2 +- plugingui/widget.cc | 20 +++--- plugingui/widget.h | 20 +++--- plugingui/window.cc | 44 ++++++++----- plugingui/window.h | 10 ++- 26 files changed, 341 insertions(+), 101 deletions(-) create mode 100644 plugingui/tests/Makefile.am create mode 100644 plugingui/tests/resizetest.cc diff --git a/configure.ac b/configure.ac index f93e6ae..828cd8e 100644 --- a/configure.ac +++ b/configure.ac @@ -662,6 +662,7 @@ AC_CONFIG_FILES( plugin/Makefile plugin/vst/Makefile plugingui/Makefile + plugingui/tests/Makefile include/Makefile man/Makefile test/Makefile diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index 4470d00..329c599 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -99,9 +99,9 @@ void FileBrowser::setPath(const std::string& path) changeDir(); } -void FileBrowser::resize(int w, int h) +void FileBrowser::resize(std::size_t width, std::size_t height) { - Widget::resize(w,h); + Widget::resize(width, height); int offset = 0; int brd = 5; // border @@ -115,18 +115,18 @@ void FileBrowser::resize(int w, int h) offset += btn_h; lbl_path.resize(60, btn_h); - lineedit.resize(w - 60 - brd, btn_h); + lineedit.resize(width - 60 - brd, btn_h); offset += brd; listbox.move(brd, offset); - listbox.resize(w - 1 - 2*brd, h - btn_h - 2*brd - offset); + listbox.resize(width - 1 - 2*brd, height - btn_h - 2*brd - offset); - btn_esc.move(brd, h - btn_h - brd); - btn_esc.resize((w - 1 - 2*brd) / 2 - brd / 2, btn_h); + btn_esc.move(brd, height - btn_h - brd); + btn_esc.resize((width - 1 - 2*brd) / 2 - brd / 2, btn_h); - btn_sel.move(brd + w / 2 - brd / 2, h - btn_h - brd); - btn_sel.resize((w - 1 - 2*brd) / 2, btn_h); + btn_sel.move(brd + width / 2 - brd / 2, height - btn_h - brd); + btn_sel.resize((width - 1 - 2*brd) / 2, btn_h); } void FileBrowser::repaintEvent(RepaintEvent* repaintEvent) diff --git a/plugingui/filebrowser.h b/plugingui/filebrowser.h index 20f8197..64ef4da 100644 --- a/plugingui/filebrowser.h +++ b/plugingui/filebrowser.h @@ -50,7 +50,7 @@ public: // From Widget: bool isFocusable() override { return true; } virtual void repaintEvent(RepaintEvent* repaintEvent) override; - virtual void resize(int w, int h) override; + virtual void resize(std::size_t width, std::size_t height) override; private: void listSelectionChanged(); diff --git a/plugingui/layout.h b/plugingui/layout.h index 49bf75c..bd64fdd 100644 --- a/plugingui/layout.h +++ b/plugingui/layout.h @@ -31,30 +31,33 @@ #include -namespace GUI { +namespace GUI +{ class Layout; -class LayoutItem { +class LayoutItem +{ public: LayoutItem(); virtual ~LayoutItem(); void setLayoutParent(Layout* parent); - virtual void resize(int width, int height) = 0; - virtual void move(size_t x, size_t y) = 0; + virtual void resize(std::size_t width, std::size_t height) = 0; + virtual void move(int x, int y) = 0; virtual int x() = 0; virtual int y() = 0; - virtual size_t width() = 0; - virtual size_t height() = 0; + virtual std::size_t width() = 0; + virtual std::size_t height() = 0; private: Layout* parent; }; //! \brief Abtract Layout class. -class Layout : public Listener +class Layout + : public Listener { public: Layout(LayoutItem *parent); @@ -75,7 +78,9 @@ protected: }; //! \brief Abstract box layout -class BoxLayout : public Layout { +class BoxLayout + : public Layout +{ public: BoxLayout(LayoutItem *parent); @@ -99,7 +104,9 @@ enum class HAlignment { }; //! \brief A Layout that lays out its elements vertically. -class VBoxLayout : public BoxLayout { +class VBoxLayout + : public BoxLayout +{ public: VBoxLayout(LayoutItem *parent); @@ -119,7 +126,9 @@ enum class VAlignment { }; //! \brief A Layout that lays out its elements vertically. -class HBoxLayout : public BoxLayout { +class HBoxLayout + : public BoxLayout +{ public: HBoxLayout(LayoutItem *parent); diff --git a/plugingui/listbox.cc b/plugingui/listbox.cc index ba61424..15e4620 100644 --- a/plugingui/listbox.cc +++ b/plugingui/listbox.cc @@ -97,7 +97,7 @@ void ListBox::repaintEvent(RepaintEvent* repaintEvent) p.drawImage(0, 0, box); } -void ListBox::resize(int width, int height) +void ListBox::resize(std::size_t width, std::size_t height) { Widget::resize(width, height); basic.resize(width - (7 + 7), diff --git a/plugingui/listbox.h b/plugingui/listbox.h index 3032951..a17a475 100644 --- a/plugingui/listbox.h +++ b/plugingui/listbox.h @@ -55,7 +55,7 @@ public: // From Widget: virtual void repaintEvent(RepaintEvent* repaintEvent) override; - virtual void resize(int w, int h) override; + virtual void resize(std::size_t width, std::size_t height) override; // Forwarded notifiers from ListBoxBasic::basic Notifier<>& selectionNotifier; diff --git a/plugingui/listboxbasic.cc b/plugingui/listboxbasic.cc index 2e02301..730880a 100644 --- a/plugingui/listboxbasic.cc +++ b/plugingui/listboxbasic.cc @@ -355,11 +355,11 @@ void ListBoxBasic::buttonEvent(ButtonEvent* buttonEvent) } } -void ListBoxBasic::resize(int w, int h) +void ListBoxBasic::resize(std::size_t width, std::size_t height) { - Widget::resize(w,h); - scroll.move(w - scroll.width(), 0); - scroll.resize(scroll.width(), h); + Widget::resize(width, height); + scroll.move(width - scroll.width(), 0); + scroll.resize(scroll.width(), height); } } // GUI:: diff --git a/plugingui/listboxbasic.h b/plugingui/listboxbasic.h index ae2bdae..662c5d3 100644 --- a/plugingui/listboxbasic.h +++ b/plugingui/listboxbasic.h @@ -64,7 +64,7 @@ public: Notifier<> valueChangedNotifier; // From Widget: - virtual void resize(int w, int h) override; + virtual void resize(std::size_t width, std::size_t height) override; protected: void onScrollBarValueChange(int value); diff --git a/plugingui/listboxthin.cc b/plugingui/listboxthin.cc index 5d8ab49..ce5bdbc 100644 --- a/plugingui/listboxthin.cc +++ b/plugingui/listboxthin.cc @@ -93,7 +93,7 @@ void ListBoxThin::repaintEvent(RepaintEvent* repaintEvent) p.drawImage(0, 0, box); } -void ListBoxThin::resize(int width, int height) +void ListBoxThin::resize(std::size_t height, std::size_t width) { Widget::resize(width, height); basic.resize(width - (1 + 1), diff --git a/plugingui/listboxthin.h b/plugingui/listboxthin.h index a5605f2..539946a 100644 --- a/plugingui/listboxthin.h +++ b/plugingui/listboxthin.h @@ -56,7 +56,7 @@ public: // From Widget: virtual void repaintEvent(GUI::RepaintEvent* repaintEvent) override; - virtual void resize(int w, int h) override; + virtual void resize(std::size_t height, std::size_t width) override; // Forwarded notifier from ListBoxBasic::basic Notifier<>& selectionNotifier; diff --git a/plugingui/mainwindow.cc b/plugingui/mainwindow.cc index cff6b04..3a1727f 100644 --- a/plugingui/mainwindow.cc +++ b/plugingui/mainwindow.cc @@ -39,7 +39,6 @@ MainWindow::MainWindow(Settings& settings, void* native_window) { CONNECT(this, sizeChangeNotifier, this, &MainWindow::sizeChanged); CONNECT(eventHandler(), closeNotifier, this, &MainWindow::closeEventHandler); - resize(370, 330); tabs.move(16, 0); // x-offset to make room for the left side bar. setCaption("DrumGizmo v" VERSION); } @@ -90,7 +89,7 @@ void MainWindow::repaintEvent(RepaintEvent* repaintEvent) painter.drawImage(width() - 16, 0, sidebar); } -void MainWindow::sizeChanged(int width, int height) +void MainWindow::sizeChanged(std::size_t width, std::size_t height) { tabs.resize(width - 2 * 16, height); } diff --git a/plugingui/mainwindow.h b/plugingui/mainwindow.h index bfef4a0..886dc3e 100644 --- a/plugingui/mainwindow.h +++ b/plugingui/mainwindow.h @@ -50,7 +50,7 @@ public: Notifier<> closeNotifier; private: - void sizeChanged(int width, int height); + void sizeChanged(std::size_t width, std::size_t height); void closeEventHandler(); // From Widget diff --git a/plugingui/nativewindow.h b/plugingui/nativewindow.h index ff24fed..ffa5c15 100644 --- a/plugingui/nativewindow.h +++ b/plugingui/nativewindow.h @@ -31,53 +31,60 @@ #include "guievent.h" -namespace GUI { +namespace GUI +{ -//! \brief Interface class for native window implementations. +//! Interface class for native window implementations. class NativeWindow { public: NativeWindow() {} virtual ~NativeWindow() {} - //! \brief Set a fixed size to the window. + //! Set a fixed size to the window. //! It resizes the window and disallows user resizing. - virtual void setFixedSize(int width, int height) = 0; + virtual void setFixedSize(std::size_t width, std::size_t height) = 0; - // TODO: setScalable(bool) ?? + //! Set a new size of the window. + virtual void resize(std::size_t width, std::size_t height) = 0; - //! \brief Set a new size of the window. - virtual void resize(int width, int height) = 0; + //! Query size of the native window. + virtual std::pair getSize() = 0; - //! \brief Move the window to a new position. + //! Move the window to a new position. + //! Note: negative value are allowed. virtual void move(int x, int y) = 0; - //! \brief Show the window if it is hidden. + //! Query the screen position of the native window. + //! Note: returned values can be negative. + virtual std::pair getPosition() = 0; + + //! Show the window if it is hidden. virtual void show() = 0; - //! \brief Hides the window. + //! Hides the window. virtual void hide() = 0; - //! \brief Sets the window caption in the title bar (if it has one). + //! Sets the window caption in the title bar (if it has one). virtual void setCaption(const std::string &caption) = 0; - //! \brief Recreate a window render buffer based on the internal buffer. + //! Recreate a window render buffer based on the internal buffer. //! This need to be called whenever the internal buffer size has changed. virtual void handleBuffer() = 0; - //! \brief Draw the internal rendering buffer to the window buffer. + //! Draw the internal rendering buffer to the window buffer. virtual void redraw() = 0; - //! \brief Toggle capture mouse mode. + //! Toggle capture mouse mode. virtual void grabMouse(bool grab) = 0; - //! \brief Query if the event queue contains any events. + //! Query if the event queue contains any events. virtual bool hasEvent() = 0; - //! \brief Read a single event from the event queue. + //! Read a single event from the event queue. //! \return A pointer to the event or nullptr is none exists. virtual std::shared_ptr getNextEvent() = 0; - //! \brief Read next event without popping it from the event queue. + //! Read next event without popping it from the event queue. //! \return A pointer to the event or nullptr is none exists. virtual std::shared_ptr peekNextEvent() = 0; }; diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc index 172850d..873a334 100644 --- a/plugingui/nativewindow_win32.cc +++ b/plugingui/nativewindow_win32.cc @@ -335,7 +335,7 @@ NativeWindowWin32::~NativeWindowWin32() free(m_className); } -void NativeWindowWin32::setFixedSize(int width, int height) +void NativeWindowWin32::setFixedSize(std::size_t width, std::size_t height) { resize(width, height); LONG style = GetWindowLong(m_hwnd, GWL_STYLE); @@ -343,22 +343,36 @@ void NativeWindowWin32::setFixedSize(int width, int height) SetWindowLong(m_hwnd, GWL_STYLE, style); } -void NativeWindowWin32::resize(int width, int height) +void NativeWindowWin32::resize(std::size_t width, std::size_t height) { SetWindowPos(m_hwnd, nullptr, -1, -1, (int)width, (int)height, SWP_NOMOVE); - RECT r; - GetClientRect(m_hwnd, &r); - int w = width - r.right; - int h = height - r.bottom; + RECT rect; + GetClientRect(m_hwnd, &rect); + int w = width - rect.right; + int h = height - rect.bottom; SetWindowPos(m_hwnd, nullptr, -1, -1, width + w, height + h, SWP_NOMOVE); } +std::pair NativeWindowWin32::getSize() +{ + RECT rect; + GetClientRect(m_hwnd, &rect); + return std::make_pair(rect.right - rect.left, rect.bottom - rect.top); +} + void NativeWindowWin32::move(int x, int y) { SetWindowPos(m_hwnd, nullptr, (int)x, (int)y, -1, -1, SWP_NOSIZE); } +std::pair NativeWindowWin32::getPosition() +{ + RECT rect; + GetClientRect(m_hwnd, &rect); + return std::make_pair(rect.left, rect.top); +} + void NativeWindowWin32::show() { ShowWindow(m_hwnd, SW_SHOW); diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h index 110085c..8282c6f 100644 --- a/plugingui/nativewindow_win32.h +++ b/plugingui/nativewindow_win32.h @@ -44,9 +44,11 @@ public: NativeWindowWin32(void* native_window, Window& window); ~NativeWindowWin32(); - void setFixedSize(int width, int height) override; - void resize(int width, int height) override; + void setFixedSize(std::size_t width, std::size_t height) override; + void resize(std::size_t width, std::size_t height) override; + std::pair getSize() override; void move(int x, int y) override; + std::pair getPosition() override; void show() override; void setCaption(const std::string &caption) override; void hide() override; diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc index 92e1f3a..f50e01d 100644 --- a/plugingui/nativewindow_x11.cc +++ b/plugingui/nativewindow_x11.cc @@ -73,8 +73,8 @@ NativeWindowX11::NativeWindowX11(void* native_window, Window& window) swa.backing_store = Always; xwindow = XCreateWindow(display, parentWindow, - window.x(), window.y(), - window.width(), window.height(), + 0, 0, //window.x(), window.y(), + 1, 1, //window.width(), window.height(), 0, // border CopyFromParent, // depth CopyFromParent, // class @@ -119,7 +119,7 @@ NativeWindowX11::~NativeWindowX11() XCloseDisplay(display); } -void NativeWindowX11::setFixedSize(int width, int height) +void NativeWindowX11::setFixedSize(std::size_t width, std::size_t height) { if(display == nullptr) { @@ -149,7 +149,7 @@ void NativeWindowX11::setFixedSize(int width, int height) XSetWMNormalHints(display, xwindow, size_hints); } -void NativeWindowX11::resize(int width, int height) +void NativeWindowX11::resize(std::size_t width, std::size_t height) { if(display == nullptr) { @@ -159,6 +159,23 @@ void NativeWindowX11::resize(int width, int height) XResizeWindow(display, xwindow, width, height); } +std::pair NativeWindowX11::getSize() +{ +// XWindowAttributes attributes; +// XGetWindowAttributes(display, xwindow, &attributes); +// return std::make_pair(attributes.width, attributes.height); + + ::Window root_window; + int x, y; + unsigned int width, height, border, depth; + + XGetGeometry(display, xwindow, &root_window, + &x, &y, + &width, &height, &border, &depth); + + return std::make_pair(width, height); +} + void NativeWindowX11::move(int x, int y) { if(display == nullptr) @@ -169,6 +186,23 @@ void NativeWindowX11::move(int x, int y) XMoveWindow(display, xwindow, x, y); } +std::pair NativeWindowX11::getPosition() +{ + ::Window root_window; + ::Window child_window; + int x, y; + unsigned int width, height, border, depth; + + XGetGeometry(display, xwindow, &root_window, + &x, &y, + &width, &height, &border, &depth); + + XTranslateCoordinates(display, xwindow, root_window, + 0, 0, &x, &y, &child_window); + + return std::make_pair(x, y); +} + void NativeWindowX11::show() { if(display == nullptr) diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h index f36be51..10c2026 100644 --- a/plugingui/nativewindow_x11.h +++ b/plugingui/nativewindow_x11.h @@ -44,9 +44,11 @@ public: ~NativeWindowX11(); // From NativeWindow: - void setFixedSize(int width, int height) override; - void resize(int width, int height) override; + void setFixedSize(std::size_t width, std::size_t height) override; + void resize(std::size_t width, std::size_t height) override; + std::pair getSize() override; void move(int x, int y) override; + std::pair getPosition() override; void show() override; void hide() override; void setCaption(const std::string &caption) override; diff --git a/plugingui/testmain.cc b/plugingui/testmain.cc index c4ca18d..d77ac8a 100644 --- a/plugingui/testmain.cc +++ b/plugingui/testmain.cc @@ -41,6 +41,7 @@ int main() Settings settings; GUI::MainWindow main_window(settings, nullptr); main_window.show(); + main_window.resize(370, 330); while(main_window.processEvents()) { diff --git a/plugingui/tests/Makefile.am b/plugingui/tests/Makefile.am new file mode 100644 index 0000000..b32a477 --- /dev/null +++ b/plugingui/tests/Makefile.am @@ -0,0 +1,10 @@ +noinst_PROGRAMS = resizetest + +resizetest_LDADD = $(top_srcdir)/plugingui/libdggui.la +resizetest_CXXFLAGS = \ + -I$(top_srcdir)/plugingui \ + -I$(top_srcdir)/src \ + -I$(top_srcdir)/hugin +resizetest_SOURCES = \ + resizetest.cc \ + $(top_srcdir)/hugin/hugin.c diff --git a/plugingui/tests/resizetest.cc b/plugingui/tests/resizetest.cc new file mode 100644 index 0000000..5a27eb8 --- /dev/null +++ b/plugingui/tests/resizetest.cc @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * resizetest.cc + * + * Sun Feb 5 20:05:24 CET 2017 + * Copyright 2017 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif + +#include +#include + +class TestWindow + : public GUI::Window +{ +public: + TestWindow() + : GUI::Window(nullptr) + { + setCaption("ResizeTest Window"); + CONNECT(eventHandler(), closeNotifier, + this, &TestWindow::closeEventHandler); + CONNECT(this, sizeChangeNotifier, this, &TestWindow::sizeChanged); + CONNECT(this, positionChangeNotifier, this, &TestWindow::positionChanged); + } + + void sizeChanged(std::size_t width, std::size_t height) + { + reportedSize = std::make_pair(width, height); + repaintEvent(nullptr); + } + + void positionChanged(int x, int y) + { + reportedPosition = std::make_pair(x, y); + repaintEvent(nullptr); + } + + void closeEventHandler() + { + closing = true; + } + + bool processEvents() + { + eventHandler()->processEvents(); + return !closing; + } + + void repaintEvent(GUI::RepaintEvent* repaintEvent) + { + GUI::Painter painter(*this); + + //painter.clear(); + painter.setColour(GUI::Colour(0,1,0)); + painter.drawFilledRectangle(0, 0, width(), height()); + + auto currentSize = std::make_pair(width(), height()); + auto currentPosition = std::make_pair(x(), y()); + + { + painter.setColour(GUI::Colour(1,0,0)); + char str[64]; + sprintf(str, "reported: (%d, %d); (%d, %d)", + reportedPosition.first, + reportedPosition.second, + reportedSize.first, + reportedSize.second); + auto stringWidth = font.textWidth(str); + auto stringHeight = font.textHeight(str); + painter.drawText(reportedSize.first / 2 - stringWidth / 2, + reportedSize.second / 2 + stringHeight / 2 - 7, + font, str, false); + } + + { + painter.setColour(GUI::Colour(1,0,0)); + char str[64]; + sprintf(str, "current: (%d, %d); (%d, %d)", + currentPosition.first, + currentPosition.second, + currentSize.first, + currentSize.second); + auto stringWidth = font.textWidth(str); + auto stringHeight = font.textHeight(str); + painter.drawText(currentSize.first / 2 - stringWidth / 2, + currentSize.second / 2 + stringHeight / 2 + 7, + font, str, false); + } + } + +private: + bool closing{false}; + GUI::Font font{":font.png"}; + std::pair reportedSize; + std::pair reportedPosition; +}; + +int main() +{ + INFO(example, "We are up and running"); + + TestWindow test_window; + test_window.show(); + test_window.resize(300,300); + + while(test_window.processEvents()) + { +#ifdef WIN32 + SleepEx(50, FALSE); +#else + usleep(50000); +#endif + } + + return 0; +} diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc index 05aa251..6274d37 100644 --- a/plugingui/textedit.cc +++ b/plugingui/textedit.cc @@ -52,7 +52,7 @@ TextEdit::~TextEdit() { } -void TextEdit::resize(int height, int width) +void TextEdit::resize(std::size_t height, std::size_t width) { Widget::resize(height, width); scroll.resize(scroll.width(), height-10); diff --git a/plugingui/textedit.h b/plugingui/textedit.h index 44189a5..83a7627 100644 --- a/plugingui/textedit.h +++ b/plugingui/textedit.h @@ -45,7 +45,7 @@ public: // From Widget bool isFocusable() override { return true; } - void resize(int width, int height) override; + void resize(std::size_t width, std::size_t height) override; std::string text(); void setText(const std::string& text); diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 476746e..9b8b173 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -41,9 +41,6 @@ Widget::Widget(Widget* parent) parent->addChild(this); _window = parent->window(); } - - _width = _height = 0; - _visible = true; } Widget::~Widget() @@ -67,7 +64,11 @@ void Widget::hide() void Widget::setVisible(bool visible) { _visible = visible; - repaintEvent(nullptr); + + if(visible) + { + repaintEvent(nullptr); + } } bool Widget::visible() @@ -112,10 +113,10 @@ void Widget::reparent(Widget* parent) this->parent = parent; } -void Widget::resize(int width, int height) +void Widget::resize(std::size_t width, std::size_t height) { if((width < 1) || (height < 1) || - (((size_t)width == _width) && ((size_t)height == _height))) + ((width == _width) && (height == _height))) { return; } @@ -127,10 +128,11 @@ void Widget::resize(int width, int height) sizeChangeNotifier(width, height); } -void Widget::move(size_t x, size_t y) +void Widget::move(int x, int y) { _x = x; _y = y; + positionChangeNotifier(x, y); } int Widget::x() @@ -143,12 +145,12 @@ int Widget::y() return _y; } -size_t Widget::width() +std::size_t Widget::width() { return _width; } -size_t Widget::height() +std::size_t Widget::height() { return _height; } diff --git a/plugingui/widget.h b/plugingui/widget.h index 03e8ab0..4ea79e9 100644 --- a/plugingui/widget.h +++ b/plugingui/widget.h @@ -54,19 +54,22 @@ public: virtual void hide(); // From LayoutItem - virtual void resize(int width, int height) override; - virtual void move(size_t x, size_t y) override; + virtual void resize(std::size_t width, std::size_t height) override; + virtual void move(int x, int y) override; virtual int x() override; virtual int y() override; - virtual size_t width() override; - virtual size_t height() override; + virtual std::size_t width() override; + virtual std::size_t height() override; // From Canvas PixelBufferAlpha& GetPixelBuffer() override; void beginPaint() override; void endPaint() override; + //! Translate x-coordinate from parent-space to window-space. virtual size_t windowX(); + + //! Translate y-coordinate from parent-space to window-space. virtual size_t windowY(); virtual bool isFocusable() { return false; } @@ -97,7 +100,8 @@ public: bool visible(); void setVisible(bool visible); - Notifier sizeChangeNotifier; // (int, width, int height) + Notifier sizeChangeNotifier; // (width, height) + Notifier positionChangeNotifier; // (x, y) protected: void repaintChildren(RepaintEvent* repaintEvent); @@ -111,10 +115,10 @@ protected: int _x{0}; int _y{0}; - size_t _width{0}; - size_t _height{0}; + std::size_t _width{0}; + std::size_t _height{0}; - bool _visible = true; + bool _visible{true}; }; } // GUI:: diff --git a/plugingui/window.cc b/plugingui/window.cc index 5d929b2..98c0c80 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -46,7 +46,7 @@ namespace GUI { Window::Window(void* native_window) : Widget(nullptr) - , wpixbuf(100, 100) + , wpixbuf(1, 1) { // Make sure we have a valid size when initialising the NativeWindow _width = wpixbuf.width; @@ -88,24 +88,39 @@ void Window::setCaption(const std::string& caption) //! This overload the resize method on Widget and simply requests a window resize //! on the windowmanager/OS. The resized() method is called by the event handler //! once the window has been resized. -void Window::resize(int width, int height) +void Window::resize(std::size_t width, std::size_t height) { - if((width < 1) || (height < 1)) - { - return; - } - native->resize(width, height); } //! This overload the move method on Widget and simply requests a window move //! on the windowmanager/OS. The moved() method is called by the event handler //! once the window has been moved. -void Window::move(size_t x, size_t y) +void Window::move(int x, int y) { native->move(x, y); } +int Window::x() +{ + return native->getPosition().first; +} + +int Window::y() +{ + return native->getPosition().second; +} + +size_t Window::width() +{ + return native->getSize().first; +} + +size_t Window::height() +{ + return native->getSize().second; +} + size_t Window::windowX() { return 0; @@ -194,15 +209,10 @@ void Window::redraw() //! Called by event handler when an windowmanager/OS window resize event has //! been received. Do not call this directly. -void Window::resized(size_t width, size_t height) +void Window::resized(std::size_t width, std::size_t height) { - if((_width == width) && (_height == height)) - { - return; - } - - wpixbuf.realloc(width, height); - Widget::resize(width, height); + wpixbuf.realloc(this->width(), this->height()); + Widget::resize(this->width(), this->height()); updateBuffer(); } @@ -210,7 +220,7 @@ void Window::resized(size_t width, size_t height) //! been received. Do not call this directly. void Window::moved(int x, int y) { - // Make sure widget corrdinates are updated. + // Make sure widget coordinates are updated. Widget::move(x, y); } diff --git a/plugingui/window.h b/plugingui/window.h index 705742f..0a10b3e 100644 --- a/plugingui/window.h +++ b/plugingui/window.h @@ -48,8 +48,12 @@ public: void setCaption(const std::string& caption); // From Widget: - void resize(int width, int height) override; - void move(size_t x, size_t y) override; + void resize(std::size_t width, std::size_t height) override; + void move(int x, int y) override; + int x() override; + int y() override; + size_t width() override; + size_t height() override; size_t windowX() override; size_t windowY() override; void show() override; @@ -73,7 +77,7 @@ protected: // For the EventHandler friend class EventHandler; void redraw(); - void resized(size_t w, size_t h); + void resized(std::size_t width, std::size_t height); void moved(int x, int y); void updateBuffer(); -- cgit v1.2.3