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
);
81 XRC_ADD_STYLE(wxRESERVE_SPACE_EVEN_IF_HIDDEN
);
86 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
88 return ( (!m_isInside
&& IsSizerNode(node
)) ||
89 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
90 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
95 wxObject
* wxSizerXmlHandler::DoCreateResource()
97 if (m_class
== wxT("sizeritem"))
98 return Handle_sizeritem();
100 else if (m_class
== wxT("spacer"))
101 return Handle_spacer();
104 return Handle_sizer();
110 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
112 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
113 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
114 (IsOfClass(node
, wxT("wxGridSizer"))) ||
115 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
116 (IsOfClass(node
, wxT("wxGridBagSizer")));
120 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
122 // find the item to be managed by this sizeritem
123 wxXmlNode
*n
= GetParamNode(wxT("object"));
125 n
= GetParamNode(wxT("object_ref"));
130 // create a sizer item for it
131 wxSizerItem
* sitem
= MakeSizerItem();
133 // now fetch the item to be managed
134 bool old_gbs
= m_isGBS
;
135 bool old_ins
= m_isInside
;
136 wxSizer
*old_par
= m_parentSizer
;
138 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
139 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
140 m_isInside
= old_ins
;
141 m_parentSizer
= old_par
;
144 // and figure out what type it is
145 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
146 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
149 sitem
->AssignSizer(sizer
);
151 sitem
->AssignWindow(wnd
);
153 wxLogError(wxT("Error in resource."));
155 // finally, set other wxSizerItem attributes
156 SetSizerItemAttributes(sitem
);
163 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
169 wxObject
* wxSizerXmlHandler::Handle_spacer()
171 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
173 wxSizerItem
* sitem
= MakeSizerItem();
174 SetSizerItemAttributes(sitem
);
175 sitem
->AssignSpacer(GetSize());
181 wxObject
* wxSizerXmlHandler::Handle_sizer()
183 wxSizer
*sizer
= NULL
;
185 wxXmlNode
*parentNode
= m_node
->GetParent();
187 wxCHECK_MSG(m_parentSizer
!= NULL
||
188 (parentNode
&& parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
189 m_parentAsWindow
), NULL
,
190 wxT("Sizer must have a window parent node"));
192 if (m_class
== wxT("wxBoxSizer"))
193 sizer
= Handle_wxBoxSizer();
196 else if (m_class
== wxT("wxStaticBoxSizer"))
197 sizer
= Handle_wxStaticBoxSizer();
200 else if (m_class
== wxT("wxGridSizer"))
201 sizer
= Handle_wxGridSizer();
203 else if (m_class
== wxT("wxFlexGridSizer"))
204 sizer
= Handle_wxFlexGridSizer();
206 else if (m_class
== wxT("wxGridBagSizer"))
207 sizer
= Handle_wxGridBagSizer();
211 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
215 wxSize minsize
= GetSize(wxT("minsize"));
216 if (!(minsize
== wxDefaultSize
))
217 sizer
->SetMinSize(minsize
);
220 wxSizer
*old_par
= m_parentSizer
;
221 bool old_ins
= m_isInside
;
224 m_parentSizer
= sizer
;
226 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
228 CreateChildren(m_parent
, true/*only this handler*/);
231 m_isInside
= old_ins
;
232 m_parentSizer
= old_par
;
234 if (m_parentSizer
== NULL
) // setup window:
236 m_parentAsWindow
->SetSizer(sizer
);
238 wxXmlNode
*nd
= m_node
;
240 if (GetSize() == wxDefaultSize
)
242 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
244 sizer
->FitInside(m_parentAsWindow
);
248 sizer
->Fit(m_parentAsWindow
);
253 if (m_parentAsWindow
->IsTopLevel())
255 sizer
->SetSizeHints(m_parentAsWindow
);
263 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
265 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
269 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
271 return new wxStaticBoxSizer(
272 new wxStaticBox(m_parentAsWindow
,
274 GetText(wxT("label")),
275 wxDefaultPosition
, wxDefaultSize
,
278 GetStyle(wxT("orient"), wxHORIZONTAL
));
280 #endif // wxUSE_STATBOX
282 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
284 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
285 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
289 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
291 wxFlexGridSizer
*sizer
=
292 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
293 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
294 SetGrowables(sizer
, wxT("growablerows"), true);
295 SetGrowables(sizer
, wxT("growablecols"), false);
300 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
302 wxGridBagSizer
*sizer
=
303 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
304 SetGrowables(sizer
, wxT("growablerows"), true);
305 SetGrowables(sizer
, wxT("growablecols"), false);
312 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
316 wxStringTokenizer tkn
;
318 tkn
.SetString(GetParamValue(param
), wxT(","));
319 while (tkn
.HasMoreTokens())
321 if (!tkn
.GetNextToken().ToULong(&l
))
322 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
325 sizer
->AddGrowableRow(l
);
327 sizer
->AddGrowableCol(l
);
333 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
335 wxSize sz
= GetSize(param
);
336 if (sz
.x
< 0) sz
.x
= 0;
337 if (sz
.y
< 0) sz
.y
= 0;
338 return wxGBPosition(sz
.x
, sz
.y
);
341 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
343 wxSize sz
= GetSize(param
);
344 if (sz
.x
< 1) sz
.x
= 1;
345 if (sz
.y
< 1) sz
.y
= 1;
346 return wxGBSpan(sz
.x
, sz
.y
);
351 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
354 return new wxGBSizerItem();
356 return new wxSizerItem();
359 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
361 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
362 sitem
->SetFlag(GetStyle(wxT("flag")));
363 sitem
->SetBorder(GetDimension(wxT("border")));
364 wxSize sz
= GetSize(wxT("minsize"));
365 if (!(sz
== wxDefaultSize
))
366 sitem
->SetMinSize(sz
);
367 sz
= GetSize(wxT("ratio"));
368 if (!(sz
== wxDefaultSize
))
373 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
374 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
375 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
378 // record the id of the item, if any, for use by XRCSIZERITEM()
379 sitem
->SetId(GetID());
382 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
385 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
387 m_parentSizer
->Add(sitem
);
392 //-----------------------------------------------------------------------------
393 // wxStdDialogButtonSizerXmlHandler
394 //-----------------------------------------------------------------------------
397 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
399 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
400 : m_isInside(false), m_parentSizer(NULL
)
404 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
406 if (m_class
== wxT("wxStdDialogButtonSizer"))
408 wxASSERT( !m_parentSizer
);
410 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
413 CreateChildren(m_parent
, true/*only this handler*/);
415 m_parentSizer
->Realize();
418 m_parentSizer
= NULL
;
422 else // m_class == "button"
424 wxASSERT( m_parentSizer
);
426 // find the item to be managed by this sizeritem
427 wxXmlNode
*n
= GetParamNode(wxT("object"));
429 n
= GetParamNode(wxT("object_ref"));
434 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
435 wxButton
*button
= wxDynamicCast(item
, wxButton
);
438 m_parentSizer
->AddButton(button
);
440 wxLogError(wxT("Error in resource - expected button."));
446 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
452 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
454 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
455 (m_isInside
&& IsOfClass(node
, wxT("button")));
457 #endif // wxUSE_BUTTON