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"
24 #include "wx/xrc/xh_sizer.h"
26 #include "wx/gbsizer.h"
28 #include "wx/statbox.h"
29 #include "wx/notebook.h"
31 #include "wx/tokenzr.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
39 wxSizerXmlHandler::wxSizerXmlHandler()
40 : wxXmlResourceHandler(),
45 XRC_ADD_STYLE(wxHORIZONTAL
);
46 XRC_ADD_STYLE(wxVERTICAL
);
49 XRC_ADD_STYLE(wxLEFT
);
50 XRC_ADD_STYLE(wxRIGHT
);
52 XRC_ADD_STYLE(wxBOTTOM
);
53 XRC_ADD_STYLE(wxNORTH
);
54 XRC_ADD_STYLE(wxSOUTH
);
55 XRC_ADD_STYLE(wxEAST
);
56 XRC_ADD_STYLE(wxWEST
);
59 XRC_ADD_STYLE(wxGROW
);
60 XRC_ADD_STYLE(wxEXPAND
);
61 XRC_ADD_STYLE(wxSHAPED
);
62 XRC_ADD_STYLE(wxSTRETCH_NOT
);
64 XRC_ADD_STYLE(wxALIGN_CENTER
);
65 XRC_ADD_STYLE(wxALIGN_CENTRE
);
66 XRC_ADD_STYLE(wxALIGN_LEFT
);
67 XRC_ADD_STYLE(wxALIGN_TOP
);
68 XRC_ADD_STYLE(wxALIGN_RIGHT
);
69 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
70 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
71 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
72 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
73 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
75 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
76 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
81 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
83 return ( (!m_isInside
&& IsSizerNode(node
)) ||
84 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
85 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
90 wxObject
* wxSizerXmlHandler::DoCreateResource()
92 if (m_class
== wxT("sizeritem"))
93 return Handle_sizeritem();
95 else if (m_class
== wxT("spacer"))
96 return Handle_spacer();
99 return Handle_sizer();
105 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
107 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
108 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
109 (IsOfClass(node
, wxT("wxGridSizer"))) ||
110 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
111 (IsOfClass(node
, wxT("wxGridBagSizer")));
115 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
117 // find the item to be managed by this sizeritem
118 wxXmlNode
*n
= GetParamNode(wxT("object"));
120 n
= GetParamNode(wxT("object_ref"));
125 // create a sizer item for it
126 wxSizerItem
* sitem
= MakeSizerItem();
128 // now fetch the item to be managed
129 bool old_gbs
= m_isGBS
;
130 bool old_ins
= m_isInside
;
131 wxSizer
*old_par
= m_parentSizer
;
133 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
134 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
135 m_isInside
= old_ins
;
136 m_parentSizer
= old_par
;
139 // and figure out what type it is
140 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
141 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
144 sitem
->SetSizer(sizer
);
146 sitem
->SetWindow(wnd
);
148 wxLogError(wxT("Error in resource."));
150 // finally, set other wxSizerItem attributes
151 SetSizerItemAttributes(sitem
);
158 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
164 wxObject
* wxSizerXmlHandler::Handle_spacer()
166 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
168 wxSizerItem
* sitem
= MakeSizerItem();
169 SetSizerItemAttributes(sitem
);
170 sitem
->SetSpacer(GetSize());
176 wxObject
* wxSizerXmlHandler::Handle_sizer()
178 wxSizer
*sizer
= NULL
;
180 wxXmlNode
*parentNode
= m_node
->GetParent();
182 wxCHECK_MSG(m_parentSizer
!= NULL
||
183 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
184 m_parentAsWindow
!= NULL
&&
185 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
186 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
187 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
189 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
191 if (m_class
== wxT("wxBoxSizer"))
192 sizer
= Handle_wxBoxSizer();
194 else if (m_class
== wxT("wxStaticBoxSizer"))
195 sizer
= Handle_wxStaticBoxSizer();
197 else if (m_class
== wxT("wxGridSizer"))
198 sizer
= Handle_wxGridSizer();
200 else if (m_class
== wxT("wxFlexGridSizer"))
201 sizer
= Handle_wxFlexGridSizer();
203 else if (m_class
== wxT("wxGridBagSizer"))
204 sizer
= Handle_wxGridBagSizer();
207 wxSize minsize
= GetSize(wxT("minsize"));
208 if (!(minsize
== wxDefaultSize
))
209 sizer
->SetMinSize(minsize
);
212 wxSizer
*old_par
= m_parentSizer
;
213 bool old_ins
= m_isInside
;
216 m_parentSizer
= sizer
;
218 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
220 CreateChildren(m_parent
, true/*only this handler*/);
223 m_isInside
= old_ins
;
224 m_parentSizer
= old_par
;
226 if (m_parentSizer
== NULL
) // setup window:
228 m_parentAsWindow
->SetSizer(sizer
);
230 wxXmlNode
*nd
= m_node
;
232 if (GetSize() == wxDefaultSize
)
233 sizer
->Fit(m_parentAsWindow
);
236 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
237 sizer
->SetSizeHints(m_parentAsWindow
);
244 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
246 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
249 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
251 return new wxStaticBoxSizer(
252 new wxStaticBox(m_parentAsWindow
,
254 GetText(wxT("label")),
255 wxDefaultPosition
, wxDefaultSize
,
258 GetStyle(wxT("orient"), wxHORIZONTAL
));
261 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
263 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
264 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
268 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
270 wxFlexGridSizer
*sizer
=
271 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
272 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
273 SetGrowables(sizer
, wxT("growablerows"), true);
274 SetGrowables(sizer
, wxT("growablecols"), false);
279 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
281 wxGridBagSizer
*sizer
=
282 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
283 SetGrowables(sizer
, wxT("growablerows"), true);
284 SetGrowables(sizer
, wxT("growablecols"), false);
291 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
295 wxStringTokenizer tkn
;
297 tkn
.SetString(GetParamValue(param
), wxT(","));
298 while (tkn
.HasMoreTokens())
300 if (!tkn
.GetNextToken().ToULong(&l
))
301 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
304 sizer
->AddGrowableRow(l
);
306 sizer
->AddGrowableCol(l
);
312 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
314 wxSize sz
= GetSize(param
);
315 if (sz
.x
< 0) sz
.x
= 0;
316 if (sz
.y
< 0) sz
.y
= 0;
317 return wxGBPosition(sz
.x
, sz
.y
);
320 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
322 wxSize sz
= GetSize(param
);
323 if (sz
.x
< 1) sz
.x
= 1;
324 if (sz
.y
< 1) sz
.y
= 1;
325 return wxGBSpan(sz
.x
, sz
.y
);
330 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
333 return new wxGBSizerItem();
335 return new wxSizerItem();
338 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
340 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
341 sitem
->SetFlag(GetStyle(wxT("flag")));
342 sitem
->SetBorder(GetDimension(wxT("border")));
343 wxSize sz
= GetSize(wxT("minsize"));
344 if (!(sz
== wxDefaultSize
))
345 sitem
->SetMinSize(sz
);
346 sz
= GetSize(wxT("ratio"));
347 if (!(sz
== wxDefaultSize
))
352 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
353 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
354 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
358 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
361 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
363 m_parentSizer
->Add(sitem
);