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