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
!= NULL
&&
146 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
147 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
148 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
150 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
152 if (m_class
== wxT("wxBoxSizer"))
153 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
155 else if (m_class
== wxT("wxStaticBoxSizer"))
157 sizer
= new wxStaticBoxSizer(
158 new wxStaticBox(m_parentAsWindow
,
160 GetText(wxT("label")),
161 wxDefaultPosition
, wxDefaultSize
,
164 GetStyle(wxT("orient"), wxHORIZONTAL
));
167 else if (m_class
== wxT("wxGridSizer"))
168 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
169 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
171 else if (m_class
== wxT("wxFlexGridSizer"))
173 wxFlexGridSizer
*fsizer
=
174 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
175 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
177 wxStringTokenizer tkn
;
179 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
180 while (tkn
.HasMoreTokens())
182 if (!tkn
.GetNextToken().ToULong(&l
))
183 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
185 fsizer
->AddGrowableRow(l
);
187 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
188 while (tkn
.HasMoreTokens())
190 if (!tkn
.GetNextToken().ToULong(&l
))
191 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
193 fsizer
->AddGrowableCol(l
);
197 wxSize minsize
= GetSize(wxT("minsize"));
198 if (!(minsize
== wxDefaultSize
))
199 sizer
->SetMinSize(minsize
);
201 wxSizer
*old_par
= m_parentSizer
;
202 m_parentSizer
= sizer
;
203 bool old_ins
= m_isInside
;
205 CreateChildren(m_parent
, TRUE
/*only this handler*/);
206 m_isInside
= old_ins
;
207 m_parentSizer
= old_par
;
209 if (m_parentSizer
== NULL
) // setup window:
211 m_parentAsWindow
->SetAutoLayout(TRUE
);
212 m_parentAsWindow
->SetSizer(sizer
);
214 wxXmlNode
*nd
= m_node
;
216 if (GetSize() == wxDefaultSize
)
217 sizer
->Fit(m_parentAsWindow
);
220 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
221 sizer
->SetSizeHints(m_parentAsWindow
);
230 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
232 return ((!m_isInside
&& IsSizerNode(node
)) ||
233 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
234 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));