]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/prophnd.cpp
workarounded false memory leak report
[wxWidgets.git] / contrib / utils / wxrcedit / prophnd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Author: Vaclav Slavik
3 // Created: 2000/05/05
4 // RCS-ID: $Id$
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifdef __GNUG__
10 #pragma implementation "prophnd.h"
11 #endif
12
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "prophnd.h"
21 #include "wx/xml/xml.h"
22 #include "wx/wx.h"
23 #include "wx/arrimpl.cpp"
24 #include "wx/valtext.h"
25 #include "wx/tokenzr.h"
26 #include "wx/checklst.h"
27 #include "wx/listctrl.h"
28 #include "xmlhelpr.h"
29 #include "editor.h"
30
31
32 WX_DEFINE_OBJARRAY(PropertyInfoArray);
33
34
35 enum
36 {
37 ID_EDITCTRL = wxID_HIGHEST + 1000,
38 ID_XEDIT,
39 ID_YEDIT,
40 ID_USEDLG,
41 ID_BOOLVAL,
42 ID_CHECKLIST
43 };
44
45
46
47 class PropertyPanel : public wxPanel
48 {
49 public:
50 PropertyPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli)
51 : wxPanel(parent, -1), m_Handler(hnd), m_PLI(pli) {}
52
53 void Update(const wxString& value)
54 {
55 XmlWriteValue(m_PLI->m_Node, m_PLI->m_PropInfo->Name, value);
56 m_PLI->m_ListCtrl->SetItemImage(m_PLI->m_Index, 1, 1);
57 m_PLI->m_ListCtrl->SetItem(m_PLI->m_Index, 1,
58 m_Handler->GetBriefValue(m_PLI->m_Node, m_PLI->m_PropInfo));
59 }
60
61 protected:
62 PropertyHandler *m_Handler;
63 PropsListInfo *m_PLI;
64 };
65
66
67
68
69
70 int PropertyHandler::CreateListItem(wxListCtrl *listctrl, wxXmlNode *node, PropertyInfo *pi)
71 {
72 wxString name, value;
73 int iconnum;
74
75 if (XmlFindNode(node, pi->Name) == NULL) iconnum = 0; else iconnum = 1;
76 name = pi->Name;
77 value = GetBriefValue(node, pi);
78
79 long pos = listctrl->GetItemCount();
80 listctrl->InsertItem(pos, name, iconnum);
81 listctrl->SetItem(pos, 1, value);
82 return pos;
83 }
84
85
86
87 wxString PropertyHandler::GetBriefValue(wxXmlNode *node, PropertyInfo *pi)
88 {
89 return XmlReadValue(node, pi->Name);
90 }
91
92
93
94
95
96
97 class TextPropPanel : public PropertyPanel
98 {
99 public:
100 TextPropPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli) : PropertyPanel(parent, hnd, pli)
101 {
102 wxSizer *sz = new wxBoxSizer(wxVERTICAL);
103 wxTextCtrl *tc;
104
105 sz->Add(new wxStaticText(this, -1, _("Value:")), 0, wxLEFT, 5);
106 sz->Add(tc = new wxTextCtrl(this, ID_EDITCTRL, XmlReadValue(pli->m_Node, pli->m_PropInfo->Name)), 0, wxALL|wxEXPAND, 5);
107 tc->SetFocus();
108
109 SetAutoLayout(TRUE);
110 SetSizer(sz);
111 Layout();
112 }
113
114 void OnEdit(wxCommandEvent &event)
115 {
116 Update(((wxTextCtrl*)event.GetEventObject())->GetValue());
117 }
118
119 DECLARE_EVENT_TABLE()
120 };
121
122 BEGIN_EVENT_TABLE(TextPropPanel, PropertyPanel)
123 EVT_TEXT(ID_EDITCTRL, TextPropPanel::OnEdit)
124 END_EVENT_TABLE()
125
126
127 wxPanel *TextPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
128 {
129 return new TextPropPanel(parent, this, pli);
130 }
131
132
133
134
135
136
137
138
139
140
141
142 class CoordPropPanel : public PropertyPanel
143 {
144 public:
145 CoordPropPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli) : PropertyPanel(parent, hnd, pli)
146 {
147 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
148 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
149 m_ed1 = m_ed2 = NULL; m_chb = NULL;
150
151 sz->Add(new wxStaticText(this, -1, _("X:")), 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
152 sz->Add(m_ed1 = new wxTextCtrl(this, ID_XEDIT, "",
153 wxDefaultPosition, wxDefaultSize, 0,
154 wxTextValidator(wxFILTER_NUMERIC)),
155 1, wxRIGHT, 5);
156 m_ed1->SetFocus();
157
158 sz->Add(new wxStaticText(this, -1, _("Y:")), 0, wxLEFT|wxRIGHT|wxALIGN_CENTER, 5);
159 sz->Add(m_ed2 = new wxTextCtrl(this, ID_YEDIT, "",
160 wxDefaultPosition, wxDefaultSize, 0,
161 wxTextValidator(wxFILTER_NUMERIC)),
162 1, wxRIGHT, 5);
163 sizer->Add(sz, 0, wxEXPAND|wxTOP, 5);
164
165 sizer->Add(m_chb = new wxCheckBox(this, ID_USEDLG, _("Use dialog units")), 0, wxLEFT|wxTOP, 5);
166
167 SetAutoLayout(TRUE);
168 SetSizer(sizer);
169 Layout();
170
171 wxString val = XmlReadValue(pli->m_Node, pli->m_PropInfo->Name);
172 m_chb->SetValue(val.Len()==0 || val[val.Len()-1] == 'd');
173 m_ed1->SetValue(val.BeforeFirst(','));
174 m_ed2->SetValue(val.AfterFirst(',').BeforeFirst('d'));
175 }
176
177 void OnEdit(wxCommandEvent &event)
178 {
179 wxString val, v1, v2;
180
181 if (m_ed1 == NULL || m_ed2 == NULL || m_chb == NULL) return;
182
183 v1 = m_ed1->GetValue();
184 v2 = m_ed2->GetValue();
185 if (v1.IsEmpty() || v2.IsEmpty()) return;
186 val = v1 + "," + v2;
187 if (m_chb->GetValue()) val << 'd';
188 Update(val);
189 }
190
191 wxTextCtrl *m_ed1, *m_ed2;
192 wxCheckBox *m_chb;
193
194 DECLARE_EVENT_TABLE()
195 };
196
197 BEGIN_EVENT_TABLE(CoordPropPanel, PropertyPanel)
198 EVT_TEXT(ID_XEDIT, CoordPropPanel::OnEdit)
199 EVT_TEXT(ID_YEDIT, CoordPropPanel::OnEdit)
200 EVT_CHECKBOX(ID_USEDLG, CoordPropPanel::OnEdit)
201 END_EVENT_TABLE()
202
203 wxPanel *CoordPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
204 {
205 return new CoordPropPanel(parent, this, pli);
206 }
207
208
209
210
211
212
213
214 class DimensionPropPanel : public PropertyPanel
215 {
216 public:
217 DimensionPropPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli) : PropertyPanel(parent, hnd, pli)
218 {
219 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
220 m_ed1 = NULL; m_chb = NULL;
221
222 sizer->Add(new wxStaticText(this, -1, _("Value:")), 0, wxLEFT, 5);
223 sizer->Add(m_ed1 = new wxTextCtrl(this, ID_XEDIT, "",
224 wxDefaultPosition, wxDefaultSize, 0,
225 wxTextValidator(wxFILTER_NUMERIC)),
226 0, wxEXPAND, 5);
227 m_ed1->SetFocus();
228
229 sizer->Add(m_chb = new wxCheckBox(this, ID_USEDLG, _("Use dialog units")), 0, wxLEFT|wxTOP, 5);
230
231 SetAutoLayout(TRUE);
232 SetSizer(sizer);
233 Layout();
234
235 wxString val = XmlReadValue(pli->m_Node, pli->m_PropInfo->Name);
236 m_chb->SetValue(val.Len()>0 && val[val.Len()-1] == 'd');
237 m_ed1->SetValue(val.BeforeFirst('d'));
238 }
239
240 void OnEdit(wxCommandEvent &event)
241 {
242 wxString val;
243
244 if (m_ed1 == NULL || m_chb == NULL) return;
245
246 val = m_ed1->GetValue();
247 if (val.IsEmpty()) return;
248 if (m_chb->GetValue()) val << 'd';
249 Update(val);
250 }
251
252 wxTextCtrl *m_ed1;
253 wxCheckBox *m_chb;
254
255 DECLARE_EVENT_TABLE()
256 };
257
258 BEGIN_EVENT_TABLE(DimensionPropPanel, PropertyPanel)
259 EVT_TEXT(ID_XEDIT, DimensionPropPanel::OnEdit)
260 EVT_TEXT(ID_YEDIT, DimensionPropPanel::OnEdit)
261 EVT_CHECKBOX(ID_USEDLG, DimensionPropPanel::OnEdit)
262 END_EVENT_TABLE()
263
264 wxPanel *DimensionPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
265 {
266 return new DimensionPropPanel(parent, this, pli);
267 }
268
269
270
271
272
273
274
275 class BoolPropPanel : public PropertyPanel
276 {
277 public:
278 BoolPropPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli) : PropertyPanel(parent, hnd, pli)
279 {
280 m_chb = NULL;
281 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
282 sizer->Add(m_chb = new wxCheckBox(this, ID_BOOLVAL, _("On/Yes/True")), 0, wxLEFT|wxTOP, 5);
283 SetAutoLayout(TRUE);
284 SetSizer(sizer);
285 Layout();
286 m_chb->SetValue(XmlReadValue(pli->m_Node, pli->m_PropInfo->Name) == "1");
287 }
288
289 void OnEdit(wxCommandEvent &event)
290 {
291 if (m_chb == NULL) return;
292 if (m_chb->GetValue()) Update("1");
293 else Update("0");
294 }
295
296 wxCheckBox *m_chb;
297
298 DECLARE_EVENT_TABLE()
299 };
300
301 BEGIN_EVENT_TABLE(BoolPropPanel, PropertyPanel)
302 EVT_CHECKBOX(ID_BOOLVAL, BoolPropPanel::OnEdit)
303 END_EVENT_TABLE()
304
305 wxPanel *BoolPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
306 {
307 return new BoolPropPanel(parent, this, pli);
308 }
309
310 wxString BoolPropertyHandler::GetBriefValue(wxXmlNode *node, PropertyInfo *pi)
311 {
312 wxString v = XmlReadValue(node, pi->Name);
313 if (v.IsEmpty()) return wxEmptyString;
314 else if (v == "1") return "true";
315 else return "false";
316 }
317
318
319
320
321
322
323
324
325 class FlagsPropPanel : public PropertyPanel
326 {
327 public:
328 FlagsPropPanel(wxWindow *parent, PropertyHandler *hnd, PropsListInfo *pli) : PropertyPanel(parent, hnd, pli)
329 {
330 m_chl = NULL;
331 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
332 sizer->Add(m_chl = new wxCheckListBox(this, ID_CHECKLIST), 1, wxEXPAND|wxALL, 5);
333 SetAutoLayout(TRUE);
334 SetSizer(sizer);
335 Layout();
336
337 {
338 wxStringTokenizer tkn(pli->m_PropInfo->MoreInfo, ",");
339 wxString s;
340 while (tkn.HasMoreTokens())
341 {
342 s = tkn.GetNextToken();
343 m_chl->Append(s);
344 m_flags.Add(s);
345 }
346 }
347
348 {
349 wxStringTokenizer tkn(XmlReadValue(pli->m_Node, pli->m_PropInfo->Name), "| ");
350 int index;
351 while (tkn.HasMoreTokens())
352 {
353 index = m_flags.Index(tkn.GetNextToken());
354 if (index != wxNOT_FOUND)
355 m_chl->Check(index);
356 }
357 }
358 }
359
360 void OnEdit(wxCommandEvent &event)
361 {
362 wxString s;
363 bool first = TRUE;
364
365 for (size_t i = 0; i < m_flags.GetCount(); i++)
366 {
367 if (m_chl->IsChecked(i))
368 {
369 if (!first) s << '|';
370 s << m_flags[i];
371 first = FALSE;
372 }
373 }
374 Update(s);
375 if (m_PLI->m_PropInfo->Name == "orient")
376 // FIXME - dirty hack related to sizers
377 EditorFrame::Get()->NotifyChanged(CHANGED_TREE_SELECTED_ICON);
378 }
379
380 wxCheckListBox *m_chl;
381 wxArrayString m_flags;
382
383 DECLARE_EVENT_TABLE()
384 };
385
386 BEGIN_EVENT_TABLE(FlagsPropPanel, PropertyPanel)
387 EVT_CHECKLISTBOX(ID_CHECKLIST, FlagsPropPanel::OnEdit)
388 END_EVENT_TABLE()
389
390 wxPanel *FlagsPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
391 {
392 return new FlagsPropPanel(parent, this, pli);
393 }
394
395
396
397
398 wxPanel *NotImplPropertyHandler::CreateEditPanel(wxWindow *parent, PropsListInfo *pli)
399 {
400 wxPanel *p = new wxPanel(parent);
401 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
402 sizer->Add(new wxStaticText(p, -1, _("Sorry, this is not supported.\nYou have to edit XML code directly.")), 1, wxEXPAND|wxALL, 5);
403 p->SetAutoLayout(TRUE);
404 p->SetSizer(sizer);
405 p->Layout();
406 return p;
407 }