]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_sizer.cpp
fixing wrong constant
[wxWidgets.git] / src / xrc / xh_sizer.cpp
CommitLineData
78d14f80 1/////////////////////////////////////////////////////////////////////////////
1c067fe3 2// Name: src/xrc/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// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
621be1ec 18#if wxUSE_XRC
a1e4ec87 19
78d14f80 20#include "wx/xrc/xh_sizer.h"
e4db172a
WS
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
8e609c82 24 #include "wx/panel.h"
876cd6f7 25 #include "wx/statbox.h"
ed2fbeb8 26 #include "wx/sizer.h"
4107600f
MW
27 #include "wx/frame.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
5e1a9f05 30 #include "wx/scrolwin.h"
e4db172a
WS
31#endif
32
0d077b41 33#include "wx/gbsizer.h"
13a38887 34#include "wx/wrapsizer.h"
78d14f80
VS
35#include "wx/notebook.h"
36#include "wx/tokenzr.h"
37
78d14f80 38
172541f6
VS
39//-----------------------------------------------------------------------------
40// wxSizerXmlHandler
41//-----------------------------------------------------------------------------
78d14f80 42
0d077b41 43IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
78d14f80 44
f80ea77b 45wxSizerXmlHandler::wxSizerXmlHandler()
ed2fbeb8
WS
46 :wxXmlResourceHandler(),
47 m_isInside(false),
48 m_isGBS(false),
49 m_parentSizer(NULL)
78d14f80 50{
544fee32
VS
51 XRC_ADD_STYLE(wxHORIZONTAL);
52 XRC_ADD_STYLE(wxVERTICAL);
78d14f80
VS
53
54 // and flags
544fee32
VS
55 XRC_ADD_STYLE(wxLEFT);
56 XRC_ADD_STYLE(wxRIGHT);
57 XRC_ADD_STYLE(wxTOP);
58 XRC_ADD_STYLE(wxBOTTOM);
59 XRC_ADD_STYLE(wxNORTH);
60 XRC_ADD_STYLE(wxSOUTH);
61 XRC_ADD_STYLE(wxEAST);
62 XRC_ADD_STYLE(wxWEST);
63 XRC_ADD_STYLE(wxALL);
78d14f80 64
544fee32
VS
65 XRC_ADD_STYLE(wxGROW);
66 XRC_ADD_STYLE(wxEXPAND);
67 XRC_ADD_STYLE(wxSHAPED);
68 XRC_ADD_STYLE(wxSTRETCH_NOT);
78d14f80 69
544fee32
VS
70 XRC_ADD_STYLE(wxALIGN_CENTER);
71 XRC_ADD_STYLE(wxALIGN_CENTRE);
72 XRC_ADD_STYLE(wxALIGN_LEFT);
73 XRC_ADD_STYLE(wxALIGN_TOP);
74 XRC_ADD_STYLE(wxALIGN_RIGHT);
75 XRC_ADD_STYLE(wxALIGN_BOTTOM);
76 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
77 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
78 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
79 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
f80ea77b 80
c66b63e5 81 XRC_ADD_STYLE(wxFIXED_MINSIZE);
a575238f 82 XRC_ADD_STYLE(wxRESERVE_SPACE_EVEN_IF_HIDDEN);
63b37a4e 83
249259e7
VZ
84 // this flag doesn't do anything any more but we can just ignore its
85 // occurrences in the old resource files instead of raising a fuss because
86 // of it
87 AddStyle("wxADJUST_MINSIZE", 0);
88
63b37a4e
VZ
89 // wxWrapSizer-specific flags
90 XRC_ADD_STYLE(wxEXTEND_LAST_ON_EACH_LINE);
91 XRC_ADD_STYLE(wxREMOVE_LEADING_SPACES);
78d14f80
VS
92}
93
94
95
0d077b41
RD
96bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
97{
98 return ( (!m_isInside && IsSizerNode(node)) ||
99 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
f80ea77b 100 (m_isInside && IsOfClass(node, wxT("spacer")))
0d077b41
RD
101 );
102}
103
f80ea77b 104
0d077b41 105wxObject* wxSizerXmlHandler::DoCreateResource()
f80ea77b 106{
78d14f80 107 if (m_class == wxT("sizeritem"))
0d077b41 108 return Handle_sizeritem();
f80ea77b 109
78d14f80 110 else if (m_class == wxT("spacer"))
0d077b41
RD
111 return Handle_spacer();
112
113 else
114 return Handle_sizer();
115}
116
117
118
119
120bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
121{
122 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
123 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
124 (IsOfClass(node, wxT("wxGridSizer"))) ||
125 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
13a38887
VZ
126 (IsOfClass(node, wxT("wxGridBagSizer"))) ||
127 (IsOfClass(node, wxT("wxWrapSizer")));
0d077b41
RD
128}
129
130
131wxObject* wxSizerXmlHandler::Handle_sizeritem()
132{
133 // find the item to be managed by this sizeritem
134 wxXmlNode *n = GetParamNode(wxT("object"));
135 if ( !n )
136 n = GetParamNode(wxT("object_ref"));
137
138 // did we find one?
139 if (n)
140 {
141 // create a sizer item for it
142 wxSizerItem* sitem = MakeSizerItem();
f80ea77b 143
0d077b41
RD
144 // now fetch the item to be managed
145 bool old_gbs = m_isGBS;
146 bool old_ins = m_isInside;
147 wxSizer *old_par = m_parentSizer;
f80ea77b
WS
148 m_isInside = false;
149 if (!IsSizerNode(n)) m_parentSizer = NULL;
0d077b41
RD
150 wxObject *item = CreateResFromNode(n, m_parent, NULL);
151 m_isInside = old_ins;
152 m_parentSizer = old_par;
153 m_isGBS = old_gbs;
154
155 // and figure out what type it is
156 wxSizer *sizer = wxDynamicCast(item, wxSizer);
157 wxWindow *wnd = wxDynamicCast(item, wxWindow);
f80ea77b 158
0d077b41 159 if (sizer)
a50cf60e 160 sitem->AssignSizer(sizer);
0d077b41 161 else if (wnd)
a50cf60e 162 sitem->AssignWindow(wnd);
f80ea77b 163 else
0d077b41
RD
164 wxLogError(wxT("Error in resource."));
165
1eba2193
RD
166 // finally, set other wxSizerItem attributes
167 SetSizerItemAttributes(sitem);
168
0d077b41
RD
169 AddSizerItem(sitem);
170 return item;
171 }
172 else /*n == NULL*/
78d14f80 173 {
0d077b41 174 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
78d14f80
VS
175 return NULL;
176 }
0d077b41
RD
177}
178
179
180wxObject* wxSizerXmlHandler::Handle_spacer()
181{
07acc3cc
VZ
182 if ( !m_parentSizer )
183 {
184 wxLogError(_("XRC syntax error: \"spacer\" only allowed inside a "
185 "sizer"));
186 return NULL;
187 }
0d077b41
RD
188
189 wxSizerItem* sitem = MakeSizerItem();
190 SetSizerItemAttributes(sitem);
a50cf60e 191 sitem->AssignSpacer(GetSize());
f80ea77b 192 AddSizerItem(sitem);
0d077b41
RD
193 return NULL;
194}
195
78d14f80 196
0d077b41
RD
197wxObject* wxSizerXmlHandler::Handle_sizer()
198{
199 wxSizer *sizer = NULL;
5dd238c6 200 wxFlexGridSizer *flexsizer = NULL;
f80ea77b 201
0d077b41 202 wxXmlNode *parentNode = m_node->GetParent();
78d14f80 203
07acc3cc
VZ
204 if ( !m_parentSizer &&
205 (!parentNode || parentNode->GetType() != wxXML_ELEMENT_NODE ||
206 !m_parentAsWindow) )
207 {
208 wxLogError(_("XRC syntax error: sizer must have a window parent."));
209 return NULL;
210 }
78d14f80 211
0d077b41
RD
212 if (m_class == wxT("wxBoxSizer"))
213 sizer = Handle_wxBoxSizer();
627ddac9 214#if wxUSE_STATBOX
0d077b41
RD
215 else if (m_class == wxT("wxStaticBoxSizer"))
216 sizer = Handle_wxStaticBoxSizer();
627ddac9 217#endif
0d077b41
RD
218 else if (m_class == wxT("wxGridSizer"))
219 sizer = Handle_wxGridSizer();
0d077b41 220 else if (m_class == wxT("wxFlexGridSizer"))
5dd238c6
VZ
221 {
222 flexsizer = Handle_wxFlexGridSizer();
223 sizer = flexsizer;
224 }
0d077b41 225 else if (m_class == wxT("wxGridBagSizer"))
1808bb47
VZ
226 {
227 flexsizer = Handle_wxGridBagSizer();
228 sizer = flexsizer;
229 }
13a38887 230 else if (m_class == wxT("wxWrapSizer"))
63b37a4e 231 sizer = Handle_wxWrapSizer();
13a38887 232
641c5f1a
VZ
233 if ( !sizer )
234 {
235 wxLogError(_T("Failed to create size of class \"%s\""), m_class.c_str());
236 return NULL;
237 }
f80ea77b 238
0d077b41
RD
239 wxSize minsize = GetSize(wxT("minsize"));
240 if (!(minsize == wxDefaultSize))
241 sizer->SetMinSize(minsize);
242
243 // save state
244 wxSizer *old_par = m_parentSizer;
245 bool old_ins = m_isInside;
0d077b41
RD
246
247 // set new state
248 m_parentSizer = sizer;
f80ea77b 249 m_isInside = true;
0d077b41 250 m_isGBS = (m_class == wxT("wxGridBagSizer"));
f80ea77b
WS
251
252 CreateChildren(m_parent, true/*only this handler*/);
0d077b41 253
5dd238c6
VZ
254 // set growable rows and cols for sizers which support this
255 if ( flexsizer )
256 {
257 SetGrowables(flexsizer, wxT("growablerows"), true);
258 SetGrowables(flexsizer, wxT("growablecols"), false);
259 }
260
0d077b41 261 // restore state
0d077b41
RD
262 m_isInside = old_ins;
263 m_parentSizer = old_par;
264
265 if (m_parentSizer == NULL) // setup window:
266 {
0d077b41
RD
267 m_parentAsWindow->SetSizer(sizer);
268
269 wxXmlNode *nd = m_node;
270 m_node = parentNode;
271 if (GetSize() == wxDefaultSize)
5e1a9f05
VS
272 {
273 if ( wxDynamicCast(m_parentAsWindow, wxScrolledWindow) != NULL )
274 {
275 sizer->FitInside(m_parentAsWindow);
276 }
277 else
278 {
279 sizer->Fit(m_parentAsWindow);
280 }
281 }
0d077b41
RD
282 m_node = nd;
283
ea813d91
VS
284 if (m_parentAsWindow->IsTopLevel())
285 {
0d077b41 286 sizer->SetSizeHints(m_parentAsWindow);
ea813d91 287 }
0d077b41 288 }
f80ea77b 289
0d077b41
RD
290 return sizer;
291}
292
293
294wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
295{
296 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
f80ea77b
WS
297}
298
627ddac9 299#if wxUSE_STATBOX
0d077b41
RD
300wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
301{
302 return new wxStaticBoxSizer(
303 new wxStaticBox(m_parentAsWindow,
304 GetID(),
305 GetText(wxT("label")),
306 wxDefaultPosition, wxDefaultSize,
307 0/*style*/,
308 GetName()),
309 GetStyle(wxT("orient"), wxHORIZONTAL));
310}
627ddac9 311#endif // wxUSE_STATBOX
f80ea77b 312
0d077b41
RD
313wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
314{
315 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
316 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
317}
318
319
5dd238c6 320wxFlexGridSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
0d077b41 321{
5dd238c6
VZ
322 return new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
323 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
0d077b41
RD
324}
325
326
1808bb47 327wxGridBagSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
0d077b41 328{
5dd238c6 329 return new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
0d077b41
RD
330}
331
13a38887
VZ
332wxSizer* wxSizerXmlHandler::Handle_wxWrapSizer()
333{
63b37a4e 334 wxWrapSizer *sizer = new wxWrapSizer(GetStyle("orient"), GetStyle("flag"));
13a38887
VZ
335 return sizer;
336}
0d077b41
RD
337
338
339
340void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
341 const wxChar* param,
342 bool rows)
343{
344 wxStringTokenizer tkn;
345 unsigned long l;
346 tkn.SetString(GetParamValue(param), wxT(","));
347 while (tkn.HasMoreTokens())
348 {
349 if (!tkn.GetNextToken().ToULong(&l))
5dd238c6 350 {
0d077b41 351 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
5dd238c6 352 break;
78d14f80 353 }
5dd238c6
VZ
354
355 if (rows)
356 sizer->AddGrowableRow(l);
357 else
358 sizer->AddGrowableCol(l);
78d14f80
VS
359 }
360}
361
362
0d077b41
RD
363wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
364{
365 wxSize sz = GetSize(param);
366 if (sz.x < 0) sz.x = 0;
367 if (sz.y < 0) sz.y = 0;
368 return wxGBPosition(sz.x, sz.y);
369}
78d14f80 370
0d077b41
RD
371wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
372{
373 wxSize sz = GetSize(param);
374 if (sz.x < 1) sz.x = 1;
375 if (sz.y < 1) sz.y = 1;
376 return wxGBSpan(sz.x, sz.y);
377}
378
379
380
381wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
382{
383 if (m_isGBS)
384 return new wxGBSizerItem();
385 else
386 return new wxSizerItem();
387}
388
389void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
390{
391 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
392 sitem->SetFlag(GetStyle(wxT("flag")));
393 sitem->SetBorder(GetDimension(wxT("border")));
394 wxSize sz = GetSize(wxT("minsize"));
395 if (!(sz == wxDefaultSize))
1eba2193 396 sitem->SetMinSize(sz);
0d077b41
RD
397 sz = GetSize(wxT("ratio"));
398 if (!(sz == wxDefaultSize))
399 sitem->SetRatio(sz);
f80ea77b 400
0d077b41
RD
401 if (m_isGBS)
402 {
403 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
ea0d8ca6
RD
404 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
405 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
f80ea77b 406 }
86909f4c
VZ
407
408 // record the id of the item, if any, for use by XRCSIZERITEM()
409 sitem->SetId(GetID());
0d077b41
RD
410}
411
412void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
78d14f80 413{
0d077b41
RD
414 if (m_isGBS)
415 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
416 else
417 m_parentSizer->Add(sitem);
78d14f80 418}
f80ea77b 419
172541f6
VS
420
421
422//-----------------------------------------------------------------------------
423// wxStdDialogButtonSizerXmlHandler
424//-----------------------------------------------------------------------------
dd47af27 425#if wxUSE_BUTTON
172541f6
VS
426
427IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler)
428
429wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
430 : m_isInside(false), m_parentSizer(NULL)
431{
432}
433
434wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource()
435{
436 if (m_class == wxT("wxStdDialogButtonSizer"))
437 {
438 wxASSERT( !m_parentSizer );
439
440 wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer;
441 m_isInside = true;
442
443 CreateChildren(m_parent, true/*only this handler*/);
444
445 m_parentSizer->Realize();
446
447 m_isInside = false;
448 m_parentSizer = NULL;
449
450 return s;
451 }
452 else // m_class == "button"
453 {
454 wxASSERT( m_parentSizer );
455
456 // find the item to be managed by this sizeritem
457 wxXmlNode *n = GetParamNode(wxT("object"));
458 if ( !n )
459 n = GetParamNode(wxT("object_ref"));
460
461 // did we find one?
462 if (n)
463 {
464 wxObject *item = CreateResFromNode(n, m_parent, NULL);
465 wxButton *button = wxDynamicCast(item, wxButton);
466
467 if (button)
468 m_parentSizer->AddButton(button);
469 else
470 wxLogError(wxT("Error in resource - expected button."));
471
472 return item;
473 }
474 else /*n == NULL*/
475 {
476 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
477 return NULL;
478 }
479 }
480}
481
482bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node)
483{
484 return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) ||
485 (m_isInside && IsOfClass(node, wxT("button")));
486}
dd47af27 487#endif // wxUSE_BUTTON
172541f6 488
621be1ec 489#endif // wxUSE_XRC