]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
implemented wxMemoryInputStream::CanRead() and added tests for CanRead() to all strea...
[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(wxFIXED_MINSIZE);
80 }
81
82
83
84 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
85 {
86 return ( (!m_isInside && IsSizerNode(node)) ||
87 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
88 (m_isInside && IsOfClass(node, wxT("spacer")))
89 );
90 }
91
92
93 wxObject* wxSizerXmlHandler::DoCreateResource()
94 {
95 if (m_class == wxT("sizeritem"))
96 return Handle_sizeritem();
97
98 else if (m_class == wxT("spacer"))
99 return Handle_spacer();
100
101 else
102 return Handle_sizer();
103 }
104
105
106
107
108 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
109 {
110 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
111 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
112 (IsOfClass(node, wxT("wxGridSizer"))) ||
113 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
114 (IsOfClass(node, wxT("wxGridBagSizer")));
115 }
116
117
118 wxObject* wxSizerXmlHandler::Handle_sizeritem()
119 {
120 // find the item to be managed by this sizeritem
121 wxXmlNode *n = GetParamNode(wxT("object"));
122 if ( !n )
123 n = GetParamNode(wxT("object_ref"));
124
125 // did we find one?
126 if (n)
127 {
128 // create a sizer item for it
129 wxSizerItem* sitem = MakeSizerItem();
130
131 // now fetch the item to be managed
132 bool old_gbs = m_isGBS;
133 bool old_ins = m_isInside;
134 wxSizer *old_par = m_parentSizer;
135 m_isInside = false;
136 if (!IsSizerNode(n)) m_parentSizer = NULL;
137 wxObject *item = CreateResFromNode(n, m_parent, NULL);
138 m_isInside = old_ins;
139 m_parentSizer = old_par;
140 m_isGBS = old_gbs;
141
142 // and figure out what type it is
143 wxSizer *sizer = wxDynamicCast(item, wxSizer);
144 wxWindow *wnd = wxDynamicCast(item, wxWindow);
145
146 if (sizer)
147 sitem->SetSizer(sizer);
148 else if (wnd)
149 sitem->SetWindow(wnd);
150 else
151 wxLogError(wxT("Error in resource."));
152
153 // finally, set other wxSizerItem attributes
154 SetSizerItemAttributes(sitem);
155
156 AddSizerItem(sitem);
157 return item;
158 }
159 else /*n == NULL*/
160 {
161 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
162 return NULL;
163 }
164 }
165
166
167 wxObject* wxSizerXmlHandler::Handle_spacer()
168 {
169 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
170
171 wxSizerItem* sitem = MakeSizerItem();
172 SetSizerItemAttributes(sitem);
173 sitem->SetSpacer(GetSize());
174 AddSizerItem(sitem);
175 return NULL;
176 }
177
178
179 wxObject* wxSizerXmlHandler::Handle_sizer()
180 {
181 wxSizer *sizer = NULL;
182
183 wxXmlNode *parentNode = m_node->GetParent();
184
185 wxCHECK_MSG(m_parentSizer != NULL ||
186 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
187 m_parentAsWindow), NULL,
188 wxT("Sizer must have a window parent node"));
189
190 if (m_class == wxT("wxBoxSizer"))
191 sizer = Handle_wxBoxSizer();
192
193 #if wxUSE_STATBOX
194 else if (m_class == wxT("wxStaticBoxSizer"))
195 sizer = Handle_wxStaticBoxSizer();
196 #endif
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 #if wxUSE_STATBOX
256 wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
257 {
258 return new wxStaticBoxSizer(
259 new wxStaticBox(m_parentAsWindow,
260 GetID(),
261 GetText(wxT("label")),
262 wxDefaultPosition, wxDefaultSize,
263 0/*style*/,
264 GetName()),
265 GetStyle(wxT("orient"), wxHORIZONTAL));
266 }
267 #endif // wxUSE_STATBOX
268
269 wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
270 {
271 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
272 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
273 }
274
275
276 wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
277 {
278 wxFlexGridSizer *sizer =
279 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
280 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
281 SetGrowables(sizer, wxT("growablerows"), true);
282 SetGrowables(sizer, wxT("growablecols"), false);
283 return sizer;
284 }
285
286
287 wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
288 {
289 wxGridBagSizer *sizer =
290 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
291 SetGrowables(sizer, wxT("growablerows"), true);
292 SetGrowables(sizer, wxT("growablecols"), false);
293 return sizer;
294 }
295
296
297
298
299 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
300 const wxChar* param,
301 bool rows)
302 {
303 wxStringTokenizer tkn;
304 unsigned long l;
305 tkn.SetString(GetParamValue(param), wxT(","));
306 while (tkn.HasMoreTokens())
307 {
308 if (!tkn.GetNextToken().ToULong(&l))
309 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
310 else {
311 if (rows)
312 sizer->AddGrowableRow(l);
313 else
314 sizer->AddGrowableCol(l);
315 }
316 }
317 }
318
319
320 wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
321 {
322 wxSize sz = GetSize(param);
323 if (sz.x < 0) sz.x = 0;
324 if (sz.y < 0) sz.y = 0;
325 return wxGBPosition(sz.x, sz.y);
326 }
327
328 wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
329 {
330 wxSize sz = GetSize(param);
331 if (sz.x < 1) sz.x = 1;
332 if (sz.y < 1) sz.y = 1;
333 return wxGBSpan(sz.x, sz.y);
334 }
335
336
337
338 wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
339 {
340 if (m_isGBS)
341 return new wxGBSizerItem();
342 else
343 return new wxSizerItem();
344 }
345
346 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
347 {
348 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
349 sitem->SetFlag(GetStyle(wxT("flag")));
350 sitem->SetBorder(GetDimension(wxT("border")));
351 wxSize sz = GetSize(wxT("minsize"));
352 if (!(sz == wxDefaultSize))
353 sitem->SetMinSize(sz);
354 sz = GetSize(wxT("ratio"));
355 if (!(sz == wxDefaultSize))
356 sitem->SetRatio(sz);
357
358 if (m_isGBS)
359 {
360 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
361 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
362 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
363 }
364 }
365
366 void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
367 {
368 if (m_isGBS)
369 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
370 else
371 m_parentSizer->Add(sitem);
372 }
373
374
375
376 //-----------------------------------------------------------------------------
377 // wxStdDialogButtonSizerXmlHandler
378 //-----------------------------------------------------------------------------
379 #if wxUSE_BUTTON
380
381 IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler)
382
383 wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler()
384 : m_isInside(false), m_parentSizer(NULL)
385 {
386 }
387
388 wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource()
389 {
390 if (m_class == wxT("wxStdDialogButtonSizer"))
391 {
392 wxASSERT( !m_parentSizer );
393
394 wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer;
395 m_isInside = true;
396
397 CreateChildren(m_parent, true/*only this handler*/);
398
399 m_parentSizer->Realize();
400
401 m_isInside = false;
402 m_parentSizer = NULL;
403
404 return s;
405 }
406 else // m_class == "button"
407 {
408 wxASSERT( m_parentSizer );
409
410 // find the item to be managed by this sizeritem
411 wxXmlNode *n = GetParamNode(wxT("object"));
412 if ( !n )
413 n = GetParamNode(wxT("object_ref"));
414
415 // did we find one?
416 if (n)
417 {
418 wxObject *item = CreateResFromNode(n, m_parent, NULL);
419 wxButton *button = wxDynamicCast(item, wxButton);
420
421 if (button)
422 m_parentSizer->AddButton(button);
423 else
424 wxLogError(wxT("Error in resource - expected button."));
425
426 return item;
427 }
428 else /*n == NULL*/
429 {
430 wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer."));
431 return NULL;
432 }
433 }
434 }
435
436 bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node)
437 {
438 return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) ||
439 (m_isInside && IsOfClass(node, wxT("button")));
440 }
441 #endif // wxUSE_BUTTON
442
443 #endif // wxUSE_XRC