00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "gui/widgets/popup.h"
00024
00025 #include "gui/widgets/windowcontainer.h"
00026
00027 #include "gui/skin.h"
00028
00029 #include "configuration.h"
00030 #include "graphics.h"
00031 #include "log.h"
00032
00033 #include "resources/image.h"
00034
00035 #include <guichan/exception.hpp>
00036
00037 Popup::Popup(const std::string &name, const std::string &skin):
00038 mPopupName(name),
00039 mDefaultSkinPath(skin),
00040 mMinWidth(100),
00041 mMinHeight(40),
00042 mMaxWidth(graphics->getWidth()),
00043 mMaxHeight(graphics->getHeight())
00044 {
00045 logger->log("Popup::Popup(\"%s\")", name.c_str());
00046
00047 if (!windowContainer)
00048 throw GCN_EXCEPTION("Popup::Popup(): no windowContainer set");
00049
00050 setPadding(3);
00051
00052
00053 mSkin = SkinLoader::instance()->load(skin, mDefaultSkinPath);
00054
00055
00056 windowContainer->add(this);
00057
00058
00059 setVisible(false);
00060 }
00061
00062 Popup::~Popup()
00063 {
00064 logger->log("Popup::~Popup(\"%s\")", mPopupName.c_str());
00065
00066 savePopupConfiguration();
00067
00068 mSkin->instances--;
00069 }
00070
00071 void Popup::setWindowContainer(WindowContainer *wc)
00072 {
00073 windowContainer = wc;
00074 }
00075
00076 void Popup::loadPopupConfiguration()
00077 {
00078 if (mPopupName.empty())
00079 return;
00080
00081 const std::string &name = mPopupName;
00082 const std::string &skinName = config.getValue(name + "Skin",
00083 mSkin->getFilePath());
00084
00085 if (skinName.compare(mSkin->getFilePath()) != 0)
00086 {
00087 mSkin->instances--;
00088 mSkin = SkinLoader::instance()->load(skinName, mDefaultSkinPath);
00089 }
00090 }
00091
00092 void Popup::savePopupConfiguration()
00093 {
00094 if (mPopupName.empty())
00095 return;
00096
00097 const std::string &name = mPopupName;
00098
00099
00100
00101 config.setValue(name + "Skin", mSkin->getFilePath());
00102 }
00103
00104 void Popup::draw(gcn::Graphics *graphics)
00105 {
00106 Graphics *g = static_cast<Graphics*>(graphics);
00107
00108 g->drawImageRect(0, 0, getWidth(), getHeight(), mSkin->getBorder());
00109
00110 drawChildren(graphics);
00111 }
00112
00113 gcn::Rectangle Popup::getChildrenArea()
00114 {
00115 return gcn::Rectangle(getPadding(), 0, getWidth() - getPadding() * 2,
00116 getHeight() - getPadding() * 2);
00117 }
00118
00119 void Popup::setContentSize(int width, int height)
00120 {
00121 width += 2 * getPadding();
00122 height += 2 * getPadding();
00123
00124 if (getMinWidth() > width)
00125 width = getMinWidth();
00126 else if (getMaxWidth() < width)
00127 width = getMaxWidth();
00128 if (getMinHeight() > height)
00129 height = getMinHeight();
00130 else if (getMaxHeight() < height)
00131 height = getMaxHeight();
00132
00133 setSize(width, height);
00134 }
00135
00136 void Popup::setLocationRelativeTo(gcn::Widget *widget)
00137 {
00138 int wx, wy;
00139 int x, y;
00140
00141 widget->getAbsolutePosition(wx, wy);
00142 getAbsolutePosition(x, y);
00143
00144 setPosition(getX() + (wx + (widget->getWidth() - getWidth()) / 2 - x),
00145 getY() + (wy + (widget->getHeight() - getHeight()) / 2 - y));
00146 }
00147
00148 void Popup::setMinWidth(int width)
00149 {
00150 mMinWidth = width > mSkin->getMinWidth() ? width : mSkin->getMinWidth();
00151 }
00152
00153 void Popup::setMinHeight(int height)
00154 {
00155 mMinHeight = height > mSkin->getMinHeight() ? height : mSkin->getMinHeight();
00156 }
00157
00158 void Popup::setMaxWidth(int width)
00159 {
00160 mMaxWidth = width;
00161 }
00162
00163 void Popup::setMaxHeight(int height)
00164 {
00165 mMaxHeight = height;
00166 }
00167
00168 void Popup::scheduleDelete()
00169 {
00170 windowContainer->scheduleDelete(this);
00171 }
00172