]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
Warning fix, minor source cleaning and better !PCH support.
[wxWidgets.git] / src / xrc / xh_sizer.cpp
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 #endif
28
29 #include "wx/gbsizer.h"
30 #include "wx/notebook.h"
31 #include "wx/tokenzr.h"
32
33
34 //-----------------------------------------------------------------------------
35 // wxSizerXmlHandler
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
39
40 wxSizerXmlHandler::wxSizerXmlHandler()
41 :wxXmlResourceHandler(),
42 m_isInside(false),
43 m_isGBS(false),
44 m_parentSizer(NULL)
45 {
46 XRC_ADD_STYLE(wxHORIZONTAL);
47 XRC_ADD_STYLE(wxVERTICAL);
48
49 // and flags
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);
59
60 XRC_ADD_STYLE(wxGROW);
61 XRC_ADD_STYLE(wxEXPAND);
62 XRC_ADD_STYLE(wxSHAPED);
63 XRC_ADD_STYLE(wxSTRETCH_NOT);
64
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);
75
76 XRC_ADD_STYLE(wxADJUST_MINSIZE);
77 XRC_ADD_STYLE(wxFIXED_MINSIZE);
78 }
79
80
81
82 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
83 {
84 return ( (!m_isInside && IsSizerNode(node)) ||
85 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
86 (m_isInside && IsOfClass(node, wxT("spacer")))
87 );
88 }
89
90
91 wxObject* wxSizerXmlHandler::DoCreateResource()
92 {
93 if (m_class == wxT("sizeritem"))
94 return Handle_sizeritem();
95
96 else if (m_class == wxT("spacer"))
97 return Handle_spacer();
98
99 else
100 return Handle_sizer();
101 }
102
103
104
105
106 bool 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
116 wxObject* 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();
128
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;
133 m_isInside = false;
134 if (!IsSizerNode(n)) m_parentSizer = NULL;
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);
143
144 if (sizer)
145 sitem->SetSizer(sizer);
146 else if (wnd)
147 sitem->SetWindow(wnd);
148 else
149 wxLogError(wxT("Error in resource."));
150
151 // finally, set other wxSizerItem attributes
152 SetSizerItemAttributes(sitem);
153
154 AddSizerItem(sitem);
155 return item;
156 }
157 else /*n == NULL*/
158 {
159 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
160 return NULL;
161 }
162 }
163
164
165 wxObject* 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());
172 AddSizerItem(sitem);
173 return NULL;
174 }
175
176
177 wxObject* wxSizerXmlHandler::Handle_sizer()
178 {
179 wxSizer *sizer = NULL;
180
181 wxXmlNode *parentNode = m_node->GetParent();
182
183 wxCHECK_MSG(m_parentSizer != NULL ||
184 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
185 m_parentAsWindow != NULL &&
186 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
187 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
188 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
189 ), NULL,
190 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
191
192 if (m_class == wxT("wxBoxSizer"))
193 sizer = Handle_wxBoxSizer();
194
195 else if (m_class == wxT("wxStaticBoxSizer"))
196 sizer = Handle_wxStaticBoxSizer();
197
198 else if (m_class == wxT("wxGridSizer"))
199 sizer = Handle_wxGridSizer();
200
201 else if (m_class == wxT("wxFlexGridSizer"))
202 sizer = Handle_wxFlexGridSizer();
203
204 else if (m_class == wxT("wxGridBagSizer"))
205 sizer = Handle_wxGridBagSizer();
206
207 if ( !sizer )
208 {
209 wxLogError(_T("Failed to create size of class \"%s\""), m_class.c_str());
210 return NULL;
211 }
212
213 wxSize minsize = GetSize(wxT("minsize"));
214 if (!(minsize == wxDefaultSize))
215 sizer->SetMinSize(minsize);
216
217 // save state
218 wxSizer *old_par = m_parentSizer;
219 bool old_ins = m_isInside;
220
221 // set new state
222 m_parentSizer = sizer;
223 m_isInside = true;
224 m_isGBS = (m_class == wxT("wxGridBagSizer"));
225
226 CreateChildren(m_parent, true/*only this handler*/);
227
228 // restore state
229 m_isInside = old_ins;
230 m_parentSizer = old_par;
231
232 if (m_parentSizer == NULL) // setup window:
233 {
234 m_parentAsWindow->SetSizer(sizer);
235
236 wxXmlNode *nd = m_node;
237 m_node = parentNode;
238 if (GetSize() == wxDefaultSize)
239 sizer->Fit(m_parentAsWindow);
240 m_node = nd;
241
242 if (m_parentAsWindow->GetWindowStyle() & (wxMAXIMIZE_BOX | wxRESIZE_BORDER))
243 sizer->SetSizeHints(m_parentAsWindow);
244 }
245
246 return sizer;
247 }
248
249
250 wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
251 {
252 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
253 }
254
255 wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
256 {
257 return new wxStaticBoxSizer(
258 new wxStaticBox(m_parentAsWindow,
259 GetID(),
260 GetText(wxT("label")),
261 wxDefaultPosition, wxDefaultSize,
262 0/*style*/,
263 GetName()),
264 GetStyle(wxT("orient"), wxHORIZONTAL));
265 }
266
267 wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
268 {
269 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
270 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
271 }
272
273
274 wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
275 {
276 wxFlexGridSizer *sizer =
277 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
278 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 wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
286 {
287 wxGridBagSizer *sizer =
288 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
289 SetGrowables(sizer, wxT("growablerows"), true);
290 SetGrowables(sizer, wxT("growablecols"), false);
291 return sizer;
292 }
293
294
295
296
297 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
298 const wxChar* param,
299 bool rows)
300 {
301 wxStringTokenizer tkn;
302 unsigned long l;
303 tkn.SetString(GetParamValue(param), wxT(","));
304 while (tkn.HasMoreTokens())
305 {
306 if (!tkn.GetNextToken().ToULong(&l))
307 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
308 else {
309 if (rows)
310 sizer->AddGrowableRow(l);
311 else
312 sizer->AddGrowableCol(l);
313 }
314 }
315 }
316
317
318 wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
319 {
320 wxSize sz = GetSize(param);
321 if (sz.x < 0) sz.x = 0;
322 if (sz.y < 0) sz.y = 0;
323 return wxGBPosition(sz.x, sz.y);
324 }
325
326 wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
327 {
328 wxSize sz = GetSize(param);
329 if (sz.x < 1) sz.x = 1;
330 if (sz.y < 1) sz.y = 1;
331 return wxGBSpan(sz.x, sz.y);
332 }
333
334
335
336 wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
337 {
338 if (m_isGBS)
339 return new wxGBSizerItem();
340 else
341 return new wxSizerItem();
342 }
343
344 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
345 {
346 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
347 sitem->SetFlag(GetStyle(wxT("flag")));
348 sitem->SetBorder(GetDimension(wxT("border")));
349 wxSize sz = GetSize(wxT("minsize"));
350 if (!(sz == wxDefaultSize))
351 sitem->SetMinSize(sz);
352 sz = GetSize(wxT("ratio"));
353 if (!(sz == wxDefaultSize))
354 sitem->SetRatio(sz);
355
356 if (m_isGBS)
357 {
358 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
359 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
360 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
361 }
362 }
363
364 void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
365 {
366 if (m_isGBS)
367 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
368 else
369 m_parentSizer->Add(sitem);
370 }
371
372
373
374 //-----------------------------------------------------------------------------
375 // wxStdDialogButtonSizerXmlHandler
376 //-----------------------------------------------------------------------------
377
378 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler)
379
380 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
381 : m_isInside(false), m_parentSizer(NULL)
382 {
383 }
384
385 wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource()
386 {
387 if (m_class == wxT("wxStdDialogButtonSizer"))
388 {
389 wxASSERT( !m_parentSizer );
390
391 wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer;
392 m_isInside = true;
393
394 CreateChildren(m_parent, true/*only this handler*/);
395
396 m_parentSizer->Realize();
397
398 m_isInside = false;
399 m_parentSizer = NULL;
400
401 return s;
402 }
403 else // m_class == "button"
404 {
405 wxASSERT( m_parentSizer );
406
407 // find the item to be managed by this sizeritem
408 wxXmlNode *n = GetParamNode(wxT("object"));
409 if ( !n )
410 n = GetParamNode(wxT("object_ref"));
411
412 // did we find one?
413 if (n)
414 {
415 wxObject *item = CreateResFromNode(n, m_parent, NULL);
416 wxButton *button = wxDynamicCast(item, wxButton);
417
418 if (button)
419 m_parentSizer->AddButton(button);
420 else
421 wxLogError(wxT("Error in resource - expected button."));
422
423 return item;
424 }
425 else /*n == NULL*/
426 {
427 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
428 return NULL;
429 }
430 }
431 }
432
433 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node)
434 {
435 return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) ||
436 (m_isInside && IsOfClass(node, wxT("button")));
437 }
438
439 #endif // wxUSE_XRC