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