]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_toolb.cpp
0fa523baa0ffac9ef7b9f30bdc9106f3c5694d3b
[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 wxCHECK_MSG(m_toolbar, NULL, wxT("Incorrect syntax of XRC resource: tool not within a toolbar!"));
57
58 wxItemKind kind = wxITEM_NORMAL;
59 if (GetBool(wxT("radio")))
60 kind = wxITEM_RADIO;
61
62 if (GetBool(wxT("toggle")))
63 {
64 wxASSERT_MSG( kind == wxITEM_NORMAL,
65 _T("can't have both toggle and radio button at once") );
66 kind = wxITEM_CHECK;
67 }
68
69 // check whether we have dropdown tag inside
70 wxMenu *menu = NULL; // menu for drop down items
71 wxXmlNode * const nodeDropdown = GetParamNode("dropdown");
72 if ( nodeDropdown )
73 {
74 wxASSERT_MSG( kind == wxITEM_NORMAL,
75 "drop down button can't be a check/radio "
76 "button too" );
77
78 kind = wxITEM_DROPDOWN;
79
80 // also check for the menu specified inside dropdown (it is
81 // optional and may be absent for e.g. dynamically-created
82 // menus)
83 wxXmlNode * const nodeMenu = nodeDropdown->GetChildren();
84 if ( nodeMenu )
85 {
86 wxObject *res = CreateResFromNode(nodeMenu, NULL);
87 menu = wxDynamicCast(res, wxMenu);
88 wxASSERT_MSG( menu, "invalid drop down object contents" );
89
90 wxASSERT_MSG( !nodeMenu->GetNext(),
91 "only single menu tag allowed inside dropdown" );
92 }
93 }
94
95 wxToolBarToolBase * const
96 tool = m_toolbar->AddTool
97 (
98 GetID(),
99 GetText(wxT("label")),
100 GetBitmap(wxT("bitmap"), wxART_TOOLBAR),
101 GetBitmap(wxT("bitmap2"), wxART_TOOLBAR),
102 kind,
103 GetText(wxT("tooltip")),
104 GetText(wxT("longhelp"))
105 );
106
107 if ( GetBool(wxT("disabled")) )
108 m_toolbar->EnableTool(GetID(), false);
109
110 if ( menu )
111 tool->SetDropdownMenu(menu);
112
113 return m_toolbar; // must return non-NULL
114 }
115
116 else if (m_class == wxT("separator"))
117 {
118 wxCHECK_MSG(m_toolbar, NULL, wxT("Incorrect syntax of XRC resource: separator not within a toolbar!"));
119 m_toolbar->AddSeparator();
120 return m_toolbar; // must return non-NULL
121 }
122
123 else /*<object class="wxToolBar">*/
124 {
125 int style = GetStyle(wxT("style"), wxNO_BORDER | wxTB_HORIZONTAL);
126 #ifdef __WXMSW__
127 if (!(style & wxNO_BORDER)) style |= wxNO_BORDER;
128 #endif
129
130 XRC_MAKE_INSTANCE(toolbar, wxToolBar)
131
132 toolbar->Create(m_parentAsWindow,
133 GetID(),
134 GetPosition(),
135 GetSize(),
136 style,
137 GetName());
138 SetupWindow(toolbar);
139
140 wxSize bmpsize = GetSize(wxT("bitmapsize"));
141 if (!(bmpsize == wxDefaultSize))
142 toolbar->SetToolBitmapSize(bmpsize);
143 wxSize margins = GetSize(wxT("margins"));
144 if (!(margins == wxDefaultSize))
145 toolbar->SetMargins(margins.x, margins.y);
146 long packing = GetLong(wxT("packing"), -1);
147 if (packing != -1)
148 toolbar->SetToolPacking(packing);
149 long separation = GetLong(wxT("separation"), -1);
150 if (separation != -1)
151 toolbar->SetToolSeparation(separation);
152
153 wxXmlNode *children_node = GetParamNode(wxT("object"));
154 if (!children_node)
155 children_node = GetParamNode(wxT("object_ref"));
156
157 if (children_node == NULL) return toolbar;
158
159 m_isInside = true;
160 m_toolbar = toolbar;
161
162 wxXmlNode *n = children_node;
163
164 while (n)
165 {
166 if ((n->GetType() == wxXML_ELEMENT_NODE) &&
167 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
168 {
169 wxObject *created = CreateResFromNode(n, toolbar, NULL);
170 wxControl *control = wxDynamicCast(created, wxControl);
171 if (!IsOfClass(n, wxT("tool")) &&
172 !IsOfClass(n, wxT("separator")) &&
173 control != NULL)
174 toolbar->AddControl(control);
175 }
176 n = n->GetNext();
177 }
178
179 m_isInside = false;
180 m_toolbar = NULL;
181
182 toolbar->Realize();
183
184 if (m_parentAsWindow && !GetBool(wxT("dontattachtoframe")))
185 {
186 wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
187 if (parentFrame)
188 parentFrame->SetToolBar(toolbar);
189 }
190
191 return toolbar;
192 }
193 }
194
195 bool wxToolBarXmlHandler::CanHandle(wxXmlNode *node)
196 {
197 return ((!m_isInside && IsOfClass(node, wxT("wxToolBar"))) ||
198 (m_isInside && IsOfClass(node, wxT("tool"))) ||
199 (m_isInside && IsOfClass(node, wxT("separator"))));
200 }
201
202 #endif // wxUSE_XRC && wxUSE_TOOLBAR