]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_sizer.cpp
add ms78 projects
[wxWidgets.git] / src / xrc / xh_sizer.cpp
CommitLineData
78d14f80 1/////////////////////////////////////////////////////////////////////////////
1c067fe3 2// Name: src/xrc/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/////////////////////////////////////////////////////////////////////////////
f80ea77b 10
78d14f80
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
621be1ec 18#if wxUSE_XRC
a1e4ec87 19
78d14f80 20#include "wx/xrc/xh_sizer.h"
e4db172a
WS
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
8e609c82 24 #include "wx/panel.h"
876cd6f7 25 #include "wx/statbox.h"
ed2fbeb8 26 #include "wx/sizer.h"
4107600f
MW
27 #include "wx/frame.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
5e1a9f05 30 #include "wx/scrolwin.h"
e4db172a
WS
31#endif
32
0d077b41 33#include "wx/gbsizer.h"
78d14f80
VS
34#include "wx/notebook.h"
35#include "wx/tokenzr.h"
36
78d14f80 37
172541f6
VS
38//-----------------------------------------------------------------------------
39// wxSizerXmlHandler
40//-----------------------------------------------------------------------------
78d14f80 41
0d077b41 42IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
78d14f80 43
f80ea77b 44wxSizerXmlHandler::wxSizerXmlHandler()
ed2fbeb8
WS
45 :wxXmlResourceHandler(),
46 m_isInside(false),
47 m_isGBS(false),
48 m_parentSizer(NULL)
78d14f80 49{
544fee32
VS
50 XRC_ADD_STYLE(wxHORIZONTAL);
51 XRC_ADD_STYLE(wxVERTICAL);
78d14f80
VS
52
53 // and flags
544fee32
VS
54 XRC_ADD_STYLE(wxLEFT);
55 XRC_ADD_STYLE(wxRIGHT);
56 XRC_ADD_STYLE(wxTOP);
57 XRC_ADD_STYLE(wxBOTTOM);
58 XRC_ADD_STYLE(wxNORTH);
59 XRC_ADD_STYLE(wxSOUTH);
60 XRC_ADD_STYLE(wxEAST);
61 XRC_ADD_STYLE(wxWEST);
62 XRC_ADD_STYLE(wxALL);
78d14f80 63
544fee32
VS
64 XRC_ADD_STYLE(wxGROW);
65 XRC_ADD_STYLE(wxEXPAND);
66 XRC_ADD_STYLE(wxSHAPED);
67 XRC_ADD_STYLE(wxSTRETCH_NOT);
78d14f80 68
544fee32
VS
69 XRC_ADD_STYLE(wxALIGN_CENTER);
70 XRC_ADD_STYLE(wxALIGN_CENTRE);
71 XRC_ADD_STYLE(wxALIGN_LEFT);
72 XRC_ADD_STYLE(wxALIGN_TOP);
73 XRC_ADD_STYLE(wxALIGN_RIGHT);
74 XRC_ADD_STYLE(wxALIGN_BOTTOM);
75 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
76 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
77 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
78 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
f80ea77b 79
c66b63e5 80 XRC_ADD_STYLE(wxFIXED_MINSIZE);
78d14f80
VS
81}
82
83
84
0d077b41
RD
85bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
86{
87 return ( (!m_isInside && IsSizerNode(node)) ||
88 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
f80ea77b 89 (m_isInside && IsOfClass(node, wxT("spacer")))
0d077b41
RD
90 );
91}
92
f80ea77b 93
0d077b41 94wxObject* wxSizerXmlHandler::DoCreateResource()
f80ea77b 95{
78d14f80 96 if (m_class == wxT("sizeritem"))
0d077b41 97 return Handle_sizeritem();
f80ea77b 98
78d14f80 99 else if (m_class == wxT("spacer"))
0d077b41
RD
100 return Handle_spacer();
101
102 else
103 return Handle_sizer();
104}
105
106
107
108
109bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
110{
111 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
112 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
113 (IsOfClass(node, wxT("wxGridSizer"))) ||
114 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
115 (IsOfClass(node, wxT("wxGridBagSizer")));
116}
117
118
119wxObject* wxSizerXmlHandler::Handle_sizeritem()
120{
121 // find the item to be managed by this sizeritem
122 wxXmlNode *n = GetParamNode(wxT("object"));
123 if ( !n )
124 n = GetParamNode(wxT("object_ref"));
125
126 // did we find one?
127 if (n)
128 {
129 // create a sizer item for it
130 wxSizerItem* sitem = MakeSizerItem();
f80ea77b 131
0d077b41
RD
132 // now fetch the item to be managed
133 bool old_gbs = m_isGBS;
134 bool old_ins = m_isInside;
135 wxSizer *old_par = m_parentSizer;
f80ea77b
WS
136 m_isInside = false;
137 if (!IsSizerNode(n)) m_parentSizer = NULL;
0d077b41
RD
138 wxObject *item = CreateResFromNode(n, m_parent, NULL);
139 m_isInside = old_ins;
140 m_parentSizer = old_par;
141 m_isGBS = old_gbs;
142
143 // and figure out what type it is
144 wxSizer *sizer = wxDynamicCast(item, wxSizer);
145 wxWindow *wnd = wxDynamicCast(item, wxWindow);
f80ea77b 146
0d077b41 147 if (sizer)
a50cf60e 148 sitem->AssignSizer(sizer);
0d077b41 149 else if (wnd)
a50cf60e 150 sitem->AssignWindow(wnd);
f80ea77b 151 else
0d077b41
RD
152 wxLogError(wxT("Error in resource."));
153
1eba2193
RD
154 // finally, set other wxSizerItem attributes
155 SetSizerItemAttributes(sitem);
156
0d077b41
RD
157 AddSizerItem(sitem);
158 return item;
159 }
160 else /*n == NULL*/
78d14f80 161 {
0d077b41 162 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
78d14f80
VS
163 return NULL;
164 }
0d077b41
RD
165}
166
167
168wxObject* wxSizerXmlHandler::Handle_spacer()
169{
170 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
171
172 wxSizerItem* sitem = MakeSizerItem();
173 SetSizerItemAttributes(sitem);
a50cf60e 174 sitem->AssignSpacer(GetSize());
f80ea77b 175 AddSizerItem(sitem);
0d077b41
RD
176 return NULL;
177}
178
78d14f80 179
0d077b41
RD
180wxObject* wxSizerXmlHandler::Handle_sizer()
181{
182 wxSizer *sizer = NULL;
f80ea77b 183
0d077b41 184 wxXmlNode *parentNode = m_node->GetParent();
78d14f80 185
0d077b41 186 wxCHECK_MSG(m_parentSizer != NULL ||
0a904ad4 187 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
35386afc
VZ
188 m_parentAsWindow), NULL,
189 wxT("Sizer must have a window parent node"));
78d14f80 190
0d077b41
RD
191 if (m_class == wxT("wxBoxSizer"))
192 sizer = Handle_wxBoxSizer();
193
627ddac9 194#if wxUSE_STATBOX
0d077b41
RD
195 else if (m_class == wxT("wxStaticBoxSizer"))
196 sizer = Handle_wxStaticBoxSizer();
627ddac9 197#endif
f80ea77b 198
0d077b41
RD
199 else if (m_class == wxT("wxGridSizer"))
200 sizer = Handle_wxGridSizer();
f80ea77b 201
0d077b41
RD
202 else if (m_class == wxT("wxFlexGridSizer"))
203 sizer = Handle_wxFlexGridSizer();
78d14f80 204
0d077b41
RD
205 else if (m_class == wxT("wxGridBagSizer"))
206 sizer = Handle_wxGridBagSizer();
78d14f80 207
641c5f1a
VZ
208 if ( !sizer )
209 {
210 wxLogError(_T("Failed to create size of class \"%s\""), m_class.c_str());
211 return NULL;
212 }
f80ea77b 213
0d077b41
RD
214 wxSize minsize = GetSize(wxT("minsize"));
215 if (!(minsize == wxDefaultSize))
216 sizer->SetMinSize(minsize);
217
218 // save state
219 wxSizer *old_par = m_parentSizer;
220 bool old_ins = m_isInside;
0d077b41
RD
221
222 // set new state
223 m_parentSizer = sizer;
f80ea77b 224 m_isInside = true;
0d077b41 225 m_isGBS = (m_class == wxT("wxGridBagSizer"));
f80ea77b
WS
226
227 CreateChildren(m_parent, true/*only this handler*/);
0d077b41
RD
228
229 // restore state
0d077b41
RD
230 m_isInside = old_ins;
231 m_parentSizer = old_par;
232
233 if (m_parentSizer == NULL) // setup window:
234 {
0d077b41
RD
235 m_parentAsWindow->SetSizer(sizer);
236
237 wxXmlNode *nd = m_node;
238 m_node = parentNode;
239 if (GetSize() == wxDefaultSize)
5e1a9f05
VS
240 {
241 if ( wxDynamicCast(m_parentAsWindow, wxScrolledWindow) != NULL )
242 {
243 sizer->FitInside(m_parentAsWindow);
244 }
245 else
246 {
247 sizer->Fit(m_parentAsWindow);
248 }
249 }
0d077b41
RD
250 m_node = nd;
251
ea813d91
VS
252 if (m_parentAsWindow->IsTopLevel())
253 {
0d077b41 254 sizer->SetSizeHints(m_parentAsWindow);
ea813d91 255 }
0d077b41 256 }
f80ea77b 257
0d077b41
RD
258 return sizer;
259}
260
261
262wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
263{
264 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
f80ea77b
WS
265}
266
627ddac9 267#if wxUSE_STATBOX
0d077b41
RD
268wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
269{
270 return new wxStaticBoxSizer(
271 new wxStaticBox(m_parentAsWindow,
272 GetID(),
273 GetText(wxT("label")),
274 wxDefaultPosition, wxDefaultSize,
275 0/*style*/,
276 GetName()),
277 GetStyle(wxT("orient"), wxHORIZONTAL));
278}
627ddac9 279#endif // wxUSE_STATBOX
f80ea77b 280
0d077b41
RD
281wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
282{
283 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
284 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
285}
286
287
288wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
289{
f80ea77b 290 wxFlexGridSizer *sizer =
0d077b41
RD
291 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
292 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
293 SetGrowables(sizer, wxT("growablerows"), true);
294 SetGrowables(sizer, wxT("growablecols"), false);
295 return sizer;
296}
297
298
299wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
300{
f80ea77b 301 wxGridBagSizer *sizer =
0d077b41
RD
302 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
303 SetGrowables(sizer, wxT("growablerows"), true);
304 SetGrowables(sizer, wxT("growablecols"), false);
305 return sizer;
306}
307
308
309
310
311void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
312 const wxChar* param,
313 bool rows)
314{
315 wxStringTokenizer tkn;
316 unsigned long l;
317 tkn.SetString(GetParamValue(param), wxT(","));
318 while (tkn.HasMoreTokens())
319 {
320 if (!tkn.GetNextToken().ToULong(&l))
321 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
322 else {
323 if (rows)
324 sizer->AddGrowableRow(l);
325 else
326 sizer->AddGrowableCol(l);
78d14f80 327 }
78d14f80
VS
328 }
329}
330
331
0d077b41
RD
332wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
333{
334 wxSize sz = GetSize(param);
335 if (sz.x < 0) sz.x = 0;
336 if (sz.y < 0) sz.y = 0;
337 return wxGBPosition(sz.x, sz.y);
338}
78d14f80 339
0d077b41
RD
340wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
341{
342 wxSize sz = GetSize(param);
343 if (sz.x < 1) sz.x = 1;
344 if (sz.y < 1) sz.y = 1;
345 return wxGBSpan(sz.x, sz.y);
346}
347
348
349
350wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
351{
352 if (m_isGBS)
353 return new wxGBSizerItem();
354 else
355 return new wxSizerItem();
356}
357
358void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
359{
360 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
361 sitem->SetFlag(GetStyle(wxT("flag")));
362 sitem->SetBorder(GetDimension(wxT("border")));
363 wxSize sz = GetSize(wxT("minsize"));
364 if (!(sz == wxDefaultSize))
1eba2193 365 sitem->SetMinSize(sz);
0d077b41
RD
366 sz = GetSize(wxT("ratio"));
367 if (!(sz == wxDefaultSize))
368 sitem->SetRatio(sz);
f80ea77b 369
0d077b41
RD
370 if (m_isGBS)
371 {
372 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
ea0d8ca6
RD
373 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
374 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
f80ea77b 375 }
86909f4c
VZ
376
377 // record the id of the item, if any, for use by XRCSIZERITEM()
378 sitem->SetId(GetID());
0d077b41
RD
379}
380
381void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
78d14f80 382{
0d077b41
RD
383 if (m_isGBS)
384 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
385 else
386 m_parentSizer->Add(sitem);
78d14f80 387}
f80ea77b 388
172541f6
VS
389
390
391//-----------------------------------------------------------------------------
392// wxStdDialogButtonSizerXmlHandler
393//-----------------------------------------------------------------------------
dd47af27 394#if wxUSE_BUTTON
172541f6
VS
395
396IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler)
397
398wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
399 : m_isInside(false), m_parentSizer(NULL)
400{
401}
402
403wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource()
404{
405 if (m_class == wxT("wxStdDialogButtonSizer"))
406 {
407 wxASSERT( !m_parentSizer );
408
409 wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer;
410 m_isInside = true;
411
412 CreateChildren(m_parent, true/*only this handler*/);
413
414 m_parentSizer->Realize();
415
416 m_isInside = false;
417 m_parentSizer = NULL;
418
419 return s;
420 }
421 else // m_class == "button"
422 {
423 wxASSERT( m_parentSizer );
424
425 // find the item to be managed by this sizeritem
426 wxXmlNode *n = GetParamNode(wxT("object"));
427 if ( !n )
428 n = GetParamNode(wxT("object_ref"));
429
430 // did we find one?
431 if (n)
432 {
433 wxObject *item = CreateResFromNode(n, m_parent, NULL);
434 wxButton *button = wxDynamicCast(item, wxButton);
435
436 if (button)
437 m_parentSizer->AddButton(button);
438 else
439 wxLogError(wxT("Error in resource - expected button."));
440
441 return item;
442 }
443 else /*n == NULL*/
444 {
445 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
446 return NULL;
447 }
448 }
449}
450
451bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node)
452{
453 return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) ||
454 (m_isInside && IsOfClass(node, wxT("button")));
455}
dd47af27 456#endif // wxUSE_BUTTON
172541f6 457
621be1ec 458#endif // wxUSE_XRC