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