]>
Commit | Line | Data |
---|---|---|
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 | ||
43 | IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler) | |
44 | ||
45 | wxSizerXmlHandler::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 | ||
96 | bool 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 | ||
105 | wxObject* 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 | wxSizer* wxSizerXmlHandler::DoCreateSizer(const wxString& name) | |
119 | { | |
120 | if (name == wxT("wxBoxSizer")) | |
121 | return Handle_wxBoxSizer(); | |
122 | #if wxUSE_STATBOX | |
123 | else if (name == wxT("wxStaticBoxSizer")) | |
124 | return Handle_wxStaticBoxSizer(); | |
125 | #endif | |
126 | else if (name == wxT("wxGridSizer")) | |
127 | { | |
128 | if ( !ValidateGridSizerChildren() ) | |
129 | return NULL; | |
130 | return Handle_wxGridSizer(); | |
131 | } | |
132 | else if (name == wxT("wxFlexGridSizer")) | |
133 | { | |
134 | return Handle_wxFlexGridSizer(); | |
135 | } | |
136 | else if (name == wxT("wxGridBagSizer")) | |
137 | { | |
138 | return Handle_wxGridBagSizer(); | |
139 | } | |
140 | else if (name == wxT("wxWrapSizer")) | |
141 | { | |
142 | return Handle_wxWrapSizer(); | |
143 | } | |
144 | ||
145 | ReportError(wxString::Format("unknown sizer class \"%s\"", name)); | |
146 | return NULL; | |
147 | } | |
148 | ||
149 | ||
150 | ||
151 | bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node) const | |
152 | { | |
153 | return (IsOfClass(node, wxT("wxBoxSizer"))) || | |
154 | (IsOfClass(node, wxT("wxStaticBoxSizer"))) || | |
155 | (IsOfClass(node, wxT("wxGridSizer"))) || | |
156 | (IsOfClass(node, wxT("wxFlexGridSizer"))) || | |
157 | (IsOfClass(node, wxT("wxGridBagSizer"))) || | |
158 | (IsOfClass(node, wxT("wxWrapSizer"))); | |
159 | } | |
160 | ||
161 | ||
162 | wxObject* wxSizerXmlHandler::Handle_sizeritem() | |
163 | { | |
164 | // find the item to be managed by this sizeritem | |
165 | wxXmlNode *n = GetParamNode(wxT("object")); | |
166 | if ( !n ) | |
167 | n = GetParamNode(wxT("object_ref")); | |
168 | ||
169 | // did we find one? | |
170 | if (n) | |
171 | { | |
172 | // create a sizer item for it | |
173 | wxSizerItem* sitem = MakeSizerItem(); | |
174 | ||
175 | // now fetch the item to be managed | |
176 | bool old_gbs = m_isGBS; | |
177 | bool old_ins = m_isInside; | |
178 | wxSizer *old_par = m_parentSizer; | |
179 | m_isInside = false; | |
180 | if (!IsSizerNode(n)) m_parentSizer = NULL; | |
181 | wxObject *item = CreateResFromNode(n, m_parent, NULL); | |
182 | m_isInside = old_ins; | |
183 | m_parentSizer = old_par; | |
184 | m_isGBS = old_gbs; | |
185 | ||
186 | // and figure out what type it is | |
187 | wxSizer *sizer = wxDynamicCast(item, wxSizer); | |
188 | wxWindow *wnd = wxDynamicCast(item, wxWindow); | |
189 | ||
190 | if (sizer) | |
191 | sitem->AssignSizer(sizer); | |
192 | else if (wnd) | |
193 | sitem->AssignWindow(wnd); | |
194 | else | |
195 | ReportError(n, "unexpected item in sizer"); | |
196 | ||
197 | // finally, set other wxSizerItem attributes | |
198 | SetSizerItemAttributes(sitem); | |
199 | ||
200 | AddSizerItem(sitem); | |
201 | return item; | |
202 | } | |
203 | else /*n == NULL*/ | |
204 | { | |
205 | ReportError("no window/sizer/spacer within sizeritem object"); | |
206 | return NULL; | |
207 | } | |
208 | } | |
209 | ||
210 | ||
211 | wxObject* wxSizerXmlHandler::Handle_spacer() | |
212 | { | |
213 | if ( !m_parentSizer ) | |
214 | { | |
215 | ReportError("spacer only allowed inside a sizer"); | |
216 | return NULL; | |
217 | } | |
218 | ||
219 | wxSizerItem* sitem = MakeSizerItem(); | |
220 | SetSizerItemAttributes(sitem); | |
221 | sitem->AssignSpacer(GetSize()); | |
222 | AddSizerItem(sitem); | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | ||
227 | wxObject* wxSizerXmlHandler::Handle_sizer() | |
228 | { | |
229 | wxXmlNode *parentNode = m_node->GetParent(); | |
230 | ||
231 | if ( !m_parentSizer && | |
232 | (!parentNode || parentNode->GetType() != wxXML_ELEMENT_NODE || | |
233 | !m_parentAsWindow) ) | |
234 | { | |
235 | ReportError("sizer must have a window parent"); | |
236 | return NULL; | |
237 | } | |
238 | ||
239 | // Create the sizer of the appropriate class. | |
240 | wxSizer * const sizer = DoCreateSizer(m_class); | |
241 | ||
242 | // creation of sizer failed for some (already reported) reason, so exit: | |
243 | if ( !sizer ) | |
244 | return NULL; | |
245 | ||
246 | wxSize minsize = GetSize(wxT("minsize")); | |
247 | if (!(minsize == wxDefaultSize)) | |
248 | sizer->SetMinSize(minsize); | |
249 | ||
250 | // save state | |
251 | wxSizer *old_par = m_parentSizer; | |
252 | bool old_ins = m_isInside; | |
253 | ||
254 | // set new state | |
255 | m_parentSizer = sizer; | |
256 | m_isInside = true; | |
257 | m_isGBS = (m_class == wxT("wxGridBagSizer")); | |
258 | ||
259 | CreateChildren(m_parent, true/*only this handler*/); | |
260 | ||
261 | // set growable rows and cols for sizers which support this | |
262 | if ( wxFlexGridSizer *flexsizer = wxDynamicCast(sizer, wxFlexGridSizer) ) | |
263 | { | |
264 | SetGrowables(flexsizer, wxT("growablerows"), true); | |
265 | SetGrowables(flexsizer, wxT("growablecols"), false); | |
266 | } | |
267 | ||
268 | // restore state | |
269 | m_isInside = old_ins; | |
270 | m_parentSizer = old_par; | |
271 | ||
272 | if (m_parentSizer == NULL) // setup window: | |
273 | { | |
274 | m_parentAsWindow->SetSizer(sizer); | |
275 | ||
276 | wxXmlNode *nd = m_node; | |
277 | m_node = parentNode; | |
278 | if (GetSize() == wxDefaultSize) | |
279 | { | |
280 | if ( wxDynamicCast(m_parentAsWindow, wxScrolledWindow) != NULL ) | |
281 | { | |
282 | sizer->FitInside(m_parentAsWindow); | |
283 | } | |
284 | else | |
285 | { | |
286 | sizer->Fit(m_parentAsWindow); | |
287 | } | |
288 | } | |
289 | m_node = nd; | |
290 | ||
291 | if (m_parentAsWindow->IsTopLevel()) | |
292 | { | |
293 | sizer->SetSizeHints(m_parentAsWindow); | |
294 | } | |
295 | } | |
296 | ||
297 | return sizer; | |
298 | } | |
299 | ||
300 | ||
301 | wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer() | |
302 | { | |
303 | return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL)); | |
304 | } | |
305 | ||
306 | #if wxUSE_STATBOX | |
307 | wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer() | |
308 | { | |
309 | return new wxStaticBoxSizer( | |
310 | new wxStaticBox(m_parentAsWindow, | |
311 | GetID(), | |
312 | GetText(wxT("label")), | |
313 | wxDefaultPosition, wxDefaultSize, | |
314 | 0/*style*/, | |
315 | GetName()), | |
316 | GetStyle(wxT("orient"), wxHORIZONTAL)); | |
317 | } | |
318 | #endif // wxUSE_STATBOX | |
319 | ||
320 | wxSizer* wxSizerXmlHandler::Handle_wxGridSizer() | |
321 | { | |
322 | return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")), | |
323 | GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); | |
324 | } | |
325 | ||
326 | ||
327 | wxFlexGridSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer() | |
328 | { | |
329 | if ( !ValidateGridSizerChildren() ) | |
330 | return NULL; | |
331 | return new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")), | |
332 | GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); | |
333 | } | |
334 | ||
335 | ||
336 | wxGridBagSizer* wxSizerXmlHandler::Handle_wxGridBagSizer() | |
337 | { | |
338 | if ( !ValidateGridSizerChildren() ) | |
339 | return NULL; | |
340 | return new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); | |
341 | } | |
342 | ||
343 | wxSizer* wxSizerXmlHandler::Handle_wxWrapSizer() | |
344 | { | |
345 | wxWrapSizer *sizer = new wxWrapSizer(GetStyle("orient"), GetStyle("flag")); | |
346 | return sizer; | |
347 | } | |
348 | ||
349 | ||
350 | bool wxSizerXmlHandler::ValidateGridSizerChildren() | |
351 | { | |
352 | int rows = GetLong("rows"); | |
353 | int cols = GetLong("cols"); | |
354 | ||
355 | if ( rows && cols ) | |
356 | { | |
357 | // fixed number of cells, need to verify children count | |
358 | int children = 0; | |
359 | for ( wxXmlNode *n = m_node->GetChildren(); n; n = n->GetNext() ) | |
360 | { | |
361 | if ( n->GetType() == wxXML_ELEMENT_NODE && | |
362 | (n->GetName() == "object" || n->GetName() == "object_ref") ) | |
363 | { | |
364 | children++; | |
365 | } | |
366 | } | |
367 | ||
368 | if ( children > rows * cols ) | |
369 | { | |
370 | ReportError | |
371 | ( | |
372 | wxString::Format | |
373 | ( | |
374 | "too many children in grid sizer: %d > %d x %d" | |
375 | " (consider omitting the number of rows or columns)", | |
376 | children, | |
377 | cols, | |
378 | rows | |
379 | ) | |
380 | ); | |
381 | return false; | |
382 | } | |
383 | } | |
384 | ||
385 | return true; | |
386 | } | |
387 | ||
388 | ||
389 | void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer, | |
390 | const wxChar* param, | |
391 | bool rows) | |
392 | { | |
393 | int nrows, ncols; | |
394 | sizer->CalcRowsCols(nrows, ncols); | |
395 | const int nslots = rows ? nrows : ncols; | |
396 | ||
397 | wxStringTokenizer tkn; | |
398 | tkn.SetString(GetParamValue(param), wxT(",")); | |
399 | ||
400 | while (tkn.HasMoreTokens()) | |
401 | { | |
402 | unsigned long l; | |
403 | if (!tkn.GetNextToken().ToULong(&l)) | |
404 | { | |
405 | ReportParamError | |
406 | ( | |
407 | param, | |
408 | "value must be comma-separated list of row numbers" | |
409 | ); | |
410 | break; | |
411 | } | |
412 | ||
413 | if ( (int)l >= nslots ) | |
414 | { | |
415 | ReportParamError | |
416 | ( | |
417 | param, | |
418 | wxString::Format | |
419 | ( | |
420 | "invalid %s index %d: must be less than %d", | |
421 | rows ? "row" : "column", | |
422 | l, | |
423 | nslots | |
424 | ) | |
425 | ); | |
426 | ||
427 | // ignore incorrect value, still try to process the rest | |
428 | continue; | |
429 | } | |
430 | ||
431 | if (rows) | |
432 | sizer->AddGrowableRow(l); | |
433 | else | |
434 | sizer->AddGrowableCol(l); | |
435 | } | |
436 | } | |
437 | ||
438 | ||
439 | wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param) | |
440 | { | |
441 | wxSize sz = GetSize(param); | |
442 | if (sz.x < 0) sz.x = 0; | |
443 | if (sz.y < 0) sz.y = 0; | |
444 | return wxGBPosition(sz.x, sz.y); | |
445 | } | |
446 | ||
447 | wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param) | |
448 | { | |
449 | wxSize sz = GetSize(param); | |
450 | if (sz.x < 1) sz.x = 1; | |
451 | if (sz.y < 1) sz.y = 1; | |
452 | return wxGBSpan(sz.x, sz.y); | |
453 | } | |
454 | ||
455 | ||
456 | ||
457 | wxSizerItem* wxSizerXmlHandler::MakeSizerItem() | |
458 | { | |
459 | if (m_isGBS) | |
460 | return new wxGBSizerItem(); | |
461 | else | |
462 | return new wxSizerItem(); | |
463 | } | |
464 | ||
465 | void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem) | |
466 | { | |
467 | sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too? | |
468 | sitem->SetFlag(GetStyle(wxT("flag"))); | |
469 | sitem->SetBorder(GetDimension(wxT("border"))); | |
470 | wxSize sz = GetSize(wxT("minsize")); | |
471 | if (!(sz == wxDefaultSize)) | |
472 | sitem->SetMinSize(sz); | |
473 | sz = GetSize(wxT("ratio")); | |
474 | if (!(sz == wxDefaultSize)) | |
475 | sitem->SetRatio(sz); | |
476 | ||
477 | if (m_isGBS) | |
478 | { | |
479 | wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem; | |
480 | gbsitem->SetPos(GetGBPos(wxT("cellpos"))); | |
481 | gbsitem->SetSpan(GetGBSpan(wxT("cellspan"))); | |
482 | } | |
483 | ||
484 | // record the id of the item, if any, for use by XRCSIZERITEM() | |
485 | sitem->SetId(GetID()); | |
486 | } | |
487 | ||
488 | void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem) | |
489 | { | |
490 | if (m_isGBS) | |
491 | ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem); | |
492 | else | |
493 | m_parentSizer->Add(sitem); | |
494 | } | |
495 | ||
496 | ||
497 | ||
498 | //----------------------------------------------------------------------------- | |
499 | // wxStdDialogButtonSizerXmlHandler | |
500 | //----------------------------------------------------------------------------- | |
501 | #if wxUSE_BUTTON | |
502 | ||
503 | IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler) | |
504 | ||
505 | wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler() | |
506 | : m_isInside(false), m_parentSizer(NULL) | |
507 | { | |
508 | } | |
509 | ||
510 | wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource() | |
511 | { | |
512 | if (m_class == wxT("wxStdDialogButtonSizer")) | |
513 | { | |
514 | wxASSERT( !m_parentSizer ); | |
515 | ||
516 | wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer; | |
517 | m_isInside = true; | |
518 | ||
519 | CreateChildren(m_parent, true/*only this handler*/); | |
520 | ||
521 | m_parentSizer->Realize(); | |
522 | ||
523 | m_isInside = false; | |
524 | m_parentSizer = NULL; | |
525 | ||
526 | return s; | |
527 | } | |
528 | else // m_class == "button" | |
529 | { | |
530 | wxASSERT( m_parentSizer ); | |
531 | ||
532 | // find the item to be managed by this sizeritem | |
533 | wxXmlNode *n = GetParamNode(wxT("object")); | |
534 | if ( !n ) | |
535 | n = GetParamNode(wxT("object_ref")); | |
536 | ||
537 | // did we find one? | |
538 | if (n) | |
539 | { | |
540 | wxObject *item = CreateResFromNode(n, m_parent, NULL); | |
541 | wxButton *button = wxDynamicCast(item, wxButton); | |
542 | ||
543 | if (button) | |
544 | m_parentSizer->AddButton(button); | |
545 | else | |
546 | ReportError(n, "expected wxButton"); | |
547 | ||
548 | return item; | |
549 | } | |
550 | else /*n == NULL*/ | |
551 | { | |
552 | ReportError("no button within wxStdDialogButtonSizer"); | |
553 | return NULL; | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
558 | bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node) | |
559 | { | |
560 | return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) || | |
561 | (m_isInside && IsOfClass(node, wxT("button"))); | |
562 | } | |
563 | #endif // wxUSE_BUTTON | |
564 | ||
565 | #endif // wxUSE_XRC |