From 75668f13e61ef137d1835b3d60e6de9a7a9ecb42 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 19 Dec 2004 00:06:24 +0000 Subject: Implemented setLocationRelativeTo method on Window allowing the dialogs to be centered on screen. --- src/gui/char_server.cpp | 9 +++++++-- src/gui/login.cpp | 3 ++- src/gui/window.cpp | 32 ++++++++++++++++++++++++++++++++ src/gui/window.h | 22 +++++++++++++++++++++- 4 files changed, 62 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gui/char_server.cpp b/src/gui/char_server.cpp index e71fdd00..342836e4 100644 --- a/src/gui/char_server.cpp +++ b/src/gui/char_server.cpp @@ -39,7 +39,7 @@ ServerSelectDialog::ServerSelectDialog(): okButton = new Button("OK"); cancelButton = new Button("Cancel"); - setDimension(gcn::Rectangle(300, 200, 200, 100)); + setSize(200, 100); scrollArea->setDimension(gcn::Rectangle(4, 4, 192, 55)); okButton->setPosition(120, 70); cancelButton->setPosition(146, 70); @@ -74,6 +74,11 @@ ServerSelectDialog::~ServerSelectDialog() delete cancelButton; } +void ServerSelectDialog::init() +{ + setLocationRelativeTo(getParent()); +} + void ServerSelectDialog::action(const std::string& eventId) { if (eventId == "ok") { @@ -99,8 +104,8 @@ std::string ServerListModel::getElementAt(int i) { void char_server() { ServerSelectDialog *dialog = new ServerSelectDialog(); - guiTop->add(dialog); + dialog->init(); state = LOGIN; diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 511b45b6..cd74e850 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -39,7 +39,7 @@ LoginDialog::LoginDialog(): okButton = new Button("OK"); cancelButton = new Button("Cancel"); - setDimension(gcn::Rectangle(300, 250, 200, 80)); + setSize(200, 80); userLabel->setPosition(4, 11); passLabel->setPosition(4, 31); userField->setPosition(60, 10); @@ -84,6 +84,7 @@ LoginDialog::~LoginDialog() void LoginDialog::init() { + setLocationRelativeTo(getParent()); userField->requestFocus(); userField->setCaretPosition(userField->getText().length()); diff --git a/src/gui/window.cpp b/src/gui/window.cpp index c038312c..a75dd242 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -48,6 +48,7 @@ Window::Window(std::string text) : // Add chrome chrome = new gcn::Container(); chrome->setOpaque(false); + chrome->setY(titlebarHeight); gcn::Container::add(chrome); } @@ -115,6 +116,37 @@ void Window::setDimension(const gcn::Rectangle &dimension) dimension.width, dimension.height)); } +void Window::setWidth(int width) +{ + gcn::Container::setWidth(width); + chrome->setWidth(width); +} + +void Window::setHeight(int height) +{ + gcn::Container::setHeight(height + titlebarHeight); + chrome->setHeight(height); +} + +void Window::setLocationRelativeTo(gcn::Widget* widget) +{ + int wx, wy; + int x, y; + + widget->getAbsolutePosition(wx, wy); + getAbsolutePosition(x, y); + + setPosition( + getX() + (wx + (widget->getWidth() - getWidth()) / 2 - x), + getY() + (wy + (widget->getHeight() - getHeight()) / 2 - y)); +} + +void Window::setSize(int width, int height) +{ + setWidth(width); + setHeight(height); +} + void Window::add(gcn::Widget *w) { chrome->add(w); diff --git a/src/gui/window.h b/src/gui/window.h index 8b9f983e..a51a150c 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -68,10 +68,30 @@ class Window : public gcn::Container, public gcn::MouseListener void add(gcn::Widget *w, int x, int y); /** - * Set the dimension of the window contents. + * Sets the dimension of the window contents. */ void setDimension(const gcn::Rectangle& dimension); + /** + * Sets the width of the window contents. + */ + void setWidth(int width); + + /** + * Sets the height of the window contents. + */ + void setHeight(int height); + + /** + * Sets the location relative to the given widget. + */ + void setLocationRelativeTo(gcn::Widget* widget); + + /** + * Sets the size of this window. + */ + void setSize(int width, int height); + // Mouse handling void mousePress(int mx, int my, int button); -- cgit v1.2.3-60-g2f50