]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_sizer.cpp
Applied Andreas patch for applying the widget style
[wxWidgets.git] / src / xrc / xh_sizer.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xh_sizer.cpp
b5d6954b 3// Purpose: XRC resource for wxBoxSizer
78d14f80
VS
4// Author: Vaclav Slavik
5// Created: 2000/03/21
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
f80ea77b 10
78d14f80
VS
11#ifdef __GNUG__
12#pragma implementation "xh_sizer.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/xrc/xh_sizer.h"
23#include "wx/sizer.h"
0d077b41 24#include "wx/gbsizer.h"
78d14f80
VS
25#include "wx/log.h"
26#include "wx/statbox.h"
27#include "wx/notebook.h"
9e82cf98 28#include "wx/panel.h"
78d14f80
VS
29#include "wx/tokenzr.h"
30
78d14f80
VS
31
32
0d077b41 33IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
78d14f80 34
f80ea77b
WS
35
36
37wxSizerXmlHandler::wxSizerXmlHandler()
0d077b41 38 : wxXmlResourceHandler(),
f80ea77b
WS
39 m_isInside(false),
40 m_isGBS(false),
0d077b41 41 m_parentSizer(NULL)
78d14f80 42{
544fee32
VS
43 XRC_ADD_STYLE(wxHORIZONTAL);
44 XRC_ADD_STYLE(wxVERTICAL);
78d14f80
VS
45
46 // and flags
544fee32
VS
47 XRC_ADD_STYLE(wxLEFT);
48 XRC_ADD_STYLE(wxRIGHT);
49 XRC_ADD_STYLE(wxTOP);
50 XRC_ADD_STYLE(wxBOTTOM);
51 XRC_ADD_STYLE(wxNORTH);
52 XRC_ADD_STYLE(wxSOUTH);
53 XRC_ADD_STYLE(wxEAST);
54 XRC_ADD_STYLE(wxWEST);
55 XRC_ADD_STYLE(wxALL);
78d14f80 56
544fee32
VS
57 XRC_ADD_STYLE(wxGROW);
58 XRC_ADD_STYLE(wxEXPAND);
59 XRC_ADD_STYLE(wxSHAPED);
60 XRC_ADD_STYLE(wxSTRETCH_NOT);
78d14f80 61
544fee32
VS
62 XRC_ADD_STYLE(wxALIGN_CENTER);
63 XRC_ADD_STYLE(wxALIGN_CENTRE);
64 XRC_ADD_STYLE(wxALIGN_LEFT);
65 XRC_ADD_STYLE(wxALIGN_TOP);
66 XRC_ADD_STYLE(wxALIGN_RIGHT);
67 XRC_ADD_STYLE(wxALIGN_BOTTOM);
68 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
69 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
70 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
71 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
f80ea77b 72
a02376c8 73 XRC_ADD_STYLE(wxADJUST_MINSIZE);
c66b63e5 74 XRC_ADD_STYLE(wxFIXED_MINSIZE);
78d14f80
VS
75}
76
77
78
0d077b41
RD
79bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
80{
81 return ( (!m_isInside && IsSizerNode(node)) ||
82 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
f80ea77b 83 (m_isInside && IsOfClass(node, wxT("spacer")))
0d077b41
RD
84 );
85}
86
f80ea77b 87
0d077b41 88wxObject* wxSizerXmlHandler::DoCreateResource()
f80ea77b 89{
78d14f80 90 if (m_class == wxT("sizeritem"))
0d077b41 91 return Handle_sizeritem();
f80ea77b 92
78d14f80 93 else if (m_class == wxT("spacer"))
0d077b41
RD
94 return Handle_spacer();
95
96 else
97 return Handle_sizer();
98}
99
100
101
102
103bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
104{
105 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
106 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
107 (IsOfClass(node, wxT("wxGridSizer"))) ||
108 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
109 (IsOfClass(node, wxT("wxGridBagSizer")));
110}
111
112
113wxObject* wxSizerXmlHandler::Handle_sizeritem()
114{
115 // find the item to be managed by this sizeritem
116 wxXmlNode *n = GetParamNode(wxT("object"));
117 if ( !n )
118 n = GetParamNode(wxT("object_ref"));
119
120 // did we find one?
121 if (n)
122 {
123 // create a sizer item for it
124 wxSizerItem* sitem = MakeSizerItem();
f80ea77b 125
0d077b41
RD
126 // now fetch the item to be managed
127 bool old_gbs = m_isGBS;
128 bool old_ins = m_isInside;
129 wxSizer *old_par = m_parentSizer;
f80ea77b
WS
130 m_isInside = false;
131 if (!IsSizerNode(n)) m_parentSizer = NULL;
0d077b41
RD
132 wxObject *item = CreateResFromNode(n, m_parent, NULL);
133 m_isInside = old_ins;
134 m_parentSizer = old_par;
135 m_isGBS = old_gbs;
136
137 // and figure out what type it is
138 wxSizer *sizer = wxDynamicCast(item, wxSizer);
139 wxWindow *wnd = wxDynamicCast(item, wxWindow);
f80ea77b 140
0d077b41
RD
141 if (sizer)
142 sitem->SetSizer(sizer);
143 else if (wnd)
144 sitem->SetWindow(wnd);
f80ea77b 145 else
0d077b41
RD
146 wxLogError(wxT("Error in resource."));
147
1eba2193
RD
148 // finally, set other wxSizerItem attributes
149 SetSizerItemAttributes(sitem);
150
0d077b41
RD
151 AddSizerItem(sitem);
152 return item;
153 }
154 else /*n == NULL*/
78d14f80 155 {
0d077b41 156 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
78d14f80
VS
157 return NULL;
158 }
0d077b41
RD
159}
160
161
162wxObject* wxSizerXmlHandler::Handle_spacer()
163{
164 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
165
166 wxSizerItem* sitem = MakeSizerItem();
167 SetSizerItemAttributes(sitem);
168 sitem->SetSpacer(GetSize());
f80ea77b 169 AddSizerItem(sitem);
0d077b41
RD
170 return NULL;
171}
172
78d14f80 173
0d077b41
RD
174wxObject* wxSizerXmlHandler::Handle_sizer()
175{
176 wxSizer *sizer = NULL;
f80ea77b 177
0d077b41 178 wxXmlNode *parentNode = m_node->GetParent();
78d14f80 179
0d077b41 180 wxCHECK_MSG(m_parentSizer != NULL ||
0a904ad4 181 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
0d077b41
RD
182 m_parentAsWindow != NULL &&
183 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
184 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
185 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
186 ), NULL,
5dff4e24 187 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
78d14f80 188
0d077b41
RD
189 if (m_class == wxT("wxBoxSizer"))
190 sizer = Handle_wxBoxSizer();
191
192 else if (m_class == wxT("wxStaticBoxSizer"))
193 sizer = Handle_wxStaticBoxSizer();
f80ea77b 194
0d077b41
RD
195 else if (m_class == wxT("wxGridSizer"))
196 sizer = Handle_wxGridSizer();
f80ea77b 197
0d077b41
RD
198 else if (m_class == wxT("wxFlexGridSizer"))
199 sizer = Handle_wxFlexGridSizer();
78d14f80 200
0d077b41
RD
201 else if (m_class == wxT("wxGridBagSizer"))
202 sizer = Handle_wxGridBagSizer();
78d14f80 203
f80ea77b 204
0d077b41
RD
205 wxSize minsize = GetSize(wxT("minsize"));
206 if (!(minsize == wxDefaultSize))
207 sizer->SetMinSize(minsize);
208
209 // save state
210 wxSizer *old_par = m_parentSizer;
211 bool old_ins = m_isInside;
0d077b41
RD
212
213 // set new state
214 m_parentSizer = sizer;
f80ea77b 215 m_isInside = true;
0d077b41 216 m_isGBS = (m_class == wxT("wxGridBagSizer"));
f80ea77b
WS
217
218 CreateChildren(m_parent, true/*only this handler*/);
0d077b41
RD
219
220 // restore state
0d077b41
RD
221 m_isInside = old_ins;
222 m_parentSizer = old_par;
223
224 if (m_parentSizer == NULL) // setup window:
225 {
0d077b41
RD
226 m_parentAsWindow->SetSizer(sizer);
227
228 wxXmlNode *nd = m_node;
229 m_node = parentNode;
230 if (GetSize() == wxDefaultSize)
231 sizer->Fit(m_parentAsWindow);
232 m_node = nd;
233
234 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
235 sizer->SetSizeHints(m_parentAsWindow);
236 }
f80ea77b 237
0d077b41
RD
238 return sizer;
239}
240
241
242wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
243{
244 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
f80ea77b
WS
245}
246
0d077b41
RD
247wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
248{
249 return new wxStaticBoxSizer(
250 new wxStaticBox(m_parentAsWindow,
251 GetID(),
252 GetText(wxT("label")),
253 wxDefaultPosition, wxDefaultSize,
254 0/*style*/,
255 GetName()),
256 GetStyle(wxT("orient"), wxHORIZONTAL));
257}
f80ea77b 258
0d077b41
RD
259wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
260{
261 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
262 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
263}
264
265
266wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
267{
f80ea77b 268 wxFlexGridSizer *sizer =
0d077b41
RD
269 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
270 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
271 SetGrowables(sizer, wxT("growablerows"), true);
272 SetGrowables(sizer, wxT("growablecols"), false);
273 return sizer;
274}
275
276
277wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
278{
f80ea77b 279 wxGridBagSizer *sizer =
0d077b41
RD
280 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
281 SetGrowables(sizer, wxT("growablerows"), true);
282 SetGrowables(sizer, wxT("growablecols"), false);
283 return sizer;
284}
285
286
287
288
289void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
290 const wxChar* param,
291 bool rows)
292{
293 wxStringTokenizer tkn;
294 unsigned long l;
295 tkn.SetString(GetParamValue(param), wxT(","));
296 while (tkn.HasMoreTokens())
297 {
298 if (!tkn.GetNextToken().ToULong(&l))
299 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
300 else {
301 if (rows)
302 sizer->AddGrowableRow(l);
303 else
304 sizer->AddGrowableCol(l);
78d14f80 305 }
78d14f80
VS
306 }
307}
308
309
0d077b41
RD
310wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
311{
312 wxSize sz = GetSize(param);
313 if (sz.x < 0) sz.x = 0;
314 if (sz.y < 0) sz.y = 0;
315 return wxGBPosition(sz.x, sz.y);
316}
78d14f80 317
0d077b41
RD
318wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
319{
320 wxSize sz = GetSize(param);
321 if (sz.x < 1) sz.x = 1;
322 if (sz.y < 1) sz.y = 1;
323 return wxGBSpan(sz.x, sz.y);
324}
325
326
327
328wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
329{
330 if (m_isGBS)
331 return new wxGBSizerItem();
332 else
333 return new wxSizerItem();
334}
335
336void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
337{
338 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
339 sitem->SetFlag(GetStyle(wxT("flag")));
340 sitem->SetBorder(GetDimension(wxT("border")));
341 wxSize sz = GetSize(wxT("minsize"));
342 if (!(sz == wxDefaultSize))
1eba2193 343 sitem->SetMinSize(sz);
0d077b41
RD
344 sz = GetSize(wxT("ratio"));
345 if (!(sz == wxDefaultSize))
346 sitem->SetRatio(sz);
f80ea77b 347
0d077b41
RD
348 if (m_isGBS)
349 {
350 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
ea0d8ca6
RD
351 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
352 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
f80ea77b 353 }
0d077b41
RD
354}
355
356void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
78d14f80 357{
0d077b41
RD
358 if (m_isGBS)
359 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
360 else
361 m_parentSizer->Add(sitem);
78d14f80 362}
f80ea77b 363
0d077b41
RD
364
365
366
367
368
369
370
371
372