| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/xrc/xh_treebk.cpp |
| 3 | // Purpose: XRC resource handler for wxTreebook |
| 4 | // Author: Evgeniy Tarassov |
| 5 | // Created: 2005/09/28 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2005 TT-Solutions <vadim@tt-solutions.com> |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // For compilers that support precompilation, includes "wx.h". |
| 12 | #include "wx/wxprec.h" |
| 13 | |
| 14 | #ifdef __BORLANDC__ |
| 15 | #pragma hdrstop |
| 16 | #endif |
| 17 | |
| 18 | #if wxUSE_XRC && wxUSE_TREEBOOK |
| 19 | |
| 20 | #include "wx/xrc/xh_treebk.h" |
| 21 | |
| 22 | #ifndef WX_PRECOMP |
| 23 | #include "wx/log.h" |
| 24 | #endif |
| 25 | |
| 26 | #include "wx/treebook.h" |
| 27 | #include "wx/imaglist.h" |
| 28 | |
| 29 | IMPLEMENT_DYNAMIC_CLASS(wxTreebookXmlHandler, wxXmlResourceHandler) |
| 30 | |
| 31 | wxTreebookXmlHandler::wxTreebookXmlHandler() |
| 32 | : wxXmlResourceHandler(), |
| 33 | m_tbk(NULL), |
| 34 | m_isInside(false) |
| 35 | { |
| 36 | XRC_ADD_STYLE(wxBK_DEFAULT); |
| 37 | XRC_ADD_STYLE(wxBK_TOP); |
| 38 | XRC_ADD_STYLE(wxBK_BOTTOM); |
| 39 | XRC_ADD_STYLE(wxBK_LEFT); |
| 40 | XRC_ADD_STYLE(wxBK_RIGHT); |
| 41 | |
| 42 | AddWindowStyles(); |
| 43 | } |
| 44 | |
| 45 | bool wxTreebookXmlHandler::CanHandle(wxXmlNode *node) |
| 46 | { |
| 47 | return ((!m_isInside && IsOfClass(node, wxT("wxTreebook"))) || |
| 48 | (m_isInside && IsOfClass(node, wxT("treebookpage")))); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | wxObject *wxTreebookXmlHandler::DoCreateResource() |
| 53 | { |
| 54 | if (m_class == wxT("wxTreebook")) |
| 55 | { |
| 56 | XRC_MAKE_INSTANCE(tbk, wxTreebook) |
| 57 | |
| 58 | tbk->Create(m_parentAsWindow, |
| 59 | GetID(), |
| 60 | GetPosition(), GetSize(), |
| 61 | GetStyle(wxT("style")), |
| 62 | GetName()); |
| 63 | |
| 64 | wxTreebook * old_par = m_tbk; |
| 65 | m_tbk = tbk; |
| 66 | |
| 67 | bool old_ins = m_isInside; |
| 68 | m_isInside = true; |
| 69 | |
| 70 | wxArrayTbkPageIndexes old_treeContext = m_treeContext; |
| 71 | m_treeContext.Clear(); |
| 72 | |
| 73 | CreateChildren(m_tbk, true/*only this handler*/); |
| 74 | |
| 75 | m_treeContext = old_treeContext; |
| 76 | m_isInside = old_ins; |
| 77 | m_tbk = old_par; |
| 78 | |
| 79 | return tbk; |
| 80 | } |
| 81 | |
| 82 | // else ( m_class == wxT("treebookpage") ) |
| 83 | wxXmlNode *n = GetParamNode(wxT("object")); |
| 84 | wxWindow *wnd = NULL; |
| 85 | |
| 86 | if ( !n ) |
| 87 | n = GetParamNode(wxT("object_ref")); |
| 88 | |
| 89 | if (n) |
| 90 | { |
| 91 | bool old_ins = m_isInside; |
| 92 | m_isInside = false; |
| 93 | wxObject *item = CreateResFromNode(n, m_tbk, NULL); |
| 94 | m_isInside = old_ins; |
| 95 | wnd = wxDynamicCast(item, wxWindow); |
| 96 | |
| 97 | if (wnd == NULL && item != NULL) |
| 98 | wxLogError(wxT("Error in resource: control within treebook's <page> tag is not a window.")); |
| 99 | } |
| 100 | |
| 101 | size_t depth = GetLong( wxT("depth") ); |
| 102 | |
| 103 | if( depth <= m_treeContext.GetCount() ) |
| 104 | { |
| 105 | // first prepare the icon |
| 106 | int imgIndex = wxNOT_FOUND; |
| 107 | if ( HasParam(wxT("bitmap")) ) |
| 108 | { |
| 109 | wxBitmap bmp = GetBitmap(wxT("bitmap"), wxART_OTHER); |
| 110 | wxImageList *imgList = m_tbk->GetImageList(); |
| 111 | if ( imgList == NULL ) |
| 112 | { |
| 113 | imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() ); |
| 114 | m_tbk->AssignImageList( imgList ); |
| 115 | } |
| 116 | imgIndex = imgList->Add(bmp); |
| 117 | } |
| 118 | |
| 119 | // then add the page to the corresponding parent |
| 120 | if( depth < m_treeContext.GetCount() ) |
| 121 | m_treeContext.RemoveAt(depth, m_treeContext.GetCount() - depth ); |
| 122 | if( depth == 0) |
| 123 | { |
| 124 | m_tbk->AddPage(wnd, |
| 125 | GetText(wxT("label")), GetBool(wxT("selected")), imgIndex); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | m_tbk->InsertSubPage(m_treeContext.Item(depth - 1), wnd, |
| 130 | GetText(wxT("label")), GetBool(wxT("selected")), imgIndex); |
| 131 | } |
| 132 | |
| 133 | m_treeContext.Add( m_tbk->GetPageCount() - 1); |
| 134 | |
| 135 | } |
| 136 | else |
| 137 | wxLogError(wxT("Error in resource. wxTreebookPage has an invalid depth.")); |
| 138 | return wnd; |
| 139 | } |
| 140 | |
| 141 | #endif // wxUSE_XRC && wxUSE_TREEBOOK |