make sure it is compilable w/o menus (eg on iPhone)
[wxWidgets.git] / src / xrc / xh_toolb.cpp
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$
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/log.h"
25 #include "wx/menu.h"
26 #include "wx/toolbar.h"
27 #endif
28
29 IMPLEMENT_DYNAMIC_CLASS(wxToolBarXmlHandler, wxXmlResourceHandler)
30
31 wxToolBarXmlHandler::wxToolBarXmlHandler()
32 : wxXmlResourceHandler(), m_isInside(false), m_toolbar(NULL)
33 {
34 XRC_ADD_STYLE(wxTB_FLAT);
35 XRC_ADD_STYLE(wxTB_DOCKABLE);
36 XRC_ADD_STYLE(wxTB_VERTICAL);
37 XRC_ADD_STYLE(wxTB_HORIZONTAL);
38 XRC_ADD_STYLE(wxTB_3DBUTTONS);
39 XRC_ADD_STYLE(wxTB_TEXT);
40 XRC_ADD_STYLE(wxTB_NOICONS);
41 XRC_ADD_STYLE(wxTB_NODIVIDER);
42 XRC_ADD_STYLE(wxTB_NOALIGN);
43 XRC_ADD_STYLE(wxTB_HORZ_LAYOUT);
44 XRC_ADD_STYLE(wxTB_HORZ_TEXT);
45
46 XRC_ADD_STYLE(wxTB_TOP);
47 XRC_ADD_STYLE(wxTB_LEFT);
48 XRC_ADD_STYLE(wxTB_RIGHT);
49 XRC_ADD_STYLE(wxTB_BOTTOM);
50
51 AddWindowStyles();
52 }
53
54 wxObject *wxToolBarXmlHandler::DoCreateResource()
55 {
56 if (m_class == wxT("tool"))
57 {
58 if ( !m_toolbar )
59 {
60 ReportError("tool only allowed inside a wxToolBar");
61 return NULL;
62 }
63
64 wxItemKind kind = wxITEM_NORMAL;
65 if (GetBool(wxT("radio")))
66 kind = wxITEM_RADIO;
67
68 if (GetBool(wxT("toggle")))
69 {
70 if ( kind != wxITEM_NORMAL )
71 {
72 ReportParamError
73 (
74 "toggle",
75 "tool can't have both <radio> and <toggle> properties"
76 );
77 }
78
79 kind = wxITEM_CHECK;
80 }
81 #if wxUSE_MENUS
82 // check whether we have dropdown tag inside
83 wxMenu *menu = NULL; // menu for drop down items
84 wxXmlNode * const nodeDropdown = GetParamNode("dropdown");
85 if ( nodeDropdown )
86 {
87 if ( kind != wxITEM_NORMAL )
88 {
89 ReportParamError
90 (
91 "dropdown",
92 "drop-down tool can't have neither <radio> nor <toggle> properties"
93 );
94 }
95
96 kind = wxITEM_DROPDOWN;
97
98 // also check for the menu specified inside dropdown (it is
99 // optional and may be absent for e.g. dynamically-created
100 // menus)
101 wxXmlNode * const nodeMenu = nodeDropdown->GetChildren();
102 if ( nodeMenu )
103 {
104 wxObject *res = CreateResFromNode(nodeMenu, NULL);
105 menu = wxDynamicCast(res, wxMenu);
106 if ( !menu )
107 {
108 ReportError
109 (
110 nodeMenu,
111 "drop-down tool contents can only be a wxMenu"
112 );
113 }
114
115 if ( nodeMenu->GetNext() )
116 {
117 ReportError
118 (
119 nodeMenu->GetNext(),
120 "unexpected extra contents under drop-down tool"
121 );
122 }
123 }
124 }
125 #endif
126 wxToolBarToolBase * const tool =
127 m_toolbar->AddTool
128 (
129 GetID(),
130 GetText(wxT("label")),
131 GetBitmap(wxT("bitmap"), wxART_TOOLBAR, m_toolSize),
132 GetBitmap(wxT("bitmap2"), wxART_TOOLBAR, m_toolSize),
133 kind,
134 GetText(wxT("tooltip")),
135 GetText(wxT("longhelp"))
136 );
137
138 if ( GetBool(wxT("disabled")) )
139 m_toolbar->EnableTool(GetID(), false);
140 #if wxUSE_MENUS
141 if ( menu )
142 tool->SetDropdownMenu(menu);
143 #endif
144
145 return m_toolbar; // must return non-NULL
146 }
147
148 else if (m_class == wxT("separator"))
149 {
150 if ( !m_toolbar )
151 {
152 ReportError("separator only allowed inside wxToolBar");
153 return NULL;
154 }
155 m_toolbar->AddSeparator();
156 return m_toolbar; // must return non-NULL
157 }
158
159 else /*<object class="wxToolBar">*/
160 {
161 int style = GetStyle(wxT("style"), wxNO_BORDER | wxTB_HORIZONTAL);
162 #ifdef __WXMSW__
163 if (!(style & wxNO_BORDER)) style |= wxNO_BORDER;
164 #endif
165
166 XRC_MAKE_INSTANCE(toolbar, wxToolBar)
167
168 toolbar->Create(m_parentAsWindow,
169 GetID(),
170 GetPosition(),
171 GetSize(),
172 style,
173 GetName());
174 SetupWindow(toolbar);
175
176 m_toolSize = GetSize(wxT("bitmapsize"));
177 if (!(m_toolSize == wxDefaultSize))
178 toolbar->SetToolBitmapSize(m_toolSize);
179 wxSize margins = GetSize(wxT("margins"));
180 if (!(margins == wxDefaultSize))
181 toolbar->SetMargins(margins.x, margins.y);
182 long packing = GetLong(wxT("packing"), -1);
183 if (packing != -1)
184 toolbar->SetToolPacking(packing);
185 long separation = GetLong(wxT("separation"), -1);
186 if (separation != -1)
187 toolbar->SetToolSeparation(separation);
188
189 wxXmlNode *children_node = GetParamNode(wxT("object"));
190 if (!children_node)
191 children_node = GetParamNode(wxT("object_ref"));
192
193 if (children_node == NULL) return toolbar;
194
195 m_isInside = true;
196 m_toolbar = toolbar;
197
198 wxXmlNode *n = children_node;
199
200 while (n)
201 {
202 if ((n->GetType() == wxXML_ELEMENT_NODE) &&
203 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
204 {
205 wxObject *created = CreateResFromNode(n, toolbar, NULL);
206 wxControl *control = wxDynamicCast(created, wxControl);
207 if (!IsOfClass(n, wxT("tool")) &&
208 !IsOfClass(n, wxT("separator")) &&
209 control != NULL)
210 toolbar->AddControl(control);
211 }
212 n = n->GetNext();
213 }
214
215 m_isInside = false;
216 m_toolbar = NULL;
217
218 toolbar->Realize();
219
220 if (m_parentAsWindow && !GetBool(wxT("dontattachtoframe")))
221 {
222 wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
223 if (parentFrame)
224 parentFrame->SetToolBar(toolbar);
225 }
226
227 return toolbar;
228 }
229 }
230
231 bool wxToolBarXmlHandler::CanHandle(wxXmlNode *node)
232 {
233 return ((!m_isInside && IsOfClass(node, wxT("wxToolBar"))) ||
234 (m_isInside && IsOfClass(node, wxT("tool"))) ||
235 (m_isInside && IsOfClass(node, wxT("separator"))));
236 }
237
238 #endif // wxUSE_XRC && wxUSE_TOOLBAR