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