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