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