Changes to the XRC library:
[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 ADD_STYLE(wxHORIZONTAL);
43 ADD_STYLE(wxVERTICAL);
44
45 // and flags
46 ADD_STYLE(wxLEFT);
47 ADD_STYLE(wxRIGHT);
48 ADD_STYLE(wxTOP);
49 ADD_STYLE(wxBOTTOM);
50 ADD_STYLE(wxNORTH);
51 ADD_STYLE(wxSOUTH);
52 ADD_STYLE(wxEAST);
53 ADD_STYLE(wxWEST);
54 ADD_STYLE(wxALL);
55
56 ADD_STYLE(wxGROW);
57 ADD_STYLE(wxEXPAND);
58 ADD_STYLE(wxSHAPED);
59 ADD_STYLE(wxSTRETCH_NOT);
60
61 ADD_STYLE(wxALIGN_CENTER);
62 ADD_STYLE(wxALIGN_CENTRE);
63 ADD_STYLE(wxALIGN_LEFT);
64 ADD_STYLE(wxALIGN_TOP);
65 ADD_STYLE(wxALIGN_RIGHT);
66 ADD_STYLE(wxALIGN_BOTTOM);
67 ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
68 ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
69 ADD_STYLE(wxALIGN_CENTER_VERTICAL);
70 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("wxDialog"))) &&
141 parentNode->GetType() == wxXML_ELEMENT_NODE), NULL,
142 wxT("Incorrect use of sizer: parent is not 'wxDialog' or 'wxPanel'."));
143
144 if (m_class == wxT("wxBoxSizer"))
145 sizer = new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
146
147 else if (m_class == wxT("wxStaticBoxSizer"))
148 {
149 sizer = new wxStaticBoxSizer(
150 new wxStaticBox(m_parentAsWindow, -1, GetText(wxT("label"))),
151 GetStyle(wxT("orient"), wxHORIZONTAL));
152 }
153
154 else if (m_class == wxT("wxGridSizer"))
155 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
156 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
157
158 else if (m_class == wxT("wxFlexGridSizer"))
159 {
160 wxFlexGridSizer *fsizer =
161 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
162 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
163 sizer = fsizer;
164 wxStringTokenizer tkn;
165 unsigned long l;
166 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
167 while (tkn.HasMoreTokens())
168 {
169 if (!tkn.GetNextToken().ToULong(&l))
170 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
171 else
172 fsizer->AddGrowableRow(l);
173 }
174 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
175 while (tkn.HasMoreTokens())
176 {
177 if (!tkn.GetNextToken().ToULong(&l))
178 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
179 else
180 fsizer->AddGrowableCol(l);
181 }
182 }
183
184 wxSize minsize = GetSize(wxT("minsize"));
185 if (!(minsize == wxDefaultSize))
186 sizer->SetMinSize(minsize);
187
188 wxSizer *old_par = m_parentSizer;
189 m_parentSizer = sizer;
190 bool old_ins = m_isInside;
191 m_isInside = TRUE;
192 CreateChildren(m_parent, TRUE/*only this handler*/);
193 m_isInside = old_ins;
194 m_parentSizer = old_par;
195
196 if (m_parentSizer == NULL) // setup window:
197 {
198 m_parentAsWindow->SetAutoLayout(TRUE);
199 m_parentAsWindow->SetSizer(sizer);
200
201 wxXmlNode *nd = m_node;
202 m_node = parentNode;
203 if (GetSize() == wxDefaultSize)
204 sizer->Fit(m_parentAsWindow);
205 m_node = nd;
206
207 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
208 sizer->SetSizeHints(m_parentAsWindow);
209 }
210
211 return sizer;
212 }
213 }
214
215
216
217 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
218 {
219 return ((!m_isInside && IsSizerNode(node)) ||
220 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
221 (m_isInside && IsOfClass(node, wxT("spacer"))));
222 }