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
, -1, GetText(wxT("label"))),
154 GetStyle(wxT("orient"), wxHORIZONTAL
));
157 else if (m_class
== wxT("wxGridSizer"))
158 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
159 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
161 else if (m_class
== wxT("wxFlexGridSizer"))
163 wxFlexGridSizer
*fsizer
=
164 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
165 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
167 wxStringTokenizer tkn
;
169 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
170 while (tkn
.HasMoreTokens())
172 if (!tkn
.GetNextToken().ToULong(&l
))
173 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
175 fsizer
->AddGrowableRow(l
);
177 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
178 while (tkn
.HasMoreTokens())
180 if (!tkn
.GetNextToken().ToULong(&l
))
181 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
183 fsizer
->AddGrowableCol(l
);
187 wxSize minsize
= GetSize(wxT("minsize"));
188 if (!(minsize
== wxDefaultSize
))
189 sizer
->SetMinSize(minsize
);
191 wxSizer
*old_par
= m_parentSizer
;
192 m_parentSizer
= sizer
;
193 bool old_ins
= m_isInside
;
195 CreateChildren(m_parent
, TRUE
/*only this handler*/);
196 m_isInside
= old_ins
;
197 m_parentSizer
= old_par
;
199 if (m_parentSizer
== NULL
) // setup window:
201 m_parentAsWindow
->SetAutoLayout(TRUE
);
202 m_parentAsWindow
->SetSizer(sizer
);
204 wxXmlNode
*nd
= m_node
;
206 if (GetSize() == wxDefaultSize
)
207 sizer
->Fit(m_parentAsWindow
);
210 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
211 sizer
->SetSizeHints(m_parentAsWindow
);
220 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
222 return ((!m_isInside
&& IsSizerNode(node
)) ||
223 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
224 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));