1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resource for wxBoxSizer
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xh_sizer.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/xrc/xh_sizer.h"
25 #include "wx/statbox.h"
26 #include "wx/notebook.h"
27 #include "wx/tokenzr.h"
29 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
31 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
32 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
33 (IsOfClass(node
, wxT("wxGridSizer"))) ||
34 (IsOfClass(node
, wxT("wxFlexGridSizer")));
39 wxSizerXmlHandler::wxSizerXmlHandler()
40 : wxXmlResourceHandler(), m_isInside(FALSE
), m_parentSizer(NULL
)
42 ADD_STYLE(wxHORIZONTAL
);
43 ADD_STYLE(wxVERTICAL
);
59 ADD_STYLE(wxSTRETCH_NOT
);
61 ADD_STYLE(wxALIGN_CENTER
);
62 ADD_STYLE(wxALIGN_CENTRE
);
63 ADD_STYLE(wxALIGN_LEFT
);
64 ADD_STYLE(wxALIGN_TOP
);
65 ADD_STYLE(wxALIGN_RIGHT
);
66 ADD_STYLE(wxALIGN_BOTTOM
);
67 ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
68 ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
69 ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
70 ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
75 wxObject
*wxSizerXmlHandler::DoCreateResource()
77 if (m_class
== wxT("sizeritem"))
79 wxXmlNode
*n
= GetParamNode(wxT("object"));
82 n
= GetParamNode(wxT("object_ref"));
86 bool old_ins
= m_isInside
;
87 wxSizer
*old_par
= m_parentSizer
;
89 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
90 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
92 m_parentSizer
= old_par
;
93 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
94 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
95 wxSize minsize
= GetSize(wxT("minsize"));
99 m_parentSizer
->Add(sizer
, GetLong(wxT("option")),
100 GetStyle(wxT("flag")), GetDimension(wxT("border")));
101 if (!(minsize
== wxDefaultSize
))
102 m_parentSizer
->SetItemMinSize(sizer
, minsize
.x
, minsize
.y
);
106 m_parentSizer
->Add(wnd
, GetLong(wxT("option")),
107 GetStyle(wxT("flag")), GetDimension(wxT("border")));
108 if (!(minsize
== wxDefaultSize
))
109 m_parentSizer
->SetItemMinSize(wnd
, minsize
.x
, minsize
.y
);
112 wxLogError(wxT("Error in resource."));
118 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
123 else if (m_class
== wxT("spacer"))
125 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
126 wxSize sz
= GetSize();
127 m_parentSizer
->Add(sz
.x
, sz
.y
,
128 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
134 wxSizer
*sizer
= NULL
;
136 wxXmlNode
*parentNode
= m_node
->GetParent();
138 wxCHECK_MSG(m_parentSizer
!= NULL
||
139 ((IsOfClass(parentNode
, wxT("wxPanel")) ||
140 IsOfClass(parentNode
, wxT("wxDialog"))) &&
141 parentNode
->GetType() == wxXML_ELEMENT_NODE
), NULL
,
142 wxT("Incorrect use of sizer: parent is not 'wxDialog' or 'wxPanel'."));
144 if (m_class
== wxT("wxBoxSizer"))
145 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
147 else if (m_class
== wxT("wxStaticBoxSizer"))
149 sizer
= new wxStaticBoxSizer(
150 new wxStaticBox(m_parentAsWindow
, -1, GetText(wxT("label"))),
151 GetStyle(wxT("orient"), wxHORIZONTAL
));
154 else if (m_class
== wxT("wxGridSizer"))
155 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
156 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
158 else if (m_class
== wxT("wxFlexGridSizer"))
160 wxFlexGridSizer
*fsizer
=
161 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
162 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
164 wxStringTokenizer tkn
;
166 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
167 while (tkn
.HasMoreTokens())
169 if (!tkn
.GetNextToken().ToULong(&l
))
170 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
172 fsizer
->AddGrowableRow(l
);
174 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
175 while (tkn
.HasMoreTokens())
177 if (!tkn
.GetNextToken().ToULong(&l
))
178 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
180 fsizer
->AddGrowableCol(l
);
184 wxSize minsize
= GetSize(wxT("minsize"));
185 if (!(minsize
== wxDefaultSize
))
186 sizer
->SetMinSize(minsize
);
188 wxSizer
*old_par
= m_parentSizer
;
189 m_parentSizer
= sizer
;
190 bool old_ins
= m_isInside
;
192 CreateChildren(m_parent
, TRUE
/*only this handler*/);
193 m_isInside
= old_ins
;
194 m_parentSizer
= old_par
;
196 if (m_parentSizer
== NULL
) // setup window:
198 m_parentAsWindow
->SetAutoLayout(TRUE
);
199 m_parentAsWindow
->SetSizer(sizer
);
201 wxXmlNode
*nd
= m_node
;
203 if (GetSize() == wxDefaultSize
)
204 sizer
->Fit(m_parentAsWindow
);
207 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
208 sizer
->SetSizeHints(m_parentAsWindow
);
217 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
219 return ((!m_isInside
&& IsSizerNode(node
)) ||
220 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
221 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));