]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_sizer.cpp
Use Bind
[wxWidgets.git] / src / xrc / xh_sizer.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xh_sizer.cpp
b5d6954b 3// Purpose: XRC resource for wxBoxSizer
78d14f80
VS
4// Author: Vaclav Slavik
5// Created: 2000/03/21
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "xh_sizer.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/xrc/xh_sizer.h"
23#include "wx/sizer.h"
0d077b41 24#include "wx/gbsizer.h"
78d14f80
VS
25#include "wx/log.h"
26#include "wx/statbox.h"
27#include "wx/notebook.h"
9e82cf98 28#include "wx/panel.h"
78d14f80
VS
29#include "wx/tokenzr.h"
30
78d14f80
VS
31
32
0d077b41 33IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
78d14f80 34
0d077b41
RD
35
36
78d14f80 37wxSizerXmlHandler::wxSizerXmlHandler()
0d077b41
RD
38 : wxXmlResourceHandler(),
39 m_isInside(FALSE),
40 m_isGBS(FALSE),
41 m_parentSizer(NULL)
78d14f80 42{
544fee32
VS
43 XRC_ADD_STYLE(wxHORIZONTAL);
44 XRC_ADD_STYLE(wxVERTICAL);
78d14f80
VS
45
46 // and flags
544fee32
VS
47 XRC_ADD_STYLE(wxLEFT);
48 XRC_ADD_STYLE(wxRIGHT);
49 XRC_ADD_STYLE(wxTOP);
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);
55 XRC_ADD_STYLE(wxALL);
78d14f80 56
544fee32
VS
57 XRC_ADD_STYLE(wxGROW);
58 XRC_ADD_STYLE(wxEXPAND);
59 XRC_ADD_STYLE(wxSHAPED);
60 XRC_ADD_STYLE(wxSTRETCH_NOT);
78d14f80 61
544fee32
VS
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);
a02376c8
VS
72
73 XRC_ADD_STYLE(wxADJUST_MINSIZE);
78d14f80
VS
74}
75
76
77
0d077b41
RD
78bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
79{
80 return ( (!m_isInside && IsSizerNode(node)) ||
81 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
82 (m_isInside && IsOfClass(node, wxT("spacer")))
83 );
84}
85
86
87wxObject* wxSizerXmlHandler::DoCreateResource()
78d14f80
VS
88{
89 if (m_class == wxT("sizeritem"))
0d077b41 90 return Handle_sizeritem();
78d14f80
VS
91
92 else if (m_class == wxT("spacer"))
0d077b41
RD
93 return Handle_spacer();
94
95 else
96 return Handle_sizer();
97}
98
99
100
101
102bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
103{
104 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
105 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
106 (IsOfClass(node, wxT("wxGridSizer"))) ||
107 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
108 (IsOfClass(node, wxT("wxGridBagSizer")));
109}
110
111
112wxObject* wxSizerXmlHandler::Handle_sizeritem()
113{
114 // find the item to be managed by this sizeritem
115 wxXmlNode *n = GetParamNode(wxT("object"));
116 if ( !n )
117 n = GetParamNode(wxT("object_ref"));
118
119 // did we find one?
120 if (n)
121 {
122 // create a sizer item for it
123 wxSizerItem* sitem = MakeSizerItem();
124 SetSizerItemAttributes(sitem);
125
126 // now fetch the item to be managed
127 bool old_gbs = m_isGBS;
128 bool old_ins = m_isInside;
129 wxSizer *old_par = m_parentSizer;
130 m_isInside = FALSE;
131 if (!IsSizerNode(n)) m_parentSizer = NULL;
132 wxObject *item = CreateResFromNode(n, m_parent, NULL);
133 m_isInside = old_ins;
134 m_parentSizer = old_par;
135 m_isGBS = old_gbs;
136
137 // and figure out what type it is
138 wxSizer *sizer = wxDynamicCast(item, wxSizer);
139 wxWindow *wnd = wxDynamicCast(item, wxWindow);
140
141 if (sizer)
142 sitem->SetSizer(sizer);
143 else if (wnd)
144 sitem->SetWindow(wnd);
145 else
146 wxLogError(wxT("Error in resource."));
147
148 AddSizerItem(sitem);
149 return item;
150 }
151 else /*n == NULL*/
78d14f80 152 {
0d077b41 153 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
78d14f80
VS
154 return NULL;
155 }
0d077b41
RD
156}
157
158
159wxObject* wxSizerXmlHandler::Handle_spacer()
160{
161 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
162
163 wxSizerItem* sitem = MakeSizerItem();
164 SetSizerItemAttributes(sitem);
165 sitem->SetSpacer(GetSize());
166 AddSizerItem(sitem);
167 return NULL;
168}
169
78d14f80 170
0d077b41
RD
171wxObject* wxSizerXmlHandler::Handle_sizer()
172{
173 wxSizer *sizer = NULL;
78d14f80 174
0d077b41 175 wxXmlNode *parentNode = m_node->GetParent();
78d14f80 176
0d077b41 177 wxCHECK_MSG(m_parentSizer != NULL ||
0a904ad4 178 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
0d077b41
RD
179 m_parentAsWindow != NULL &&
180 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
181 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
182 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
183 ), NULL,
5dff4e24 184 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
78d14f80 185
0d077b41
RD
186 if (m_class == wxT("wxBoxSizer"))
187 sizer = Handle_wxBoxSizer();
188
189 else if (m_class == wxT("wxStaticBoxSizer"))
190 sizer = Handle_wxStaticBoxSizer();
78d14f80 191
0d077b41
RD
192 else if (m_class == wxT("wxGridSizer"))
193 sizer = Handle_wxGridSizer();
78d14f80 194
0d077b41
RD
195 else if (m_class == wxT("wxFlexGridSizer"))
196 sizer = Handle_wxFlexGridSizer();
78d14f80 197
0d077b41
RD
198 else if (m_class == wxT("wxGridBagSizer"))
199 sizer = Handle_wxGridBagSizer();
78d14f80 200
0d077b41
RD
201
202 wxSize minsize = GetSize(wxT("minsize"));
203 if (!(minsize == wxDefaultSize))
204 sizer->SetMinSize(minsize);
205
206 // save state
207 wxSizer *old_par = m_parentSizer;
208 bool old_ins = m_isInside;
0d077b41
RD
209
210 // set new state
211 m_parentSizer = sizer;
212 m_isInside = TRUE;
213 m_isGBS = (m_class == wxT("wxGridBagSizer"));
214
215 CreateChildren(m_parent, TRUE/*only this handler*/);
216
217 // restore state
0d077b41
RD
218 m_isInside = old_ins;
219 m_parentSizer = old_par;
220
221 if (m_parentSizer == NULL) // setup window:
222 {
223 m_parentAsWindow->SetAutoLayout(TRUE);
224 m_parentAsWindow->SetSizer(sizer);
225
226 wxXmlNode *nd = m_node;
227 m_node = parentNode;
228 if (GetSize() == wxDefaultSize)
229 sizer->Fit(m_parentAsWindow);
230 m_node = nd;
231
232 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
233 sizer->SetSizeHints(m_parentAsWindow);
234 }
78d14f80 235
0d077b41
RD
236 return sizer;
237}
238
239
240wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
241{
242 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
243}
244
245wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
246{
247 return new wxStaticBoxSizer(
248 new wxStaticBox(m_parentAsWindow,
249 GetID(),
250 GetText(wxT("label")),
251 wxDefaultPosition, wxDefaultSize,
252 0/*style*/,
253 GetName()),
254 GetStyle(wxT("orient"), wxHORIZONTAL));
255}
256
257wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
258{
259 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
260 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
261}
262
263
264wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
265{
266 wxFlexGridSizer *sizer =
267 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
268 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
269 SetGrowables(sizer, wxT("growablerows"), true);
270 SetGrowables(sizer, wxT("growablecols"), false);
271 return sizer;
272}
273
274
275wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
276{
277 wxGridBagSizer *sizer =
278 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
279 SetGrowables(sizer, wxT("growablerows"), true);
280 SetGrowables(sizer, wxT("growablecols"), false);
281 return sizer;
282}
283
284
285
286
287void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
288 const wxChar* param,
289 bool rows)
290{
291 wxStringTokenizer tkn;
292 unsigned long l;
293 tkn.SetString(GetParamValue(param), wxT(","));
294 while (tkn.HasMoreTokens())
295 {
296 if (!tkn.GetNextToken().ToULong(&l))
297 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
298 else {
299 if (rows)
300 sizer->AddGrowableRow(l);
301 else
302 sizer->AddGrowableCol(l);
78d14f80 303 }
78d14f80
VS
304 }
305}
306
307
0d077b41
RD
308wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
309{
310 wxSize sz = GetSize(param);
311 if (sz.x < 0) sz.x = 0;
312 if (sz.y < 0) sz.y = 0;
313 return wxGBPosition(sz.x, sz.y);
314}
78d14f80 315
0d077b41
RD
316wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
317{
318 wxSize sz = GetSize(param);
319 if (sz.x < 1) sz.x = 1;
320 if (sz.y < 1) sz.y = 1;
321 return wxGBSpan(sz.x, sz.y);
322}
323
324
325
326wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
327{
328 if (m_isGBS)
329 return new wxGBSizerItem();
330 else
331 return new wxSizerItem();
332}
333
334void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
335{
336 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
337 sitem->SetFlag(GetStyle(wxT("flag")));
338 sitem->SetBorder(GetDimension(wxT("border")));
339 wxSize sz = GetSize(wxT("minsize"));
340 if (!(sz == wxDefaultSize))
341 sitem->SetInitSize(sz.x, sz.y);
342 sz = GetSize(wxT("ratio"));
343 if (!(sz == wxDefaultSize))
344 sitem->SetRatio(sz);
345
346 if (m_isGBS)
347 {
348 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
ea0d8ca6
RD
349 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
350 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
0d077b41
RD
351 }
352}
353
354void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
78d14f80 355{
0d077b41
RD
356 if (m_isGBS)
357 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
358 else
359 m_parentSizer->Add(sitem);
78d14f80 360}
0d077b41
RD
361
362
363
364
365
366
367
368
369
370