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