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
->SetSizer(sizer
);
149 sitem
->SetWindow(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
->SetSpacer(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
->GetWindowStyle() & (wxMAXIMIZE_BOX
| wxRESIZE_BORDER
))
243 sizer
->SetSizeHints(m_parentAsWindow
);
250 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
252 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
256 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
258 return new wxStaticBoxSizer(
259 new wxStaticBox(m_parentAsWindow
,
261 GetText(wxT("label")),
262 wxDefaultPosition
, wxDefaultSize
,
265 GetStyle(wxT("orient"), wxHORIZONTAL
));
267 #endif // wxUSE_STATBOX
269 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
271 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
272 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
276 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
278 wxFlexGridSizer
*sizer
=
279 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
280 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
281 SetGrowables(sizer
, wxT("growablerows"), true);
282 SetGrowables(sizer
, wxT("growablecols"), false);
287 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
289 wxGridBagSizer
*sizer
=
290 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
291 SetGrowables(sizer
, wxT("growablerows"), true);
292 SetGrowables(sizer
, wxT("growablecols"), false);
299 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
303 wxStringTokenizer tkn
;
305 tkn
.SetString(GetParamValue(param
), wxT(","));
306 while (tkn
.HasMoreTokens())
308 if (!tkn
.GetNextToken().ToULong(&l
))
309 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
312 sizer
->AddGrowableRow(l
);
314 sizer
->AddGrowableCol(l
);
320 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
322 wxSize sz
= GetSize(param
);
323 if (sz
.x
< 0) sz
.x
= 0;
324 if (sz
.y
< 0) sz
.y
= 0;
325 return wxGBPosition(sz
.x
, sz
.y
);
328 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
330 wxSize sz
= GetSize(param
);
331 if (sz
.x
< 1) sz
.x
= 1;
332 if (sz
.y
< 1) sz
.y
= 1;
333 return wxGBSpan(sz
.x
, sz
.y
);
338 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
341 return new wxGBSizerItem();
343 return new wxSizerItem();
346 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
348 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
349 sitem
->SetFlag(GetStyle(wxT("flag")));
350 sitem
->SetBorder(GetDimension(wxT("border")));
351 wxSize sz
= GetSize(wxT("minsize"));
352 if (!(sz
== wxDefaultSize
))
353 sitem
->SetMinSize(sz
);
354 sz
= GetSize(wxT("ratio"));
355 if (!(sz
== wxDefaultSize
))
360 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
361 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
362 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
366 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
369 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
371 m_parentSizer
->Add(sitem
);
376 //-----------------------------------------------------------------------------
377 // wxStdDialogButtonSizerXmlHandler
378 //-----------------------------------------------------------------------------
381 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
383 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
384 : m_isInside(false), m_parentSizer(NULL
)
388 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
390 if (m_class
== wxT("wxStdDialogButtonSizer"))
392 wxASSERT( !m_parentSizer
);
394 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
397 CreateChildren(m_parent
, true/*only this handler*/);
399 m_parentSizer
->Realize();
402 m_parentSizer
= NULL
;
406 else // m_class == "button"
408 wxASSERT( m_parentSizer
);
410 // find the item to be managed by this sizeritem
411 wxXmlNode
*n
= GetParamNode(wxT("object"));
413 n
= GetParamNode(wxT("object_ref"));
418 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
419 wxButton
*button
= wxDynamicCast(item
, wxButton
);
422 m_parentSizer
->AddButton(button
);
424 wxLogError(wxT("Error in resource - expected button."));
430 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
436 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
438 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
439 (m_isInside
&& IsOfClass(node
, wxT("button")));
441 #endif // wxUSE_BUTTON