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 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode
*node
)
32 return (IsOfClass(node
, wxT("wxBoxSizer"))) ||
33 (IsOfClass(node
, wxT("wxStaticBoxSizer"))) ||
34 (IsOfClass(node
, wxT("wxGridSizer"))) ||
35 (IsOfClass(node
, wxT("wxFlexGridSizer")));
40 wxSizerXmlHandler::wxSizerXmlHandler()
41 : wxXmlResourceHandler(), m_isInside(FALSE
), m_parentSizer(NULL
)
43 XRC_ADD_STYLE(wxHORIZONTAL
);
44 XRC_ADD_STYLE(wxVERTICAL
);
47 XRC_ADD_STYLE(wxLEFT
);
48 XRC_ADD_STYLE(wxRIGHT
);
50 XRC_ADD_STYLE(wxBOTTOM
);
51 XRC_ADD_STYLE(wxNORTH
);
52 XRC_ADD_STYLE(wxSOUTH
);
53 XRC_ADD_STYLE(wxEAST
);
54 XRC_ADD_STYLE(wxWEST
);
57 XRC_ADD_STYLE(wxGROW
);
58 XRC_ADD_STYLE(wxEXPAND
);
59 XRC_ADD_STYLE(wxSHAPED
);
60 XRC_ADD_STYLE(wxSTRETCH_NOT
);
62 XRC_ADD_STYLE(wxALIGN_CENTER
);
63 XRC_ADD_STYLE(wxALIGN_CENTRE
);
64 XRC_ADD_STYLE(wxALIGN_LEFT
);
65 XRC_ADD_STYLE(wxALIGN_TOP
);
66 XRC_ADD_STYLE(wxALIGN_RIGHT
);
67 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
68 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
69 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
70 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
71 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
73 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
78 wxObject
*wxSizerXmlHandler::DoCreateResource()
80 if (m_class
== wxT("sizeritem"))
82 wxXmlNode
*n
= GetParamNode(wxT("object"));
85 n
= GetParamNode(wxT("object_ref"));
89 bool old_ins
= m_isInside
;
90 wxSizer
*old_par
= m_parentSizer
;
92 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
93 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
95 m_parentSizer
= old_par
;
96 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
97 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
98 wxSize minsize
= GetSize(wxT("minsize"));
102 m_parentSizer
->Add(sizer
, GetLong(wxT("option")),
103 GetStyle(wxT("flag")), GetDimension(wxT("border")));
104 if (!(minsize
== wxDefaultSize
))
105 m_parentSizer
->SetItemMinSize(sizer
, minsize
.x
, minsize
.y
);
109 m_parentSizer
->Add(wnd
, GetLong(wxT("option")),
110 GetStyle(wxT("flag")), GetDimension(wxT("border")));
111 if (!(minsize
== wxDefaultSize
))
112 m_parentSizer
->SetItemMinSize(wnd
, minsize
.x
, minsize
.y
);
115 wxLogError(wxT("Error in resource."));
121 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
126 else if (m_class
== wxT("spacer"))
128 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
129 wxSize sz
= GetSize();
130 m_parentSizer
->Add(sz
.x
, sz
.y
,
131 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
137 wxSizer
*sizer
= NULL
;
139 wxXmlNode
*parentNode
= m_node
->GetParent();
141 wxCHECK_MSG(m_parentSizer
!= NULL
||
142 (parentNode
->GetType() == wxXML_ELEMENT_NODE
&&
143 (m_parentAsWindow
->IsKindOf(CLASSINFO(wxPanel
)) ||
144 m_parentAsWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
145 m_parentAsWindow
->IsKindOf(CLASSINFO(wxDialog
)))
147 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
149 if (m_class
== wxT("wxBoxSizer"))
150 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
152 else if (m_class
== wxT("wxStaticBoxSizer"))
154 sizer
= new wxStaticBoxSizer(
155 new wxStaticBox(m_parentAsWindow
,
157 GetText(wxT("label")),
158 wxDefaultPosition
, wxDefaultSize
,
161 GetStyle(wxT("orient"), wxHORIZONTAL
));
164 else if (m_class
== wxT("wxGridSizer"))
165 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
166 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
168 else if (m_class
== wxT("wxFlexGridSizer"))
170 wxFlexGridSizer
*fsizer
=
171 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
172 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
174 wxStringTokenizer tkn
;
176 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
177 while (tkn
.HasMoreTokens())
179 if (!tkn
.GetNextToken().ToULong(&l
))
180 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
182 fsizer
->AddGrowableRow(l
);
184 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
185 while (tkn
.HasMoreTokens())
187 if (!tkn
.GetNextToken().ToULong(&l
))
188 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
190 fsizer
->AddGrowableCol(l
);
194 wxSize minsize
= GetSize(wxT("minsize"));
195 if (!(minsize
== wxDefaultSize
))
196 sizer
->SetMinSize(minsize
);
198 wxSizer
*old_par
= m_parentSizer
;
199 m_parentSizer
= sizer
;
200 bool old_ins
= m_isInside
;
202 CreateChildren(m_parent
, TRUE
/*only this handler*/);
203 m_isInside
= old_ins
;
204 m_parentSizer
= old_par
;
206 if (m_parentSizer
== NULL
) // setup window:
208 m_parentAsWindow
->SetAutoLayout(TRUE
);
209 m_parentAsWindow
->SetSizer(sizer
);
211 wxXmlNode
*nd
= m_node
;
213 if (GetSize() == wxDefaultSize
)
214 sizer
->Fit(m_parentAsWindow
);
217 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
218 sizer
->SetSizeHints(m_parentAsWindow
);
227 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
229 return ((!m_isInside
&& IsSizerNode(node
)) ||
230 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
231 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));