]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xrc/xh_sizer.cpp
Applied patch [ 795491 ] Multimon sample compatibility with various conditions
[wxWidgets.git] / contrib / 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 != NULL &&
146 (m_parentAsWindow->IsKindOf(CLASSINFO(wxPanel)) ||
147 m_parentAsWindow->IsKindOf(CLASSINFO(wxFrame)) ||
148 m_parentAsWindow->IsKindOf(CLASSINFO(wxDialog)))
149 ), NULL,
150 wxT("Incorrect use of sizer: parent is not 'wxDialog', 'wxFrame' or 'wxPanel'."));
151
152 if (m_class == wxT("wxBoxSizer"))
153 sizer = new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
154
155 else if (m_class == wxT("wxStaticBoxSizer"))
156 {
157 sizer = new wxStaticBoxSizer(
158 new wxStaticBox(m_parentAsWindow,
159 GetID(),
160 GetText(wxT("label")),
161 wxDefaultPosition, wxDefaultSize,
162 0/*style*/,
163 GetName()),
164 GetStyle(wxT("orient"), wxHORIZONTAL));
165 }
166
167 else if (m_class == wxT("wxGridSizer"))
168 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
169 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
170
171 else if (m_class == wxT("wxFlexGridSizer"))
172 {
173 wxFlexGridSizer *fsizer =
174 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
175 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
176 sizer = fsizer;
177 wxStringTokenizer tkn;
178 unsigned long l;
179 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
180 while (tkn.HasMoreTokens())
181 {
182 if (!tkn.GetNextToken().ToULong(&l))
183 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
184 else
185 fsizer->AddGrowableRow(l);
186 }
187 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
188 while (tkn.HasMoreTokens())
189 {
190 if (!tkn.GetNextToken().ToULong(&l))
191 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
192 else
193 fsizer->AddGrowableCol(l);
194 }
195 }
196
197 wxSize minsize = GetSize(wxT("minsize"));
198 if (!(minsize == wxDefaultSize))
199 sizer->SetMinSize(minsize);
200
201 wxSizer *old_par = m_parentSizer;
202 m_parentSizer = sizer;
203 bool old_ins = m_isInside;
204 m_isInside = TRUE;
205 CreateChildren(m_parent, TRUE/*only this handler*/);
206 m_isInside = old_ins;
207 m_parentSizer = old_par;
208
209 if (m_parentSizer == NULL) // setup window:
210 {
211 m_parentAsWindow->SetAutoLayout(TRUE);
212 m_parentAsWindow->SetSizer(sizer);
213
214 wxXmlNode *nd = m_node;
215 m_node = parentNode;
216 if (GetSize() == wxDefaultSize)
217 sizer->Fit(m_parentAsWindow);
218 m_node = nd;
219
220 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
221 sizer->SetSizeHints(m_parentAsWindow);
222 }
223
224 return sizer;
225 }
226 }
227
228
229
230 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
231 {
232 return ((!m_isInside && IsSizerNode(node)) ||
233 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
234 (m_isInside && IsOfClass(node, wxT("spacer"))));
235 }