]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/xh_sizer.cpp
documented EVT_LIST_ITEM_RIGHT_CLICK
[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 static bool IsSizerNode(wxXmlNode *node)
29 {
30 return (node->GetName() == _T("boxsizer")) ||
31 (node->GetName() == _T("staticboxsizer")) ||
32 (node->GetName() == _T("gridsizer")) ||
33 (node->GetName() == _T("flexgridsizer"));
34 }
35
36
37
38 wxSizerXmlHandler::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
74 wxObject *wxSizerXmlHandler::DoCreateResource()
75 {
76 if (m_Node->GetName() == _T("sizeritem"))
77 {
78 wxXmlNode *n = GetParamNode(_T("window"))->GetChildren();
79
80 while (n)
81 {
82 if (n->GetType() == wxXML_ELEMENT_NODE)
83 {
84 bool old_ins = m_IsInside;
85 wxSizer *old_par = m_ParentSizer;
86 m_IsInside = FALSE;
87 if (!IsSizerNode(n)) m_ParentSizer = NULL;
88 wxObject *item = CreateResFromNode(n, m_Parent, NULL);
89 m_IsInside = old_ins;
90 m_ParentSizer = old_par;
91 wxSizer *sizer = wxDynamicCast(item, wxSizer);
92 wxWindow *wnd = wxDynamicCast(item, wxWindow);
93 wxSize minsize = GetSize(_T("minsize"));
94
95 if (sizer)
96 {
97 m_ParentSizer->Add(sizer, GetLong(_T("option")),
98 GetStyle(_T("flag")), GetDimension(_T("border")));
99 if (!(minsize == wxDefaultSize))
100 m_ParentSizer->SetItemMinSize(sizer, minsize.x, minsize.y);
101 }
102 else if (wnd)
103 {
104 m_ParentSizer->Add(wnd, GetLong(_T("option")),
105 GetStyle(_T("flag")), GetDimension(_T("border")));
106 if (!(minsize == wxDefaultSize))
107 m_ParentSizer->SetItemMinSize(wnd, minsize.x, minsize.y);
108 }
109 else
110 wxLogError(_T("Error in resource."));
111
112 return item;
113 }
114 n = n->GetNext();
115 }
116 wxLogError(_T("Error in resource: no control/sizer within sizer's <item> tag."));
117 return NULL;
118 }
119
120 else if (m_Node->GetName() == _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()->GetParent();
134
135 wxCHECK_MSG(m_ParentSizer != NULL ||
136 ((parentNode->GetName() == _T("panel") ||
137 parentNode->GetName() == _T("dialog")) &&
138 parentNode->GetType() == wxXML_ELEMENT_NODE), NULL,
139 _T("Incorrect use of sizer: parent is not 'dialog' or 'panel'."));
140
141 if (m_Node->GetName() == _T("boxsizer"))
142 sizer = new wxBoxSizer(GetStyle(_T("orient"), wxHORIZONTAL));
143
144 else if (m_Node->GetName() == _T("staticboxsizer"))
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_Node->GetName() == _T("gridsizer"))
152 sizer = new wxGridSizer(GetLong(_T("rows")), GetLong(_T("cols")),
153 GetDimension(_T("vgap")), GetDimension(_T("hgap")));
154
155 else if (m_Node->GetName() == _T("flexgridsizer"))
156 sizer = new wxFlexGridSizer(GetLong(_T("rows")), GetLong(_T("cols")),
157 GetDimension(_T("vgap")), GetDimension(_T("hgap")));
158
159 wxSize minsize = GetSize(_T("minsize"));
160 if (!(minsize == wxDefaultSize))
161 sizer->SetMinSize(minsize);
162
163
164 wxSizer *old_par = m_ParentSizer;
165 m_ParentSizer = sizer;
166 bool old_ins = m_IsInside;
167 m_IsInside = TRUE;
168 CreateChildren(m_Parent, TRUE/*only this handler*/);
169 m_IsInside = old_ins;
170 m_ParentSizer = old_par;
171
172 if (m_ParentSizer == NULL) // setup window:
173 {
174 m_ParentAsWindow->SetAutoLayout(TRUE);
175 m_ParentAsWindow->SetSizer(sizer);
176
177 wxXmlNode *nd = m_Node;
178 m_Node = parentNode;
179 if (GetSize() == wxDefaultSize)
180 sizer->Fit(m_ParentAsWindow);
181 m_Node = nd;
182
183 if (m_ParentAsWindow->GetWindowStyle() & (wxRESIZE_BOX | wxRESIZE_BORDER))
184 sizer->SetSizeHints(m_ParentAsWindow);
185 }
186
187 return sizer;
188 }
189 }
190
191
192
193 bool wxSizerXmlHandler::CanHandle(wxXmlNode *node)
194 {
195 return ((!m_IsInside && IsSizerNode(node)) ||
196 (m_IsInside && node->GetName() == _T("sizeritem")) ||
197 (m_IsInside && node->GetName() == _T("spacer")));
198 }