1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xh_sizer.cpp
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"
25 #include "wx/statbox.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
32 #include "wx/gbsizer.h"
33 #include "wx/notebook.h"
34 #include "wx/tokenzr.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
43 wxSizerXmlHandler::wxSizerXmlHandler()
44 :wxXmlResourceHandler(),
49 XRC_ADD_STYLE(wxHORIZONTAL
);
50 XRC_ADD_STYLE(wxVERTICAL
);
53 XRC_ADD_STYLE(wxLEFT
);
54 XRC_ADD_STYLE(wxRIGHT
);
56 XRC_ADD_STYLE(wxBOTTOM
);
57 XRC_ADD_STYLE(wxNORTH
);
58 XRC_ADD_STYLE(wxSOUTH
);
59 XRC_ADD_STYLE(wxEAST
);
60 XRC_ADD_STYLE(wxWEST
);
63 XRC_ADD_STYLE(wxGROW
);
64 XRC_ADD_STYLE(wxEXPAND
);
65 XRC_ADD_STYLE(wxSHAPED
);
66 XRC_ADD_STYLE(wxSTRETCH_NOT
);
68 XRC_ADD_STYLE(wxALIGN_CENTER
);
69 XRC_ADD_STYLE(wxALIGN_CENTRE
);
70 XRC_ADD_STYLE(wxALIGN_LEFT
);
71 XRC_ADD_STYLE(wxALIGN_TOP
);
72 XRC_ADD_STYLE(wxALIGN_RIGHT
);
73 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
74 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
75 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
76 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
77 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
79 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
84 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
86 return ( (!m_isInside
&& IsSizerNode(node
)) ||
87 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
88 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
93 wxObject
* wxSizerXmlHandler::DoCreateResource()
95 if (m_class
== wxT("sizeritem"))
96 return Handle_sizeritem();
98 else if (m_class
== wxT("spacer"))
99 return Handle_spacer();
102 return Handle_sizer();
108 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
110 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
111 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
112 (IsOfClass(node
, wxT("wxGridSizer"))) ||
113 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
114 (IsOfClass(node
, wxT("wxGridBagSizer")));
118 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
120 // find the item to be managed by this sizeritem
121 wxXmlNode
*n
= GetParamNode(wxT("object"));
123 n
= GetParamNode(wxT("object_ref"));
128 // create a sizer item for it
129 wxSizerItem
* sitem
= MakeSizerItem();
131 // now fetch the item to be managed
132 bool old_gbs
= m_isGBS
;
133 bool old_ins
= m_isInside
;
134 wxSizer
*old_par
= m_parentSizer
;
136 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
137 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
138 m_isInside
= old_ins
;
139 m_parentSizer
= old_par
;
142 // and figure out what type it is
143 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
144 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
147 sitem
->AssignSizer(sizer
);
149 sitem
->AssignWindow(wnd
);
151 wxLogError(wxT("Error in resource."));
153 // finally, set other wxSizerItem attributes
154 SetSizerItemAttributes(sitem
);
161 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
167 wxObject
* wxSizerXmlHandler::Handle_spacer()
169 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
171 wxSizerItem
* sitem
= MakeSizerItem();
172 SetSizerItemAttributes(sitem
);
173 sitem
->AssignSpacer(GetSize());
179 wxObject
* wxSizerXmlHandler::Handle_sizer()
181 wxSizer
*sizer
= NULL
;
183 wxXmlNode
*parentNode
= m_node
->GetParent();
185 wxCHECK_MSG(m_parentSizer
!= NULL
||
186 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
187 m_parentAsWindow
), NULL
,
188 wxT("Sizer must have a window parent node"));
190 if (m_class
== wxT("wxBoxSizer"))
191 sizer
= Handle_wxBoxSizer();
194 else if (m_class
== wxT("wxStaticBoxSizer"))
195 sizer
= Handle_wxStaticBoxSizer();
198 else if (m_class
== wxT("wxGridSizer"))
199 sizer
= Handle_wxGridSizer();
201 else if (m_class
== wxT("wxFlexGridSizer"))
202 sizer
= Handle_wxFlexGridSizer();
204 else if (m_class
== wxT("wxGridBagSizer"))
205 sizer
= Handle_wxGridBagSizer();
209 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
213 wxSize minsize
= GetSize(wxT("minsize"));
214 if (!(minsize
== wxDefaultSize
))
215 sizer
->SetMinSize(minsize
);
218 wxSizer
*old_par
= m_parentSizer
;
219 bool old_ins
= m_isInside
;
222 m_parentSizer
= sizer
;
224 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
226 CreateChildren(m_parent
, true/*only this handler*/);
229 m_isInside
= old_ins
;
230 m_parentSizer
= old_par
;
232 if (m_parentSizer
== NULL
) // setup window:
234 m_parentAsWindow
->SetSizer(sizer
);
236 wxXmlNode
*nd
= m_node
;
238 if (GetSize() == wxDefaultSize
)
239 sizer
->Fit(m_parentAsWindow
);
242 if (m_parentAsWindow
->IsTopLevel())
244 sizer
->SetSizeHints(m_parentAsWindow
);
252 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
254 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
258 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
260 return new wxStaticBoxSizer(
261 new wxStaticBox(m_parentAsWindow
,
263 GetText(wxT("label")),
264 wxDefaultPosition
, wxDefaultSize
,
267 GetStyle(wxT("orient"), wxHORIZONTAL
));
269 #endif // wxUSE_STATBOX
271 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
273 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
274 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
278 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
280 wxFlexGridSizer
*sizer
=
281 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
282 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
283 SetGrowables(sizer
, wxT("growablerows"), true);
284 SetGrowables(sizer
, wxT("growablecols"), false);
289 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
291 wxGridBagSizer
*sizer
=
292 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
293 SetGrowables(sizer
, wxT("growablerows"), true);
294 SetGrowables(sizer
, wxT("growablecols"), false);
301 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
305 wxStringTokenizer tkn
;
307 tkn
.SetString(GetParamValue(param
), wxT(","));
308 while (tkn
.HasMoreTokens())
310 if (!tkn
.GetNextToken().ToULong(&l
))
311 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
314 sizer
->AddGrowableRow(l
);
316 sizer
->AddGrowableCol(l
);
322 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
324 wxSize sz
= GetSize(param
);
325 if (sz
.x
< 0) sz
.x
= 0;
326 if (sz
.y
< 0) sz
.y
= 0;
327 return wxGBPosition(sz
.x
, sz
.y
);
330 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
332 wxSize sz
= GetSize(param
);
333 if (sz
.x
< 1) sz
.x
= 1;
334 if (sz
.y
< 1) sz
.y
= 1;
335 return wxGBSpan(sz
.x
, sz
.y
);
340 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
343 return new wxGBSizerItem();
345 return new wxSizerItem();
348 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
350 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
351 sitem
->SetFlag(GetStyle(wxT("flag")));
352 sitem
->SetBorder(GetDimension(wxT("border")));
353 wxSize sz
= GetSize(wxT("minsize"));
354 if (!(sz
== wxDefaultSize
))
355 sitem
->SetMinSize(sz
);
356 sz
= GetSize(wxT("ratio"));
357 if (!(sz
== wxDefaultSize
))
362 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
363 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
364 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
367 // record the id of the item, if any, for use by XRCSIZERITEM()
368 sitem
->SetId(GetID());
371 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
374 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
376 m_parentSizer
->Add(sitem
);
381 //-----------------------------------------------------------------------------
382 // wxStdDialogButtonSizerXmlHandler
383 //-----------------------------------------------------------------------------
386 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
388 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
389 : m_isInside(false), m_parentSizer(NULL
)
393 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
395 if (m_class
== wxT("wxStdDialogButtonSizer"))
397 wxASSERT( !m_parentSizer
);
399 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
402 CreateChildren(m_parent
, true/*only this handler*/);
404 m_parentSizer
->Realize();
407 m_parentSizer
= NULL
;
411 else // m_class == "button"
413 wxASSERT( m_parentSizer
);
415 // find the item to be managed by this sizeritem
416 wxXmlNode
*n
= GetParamNode(wxT("object"));
418 n
= GetParamNode(wxT("object_ref"));
423 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
424 wxButton
*button
= wxDynamicCast(item
, wxButton
);
427 m_parentSizer
->AddButton(button
);
429 wxLogError(wxT("Error in resource - expected button."));
435 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
441 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
443 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
444 (m_isInside
&& IsOfClass(node
, wxT("button")));
446 #endif // wxUSE_BUTTON