]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
no changes, just de TABified
[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 #include "wx/frame.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
30 #endif
31
32 #include "wx/gbsizer.h"
33 #include "wx/notebook.h"
34 #include "wx/tokenzr.h"
35
36
37 //-----------------------------------------------------------------------------
38 // wxSizerXmlHandler
39 //-----------------------------------------------------------------------------
40
41 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
42
43 wxSizerXmlHandler::wxSizerXmlHandler()
44 :wxXmlResourceHandler(),
45 m_isInside(false),
46 m_isGBS(false),
47 m_parentSizer(NULL)
48 {
49 XRC_ADD_STYLE(wxHORIZONTAL);
50 XRC_ADD_STYLE(wxVERTICAL);
51
52 // and flags
53 XRC_ADD_STYLE(wxLEFT);
54 XRC_ADD_STYLE(wxRIGHT);
55 XRC_ADD_STYLE(wxTOP);
56 XRC_ADD_STYLE(wxBOTTOM);
57 XRC_ADD_STYLE(wxNORTH);
58 XRC_ADD_STYLE(wxSOUTH);
59 XRC_ADD_STYLE(wxEAST);
60 XRC_ADD_STYLE(wxWEST);
61 XRC_ADD_STYLE(wxALL);
62
63 XRC_ADD_STYLE(wxGROW);
64 XRC_ADD_STYLE(wxEXPAND);
65 XRC_ADD_STYLE(wxSHAPED);
66 XRC_ADD_STYLE(wxSTRETCH_NOT);
67
68 XRC_ADD_STYLE(wxALIGN_CENTER);
69 XRC_ADD_STYLE(wxALIGN_CENTRE);
70 XRC_ADD_STYLE(wxALIGN_LEFT);
71 XRC_ADD_STYLE(wxALIGN_TOP);
72 XRC_ADD_STYLE(wxALIGN_RIGHT);
73 XRC_ADD_STYLE(wxALIGN_BOTTOM);
74 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
75 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
76 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
77 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
78
79 XRC_ADD_STYLE(wxADJUST_MINSIZE);
80 XRC_ADD_STYLE(wxFIXED_MINSIZE);
81 }
82
83
84
85 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
86 {
87 return ( (!m_isInside && IsSizerNode(node)) ||
88 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
89 (m_isInside && IsOfClass(node, wxT("spacer")))
90 );
91 }
92
93
94 wxObject* wxSizerXmlHandler::DoCreateResource()
95 {
96 if (m_class == wxT("sizeritem"))
97 return Handle_sizeritem();
98
99 else if (m_class == wxT("spacer"))
100 return Handle_spacer();
101
102 else
103 return Handle_sizer();
104 }
105
106
107
108
109 bool 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
119 wxObject* 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();
131
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;
136 m_isInside = false;
137 if (!IsSizerNode(n)) m_parentSizer = NULL;
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);
146
147 if (sizer)
148 sitem->SetSizer(sizer);
149 else if (wnd)
150 sitem->SetWindow(wnd);
151 else
152 wxLogError(wxT("Error in resource."));
153
154 // finally, set other wxSizerItem attributes
155 SetSizerItemAttributes(sitem);
156
157 AddSizerItem(sitem);
158 return item;
159 }
160 else /*n == NULL*/
161 {
162 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
163 return NULL;
164 }
165 }
166
167
168 wxObject* 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);
174 sitem->SetSpacer(GetSize());
175 AddSizerItem(sitem);
176 return NULL;
177 }
178
179
180 wxObject* wxSizerXmlHandler::Handle_sizer()
181 {
182 wxSizer *sizer = NULL;
183
184 wxXmlNode *parentNode = m_node->GetParent();
185
186 wxCHECK_MSG(m_parentSizer != NULL ||
187 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
188 m_parentAsWindow), NULL,
189 wxT("Sizer must have a window parent node"));
190
191 if (m_class == wxT("wxBoxSizer"))
192 sizer = Handle_wxBoxSizer();
193
194 else if (m_class == wxT("wxStaticBoxSizer"))
195 sizer = Handle_wxStaticBoxSizer();
196
197 else if (m_class == wxT("wxGridSizer"))
198 sizer = Handle_wxGridSizer();
199
200 else if (m_class == wxT("wxFlexGridSizer"))
201 sizer = Handle_wxFlexGridSizer();
202
203 else if (m_class == wxT("wxGridBagSizer"))
204 sizer = Handle_wxGridBagSizer();
205
206 if ( !sizer )
207 {
208 wxLogError(_T("Failed to create size of class \"%s\""), m_class.c_str());
209 return NULL;
210 }
211
212 wxSize minsize = GetSize(wxT("minsize"));
213 if (!(minsize == wxDefaultSize))
214 sizer->SetMinSize(minsize);
215
216 // save state
217 wxSizer *old_par = m_parentSizer;
218 bool old_ins = m_isInside;
219
220 // set new state
221 m_parentSizer = sizer;
222 m_isInside = true;
223 m_isGBS = (m_class == wxT("wxGridBagSizer"));
224
225 CreateChildren(m_parent, true/*only this handler*/);
226
227 // restore state
228 m_isInside = old_ins;
229 m_parentSizer = old_par;
230
231 if (m_parentSizer == NULL) // setup window:
232 {
233 m_parentAsWindow->SetSizer(sizer);
234
235 wxXmlNode *nd = m_node;
236 m_node = parentNode;
237 if (GetSize() == wxDefaultSize)
238 sizer->Fit(m_parentAsWindow);
239 m_node = nd;
240
241 if (m_parentAsWindow->GetWindowStyle() & (wxMAXIMIZE_BOX | wxRESIZE_BORDER))
242 sizer->SetSizeHints(m_parentAsWindow);
243 }
244
245 return sizer;
246 }
247
248
249 wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
250 {
251 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
252 }
253
254 wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
255 {
256 return new wxStaticBoxSizer(
257 new wxStaticBox(m_parentAsWindow,
258 GetID(),
259 GetText(wxT("label")),
260 wxDefaultPosition, wxDefaultSize,
261 0/*style*/,
262 GetName()),
263 GetStyle(wxT("orient"), wxHORIZONTAL));
264 }
265
266 wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
267 {
268 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
269 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
270 }
271
272
273 wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
274 {
275 wxFlexGridSizer *sizer =
276 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
277 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
278 SetGrowables(sizer, wxT("growablerows"), true);
279 SetGrowables(sizer, wxT("growablecols"), false);
280 return sizer;
281 }
282
283
284 wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
285 {
286 wxGridBagSizer *sizer =
287 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
288 SetGrowables(sizer, wxT("growablerows"), true);
289 SetGrowables(sizer, wxT("growablecols"), false);
290 return sizer;
291 }
292
293
294
295
296 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
297 const wxChar* param,
298 bool rows)
299 {
300 wxStringTokenizer tkn;
301 unsigned long l;
302 tkn.SetString(GetParamValue(param), wxT(","));
303 while (tkn.HasMoreTokens())
304 {
305 if (!tkn.GetNextToken().ToULong(&l))
306 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
307 else {
308 if (rows)
309 sizer->AddGrowableRow(l);
310 else
311 sizer->AddGrowableCol(l);
312 }
313 }
314 }
315
316
317 wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
318 {
319 wxSize sz = GetSize(param);
320 if (sz.x < 0) sz.x = 0;
321 if (sz.y < 0) sz.y = 0;
322 return wxGBPosition(sz.x, sz.y);
323 }
324
325 wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
326 {
327 wxSize sz = GetSize(param);
328 if (sz.x < 1) sz.x = 1;
329 if (sz.y < 1) sz.y = 1;
330 return wxGBSpan(sz.x, sz.y);
331 }
332
333
334
335 wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
336 {
337 if (m_isGBS)
338 return new wxGBSizerItem();
339 else
340 return new wxSizerItem();
341 }
342
343 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
344 {
345 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
346 sitem->SetFlag(GetStyle(wxT("flag")));
347 sitem->SetBorder(GetDimension(wxT("border")));
348 wxSize sz = GetSize(wxT("minsize"));
349 if (!(sz == wxDefaultSize))
350 sitem->SetMinSize(sz);
351 sz = GetSize(wxT("ratio"));
352 if (!(sz == wxDefaultSize))
353 sitem->SetRatio(sz);
354
355 if (m_isGBS)
356 {
357 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
358 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
359 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
360 }
361 }
362
363 void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
364 {
365 if (m_isGBS)
366 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
367 else
368 m_parentSizer->Add(sitem);
369 }
370
371
372
373 //-----------------------------------------------------------------------------
374 // wxStdDialogButtonSizerXmlHandler
375 //-----------------------------------------------------------------------------
376 #if wxUSE_BUTTON
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 #endif // wxUSE_BUTTON
439
440 #endif // wxUSE_XRC