]>
Commit | Line | Data |
---|---|---|
78d14f80 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1c067fe3 | 2 | // Name: src/xrc/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 | |
78d14f80 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
621be1ec | 18 | #if wxUSE_XRC |
a1e4ec87 | 19 | |
78d14f80 | 20 | #include "wx/xrc/xh_sizer.h" |
e4db172a WS |
21 | |
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/log.h" | |
8e609c82 | 24 | #include "wx/panel.h" |
876cd6f7 | 25 | #include "wx/statbox.h" |
ed2fbeb8 | 26 | #include "wx/sizer.h" |
4107600f MW |
27 | #include "wx/frame.h" |
28 | #include "wx/dialog.h" | |
29 | #include "wx/button.h" | |
5e1a9f05 | 30 | #include "wx/scrolwin.h" |
e4db172a WS |
31 | #endif |
32 | ||
0d077b41 | 33 | #include "wx/gbsizer.h" |
13a38887 | 34 | #include "wx/wrapsizer.h" |
78d14f80 VS |
35 | #include "wx/notebook.h" |
36 | #include "wx/tokenzr.h" | |
37 | ||
78d14f80 | 38 | |
172541f6 VS |
39 | //----------------------------------------------------------------------------- |
40 | // wxSizerXmlHandler | |
41 | //----------------------------------------------------------------------------- | |
78d14f80 | 42 | |
0d077b41 | 43 | IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler) |
78d14f80 | 44 | |
f80ea77b | 45 | wxSizerXmlHandler::wxSizerXmlHandler() |
ed2fbeb8 WS |
46 | :wxXmlResourceHandler(), |
47 | m_isInside(false), | |
48 | m_isGBS(false), | |
49 | m_parentSizer(NULL) | |
78d14f80 | 50 | { |
544fee32 VS |
51 | XRC_ADD_STYLE(wxHORIZONTAL); |
52 | XRC_ADD_STYLE(wxVERTICAL); | |
78d14f80 VS |
53 | |
54 | // and flags | |
544fee32 VS |
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); | |
78d14f80 | 64 | |
544fee32 VS |
65 | XRC_ADD_STYLE(wxGROW); |
66 | XRC_ADD_STYLE(wxEXPAND); | |
67 | XRC_ADD_STYLE(wxSHAPED); | |
68 | XRC_ADD_STYLE(wxSTRETCH_NOT); | |
78d14f80 | 69 | |
544fee32 VS |
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); | |
f80ea77b | 80 | |
c66b63e5 | 81 | XRC_ADD_STYLE(wxFIXED_MINSIZE); |
a575238f | 82 | XRC_ADD_STYLE(wxRESERVE_SPACE_EVEN_IF_HIDDEN); |
63b37a4e | 83 | |
249259e7 VZ |
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 | ||
63b37a4e VZ |
89 | // wxWrapSizer-specific flags |
90 | XRC_ADD_STYLE(wxEXTEND_LAST_ON_EACH_LINE); | |
91 | XRC_ADD_STYLE(wxREMOVE_LEADING_SPACES); | |
78d14f80 VS |
92 | } |
93 | ||
94 | ||
95 | ||
0d077b41 RD |
96 | bool wxSizerXmlHandler::CanHandle(wxXmlNode *node) |
97 | { | |
98 | return ( (!m_isInside && IsSizerNode(node)) || | |
99 | (m_isInside && IsOfClass(node, wxT("sizeritem"))) || | |
f80ea77b | 100 | (m_isInside && IsOfClass(node, wxT("spacer"))) |
0d077b41 RD |
101 | ); |
102 | } | |
103 | ||
f80ea77b | 104 | |
0d077b41 | 105 | wxObject* wxSizerXmlHandler::DoCreateResource() |
f80ea77b | 106 | { |
78d14f80 | 107 | if (m_class == wxT("sizeritem")) |
0d077b41 | 108 | return Handle_sizeritem(); |
f80ea77b | 109 | |
78d14f80 | 110 | else if (m_class == wxT("spacer")) |
0d077b41 RD |
111 | return Handle_spacer(); |
112 | ||
113 | else | |
114 | return Handle_sizer(); | |
115 | } | |
116 | ||
117 | ||
118 | ||
119 | ||
120 | bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node) | |
121 | { | |
122 | return (IsOfClass(node, wxT("wxBoxSizer"))) || | |
123 | (IsOfClass(node, wxT("wxStaticBoxSizer"))) || | |
124 | (IsOfClass(node, wxT("wxGridSizer"))) || | |
125 | (IsOfClass(node, wxT("wxFlexGridSizer"))) || | |
13a38887 VZ |
126 | (IsOfClass(node, wxT("wxGridBagSizer"))) || |
127 | (IsOfClass(node, wxT("wxWrapSizer"))); | |
0d077b41 RD |
128 | } |
129 | ||
130 | ||
131 | wxObject* wxSizerXmlHandler::Handle_sizeritem() | |
132 | { | |
133 | // find the item to be managed by this sizeritem | |
134 | wxXmlNode *n = GetParamNode(wxT("object")); | |
135 | if ( !n ) | |
136 | n = GetParamNode(wxT("object_ref")); | |
137 | ||
138 | // did we find one? | |
139 | if (n) | |
140 | { | |
141 | // create a sizer item for it | |
142 | wxSizerItem* sitem = MakeSizerItem(); | |
f80ea77b | 143 | |
0d077b41 RD |
144 | // now fetch the item to be managed |
145 | bool old_gbs = m_isGBS; | |
146 | bool old_ins = m_isInside; | |
147 | wxSizer *old_par = m_parentSizer; | |
f80ea77b WS |
148 | m_isInside = false; |
149 | if (!IsSizerNode(n)) m_parentSizer = NULL; | |
0d077b41 RD |
150 | wxObject *item = CreateResFromNode(n, m_parent, NULL); |
151 | m_isInside = old_ins; | |
152 | m_parentSizer = old_par; | |
153 | m_isGBS = old_gbs; | |
154 | ||
155 | // and figure out what type it is | |
156 | wxSizer *sizer = wxDynamicCast(item, wxSizer); | |
157 | wxWindow *wnd = wxDynamicCast(item, wxWindow); | |
f80ea77b | 158 | |
0d077b41 | 159 | if (sizer) |
a50cf60e | 160 | sitem->AssignSizer(sizer); |
0d077b41 | 161 | else if (wnd) |
a50cf60e | 162 | sitem->AssignWindow(wnd); |
f80ea77b | 163 | else |
0d077b41 RD |
164 | wxLogError(wxT("Error in resource.")); |
165 | ||
1eba2193 RD |
166 | // finally, set other wxSizerItem attributes |
167 | SetSizerItemAttributes(sitem); | |
168 | ||
0d077b41 RD |
169 | AddSizerItem(sitem); |
170 | return item; | |
171 | } | |
172 | else /*n == NULL*/ | |
78d14f80 | 173 | { |
0d077b41 | 174 | wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object.")); |
78d14f80 VS |
175 | return NULL; |
176 | } | |
0d077b41 RD |
177 | } |
178 | ||
179 | ||
180 | wxObject* wxSizerXmlHandler::Handle_spacer() | |
181 | { | |
07acc3cc VZ |
182 | if ( !m_parentSizer ) |
183 | { | |
184 | wxLogError(_("XRC syntax error: \"spacer\" only allowed inside a " | |
185 | "sizer")); | |
186 | return NULL; | |
187 | } | |
0d077b41 RD |
188 | |
189 | wxSizerItem* sitem = MakeSizerItem(); | |
190 | SetSizerItemAttributes(sitem); | |
a50cf60e | 191 | sitem->AssignSpacer(GetSize()); |
f80ea77b | 192 | AddSizerItem(sitem); |
0d077b41 RD |
193 | return NULL; |
194 | } | |
195 | ||
78d14f80 | 196 | |
0d077b41 RD |
197 | wxObject* wxSizerXmlHandler::Handle_sizer() |
198 | { | |
199 | wxSizer *sizer = NULL; | |
f80ea77b | 200 | |
0d077b41 | 201 | wxXmlNode *parentNode = m_node->GetParent(); |
78d14f80 | 202 | |
07acc3cc VZ |
203 | if ( !m_parentSizer && |
204 | (!parentNode || parentNode->GetType() != wxXML_ELEMENT_NODE || | |
205 | !m_parentAsWindow) ) | |
206 | { | |
207 | wxLogError(_("XRC syntax error: sizer must have a window parent.")); | |
208 | return NULL; | |
209 | } | |
78d14f80 | 210 | |
0d077b41 RD |
211 | if (m_class == wxT("wxBoxSizer")) |
212 | sizer = Handle_wxBoxSizer(); | |
213 | ||
627ddac9 | 214 | #if wxUSE_STATBOX |
0d077b41 RD |
215 | else if (m_class == wxT("wxStaticBoxSizer")) |
216 | sizer = Handle_wxStaticBoxSizer(); | |
627ddac9 | 217 | #endif |
f80ea77b | 218 | |
0d077b41 RD |
219 | else if (m_class == wxT("wxGridSizer")) |
220 | sizer = Handle_wxGridSizer(); | |
f80ea77b | 221 | |
0d077b41 RD |
222 | else if (m_class == wxT("wxFlexGridSizer")) |
223 | sizer = Handle_wxFlexGridSizer(); | |
78d14f80 | 224 | |
0d077b41 RD |
225 | else if (m_class == wxT("wxGridBagSizer")) |
226 | sizer = Handle_wxGridBagSizer(); | |
78d14f80 | 227 | |
13a38887 | 228 | else if (m_class == wxT("wxWrapSizer")) |
63b37a4e | 229 | sizer = Handle_wxWrapSizer(); |
13a38887 | 230 | |
641c5f1a VZ |
231 | if ( !sizer ) |
232 | { | |
233 | wxLogError(_T("Failed to create size of class \"%s\""), m_class.c_str()); | |
234 | return NULL; | |
235 | } | |
f80ea77b | 236 | |
0d077b41 RD |
237 | wxSize minsize = GetSize(wxT("minsize")); |
238 | if (!(minsize == wxDefaultSize)) | |
239 | sizer->SetMinSize(minsize); | |
240 | ||
241 | // save state | |
242 | wxSizer *old_par = m_parentSizer; | |
243 | bool old_ins = m_isInside; | |
0d077b41 RD |
244 | |
245 | // set new state | |
246 | m_parentSizer = sizer; | |
f80ea77b | 247 | m_isInside = true; |
0d077b41 | 248 | m_isGBS = (m_class == wxT("wxGridBagSizer")); |
f80ea77b WS |
249 | |
250 | CreateChildren(m_parent, true/*only this handler*/); | |
0d077b41 RD |
251 | |
252 | // restore state | |
0d077b41 RD |
253 | m_isInside = old_ins; |
254 | m_parentSizer = old_par; | |
255 | ||
256 | if (m_parentSizer == NULL) // setup window: | |
257 | { | |
0d077b41 RD |
258 | m_parentAsWindow->SetSizer(sizer); |
259 | ||
260 | wxXmlNode *nd = m_node; | |
261 | m_node = parentNode; | |
262 | if (GetSize() == wxDefaultSize) | |
5e1a9f05 VS |
263 | { |
264 | if ( wxDynamicCast(m_parentAsWindow, wxScrolledWindow) != NULL ) | |
265 | { | |
266 | sizer->FitInside(m_parentAsWindow); | |
267 | } | |
268 | else | |
269 | { | |
270 | sizer->Fit(m_parentAsWindow); | |
271 | } | |
272 | } | |
0d077b41 RD |
273 | m_node = nd; |
274 | ||
ea813d91 VS |
275 | if (m_parentAsWindow->IsTopLevel()) |
276 | { | |
0d077b41 | 277 | sizer->SetSizeHints(m_parentAsWindow); |
ea813d91 | 278 | } |
0d077b41 | 279 | } |
f80ea77b | 280 | |
0d077b41 RD |
281 | return sizer; |
282 | } | |
283 | ||
284 | ||
285 | wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer() | |
286 | { | |
287 | return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL)); | |
f80ea77b WS |
288 | } |
289 | ||
627ddac9 | 290 | #if wxUSE_STATBOX |
0d077b41 RD |
291 | wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer() |
292 | { | |
293 | return new wxStaticBoxSizer( | |
294 | new wxStaticBox(m_parentAsWindow, | |
295 | GetID(), | |
296 | GetText(wxT("label")), | |
297 | wxDefaultPosition, wxDefaultSize, | |
298 | 0/*style*/, | |
299 | GetName()), | |
300 | GetStyle(wxT("orient"), wxHORIZONTAL)); | |
301 | } | |
627ddac9 | 302 | #endif // wxUSE_STATBOX |
f80ea77b | 303 | |
0d077b41 RD |
304 | wxSizer* wxSizerXmlHandler::Handle_wxGridSizer() |
305 | { | |
306 | return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")), | |
307 | GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); | |
308 | } | |
309 | ||
310 | ||
311 | wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer() | |
312 | { | |
f80ea77b | 313 | wxFlexGridSizer *sizer = |
0d077b41 RD |
314 | new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")), |
315 | GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); | |
316 | SetGrowables(sizer, wxT("growablerows"), true); | |
317 | SetGrowables(sizer, wxT("growablecols"), false); | |
318 | return sizer; | |
319 | } | |
320 | ||
321 | ||
322 | wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer() | |
323 | { | |
f80ea77b | 324 | wxGridBagSizer *sizer = |
0d077b41 RD |
325 | new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap"))); |
326 | SetGrowables(sizer, wxT("growablerows"), true); | |
327 | SetGrowables(sizer, wxT("growablecols"), false); | |
328 | return sizer; | |
329 | } | |
330 | ||
13a38887 VZ |
331 | wxSizer* wxSizerXmlHandler::Handle_wxWrapSizer() |
332 | { | |
63b37a4e | 333 | wxWrapSizer *sizer = new wxWrapSizer(GetStyle("orient"), GetStyle("flag")); |
13a38887 VZ |
334 | return sizer; |
335 | } | |
0d077b41 RD |
336 | |
337 | ||
338 | ||
339 | void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer, | |
340 | const wxChar* param, | |
341 | bool rows) | |
342 | { | |
343 | wxStringTokenizer tkn; | |
344 | unsigned long l; | |
345 | tkn.SetString(GetParamValue(param), wxT(",")); | |
346 | while (tkn.HasMoreTokens()) | |
347 | { | |
348 | if (!tkn.GetNextToken().ToULong(&l)) | |
349 | wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers")); | |
350 | else { | |
351 | if (rows) | |
352 | sizer->AddGrowableRow(l); | |
353 | else | |
354 | sizer->AddGrowableCol(l); | |
78d14f80 | 355 | } |
78d14f80 VS |
356 | } |
357 | } | |
358 | ||
359 | ||
0d077b41 RD |
360 | wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param) |
361 | { | |
362 | wxSize sz = GetSize(param); | |
363 | if (sz.x < 0) sz.x = 0; | |
364 | if (sz.y < 0) sz.y = 0; | |
365 | return wxGBPosition(sz.x, sz.y); | |
366 | } | |
78d14f80 | 367 | |
0d077b41 RD |
368 | wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param) |
369 | { | |
370 | wxSize sz = GetSize(param); | |
371 | if (sz.x < 1) sz.x = 1; | |
372 | if (sz.y < 1) sz.y = 1; | |
373 | return wxGBSpan(sz.x, sz.y); | |
374 | } | |
375 | ||
376 | ||
377 | ||
378 | wxSizerItem* wxSizerXmlHandler::MakeSizerItem() | |
379 | { | |
380 | if (m_isGBS) | |
381 | return new wxGBSizerItem(); | |
382 | else | |
383 | return new wxSizerItem(); | |
384 | } | |
385 | ||
386 | void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem) | |
387 | { | |
388 | sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too? | |
389 | sitem->SetFlag(GetStyle(wxT("flag"))); | |
390 | sitem->SetBorder(GetDimension(wxT("border"))); | |
391 | wxSize sz = GetSize(wxT("minsize")); | |
392 | if (!(sz == wxDefaultSize)) | |
1eba2193 | 393 | sitem->SetMinSize(sz); |
0d077b41 RD |
394 | sz = GetSize(wxT("ratio")); |
395 | if (!(sz == wxDefaultSize)) | |
396 | sitem->SetRatio(sz); | |
f80ea77b | 397 | |
0d077b41 RD |
398 | if (m_isGBS) |
399 | { | |
400 | wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem; | |
ea0d8ca6 RD |
401 | gbsitem->SetPos(GetGBPos(wxT("cellpos"))); |
402 | gbsitem->SetSpan(GetGBSpan(wxT("cellspan"))); | |
f80ea77b | 403 | } |
86909f4c VZ |
404 | |
405 | // record the id of the item, if any, for use by XRCSIZERITEM() | |
406 | sitem->SetId(GetID()); | |
0d077b41 RD |
407 | } |
408 | ||
409 | void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem) | |
78d14f80 | 410 | { |
0d077b41 RD |
411 | if (m_isGBS) |
412 | ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem); | |
413 | else | |
414 | m_parentSizer->Add(sitem); | |
78d14f80 | 415 | } |
f80ea77b | 416 | |
172541f6 VS |
417 | |
418 | ||
419 | //----------------------------------------------------------------------------- | |
420 | // wxStdDialogButtonSizerXmlHandler | |
421 | //----------------------------------------------------------------------------- | |
dd47af27 | 422 | #if wxUSE_BUTTON |
172541f6 VS |
423 | |
424 | IMPLEMENT_DYNAMIC_CLASS(wxStdDialogButtonSizerXmlHandler, wxXmlResourceHandler) | |
425 | ||
426 | wxStdDialogButtonSizerXmlHandler::wxStdDialogButtonSizerXmlHandler() | |
427 | : m_isInside(false), m_parentSizer(NULL) | |
428 | { | |
429 | } | |
430 | ||
431 | wxObject *wxStdDialogButtonSizerXmlHandler::DoCreateResource() | |
432 | { | |
433 | if (m_class == wxT("wxStdDialogButtonSizer")) | |
434 | { | |
435 | wxASSERT( !m_parentSizer ); | |
436 | ||
437 | wxSizer *s = m_parentSizer = new wxStdDialogButtonSizer; | |
438 | m_isInside = true; | |
439 | ||
440 | CreateChildren(m_parent, true/*only this handler*/); | |
441 | ||
442 | m_parentSizer->Realize(); | |
443 | ||
444 | m_isInside = false; | |
445 | m_parentSizer = NULL; | |
446 | ||
447 | return s; | |
448 | } | |
449 | else // m_class == "button" | |
450 | { | |
451 | wxASSERT( m_parentSizer ); | |
452 | ||
453 | // find the item to be managed by this sizeritem | |
454 | wxXmlNode *n = GetParamNode(wxT("object")); | |
455 | if ( !n ) | |
456 | n = GetParamNode(wxT("object_ref")); | |
457 | ||
458 | // did we find one? | |
459 | if (n) | |
460 | { | |
461 | wxObject *item = CreateResFromNode(n, m_parent, NULL); | |
462 | wxButton *button = wxDynamicCast(item, wxButton); | |
463 | ||
464 | if (button) | |
465 | m_parentSizer->AddButton(button); | |
466 | else | |
467 | wxLogError(wxT("Error in resource - expected button.")); | |
468 | ||
469 | return item; | |
470 | } | |
471 | else /*n == NULL*/ | |
472 | { | |
473 | wxLogError(wxT("Error in resource: no button within wxStdDialogButtonSizer.")); | |
474 | return NULL; | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
479 | bool wxStdDialogButtonSizerXmlHandler::CanHandle(wxXmlNode *node) | |
480 | { | |
481 | return (!m_isInside && IsOfClass(node, wxT("wxStdDialogButtonSizer"))) || | |
482 | (m_isInside && IsOfClass(node, wxT("button"))); | |
483 | } | |
dd47af27 | 484 | #endif // wxUSE_BUTTON |
172541f6 | 485 | |
621be1ec | 486 | #endif // wxUSE_XRC |