diff --git a/apps/openmw/mwgui/bookpage.cpp b/apps/openmw/mwgui/bookpage.cpp index 8506f04ed..dbd829176 100644 --- a/apps/openmw/mwgui/bookpage.cpp +++ b/apps/openmw/mwgui/bookpage.cpp @@ -1252,72 +1252,65 @@ class BookPageImpl : public BookPage MYGUI_RTTI_DERIVED(BookPage) public: + BookPageImpl() + : mPageDisplay(NULL) + { + } + void showPage (TypesetBook::Ptr book, size_t page) { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - pd->showPage (book, page); - else - throw std::runtime_error ("The main sub-widget for a BookPage must be a PageDisplay."); + mPageDisplay->showPage (book, page); } void adviseLinkClicked (boost::function linkClicked) { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - { - pd->mLinkClicked = linkClicked; - } + mPageDisplay->mLinkClicked = linkClicked; } void unadviseLinkClicked () { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) + mPageDisplay->mLinkClicked = boost::function (); + } + +protected: + + virtual void initialiseOverride() + { + Base::initialiseOverride(); + + if (getSubWidgetText()) + { + mPageDisplay = getSubWidgetText()->castType(); + } + else { - pd->mLinkClicked = boost::function (); + throw std::runtime_error("BookPage unable to find page display sub widget"); } } -protected: void onMouseLostFocus(Widget* _new) { // NOTE: MyGUI also fires eventMouseLostFocus for widgets that are about to be destroyed (if they had focus). // Child widgets may already be destroyed! So be careful. - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - { - pd->onMouseLostFocus (); - } - else - Widget::onMouseLostFocus (_new); + mPageDisplay->onMouseLostFocus (); } void onMouseMove(int left, int top) { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - { - pd->onMouseMove (left, top); - } - else - Widget::onMouseMove (left, top); + mPageDisplay->onMouseMove (left, top); } void onMouseButtonPressed (int left, int top, MyGUI::MouseButton id) { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - { - pd->onMouseButtonPressed (left, top, id); - } - else - Widget::onMouseButtonPressed (left, top, id); + mPageDisplay->onMouseButtonPressed (left, top, id); } void onMouseButtonReleased(int left, int top, MyGUI::MouseButton id) { - if (PageDisplay* pd = dynamic_cast (getSubWidgetText ())) - { - pd->onMouseButtonReleased (left, top, id); - } - else - Widget::onMouseButtonReleased (left, top, id); + mPageDisplay->onMouseButtonReleased (left, top, id); } + + PageDisplay* mPageDisplay; }; void BookPage::registerMyGUIComponents ()