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 XRC_ADD_STYLE(wxHORIZONTAL
);
43 XRC_ADD_STYLE(wxVERTICAL
);
46 XRC_ADD_STYLE(wxLEFT
);
47 XRC_ADD_STYLE(wxRIGHT
);
49 XRC_ADD_STYLE(wxBOTTOM
);
50 XRC_ADD_STYLE(wxNORTH
);
51 XRC_ADD_STYLE(wxSOUTH
);
52 XRC_ADD_STYLE(wxEAST
);
53 XRC_ADD_STYLE(wxWEST
);
56 XRC_ADD_STYLE(wxGROW
);
57 XRC_ADD_STYLE(wxEXPAND
);
58 XRC_ADD_STYLE(wxSHAPED
);
59 XRC_ADD_STYLE(wxSTRETCH_NOT
);
61 XRC_ADD_STYLE(wxALIGN_CENTER
);
62 XRC_ADD_STYLE(wxALIGN_CENTRE
);
63 XRC_ADD_STYLE(wxALIGN_LEFT
);
64 XRC_ADD_STYLE(wxALIGN_TOP
);
65 XRC_ADD_STYLE(wxALIGN_RIGHT
);
66 XRC_ADD_STYLE(wxALIGN_BOTTOM
);
67 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
);
68 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
);
69 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL
);
70 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL
);
72 XRC_ADD_STYLE(wxADJUST_MINSIZE
);
77 wxObject
*wxSizerXmlHandler::DoCreateResource()
79 if (m_class
== wxT("sizeritem"))
81 wxXmlNode
*n
= GetParamNode(wxT("object"));
84 n
= GetParamNode(wxT("object_ref"));
88 bool old_ins
= m_isInside
;
89 wxSizer
*old_par
= m_parentSizer
;
91 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
92 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
94 m_parentSizer
= old_par
;
95 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
96 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
97 wxSize minsize
= GetSize(wxT("minsize"));
101 m_parentSizer
->Add(sizer
, GetLong(wxT("option")),
102 GetStyle(wxT("flag")), GetDimension(wxT("border")));
103 if (!(minsize
== wxDefaultSize
))
104 m_parentSizer
->SetItemMinSize(sizer
, minsize
.x
, minsize
.y
);
108 m_parentSizer
->Add(wnd
, GetLong(wxT("option")),
109 GetStyle(wxT("flag")), GetDimension(wxT("border")));
110 if (!(minsize
== wxDefaultSize
))
111 m_parentSizer
->SetItemMinSize(wnd
, minsize
.x
, minsize
.y
);
114 wxLogError(wxT("Error in resource."));
120 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
125 else if (m_class
== wxT("spacer"))
127 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
128 wxSize sz
= GetSize();
129 m_parentSizer
->Add(sz
.x
, sz
.y
,
130 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
136 wxSizer
*sizer
= NULL
;
138 wxXmlNode
*parentNode
= m_node
->GetParent();
140 wxCHECK_MSG(m_parentSizer
!= NULL
||
141 ((IsOfClass(parentNode
, wxT("wxPanel")) ||
142 IsOfClass(parentNode
, wxT("wxFrame")) ||
143 IsOfClass(parentNode
, wxT("wxDialog"))) &&
144 parentNode
->GetType() == wxXML_ELEMENT_NODE
), NULL
,
145 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
147 if (m_class
== wxT("wxBoxSizer"))
148 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
150 else if (m_class
== wxT("wxStaticBoxSizer"))
152 sizer
= new wxStaticBoxSizer(
153 new wxStaticBox(m_parentAsWindow
,
155 GetText(wxT("label")),
156 wxDefaultPosition
, wxDefaultSize
,
159 GetStyle(wxT("orient"), wxHORIZONTAL
));
162 else if (m_class
== wxT("wxGridSizer"))
163 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
164 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
166 else if (m_class
== wxT("wxFlexGridSizer"))
168 wxFlexGridSizer
*fsizer
=
169 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
170 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
172 wxStringTokenizer tkn
;
174 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
175 while (tkn
.HasMoreTokens())
177 if (!tkn
.GetNextToken().ToULong(&l
))
178 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
180 fsizer
->AddGrowableRow(l
);
182 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
183 while (tkn
.HasMoreTokens())
185 if (!tkn
.GetNextToken().ToULong(&l
))
186 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
188 fsizer
->AddGrowableCol(l
);
192 wxSize minsize
= GetSize(wxT("minsize"));
193 if (!(minsize
== wxDefaultSize
))
194 sizer
->SetMinSize(minsize
);
196 wxSizer
*old_par
= m_parentSizer
;
197 m_parentSizer
= sizer
;
198 bool old_ins
= m_isInside
;
200 CreateChildren(m_parent
, TRUE
/*only this handler*/);
201 m_isInside
= old_ins
;
202 m_parentSizer
= old_par
;
204 if (m_parentSizer
== NULL
) // setup window:
206 m_parentAsWindow
->SetAutoLayout(TRUE
);
207 m_parentAsWindow
->SetSizer(sizer
);
209 wxXmlNode
*nd
= m_node
;
211 if (GetSize() == wxDefaultSize
)
212 sizer
->Fit(m_parentAsWindow
);
215 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
216 sizer
->SetSizeHints(m_parentAsWindow
);
225 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
227 return ((!m_isInside
&& IsSizerNode(node
)) ||
228 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
229 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));