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