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