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
);
78 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
80 return ( (!m_isInside
&& IsSizerNode(node
)) ||
81 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
82 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
87 wxObject
* wxSizerXmlHandler::DoCreateResource()
89 if (m_class
== wxT("sizeritem"))
90 return Handle_sizeritem();
92 else if (m_class
== wxT("spacer"))
93 return Handle_spacer();
96 return Handle_sizer();
102 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
104 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
105 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
106 (IsOfClass(node
, wxT("wxGridSizer"))) ||
107 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
108 (IsOfClass(node
, wxT("wxGridBagSizer")));
112 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
114 // find the item to be managed by this sizeritem
115 wxXmlNode
*n
= GetParamNode(wxT("object"));
117 n
= GetParamNode(wxT("object_ref"));
122 // create a sizer item for it
123 wxSizerItem
* sitem
= MakeSizerItem();
124 SetSizerItemAttributes(sitem
);
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."));
153 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
159 wxObject
* wxSizerXmlHandler::Handle_spacer()
161 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
163 wxSizerItem
* sitem
= MakeSizerItem();
164 SetSizerItemAttributes(sitem
);
165 sitem
->SetSpacer(GetSize());
171 wxObject
* wxSizerXmlHandler::Handle_sizer()
173 wxSizer
*sizer
= NULL
;
175 wxXmlNode
*parentNode
= m_node
->GetParent();
177 wxCHECK_MSG(m_parentSizer
!= NULL
||
178 (parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
179 m_parentAsWindow
!= NULL
&&
180 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
181 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
182 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
184 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
186 if (m_class
== wxT("wxBoxSizer"))
187 sizer
= Handle_wxBoxSizer();
189 else if (m_class
== wxT("wxStaticBoxSizer"))
190 sizer
= Handle_wxStaticBoxSizer();
192 else if (m_class
== wxT("wxGridSizer"))
193 sizer
= Handle_wxGridSizer();
195 else if (m_class
== wxT("wxFlexGridSizer"))
196 sizer
= Handle_wxFlexGridSizer();
198 else if (m_class
== wxT("wxGridBagSizer"))
199 sizer
= Handle_wxGridBagSizer();
202 wxSize minsize
= GetSize(wxT("minsize"));
203 if (!(minsize
== wxDefaultSize
))
204 sizer
->SetMinSize(minsize
);
207 wxSizer
*old_par
= m_parentSizer
;
208 bool old_ins
= m_isInside
;
209 bool old_gbs
= m_isGBS
;
212 m_parentSizer
= sizer
;
214 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
216 CreateChildren(m_parent
, TRUE
/*only this handler*/);
220 m_isInside
= old_ins
;
221 m_parentSizer
= old_par
;
223 if (m_parentSizer
== NULL
) // setup window:
225 m_parentAsWindow
->SetAutoLayout(TRUE
);
226 m_parentAsWindow
->SetSizer(sizer
);
228 wxXmlNode
*nd
= m_node
;
230 if (GetSize() == wxDefaultSize
)
231 sizer
->Fit(m_parentAsWindow
);
234 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
235 sizer
->SetSizeHints(m_parentAsWindow
);
242 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
244 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
247 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
249 return new wxStaticBoxSizer(
250 new wxStaticBox(m_parentAsWindow
,
252 GetText(wxT("label")),
253 wxDefaultPosition
, wxDefaultSize
,
256 GetStyle(wxT("orient"), wxHORIZONTAL
));
259 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
261 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
262 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
266 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
268 wxFlexGridSizer
*sizer
=
269 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
270 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
271 SetGrowables(sizer
, wxT("growablerows"), true);
272 SetGrowables(sizer
, wxT("growablecols"), false);
277 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
279 wxGridBagSizer
*sizer
=
280 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
281 SetGrowables(sizer
, wxT("growablerows"), true);
282 SetGrowables(sizer
, wxT("growablecols"), false);
289 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
293 wxStringTokenizer tkn
;
295 tkn
.SetString(GetParamValue(param
), wxT(","));
296 while (tkn
.HasMoreTokens())
298 if (!tkn
.GetNextToken().ToULong(&l
))
299 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
302 sizer
->AddGrowableRow(l
);
304 sizer
->AddGrowableCol(l
);
310 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
312 wxSize sz
= GetSize(param
);
313 if (sz
.x
< 0) sz
.x
= 0;
314 if (sz
.y
< 0) sz
.y
= 0;
315 return wxGBPosition(sz
.x
, sz
.y
);
318 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
320 wxSize sz
= GetSize(param
);
321 if (sz
.x
< 1) sz
.x
= 1;
322 if (sz
.y
< 1) sz
.y
= 1;
323 return wxGBSpan(sz
.x
, sz
.y
);
328 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
331 return new wxGBSizerItem();
333 return new wxSizerItem();
336 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
338 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
339 sitem
->SetFlag(GetStyle(wxT("flag")));
340 sitem
->SetBorder(GetDimension(wxT("border")));
341 wxSize sz
= GetSize(wxT("minsize"));
342 if (!(sz
== wxDefaultSize
))
343 sitem
->SetInitSize(sz
.x
, sz
.y
);
344 sz
= GetSize(wxT("ratio"));
345 if (!(sz
== wxDefaultSize
))
350 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
351 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
352 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
356 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
359 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
361 m_parentSizer
->Add(sitem
);