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"
30 #include "wx/scrolwin.h"
33 #include "wx/gbsizer.h"
34 #include "wx/notebook.h"
35 #include "wx/tokenzr.h"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
44 wxSizerXmlHandler::wxSizerXmlHandler()
45 :wxXmlResourceHandler(),
50 XRC_ADD_STYLE(wxHORIZONTAL
);
51 XRC_ADD_STYLE(wxVERTICAL
);
54 XRC_ADD_STYLE(wxLEFT
);
55 XRC_ADD_STYLE(wxRIGHT
);
57 XRC_ADD_STYLE(wxBOTTOM
);
58 XRC_ADD_STYLE(wxNORTH
);
59 XRC_ADD_STYLE(wxSOUTH
);
60 XRC_ADD_STYLE(wxEAST
);
61 XRC_ADD_STYLE(wxWEST
);
64 XRC_ADD_STYLE(wxGROW
);
65 XRC_ADD_STYLE(wxEXPAND
);
66 XRC_ADD_STYLE(wxSHAPED
);
67 XRC_ADD_STYLE(wxSTRETCH_NOT
);
69 XRC_ADD_STYLE(wxALIGN_CENTER
);
70 XRC_ADD_STYLE(wxALIGN_CENTRE
);
71 XRC_ADD_STYLE(wxALIGN_LEFT
);
72 XRC_ADD_STYLE(wxALIGN_TOP
);
73 XRC_ADD_STYLE(wxALIGN_RIGHT
);
74 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
75 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
76 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
77 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
78 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
80 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
85 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
87 return ( (!m_isInside
&& IsSizerNode(node
)) ||
88 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
89 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
94 wxObject
* wxSizerXmlHandler::DoCreateResource()
96 if (m_class
== wxT("sizeritem"))
97 return Handle_sizeritem();
99 else if (m_class
== wxT("spacer"))
100 return Handle_spacer();
103 return Handle_sizer();
109 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
111 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
112 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
113 (IsOfClass(node
, wxT("wxGridSizer"))) ||
114 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
115 (IsOfClass(node
, wxT("wxGridBagSizer")));
119 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
121 // find the item to be managed by this sizeritem
122 wxXmlNode
*n
= GetParamNode(wxT("object"));
124 n
= GetParamNode(wxT("object_ref"));
129 // create a sizer item for it
130 wxSizerItem
* sitem
= MakeSizerItem();
132 // now fetch the item to be managed
133 bool old_gbs
= m_isGBS
;
134 bool old_ins
= m_isInside
;
135 wxSizer
*old_par
= m_parentSizer
;
137 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
138 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
139 m_isInside
= old_ins
;
140 m_parentSizer
= old_par
;
143 // and figure out what type it is
144 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
145 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
148 sitem
->AssignSizer(sizer
);
150 sitem
->AssignWindow(wnd
);
152 wxLogError(wxT("Error in resource."));
154 // finally, set other wxSizerItem attributes
155 SetSizerItemAttributes(sitem
);
162 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
168 wxObject
* wxSizerXmlHandler::Handle_spacer()
170 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
172 wxSizerItem
* sitem
= MakeSizerItem();
173 SetSizerItemAttributes(sitem
);
174 sitem
->AssignSpacer(GetSize());
180 wxObject
* wxSizerXmlHandler::Handle_sizer()
182 wxSizer
*sizer
= NULL
;
184 wxXmlNode
*parentNode
= m_node
->GetParent();
186 wxCHECK_MSG(m_parentSizer
!= NULL
||
187 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
188 m_parentAsWindow
), NULL
,
189 wxT("Sizer must have a window parent node"));
191 if (m_class
== wxT("wxBoxSizer"))
192 sizer
= Handle_wxBoxSizer();
195 else if (m_class
== wxT("wxStaticBoxSizer"))
196 sizer
= Handle_wxStaticBoxSizer();
199 else if (m_class
== wxT("wxGridSizer"))
200 sizer
= Handle_wxGridSizer();
202 else if (m_class
== wxT("wxFlexGridSizer"))
203 sizer
= Handle_wxFlexGridSizer();
205 else if (m_class
== wxT("wxGridBagSizer"))
206 sizer
= Handle_wxGridBagSizer();
210 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
214 wxSize minsize
= GetSize(wxT("minsize"));
215 if (!(minsize
== wxDefaultSize
))
216 sizer
->SetMinSize(minsize
);
219 wxSizer
*old_par
= m_parentSizer
;
220 bool old_ins
= m_isInside
;
223 m_parentSizer
= sizer
;
225 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
227 CreateChildren(m_parent
, true/*only this handler*/);
230 m_isInside
= old_ins
;
231 m_parentSizer
= old_par
;
233 if (m_parentSizer
== NULL
) // setup window:
235 m_parentAsWindow
->SetSizer(sizer
);
237 wxXmlNode
*nd
= m_node
;
239 if (GetSize() == wxDefaultSize
)
241 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
243 sizer
->FitInside(m_parentAsWindow
);
247 sizer
->Fit(m_parentAsWindow
);
252 if (m_parentAsWindow
->IsTopLevel())
254 sizer
->SetSizeHints(m_parentAsWindow
);
262 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
264 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
268 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
270 return new wxStaticBoxSizer(
271 new wxStaticBox(m_parentAsWindow
,
273 GetText(wxT("label")),
274 wxDefaultPosition
, wxDefaultSize
,
277 GetStyle(wxT("orient"), wxHORIZONTAL
));
279 #endif // wxUSE_STATBOX
281 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
283 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
284 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
288 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
290 wxFlexGridSizer
*sizer
=
291 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
292 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
293 SetGrowables(sizer
, wxT("growablerows"), true);
294 SetGrowables(sizer
, wxT("growablecols"), false);
299 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
301 wxGridBagSizer
*sizer
=
302 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
303 SetGrowables(sizer
, wxT("growablerows"), true);
304 SetGrowables(sizer
, wxT("growablecols"), false);
311 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
315 wxStringTokenizer tkn
;
317 tkn
.SetString(GetParamValue(param
), wxT(","));
318 while (tkn
.HasMoreTokens())
320 if (!tkn
.GetNextToken().ToULong(&l
))
321 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
324 sizer
->AddGrowableRow(l
);
326 sizer
->AddGrowableCol(l
);
332 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
334 wxSize sz
= GetSize(param
);
335 if (sz
.x
< 0) sz
.x
= 0;
336 if (sz
.y
< 0) sz
.y
= 0;
337 return wxGBPosition(sz
.x
, sz
.y
);
340 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
342 wxSize sz
= GetSize(param
);
343 if (sz
.x
< 1) sz
.x
= 1;
344 if (sz
.y
< 1) sz
.y
= 1;
345 return wxGBSpan(sz
.x
, sz
.y
);
350 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
353 return new wxGBSizerItem();
355 return new wxSizerItem();
358 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
360 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
361 sitem
->SetFlag(GetStyle(wxT("flag")));
362 sitem
->SetBorder(GetDimension(wxT("border")));
363 wxSize sz
= GetSize(wxT("minsize"));
364 if (!(sz
== wxDefaultSize
))
365 sitem
->SetMinSize(sz
);
366 sz
= GetSize(wxT("ratio"));
367 if (!(sz
== wxDefaultSize
))
372 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
373 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
374 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
377 // record the id of the item, if any, for use by XRCSIZERITEM()
378 sitem
->SetId(GetID());
381 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
384 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
386 m_parentSizer
->Add(sitem
);
391 //-----------------------------------------------------------------------------
392 // wxStdDialogButtonSizerXmlHandler
393 //-----------------------------------------------------------------------------
396 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
398 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
399 : m_isInside(false), m_parentSizer(NULL
)
403 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
405 if (m_class
== wxT("wxStdDialogButtonSizer"))
407 wxASSERT( !m_parentSizer
);
409 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
412 CreateChildren(m_parent
, true/*only this handler*/);
414 m_parentSizer
->Realize();
417 m_parentSizer
= NULL
;
421 else // m_class == "button"
423 wxASSERT( m_parentSizer
);
425 // find the item to be managed by this sizeritem
426 wxXmlNode
*n
= GetParamNode(wxT("object"));
428 n
= GetParamNode(wxT("object_ref"));
433 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
434 wxButton
*button
= wxDynamicCast(item
, wxButton
);
437 m_parentSizer
->AddButton(button
);
439 wxLogError(wxT("Error in resource - expected button."));
445 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
451 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
453 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
454 (m_isInside
&& IsOfClass(node
, wxT("button")));
456 #endif // wxUSE_BUTTON