1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resource for wxBoxSizer
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "xh_sizer.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
24 #include "wx/xrc/xh_sizer.h"
26 #include "wx/gbsizer.h"
28 #include "wx/statbox.h"
29 #include "wx/notebook.h"
31 #include "wx/tokenzr.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
40 wxSizerXmlHandler::wxSizerXmlHandler()
41 : wxXmlResourceHandler(),
46 XRC_ADD_STYLE(wxHORIZONTAL
);
47 XRC_ADD_STYLE(wxVERTICAL
);
50 XRC_ADD_STYLE(wxLEFT
);
51 XRC_ADD_STYLE(wxRIGHT
);
53 XRC_ADD_STYLE(wxBOTTOM
);
54 XRC_ADD_STYLE(wxNORTH
);
55 XRC_ADD_STYLE(wxSOUTH
);
56 XRC_ADD_STYLE(wxEAST
);
57 XRC_ADD_STYLE(wxWEST
);
60 XRC_ADD_STYLE(wxGROW
);
61 XRC_ADD_STYLE(wxEXPAND
);
62 XRC_ADD_STYLE(wxSHAPED
);
63 XRC_ADD_STYLE(wxSTRETCH_NOT
);
65 XRC_ADD_STYLE(wxALIGN_CENTER
);
66 XRC_ADD_STYLE(wxALIGN_CENTRE
);
67 XRC_ADD_STYLE(wxALIGN_LEFT
);
68 XRC_ADD_STYLE(wxALIGN_TOP
);
69 XRC_ADD_STYLE(wxALIGN_RIGHT
);
70 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
71 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
72 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
73 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
74 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
76 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
77 XRC_ADD_STYLE(wxFIXED_MINSIZE
);
82 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
84 return ( (!m_isInside
&& IsSizerNode(node
)) ||
85 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
86 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
91 wxObject
* wxSizerXmlHandler::DoCreateResource()
93 if (m_class
== wxT("sizeritem"))
94 return Handle_sizeritem();
96 else if (m_class
== wxT("spacer"))
97 return Handle_spacer();
100 return Handle_sizer();
106 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
108 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
109 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
110 (IsOfClass(node
, wxT("wxGridSizer"))) ||
111 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
112 (IsOfClass(node
, wxT("wxGridBagSizer")));
116 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
118 // find the item to be managed by this sizeritem
119 wxXmlNode
*n
= GetParamNode(wxT("object"));
121 n
= GetParamNode(wxT("object_ref"));
126 // create a sizer item for it
127 wxSizerItem
* sitem
= MakeSizerItem();
129 // now fetch the item to be managed
130 bool old_gbs
= m_isGBS
;
131 bool old_ins
= m_isInside
;
132 wxSizer
*old_par
= m_parentSizer
;
134 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
135 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
136 m_isInside
= old_ins
;
137 m_parentSizer
= old_par
;
140 // and figure out what type it is
141 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
142 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
145 sitem
->SetSizer(sizer
);
147 sitem
->SetWindow(wnd
);
149 wxLogError(wxT("Error in resource."));
151 // finally, set other wxSizerItem attributes
152 SetSizerItemAttributes(sitem
);
159 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
165 wxObject
* wxSizerXmlHandler::Handle_spacer()
167 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
169 wxSizerItem
* sitem
= MakeSizerItem();
170 SetSizerItemAttributes(sitem
);
171 sitem
->SetSpacer(GetSize());
177 wxObject
* wxSizerXmlHandler::Handle_sizer()
179 wxSizer
*sizer
= NULL
;
181 wxXmlNode
*parentNode
= m_node
->GetParent();
183 wxCHECK_MSG(m_parentSizer
!= NULL
||
184 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
185 m_parentAsWindow
!= NULL
&&
186 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
187 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
188 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
190 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
192 if (m_class
== wxT("wxBoxSizer"))
193 sizer
= Handle_wxBoxSizer();
195 else if (m_class
== wxT("wxStaticBoxSizer"))
196 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();
208 wxSize minsize
= GetSize(wxT("minsize"));
209 if (!(minsize
== wxDefaultSize
))
210 sizer
->SetMinSize(minsize
);
213 wxSizer
*old_par
= m_parentSizer
;
214 bool old_ins
= m_isInside
;
217 m_parentSizer
= sizer
;
219 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
221 CreateChildren(m_parent
, true/*only this handler*/);
224 m_isInside
= old_ins
;
225 m_parentSizer
= old_par
;
227 if (m_parentSizer
== NULL
) // setup window:
229 m_parentAsWindow
->SetSizer(sizer
);
231 wxXmlNode
*nd
= m_node
;
233 if (GetSize() == wxDefaultSize
)
234 sizer
->Fit(m_parentAsWindow
);
237 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
238 sizer
->SetSizeHints(m_parentAsWindow
);
245 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
247 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
250 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
252 return new wxStaticBoxSizer(
253 new wxStaticBox(m_parentAsWindow
,
255 GetText(wxT("label")),
256 wxDefaultPosition
, wxDefaultSize
,
259 GetStyle(wxT("orient"), wxHORIZONTAL
));
262 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
264 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
265 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
269 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
271 wxFlexGridSizer
*sizer
=
272 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
273 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
274 SetGrowables(sizer
, wxT("growablerows"), true);
275 SetGrowables(sizer
, wxT("growablecols"), false);
280 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
282 wxGridBagSizer
*sizer
=
283 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
284 SetGrowables(sizer
, wxT("growablerows"), true);
285 SetGrowables(sizer
, wxT("growablecols"), false);
292 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
296 wxStringTokenizer tkn
;
298 tkn
.SetString(GetParamValue(param
), wxT(","));
299 while (tkn
.HasMoreTokens())
301 if (!tkn
.GetNextToken().ToULong(&l
))
302 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
305 sizer
->AddGrowableRow(l
);
307 sizer
->AddGrowableCol(l
);
313 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
315 wxSize sz
= GetSize(param
);
316 if (sz
.x
< 0) sz
.x
= 0;
317 if (sz
.y
< 0) sz
.y
= 0;
318 return wxGBPosition(sz
.x
, sz
.y
);
321 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
323 wxSize sz
= GetSize(param
);
324 if (sz
.x
< 1) sz
.x
= 1;
325 if (sz
.y
< 1) sz
.y
= 1;
326 return wxGBSpan(sz
.x
, sz
.y
);
331 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
334 return new wxGBSizerItem();
336 return new wxSizerItem();
339 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
341 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
342 sitem
->SetFlag(GetStyle(wxT("flag")));
343 sitem
->SetBorder(GetDimension(wxT("border")));
344 wxSize sz
= GetSize(wxT("minsize"));
345 if (!(sz
== wxDefaultSize
))
346 sitem
->SetMinSize(sz
);
347 sz
= GetSize(wxT("ratio"));
348 if (!(sz
== wxDefaultSize
))
353 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
354 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
355 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
359 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
362 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
364 m_parentSizer
->Add(sitem
);
369 //-----------------------------------------------------------------------------
370 // wxStdDialogButtonSizerXmlHandler
371 //-----------------------------------------------------------------------------
373 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
375 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
376 : m_isInside(false), m_parentSizer(NULL
)
380 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
382 if (m_class
== wxT("wxStdDialogButtonSizer"))
384 wxASSERT( !m_parentSizer
);
386 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
389 CreateChildren(m_parent
, true/*only this handler*/);
391 m_parentSizer
->Realize();
394 m_parentSizer
= NULL
;
398 else // m_class == "button"
400 wxASSERT( m_parentSizer
);
402 // find the item to be managed by this sizeritem
403 wxXmlNode
*n
= GetParamNode(wxT("object"));
405 n
= GetParamNode(wxT("object_ref"));
410 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
411 wxButton
*button
= wxDynamicCast(item
, wxButton
);
414 m_parentSizer
->AddButton(button
);
416 wxLogError(wxT("Error in resource - expected button."));
422 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
428 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
430 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
431 (m_isInside
&& IsOfClass(node
, wxT("button")));