]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/xml/xh_sizer.cpp
Adding new wxCanvas
[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"
27
e066e256 28bool wxSizerXmlHandler::IsSizerNode(wxXmlNode *node)
0bed1cee 29{
e066e256
VS
30 return (IsOfClass(node, _T("wxBoxSizer"))) ||
31 (IsOfClass(node, _T("wxStaticBoxSizer"))) ||
32 (IsOfClass(node, _T("wxGridSizer"))) ||
33 (IsOfClass(node, _T("wxFlexGridSizer")));
0bed1cee
VS
34}
35
36
37
56d2f750
VS
38wxSizerXmlHandler::wxSizerXmlHandler()
39: wxXmlResourceHandler(), m_IsInside(FALSE), m_ParentSizer(NULL)
40{
41 ADD_STYLE(wxHORIZONTAL);
42 ADD_STYLE(wxVERTICAL);
43
44 // and flags
45 ADD_STYLE(wxLEFT);
46 ADD_STYLE(wxRIGHT);
47 ADD_STYLE(wxTOP);
48 ADD_STYLE(wxBOTTOM);
49 ADD_STYLE(wxNORTH);
50 ADD_STYLE(wxSOUTH);
51 ADD_STYLE(wxEAST);
52 ADD_STYLE(wxWEST);
53 ADD_STYLE(wxALL);
54
55 ADD_STYLE(wxGROW);
56 ADD_STYLE(wxEXPAND);
57 ADD_STYLE(wxSHAPED);
58 ADD_STYLE(wxSTRETCH_NOT);
59
60 ADD_STYLE(wxALIGN_CENTER);
61 ADD_STYLE(wxALIGN_CENTRE);
62 ADD_STYLE(wxALIGN_LEFT);
63 ADD_STYLE(wxALIGN_TOP);
64 ADD_STYLE(wxALIGN_RIGHT);
65 ADD_STYLE(wxALIGN_BOTTOM);
66 ADD_STYLE(wxALIGN_CENTER_HORIZONTAL);
67 ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL);
68 ADD_STYLE(wxALIGN_CENTER_VERTICAL);
69 ADD_STYLE(wxALIGN_CENTRE_VERTICAL);
70}
71
72
73
74wxObject *wxSizerXmlHandler::DoCreateResource()
75{
e066e256 76 if (m_Class == _T("sizeritem"))
56d2f750 77 {
e066e256 78 wxXmlNode *n = GetParamNode(_T("object"));
56d2f750 79
e066e256 80 if (n)
56d2f750 81 {
e066e256
VS
82 bool old_ins = m_IsInside;
83 wxSizer *old_par = m_ParentSizer;
84 m_IsInside = FALSE;
85 if (!IsSizerNode(n)) m_ParentSizer = NULL;
86 wxObject *item = CreateResFromNode(n, m_Parent, NULL);
87 m_IsInside = old_ins;
88 m_ParentSizer = old_par;
89 wxSizer *sizer = wxDynamicCast(item, wxSizer);
90 wxWindow *wnd = wxDynamicCast(item, wxWindow);
91 wxSize minsize = GetSize(_T("minsize"));
92
93 if (sizer)
94 {
95 m_ParentSizer->Add(sizer, GetLong(_T("option")),
96 GetStyle(_T("flag")), GetDimension(_T("border")));
97 if (!(minsize == wxDefaultSize))
98 m_ParentSizer->SetItemMinSize(sizer, minsize.x, minsize.y);
56d2f750 99 }
e066e256
VS
100 else if (wnd)
101 {
102 m_ParentSizer->Add(wnd, GetLong(_T("option")),
103 GetStyle(_T("flag")), GetDimension(_T("border")));
104 if (!(minsize == wxDefaultSize))
105 m_ParentSizer->SetItemMinSize(wnd, minsize.x, minsize.y);
106 }
107 else
108 wxLogError(_T("Error in resource."));
109
110 return item;
111 }
112 else /*n == NULL*/
113 {
114 wxLogError(_T("Error in resource: no control/sizer within sizer's <item> tag."));
115 return NULL;
56d2f750 116 }
56d2f750
VS
117 }
118
e066e256 119 else if (m_Class == _T("spacer"))
56d2f750
VS
120 {
121 wxCHECK_MSG(m_ParentSizer, NULL, _T("Incorrect syntax of XML resource: spacer not within sizer!"));
122 wxSize sz = GetSize();
123 m_ParentSizer->Add(sz.x, sz.y,
bebb14d5 124 GetLong(_T("option")), GetStyle(_T("flag")), GetDimension(_T("border")));
56d2f750
VS
125 return NULL;
126 }
127
56d2f750 128
56d2f750
VS
129 else {
130 wxSizer *sizer = NULL;
131
e066e256 132 wxXmlNode *parentNode = m_Node->GetParent();
56d2f750
VS
133
134 wxCHECK_MSG(m_ParentSizer != NULL ||
e066e256
VS
135 ((IsOfClass(parentNode, _T("wxPanel")) ||
136 IsOfClass(parentNode, _T("wxDialog"))) &&
56d2f750 137 parentNode->GetType() == wxXML_ELEMENT_NODE), NULL,
e066e256 138 _T("Incorrect use of sizer: parent is not 'wxDialog' or 'wxPanel'."));
56d2f750 139
e066e256 140 if (m_Class == _T("wxBoxSizer"))
56d2f750
VS
141 sizer = new wxBoxSizer(GetStyle(_T("orient"), wxHORIZONTAL));
142
e066e256 143 else if (m_Class == _T("wxStaticBoxSizer"))
56d2f750
VS
144 {
145 sizer = new wxStaticBoxSizer(
146 new wxStaticBox(m_ParentAsWindow, -1, GetText(_T("label"))),
147 GetStyle(_T("orient"), wxHORIZONTAL));
148 }
fccd6cdc 149
e066e256 150 else if (m_Class == _T("wxGridSizer"))
fccd6cdc 151 sizer = new wxGridSizer(GetLong(_T("rows")), GetLong(_T("cols")),
bebb14d5 152 GetDimension(_T("vgap")), GetDimension(_T("hgap")));
fccd6cdc 153
e066e256 154 else if (m_Class == _T("wxFlexGridSizer"))
fccd6cdc 155 sizer = new wxFlexGridSizer(GetLong(_T("rows")), GetLong(_T("cols")),
bebb14d5 156 GetDimension(_T("vgap")), GetDimension(_T("hgap")));
56d2f750 157
266527ec
VS
158 wxSize minsize = GetSize(_T("minsize"));
159 if (!(minsize == wxDefaultSize))
160 sizer->SetMinSize(minsize);
161
162
56d2f750
VS
163 wxSizer *old_par = m_ParentSizer;
164 m_ParentSizer = sizer;
165 bool old_ins = m_IsInside;
166 m_IsInside = TRUE;
167 CreateChildren(m_Parent, TRUE/*only this handler*/);
168 m_IsInside = old_ins;
169 m_ParentSizer = old_par;
170
171 if (m_ParentSizer == NULL) // setup window:
172 {
173 m_ParentAsWindow->SetAutoLayout(TRUE);
174 m_ParentAsWindow->SetSizer(sizer);
175
176 wxXmlNode *nd = m_Node;
177 m_Node = parentNode;
178 if (GetSize() == wxDefaultSize)
179 sizer->Fit(m_ParentAsWindow);
180 m_Node = nd;
181
182 if (m_ParentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
183 sizer->SetSizeHints(m_ParentAsWindow);
184 }
185
186 return sizer;
187 }
188}
189
190
191
192bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
193{
0bed1cee 194 return ((!m_IsInside && IsSizerNode(node)) ||
e066e256
VS
195 (m_IsInside && IsOfClass(node, _T("sizeritem"))) ||
196 (m_IsInside && IsOfClass(node, _T("spacer"))));
56d2f750 197}