1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resource for wxBoxSizer
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xh_sizer.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/xrc/xh_sizer.h"
24 #include "wx/gbsizer.h"
26 #include "wx/statbox.h"
27 #include "wx/notebook.h"
29 #include "wx/tokenzr.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
37 wxSizerXmlHandler::wxSizerXmlHandler()
38 : wxXmlResourceHandler(),
43 XRC_ADD_STYLE(wxHORIZONTAL
);
44 XRC_ADD_STYLE(wxVERTICAL
);
47 XRC_ADD_STYLE(wxLEFT
);
48 XRC_ADD_STYLE(wxRIGHT
);
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
);
57 XRC_ADD_STYLE(wxGROW
);
58 XRC_ADD_STYLE(wxEXPAND
);
59 XRC_ADD_STYLE(wxSHAPED
);
60 XRC_ADD_STYLE(wxSTRETCH_NOT
);
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
);
73 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
74 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
79 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
81 return ( (!m_isInside
&& IsSizerNode(node
)) ||
82 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
83 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
88 wxObject
* wxSizerXmlHandler::DoCreateResource()
90 if (m_class
== wxT("sizeritem"))
91 return Handle_sizeritem();
93 else if (m_class
== wxT("spacer"))
94 return Handle_spacer();
97 return Handle_sizer();
103 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
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")));
113 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
115 // find the item to be managed by this sizeritem
116 wxXmlNode
*n
= GetParamNode(wxT("object"));
118 n
= GetParamNode(wxT("object_ref"));
123 // create a sizer item for it
124 wxSizerItem
* sitem
= MakeSizerItem();
125 SetSizerItemAttributes(sitem
);
127 // now fetch the item to be managed
128 bool old_gbs
= m_isGBS
;
129 bool old_ins
= m_isInside
;
130 wxSizer
*old_par
= m_parentSizer
;
132 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
133 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
134 m_isInside
= old_ins
;
135 m_parentSizer
= old_par
;
138 // and figure out what type it is
139 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
140 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
143 sitem
->SetSizer(sizer
);
145 sitem
->SetWindow(wnd
);
147 wxLogError(wxT("Error in resource."));
154 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
160 wxObject
* wxSizerXmlHandler::Handle_spacer()
162 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
164 wxSizerItem
* sitem
= MakeSizerItem();
165 SetSizerItemAttributes(sitem
);
166 sitem
->SetSpacer(GetSize());
172 wxObject
* wxSizerXmlHandler::Handle_sizer()
174 wxSizer
*sizer
= NULL
;
176 wxXmlNode
*parentNode
= m_node
->GetParent();
178 wxCHECK_MSG(m_parentSizer
!= NULL
||
179 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
180 m_parentAsWindow
!= NULL
&&
181 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
182 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
183 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
185 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
187 if (m_class
== wxT("wxBoxSizer"))
188 sizer
= Handle_wxBoxSizer();
190 else if (m_class
== wxT("wxStaticBoxSizer"))
191 sizer
= Handle_wxStaticBoxSizer();
193 else if (m_class
== wxT("wxGridSizer"))
194 sizer
= Handle_wxGridSizer();
196 else if (m_class
== wxT("wxFlexGridSizer"))
197 sizer
= Handle_wxFlexGridSizer();
199 else if (m_class
== wxT("wxGridBagSizer"))
200 sizer
= Handle_wxGridBagSizer();
203 wxSize minsize
= GetSize(wxT("minsize"));
204 if (!(minsize
== wxDefaultSize
))
205 sizer
->SetMinSize(minsize
);
208 wxSizer
*old_par
= m_parentSizer
;
209 bool old_ins
= m_isInside
;
212 m_parentSizer
= sizer
;
214 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
216 CreateChildren(m_parent
, TRUE
/*only this handler*/);
219 m_isInside
= old_ins
;
220 m_parentSizer
= old_par
;
222 if (m_parentSizer
== NULL
) // setup window:
224 m_parentAsWindow
->SetAutoLayout(TRUE
);
225 m_parentAsWindow
->SetSizer(sizer
);
227 wxXmlNode
*nd
= m_node
;
229 if (GetSize() == wxDefaultSize
)
230 sizer
->Fit(m_parentAsWindow
);
233 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
234 sizer
->SetSizeHints(m_parentAsWindow
);
241 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
243 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
246 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
248 return new wxStaticBoxSizer(
249 new wxStaticBox(m_parentAsWindow
,
251 GetText(wxT("label")),
252 wxDefaultPosition
, wxDefaultSize
,
255 GetStyle(wxT("orient"), wxHORIZONTAL
));
258 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
260 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
261 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
265 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
267 wxFlexGridSizer
*sizer
=
268 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
269 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
270 SetGrowables(sizer
, wxT("growablerows"), true);
271 SetGrowables(sizer
, wxT("growablecols"), false);
276 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
278 wxGridBagSizer
*sizer
=
279 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
280 SetGrowables(sizer
, wxT("growablerows"), true);
281 SetGrowables(sizer
, wxT("growablecols"), false);
288 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
292 wxStringTokenizer tkn
;
294 tkn
.SetString(GetParamValue(param
), wxT(","));
295 while (tkn
.HasMoreTokens())
297 if (!tkn
.GetNextToken().ToULong(&l
))
298 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
301 sizer
->AddGrowableRow(l
);
303 sizer
->AddGrowableCol(l
);
309 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
311 wxSize sz
= GetSize(param
);
312 if (sz
.x
< 0) sz
.x
= 0;
313 if (sz
.y
< 0) sz
.y
= 0;
314 return wxGBPosition(sz
.x
, sz
.y
);
317 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
319 wxSize sz
= GetSize(param
);
320 if (sz
.x
< 1) sz
.x
= 1;
321 if (sz
.y
< 1) sz
.y
= 1;
322 return wxGBSpan(sz
.x
, sz
.y
);
327 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
330 return new wxGBSizerItem();
332 return new wxSizerItem();
335 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
337 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
338 sitem
->SetFlag(GetStyle(wxT("flag")));
339 sitem
->SetBorder(GetDimension(wxT("border")));
340 wxSize sz
= GetSize(wxT("minsize"));
341 if (!(sz
== wxDefaultSize
))
342 sitem
->SetInitSize(sz
.x
, sz
.y
);
343 sz
= GetSize(wxT("ratio"));
344 if (!(sz
== wxDefaultSize
))
349 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
350 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
351 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
355 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
358 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
360 m_parentSizer
->Add(sitem
);