diff options
Diffstat (limited to 'src/gui/window.cpp')
-rw-r--r-- | src/gui/window.cpp | 209 |
1 files changed, 103 insertions, 106 deletions
diff --git a/src/gui/window.cpp b/src/gui/window.cpp index 64607243..58439316 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -1,21 +1,21 @@ /* * The Mana World - * Copyright 2004 The Mana World Development Team + * Copyright (C) 2004 The Mana World Development Team * * This file is part of The Mana World. * - * The Mana World is free software; you can redistribute it and/or modify + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * any later version. * - * The Mana World is distributed in the hope that it will be useful, + * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software + * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -24,13 +24,9 @@ #include <climits> #include <guichan/exception.hpp> -#include <guichan/widgets/icon.hpp> - -#include <libxml/tree.h> - -#include "window.h" #include "gui.h" +#include "window.h" #include "windowcontainer.h" #include "widgets/layout.h" @@ -38,7 +34,6 @@ #include "../configlistener.h" #include "../configuration.h" -#include "../graphics.h" #include "../log.h" #include "../resources/image.h" @@ -50,55 +45,53 @@ ConfigListener *Window::windowConfigListener = 0; WindowContainer *Window::windowContainer = 0; int Window::instances = 0; int Window::mouseResize = 0; +//ImageRect Window::border; Image *Window::closeImage = NULL; -bool mLoaded = false; bool Window::mAlphaChanged = false; class WindowConfigListener : public ConfigListener { - /* - void optionChanged(const std::string &) - { - for_each(Window::border.grid, Window::border.grid + 9, - std::bind2nd(std::mem_fun(&Image::setAlpha), - config.getValue("guialpha", 0.8))); - } - */ - void optionChanged(const std::string &) { Window::mAlphaChanged = true; } }; -Window::Window(const std::string& caption, bool modal, Window *parent): +Window::Window(const std::string& caption, bool modal, Window *parent, const std::string& skin): gcn::Window(caption), mGrip(0), mParent(parent), mLayout(NULL), + mWindowName("window"), + mShowTitle(true), mModal(modal), mCloseButton(false), mSticky(false), mMinWinWidth(100), mMinWinHeight(40), mMaxWinWidth(INT_MAX), - mMaxWinHeight(INT_MAX) + mMaxWinHeight(INT_MAX), + mSkin(skin) { logger->log("Window::Window(\"%s\")", caption.c_str()); - if (!windowContainer) { + if (!windowContainer) + { throw GCN_EXCEPTION("Window::Window(): no windowContainer set"); } - loadSkin("graphics/gui/gui.xml"); + // Loads the skin + loadSkin(mSkin); + + setGuiAlpha(); - //if (instances == 0) - //{ - //WindowConfigListener = new WindowConfigListener(); + if (instances == 0) + { + windowConfigListener = new WindowConfigListener; // Send GUI alpha changed for initialization - //windowConfigListener->optionChanged("guialpha"); - //config.addListener("guialpha", windowConfigListener); - //} + windowConfigListener->optionChanged("guialpha"); + config.addListener("guialpha", windowConfigListener); + } instances++; @@ -106,8 +99,6 @@ Window::Window(const std::string& caption, bool modal, Window *parent): setPadding(3); setTitleBarHeight(20); - setGuiAlpha(); - // Add this window to the window container windowContainer->add(this); @@ -125,16 +116,18 @@ Window::Window(const std::string& caption, bool modal, Window *parent): Window::~Window() { - logger->log("UNLOAD: Window::~Window(\"%s\")", getCaption().c_str()); + logger->log("Window::~Window(\"%s\")", getCaption().c_str()); const std::string &name = mWindowName; // Saving X, Y and Width and Height for resizables in the config - if (!name.empty()) { + if (!name.empty()) + { config.setValue(name + "WinX", getX()); config.setValue(name + "WinY", getY()); config.setValue(name + "Visible", isVisible()); - if (mGrip) { + if (mGrip) + { config.setValue(name + "WinWidth", getWidth()); config.setValue(name + "WinHeight", getHeight()); } @@ -151,22 +144,21 @@ Window::~Window() instances--; + // Clean up static resources + for (int i = 0; i < 9; i++) + { + delete border.grid[i]; + border.grid[i] = NULL; + } + if (instances == 0) { config.removeListener("guialpha", windowConfigListener); delete windowConfigListener; windowConfigListener = NULL; - // Clean up static resources closeImage->decRef(); } - - // Clean up Border images. - for( int i = 0; i < 9; i++ ) - { - delete border[i]; - border[i] = NULL; - } } void Window::setWindowContainer(WindowContainer *wc) @@ -176,17 +168,12 @@ void Window::setWindowContainer(WindowContainer *wc) void Window::draw(gcn::Graphics *graphics) { - if (mAlphaChanged) - setGuiAlpha(); - Graphics *g = static_cast<Graphics*>(graphics); - //g->drawImageRect(0, 0, getWidth(), getHeight(), border); - - g->drawImageRect(0, 0, getWidth(), getHeight(), border[0], border[2], border[6], border[8], border[1], border[5], border[7], border[3], border[4]); + g->drawImageRect(0, 0, getWidth(), getHeight(), border); // Draw title - if (getTitleBarHeight()) + if (mShowTitle) { g->setColor(gcn::Color(0, 0, 0)); g->setFont(getFont()); @@ -201,6 +188,15 @@ void Window::draw(gcn::Graphics *graphics) getPadding() ); } + + // Update window alpha values + if (mAlphaChanged) + { + for_each(border.grid, border.grid + 9, + std::bind2nd(std::mem_fun(&Image::setAlpha), + config.getValue("guialpha", 0.8))); + closeImage->setAlpha(config.getValue("guialpha", 0.8)); + } drawChildren(graphics); } @@ -248,7 +244,7 @@ void Window::setResizable(bool r) if (r) { - mGrip = new ResizeGrip(); + mGrip = new ResizeGrip; mGrip->setX(getWidth() - mGrip->getWidth() - getChildrenArea().x); mGrip->setY(getHeight() - mGrip->getHeight() - getChildrenArea().y); add(mGrip); @@ -263,17 +259,16 @@ void Window::setResizable(bool r) void Window::widgetResized(const gcn::Event &event) { + const gcn::Rectangle area = getChildrenArea(); + if (mGrip) - { - const gcn::Rectangle area = getChildrenArea(); mGrip->setPosition(getWidth() - mGrip->getWidth() - area.x, getHeight() - mGrip->getHeight() - area.y); - } if (mLayout) { - int w = getWidth() - 2 * getPadding(); - int h = getHeight() - getPadding() - getTitleBarHeight(); + int w = area.width; + int h = area.height; mLayout->reflow(w, h); } } @@ -527,39 +522,13 @@ int Window::getResizeHandles(gcn::MouseEvent &event) return resizeHandles; } -Layout &Window::getLayout() -{ - if (!mLayout) mLayout = new Layout; - return *mLayout; -} - -LayoutCell &Window::place(int x, int y, gcn::Widget *wg, int w, int h) -{ - add(wg); - return getLayout().place(wg, x, y, w, h); -} - -ContainerPlacer Window::getPlacer(int x, int y) -{ - return ContainerPlacer(this, &getLayout().at(x, y)); -} - -void Window::reflowLayout(int w, int h) -{ - assert(mLayout); - mLayout->reflow(w, h); - delete mLayout; - mLayout = NULL; - setContentSize(w, h); -} - void Window::setGuiAlpha() { //logger->log("Window::setGuiAlpha: Alpha Value %f", config.getValue("guialpha", 0.8)); - for(int i = 0; i < 9; i++) + for (int i = 0; i < 9; i++) { //logger->log("Window::setGuiAlpha: Border Image (%i)", i); - border[i]->setAlpha(config.getValue("guialpha", 0.8)); + border.grid[i]->setAlpha(config.getValue("guialpha", 0.8)); } mAlphaChanged = false; @@ -567,13 +536,15 @@ void Window::setGuiAlpha() void Window::loadSkin(const std::string &fileName) { + const std::string windowId = Window::getId(); + ResourceManager *resman = ResourceManager::getInstance(); logger->log("Loading Window Skin '%s'.", fileName.c_str()); - logger->log("Loading Window ID '%s'.", Window::getId().c_str()); + logger->log("Loading Window ID '%s'.", windowId.c_str()); - if (fileName == "") + if (fileName.empty()) logger->error("Window::loadSkin(): Invalid File Name."); // TODO: @@ -591,7 +562,7 @@ void Window::loadSkin(const std::string &fileName) std::string skinSetImage; skinSetImage = XML::getProperty(rootNode, "image", ""); Image *dBorders = NULL; - if(skinSetImage != "") + if (!skinSetImage.empty()) { logger->log("Window::loadSkin(): <skinset> defines '%s' as a skin image.", skinSetImage.c_str()); dBorders = resman->getImage("graphics/gui/" + skinSetImage);//"graphics/gui/speech_bubble.png"); @@ -611,7 +582,7 @@ void Window::loadSkin(const std::string &fileName) widgetType = XML::getProperty(widgetNode, "type", "unknown"); if (widgetType == "Window") { - // Itarate through <part>'s + // Iterate through <part>'s // LEEOR / TODO: // We need to make provisions to load in a CloseButton image. For now it // can just be hard-coded. @@ -625,90 +596,90 @@ void Window::loadSkin(const std::string &fileName) std::string partType; partType = XML::getProperty(partNode, "type", "unknown"); // TOP ROW - if(partType == "top-left-corner") + if (partType == "top-left-corner") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[0] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[0] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "top-edge") + else if (partType == "top-edge") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[1] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[1] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "top-right-corner") + else if (partType == "top-right-corner") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[2] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[2] = dBorders->getSubImage(xPos, yPos, width, height); } // MIDDLE ROW - else if(partType == "left-edge") + else if (partType == "left-edge") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[3] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[3] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "bg-quad") + else if (partType == "bg-quad") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[4] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[4] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "right-edge") + else if (partType == "right-edge") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[5] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[5] = dBorders->getSubImage(xPos, yPos, width, height); } // BOTTOM ROW - else if(partType == "bottom-left-corner") + else if (partType == "bottom-left-corner") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[6] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[6] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "bottom-edge") + else if (partType == "bottom-edge") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[7] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[7] = dBorders->getSubImage(xPos, yPos, width, height); } - else if(partType == "bottom-right-corner") + else if (partType == "bottom-right-corner") { const int xPos = XML::getProperty(partNode, "xpos", 0); const int yPos = XML::getProperty(partNode, "ypos", 0); const int width = XML::getProperty(partNode, "width", 1); const int height = XML::getProperty(partNode, "height", 1); - border[8] = dBorders->getSubImage(xPos, yPos, width, height); + border.grid[8] = dBorders->getSubImage(xPos, yPos, width, height); } // Part is of an uknown type. @@ -731,3 +702,29 @@ void Window::loadSkin(const std::string &fileName) // Hard-coded for now until we update the above code to look for window buttons. closeImage = resman->getImage("graphics/gui/close_button.png"); } + +Layout &Window::getLayout() +{ + if (!mLayout) mLayout = new Layout; + return *mLayout; +} + +LayoutCell &Window::place(int x, int y, gcn::Widget *wg, int w, int h) +{ + add(wg); + return getLayout().place(wg, x, y, w, h); +} + +ContainerPlacer Window::getPlacer(int x, int y) +{ + return ContainerPlacer(this, &getLayout().at(x, y)); +} + +void Window::reflowLayout(int w, int h) +{ + assert(mLayout); + mLayout->reflow(w, h); + delete mLayout; + mLayout = NULL; + setContentSize(w, h); +} |