replace asserts in XRC code with wxLogError/Warning() calls as XRC can come from...
[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/toolbar.h"
25 #endif
26
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
52 wxObject *wxToolBarXmlHandler::DoCreateResource()
53 {
54 if (m_class == wxT("tool"))
55 {
56 if ( !m_toolbar )
57 {
58 wxLogError(_("XRC syntax error: \"tool\" only allowed inside a "
59 "toolbar"));
60 return NULL;
61 }
62
63 wxItemKind kind = wxITEM_NORMAL;
64 if (GetBool(wxT("radio")))
65 kind = wxITEM_RADIO;
66
67 if (GetBool(wxT("toggle")))
68 {
69 if ( kind != wxITEM_NORMAL )
70 {
71 wxLogWarning(_("XRC syntax error: tool can't have both "
72 "\"radio\" and \"toggle\" properties, "
73 "ignoring the former."));
74 }
75
76 kind = wxITEM_CHECK;
77 }
78
79 // check whether we have dropdown tag inside
80 wxMenu *menu = NULL; // menu for drop down items
81 wxXmlNode * const nodeDropdown = GetParamNode("dropdown");
82 if ( nodeDropdown )
83 {
84 if ( kind != wxITEM_NORMAL )
85 {
86 wxLogWarning(_("XRC syntax error: drop-down tool can't have "
87 "neither \"radio\" nor \"toggle\" properties, "
88 "ignoring them."));
89 }
90
91 kind = wxITEM_DROPDOWN;
92
93 // also check for the menu specified inside dropdown (it is
94 // optional and may be absent for e.g. dynamically-created
95 // menus)
96 wxXmlNode * const nodeMenu = nodeDropdown->GetChildren();
97 if ( nodeMenu )
98 {
99 wxObject *res = CreateResFromNode(nodeMenu, NULL);
100 menu = wxDynamicCast(res, wxMenu);
101 if ( !menu )
102 {
103 wxLogError(_("XRC syntax error: invalid drop-down tool "
104 "contents (expected a menu)."));
105 }
106
107 if ( nodeMenu->GetNext() )
108 {
109 wxLogWarning(_("XRC syntax error: unexpected extra "
110 "contents under drop-down tool."));
111 }
112 }
113 }
114
115 wxToolBarToolBase * const
116 tool = m_toolbar->AddTool
117 (
118 GetID(),
119 GetText(wxT("label")),
120 GetBitmap(wxT("bitmap"), wxART_TOOLBAR),
121 GetBitmap(wxT("bitmap2"), wxART_TOOLBAR),
122 kind,
123 GetText(wxT("tooltip")),
124 GetText(wxT("longhelp"))
125 );
126
127 if ( GetBool(wxT("disabled")) )
128 m_toolbar->EnableTool(GetID(), false);
129
130 if ( menu )
131 tool->SetDropdownMenu(menu);
132
133 return m_toolbar; // must return non-NULL
134 }
135
136 else if (m_class == wxT("separator"))
137 {
138 if ( !m_toolbar )
139 {
140 wxLogError(_("XRC syntax error: \"separator\" only allowed inside a "
141 "toolbar"));
142 return NULL;
143 }
144 m_toolbar->AddSeparator();
145 return m_toolbar; // must return non-NULL
146 }
147
148 else /*<object class="wxToolBar">*/
149 {
150 int style = GetStyle(wxT("style"), wxNO_BORDER | wxTB_HORIZONTAL);
151 #ifdef __WXMSW__
152 if (!(style & wxNO_BORDER)) style |= wxNO_BORDER;
153 #endif
154
155 XRC_MAKE_INSTANCE(toolbar, wxToolBar)
156
157 toolbar->Create(m_parentAsWindow,
158 GetID(),
159 GetPosition(),
160 GetSize(),
161 style,
162 GetName());
163 SetupWindow(toolbar);
164
165 wxSize bmpsize = GetSize(wxT("bitmapsize"));
166 if (!(bmpsize == wxDefaultSize))
167 toolbar->SetToolBitmapSize(bmpsize);
168 wxSize margins = GetSize(wxT("margins"));
169 if (!(margins == wxDefaultSize))
170 toolbar->SetMargins(margins.x, margins.y);
171 long packing = GetLong(wxT("packing"), -1);
172 if (packing != -1)
173 toolbar->SetToolPacking(packing);
174 long separation = GetLong(wxT("separation"), -1);
175 if (separation != -1)
176 toolbar->SetToolSeparation(separation);
177
178 wxXmlNode *children_node = GetParamNode(wxT("object"));
179 if (!children_node)
180 children_node = GetParamNode(wxT("object_ref"));
181
182 if (children_node == NULL) return toolbar;
183
184 m_isInside = true;
185 m_toolbar = toolbar;
186
187 wxXmlNode *n = children_node;
188
189 while (n)
190 {
191 if ((n->GetType() == wxXML_ELEMENT_NODE) &&
192 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
193 {
194 wxObject *created = CreateResFromNode(n, toolbar, NULL);
195 wxControl *control = wxDynamicCast(created, wxControl);
196 if (!IsOfClass(n, wxT("tool")) &&
197 !IsOfClass(n, wxT("separator")) &&
198 control != NULL)
199 toolbar->AddControl(control);
200 }
201 n = n->GetNext();
202 }
203
204 m_isInside = false;
205 m_toolbar = NULL;
206
207 toolbar->Realize();
208
209 if (m_parentAsWindow && !GetBool(wxT("dontattachtoframe")))
210 {
211 wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
212 if (parentFrame)
213 parentFrame->SetToolBar(toolbar);
214 }
215
216 return toolbar;
217 }
218 }
219
220 bool wxToolBarXmlHandler::CanHandle(wxXmlNode *node)
221 {
222 return ((!m_isInside && IsOfClass(node, wxT("wxToolBar"))) ||
223 (m_isInside && IsOfClass(node, wxT("tool"))) ||
224 (m_isInside && IsOfClass(node, wxT("separator"))));
225 }
226
227 #endif // wxUSE_XRC && wxUSE_TOOLBAR