]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
fixed crash under GTK+ 2.0
[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 #ifdef __GNUG__
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 #include "wx/xrc/xh_sizer.h"
23 #include "wx/sizer.h"
24 #include "wx/log.h"
25 #include "wx/statbox.h"
26 #include "wx/notebook.h"
27 #include "wx/tokenzr.h"
28
29 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
30 {
31 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
32 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
33 (IsOfClass(node, wxT("wxGridSizer"))) ||
34 (IsOfClass(node, wxT("wxFlexGridSizer")));
35 }
36
37
38
39 wxSizerXmlHandler::wxSizerXmlHandler()
40 : wxXmlResourceHandler(), m_isInside(FALSE), m_parentSizer(NULL)
41 {
42 XRC_ADD_STYLE(wxHORIZONTAL);
43 XRC_ADD_STYLE(wxVERTICAL);
44
45 // and flags
46 XRC_ADD_STYLE(wxLEFT);
47 XRC_ADD_STYLE(wxRIGHT);
48 XRC_ADD_STYLE(wxTOP);
49 XRC_ADD_STYLE(wxBOTTOM);
50 XRC_ADD_STYLE(wxNORTH);
51 XRC_ADD_STYLE(wxSOUTH);
52 XRC_ADD_STYLE(wxEAST);
53 XRC_ADD_STYLE(wxWEST);
54 XRC_ADD_STYLE(wxALL);
55
56 XRC_ADD_STYLE(wxGROW);
57 XRC_ADD_STYLE(wxEXPAND);
58 XRC_ADD_STYLE(wxSHAPED);
59 XRC_ADD_STYLE(wxSTRETCH_NOT);
60
61 XRC_ADD_STYLE(wxALIGN_CENTER);
62 XRC_ADD_STYLE(wxALIGN_CENTRE);
63 XRC_ADD_STYLE(wxALIGN_LEFT);
64 XRC_ADD_STYLE(wxALIGN_TOP);
65 XRC_ADD_STYLE(wxALIGN_RIGHT);
66 XRC_ADD_STYLE(wxALIGN_BOTTOM);
67 XRC_ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
68 XRC_ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
69 XRC_ADD_STYLE(wxALIGN_CENTER_VERTICAL);
70 XRC_ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
71 }
72
73
74
75 wxObject *wxSizerXmlHandler::DoCreateResource()
76 {
77 if (m_class == wxT("sizeritem"))
78 {
79 wxXmlNode *n = GetParamNode(wxT("object"));
80
81 if ( !n )
82 n = GetParamNode(wxT("object_ref"));
83
84 if (n)
85 {
86 bool old_ins = m_isInside;
87 wxSizer *old_par = m_parentSizer;
88 m_isInside = FALSE;
89 if (!IsSizerNode(n)) m_parentSizer = NULL;
90 wxObject *item = CreateResFromNode(n, m_parent, NULL);
91 m_isInside = old_ins;
92 m_parentSizer = old_par;
93 wxSizer *sizer = wxDynamicCast(item, wxSizer);
94 wxWindow *wnd = wxDynamicCast(item, wxWindow);
95 wxSize minsize = GetSize(wxT("minsize"));
96
97 if (sizer)
98 {
99 m_parentSizer->Add(sizer, GetLong(wxT("option")),
100 GetStyle(wxT("flag")), GetDimension(wxT("border")));
101 if (!(minsize == wxDefaultSize))
102 m_parentSizer->SetItemMinSize(sizer, minsize.x, minsize.y);
103 }
104 else if (wnd)
105 {
106 m_parentSizer->Add(wnd, GetLong(wxT("option")),
107 GetStyle(wxT("flag")), GetDimension(wxT("border")));
108 if (!(minsize == wxDefaultSize))
109 m_parentSizer->SetItemMinSize(wnd, minsize.x, minsize.y);
110 }
111 else
112 wxLogError(wxT("Error in resource."));
113
114 return item;
115 }
116 else /*n == NULL*/
117 {
118 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
119 return NULL;
120 }
121 }
122
123 else if (m_class == wxT("spacer"))
124 {
125 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
126 wxSize sz = GetSize();
127 m_parentSizer->Add(sz.x, sz.y,
128 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
129 return NULL;
130 }
131
132
133 else {
134 wxSizer *sizer = NULL;
135
136 wxXmlNode *parentNode = m_node->GetParent();
137
138 wxCHECK_MSG(m_parentSizer != NULL ||
139 ((IsOfClass(parentNode, wxT("wxPanel")) ||
140 IsOfClass(parentNode, wxT("wxFrame")) ||
141 IsOfClass(parentNode, wxT("wxDialog"))) &&
142 parentNode->GetType() == wxXML_ELEMENT_NODE), NULL,
143 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
144
145 if (m_class == wxT("wxBoxSizer"))
146 sizer = new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
147
148 else if (m_class == wxT("wxStaticBoxSizer"))
149 {
150 sizer = new wxStaticBoxSizer(
151 new wxStaticBox(m_parentAsWindow, -1, GetText(wxT("label"))),
152 GetStyle(wxT("orient"), wxHORIZONTAL));
153 }
154
155 else if (m_class == wxT("wxGridSizer"))
156 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
157 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
158
159 else if (m_class == wxT("wxFlexGridSizer"))
160 {
161 wxFlexGridSizer *fsizer =
162 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
163 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
164 sizer = fsizer;
165 wxStringTokenizer tkn;
166 unsigned long l;
167 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
168 while (tkn.HasMoreTokens())
169 {
170 if (!tkn.GetNextToken().ToULong(&l))
171 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
172 else
173 fsizer->AddGrowableRow(l);
174 }
175 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
176 while (tkn.HasMoreTokens())
177 {
178 if (!tkn.GetNextToken().ToULong(&l))
179 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
180 else
181 fsizer->AddGrowableCol(l);
182 }
183 }
184
185 wxSize minsize = GetSize(wxT("minsize"));
186 if (!(minsize == wxDefaultSize))
187 sizer->SetMinSize(minsize);
188
189 wxSizer *old_par = m_parentSizer;
190 m_parentSizer = sizer;
191 bool old_ins = m_isInside;
192 m_isInside = TRUE;
193 CreateChildren(m_parent, TRUE/*only this handler*/);
194 m_isInside = old_ins;
195 m_parentSizer = old_par;
196
197 if (m_parentSizer == NULL) // setup window:
198 {
199 m_parentAsWindow->SetAutoLayout(TRUE);
200 m_parentAsWindow->SetSizer(sizer);
201
202 wxXmlNode *nd = m_node;
203 m_node = parentNode;
204 if (GetSize() == wxDefaultSize)
205 sizer->Fit(m_parentAsWindow);
206 m_node = nd;
207
208 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
209 sizer->SetSizeHints(m_parentAsWindow);
210 }
211
212 return sizer;
213 }
214 }
215
216
217
218 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
219 {
220 return ((!m_isInside && IsSizerNode(node)) ||
221 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
222 (m_isInside && IsOfClass(node, wxT("spacer"))));
223 }