From 139346a2f195ee352c09f9cbb4a3b644ac8b2700 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Sun, 21 Oct 2007 06:58:19 +0000 Subject: Removed redundant resizable flag. Factored moving/resizing code, so that events are fired only once per size change. --- ChangeLog | 6 +++++ src/gui/window.cpp | 73 ++++++++++++++++++++++++++---------------------------- src/gui/window.h | 6 ++++- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8ab87921..8a62cac7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-10-21 Guillaume Melquiond + + * src/gui/window.cpp, src/gui/window.h: Removed redundant resizable + flag. Factored moving/resizing code, so that events are fired only + once per size change. + 2007-10-20 Guillaume Melquiond * src/gui/widgets/layout.h, src/gui/widgets/layout.cpp, diff --git a/src/gui/window.cpp b/src/gui/window.cpp index b5ac79b5..48fe3bfb 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -64,7 +64,6 @@ Window::Window(const std::string& caption, bool modal, Window *parent): mParent(parent), mLayout(NULL), mModal(modal), - mResizable(false), mCloseButton(false), mSticky(false), mMinWinWidth(100), @@ -131,7 +130,7 @@ Window::~Window() config.setValue(name + "WinX", getX()); config.setValue(name + "WinY", getY()); - if (mResizable) + if (mGrip) { config.setValue(name + "WinWidth", getWidth()); config.setValue(name + "WinHeight", getHeight()); @@ -211,64 +210,63 @@ void Window::setContentHeight(int height) void Window::setContentSize(int width, int height) { - setContentWidth(width); - setContentHeight(height); + setSize(width + 2 * getPadding(), + height + getPadding() + getTitleBarHeight()); } -void Window::setWidth(int width) +void Window::setSize(int width, int height) { - gcn::Window::setWidth(width); + if (width == mDimension.width && height == mDimension.height) return; + + // No call to ancestor! Infinite loop otherwise. + mDimension.width = width; + mDimension.height = height; if (mGrip) { - mGrip->setX(getWidth() - mGrip->getWidth() - getChildrenArea().x); + gcn::Rectangle const &area = getChildrenArea(); + mGrip->setPosition(width - mGrip->getWidth() - area.x, + height - mGrip->getHeight() - area.y); } fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_RESIZED)); } -void Window::setHeight(int height) +void Window::setWidth(int width) { - gcn::Window::setHeight(height); - - if (mGrip) - { - mGrip->setY(getHeight() - mGrip->getHeight() - getChildrenArea().y); - } + setSize(width, mDimension.height); +} - fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_RESIZED)); +void Window::setHeight(int height) +{ + setSize(mDimension.width, height); } -void Window::setDimension(const gcn::Rectangle &dimension) +void Window::setPosition(int x, int y) { - gcn::Window::setDimension(dimension); + if (x == mDimension.x && y == mDimension.y) return; - if (mGrip) - { - mGrip->setX(getWidth() - mGrip->getWidth() - getChildrenArea().x); - mGrip->setY(getHeight() - mGrip->getHeight() - getChildrenArea().y); - } + // No call to ancestor! + mDimension.x = x; + mDimension.y = y; - fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_RESIZED)); fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_MOVED)); } -void Window::setPosition(int x, int y) +void Window::setDimension(const gcn::Rectangle &dimension) { - gcn::Window::setPosition(x, y); - fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_MOVED)); + setPosition(dimension.x, dimension.y); + setSize(dimension.width, dimension.height); } void Window::setX(int x) { - gcn::Window::setX(x); - fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_MOVED)); + setPosition(x, mDimension.y); } void Window::setY(int y) { - gcn::Window::setY(y); - fireWindowEvent(WindowEvent(this, WindowEvent::WINDOW_MOVED)); + setPosition(mDimension.x, y); } void Window::setLocationRelativeTo(gcn::Widget *widget) @@ -305,10 +303,9 @@ void Window::setMaxHeight(unsigned int height) void Window::setResizable(bool r) { - if (mResizable == r) return; - mResizable = r; + if ((bool)mGrip == r) return; - if (mResizable) + if (r) { mGrip = new ResizeGrip(); mGrip->setX(getWidth() - mGrip->getWidth() - getChildrenArea().x); @@ -330,7 +327,7 @@ void Window::setCloseButton(bool flag) bool Window::isResizable() { - return mResizable; + return mGrip; } void Window::setSticky(bool sticky) @@ -392,7 +389,7 @@ void Window::mousePressed(gcn::MouseEvent &event) void Window::mouseReleased(gcn::MouseEvent &event) { - if (mResizable && mouseResize) + if (mGrip && mouseResize) { mouseResize = 0; gui->setCursorType(Gui::CURSOR_POINTER); @@ -404,7 +401,7 @@ void Window::mouseReleased(gcn::MouseEvent &event) void Window::mouseExited(gcn::MouseEvent &event) { - if (mResizable && !mouseResize) + if (mGrip && !mouseResize) { gui->setCursorType(Gui::CURSOR_POINTER); } @@ -523,7 +520,7 @@ void Window::loadWindowState(std::string const &name) setPosition((int) config.getValue(name + "WinX", getX()), (int) config.getValue(name + "WinY", getY())); - if (mResizable) + if (mGrip) { setSize((int) config.getValue(name + "WinWidth", getWidth()), (int) config.getValue(name + "WinHeight", getHeight())); @@ -554,7 +551,7 @@ int Window::getResizeHandles(gcn::MouseEvent &event) int resizeHandles = 0; const int y = event.getY(); - if (mResizable && y > (int) mTitleBarHeight) + if (mGrip && y > (int) mTitleBarHeight) { const int x = event.getX(); diff --git a/src/gui/window.h b/src/gui/window.h index 034c99da..651a8a4e 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -98,6 +98,11 @@ class Window : public gcn::Window */ virtual void updateContentSize() {} + /** + * Sets the size of this window. + */ + void setSize(int width, int height); + /** * Sets the width of this window. */ @@ -318,7 +323,6 @@ class Window : public gcn::Window std::string mConfigName; /**< Name used for saving window-related data */ bool mShowTitle; /**< Window has a title bar */ bool mModal; /**< Window is modal */ - bool mResizable; /**< Window can be resized */ bool mCloseButton; /**< Window has a close button */ bool mSticky; /**< Window resists minimization */ int mMinWinWidth; /**< Minimum window width */ -- cgit v1.2.3-70-g09d2