1 /////////////////////////////////////////////////////////////////////////////
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"
22 #include "wx/gbsizer.h"
24 #include "wx/statbox.h"
25 #include "wx/notebook.h"
27 #include "wx/tokenzr.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
36 wxSizerXmlHandler::wxSizerXmlHandler()
37 : wxXmlResourceHandler(),
42 XRC_ADD_STYLE(wxHORIZONTAL
);
43 XRC_ADD_STYLE(wxVERTICAL
);
46 XRC_ADD_STYLE(wxLEFT
);
47 XRC_ADD_STYLE(wxRIGHT
);
49 XRC_ADD_STYLE(wxBOTTOM
);
50 XRC_ADD_STYLE(wxNORTH
);
51 XRC_ADD_STYLE(wxSOUTH
);
52 XRC_ADD_STYLE(wxEAST
);
53 XRC_ADD_STYLE(wxWEST
);
56 XRC_ADD_STYLE(wxGROW
);
57 XRC_ADD_STYLE(wxEXPAND
);
58 XRC_ADD_STYLE(wxSHAPED
);
59 XRC_ADD_STYLE(wxSTRETCH_NOT
);
61 XRC_ADD_STYLE(wxALIGN_CENTER
);
62 XRC_ADD_STYLE(wxALIGN_CENTRE
);
63 XRC_ADD_STYLE(wxALIGN_LEFT
);
64 XRC_ADD_STYLE(wxALIGN_TOP
);
65 XRC_ADD_STYLE(wxALIGN_RIGHT
);
66 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
67 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
68 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
69 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
70 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
72 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
73 XRC_ADD_STYLE(wxFIXED_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();
125 // now fetch the item to be managed
126 bool old_gbs
= m_isGBS
;
127 bool old_ins
= m_isInside
;
128 wxSizer
*old_par
= m_parentSizer
;
130 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
131 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
132 m_isInside
= old_ins
;
133 m_parentSizer
= old_par
;
136 // and figure out what type it is
137 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
138 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
141 sitem
->SetSizer(sizer
);
143 sitem
->SetWindow(wnd
);
145 wxLogError(wxT("Error in resource."));
147 // finally, set other wxSizerItem attributes
148 SetSizerItemAttributes(sitem
);
155 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
161 wxObject
* wxSizerXmlHandler::Handle_spacer()
163 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
165 wxSizerItem
* sitem
= MakeSizerItem();
166 SetSizerItemAttributes(sitem
);
167 sitem
->SetSpacer(GetSize());
173 wxObject
* wxSizerXmlHandler::Handle_sizer()
175 wxSizer
*sizer
= NULL
;
177 wxXmlNode
*parentNode
= m_node
->GetParent();
179 wxCHECK_MSG(m_parentSizer
!= NULL
||
180 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
181 m_parentAsWindow
!= NULL
&&
182 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
183 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
184 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
186 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
188 if (m_class
== wxT("wxBoxSizer"))
189 sizer
= Handle_wxBoxSizer();
191 else if (m_class
== wxT("wxStaticBoxSizer"))
192 sizer
= Handle_wxStaticBoxSizer();
194 else if (m_class
== wxT("wxGridSizer"))
195 sizer
= Handle_wxGridSizer();
197 else if (m_class
== wxT("wxFlexGridSizer"))
198 sizer
= Handle_wxFlexGridSizer();
200 else if (m_class
== wxT("wxGridBagSizer"))
201 sizer
= Handle_wxGridBagSizer();
205 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
209 wxSize minsize
= GetSize(wxT("minsize"));
210 if (!(minsize
== wxDefaultSize
))
211 sizer
->SetMinSize(minsize
);
214 wxSizer
*old_par
= m_parentSizer
;
215 bool old_ins
= m_isInside
;
218 m_parentSizer
= sizer
;
220 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
222 CreateChildren(m_parent
, true/*only this handler*/);
225 m_isInside
= old_ins
;
226 m_parentSizer
= old_par
;
228 if (m_parentSizer
== NULL
) // setup window:
230 m_parentAsWindow
->SetSizer(sizer
);
232 wxXmlNode
*nd
= m_node
;
234 if (GetSize() == wxDefaultSize
)
235 sizer
->Fit(m_parentAsWindow
);
238 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
239 sizer
->SetSizeHints(m_parentAsWindow
);
246 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
248 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
251 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
253 return new wxStaticBoxSizer(
254 new wxStaticBox(m_parentAsWindow
,
256 GetText(wxT("label")),
257 wxDefaultPosition
, wxDefaultSize
,
260 GetStyle(wxT("orient"), wxHORIZONTAL
));
263 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
265 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
266 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
270 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
272 wxFlexGridSizer
*sizer
=
273 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
274 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
275 SetGrowables(sizer
, wxT("growablerows"), true);
276 SetGrowables(sizer
, wxT("growablecols"), false);
281 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
283 wxGridBagSizer
*sizer
=
284 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
285 SetGrowables(sizer
, wxT("growablerows"), true);
286 SetGrowables(sizer
, wxT("growablecols"), false);
293 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
297 wxStringTokenizer tkn
;
299 tkn
.SetString(GetParamValue(param
), wxT(","));
300 while (tkn
.HasMoreTokens())
302 if (!tkn
.GetNextToken().ToULong(&l
))
303 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
306 sizer
->AddGrowableRow(l
);
308 sizer
->AddGrowableCol(l
);
314 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
316 wxSize sz
= GetSize(param
);
317 if (sz
.x
< 0) sz
.x
= 0;
318 if (sz
.y
< 0) sz
.y
= 0;
319 return wxGBPosition(sz
.x
, sz
.y
);
322 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
324 wxSize sz
= GetSize(param
);
325 if (sz
.x
< 1) sz
.x
= 1;
326 if (sz
.y
< 1) sz
.y
= 1;
327 return wxGBSpan(sz
.x
, sz
.y
);
332 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
335 return new wxGBSizerItem();
337 return new wxSizerItem();
340 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
342 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
343 sitem
->SetFlag(GetStyle(wxT("flag")));
344 sitem
->SetBorder(GetDimension(wxT("border")));
345 wxSize sz
= GetSize(wxT("minsize"));
346 if (!(sz
== wxDefaultSize
))
347 sitem
->SetMinSize(sz
);
348 sz
= GetSize(wxT("ratio"));
349 if (!(sz
== wxDefaultSize
))
354 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
355 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
356 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
360 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
363 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
365 m_parentSizer
->Add(sitem
);
370 //-----------------------------------------------------------------------------
371 // wxStdDialogButtonSizerXmlHandler
372 //-----------------------------------------------------------------------------
374 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
376 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
377 : m_isInside(false), m_parentSizer(NULL
)
381 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
383 if (m_class
== wxT("wxStdDialogButtonSizer"))
385 wxASSERT( !m_parentSizer
);
387 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
390 CreateChildren(m_parent
, true/*only this handler*/);
392 m_parentSizer
->Realize();
395 m_parentSizer
= NULL
;
399 else // m_class == "button"
401 wxASSERT( m_parentSizer
);
403 // find the item to be managed by this sizeritem
404 wxXmlNode
*n
= GetParamNode(wxT("object"));
406 n
= GetParamNode(wxT("object_ref"));
411 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
412 wxButton
*button
= wxDynamicCast(item
, wxButton
);
415 m_parentSizer
->AddButton(button
);
417 wxLogError(wxT("Error in resource - expected button."));
423 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
429 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
431 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
432 (m_isInside
&& IsOfClass(node
, wxT("button")));