Added wxTreebook:
[wxWidgets.git] / src / xrc / xh_treebk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/treebook.h"
23 #include "wx/imaglist.h"
24 #include "wx/log.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxTreebookXmlHandler, wxXmlResourceHandler)
27
28 wxTreebookXmlHandler::wxTreebookXmlHandler()
29 : wxXmlResourceHandler(), m_isInside(false), m_tbk(NULL), m_treeContext()
30 {
31 XRC_ADD_STYLE(wxTBK_DEFAULT);
32 XRC_ADD_STYLE(wxTBK_LEFT);
33 XRC_ADD_STYLE(wxTBK_RIGHT);
34
35 AddWindowStyles();
36 }
37
38 bool wxTreebookXmlHandler::CanHandle(wxXmlNode *node)
39 {
40 return ((!m_isInside && IsOfClass(node, wxT("wxTreebook"))) ||
41 (m_isInside && IsOfClass(node, wxT("treebookpage"))));
42 }
43
44
45 wxObject *wxTreebookXmlHandler::DoCreateResource()
46 {
47 if (m_class == wxT("wxTreebook"))
48 {
49 XRC_MAKE_INSTANCE(tbk, wxTreebook)
50
51 tbk->Create(m_parentAsWindow,
52 GetID(),
53 GetPosition(), GetSize(),
54 GetStyle(wxT("style")),
55 GetName());
56
57 wxTreebook * old_par = m_tbk;
58 m_tbk = tbk;
59
60 bool old_ins = m_isInside;
61 m_isInside = true;
62
63 wxArrayTbkPageIndexes old_treeContext = m_treeContext;
64 m_treeContext.Clear();
65
66 CreateChildren(m_tbk, true/*only this handler*/);
67
68 m_treeContext = old_treeContext;
69 m_isInside = old_ins;
70 m_tbk = old_par;
71
72 return tbk;
73 }
74
75 // else ( m_class == wxT("treebookpage") )
76 wxXmlNode *n = GetParamNode(wxT("object"));
77 wxWindow *wnd = NULL;
78
79 if ( !n )
80 n = GetParamNode(wxT("object_ref"));
81
82 if (n)
83 {
84 bool old_ins = m_isInside;
85 m_isInside = false;
86 wxObject *item = CreateResFromNode(n, m_tbk, NULL);
87 m_isInside = old_ins;
88 wnd = wxDynamicCast(item, wxWindow);
89
90 if (wnd == NULL && item != NULL)
91 wxLogError(wxT("Error in resource: control within treebook's <page> tag is not a window."));
92 }
93
94 size_t depth = GetLong( wxT("depth") );
95
96 if( depth <= m_treeContext.Count() )
97 {
98 // first prepare the icon
99 int imgIndex = wxNOT_FOUND;
100 if ( HasParam(wxT("bitmap")) )
101 {
102 wxBitmap bmp = GetBitmap(wxT("bitmap"), wxART_OTHER);
103 wxImageList *imgList = m_tbk->GetImageList();
104 if ( imgList == NULL )
105 {
106 imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() );
107 m_tbk->AssignImageList( imgList );
108 }
109 imgIndex = imgList->Add(bmp);
110 }
111
112 // then add the page to the corresponding parent
113 if( depth < m_treeContext.Count() )
114 m_treeContext.RemoveAt(depth, m_treeContext.Count() - depth );
115 if( depth == 0)
116 {
117 m_tbk->AddPage(wnd,
118 GetText(wxT("label")), GetBool(wxT("selected")), imgIndex);
119 }
120 else
121 {
122 m_tbk->AddSubPage(m_treeContext.Item(depth - 1), wnd,
123 GetText(wxT("label")), GetBool(wxT("selected")), imgIndex);
124 }
125
126 m_treeContext.Add( m_tbk->GetPageCount() - 1);
127
128 }
129 else
130 wxLogError(wxT("Error in resource. wxTreebookPage has an invalid depth."));
131 return wnd;
132 }
133
134 #endif // wxUSE_XRC && wxUSE_TREEBOOK