]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_sizer.cpp
remove unused variables, minor cleanup
[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
819559b2 164 ReportError(n, "unexpected item in sizer");
0d077b41 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 {
819559b2 174 ReportError("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 {
819559b2 184 ReportError("spacer only allowed inside a sizer");
07acc3cc
VZ
185 return NULL;
186 }
0d077b41
RD
187
188 wxSizerItem* sitem = MakeSizerItem();
189 SetSizerItemAttributes(sitem);
a50cf60e 190 sitem->AssignSpacer(GetSize());
f80ea77b 191 AddSizerItem(sitem);
0d077b41
RD
192 return NULL;
193}
194
78d14f80 195
0d077b41
RD
196wxObject* wxSizerXmlHandler::Handle_sizer()
197{
198 wxSizer *sizer = NULL;
5dd238c6 199 wxFlexGridSizer *flexsizer = NULL;
f80ea77b 200
0d077b41 201 wxXmlNode *parentNode = m_node->GetParent();
78d14f80 202
07acc3cc
VZ
203 if ( !m_parentSizer &&
204 (!parentNode || parentNode->GetType() != wxXML_ELEMENT_NODE ||
205 !m_parentAsWindow) )
206 {
819559b2 207 ReportError("sizer must have a window parent");
07acc3cc
VZ
208 return NULL;
209 }
78d14f80 210
0d077b41
RD
211 if (m_class == wxT("wxBoxSizer"))
212 sizer = Handle_wxBoxSizer();
627ddac9 213#if wxUSE_STATBOX
0d077b41
RD
214 else if (m_class == wxT("wxStaticBoxSizer"))
215 sizer = Handle_wxStaticBoxSizer();
627ddac9 216#endif
0d077b41 217 else if (m_class == wxT("wxGridSizer"))
afc0db8c
VS
218 {
219 if ( !ValidateGridSizerChildren() )
220 return NULL;
0d077b41 221 sizer = Handle_wxGridSizer();
afc0db8c 222 }
0d077b41 223 else if (m_class == wxT("wxFlexGridSizer"))
5dd238c6
VZ
224 {
225 flexsizer = Handle_wxFlexGridSizer();
226 sizer = flexsizer;
227 }
0d077b41 228 else if (m_class == wxT("wxGridBagSizer"))
1808bb47
VZ
229 {
230 flexsizer = Handle_wxGridBagSizer();
231 sizer = flexsizer;
232 }
13a38887 233 else if (m_class == wxT("wxWrapSizer"))
afc0db8c 234 {
63b37a4e 235 sizer = Handle_wxWrapSizer();
afc0db8c
VS
236 }
237 else
641c5f1a 238 {
819559b2 239 ReportError(wxString::Format("unknown sizer class \"%s\"", m_class));
641c5f1a 240 }
f80ea77b 241
afc0db8c
VS
242 // creation of sizer failed for some (already reported) reason, so exit:
243 if ( !sizer )
244 return NULL;
245
0d077b41
RD
246 wxSize minsize = GetSize(wxT("minsize"));
247 if (!(minsize == wxDefaultSize))
248 sizer->SetMinSize(minsize);
249
250 // save state
251 wxSizer *old_par = m_parentSizer;
252 bool old_ins = m_isInside;
0d077b41
RD
253
254 // set new state
255 m_parentSizer = sizer;
f80ea77b 256 m_isInside = true;
0d077b41 257 m_isGBS = (m_class == wxT("wxGridBagSizer"));
f80ea77b
WS
258
259 CreateChildren(m_parent, true/*only this handler*/);
0d077b41 260
5dd238c6
VZ
261 // set growable rows and cols for sizers which support this
262 if ( flexsizer )
263 {
264 SetGrowables(flexsizer, wxT("growablerows"), true);
265 SetGrowables(flexsizer, wxT("growablecols"), false);
266 }
267
0d077b41 268 // restore state
0d077b41
RD
269 m_isInside = old_ins;
270 m_parentSizer = old_par;
271
272 if (m_parentSizer == NULL) // setup window:
273 {
0d077b41
RD
274 m_parentAsWindow->SetSizer(sizer);
275
276 wxXmlNode *nd = m_node;
277 m_node = parentNode;
278 if (GetSize() == wxDefaultSize)
5e1a9f05
VS
279 {
280 if ( wxDynamicCast(m_parentAsWindow, wxScrolledWindow) != NULL )
281 {
282 sizer->FitInside(m_parentAsWindow);
283 }
284 else
285 {
286 sizer->Fit(m_parentAsWindow);
287 }
288 }
0d077b41
RD
289 m_node = nd;
290
ea813d91
VS
291 if (m_parentAsWindow->IsTopLevel())
292 {
0d077b41 293 sizer->SetSizeHints(m_parentAsWindow);
ea813d91 294 }
0d077b41 295 }
f80ea77b 296
0d077b41
RD
297 return sizer;
298}
299
300
301wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
302{
303 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
f80ea77b
WS
304}
305
627ddac9 306#if wxUSE_STATBOX
0d077b41
RD
307wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
308{
309 return new wxStaticBoxSizer(
310 new wxStaticBox(m_parentAsWindow,
311 GetID(),
312 GetText(wxT("label")),
313 wxDefaultPosition, wxDefaultSize,
314 0/*style*/,
315 GetName()),
316 GetStyle(wxT("orient"), wxHORIZONTAL));
317}
627ddac9 318#endif // wxUSE_STATBOX
f80ea77b 319
0d077b41
RD
320wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
321{
322 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
323 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
324}
325
326
5dd238c6 327wxFlexGridSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
0d077b41 328{
afc0db8c
VS
329 if ( !ValidateGridSizerChildren() )
330 return NULL;
5dd238c6
VZ
331 return new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
332 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
0d077b41
RD
333}
334
335
1808bb47 336wxGridBagSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
0d077b41 337{
afc0db8c
VS
338 if ( !ValidateGridSizerChildren() )
339 return NULL;
5dd238c6 340 return new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
0d077b41
RD
341}
342
13a38887
VZ
343wxSizer* wxSizerXmlHandler::Handle_wxWrapSizer()
344{
63b37a4e 345 wxWrapSizer *sizer = new wxWrapSizer(GetStyle("orient"), GetStyle("flag"));
13a38887
VZ
346 return sizer;
347}
0d077b41
RD
348
349
afc0db8c
VS
350bool wxSizerXmlHandler::ValidateGridSizerChildren()
351{
352 int rows = GetLong("rows");
353 int cols = GetLong("cols");
354
355 if ( rows && cols )
356 {
357 // fixed number of cells, need to verify children count
358 int children = 0;
359 for ( wxXmlNode *n = m_node->GetChildren(); n; n = n->GetNext() )
360 {
361 if ( n->GetType() == wxXML_ELEMENT_NODE &&
362 (n->GetName() == "object" || n->GetName() == "object_ref") )
363 {
364 children++;
365 }
366 }
367
368 if ( children > rows * cols )
369 {
370 ReportError
371 (
372 wxString::Format
373 (
374 "too many children in grid sizer: %d > %d x %d"
375 " (consider omitting the number of rows or columns)",
376 children,
377 cols,
378 rows
379 )
380 );
381 return false;
382 }
383 }
384
385 return true;
386}
387
0d077b41
RD
388
389void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
390 const wxChar* param,
391 bool rows)
392{
afc0db8c
VS
393 int nrows, ncols;
394 sizer->CalcRowsCols(nrows, ncols);
395 const int nslots = rows ? nrows : ncols;
396
0d077b41 397 wxStringTokenizer tkn;
0d077b41 398 tkn.SetString(GetParamValue(param), wxT(","));
afc0db8c 399
0d077b41
RD
400 while (tkn.HasMoreTokens())
401 {
afc0db8c 402 unsigned long l;
0d077b41 403 if (!tkn.GetNextToken().ToULong(&l))
5dd238c6 404 {
819559b2
VS
405 ReportParamError
406 (
407 param,
408 "value must be comma-separated list of row numbers"
409 );
5dd238c6 410 break;
78d14f80 411 }
5dd238c6 412
afc0db8c
VS
413 if ( (int)l >= nslots )
414 {
415 ReportParamError
416 (
417 param,
418 wxString::Format
419 (
420 "invalid %s index %d: must be less than %d",
421 rows ? "row" : "column",
422 l,
423 nslots
424 )
425 );
426
427 // ignore incorrect value, still try to process the rest
428 continue;
429 }
430
5dd238c6
VZ
431 if (rows)
432 sizer->AddGrowableRow(l);
433 else
434 sizer->AddGrowableCol(l);
78d14f80
VS
435 }
436}
437
438
0d077b41
RD
439wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
440{
441 wxSize sz = GetSize(param);
442 if (sz.x < 0) sz.x = 0;
443 if (sz.y < 0) sz.y = 0;
444 return wxGBPosition(sz.x, sz.y);
445}
78d14f80 446
0d077b41
RD
447wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
448{
449 wxSize sz = GetSize(param);
450 if (sz.x < 1) sz.x = 1;
451 if (sz.y < 1) sz.y = 1;
452 return wxGBSpan(sz.x, sz.y);
453}
454
455
456
457wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
458{
459 if (m_isGBS)
460 return new wxGBSizerItem();
461 else
462 return new wxSizerItem();
463}
464
465void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
466{
467 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
468 sitem->SetFlag(GetStyle(wxT("flag")));
469 sitem->SetBorder(GetDimension(wxT("border")));
470 wxSize sz = GetSize(wxT("minsize"));
471 if (!(sz == wxDefaultSize))
1eba2193 472 sitem->SetMinSize(sz);
0d077b41
RD
473 sz = GetSize(wxT("ratio"));
474 if (!(sz == wxDefaultSize))
475 sitem->SetRatio(sz);
f80ea77b 476
0d077b41
RD
477 if (m_isGBS)
478 {
479 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
ea0d8ca6
RD
480 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
481 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
f80ea77b 482 }
86909f4c
VZ
483
484 // record the id of the item, if any, for use by XRCSIZERITEM()
485 sitem->SetId(GetID());
0d077b41
RD
486}
487
488void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
78d14f80 489{
0d077b41
RD
490 if (m_isGBS)
491 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
492 else
493 m_parentSizer->Add(sitem);
78d14f80 494}
f80ea77b 495
172541f6
VS
496
497
498//-----------------------------------------------------------------------------
499// wxStdDialogButtonSizerXmlHandler
500//-----------------------------------------------------------------------------
dd47af27 501#if wxUSE_BUTTON
172541f6
VS
502
503IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler)
504
505wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
506 : m_isInside(false), m_parentSizer(NULL)
507{
508}
509
510wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource()
511{
512 if (m_class == wxT("wxStdDialogButtonSizer"))
513 {
514 wxASSERT( !m_parentSizer );
515
516 wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer;
517 m_isInside = true;
518
519 CreateChildren(m_parent, true/*only this handler*/);
520
521 m_parentSizer->Realize();
522
523 m_isInside = false;
524 m_parentSizer = NULL;
525
526 return s;
527 }
528 else // m_class == "button"
529 {
530 wxASSERT( m_parentSizer );
531
532 // find the item to be managed by this sizeritem
533 wxXmlNode *n = GetParamNode(wxT("object"));
534 if ( !n )
535 n = GetParamNode(wxT("object_ref"));
536
537 // did we find one?
538 if (n)
539 {
540 wxObject *item = CreateResFromNode(n, m_parent, NULL);
541 wxButton *button = wxDynamicCast(item, wxButton);
542
543 if (button)
544 m_parentSizer->AddButton(button);
545 else
819559b2 546 ReportError(n, "expected wxButton");
172541f6
VS
547
548 return item;
549 }
550 else /*n == NULL*/
551 {
819559b2 552 ReportError("no button within wxStdDialogButtonSizer");
172541f6
VS
553 return NULL;
554 }
555 }
556}
557
558bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node)
559{
560 return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) ||
561 (m_isInside && IsOfClass(node, wxT("button")));
562}
dd47af27 563#endif // wxUSE_BUTTON
172541f6 564
621be1ec 565#endif // wxUSE_XRC