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