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
);
75 wxObject
*wxSizerXmlHandler::DoCreateResource()
77 if (m_class
== wxT("sizeritem"))
79 wxXmlNode
*n
= GetParamNode(wxT("object"));
82 n
= GetParamNode(wxT("object_ref"));
86 bool old_ins
= m_isInside
;
87 wxSizer
*old_par
= m_parentSizer
;
89 if (!IsSizerNode(n
)) m_parentSizer
= NULL
;
90 wxObject
*item
= CreateResFromNode(n
, m_parent
, NULL
);
92 m_parentSizer
= old_par
;
93 wxSizer
*sizer
= wxDynamicCast(item
, wxSizer
);
94 wxWindow
*wnd
= wxDynamicCast(item
, wxWindow
);
95 wxSize minsize
= GetSize(wxT("minsize"));
99 m_parentSizer
->Add(sizer
, GetLong(wxT("option")),
100 GetStyle(wxT("flag")), GetDimension(wxT("border")));
101 if (!(minsize
== wxDefaultSize
))
102 m_parentSizer
->SetItemMinSize(sizer
, minsize
.x
, minsize
.y
);
106 m_parentSizer
->Add(wnd
, GetLong(wxT("option")),
107 GetStyle(wxT("flag")), GetDimension(wxT("border")));
108 if (!(minsize
== wxDefaultSize
))
109 m_parentSizer
->SetItemMinSize(wnd
, minsize
.x
, minsize
.y
);
112 wxLogError(wxT("Error in resource."));
118 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
123 else if (m_class
== wxT("spacer"))
125 wxCHECK_MSG(m_parentSizer
, NULL
, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
126 wxSize sz
= GetSize();
127 m_parentSizer
->Add(sz
.x
, sz
.y
,
128 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
134 wxSizer
*sizer
= NULL
;
136 wxXmlNode
*parentNode
= m_node
->GetParent();
138 wxCHECK_MSG(m_parentSizer
!= NULL
||
139 ((IsOfClass(parentNode
, wxT("wxPanel")) ||
140 IsOfClass(parentNode
, wxT("wxFrame")) ||
141 IsOfClass(parentNode
, wxT("wxDialog"))) &&
142 parentNode
->GetType() == wxXML_ELEMENT_NODE
), NULL
,
143 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
145 if (m_class
== wxT("wxBoxSizer"))
146 sizer
= new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL
));
148 else if (m_class
== wxT("wxStaticBoxSizer"))
150 sizer
= new wxStaticBoxSizer(
151 new wxStaticBox(m_parentAsWindow
, -1, GetText(wxT("label"))),
152 GetStyle(wxT("orient"), wxHORIZONTAL
));
155 else if (m_class
== wxT("wxGridSizer"))
156 sizer
= new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
157 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
159 else if (m_class
== wxT("wxFlexGridSizer"))
161 wxFlexGridSizer
*fsizer
=
162 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
163 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
165 wxStringTokenizer tkn
;
167 tkn
.SetString(GetParamValue(wxT("growablerows")), wxT(","));
168 while (tkn
.HasMoreTokens())
170 if (!tkn
.GetNextToken().ToULong(&l
))
171 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
173 fsizer
->AddGrowableRow(l
);
175 tkn
.SetString(GetParamValue(wxT("growablecols")), wxT(","));
176 while (tkn
.HasMoreTokens())
178 if (!tkn
.GetNextToken().ToULong(&l
))
179 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
181 fsizer
->AddGrowableCol(l
);
185 wxSize minsize
= GetSize(wxT("minsize"));
186 if (!(minsize
== wxDefaultSize
))
187 sizer
->SetMinSize(minsize
);
189 wxSizer
*old_par
= m_parentSizer
;
190 m_parentSizer
= sizer
;
191 bool old_ins
= m_isInside
;
193 CreateChildren(m_parent
, TRUE
/*only this handler*/);
194 m_isInside
= old_ins
;
195 m_parentSizer
= old_par
;
197 if (m_parentSizer
== NULL
) // setup window:
199 m_parentAsWindow
->SetAutoLayout(TRUE
);
200 m_parentAsWindow
->SetSizer(sizer
);
202 wxXmlNode
*nd
= m_node
;
204 if (GetSize() == wxDefaultSize
)
205 sizer
->Fit(m_parentAsWindow
);
208 if (m_parentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX
| wxRESIZE_BORDER
))
209 sizer
->SetSizeHints(m_parentAsWindow
);
218 bool wxSizerXmlHandler::CanHandle(wxXmlNode
*node
)
220 return ((!m_isInside
&& IsSizerNode(node
)) ||
221 (m_isInside
&& IsOfClass(node
, wxT("sizeritem"))) ||
222 (m_isInside
&& IsOfClass(node
, wxT("spacer"))));