]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_sizer.cpp
Further version changes
[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 XRC_ADD_STYLE(wxADJUST_MINSIZE);
73 }
74
75
76
77 wxObject *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, -1, GetText(wxT("label"))),
154 GetStyle(wxT("orient"), wxHORIZONTAL));
155 }
156
157 else if (m_class == wxT("wxGridSizer"))
158 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
159 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
160
161 else if (m_class == wxT("wxFlexGridSizer"))
162 {
163 wxFlexGridSizer *fsizer =
164 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
165 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
166 sizer = fsizer;
167 wxStringTokenizer tkn;
168 unsigned long l;
169 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
170 while (tkn.HasMoreTokens())
171 {
172 if (!tkn.GetNextToken().ToULong(&l))
173 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
174 else
175 fsizer->AddGrowableRow(l);
176 }
177 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
178 while (tkn.HasMoreTokens())
179 {
180 if (!tkn.GetNextToken().ToULong(&l))
181 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
182 else
183 fsizer->AddGrowableCol(l);
184 }
185 }
186
187 wxSize minsize = GetSize(wxT("minsize"));
188 if (!(minsize == wxDefaultSize))
189 sizer->SetMinSize(minsize);
190
191 wxSizer *old_par = m_parentSizer;
192 m_parentSizer = sizer;
193 bool old_ins = m_isInside;
194 m_isInside = TRUE;
195 CreateChildren(m_parent, TRUE/*only this handler*/);
196 m_isInside = old_ins;
197 m_parentSizer = old_par;
198
199 if (m_parentSizer == NULL) // setup window:
200 {
201 m_parentAsWindow->SetAutoLayout(TRUE);
202 m_parentAsWindow->SetSizer(sizer);
203
204 wxXmlNode *nd = m_node;
205 m_node = parentNode;
206 if (GetSize() == wxDefaultSize)
207 sizer->Fit(m_parentAsWindow);
208 m_node = nd;
209
210 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
211 sizer->SetSizeHints(m_parentAsWindow);
212 }
213
214 return sizer;
215 }
216 }
217
218
219
220 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
221 {
222 return ((!m_isInside && IsSizerNode(node)) ||
223 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
224 (m_isInside && IsOfClass(node, wxT("spacer"))));
225 }