1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/xrc/xh_toolb.cpp
3 // Purpose:     XRC resource for wxToolBar
4 // Author:      Vaclav Slavik
5 // Created:     2000/08/11
6 // RCS-ID:      $Id: xh_toolb.cpp 55979 2008-09-30 14:37:26Z VS $
7 // Copyright:   (c) 2000 Vaclav Slavik
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_TOOLBAR
19 
20 #include "wx/xrc/xh_toolb.h"
21 
22 #ifndef WX_PRECOMP
23     #include "wx/frame.h"
24     #include "wx/toolbar.h"
25 #endif
26 
IMPLEMENT_DYNAMIC_CLASS(wxToolBarXmlHandler,wxXmlResourceHandler)27 IMPLEMENT_DYNAMIC_CLASS(wxToolBarXmlHandler, wxXmlResourceHandler)
28 
29 wxToolBarXmlHandler::wxToolBarXmlHandler()
30 : wxXmlResourceHandler(), m_isInside(false), m_toolbar(NULL)
31 {
32     XRC_ADD_STYLE(wxTB_FLAT);
33     XRC_ADD_STYLE(wxTB_DOCKABLE);
34     XRC_ADD_STYLE(wxTB_VERTICAL);
35     XRC_ADD_STYLE(wxTB_HORIZONTAL);
36     XRC_ADD_STYLE(wxTB_3DBUTTONS);
37     XRC_ADD_STYLE(wxTB_TEXT);
38     XRC_ADD_STYLE(wxTB_NOICONS);
39     XRC_ADD_STYLE(wxTB_NODIVIDER);
40     XRC_ADD_STYLE(wxTB_NOALIGN);
41     XRC_ADD_STYLE(wxTB_HORZ_LAYOUT);
42     XRC_ADD_STYLE(wxTB_HORZ_TEXT);
43 
44     XRC_ADD_STYLE(wxTB_TOP);
45     XRC_ADD_STYLE(wxTB_LEFT);
46     XRC_ADD_STYLE(wxTB_RIGHT);
47     XRC_ADD_STYLE(wxTB_BOTTOM);
48 
49     AddWindowStyles();
50 }
51 
DoCreateResource()52 wxObject *wxToolBarXmlHandler::DoCreateResource()
53 {
54     if (m_class == wxT("tool"))
55     {
56         wxCHECK_MSG(m_toolbar, NULL, wxT("Incorrect syntax of XRC resource: tool not within a toolbar!"));
57 
58         if (GetPosition() != wxDefaultPosition)
59         {
60             m_toolbar->AddTool(GetID(),
61                                GetBitmap(wxT("bitmap"), wxART_TOOLBAR),
62                                GetBitmap(wxT("bitmap2"), wxART_TOOLBAR),
63                                GetBool(wxT("toggle")),
64                                GetPosition().x,
65                                GetPosition().y,
66                                NULL,
67                                GetText(wxT("tooltip")),
68                                GetText(wxT("longhelp")));
69         }
70         else
71         {
72             wxItemKind kind = wxITEM_NORMAL;
73             if (GetBool(wxT("radio")))
74                 kind = wxITEM_RADIO;
75             if (GetBool(wxT("toggle")))
76             {
77                 wxASSERT_MSG( kind == wxITEM_NORMAL,
78                               _T("can't have both toggleable and radion button at once") );
79                 kind = wxITEM_CHECK;
80             }
81             m_toolbar->AddTool(GetID(),
82                                GetText(wxT("label")),
83                                GetBitmap(wxT("bitmap"), wxART_TOOLBAR),
84                                GetBitmap(wxT("bitmap2"), wxART_TOOLBAR),
85                                kind,
86                                GetText(wxT("tooltip")),
87                                GetText(wxT("longhelp")));
88 
89             if ( GetBool(wxT("disabled")) )
90                 m_toolbar->EnableTool(GetID(), false);
91         }
92         return m_toolbar; // must return non-NULL
93     }
94 
95     else if (m_class == wxT("separator"))
96     {
97         wxCHECK_MSG(m_toolbar, NULL, wxT("Incorrect syntax of XRC resource: separator not within a toolbar!"));
98         m_toolbar->AddSeparator();
99         return m_toolbar; // must return non-NULL
100     }
101 
102     else /*<object class="wxToolBar">*/
103     {
104         int style = GetStyle(wxT("style"), wxNO_BORDER | wxTB_HORIZONTAL);
105 #ifdef __WXMSW__
106         if (!(style & wxNO_BORDER)) style |= wxNO_BORDER;
107 #endif
108 
109         XRC_MAKE_INSTANCE(toolbar, wxToolBar)
110 
111         toolbar->Create(m_parentAsWindow,
112                          GetID(),
113                          GetPosition(),
114                          GetSize(),
115                          style,
116                          GetName());
117         SetupWindow(toolbar);
118 
119         wxSize bmpsize = GetSize(wxT("bitmapsize"));
120         if (!(bmpsize == wxDefaultSize))
121             toolbar->SetToolBitmapSize(bmpsize);
122         wxSize margins = GetSize(wxT("margins"));
123         if (!(margins == wxDefaultSize))
124             toolbar->SetMargins(margins.x, margins.y);
125         long packing = GetLong(wxT("packing"), -1);
126         if (packing != -1)
127             toolbar->SetToolPacking(packing);
128         long separation = GetLong(wxT("separation"), -1);
129         if (separation != -1)
130             toolbar->SetToolSeparation(separation);
131 
132         wxXmlNode *children_node = GetParamNode(wxT("object"));
133         if (!children_node)
134            children_node = GetParamNode(wxT("object_ref"));
135 
136         if (children_node == NULL) return toolbar;
137 
138         m_isInside = true;
139         m_toolbar = toolbar;
140 
141         wxXmlNode *n = children_node;
142 
143         while (n)
144         {
145             if ((n->GetType() == wxXML_ELEMENT_NODE) &&
146                 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
147             {
148                 wxObject *created = CreateResFromNode(n, toolbar, NULL);
149                 wxControl *control = wxDynamicCast(created, wxControl);
150                 if (!IsOfClass(n, wxT("tool")) &&
151                     !IsOfClass(n, wxT("separator")) &&
152                     control != NULL)
153                     toolbar->AddControl(control);
154             }
155             n = n->GetNext();
156         }
157 
158         m_isInside = false;
159         m_toolbar = NULL;
160 
161         toolbar->Realize();
162 
163         if (m_parentAsWindow && !GetBool(wxT("dontattachtoframe")))
164         {
165             wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
166             if (parentFrame)
167                 parentFrame->SetToolBar(toolbar);
168         }
169 
170         return toolbar;
171     }
172 }
173 
CanHandle(wxXmlNode * node)174 bool wxToolBarXmlHandler::CanHandle(wxXmlNode *node)
175 {
176     return ((!m_isInside && IsOfClass(node, wxT("wxToolBar"))) ||
177             (m_isInside && IsOfClass(node, wxT("tool"))) ||
178             (m_isInside && IsOfClass(node, wxT("separator"))));
179 }
180 
181 #endif // wxUSE_XRC && wxUSE_TOOLBAR
182