]> git.saurik.com Git - wxWidgets.git/blob - contrib/utils/wxrcedit/pe_basic.cpp
implemented IsModified() and DiscardEdits()
[wxWidgets.git] / contrib / utils / wxrcedit / pe_basic.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 "pe_basic.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 "wx/wx.h"
21 #include "wx/xrc/xml.h"
22 #include "wx/tokenzr.h"
23 #include "pe_basic.h"
24 #include "xmlhelpr.h"
25 #include "editor.h"
26 #include "preview.h"
27 #include "nodehnd.h"
28
29
30 BEGIN_EVENT_TABLE(PropEditCtrlTxt, PropEditCtrl)
31 EVT_TEXT(-1, PropEditCtrlTxt::OnText)
32 END_EVENT_TABLE()
33
34
35 wxWindow *PropEditCtrlTxt::CreateEditCtrl()
36 {
37 return (m_TextCtrl = new wxTextCtrl(this, -1));
38 }
39
40
41
42 void PropEditCtrlTxt::OnText(wxCommandEvent& event)
43 {
44 if (CanSave())
45 {
46 WriteValue();
47 EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
48 }
49 }
50
51
52
53 void PropEditCtrlTxt::ReadValue()
54 {
55 m_TextCtrl->SetValue(XmlReadValue(GetNode(), m_PropInfo->Name));
56 }
57
58
59
60 void PropEditCtrlTxt::WriteValue()
61 {
62 wxString newv = m_TextCtrl->GetValue();
63 XmlWriteValue(GetNode(), m_PropInfo->Name, newv);
64 m_TreeCtrl->SetItemBold(m_TreeItem, TRUE);
65 }
66
67
68
69 wxWindow *PropEditCtrlInt::CreateEditCtrl()
70 {
71 PropEditCtrlTxt::CreateEditCtrl();
72 #if wxUSE_VALIDATORS
73 m_TextCtrl->SetValidator(wxTextValidator(wxFILTER_NUMERIC));
74 #endif
75 return m_TextCtrl;
76 }
77
78
79
80
81
82
83
84 BEGIN_EVENT_TABLE(PropEditCtrlBool, PropEditCtrl)
85 EVT_CHOICE(-1, PropEditCtrlBool::OnChoice)
86 END_EVENT_TABLE()
87
88 wxWindow *PropEditCtrlBool::CreateEditCtrl()
89 {
90 m_Choice = new wxChoice(this, -1);
91 m_Choice->Append(_T("false"));
92 m_Choice->Append(_T("true"));
93 return m_Choice;
94 }
95
96
97
98 void PropEditCtrlBool::ReadValue()
99 {
100 int sel;
101 if (XmlReadValue(GetNode(), m_PropInfo->Name) == _T("0")) sel = 0;
102 else sel = 1;
103 m_Choice->SetSelection(sel);
104 }
105
106
107
108 void PropEditCtrlBool::WriteValue()
109 {
110 wxString newv = m_Choice->GetSelection() == 0 ? _T("0") : _T("1");
111
112 XmlWriteValue(GetNode(), m_PropInfo->Name, newv);
113 m_TreeCtrl->SetItemBold(m_TreeItem, TRUE);
114 }
115
116
117
118 wxString PropEditCtrlBool::GetValueAsText(wxTreeItemId ti)
119 {
120 PropertyInfo& pir = ((PETreeData*)m_TreeCtrl->GetItemData(ti))->PropInfo;
121 wxString x = XmlReadValue(GetNode(), pir.Name);
122 if (x == _T("1")) x = _T("true");
123 else if (x == _T("0")) x = _T("false");
124 return x;
125 }
126
127
128
129 void PropEditCtrlBool::OnChoice(wxCommandEvent& event)
130 {
131 if (CanSave())
132 {
133 WriteValue();
134 EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
135 }
136 }
137
138
139
140
141 // --------------------------- PropEditCtrlCoord -----------------------
142
143 class PropEditCtrlCoordXY : public PropEditCtrlInt
144 {
145 public:
146 PropEditCtrlCoordXY(PropertiesFrame *propFrame, int which)
147 : PropEditCtrlInt(propFrame), m_which(which) {}
148
149 virtual void ReadValue()
150 {
151 wxString s = XmlReadValue(GetNode(), m_PropInfo->Name);
152 if (s.IsEmpty())
153 {
154 m_c[0] = m_c[1] = _T("-1");
155 }
156 else
157 {
158 wxStringTokenizer tkn(s.BeforeFirst(_T('d')), _T(","));
159 m_c[0] = tkn.GetNextToken();
160 m_c[1] = tkn.GetNextToken();
161 }
162 m_TextCtrl->SetValue(m_c[m_which]);
163 }
164
165 virtual void WriteValue()
166 {
167 m_c[m_which] = m_TextCtrl->GetValue();
168 if (m_c[0].IsEmpty()) m_c[0] = _T("-1");
169 if (m_c[1].IsEmpty()) m_c[1] = _T("-1");
170 wxString s;
171 s << m_c[0] << _T(',') << m_c[1];
172 wxString prev = XmlReadValue(GetNode(), m_PropInfo->Name);
173 if (prev[prev.Len()-1] == _T('d')) s << _T('d');
174 XmlWriteValue(GetNode(), m_PropInfo->Name, s);
175 m_TreeCtrl->SetItemBold(m_TreeCtrl->GetItemParent(m_TreeItem), TRUE);
176 }
177
178 virtual wxString GetValueAsText(wxTreeItemId ti)
179 {
180 PropertyInfo *pi = &(((PETreeData*)m_TreeCtrl->GetItemData(ti))->PropInfo);
181 wxString s = XmlReadValue(GetNode(), pi->Name);
182 if (s.IsEmpty())
183 {
184 m_c[0] = m_c[1] = _T("-1");
185 }
186 else
187 {
188 wxStringTokenizer tkn(s.BeforeFirst(_T('d')), _T(","));
189 m_c[0] = tkn.GetNextToken();
190 m_c[1] = tkn.GetNextToken();
191 }
192 return m_c[m_which];
193 }
194
195 virtual wxString GetPropName(const PropertyInfo& pinfo)
196 {
197 if (m_which == 0) return _T("x"); else return _T("y");
198 }
199
200 virtual bool HasClearButton() { return FALSE; }
201 virtual bool IsPresent(const PropertyInfo& pinfo) { return FALSE; }
202
203
204 protected:
205 wxString m_c[2];
206 int m_which;
207 };
208
209
210 class PropEditCtrlCoordDlg : public PropEditCtrlBool
211 {
212 public:
213 PropEditCtrlCoordDlg(PropertiesFrame *propFrame)
214 : PropEditCtrlBool(propFrame) {}
215
216 virtual void ReadValue()
217 {
218 wxString s = XmlReadValue(GetNode(), m_PropInfo->Name);
219 if (s.IsEmpty()) m_Choice->SetSelection(1);
220 else if (s[s.Length()-1] == _T('d'))
221 m_Choice->SetSelection(1);
222 else
223 m_Choice->SetSelection(0);
224 }
225
226 virtual void WriteValue()
227 {
228 wxString s = XmlReadValue(GetNode(), m_PropInfo->Name).BeforeFirst(_T('d'));
229 if (m_Choice->GetSelection() == 1) s << _T('d');
230 XmlWriteValue(GetNode(), m_PropInfo->Name, s);
231 m_TreeCtrl->SetItemBold(m_TreeCtrl->GetItemParent(m_TreeItem), TRUE);
232 }
233
234 virtual wxString GetValueAsText(wxTreeItemId ti)
235 {
236 PropertyInfo *pi = &(((PETreeData*)m_TreeCtrl->GetItemData(ti))->PropInfo);
237 wxString s = XmlReadValue(GetNode(), pi->Name);
238 if (s.IsEmpty())
239 return _("true");
240 else if (s[s.Length()-1] != _T('d'))
241 return _("false");
242 else
243 return _("true");
244 }
245
246 virtual wxString GetPropName(const PropertyInfo& pinfo)
247 {
248 return _T("dlg");
249 }
250
251 virtual bool HasClearButton() { return FALSE; }
252 virtual bool IsPresent(const PropertyInfo& pinfo) { return FALSE; }
253 };
254
255
256
257
258 PropEditCtrlCoord::PropEditCtrlCoord(PropertiesFrame *propFrame)
259 : PropEditCtrlTxt(propFrame)
260 {
261 m_CtrlX = new PropEditCtrlCoordXY(propFrame, 0);
262 m_CtrlY = new PropEditCtrlCoordXY(propFrame, 1);
263 m_CtrlDlg = new PropEditCtrlCoordDlg(propFrame);
264 }
265
266 PropEditCtrlCoord::~PropEditCtrlCoord()
267 {
268 delete m_CtrlX;
269 delete m_CtrlY;
270 delete m_CtrlDlg;
271 }
272
273
274
275 wxTreeItemId PropEditCtrlCoord::CreateTreeEntry(wxTreeItemId parent, const PropertyInfo& pinfo)
276 {
277 wxTreeItemId id = PropEditCtrlTxt::CreateTreeEntry(parent, pinfo);
278 m_CtrlX->CreateTreeEntry(id, PropertyInfo(_T("text"), pinfo.Name, _T("")));
279 m_CtrlY->CreateTreeEntry(id, PropertyInfo(_T("text"), pinfo.Name, _T("")));
280 m_CtrlDlg->CreateTreeEntry(id, PropertyInfo(_T("bool"), pinfo.Name, _T("")));
281 return id;
282 }
283
284
285
286
287
288
289 // --------------------------- PropEditCtrlDim -----------------------
290
291 class PropEditCtrlDimX : public PropEditCtrlInt
292 {
293 public:
294 PropEditCtrlDimX(PropertiesFrame *propFrame)
295 : PropEditCtrlInt(propFrame){}
296
297 virtual void ReadValue()
298 {
299 wxString s = XmlReadValue(GetNode(), m_PropInfo->Name);
300 m_c = s.BeforeFirst(_T('d'));
301 m_TextCtrl->SetValue(m_c);
302 }
303
304 virtual void WriteValue()
305 {
306 wxString s = XmlReadValue(GetNode(), m_PropInfo->Name);
307 bool dlg = !s.IsEmpty() && s[s.Length()-1] == _T('d');
308 m_c = m_TextCtrl->GetValue();
309 s = m_c;
310 if (dlg) s << _T('d');
311 XmlWriteValue(GetNode(), m_PropInfo->Name, s);
312 m_TreeCtrl->SetItemBold(m_TreeCtrl->GetItemParent(m_TreeItem), TRUE);
313 }
314
315 virtual wxString GetValueAsText(wxTreeItemId ti)
316 {
317 PropertyInfo *pi = &(((PETreeData*)m_TreeCtrl->GetItemData(ti))->PropInfo);
318 return XmlReadValue(GetNode(), pi->Name).BeforeFirst(_T('d'));
319 }
320
321 virtual wxString GetPropName(const PropertyInfo& pinfo)
322 {
323 return _T("val");
324 }
325
326 virtual bool HasClearButton() { return FALSE; }
327 virtual bool IsPresent(const PropertyInfo& pinfo) { return FALSE; }
328
329
330 protected:
331 wxString m_c;
332 };
333
334
335
336 PropEditCtrlDim::PropEditCtrlDim(PropertiesFrame *propFrame)
337 : PropEditCtrlTxt(propFrame)
338 {
339 m_CtrlX = new PropEditCtrlDimX(propFrame);
340 m_CtrlDlg = new PropEditCtrlCoordDlg(propFrame);
341 }
342
343 PropEditCtrlDim::~PropEditCtrlDim()
344 {
345 delete m_CtrlX;
346 delete m_CtrlDlg;
347 }
348
349
350
351 wxTreeItemId PropEditCtrlDim::CreateTreeEntry(wxTreeItemId parent, const PropertyInfo& pinfo)
352 {
353 wxTreeItemId id = PropEditCtrlTxt::CreateTreeEntry(parent, pinfo);
354 m_CtrlX->CreateTreeEntry(id, PropertyInfo(_T("text"), pinfo.Name, _T("")));
355 m_CtrlDlg->CreateTreeEntry(id, PropertyInfo(_T("bool"), pinfo.Name, _T("")));
356 return id;
357 }
358
359
360
361
362 // --------------------- PropEditCtrlXRCID -----------------------------
363
364 #define REAL_NODE (NodeHandler::Find(GetNode())->GetRealNode(GetNode()))
365
366
367 void PropEditCtrlXRCID::ReadValue()
368 {
369 m_TextCtrl->SetValue(REAL_NODE->GetPropVal(_T("name"), wxEmptyString));
370 }
371
372
373
374 void PropEditCtrlXRCID::WriteValue()
375 {
376 wxString s =m_TextCtrl->GetValue();
377 if (s.IsEmpty()) s = _T("-1");
378
379 REAL_NODE->DeleteProperty(_T("name"));
380 REAL_NODE->AddProperty(_T("name"), s);
381
382 m_TreeCtrl->SetItemBold(m_TreeItem, TRUE);
383 EditorFrame::Get()->NotifyChanged(CHANGED_TREE_SELECTED);
384 }
385
386
387
388 void PropEditCtrlXRCID::Clear()
389 {
390 EndEdit();
391 REAL_NODE->DeleteProperty(_T("name"));
392 m_TreeCtrl->SetItemBold(m_TreeItem, FALSE);
393 EditorFrame::Get()->NotifyChanged(CHANGED_TREE_SELECTED);
394 }
395
396
397
398 void PropEditCtrlXRCID::OnDetails()
399 {
400 wxString choices[] = {wxString(_T("-1"))
401 #define stdID(id) , wxString(#id)
402 stdID(wxID_OK) stdID(wxID_CANCEL)
403 stdID(wxID_YES) stdID(wxID_NO)
404 stdID(wxID_APPLY) stdID(wxID_HELP)
405 stdID(wxID_HELP_CONTEXT)
406
407 stdID(wxID_OPEN) stdID(wxID_CLOSE) stdID(wxID_NEW)
408 stdID(wxID_SAVE) stdID(wxID_SAVEAS) stdID(wxID_REVERT)
409 stdID(wxID_EXIT) stdID(wxID_UNDO) stdID(wxID_REDO)
410 stdID(wxID_PRINT) stdID(wxID_PRINT_SETUP)
411 stdID(wxID_PREVIEW) stdID(wxID_ABOUT) stdID(wxID_HELP_CONTENTS)
412 stdID(wxID_HELP_COMMANDS) stdID(wxID_HELP_PROCEDURES)
413 stdID(wxID_CUT) stdID(wxID_COPY) stdID(wxID_PASTE)
414 stdID(wxID_CLEAR) stdID(wxID_FIND) stdID(wxID_DUPLICATE)
415 stdID(wxID_SELECTALL)
416 stdID(wxID_STATIC) stdID(wxID_FORWARD) stdID(wxID_BACKWARD)
417 stdID(wxID_DEFAULT) stdID(wxID_MORE) stdID(wxID_SETUP)
418 stdID(wxID_RESET)
419 #undef stdID
420 };
421
422 wxString s =
423 wxGetSingleChoice(_("Choose from predefined IDs:"), _("XRCID"),
424 38/*sizeof choices*/, choices);
425 if (!s) return;
426 m_TextCtrl->SetValue(s);
427 WriteValue();
428 EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
429 }
430
431
432
433 wxString PropEditCtrlXRCID::GetValueAsText(wxTreeItemId ti)
434 {
435 return REAL_NODE->GetPropVal(_T("name"), wxEmptyString);
436 }
437
438
439
440 bool PropEditCtrlXRCID::IsPresent(const PropertyInfo& pinfo)
441 {
442 return REAL_NODE->HasProp(_T("name"));
443 }
444
445 #undef REAL_NODE
446