OpenMW
libs/openengine/gui/layout.hpp
Go to the documentation of this file.
00001 #ifndef OENGINE_MYGUI_LAYOUT_H
00002 #define OENGINE_MYGUI_LAYOUT_H
00003 
00004 #include <MyGUI.h>
00005 
00006 namespace OEngine {
00007 namespace GUI
00008 {
00012   class Layout
00013   {
00014   public:
00015     Layout(const std::string & _layout, MyGUI::Widget* _parent = nullptr)
00016       : mMainWidget(nullptr)
00017     { initialise(_layout, _parent); }
00018     virtual ~Layout() { shutdown();  }
00019 
00020     template <typename T>
00021     void getWidget(T * & _widget, const std::string & _name, bool _throw = true)
00022     {
00023       _widget = nullptr;
00024       for (MyGUI::VectorWidgetPtr::iterator iter=mListWindowRoot.begin();
00025            iter!=mListWindowRoot.end(); ++iter)
00026         {
00027           MyGUI::Widget* find = (*iter)->findWidget(mPrefix + _name);
00028           if (nullptr != find)
00029             {
00030               T * cast = find->castType<T>(false);
00031               if (nullptr != cast)
00032                 _widget = cast;
00033               else if (_throw)
00034                 {
00035                   MYGUI_EXCEPT("Error cast : dest type = '" << T::getClassTypeName()
00036                                << "' source name = '" << find->getName()
00037                                << "' source type = '" << find->getTypeName() << "' in layout '" << mLayoutName << "'");
00038                 }
00039               return;
00040             }
00041         }
00042       MYGUI_ASSERT( ! _throw, "widget name '" << _name << "' in layout '" << mLayoutName << "' not found.");
00043     }
00044 
00045     void initialise(const std::string & _layout,
00046                     MyGUI::Widget* _parent = nullptr)
00047     {
00048       const std::string MAIN_WINDOW = "_Main";
00049       mLayoutName = _layout;
00050 
00051       if (mLayoutName.empty())
00052         mMainWidget = _parent;
00053       else
00054         {
00055           mPrefix = MyGUI::utility::toString(this, "_");
00056           mListWindowRoot = MyGUI::LayoutManager::getInstance().loadLayout(mLayoutName, mPrefix, _parent);
00057 
00058           const std::string main_name = mPrefix + MAIN_WINDOW;
00059           for (MyGUI::VectorWidgetPtr::iterator iter=mListWindowRoot.begin(); iter!=mListWindowRoot.end(); ++iter)
00060             {
00061               if ((*iter)->getName() == main_name)
00062                 {
00063                   mMainWidget = (*iter);
00064                   break;
00065                 }
00066             }
00067           MYGUI_ASSERT(mMainWidget, "root widget name '" << MAIN_WINDOW << "' in layout '" << mLayoutName << "' not found.");
00068         }
00069     }
00070 
00071     void shutdown()
00072     {
00073       MyGUI::Gui::getInstance().destroyWidget(mMainWidget);
00074       mListWindowRoot.clear();
00075     }
00076 
00077     void setCoord(int x, int y, int w, int h)
00078     {
00079       mMainWidget->setCoord(x,y,w,h);
00080     }
00081 
00082     void adjustWindowCaption()
00083     {
00084       // adjust the size of the window caption so that all text is visible
00085       // NOTE: this assumes that mMainWidget is of type Window.
00086       MyGUI::TextBox* box = static_cast<MyGUI::Window*>(mMainWidget)->getCaptionWidget();
00087       box->setSize(box->getTextSize().width + 24, box->getSize().height);
00088 
00089       // in order to trigger alignment updates, we need to update the parent
00090       // mygui doesn't provide a proper way of doing this, so we are just changing size
00091       box->getParent()->setCoord(MyGUI::IntCoord(
00092           box->getParent()->getCoord().left,
00093           box->getParent()->getCoord().top,
00094           box->getParent()->getCoord().width,
00095           box->getParent()->getCoord().height+1
00096       ));
00097       box->getParent()->setCoord(MyGUI::IntCoord(
00098           box->getParent()->getCoord().left,
00099           box->getParent()->getCoord().top,
00100           box->getParent()->getCoord().width,
00101           box->getParent()->getCoord().height-1
00102       ));
00103     }
00104 
00105     virtual void setVisible(bool b)
00106     {
00107       mMainWidget->setVisible(b);
00108     }
00109 
00110     void setText(const std::string& name, const std::string& caption)
00111     {
00112       MyGUI::Widget* pt;
00113       getWidget(pt, name);
00114       static_cast<MyGUI::TextBox*>(pt)->setCaption(caption);
00115     }
00116 
00117     void setTitle(const std::string& title)
00118     {
00119       // NOTE: this assume that mMainWidget is of type Window.
00120       static_cast<MyGUI::Window*>(mMainWidget)->setCaptionWithReplacing(title);
00121       adjustWindowCaption();
00122     }
00123 
00124     void setState(const std::string& widget, const std::string& state)
00125     {
00126       MyGUI::Widget* pt;
00127       getWidget(pt, widget);
00128       pt->_setWidgetState(state);
00129     }
00130 
00131     void setTextColor(const std::string& name, float r, float g, float b)
00132     {
00133       MyGUI::Widget* pt;
00134       getWidget(pt, name);
00135       MyGUI::TextBox *st = dynamic_cast<MyGUI::TextBox*>(pt);
00136       if(st != NULL)
00137         st->setTextColour(MyGUI::Colour(b,g,r));
00138     }
00139 
00140     void setImage(const std::string& name, const std::string& imgName)
00141     {
00142       MyGUI::ImageBox* pt;
00143       getWidget(pt, name);
00144       pt->setImageTexture(imgName);
00145     }
00146 
00147     void adjustButtonSize(MyGUI::Button* button)
00148     {
00149       // adjust size of button to fit its text
00150       MyGUI::IntSize size = button->getTextSize();
00151       button->setSize(size.width + 24, button->getSize().height);
00152     }
00153     MyGUI::Widget* mMainWidget;
00154 
00155   protected:
00156 
00157     std::string mPrefix;
00158     std::string mLayoutName;
00159     MyGUI::VectorWidgetPtr mListWindowRoot;
00160   };
00161 }}
00162 #endif