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
);
84 // this flag doesn't do anything any more but we can just ignore its
85 // occurrences in the old resource files instead of raising a fuss because
87 AddStyle("wxADJUST_MINSIZE", 0);
89 // wxWrapSizer-specific flags
90 XRC_ADD_STYLE(wxEXTEND_LAST_ON_EACH_LINE
);
91 XRC_ADD_STYLE(wxREMOVE_LEADING_SPACES
);
96 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
98 return ( (!m_isInside
&& IsSizerNode(node
)) ||
99 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
100 (m_isInside
&& IsOfClass(node
, wxT("spacer")))
105 wxObject
* wxSizerXmlHandler::DoCreateResource()
107 if (m_class
== wxT("sizeritem"))
108 return Handle_sizeritem();
110 else if (m_class
== wxT("spacer"))
111 return Handle_spacer();
114 return Handle_sizer();
120 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
122 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
123 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
124 (IsOfClass(node
, wxT("wxGridSizer"))) ||
125 (IsOfClass(node
, wxT("wxFlexGridSizer"))) ||
126 (IsOfClass(node
, wxT("wxGridBagSizer"))) ||
127 (IsOfClass(node
, wxT("wxWrapSizer")));
131 wxObject
* wxSizerXmlHandler::Handle_sizeritem()
133 // find the item to be managed by this sizeritem
134 wxXmlNode
*n
= GetParamNode(wxT("object"));
136 n
= GetParamNode(wxT("object_ref"));
141 // create a sizer item for it
142 wxSizerItem
* sitem
= MakeSizerItem();
144 // now fetch the item to be managed
145 bool old_gbs
= m_isGBS
;
146 bool old_ins
= m_isInside
;
147 wxSizer
*old_par
= m_parentSizer
;
149 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
150 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
151 m_isInside
= old_ins
;
152 m_parentSizer
= old_par
;
155 // and figure out what type it is
156 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
157 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
160 sitem
->AssignSizer(sizer
);
162 sitem
->AssignWindow(wnd
);
164 wxLogError(wxT("Error in resource."));
166 // finally, set other wxSizerItem attributes
167 SetSizerItemAttributes(sitem
);
174 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
180 wxObject
* wxSizerXmlHandler::Handle_spacer()
182 if ( !m_parentSizer
)
184 wxLogError(_("XRC syntax error: \"spacer\" only allowed inside a "
189 wxSizerItem
* sitem
= MakeSizerItem();
190 SetSizerItemAttributes(sitem
);
191 sitem
->AssignSpacer(GetSize());
197 wxObject
* wxSizerXmlHandler::Handle_sizer()
199 wxSizer
*sizer
= NULL
;
201 wxXmlNode
*parentNode
= m_node
->GetParent();
203 if ( !m_parentSizer
&&
204 (!parentNode
|| parentNode
->GetType() != wxXML_ELEMENT_NODE
||
207 wxLogError(_("XRC syntax error: sizer must have a window parent."));
211 if (m_class
== wxT("wxBoxSizer"))
212 sizer
= Handle_wxBoxSizer();
215 else if (m_class
== wxT("wxStaticBoxSizer"))
216 sizer
= Handle_wxStaticBoxSizer();
219 else if (m_class
== wxT("wxGridSizer"))
220 sizer
= Handle_wxGridSizer();
222 else if (m_class
== wxT("wxFlexGridSizer"))
223 sizer
= Handle_wxFlexGridSizer();
225 else if (m_class
== wxT("wxGridBagSizer"))
226 sizer
= Handle_wxGridBagSizer();
228 else if (m_class
== wxT("wxWrapSizer"))
229 sizer
= Handle_wxWrapSizer();
233 wxLogError(_T("Failed to create size of class \"%s\""), m_class
.c_str());
237 wxSize minsize
= GetSize(wxT("minsize"));
238 if (!(minsize
== wxDefaultSize
))
239 sizer
->SetMinSize(minsize
);
242 wxSizer
*old_par
= m_parentSizer
;
243 bool old_ins
= m_isInside
;
246 m_parentSizer
= sizer
;
248 m_isGBS
= (m_class
== wxT("wxGridBagSizer"));
250 CreateChildren(m_parent
, true/*only this handler*/);
253 m_isInside
= old_ins
;
254 m_parentSizer
= old_par
;
256 if (m_parentSizer
== NULL
) // setup window:
258 m_parentAsWindow
->SetSizer(sizer
);
260 wxXmlNode
*nd
= m_node
;
262 if (GetSize() == wxDefaultSize
)
264 if ( wxDynamicCast(m_parentAsWindow
, wxScrolledWindow
) != NULL
)
266 sizer
->FitInside(m_parentAsWindow
);
270 sizer
->Fit(m_parentAsWindow
);
275 if (m_parentAsWindow
->IsTopLevel())
277 sizer
->SetSizeHints(m_parentAsWindow
);
285 wxSizer
* wxSizerXmlHandler::Handle_wxBoxSizer()
287 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
291 wxSizer
* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
293 return new wxStaticBoxSizer(
294 new wxStaticBox(m_parentAsWindow
,
296 GetText(wxT("label")),
297 wxDefaultPosition
, wxDefaultSize
,
300 GetStyle(wxT("orient"), wxHORIZONTAL
));
302 #endif // wxUSE_STATBOX
304 wxSizer
* wxSizerXmlHandler::Handle_wxGridSizer()
306 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
307 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
311 wxSizer
* wxSizerXmlHandler::Handle_wxFlexGridSizer()
313 wxFlexGridSizer
*sizer
=
314 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
315 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
316 SetGrowables(sizer
, wxT("growablerows"), true);
317 SetGrowables(sizer
, wxT("growablecols"), false);
322 wxSizer
* wxSizerXmlHandler::Handle_wxGridBagSizer()
324 wxGridBagSizer
*sizer
=
325 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
326 SetGrowables(sizer
, wxT("growablerows"), true);
327 SetGrowables(sizer
, wxT("growablecols"), false);
331 wxSizer
* wxSizerXmlHandler::Handle_wxWrapSizer()
333 wxWrapSizer
*sizer
= new wxWrapSizer(GetStyle("orient"), GetStyle("flag"));
339 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer
* sizer
,
343 wxStringTokenizer tkn
;
345 tkn
.SetString(GetParamValue(param
), wxT(","));
346 while (tkn
.HasMoreTokens())
348 if (!tkn
.GetNextToken().ToULong(&l
))
349 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
352 sizer
->AddGrowableRow(l
);
354 sizer
->AddGrowableCol(l
);
360 wxGBPosition
wxSizerXmlHandler::GetGBPos(const wxString
& param
)
362 wxSize sz
= GetSize(param
);
363 if (sz
.x
< 0) sz
.x
= 0;
364 if (sz
.y
< 0) sz
.y
= 0;
365 return wxGBPosition(sz
.x
, sz
.y
);
368 wxGBSpan
wxSizerXmlHandler::GetGBSpan(const wxString
& param
)
370 wxSize sz
= GetSize(param
);
371 if (sz
.x
< 1) sz
.x
= 1;
372 if (sz
.y
< 1) sz
.y
= 1;
373 return wxGBSpan(sz
.x
, sz
.y
);
378 wxSizerItem
* wxSizerXmlHandler::MakeSizerItem()
381 return new wxGBSizerItem();
383 return new wxSizerItem();
386 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem
* sitem
)
388 sitem
->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
389 sitem
->SetFlag(GetStyle(wxT("flag")));
390 sitem
->SetBorder(GetDimension(wxT("border")));
391 wxSize sz
= GetSize(wxT("minsize"));
392 if (!(sz
== wxDefaultSize
))
393 sitem
->SetMinSize(sz
);
394 sz
= GetSize(wxT("ratio"));
395 if (!(sz
== wxDefaultSize
))
400 wxGBSizerItem
* gbsitem
= (wxGBSizerItem
*)sitem
;
401 gbsitem
->SetPos(GetGBPos(wxT("cellpos")));
402 gbsitem
->SetSpan(GetGBSpan(wxT("cellspan")));
405 // record the id of the item, if any, for use by XRCSIZERITEM()
406 sitem
->SetId(GetID());
409 void wxSizerXmlHandler::AddSizerItem(wxSizerItem
* sitem
)
412 ((wxGridBagSizer
*)m_parentSizer
)->Add((wxGBSizerItem
*)sitem
);
414 m_parentSizer
->Add(sitem
);
419 //-----------------------------------------------------------------------------
420 // wxStdDialogButtonSizerXmlHandler
421 //-----------------------------------------------------------------------------
424 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler
, wxXmlResourceHandler
)
426 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
427 : m_isInside(false), m_parentSizer(NULL
)
431 wxObject
*wxStdDialogButtonSizerXmlHandler::DoCreateResource()
433 if (m_class
== wxT("wxStdDialogButtonSizer"))
435 wxASSERT( !m_parentSizer
);
437 wxSizer
*s
= m_parentSizer
= new wxStdDialogButtonSizer
;
440 CreateChildren(m_parent
, true/*only this handler*/);
442 m_parentSizer
->Realize();
445 m_parentSizer
= NULL
;
449 else // m_class == "button"
451 wxASSERT( m_parentSizer
);
453 // find the item to be managed by this sizeritem
454 wxXmlNode
*n
= GetParamNode(wxT("object"));
456 n
= GetParamNode(wxT("object_ref"));
461 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
462 wxButton
*button
= wxDynamicCast(item
, wxButton
);
465 m_parentSizer
->AddButton(button
);
467 wxLogError(wxT("Error in resource - expected button."));
473 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
479 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode
*node
)
481 return (!m_isInside
&& IsOfClass(node
, wxT("wxStdDialogButtonSizer"))) ||
482 (m_isInside
&& IsOfClass(node
, wxT("button")));
484 #endif // wxUSE_BUTTON