]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
Add samples mobile and ownerdrw. Make sample richedit available on all
[wxWidgets.git] / src / xrc / xh_sizer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "xh_sizer.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #if wxUSE_XRC
23
24 #include "wx/xrc/xh_sizer.h"
25 #include "wx/sizer.h"
26 #include "wx/gbsizer.h"
27 #include "wx/log.h"
28 #include "wx/statbox.h"
29 #include "wx/notebook.h"
30 #include "wx/panel.h"
31 #include "wx/tokenzr.h"
32
33
34
35 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
36
37
38
39 wxSizerXmlHandler::wxSizerXmlHandler()
40 : wxXmlResourceHandler(),
41 m_isInside(false),
42 m_isGBS(false),
43 m_parentSizer(NULL)
44 {
45 XRC_ADD_STYLE(wxHORIZONTAL);
46 XRC_ADD_STYLE(wxVERTICAL);
47
48 // and flags
49 XRC_ADD_STYLE(wxLEFT);
50 XRC_ADD_STYLE(wxRIGHT);
51 XRC_ADD_STYLE(wxTOP);
52 XRC_ADD_STYLE(wxBOTTOM);
53 XRC_ADD_STYLE(wxNORTH);
54 XRC_ADD_STYLE(wxSOUTH);
55 XRC_ADD_STYLE(wxEAST);
56 XRC_ADD_STYLE(wxWEST);
57 XRC_ADD_STYLE(wxALL);
58
59 XRC_ADD_STYLE(wxGROW);
60 XRC_ADD_STYLE(wxEXPAND);
61 XRC_ADD_STYLE(wxSHAPED);
62 XRC_ADD_STYLE(wxSTRETCH_NOT);
63
64 XRC_ADD_STYLE(wxALIGN_CENTER);
65 XRC_ADD_STYLE(wxALIGN_CENTRE);
66 XRC_ADD_STYLE(wxALIGN_LEFT);
67 XRC_ADD_STYLE(wxALIGN_TOP);
68 XRC_ADD_STYLE(wxALIGN_RIGHT);
69 XRC_ADD_STYLE(wxALIGN_BOTTOM);
70 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
71 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
72 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
73 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
74
75 XRC_ADD_STYLE(wxADJUST_MINSIZE);
76 XRC_ADD_STYLE(wxFIXED_MINSIZE);
77 }
78
79
80
81 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
82 {
83 return ( (!m_isInside && IsSizerNode(node)) ||
84 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
85 (m_isInside && IsOfClass(node, wxT("spacer")))
86 );
87 }
88
89
90 wxObject* wxSizerXmlHandler::DoCreateResource()
91 {
92 if (m_class == wxT("sizeritem"))
93 return Handle_sizeritem();
94
95 else if (m_class == wxT("spacer"))
96 return Handle_spacer();
97
98 else
99 return Handle_sizer();
100 }
101
102
103
104
105 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
106 {
107 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
108 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
109 (IsOfClass(node, wxT("wxGridSizer"))) ||
110 (IsOfClass(node, wxT("wxFlexGridSizer"))) ||
111 (IsOfClass(node, wxT("wxGridBagSizer")));
112 }
113
114
115 wxObject* wxSizerXmlHandler::Handle_sizeritem()
116 {
117 // find the item to be managed by this sizeritem
118 wxXmlNode *n = GetParamNode(wxT("object"));
119 if ( !n )
120 n = GetParamNode(wxT("object_ref"));
121
122 // did we find one?
123 if (n)
124 {
125 // create a sizer item for it
126 wxSizerItem* sitem = MakeSizerItem();
127
128 // now fetch the item to be managed
129 bool old_gbs = m_isGBS;
130 bool old_ins = m_isInside;
131 wxSizer *old_par = m_parentSizer;
132 m_isInside = false;
133 if (!IsSizerNode(n)) m_parentSizer = NULL;
134 wxObject *item = CreateResFromNode(n, m_parent, NULL);
135 m_isInside = old_ins;
136 m_parentSizer = old_par;
137 m_isGBS = old_gbs;
138
139 // and figure out what type it is
140 wxSizer *sizer = wxDynamicCast(item, wxSizer);
141 wxWindow *wnd = wxDynamicCast(item, wxWindow);
142
143 if (sizer)
144 sitem->SetSizer(sizer);
145 else if (wnd)
146 sitem->SetWindow(wnd);
147 else
148 wxLogError(wxT("Error in resource."));
149
150 // finally, set other wxSizerItem attributes
151 SetSizerItemAttributes(sitem);
152
153 AddSizerItem(sitem);
154 return item;
155 }
156 else /*n == NULL*/
157 {
158 wxLogError(wxT("Error in resource: no window/sizer/spacer within sizeritem object."));
159 return NULL;
160 }
161 }
162
163
164 wxObject* wxSizerXmlHandler::Handle_spacer()
165 {
166 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
167
168 wxSizerItem* sitem = MakeSizerItem();
169 SetSizerItemAttributes(sitem);
170 sitem->SetSpacer(GetSize());
171 AddSizerItem(sitem);
172 return NULL;
173 }
174
175
176 wxObject* wxSizerXmlHandler::Handle_sizer()
177 {
178 wxSizer *sizer = NULL;
179
180 wxXmlNode *parentNode = m_node->GetParent();
181
182 wxCHECK_MSG(m_parentSizer != NULL ||
183 (parentNode && parentNode->GetType() == wxXML_ELEMENT_NODE &&
184 m_parentAsWindow != NULL &&
185 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
186 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
187 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
188 ), NULL,
189 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
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
207 wxSize minsize = GetSize(wxT("minsize"));
208 if (!(minsize == wxDefaultSize))
209 sizer->SetMinSize(minsize);
210
211 // save state
212 wxSizer *old_par = m_parentSizer;
213 bool old_ins = m_isInside;
214
215 // set new state
216 m_parentSizer = sizer;
217 m_isInside = true;
218 m_isGBS = (m_class == wxT("wxGridBagSizer"));
219
220 CreateChildren(m_parent, true/*only this handler*/);
221
222 // restore state
223 m_isInside = old_ins;
224 m_parentSizer = old_par;
225
226 if (m_parentSizer == NULL) // setup window:
227 {
228 m_parentAsWindow->SetSizer(sizer);
229
230 wxXmlNode *nd = m_node;
231 m_node = parentNode;
232 if (GetSize() == wxDefaultSize)
233 sizer->Fit(m_parentAsWindow);
234 m_node = nd;
235
236 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
237 sizer->SetSizeHints(m_parentAsWindow);
238 }
239
240 return sizer;
241 }
242
243
244 wxSizer* wxSizerXmlHandler::Handle_wxBoxSizer()
245 {
246 return new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
247 }
248
249 wxSizer* wxSizerXmlHandler::Handle_wxStaticBoxSizer()
250 {
251 return new wxStaticBoxSizer(
252 new wxStaticBox(m_parentAsWindow,
253 GetID(),
254 GetText(wxT("label")),
255 wxDefaultPosition, wxDefaultSize,
256 0/*style*/,
257 GetName()),
258 GetStyle(wxT("orient"), wxHORIZONTAL));
259 }
260
261 wxSizer* wxSizerXmlHandler::Handle_wxGridSizer()
262 {
263 return new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
264 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
265 }
266
267
268 wxSizer* wxSizerXmlHandler::Handle_wxFlexGridSizer()
269 {
270 wxFlexGridSizer *sizer =
271 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
272 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
273 SetGrowables(sizer, wxT("growablerows"), true);
274 SetGrowables(sizer, wxT("growablecols"), false);
275 return sizer;
276 }
277
278
279 wxSizer* wxSizerXmlHandler::Handle_wxGridBagSizer()
280 {
281 wxGridBagSizer *sizer =
282 new wxGridBagSizer(GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
283 SetGrowables(sizer, wxT("growablerows"), true);
284 SetGrowables(sizer, wxT("growablecols"), false);
285 return sizer;
286 }
287
288
289
290
291 void wxSizerXmlHandler::SetGrowables(wxFlexGridSizer* sizer,
292 const wxChar* param,
293 bool rows)
294 {
295 wxStringTokenizer tkn;
296 unsigned long l;
297 tkn.SetString(GetParamValue(param), wxT(","));
298 while (tkn.HasMoreTokens())
299 {
300 if (!tkn.GetNextToken().ToULong(&l))
301 wxLogError(wxT("growable[rows|cols] must be comma-separated list of row numbers"));
302 else {
303 if (rows)
304 sizer->AddGrowableRow(l);
305 else
306 sizer->AddGrowableCol(l);
307 }
308 }
309 }
310
311
312 wxGBPosition wxSizerXmlHandler::GetGBPos(const wxString& param)
313 {
314 wxSize sz = GetSize(param);
315 if (sz.x < 0) sz.x = 0;
316 if (sz.y < 0) sz.y = 0;
317 return wxGBPosition(sz.x, sz.y);
318 }
319
320 wxGBSpan wxSizerXmlHandler::GetGBSpan(const wxString& param)
321 {
322 wxSize sz = GetSize(param);
323 if (sz.x < 1) sz.x = 1;
324 if (sz.y < 1) sz.y = 1;
325 return wxGBSpan(sz.x, sz.y);
326 }
327
328
329
330 wxSizerItem* wxSizerXmlHandler::MakeSizerItem()
331 {
332 if (m_isGBS)
333 return new wxGBSizerItem();
334 else
335 return new wxSizerItem();
336 }
337
338 void wxSizerXmlHandler::SetSizerItemAttributes(wxSizerItem* sitem)
339 {
340 sitem->SetProportion(GetLong(wxT("option"))); // Should this check for "proportion" too?
341 sitem->SetFlag(GetStyle(wxT("flag")));
342 sitem->SetBorder(GetDimension(wxT("border")));
343 wxSize sz = GetSize(wxT("minsize"));
344 if (!(sz == wxDefaultSize))
345 sitem->SetMinSize(sz);
346 sz = GetSize(wxT("ratio"));
347 if (!(sz == wxDefaultSize))
348 sitem->SetRatio(sz);
349
350 if (m_isGBS)
351 {
352 wxGBSizerItem* gbsitem = (wxGBSizerItem*)sitem;
353 gbsitem->SetPos(GetGBPos(wxT("cellpos")));
354 gbsitem->SetSpan(GetGBSpan(wxT("cellspan")));
355 }
356 }
357
358 void wxSizerXmlHandler::AddSizerItem(wxSizerItem* sitem)
359 {
360 if (m_isGBS)
361 ((wxGridBagSizer*)m_parentSizer)->Add((wxGBSizerItem*)sitem);
362 else
363 m_parentSizer->Add(sitem);
364 }
365
366 #endif // wxUSE_XRC