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 // Copyright:   (c) 2005 TT-Solutions <vadim@tt-solutions.com>
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12 
13 #ifdef __BORLANDC__
14     #pragma hdrstop
15 #endif
16 
17 #if wxUSE_XRC && wxUSE_TREEBOOK
18 
19 #include "wx/xrc/xh_treebk.h"
20 
21 #ifndef WX_PRECOMP
22     #include "wx/log.h"
23 #endif
24 
25 #include "wx/treebook.h"
26 #include "wx/imaglist.h"
27 
28 #include "wx/xml/xml.h"
29 
IMPLEMENT_DYNAMIC_CLASS(wxTreebookXmlHandler,wxXmlResourceHandler)30 IMPLEMENT_DYNAMIC_CLASS(wxTreebookXmlHandler, wxXmlResourceHandler)
31 
32 wxTreebookXmlHandler::wxTreebookXmlHandler()
33                     : wxXmlResourceHandler(),
34                       m_tbk(NULL),
35                       m_isInside(false)
36 {
37     XRC_ADD_STYLE(wxBK_DEFAULT);
38     XRC_ADD_STYLE(wxBK_TOP);
39     XRC_ADD_STYLE(wxBK_BOTTOM);
40     XRC_ADD_STYLE(wxBK_LEFT);
41     XRC_ADD_STYLE(wxBK_RIGHT);
42 
43     AddWindowStyles();
44 }
45 
CanHandle(wxXmlNode * node)46 bool wxTreebookXmlHandler::CanHandle(wxXmlNode *node)
47 {
48     return ((!m_isInside && IsOfClass(node, wxT("wxTreebook"))) ||
49             (m_isInside && IsOfClass(node, wxT("treebookpage"))));
50 }
51 
52 
DoCreateResource()53 wxObject *wxTreebookXmlHandler::DoCreateResource()
54 {
55     if (m_class == wxT("wxTreebook"))
56     {
57         XRC_MAKE_INSTANCE(tbk, wxTreebook)
58 
59         tbk->Create(m_parentAsWindow,
60                     GetID(),
61                     GetPosition(), GetSize(),
62                     GetStyle(wxT("style")),
63                     GetName());
64 
65         wxImageList *imagelist = GetImageList();
66         if ( imagelist )
67             tbk->AssignImageList(imagelist);
68 
69         wxTreebook * old_par = m_tbk;
70         m_tbk = tbk;
71 
72         bool old_ins = m_isInside;
73         m_isInside = true;
74 
75         wxArrayTbkPageIndexes old_treeContext = m_treeContext;
76         m_treeContext.Clear();
77 
78         CreateChildren(m_tbk, true/*only this handler*/);
79 
80         wxXmlNode *node = GetParamNode("object");
81         int pageIndex = 0;
82         for (unsigned int i = 0; i < m_tbk->GetPageCount(); i++)
83         {
84             if ( m_tbk->GetPage(i) )
85             {
86                 wxXmlNode *child = node->GetChildren();
87                 while (child)
88                 {
89                     if (child->GetName() == "expanded" && child->GetNodeContent() == "1")
90                         m_tbk->ExpandNode(pageIndex, true);
91 
92                     child = child->GetNext();
93                 }
94                 pageIndex++;
95             }
96         }
97 
98         m_treeContext = old_treeContext;
99         m_isInside = old_ins;
100         m_tbk = old_par;
101 
102         return tbk;
103     }
104 
105 //    else ( m_class == wxT("treebookpage") )
106     wxXmlNode *n = GetParamNode(wxT("object"));
107     wxWindow *wnd = NULL;
108 
109     if ( !n )
110         n = GetParamNode(wxT("object_ref"));
111 
112     if (n)
113     {
114         bool old_ins = m_isInside;
115         m_isInside = false;
116         wxObject *item = CreateResFromNode(n, m_tbk, NULL);
117         m_isInside = old_ins;
118         wnd = wxDynamicCast(item, wxWindow);
119 
120         if (wnd == NULL && item != NULL)
121         {
122             ReportError(n, "treebookpage child must be a window");
123         }
124     }
125 
126     size_t depth = GetLong( wxT("depth") );
127 
128     if( depth <= m_treeContext.GetCount() )
129     {
130         // first prepare the icon
131         int imgIndex = wxNOT_FOUND;
132         if ( HasParam(wxT("bitmap")) )
133         {
134             wxBitmap bmp = GetBitmap(wxT("bitmap"), wxART_OTHER);
135             wxImageList *imgList = m_tbk->GetImageList();
136             if ( imgList == NULL )
137             {
138                 imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() );
139                 m_tbk->AssignImageList( imgList );
140             }
141             imgIndex = imgList->Add(bmp);
142         }
143         else if ( HasParam(wxT("image")) )
144         {
145             if ( m_tbk->GetImageList() )
146             {
147                 imgIndex = GetLong(wxT("image"));
148             }
149             else // image without image list?
150             {
151                 ReportError(n, "image can only be used in conjunction "
152                                "with imagelist");
153             }
154         }
155 
156         // then add the page to the corresponding parent
157         if( depth < m_treeContext.GetCount() )
158             m_treeContext.RemoveAt(depth, m_treeContext.GetCount() - depth );
159         if( depth == 0)
160         {
161             m_tbk->AddPage(wnd,
162                 GetText(wxT("label")), GetBool(wxT("selected")), imgIndex);
163         }
164         else
165         {
166             m_tbk->InsertSubPage(m_treeContext.Item(depth - 1), wnd,
167                 GetText(wxT("label")), GetBool(wxT("selected")), imgIndex);
168         }
169 
170         m_treeContext.Add( m_tbk->GetPageCount() - 1);
171 
172     }
173     else
174     {
175         ReportParamError("depth", "invalid depth");
176     }
177 
178     return wnd;
179 }
180 
181 #endif // wxUSE_XRC && wxUSE_TREEBOOK
182