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();
126 // now fetch the item to be managed
127 bool old_gbs
= m_isGBS
;
128 bool old_ins
= m_isInside
;
129 wxSizer
*old_par
= m_parentSizer
;
131 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
132 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
133 m_isInside
= old_ins
;
134 m_parentSizer
= old_par
;
137 // and figure out what type it is
138 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
139 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
142 sitem
->SetSizer(sizer
);
144 sitem
->SetWindow(wnd
);
146 wxLogError(wxT("Error in resource."));
148 // finally, set other wxSizerItem attributes
149 SetSizerItemAttributes(sitem
);
156 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
162 wxObject
* wxSizerXmlHandler::Handle_spacer()
164 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
166 wxSizerItem
* sitem
= MakeSizerItem();
167 SetSizerItemAttributes(sitem
);
168 sitem
->SetSpacer(GetSize());
174 wxObject
* wxSizerXmlHandler::Handle_sizer()
176 wxSizer
*sizer
= NULL
;
178 wxXmlNode
*parentNode
= m_node
->GetParent();
180 wxCHECK_MSG(m_parentSizer
!= NULL
||
181 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
182 m_parentAsWindow
!= NULL
&&
183 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
184 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
185 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
187 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
189 if (m_class
== wxT("wxBoxSizer"))
190 sizer
= Handle_wxBoxSizer();
192 else if (m_class
== wxT("wxStaticBoxSizer"))
193 sizer
= Handle_wxStaticBoxSizer();
195 else if (m_class
== wxT("wxGridSizer"))
196 sizer
= Handle_wxGridSizer();
198 else if (m_class
== wxT("wxFlexGridSizer"))
199 sizer
= Handle_wxFlexGridSizer();
201 else if (m_class
== wxT("wxGridBagSizer"))
202 sizer
= Handle_wxGridBagSizer();
205 wxSize minsize
= GetSize(wxT("minsize"));
206 if (!(minsize
== wxDefaultSize
))
207 sizer
->SetMinSize(minsize
);
210 wxSizer
*old_par
= m_parentSizer
;
211 bool old_ins
= m_isInside
;
214 m_parentSizer
= sizer
;
216 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
218 CreateChildren(m_parent
, TRUE
/*only this handler*/);
221 m_isInside
= old_ins
;
222 m_parentSizer
= old_par
;
224 if (m_parentSizer
== NULL
) // setup window:
226 m_parentAsWindow
->SetAutoLayout(TRUE
);
227 m_parentAsWindow
->SetSizer(sizer
);
229 wxXmlNode
*nd
= m_node
;
231 if (GetSize() == wxDefaultSize
)
232 sizer
->Fit(m_parentAsWindow
);
235 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
236 sizer
->SetSizeHints(m_parentAsWindow
);
243 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
245 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
248 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
250 return new wxStaticBoxSizer(
251 new wxStaticBox(m_parentAsWindow
,
253 GetText(wxT("label")),
254 wxDefaultPosition
, wxDefaultSize
,
257 GetStyle(wxT("orient"), wxHORIZONTAL
));
260 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
262 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
263 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
267 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
269 wxFlexGridSizer
*sizer
=
270 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
271 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
272 SetGrowables(sizer
, wxT("growablerows"), true);
273 SetGrowables(sizer
, wxT("growablecols"), false);
278 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
280 wxGridBagSizer
*sizer
=
281 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
282 SetGrowables(sizer
, wxT("growablerows"), true);
283 SetGrowables(sizer
, wxT("growablecols"), false);
290 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
294 wxStringTokenizer tkn
;
296 tkn
.SetString(GetParamValue(param
), wxT(","));
297 while (tkn
.HasMoreTokens())
299 if (!tkn
.GetNextToken().ToULong(&l
))
300 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
303 sizer
->AddGrowableRow(l
);
305 sizer
->AddGrowableCol(l
);
311 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
313 wxSize sz
= GetSize(param
);
314 if (sz
.x
< 0) sz
.x
= 0;
315 if (sz
.y
< 0) sz
.y
= 0;
316 return wxGBPosition(sz
.x
, sz
.y
);
319 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
321 wxSize sz
= GetSize(param
);
322 if (sz
.x
< 1) sz
.x
= 1;
323 if (sz
.y
< 1) sz
.y
= 1;
324 return wxGBSpan(sz
.x
, sz
.y
);
329 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
332 return new wxGBSizerItem();
334 return new wxSizerItem();
337 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
339 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
340 sitem
->SetFlag(GetStyle(wxT("flag")));
341 sitem
->SetBorder(GetDimension(wxT("border")));
342 wxSize sz
= GetSize(wxT("minsize"));
343 if (!(sz
== wxDefaultSize
))
344 sitem
->SetMinSize(sz
);
345 sz
= GetSize(wxT("ratio"));
346 if (!(sz
== wxDefaultSize
))
351 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
352 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
353 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
357 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
360 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
362 m_parentSizer
->Add(sitem
);