]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
wxCocoa: Added preliminary Blit support
[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/panel.h"
28 #include "wx/tokenzr.h"
29
30 IMPLEMENT_DYNAMIC_CLASS(wxSizerXmlHandler, wxXmlResourceHandler)
31
32 bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
33 {
34 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
35 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
36 (IsOfClass(node, wxT("wxGridSizer"))) ||
37 (IsOfClass(node, wxT("wxFlexGridSizer")));
38 }
39
40
41
42 wxSizerXmlHandler::wxSizerXmlHandler()
43 : wxXmlResourceHandler(), m_isInside(FALSE), 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 }
77
78
79
80 wxObject *wxSizerXmlHandler::DoCreateResource()
81 {
82 if (m_class == wxT("sizeritem"))
83 {
84 wxXmlNode *n = GetParamNode(wxT("object"));
85
86 if ( !n )
87 n = GetParamNode(wxT("object_ref"));
88
89 if (n)
90 {
91 bool old_ins = m_isInside;
92 wxSizer *old_par = m_parentSizer;
93 m_isInside = FALSE;
94 if (!IsSizerNode(n)) m_parentSizer = NULL;
95 wxObject *item = CreateResFromNode(n, m_parent, NULL);
96 m_isInside = old_ins;
97 m_parentSizer = old_par;
98 wxSizer *sizer = wxDynamicCast(item, wxSizer);
99 wxWindow *wnd = wxDynamicCast(item, wxWindow);
100 wxSize minsize = GetSize(wxT("minsize"));
101
102 if (sizer)
103 {
104 m_parentSizer->Add(sizer, GetLong(wxT("option")),
105 GetStyle(wxT("flag")), GetDimension(wxT("border")));
106 if (!(minsize == wxDefaultSize))
107 m_parentSizer->SetItemMinSize(sizer, minsize.x, minsize.y);
108 }
109 else if (wnd)
110 {
111 m_parentSizer->Add(wnd, GetLong(wxT("option")),
112 GetStyle(wxT("flag")), GetDimension(wxT("border")));
113 if (!(minsize == wxDefaultSize))
114 m_parentSizer->SetItemMinSize(wnd, minsize.x, minsize.y);
115 }
116 else
117 wxLogError(wxT("Error in resource."));
118
119 return item;
120 }
121 else /*n == NULL*/
122 {
123 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
124 return NULL;
125 }
126 }
127
128 else if (m_class == wxT("spacer"))
129 {
130 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XRC resource: spacer not within sizer!"));
131 wxSize sz = GetSize();
132 m_parentSizer->Add(sz.x, sz.y,
133 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
134 return NULL;
135 }
136
137
138 else {
139 wxSizer *sizer = NULL;
140
141 wxXmlNode *parentNode = m_node->GetParent();
142
143 wxCHECK_MSG(m_parentSizer != NULL ||
144 (parentNode->GetType() == wxXML_ELEMENT_NODE &&
145 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
146 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
147 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
148 ), NULL,
149 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
150
151 if (m_class == wxT("wxBoxSizer"))
152 sizer = new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
153
154 else if (m_class == wxT("wxStaticBoxSizer"))
155 {
156 sizer = new wxStaticBoxSizer(
157 new wxStaticBox(m_parentAsWindow,
158 GetID(),
159 GetText(wxT("label")),
160 wxDefaultPosition, wxDefaultSize,
161 0/*style*/,
162 GetName()),
163 GetStyle(wxT("orient"), wxHORIZONTAL));
164 }
165
166 else if (m_class == wxT("wxGridSizer"))
167 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
168 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
169
170 else if (m_class == wxT("wxFlexGridSizer"))
171 {
172 wxFlexGridSizer *fsizer =
173 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
174 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
175 sizer = fsizer;
176 wxStringTokenizer tkn;
177 unsigned long l;
178 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
179 while (tkn.HasMoreTokens())
180 {
181 if (!tkn.GetNextToken().ToULong(&l))
182 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
183 else
184 fsizer->AddGrowableRow(l);
185 }
186 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
187 while (tkn.HasMoreTokens())
188 {
189 if (!tkn.GetNextToken().ToULong(&l))
190 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
191 else
192 fsizer->AddGrowableCol(l);
193 }
194 }
195
196 wxSize minsize = GetSize(wxT("minsize"));
197 if (!(minsize == wxDefaultSize))
198 sizer->SetMinSize(minsize);
199
200 wxSizer *old_par = m_parentSizer;
201 m_parentSizer = sizer;
202 bool old_ins = m_isInside;
203 m_isInside = TRUE;
204 CreateChildren(m_parent, TRUE/*only this handler*/);
205 m_isInside = old_ins;
206 m_parentSizer = old_par;
207
208 if (m_parentSizer == NULL) // setup window:
209 {
210 m_parentAsWindow->SetAutoLayout(TRUE);
211 m_parentAsWindow->SetSizer(sizer);
212
213 wxXmlNode *nd = m_node;
214 m_node = parentNode;
215 if (GetSize() == wxDefaultSize)
216 sizer->Fit(m_parentAsWindow);
217 m_node = nd;
218
219 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
220 sizer->SetSizeHints(m_parentAsWindow);
221 }
222
223 return sizer;
224 }
225 }
226
227
228
229 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
230 {
231 return ((!m_isInside && IsSizerNode(node)) ||
232 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
233 (m_isInside && IsOfClass(node, wxT("spacer"))));
234 }