]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/xml/xh_sizer.cpp
oops, forgot to remove temporary debug printf...
[wxWidgets.git] / contrib / src / xml / xh_sizer.cpp
CommitLineData
56d2f750
VS
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"
bee5a36e 27#include "wx/tokenzr.h"
56d2f750 28
e066e256 29bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
0bed1cee 30{
a559d708
VS
31 return (IsOfClass(node, wxT("wxBoxSizer"))) ||
32 (IsOfClass(node, wxT("wxStaticBoxSizer"))) ||
33 (IsOfClass(node, wxT("wxGridSizer"))) ||
34 (IsOfClass(node, wxT("wxFlexGridSizer")));
0bed1cee
VS
35}
36
37
38
56d2f750 39wxSizerXmlHandler::wxSizerXmlHandler()
ab708d5d 40: wxXmlResourceHandler(), m_isInside(FALSE), m_parentSizer(NULL)
56d2f750
VS
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
75wxObject *wxSizerXmlHandler::DoCreateResource()
76{
ab708d5d 77 if (m_class == wxT("sizeritem"))
56d2f750 78 {
a559d708 79 wxXmlNode *n = GetParamNode(wxT("object"));
56d2f750 80
e066e256 81 if (n)
56d2f750 82 {
ab708d5d
VS
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;
e066e256
VS
90 wxSizer *sizer = wxDynamicCast(item, wxSizer);
91 wxWindow *wnd = wxDynamicCast(item, wxWindow);
a559d708 92 wxSize minsize = GetSize(wxT("minsize"));
e066e256
VS
93
94 if (sizer)
95 {
ab708d5d 96 m_parentSizer->Add(sizer, GetLong(wxT("option")),
a559d708 97 GetStyle(wxT("flag")), GetDimension(wxT("border")));
e066e256 98 if (!(minsize == wxDefaultSize))
ab708d5d 99 m_parentSizer->SetItemMinSize(sizer, minsize.x, minsize.y);
56d2f750 100 }
e066e256
VS
101 else if (wnd)
102 {
ab708d5d 103 m_parentSizer->Add(wnd, GetLong(wxT("option")),
a559d708 104 GetStyle(wxT("flag")), GetDimension(wxT("border")));
e066e256 105 if (!(minsize == wxDefaultSize))
ab708d5d 106 m_parentSizer->SetItemMinSize(wnd, minsize.x, minsize.y);
e066e256
VS
107 }
108 else
a559d708 109 wxLogError(wxT("Error in resource."));
e066e256
VS
110
111 return item;
112 }
113 else /*n == NULL*/
114 {
a559d708 115 wxLogError(wxT("Error in resource: no control/sizer within sizer's <item> tag."));
e066e256 116 return NULL;
56d2f750 117 }
56d2f750
VS
118 }
119
ab708d5d 120 else if (m_class == wxT("spacer"))
56d2f750 121 {
ab708d5d 122 wxCHECK_MSG(m_parentSizer, NULL, wxT("Incorrect syntax of XML resource: spacer not within sizer!"));
56d2f750 123 wxSize sz = GetSize();
ab708d5d 124 m_parentSizer->Add(sz.x, sz.y,
a559d708 125 GetLong(wxT("option")), GetStyle(wxT("flag")), GetDimension(wxT("border")));
56d2f750
VS
126 return NULL;
127 }
128
56d2f750 129
56d2f750
VS
130 else {
131 wxSizer *sizer = NULL;
132
ab708d5d 133 wxXmlNode *parentNode = m_node->GetParent();
56d2f750 134
ab708d5d 135 wxCHECK_MSG(m_parentSizer != NULL ||
a559d708
VS
136 ((IsOfClass(parentNode, wxT("wxPanel")) ||
137 IsOfClass(parentNode, wxT("wxDialog"))) &&
56d2f750 138 parentNode->GetType() == wxXML_ELEMENT_NODE), NULL,
a559d708 139 wxT("Incorrect use of sizer: parent is not 'wxDialog' or 'wxPanel'."));
56d2f750 140
ab708d5d 141 if (m_class == wxT("wxBoxSizer"))
a559d708 142 sizer = new wxBoxSizer(GetStyle(wxT("orient"), wxHORIZONTAL));
56d2f750 143
ab708d5d 144 else if (m_class == wxT("wxStaticBoxSizer"))
56d2f750
VS
145 {
146 sizer = new wxStaticBoxSizer(
ab708d5d 147 new wxStaticBox(m_parentAsWindow, -1, GetText(wxT("label"))),
a559d708 148 GetStyle(wxT("orient"), wxHORIZONTAL));
56d2f750 149 }
fccd6cdc 150
ab708d5d 151 else if (m_class == wxT("wxGridSizer"))
a559d708
VS
152 sizer = new wxGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
153 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
fccd6cdc 154
ab708d5d 155 else if (m_class == wxT("wxFlexGridSizer"))
bee5a36e
VS
156 {
157 wxFlexGridSizer *fsizer =
a559d708
VS
158 new wxFlexGridSizer(GetLong(wxT("rows")), GetLong(wxT("cols")),
159 GetDimension(wxT("vgap")), GetDimension(wxT("hgap")));
bee5a36e
VS
160 sizer = fsizer;
161 wxStringTokenizer tkn;
162 unsigned long l;
a559d708 163 tkn.SetString(GetParamValue(wxT("growablerows")), wxT(","));
bee5a36e
VS
164 while (tkn.HasMoreTokens())
165 {
166 if (!tkn.GetNextToken().ToULong(&l))
a559d708 167 wxLogError(wxT("growablerows must be comma-separated list of row numbers"));
bee5a36e
VS
168 else
169 fsizer->AddGrowableRow(l);
170 }
a559d708 171 tkn.SetString(GetParamValue(wxT("growablecols")), wxT(","));
bee5a36e
VS
172 while (tkn.HasMoreTokens())
173 {
174 if (!tkn.GetNextToken().ToULong(&l))
a559d708 175 wxLogError(wxT("growablecols must be comma-separated list of column numbers"));
bee5a36e
VS
176 else
177 fsizer->AddGrowableCol(l);
178 }
179 }
56d2f750 180
a559d708 181 wxSize minsize = GetSize(wxT("minsize"));
266527ec
VS
182 if (!(minsize == wxDefaultSize))
183 sizer->SetMinSize(minsize);
184
ab708d5d
VS
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;
56d2f750 192
ab708d5d 193 if (m_parentSizer == NULL) // setup window:
56d2f750 194 {
ab708d5d
VS
195 m_parentAsWindow->SetAutoLayout(TRUE);
196 m_parentAsWindow->SetSizer(sizer);
56d2f750 197
ab708d5d
VS
198 wxXmlNode *nd = m_node;
199 m_node = parentNode;
56d2f750 200 if (GetSize() == wxDefaultSize)
ab708d5d
VS
201 sizer->Fit(m_parentAsWindow);
202 m_node = nd;
56d2f750 203
ab708d5d
VS
204 if (m_parentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
205 sizer->SetSizeHints(m_parentAsWindow);
56d2f750
VS
206 }
207
208 return sizer;
209 }
210}
211
212
213
214bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
215{
ab708d5d
VS
216 return ((!m_isInside && IsSizerNode(node)) ||
217 (m_isInside && IsOfClass(node, wxT("sizeritem"))) ||
218 (m_isInside && IsOfClass(node, wxT("spacer"))));
56d2f750 219}