1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xh_sizer.cpp
3 // Purpose: XRC resource for wxBoxSizer
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/xrc/xh_sizer.h"
25 #include "wx/statbox.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
30 #include "wx/scrolwin.h"
33 #include "wx/gbsizer.h"
34 #include "wx/wrapsizer.h"
35 #include "wx/notebook.h"
36 #include "wx/tokenzr.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 SetGrowables(flexsizer
, wxT("growablerows"), true);
274 SetGrowables(flexsizer
, wxT("growablecols"), false);
278 m_isInside
= old_ins
;
279 m_parentSizer
= old_par
;
281 if (m_parentSizer
== NULL
) // setup window:
283 m_parentAsWindow
->SetSizer(sizer
);
285 wxXmlNode
*nd
= m_node
;
287 if (GetSize() == wxDefaultSize
)
289 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
291 sizer
->FitInside(m_parentAsWindow
);
295 sizer
->Fit(m_parentAsWindow
);
300 if (m_parentAsWindow
->IsTopLevel())
302 sizer
->SetSizeHints(m_parentAsWindow
);
310 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
312 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
316 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
318 return new wxStaticBoxSizer(
319 new wxStaticBox(m_parentAsWindow
,
321 GetText(wxT("label")),
322 wxDefaultPosition
, wxDefaultSize
,
325 GetStyle(wxT("orient"), wxHORIZONTAL
));
327 #endif // wxUSE_STATBOX
329 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
331 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
332 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
336 wxFlexGridSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
338 if ( !ValidateGridSizerChildren() )
340 return new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
341 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
345 wxGridBagSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
347 if ( !ValidateGridSizerChildren() )
349 return new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
352 wxSizer
* wxSizerXmlHandler::Handle_wxWrapSizer()
354 wxWrapSizer
*sizer
= new wxWrapSizer(GetStyle("orient"), GetStyle("flag"));
359 bool wxSizerXmlHandler::ValidateGridSizerChildren()
361 int rows
= GetLong("rows");
362 int cols
= GetLong("cols");
366 // fixed number of cells, need to verify children count
368 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
370 if ( n
->GetType() == wxXML_ELEMENT_NODE
&&
371 (n
->GetName() == "object" || n
->GetName() == "object_ref") )
377 if ( children
> rows
* cols
)
383 "too many children in grid sizer: %d > %d x %d"
384 " (consider omitting the number of rows or columns)",
398 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
403 sizer
->CalcRowsCols(nrows
, ncols
);
404 const int nslots
= rows
? nrows
: ncols
;
406 wxStringTokenizer tkn
;
407 tkn
.SetString(GetParamValue(param
), wxT(","));
409 while (tkn
.HasMoreTokens())
412 if (!tkn
.GetNextToken().ToULong(&l
))
417 "value must be comma-separated list of row numbers"
422 if ( (int)l
>= nslots
)
429 "invalid %s index %d: must be less than %d",
430 rows
? "row" : "column",
436 // ignore incorrect value, still try to process the rest
441 sizer
->AddGrowableRow(l
);
443 sizer
->AddGrowableCol(l
);
448 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
450 wxSize sz
= GetSize(param
);
451 if (sz
.x
< 0) sz
.x
= 0;
452 if (sz
.y
< 0) sz
.y
= 0;
453 return wxGBPosition(sz
.x
, sz
.y
);
456 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
458 wxSize sz
= GetSize(param
);
459 if (sz
.x
< 1) sz
.x
= 1;
460 if (sz
.y
< 1) sz
.y
= 1;
461 return wxGBSpan(sz
.x
, sz
.y
);
466 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
469 return new wxGBSizerItem();
471 return new wxSizerItem();
474 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
476 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
477 sitem
->SetFlag(GetStyle(wxT("flag")));
478 sitem
->SetBorder(GetDimension(wxT("border")));
479 wxSize sz
= GetSize(wxT("minsize"));
480 if (!(sz
== wxDefaultSize
))
481 sitem
->SetMinSize(sz
);
482 sz
= GetSize(wxT("ratio"));
483 if (!(sz
== wxDefaultSize
))
488 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
489 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
490 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
493 // record the id of the item, if any, for use by XRCSIZERITEM()
494 sitem
->SetId(GetID());
497 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
500 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
502 m_parentSizer
->Add(sitem
);
507 //-----------------------------------------------------------------------------
508 // wxStdDialogButtonSizerXmlHandler
509 //-----------------------------------------------------------------------------
512 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
514 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
515 : m_isInside(false), m_parentSizer(NULL
)
519 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
521 if (m_class
== wxT("wxStdDialogButtonSizer"))
523 wxASSERT( !m_parentSizer
);
525 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
528 CreateChildren(m_parent
, true/*only this handler*/);
530 m_parentSizer
->Realize();
533 m_parentSizer
= NULL
;
537 else // m_class == "button"
539 wxASSERT( m_parentSizer
);
541 // find the item to be managed by this sizeritem
542 wxXmlNode
*n
= GetParamNode(wxT("object"));
544 n
= GetParamNode(wxT("object_ref"));
549 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
550 wxButton
*button
= wxDynamicCast(item
, wxButton
);
553 m_parentSizer
->AddButton(button
);
555 ReportError(n
, "expected wxButton");
561 ReportError("no button within wxStdDialogButtonSizer");
567 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
569 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
570 (m_isInside
&& IsOfClass(node
, wxT("button")));
572 #endif // wxUSE_BUTTON