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"
28 #include "wx/tokenzr.h"
30 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler
, wxXmlResourceHandler
)
32 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
34 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
35 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
36 (IsOfClass(node
, wxT("wxGridSizer"))) ||
37 (IsOfClass(node
, wxT("wxFlexGridSizer")));
42 wxSizerXmlHandler::wxSizerXmlHandler()
43 : wxXmlResourceHandler(), m_isInside(FALSE
), m_parentSizer(NULL
)
45 XRC_ADD_STYLE(wxHORIZONTAL
);
46 XRC_ADD_STYLE(wxVERTICAL
);
49 XRC_ADD_STYLE(wxLEFT
);
50 XRC_ADD_STYLE(wxRIGHT
);
52 XRC_ADD_STYLE(wxBOTTOM
);
53 XRC_ADD_STYLE(wxNORTH
);
54 XRC_ADD_STYLE(wxSOUTH
);
55 XRC_ADD_STYLE(wxEAST
);
56 XRC_ADD_STYLE(wxWEST
);
59 XRC_ADD_STYLE(wxGROW
);
60 XRC_ADD_STYLE(wxEXPAND
);
61 XRC_ADD_STYLE(wxSHAPED
);
62 XRC_ADD_STYLE(wxSTRETCH_NOT
);
64 XRC_ADD_STYLE(wxALIGN_CENTER
);
65 XRC_ADD_STYLE(wxALIGN_CENTRE
);
66 XRC_ADD_STYLE(wxALIGN_LEFT
);
67 XRC_ADD_STYLE(wxALIGN_TOP
);
68 XRC_ADD_STYLE(wxALIGN_RIGHT
);
69 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
70 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
71 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
72 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
73 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
75 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
80 wxObject
*wxSizerXmlHandler::DoCreateResource()
82 if (m_class
== wxT("sizeritem"))
84 wxXmlNode
*n
= GetParamNode(wxT("object"));
87 n
= GetParamNode(wxT("object_ref"));
91 bool old_ins
= m_isInside
;
92 wxSizer
*old_par
= m_parentSizer
;
94 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
95 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
97 m_parentSizer
= old_par
;
98 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
99 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
100 wxSize minsize
= GetSize(wxT("minsize"));
104 m_parentSizer
->Add(sizer
, GetLong(wxT("option")),
105 GetStyle(wxT("flag")), GetDimension(wxT("border")));
106 if (!(minsize
== wxDefaultSize
))
107 m_parentSizer
->SetItemMinSize(sizer
, minsize
.x
, minsize
.y
);
111 m_parentSizer
->Add(wnd
, GetLong(wxT("option")),
112 GetStyle(wxT("flag")), GetDimension(wxT("border")));
113 if (!(minsize
== wxDefaultSize
))
114 m_parentSizer
->SetItemMinSize(wnd
, minsize
.x
, minsize
.y
);
117 wxLogError(wxT("Error in resource."));
123 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
128 else if (m_class
== wxT("spacer"))
130 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
131 wxSize sz
= GetSize();
132 m_parentSizer
->Add(sz
.x
, sz
.y
,
133 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
139 wxSizer
*sizer
= NULL
;
141 wxXmlNode
*parentNode
= m_node
->GetParent();
143 wxCHECK_MSG(m_parentSizer
!= NULL
||
144 (parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
145 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
146 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
147 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
149 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
151 if (m_class
== wxT("wxBoxSizer"))
152 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
154 else if (m_class
== wxT("wxStaticBoxSizer"))
156 sizer
= new wxStaticBoxSizer(
157 new wxStaticBox(m_parentAsWindow
,
159 GetText(wxT("label")),
160 wxDefaultPosition
, wxDefaultSize
,
163 GetStyle(wxT("orient"), wxHORIZONTAL
));
166 else if (m_class
== wxT("wxGridSizer"))
167 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
168 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
170 else if (m_class
== wxT("wxFlexGridSizer"))
172 wxFlexGridSizer
*fsizer
=
173 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
174 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
176 wxStringTokenizer tkn
;
178 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
179 while (tkn
.HasMoreTokens())
181 if (!tkn
.GetNextToken().ToULong(&l
))
182 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
184 fsizer
->AddGrowableRow(l
);
186 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
187 while (tkn
.HasMoreTokens())
189 if (!tkn
.GetNextToken().ToULong(&l
))
190 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
192 fsizer
->AddGrowableCol(l
);
196 wxSize minsize
= GetSize(wxT("minsize"));
197 if (!(minsize
== wxDefaultSize
))
198 sizer
->SetMinSize(minsize
);
200 wxSizer
*old_par
= m_parentSizer
;
201 m_parentSizer
= sizer
;
202 bool old_ins
= m_isInside
;
204 CreateChildren(m_parent
, TRUE
/*only this handler*/);
205 m_isInside
= old_ins
;
206 m_parentSizer
= old_par
;
208 if (m_parentSizer
== NULL
) // setup window:
210 m_parentAsWindow
->SetAutoLayout(TRUE
);
211 m_parentAsWindow
->SetSizer(sizer
);
213 wxXmlNode
*nd
= m_node
;
215 if (GetSize() == wxDefaultSize
)
216 sizer
->Fit(m_parentAsWindow
);
219 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
220 sizer
->SetSizeHints(m_parentAsWindow
);
229 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
231 return ((!m_isInside
&& IsSizerNode(node
)) ||
232 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
233 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));