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/wrapsizer.h"
35 #include "wx/notebook.h"
36 #include "wx/tokenzr.h"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
45 wxSizerXmlHandler::wxSizerXmlHandler()
46 :wxXmlResourceHandler(),
51 XRC_ADD_STYLE(wxHORIZONTAL
);
52 XRC_ADD_STYLE(wxVERTICAL
);
55 XRC_ADD_STYLE(wxLEFT
);
56 XRC_ADD_STYLE(wxRIGHT
);
58 XRC_ADD_STYLE(wxBOTTOM
);
59 XRC_ADD_STYLE(wxNORTH
);
60 XRC_ADD_STYLE(wxSOUTH
);
61 XRC_ADD_STYLE(wxEAST
);
62 XRC_ADD_STYLE(wxWEST
);
65 XRC_ADD_STYLE(wxGROW
);
66 XRC_ADD_STYLE(wxEXPAND
);
67 XRC_ADD_STYLE(wxSHAPED
);
68 XRC_ADD_STYLE(wxSTRETCH_NOT
);
70 XRC_ADD_STYLE(wxALIGN_CENTER
);
71 XRC_ADD_STYLE(wxALIGN_CENTRE
);
72 XRC_ADD_STYLE(wxALIGN_LEFT
);
73 XRC_ADD_STYLE(wxALIGN_TOP
);
74 XRC_ADD_STYLE(wxALIGN_RIGHT
);
75 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
76 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
77 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
78 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
79 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
81 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
82 XRC_ADD_STYLE(wxRESERVE_SPACE_EVEN_IF_HIDDEN
);
87 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
89 return ( (!m_isInside
&& IsSizerNode(node
)) ||
90 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
91 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
96 wxObject
* wxSizerXmlHandler::DoCreateResource()
98 if (m_class
== wxT("sizeritem"))
99 return Handle_sizeritem();
101 else if (m_class
== wxT("spacer"))
102 return Handle_spacer();
105 return Handle_sizer();
111 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
113 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
114 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
115 (IsOfClass(node
, wxT("wxGridSizer"))) ||
116 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
117 (IsOfClass(node
, wxT("wxGridBagSizer"))) ||
118 (IsOfClass(node
, wxT("wxWrapSizer")));
122 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
124 // find the item to be managed by this sizeritem
125 wxXmlNode
*n
= GetParamNode(wxT("object"));
127 n
= GetParamNode(wxT("object_ref"));
132 // create a sizer item for it
133 wxSizerItem
* sitem
= MakeSizerItem();
135 // now fetch the item to be managed
136 bool old_gbs
= m_isGBS
;
137 bool old_ins
= m_isInside
;
138 wxSizer
*old_par
= m_parentSizer
;
140 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
141 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
142 m_isInside
= old_ins
;
143 m_parentSizer
= old_par
;
146 // and figure out what type it is
147 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
148 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
151 sitem
->AssignSizer(sizer
);
153 sitem
->AssignWindow(wnd
);
155 wxLogError(wxT("Error in resource."));
157 // finally, set other wxSizerItem attributes
158 SetSizerItemAttributes(sitem
);
165 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
171 wxObject
* wxSizerXmlHandler::Handle_spacer()
173 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
175 wxSizerItem
* sitem
= MakeSizerItem();
176 SetSizerItemAttributes(sitem
);
177 sitem
->AssignSpacer(GetSize());
183 wxObject
* wxSizerXmlHandler::Handle_sizer()
185 wxSizer
*sizer
= NULL
;
187 wxXmlNode
*parentNode
= m_node
->GetParent();
189 wxCHECK_MSG(m_parentSizer
!= NULL
||
190 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
191 m_parentAsWindow
), NULL
,
192 wxT("Sizer must have a window parent node"));
194 if (m_class
== wxT("wxBoxSizer"))
195 sizer
= Handle_wxBoxSizer();
198 else if (m_class
== wxT("wxStaticBoxSizer"))
199 sizer
= Handle_wxStaticBoxSizer();
202 else if (m_class
== wxT("wxGridSizer"))
203 sizer
= Handle_wxGridSizer();
205 else if (m_class
== wxT("wxFlexGridSizer"))
206 sizer
= Handle_wxFlexGridSizer();
208 else if (m_class
== wxT("wxGridBagSizer"))
209 sizer
= Handle_wxGridBagSizer();
211 else if (m_class
== wxT("wxWrapSizer"))
212 sizer
= Handle_wxGridBagSizer();
216 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
220 wxSize minsize
= GetSize(wxT("minsize"));
221 if (!(minsize
== wxDefaultSize
))
222 sizer
->SetMinSize(minsize
);
225 wxSizer
*old_par
= m_parentSizer
;
226 bool old_ins
= m_isInside
;
229 m_parentSizer
= sizer
;
231 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
233 CreateChildren(m_parent
, true/*only this handler*/);
236 m_isInside
= old_ins
;
237 m_parentSizer
= old_par
;
239 if (m_parentSizer
== NULL
) // setup window:
241 m_parentAsWindow
->SetSizer(sizer
);
243 wxXmlNode
*nd
= m_node
;
245 if (GetSize() == wxDefaultSize
)
247 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
249 sizer
->FitInside(m_parentAsWindow
);
253 sizer
->Fit(m_parentAsWindow
);
258 if (m_parentAsWindow
->IsTopLevel())
260 sizer
->SetSizeHints(m_parentAsWindow
);
268 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
270 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
274 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
276 return new wxStaticBoxSizer(
277 new wxStaticBox(m_parentAsWindow
,
279 GetText(wxT("label")),
280 wxDefaultPosition
, wxDefaultSize
,
283 GetStyle(wxT("orient"), wxHORIZONTAL
));
285 #endif // wxUSE_STATBOX
287 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
289 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
290 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
294 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
296 wxFlexGridSizer
*sizer
=
297 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
298 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
299 SetGrowables(sizer
, wxT("growablerows"), true);
300 SetGrowables(sizer
, wxT("growablecols"), false);
305 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
307 wxGridBagSizer
*sizer
=
308 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
309 SetGrowables(sizer
, wxT("growablerows"), true);
310 SetGrowables(sizer
, wxT("growablecols"), false);
314 wxSizer
* wxSizerXmlHandler::Handle_wxWrapSizer()
316 wxWrapSizer
*sizer
= new wxWrapSizer(GetStyle("orient"), wxHORIZONTAL
);
322 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
326 wxStringTokenizer tkn
;
328 tkn
.SetString(GetParamValue(param
), wxT(","));
329 while (tkn
.HasMoreTokens())
331 if (!tkn
.GetNextToken().ToULong(&l
))
332 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
335 sizer
->AddGrowableRow(l
);
337 sizer
->AddGrowableCol(l
);
343 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
345 wxSize sz
= GetSize(param
);
346 if (sz
.x
< 0) sz
.x
= 0;
347 if (sz
.y
< 0) sz
.y
= 0;
348 return wxGBPosition(sz
.x
, sz
.y
);
351 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
353 wxSize sz
= GetSize(param
);
354 if (sz
.x
< 1) sz
.x
= 1;
355 if (sz
.y
< 1) sz
.y
= 1;
356 return wxGBSpan(sz
.x
, sz
.y
);
361 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
364 return new wxGBSizerItem();
366 return new wxSizerItem();
369 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
371 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
372 sitem
->SetFlag(GetStyle(wxT("flag")));
373 sitem
->SetBorder(GetDimension(wxT("border")));
374 wxSize sz
= GetSize(wxT("minsize"));
375 if (!(sz
== wxDefaultSize
))
376 sitem
->SetMinSize(sz
);
377 sz
= GetSize(wxT("ratio"));
378 if (!(sz
== wxDefaultSize
))
383 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
384 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
385 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
388 // record the id of the item, if any, for use by XRCSIZERITEM()
389 sitem
->SetId(GetID());
392 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
395 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
397 m_parentSizer
->Add(sitem
);
402 //-----------------------------------------------------------------------------
403 // wxStdDialogButtonSizerXmlHandler
404 //-----------------------------------------------------------------------------
407 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
409 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
410 : m_isInside(false), m_parentSizer(NULL
)
414 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
416 if (m_class
== wxT("wxStdDialogButtonSizer"))
418 wxASSERT( !m_parentSizer
);
420 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
423 CreateChildren(m_parent
, true/*only this handler*/);
425 m_parentSizer
->Realize();
428 m_parentSizer
= NULL
;
432 else // m_class == "button"
434 wxASSERT( m_parentSizer
);
436 // find the item to be managed by this sizeritem
437 wxXmlNode
*n
= GetParamNode(wxT("object"));
439 n
= GetParamNode(wxT("object_ref"));
444 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
445 wxButton
*button
= wxDynamicCast(item
, wxButton
);
448 m_parentSizer
->AddButton(button
);
450 wxLogError(wxT("Error in resource - expected button."));
456 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
462 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
464 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
465 (m_isInside
&& IsOfClass(node
, wxT("button")));
467 #endif // wxUSE_BUTTON