1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xh_sizer.cpp
3 // Purpose: XRC resource for wxBoxSizer
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/xrc/xh_sizer.h"
24 #include "wx/statbox.h"
27 #include "wx/dialog.h"
28 #include "wx/button.h"
29 #include "wx/scrolwin.h"
32 #include "wx/gbsizer.h"
33 #include "wx/wrapsizer.h"
34 #include "wx/notebook.h"
35 #include "wx/tokenzr.h"
37 #include "wx/xml/xml.h"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
45 wxSizerXmlHandler::wxSizerXmlHandler()
46 :wxXmlResourceHandler(),
51 XRC_ADD_STYLE(wxHORIZONTAL
);
52 XRC_ADD_STYLE(wxVERTICAL
);
55 XRC_ADD_STYLE(wxLEFT
);
56 XRC_ADD_STYLE(wxRIGHT
);
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
);
65 XRC_ADD_STYLE(wxGROW
);
66 XRC_ADD_STYLE(wxEXPAND
);
67 XRC_ADD_STYLE(wxSHAPED
);
68 XRC_ADD_STYLE(wxSTRETCH_NOT
);
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
);
81 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
82 XRC_ADD_STYLE(wxRESERVE_SPACE_EVEN_IF_HIDDEN
);
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
87 AddStyle("wxADJUST_MINSIZE", 0);
89 // wxWrapSizer-specific flags
90 XRC_ADD_STYLE(wxEXTEND_LAST_ON_EACH_LINE
);
91 XRC_ADD_STYLE(wxREMOVE_LEADING_SPACES
);
96 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
98 return ( (!m_isInside
&& IsSizerNode(node
)) ||
99 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
100 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
105 wxObject
* wxSizerXmlHandler::DoCreateResource()
107 if (m_class
== wxT("sizeritem"))
108 return Handle_sizeritem();
110 else if (m_class
== wxT("spacer"))
111 return Handle_spacer();
114 return Handle_sizer();
118 wxSizer
* wxSizerXmlHandler::DoCreateSizer(const wxString
& name
)
120 if (name
== wxT("wxBoxSizer"))
121 return Handle_wxBoxSizer();
123 else if (name
== wxT("wxStaticBoxSizer"))
124 return Handle_wxStaticBoxSizer();
126 else if (name
== wxT("wxGridSizer"))
128 if ( !ValidateGridSizerChildren() )
130 return Handle_wxGridSizer();
132 else if (name
== wxT("wxFlexGridSizer"))
134 return Handle_wxFlexGridSizer();
136 else if (name
== wxT("wxGridBagSizer"))
138 return Handle_wxGridBagSizer();
140 else if (name
== wxT("wxWrapSizer"))
142 return Handle_wxWrapSizer();
145 ReportError(wxString::Format("unknown sizer class \"%s\"", name
));
151 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
) const
153 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
154 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
155 (IsOfClass(node
, wxT("wxGridSizer"))) ||
156 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
157 (IsOfClass(node
, wxT("wxGridBagSizer"))) ||
158 (IsOfClass(node
, wxT("wxWrapSizer")));
162 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
164 // find the item to be managed by this sizeritem
165 wxXmlNode
*n
= GetParamNode(wxT("object"));
167 n
= GetParamNode(wxT("object_ref"));
172 // create a sizer item for it
173 wxSizerItem
* sitem
= MakeSizerItem();
175 // now fetch the item to be managed
176 bool old_gbs
= m_isGBS
;
177 bool old_ins
= m_isInside
;
178 wxSizer
*old_par
= m_parentSizer
;
180 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
181 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
182 m_isInside
= old_ins
;
183 m_parentSizer
= old_par
;
186 // and figure out what type it is
187 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
188 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
191 sitem
->AssignSizer(sizer
);
193 sitem
->AssignWindow(wnd
);
195 ReportError(n
, "unexpected item in sizer");
197 // finally, set other wxSizerItem attributes
198 SetSizerItemAttributes(sitem
);
205 ReportError("no window/sizer/spacer within sizeritem object");
211 wxObject
* wxSizerXmlHandler::Handle_spacer()
213 if ( !m_parentSizer
)
215 ReportError("spacer only allowed inside a sizer");
219 wxSizerItem
* sitem
= MakeSizerItem();
220 SetSizerItemAttributes(sitem
);
221 sitem
->AssignSpacer(GetSize());
227 wxObject
* wxSizerXmlHandler::Handle_sizer()
229 wxXmlNode
*parentNode
= m_node
->GetParent();
231 if ( !m_parentSizer
&&
232 (!parentNode
|| parentNode
->GetType() != wxXML_ELEMENT_NODE
||
235 ReportError("sizer must have a window parent");
239 // Create the sizer of the appropriate class.
240 wxSizer
* const sizer
= DoCreateSizer(m_class
);
242 // creation of sizer failed for some (already reported) reason, so exit:
246 wxSize minsize
= GetSize(wxT("minsize"));
247 if (!(minsize
== wxDefaultSize
))
248 sizer
->SetMinSize(minsize
);
251 wxSizer
*old_par
= m_parentSizer
;
252 bool old_ins
= m_isInside
;
255 m_parentSizer
= sizer
;
257 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
259 wxObject
* parent
= m_parent
;
261 // wxStaticBoxSizer's child controls should be parented by the box itself,
263 wxStaticBoxSizer
* const stsizer
= wxDynamicCast(sizer
, wxStaticBoxSizer
);
265 parent
= stsizer
->GetStaticBox();
266 #endif // wxUSE_STATBOX
268 CreateChildren(parent
, true/*only this handler*/);
270 // set growable rows and cols for sizers which support this
271 if ( wxFlexGridSizer
*flexsizer
= wxDynamicCast(sizer
, wxFlexGridSizer
) )
273 SetFlexibleMode(flexsizer
);
274 SetGrowables(flexsizer
, wxT("growablerows"), true);
275 SetGrowables(flexsizer
, wxT("growablecols"), false);
279 m_isInside
= old_ins
;
280 m_parentSizer
= old_par
;
282 if (m_parentSizer
== NULL
) // setup window:
284 m_parentAsWindow
->SetSizer(sizer
);
286 wxXmlNode
*nd
= m_node
;
288 if (GetSize() == wxDefaultSize
)
290 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
292 sizer
->FitInside(m_parentAsWindow
);
296 sizer
->Fit(m_parentAsWindow
);
301 if (m_parentAsWindow
->IsTopLevel())
303 sizer
->SetSizeHints(m_parentAsWindow
);
311 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
313 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
317 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
319 return new wxStaticBoxSizer(
320 new wxStaticBox(m_parentAsWindow
,
322 GetText(wxT("label")),
323 wxDefaultPosition
, wxDefaultSize
,
326 GetStyle(wxT("orient"), wxHORIZONTAL
));
328 #endif // wxUSE_STATBOX
330 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
332 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
333 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
337 wxFlexGridSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
339 if ( !ValidateGridSizerChildren() )
341 return new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
342 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
346 wxGridBagSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
348 if ( !ValidateGridSizerChildren() )
350 return new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
353 wxSizer
* wxSizerXmlHandler::Handle_wxWrapSizer()
355 wxWrapSizer
*sizer
= new wxWrapSizer(GetStyle("orient"), GetStyle("flag"));
360 bool wxSizerXmlHandler::ValidateGridSizerChildren()
362 int rows
= GetLong("rows");
363 int cols
= GetLong("cols");
367 // fixed number of cells, need to verify children count
369 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
371 if ( n
->GetType() == wxXML_ELEMENT_NODE
&&
372 (n
->GetName() == "object" || n
->GetName() == "object_ref") )
378 if ( children
> rows
* cols
)
384 "too many children in grid sizer: %d > %d x %d"
385 " (consider omitting the number of rows or columns)",
399 void wxSizerXmlHandler::SetFlexibleMode(wxFlexGridSizer
* fsizer
)
401 if (HasParam(wxT("flexibledirection")))
403 wxString dir
= GetParamValue(wxT("flexibledirection"));
405 if (dir
== wxT("wxVERTICAL"))
406 fsizer
->SetFlexibleDirection(wxVERTICAL
);
407 else if (dir
== wxT("wxHORIZONTAL"))
408 fsizer
->SetFlexibleDirection(wxHORIZONTAL
);
409 else if (dir
== wxT("wxBOTH"))
410 fsizer
->SetFlexibleDirection(wxBOTH
);
415 wxT("flexibledirection"),
416 wxString::Format("unknown direction \"%s\"", dir
)
421 if (HasParam(wxT("nonflexiblegrowmode")))
423 wxString mode
= GetParamValue(wxT("nonflexiblegrowmode"));
425 if (mode
== wxT("wxFLEX_GROWMODE_NONE"))
426 fsizer
->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_NONE
);
427 else if (mode
== wxT("wxFLEX_GROWMODE_SPECIFIED"))
428 fsizer
->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_SPECIFIED
);
429 else if (mode
== wxT("wxFLEX_GROWMODE_ALL"))
430 fsizer
->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL
);
435 wxT("nonflexiblegrowmode"),
436 wxString::Format("unknown grow mode \"%s\"", mode
)
443 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
448 sizer
->CalcRowsCols(nrows
, ncols
);
449 const int nslots
= rows
? nrows
: ncols
;
451 wxStringTokenizer tkn
;
452 tkn
.SetString(GetParamValue(param
), wxT(","));
454 while (tkn
.HasMoreTokens())
457 wxString idxStr
= tkn
.GetNextToken().BeforeFirst(wxT(':'), &propStr
);
460 if (!idxStr
.ToULong(&li
))
465 "value must be a comma-separated list of numbers"
470 unsigned long lp
= 0;
471 if (!propStr
.empty())
473 if (!propStr
.ToULong(&lp
))
478 "value must be a comma-separated list of numbers"
484 const int n
= static_cast<int>(li
);
492 "invalid %s index %d: must be less than %d",
493 rows
? "row" : "column",
499 // ignore incorrect value, still try to process the rest
504 sizer
->AddGrowableRow(n
, static_cast<int>(lp
));
506 sizer
->AddGrowableCol(n
, static_cast<int>(lp
));
511 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
513 wxSize sz
= GetSize(param
);
514 if (sz
.x
< 0) sz
.x
= 0;
515 if (sz
.y
< 0) sz
.y
= 0;
516 return wxGBPosition(sz
.x
, sz
.y
);
519 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
521 wxSize sz
= GetSize(param
);
522 if (sz
.x
< 1) sz
.x
= 1;
523 if (sz
.y
< 1) sz
.y
= 1;
524 return wxGBSpan(sz
.x
, sz
.y
);
529 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
532 return new wxGBSizerItem();
534 return new wxSizerItem();
537 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
539 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
540 sitem
->SetFlag(GetStyle(wxT("flag")));
541 sitem
->SetBorder(GetDimension(wxT("border")));
542 wxSize sz
= GetSize(wxT("minsize"));
543 if (!(sz
== wxDefaultSize
))
544 sitem
->SetMinSize(sz
);
545 sz
= GetSize(wxT("ratio"));
546 if (!(sz
== wxDefaultSize
))
551 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
552 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
553 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
556 // record the id of the item, if any, for use by XRCSIZERITEM()
557 sitem
->SetId(GetID());
560 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
563 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
565 m_parentSizer
->Add(sitem
);
570 //-----------------------------------------------------------------------------
571 // wxStdDialogButtonSizerXmlHandler
572 //-----------------------------------------------------------------------------
575 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
577 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
578 : m_isInside(false), m_parentSizer(NULL
)
582 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
584 if (m_class
== wxT("wxStdDialogButtonSizer"))
586 wxASSERT( !m_parentSizer
);
588 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
591 CreateChildren(m_parent
, true/*only this handler*/);
593 m_parentSizer
->Realize();
596 m_parentSizer
= NULL
;
600 else // m_class == "button"
602 wxASSERT( m_parentSizer
);
604 // find the item to be managed by this sizeritem
605 wxXmlNode
*n
= GetParamNode(wxT("object"));
607 n
= GetParamNode(wxT("object_ref"));
612 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
613 wxButton
*button
= wxDynamicCast(item
, wxButton
);
616 m_parentSizer
->AddButton(button
);
618 ReportError(n
, "expected wxButton");
624 ReportError("no button within wxStdDialogButtonSizer");
630 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
632 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
633 (m_isInside
&& IsOfClass(node
, wxT("button")));
635 #endif // wxUSE_BUTTON