]> git.saurik.com Git - wxWidgets.git/blob - utils/configtool/src/configtoolview.cpp
c9549799ab24e386aa26606d9667cb11d49447e1
[wxWidgets.git] / utils / configtool / src / configtoolview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: configtoolview.cpp
3 // Purpose: View class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2003-06-04
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence:
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "configtoolview.h"
14 #endif
15
16 // Includes other headers for precompiled compilation
17 #include "wx/wx.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/wfstream.h"
24
25 #include "configtoolview.h"
26 #include "configtooldoc.h"
27 #include "configtree.h"
28 #include "wxconfigtool.h"
29 #include "mainframe.h"
30 #include "propeditor.h"
31 #include "utils.h"
32 #include "custompropertydialog.h"
33
34 IMPLEMENT_DYNAMIC_CLASS(ctConfigToolView, wxView)
35
36 BEGIN_EVENT_TABLE(ctConfigToolView, wxView)
37 EVT_MENU(ctID_ADD_ITEM_CHECKBOX, ctConfigToolView::OnAddCheckBoxItem)
38 EVT_MENU(ctID_ADD_ITEM_RADIOBUTTON, ctConfigToolView::OnAddRadioButtonItem)
39 EVT_MENU(ctID_ADD_ITEM_GROUP, ctConfigToolView::OnAddGroupItem)
40 EVT_MENU(ctID_ADD_ITEM_CHECK_GROUP, ctConfigToolView::OnAddCheckGroupItem)
41 EVT_MENU(ctID_ADD_ITEM_RADIO_GROUP, ctConfigToolView::OnAddRadioGroupItem)
42 EVT_MENU(ctID_ADD_ITEM_STRING, ctConfigToolView::OnAddStringItem)
43 EVT_MENU(ctID_RENAME_ITEM, ctConfigToolView::OnRenameItem)
44 EVT_MENU(ctID_DELETE_ITEM, ctConfigToolView::OnDeleteItem)
45
46 EVT_MENU(wxID_COPY, ctConfigToolView::OnCopy)
47 EVT_MENU(wxID_CUT, ctConfigToolView::OnCut)
48 EVT_MENU(wxID_PASTE, ctConfigToolView::OnPaste)
49
50 EVT_UPDATE_UI(wxID_COPY, ctConfigToolView::OnUpdateCopy)
51 EVT_UPDATE_UI(wxID_CUT, ctConfigToolView::OnUpdateCut)
52 EVT_UPDATE_UI(wxID_PASTE, ctConfigToolView::OnUpdatePaste)
53
54 EVT_MENU(ctID_ITEM_HELP, ctConfigToolView::OnItemHelp)
55
56 EVT_UPDATE_UI(ctID_ADD_ITEM_CHECKBOX, ctConfigToolView::OnUpdateAddItem)
57 EVT_UPDATE_UI(ctID_ADD_ITEM_RADIOBUTTON, ctConfigToolView::OnUpdateAddItem)
58 EVT_UPDATE_UI(ctID_ADD_ITEM_GROUP, ctConfigToolView::OnUpdateAddItem)
59 EVT_UPDATE_UI(ctID_ADD_ITEM_CHECK_GROUP, ctConfigToolView::OnUpdateAddItem)
60 EVT_UPDATE_UI(ctID_ADD_ITEM_RADIO_GROUP, ctConfigToolView::OnUpdateAddItem)
61 EVT_UPDATE_UI(ctID_ADD_ITEM_STRING, ctConfigToolView::OnUpdateAddItem)
62 EVT_UPDATE_UI(ctID_RENAME_ITEM, ctConfigToolView::OnUpdateCut)
63 EVT_UPDATE_UI(ctID_DELETE_ITEM, ctConfigToolView::OnUpdateCut)
64 EVT_UPDATE_UI(ctID_ITEM_HELP, ctConfigToolView::OnUpdateItemHelp)
65
66 EVT_MENU(ctID_TREE_COPY, ctConfigToolView::OnContextCopy)
67 EVT_MENU(ctID_TREE_CUT, ctConfigToolView::OnContextCut)
68 EVT_MENU(ctID_TREE_PASTE_BEFORE, ctConfigToolView::OnContextPasteBefore)
69 EVT_MENU(ctID_TREE_PASTE_AFTER, ctConfigToolView::OnContextPasteAfter)
70 EVT_MENU(ctID_TREE_PASTE_AS_CHILD, ctConfigToolView::OnContextPasteAsChild)
71 EVT_UPDATE_UI(ctID_TREE_COPY, ctConfigToolView::OnUpdateContextCopy)
72 EVT_UPDATE_UI(ctID_TREE_CUT, ctConfigToolView::OnUpdateContextCut)
73 EVT_UPDATE_UI(ctID_TREE_PASTE_BEFORE, ctConfigToolView::OnUpdateContextPasteBefore)
74 EVT_UPDATE_UI(ctID_TREE_PASTE_AFTER, ctConfigToolView::OnUpdateContextPasteAfter)
75 EVT_UPDATE_UI(ctID_TREE_PASTE_AS_CHILD, ctConfigToolView::OnUpdateContextPasteAsChild)
76
77 EVT_MENU(ctID_ADD_CUSTOM_PROPERTY, ctConfigToolView::OnAddCustomProperty)
78 EVT_MENU(ctID_EDIT_CUSTOM_PROPERTY, ctConfigToolView::OnEditCustomProperty)
79 EVT_MENU(ctID_DELETE_CUSTOM_PROPERTY, ctConfigToolView::OnDeleteCustomProperty)
80 EVT_UPDATE_UI(ctID_ADD_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateAddCustomProperty)
81 EVT_UPDATE_UI(ctID_EDIT_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateEditCustomProperty)
82 EVT_UPDATE_UI(ctID_DELETE_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateDeleteCustomProperty)
83
84 EVT_NOTEBOOK_PAGE_CHANGED(-1, ctConfigToolView::OnTabSelect)
85
86 EVT_MENU(ctID_SAVE_SETUP_FILE, ctConfigToolView::OnSaveSetupFile)
87 EVT_MENU(ctID_SAVE_CONFIGURE_COMMAND, ctConfigToolView::OnSaveConfigureCommand)
88 EVT_UPDATE_UI(ctID_SAVE_SETUP_FILE, ctConfigToolView::OnUpdateSaveSetupFile)
89 EVT_UPDATE_UI(ctID_SAVE_CONFIGURE_COMMAND, ctConfigToolView::OnUpdateSaveConfigureCommand)
90 END_EVENT_TABLE()
91
92 ctConfigToolView::ctConfigToolView()
93 {
94 }
95
96 // What to do when a view is created. Creates actual
97 // windows for displaying the view.
98 bool ctConfigToolView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
99 {
100 wxGetApp().GetDocManager()->ActivateView(this, TRUE);
101 wxGetApp().GetMainFrame()->SetDocument((ctConfigToolDoc*) doc);
102 wxGetApp().GetMainFrame()->GetSetupPage()->SetDocument((ctConfigToolDoc*) doc) ;
103 wxGetApp().GetMainFrame()->GetConfigurePage()->SetDocument((ctConfigToolDoc*) doc) ;
104
105 return TRUE;
106 }
107
108 void ctConfigToolView::OnDraw(wxDC *dc)
109 {
110 }
111
112 void ctConfigToolView::OnUpdate(wxView *WXUNUSED(sender), wxObject *hintObj)
113 {
114 ctConfigToolDoc* doc = wxDynamicCast(GetDocument(), ctConfigToolDoc);
115 ctConfigTreeCtrl* treeCtrl = wxGetApp().GetMainFrame()->GetConfigTreeCtrl();
116 if (!treeCtrl)
117 return;
118
119 wxASSERT (doc != NULL);
120
121 ctConfigItem* selItem = NULL;
122
123 wxTreeItemId sel = treeCtrl->GetSelection();
124 if (sel.IsOk())
125 {
126 ctTreeItemData* data = (ctTreeItemData*) treeCtrl->GetItemData(sel);
127 if (data)
128 selItem = data->GetConfigItem() ;
129 }
130
131 ctConfigToolHint* hint = (ctConfigToolHint*) hintObj;
132 int hintOp = ctNoHint;
133 if (hint)
134 hintOp = hint->m_op;
135
136 switch (hintOp)
137 {
138 case ctInitialUpdate:
139 {
140 if (doc && doc->GetTopItem())
141 {
142 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->DeleteAllItems();
143 AddItems(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), doc->GetTopItem());
144 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Expand(doc->GetTopItem()->GetTreeItemId());
145 }
146 }
147 break;
148 case ctSelChanged:
149 {
150 if (selItem)
151 {
152 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(selItem);
153 }
154 }
155 break;
156 case ctAllSaved:
157 {
158 // TODO: update windows and frame title
159 }
160 break;
161 case ctFilenameChanged:
162 {
163 wxGetApp().GetMainFrame()->UpdateFrameTitle();
164 }
165 break;
166 case ctClear:
167 {
168 // TODO: update windows
169 treeCtrl->DeleteAllItems();
170 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(NULL);
171 break;
172 }
173 case ctValueChanged:
174 {
175 // ctConfigItem& ti = *(ctConfigItem *)hint->m_item;
176 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(selItem);
177 }
178 break;
179
180 default:
181 break;
182 }
183 }
184
185 // Clean up windows used for displaying the view.
186 bool ctConfigToolView::OnClose(bool deleteWindow)
187 {
188 if (!GetDocument()->Close())
189 return FALSE;
190
191 ctConfigToolHint hint(NULL, ctClear);
192 GetDocument()->UpdateAllViews (NULL, & hint);
193
194 wxGetApp().GetDocManager()->ActivateView(this, FALSE);
195
196 Activate(FALSE);
197
198 wxGetApp().GetMainFrame()->SetDocument(NULL);
199 wxGetApp().GetMainFrame()->GetSetupPage()->SetDocument(NULL) ;
200 wxGetApp().GetMainFrame()->GetConfigurePage()->SetDocument(NULL) ;
201
202 return TRUE;
203 }
204
205 void ctConfigToolView::OnChangeFilename()
206 {
207 if (wxGetApp().GetTopWindow() && GetDocument())
208 {
209 wxString docTitle(wxFileNameFromPath(GetDocument()->GetFilename()));
210 wxStripExtension(docTitle);
211 GetDocument()->SetTitle(docTitle);
212
213 wxString name(GetDocument()->GetFilename());
214 wxStripExtension(name);
215
216 wxString title;
217
218 wxString modifiedMarker;
219 if (GetDocument()->IsModified())
220 modifiedMarker = wxT("*");
221
222 title = docTitle + modifiedMarker + wxString(wxT(" - ")) + wxGetApp().GetSettings().GetAppName() ;
223
224 ((wxFrame*) wxGetApp().GetTopWindow())->SetTitle(title);
225 }
226 }
227
228 // General disabler
229 void ctConfigToolView::OnUpdateDisable(wxUpdateUIEvent& event)
230 {
231 event.Enable( FALSE );
232 }
233
234 void ctConfigToolView::OnUpdateAddItem(wxUpdateUIEvent& event)
235 {
236 event.Enable( TRUE );
237 }
238
239 /// Add item and its children to the tree
240 void ctConfigToolView::AddItems(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
241 {
242 SyncItem(treeControl, item);
243
244 int count = item->GetChildCount();
245 int i;
246 for (i = 0; i < count; i++)
247 {
248 ctConfigItem* child = item->GetChild(i);
249 AddItems(treeControl, child);
250 }
251 }
252
253 /// Gets the tree item in sync with the item
254 void ctConfigToolView::SyncItem(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
255 {
256 if (!item->GetTreeItemId().IsOk())
257 {
258 if (!item->GetParent())
259 {
260 item->SetTreeItem(treeControl->AddRoot(_(""), -1, -1, new ctTreeItemData(item)));
261 }
262 else
263 {
264 // Find the item to insert the new item after.
265 ctConfigItem* previousItem = item->FindPreviousSibling();
266 if (previousItem && previousItem->GetTreeItemId().IsOk())
267 {
268 item->SetTreeItem(treeControl->InsertItem(item->GetParent()->GetTreeItemId(),
269 previousItem->GetTreeItemId(),
270 item->GetName(), -1, -1, new ctTreeItemData(item)));
271 }
272 else if (!previousItem && item->GetParent()->GetChildCount() > 1
273 )
274 {
275 // Insert at the beginning
276 item->SetTreeItem(treeControl->InsertItem(item->GetParent()->GetTreeItemId(),
277 (size_t) 0, // Insert at first position
278 item->GetName(), -1, -1, new ctTreeItemData(item)));
279 }
280 else
281 {
282 item->SetTreeItem(treeControl->AppendItem(item->GetParent()->GetTreeItemId(),
283 item->GetName(), -1, -1, new ctTreeItemData(item)));
284 }
285 }
286 }
287
288 if (item->GetTreeItemId().IsOk())
289 {
290 treeControl->SetItemText(item->GetTreeItemId(), item->GetName());
291
292 int iconId = 0;
293
294 if (item->GetType() == ctTypeGroup)
295 {
296 iconId = treeControl->GetIconTable().GetIconId(wxT("Group"), 0, item->IsActive());
297 }
298 else if (item->GetType() == ctTypeCheckGroup)
299 {
300 iconId = treeControl->GetIconTable().GetIconId(wxT("CheckGroup"), item->IsEnabled() ? 0 : 1, item->IsActive());
301 }
302 else if (item->GetType() == ctTypeRadioGroup)
303 {
304 iconId = treeControl->GetIconTable().GetIconId(wxT("RadioGroup"), item->IsEnabled() ? 0 : 1, item->IsActive());
305 }
306 else if (item->GetType() == ctTypeBoolCheck)
307 {
308 iconId = treeControl->GetIconTable().GetIconId(wxT("Checkbox"), item->IsEnabled() ? 0 : 1, item->IsActive());
309 }
310 else if (item->GetType() == ctTypeBoolRadio)
311 {
312 iconId = treeControl->GetIconTable().GetIconId(wxT("Radiobutton"), item->IsEnabled() ? 0 : 1, item->IsActive());
313 }
314 treeControl->SetItemImage(item->GetTreeItemId(), iconId, wxTreeItemIcon_Normal);
315 treeControl->SetItemImage(item->GetTreeItemId(), iconId, wxTreeItemIcon_Selected);
316
317 if (treeControl->GetSelection() == item->GetTreeItemId())
318 {
319 if (wxGetApp().GetMainFrame()->GetPropertyEditor()->GetItem())
320 wxGetApp().GetMainFrame()->GetPropertyEditor()->UpdateTitle();
321 }
322 }
323 }
324
325 /// Clicked an icon
326 void ctConfigToolView::OnIconLeftDown(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
327 {
328 if (!item->IsActive())
329 return;
330
331 if (item->GetType() == ctTypeCheckGroup ||
332 item->GetType() == ctTypeBoolCheck ||
333 item->GetType() == ctTypeBoolRadio ||
334 item->GetType() == ctTypeRadioGroup
335 )
336 {
337 // Don't toggle a radio button that's already
338 // enabled.
339 if ((item->GetType() == ctTypeBoolRadio || item->GetType() == ctTypeRadioGroup)
340 && item->IsEnabled())
341 return;
342
343 item->Enable(!item->IsEnabled());
344
345 GetDocument()->Modify(TRUE);
346 OnChangeFilename();
347
348 SyncItem(treeControl, item);
349
350 wxList considered;
351 item->PropagateChange(considered);
352 if ((item->GetType() == ctTypeBoolRadio || item->GetType() == ctTypeRadioGroup) && item->IsEnabled())
353 {
354 item->PropagateRadioButton(considered);
355 }
356 }
357 }
358
359 /// Returns the selected config item, if any.
360 ctConfigItem* ctConfigToolView::GetSelection()
361 {
362 wxTreeCtrl* treeCtrl = wxGetApp().GetMainFrame()->GetConfigTreeCtrl();
363 if (!treeCtrl)
364 return NULL;
365
366 wxTreeItemId sel = treeCtrl->GetSelection();
367 if (sel.IsOk())
368 {
369 ctTreeItemData* data = (ctTreeItemData*) treeCtrl->GetItemData(sel);
370 if (data)
371 return data->GetConfigItem() ;
372 }
373 return NULL;
374 }
375
376 /// Add a checkbox item
377 void ctConfigToolView::OnAddCheckBoxItem(wxCommandEvent& event)
378 {
379 AddItem(ctTypeBoolCheck, _("New checkbox item"));
380 }
381
382 /// Add a radiobutton item
383 void ctConfigToolView::OnAddRadioButtonItem(wxCommandEvent& event)
384 {
385 AddItem(ctTypeBoolRadio, _("New radio button item"));
386 }
387
388 /// Add a group item
389 void ctConfigToolView::OnAddGroupItem(wxCommandEvent& event)
390 {
391 AddItem(ctTypeGroup, _("New group item"));
392 }
393
394 /// Add a group option item
395 void ctConfigToolView::OnAddCheckGroupItem(wxCommandEvent& event)
396 {
397 AddItem(ctTypeCheckGroup, _("New check group item"));
398 }
399
400 /// Add a group option item
401 void ctConfigToolView::OnAddRadioGroupItem(wxCommandEvent& event)
402 {
403 AddItem(ctTypeRadioGroup, _("New radio group item"));
404 }
405
406 /// Add a string item
407 void ctConfigToolView::OnAddStringItem(wxCommandEvent& event)
408 {
409 AddItem(ctTypeString, _("New string item"));
410 }
411
412 /// Add an item
413 void ctConfigToolView::AddItem(ctConfigType type, const wxString& msg)
414 {
415 ctConfigItem* sel = GetSelection();
416 if (sel)
417 {
418 wxString name = wxGetTextFromUser(_("Please enter a name for the new item."),
419 msg, wxT(""));
420 if (!name.IsEmpty())
421 {
422 ctConfigItem* parent ;
423 ctConfigItem* insertBefore ;
424 if (sel->CanHaveChildren())
425 {
426 parent = sel;
427 insertBefore = NULL;
428 }
429 else
430 {
431 parent = sel->GetParent();
432 insertBefore = sel->FindNextSibling();
433 }
434
435 ctConfigItem* newItem = new ctConfigItem(NULL, type, name);
436 newItem->InitProperties();
437
438 newItem->GetDocument()->GetCommandProcessor()->Submit(
439 new ctConfigCommand(msg, ctCMD_NEW_ELEMENT, NULL, newItem,
440 parent, insertBefore));
441 }
442 }
443 }
444
445 /// Delete an item
446 void ctConfigToolView::OnDeleteItem(wxCommandEvent& event)
447 {
448 ctConfigItem* sel = GetSelection();
449 if (sel)
450 {
451 wxString name(sel->GetName());
452 wxString msg;
453 msg.Printf(_("Delete %s?"), (const wxChar*) name);
454 if (wxYES == wxMessageBox(msg, _("Delete item"), wxICON_QUESTION|wxYES_NO))
455 {
456 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Delete(sel->GetTreeItemId());
457 }
458 }
459 }
460
461 /// Rename an item
462 void ctConfigToolView::OnRenameItem(wxCommandEvent& event)
463 {
464 ctConfigItem* sel = GetSelection();
465 if (sel)
466 {
467 wxString name = wxGetTextFromUser(_("Please enter a new name for the item."),
468 _("Rename item"), sel->GetName());
469 if (!name.IsEmpty())
470 {
471 sel->SetName(name);
472 SyncItem(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), sel);
473
474 ctConfigToolHint hint(NULL, ctSelChanged);
475 GetDocument()->UpdateAllViews (NULL, & hint);
476 }
477 }
478 }
479
480 /// Copy an item to the clipboard
481 void ctConfigToolView::OnCopy(wxCommandEvent& event)
482 {
483 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
484 ctConfigItem* sel = GetSelection();
485 if (sel)
486 {
487 doc->SetClipboardItem(sel->DeepClone());
488 }
489 }
490
491 /// Copy an item to the clipboard and cut the item
492 void ctConfigToolView::OnCut(wxCommandEvent& event)
493 {
494 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
495 ctConfigItem* sel = GetSelection();
496 if (sel)
497 {
498 {
499 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
500 sel, (ctConfigItem*) NULL);
501 doc->GetCommandProcessor()->Submit(cmd);
502 }
503 }
504 }
505
506 /// Paste an item from the clipboard to the tree
507 void ctConfigToolView::OnPaste(wxCommandEvent& event)
508 {
509 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
510 ctConfigItem* sel = GetSelection();
511 if (sel && doc->GetClipboardItem())
512 {
513 ctConfigItem* parent ;
514 ctConfigItem* insertBefore ;
515 if (sel->CanHaveChildren())
516 {
517 parent = sel;
518 insertBefore = NULL;
519 }
520 else
521 {
522 parent = sel->GetParent();
523 insertBefore = sel->FindNextSibling();
524 }
525
526 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
527 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
528 NULL, newItem, parent, insertBefore);
529 doc->GetCommandProcessor()->Submit(cmd);
530 }
531 }
532
533 /// Update for copy command
534 void ctConfigToolView::OnUpdateCopy(wxUpdateUIEvent& event)
535 {
536 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
537 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
538 }
539
540 /// Update for cut
541 void ctConfigToolView::OnUpdateCut(wxUpdateUIEvent& event)
542 {
543 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
544 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
545 }
546
547 /// Update for paste
548 void ctConfigToolView::OnUpdatePaste(wxUpdateUIEvent& event)
549 {
550 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
551 event.Enable( doc && doc->GetClipboardItem() && GetSelection() );
552 }
553
554 /// Copy an item to the clipboard
555 void ctConfigToolView::OnContextCopy(wxCommandEvent& event)
556 {
557 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
558 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
559 if (doc && sel)
560 {
561 doc->SetClipboardItem(sel->DeepClone());
562 }
563 }
564
565 /// Copy an item to the clipboard and cut the item
566 void ctConfigToolView::OnContextCut(wxCommandEvent& event)
567 {
568 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
569 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
570 if (sel)
571 {
572 {
573 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
574 sel, (ctConfigItem*) NULL);
575 doc->GetCommandProcessor()->Submit(cmd);
576 }
577 }
578 }
579
580 /// Paste an item from the clipboard to the tree
581 void ctConfigToolView::OnContextPasteBefore(wxCommandEvent& event)
582 {
583 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
584 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
585 if (sel && doc->GetClipboardItem())
586 {
587 ctConfigItem* parent = sel->GetParent();
588 ctConfigItem* insertBefore = sel;
589
590 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
591 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
592 NULL, newItem, parent, insertBefore);
593 doc->GetCommandProcessor()->Submit(cmd);
594 }
595 }
596
597 /// Paste an item from the clipboard to the tree
598 void ctConfigToolView::OnContextPasteAfter(wxCommandEvent& event)
599 {
600 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
601 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
602 if (sel && doc->GetClipboardItem())
603 {
604 ctConfigItem* parent = sel->GetParent();
605 ctConfigItem* insertBefore = sel->FindNextSibling();
606
607 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
608 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
609 NULL, newItem, parent, insertBefore);
610 doc->GetCommandProcessor()->Submit(cmd);
611 }
612 }
613
614 /// Paste an item from the clipboard to the tree
615 void ctConfigToolView::OnContextPasteAsChild(wxCommandEvent& event)
616 {
617 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
618 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
619 if (sel && doc->GetClipboardItem())
620 {
621 if (sel->CanHaveChildren())
622 {
623 ctConfigItem* parent = sel;
624 ctConfigItem* insertBefore = NULL;
625
626 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
627 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
628 NULL, newItem, parent, insertBefore);
629 doc->GetCommandProcessor()->Submit(cmd);
630 }
631 }
632 }
633
634 /// Copy an item to the clipboard
635 void ctConfigToolView::OnUpdateContextCopy(wxUpdateUIEvent& event)
636 {
637 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
638 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
639 event.Enable( doc && sel && sel->GetParent() );
640 }
641
642 /// Copy an item to the clipboard and cut the item
643 void ctConfigToolView::OnUpdateContextCut(wxUpdateUIEvent& event)
644 {
645 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
646 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
647 event.Enable( doc && sel && sel->GetParent() );
648 }
649
650 /// Paste an item from the clipboard to the tree
651 void ctConfigToolView::OnUpdateContextPasteBefore(wxUpdateUIEvent& event)
652 {
653 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
654 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
655 event.Enable( doc && sel && sel->GetParent() && doc->GetClipboardItem() );
656 }
657
658 /// Paste an item from the clipboard to the tree
659 void ctConfigToolView::OnUpdateContextPasteAfter(wxUpdateUIEvent& event)
660 {
661 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
662 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
663 event.Enable( doc && sel && sel->GetParent() && doc->GetClipboardItem() );
664 }
665
666 /// Paste an item from the clipboard to the tree
667 void ctConfigToolView::OnUpdateContextPasteAsChild(wxUpdateUIEvent& event)
668 {
669 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
670 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
671 event.Enable( doc && sel && sel->CanHaveChildren() && doc->GetClipboardItem() );
672 }
673
674 /// Item help
675 void ctConfigToolView::OnItemHelp(wxCommandEvent& event)
676 {
677 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
678 if ( doc && GetSelection() )
679 {
680 wxString helpTopic = GetSelection()->GetPropertyString(wxT("help-topic"));
681 if (!helpTopic.IsEmpty())
682 {
683 wxGetApp().GetReferenceHelpController().DisplaySection(helpTopic);
684 }
685 }
686 }
687
688 /// Item help update
689 void ctConfigToolView::OnUpdateItemHelp(wxUpdateUIEvent& event)
690 {
691 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
692 event.Enable( doc && GetSelection() );
693 }
694
695 /// Add a custom property
696 void ctConfigToolView::OnAddCustomProperty(wxCommandEvent& event)
697 {
698 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
699 ctConfigItem* sel = GetSelection();
700 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
701 if (doc && sel && editor)
702 {
703 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
704 -1, _("Add a custom property"));
705 if (dialog.ShowModal() == wxID_OK)
706 {
707 wxString name = dialog.GetPropertyName();
708 wxString type = dialog.GetPropertyType();
709 wxString descr = dialog.GetPropertyDescription();
710 wxString editorType = dialog.GetEditorType();
711 wxArrayString choices = dialog.GetChoices();
712
713 if (sel->GetProperties().FindProperty(name))
714 {
715 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
716 wxOK|wxICON_INFORMATION);
717 return;
718 }
719 ctProperty* property = new ctProperty;
720 if (type == wxT("bool"))
721 property->GetVariant() = wxVariant((bool) FALSE, name);
722 else if (type == wxT("double"))
723 property->GetVariant() = wxVariant((double) 0.0, name);
724 else if (type == wxT("long"))
725 property->GetVariant() = wxVariant((long) 0, name);
726 else
727 property->GetVariant() = wxVariant(wxT(""), name);
728 property->SetCustom(TRUE);
729 property->SetDescription(descr);
730 property->SetChoices(choices);
731 property->SetEditorType(editorType);
732
733 sel->GetProperties().AddProperty(property);
734 editor->ShowItem(sel);
735 OnChangeFilename();
736 }
737 }
738 }
739
740 /// Edit a custom property
741 void ctConfigToolView::OnEditCustomProperty(wxCommandEvent& event)
742 {
743 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
744 ctConfigItem* sel = GetSelection();
745 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
746 if (doc && sel && editor)
747 {
748 int row;
749 ctProperty* property = editor->FindSelectedProperty(row) ;
750 if (property && property->IsCustom())
751 {
752 wxString oldName = property->GetName();
753 wxString oldDescription = property->GetDescription();
754 wxString oldType = property->GetVariant().GetType();
755 wxString oldEditorType = property->GetEditorType();
756 wxArrayString oldChoices = property->GetChoices();
757
758 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
759 -1, _("Edit custom property"));
760 dialog.SetPropertyName(oldName);
761 dialog.SetPropertyType(oldType);
762 dialog.SetPropertyDescription(oldDescription);
763 if (dialog.ShowModal() == wxID_OK)
764 {
765 wxString name = dialog.GetPropertyName();
766 wxString type = dialog.GetPropertyType();
767 wxString editorType = dialog.GetEditorType();
768 wxArrayString choices = dialog.GetChoices();
769 wxString descr = dialog.GetPropertyDescription();
770
771 if (name != oldName && sel->GetProperties().FindProperty(name))
772 {
773 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
774 wxOK|wxICON_INFORMATION);
775 return;
776 }
777 if (type != oldType)
778 {
779 if (type == wxT("bool"))
780 property->GetVariant() = wxVariant((bool) FALSE, name);
781 else if (type == wxT("double"))
782 property->GetVariant() = wxVariant((double) 0.0, name);
783 else if (type == wxT("long"))
784 property->GetVariant() = wxVariant((long) 0, name);
785 else
786 property->GetVariant() = wxVariant(wxT(""), name);
787 }
788 if (name != oldName)
789 property->GetVariant().SetName(name);
790
791 if (choices != oldChoices)
792 property->SetChoices(choices);
793
794 if (editorType != oldEditorType)
795 property->SetEditorType(editorType);
796
797 if (name != oldName)
798 property->GetVariant().SetName(name);
799
800 property->SetCustom(TRUE);
801
802 if (descr != oldDescription)
803 property->SetDescription(descr);
804
805 editor->ShowItem(sel);
806 OnChangeFilename();
807 }
808 }
809 }
810 }
811
812 /// Delete a custom property
813 void ctConfigToolView::OnDeleteCustomProperty(wxCommandEvent& event)
814 {
815 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
816 ctConfigItem* sel = GetSelection();
817 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
818 if (doc && sel && editor)
819 {
820 int row;
821 ctProperty* property = editor->FindSelectedProperty(row) ;
822 if (property && property->IsCustom())
823 {
824 wxString name(property->GetName());
825 wxString msg;
826 msg.Printf(_("Delete custom property '%s'?"), (const wxChar*) name);
827 if (wxYES == wxMessageBox(msg, _("Delete property"), wxICON_EXCLAMATION|wxYES_NO))
828 {
829 sel->GetProperties().RemoveProperty(property);
830 editor->ShowItem(sel);
831 delete property;
832 OnChangeFilename();
833 }
834 }
835 }
836 }
837
838 /// Add a custom property: update event
839 void ctConfigToolView::OnUpdateAddCustomProperty(wxUpdateUIEvent& event)
840 {
841 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
842 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
843 }
844
845 /// Edit a custom property: update event
846 void ctConfigToolView::OnUpdateEditCustomProperty(wxUpdateUIEvent& event)
847 {
848 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
849 ctConfigItem* sel = GetSelection();
850 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
851 int row;
852 event.Enable( doc && sel && sel->GetParent() && editor &&
853 editor->FindSelectedProperty(row) &&
854 editor->FindSelectedProperty(row)->IsCustom() );
855 }
856
857 /// Delete a custom property: update event
858 void ctConfigToolView::OnUpdateDeleteCustomProperty(wxUpdateUIEvent& event)
859 {
860 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
861 ctConfigItem* sel = GetSelection();
862 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
863 int row;
864 event.Enable( doc && sel && sel->GetParent() && editor &&
865 editor->FindSelectedProperty(row) &&
866 editor->FindSelectedProperty(row)->IsCustom() );
867 }
868
869 /// Regenerate setup.h and configure command
870 void ctConfigToolView::RegenerateSetup()
871 {
872 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
873 ctOutputWindow* setupPage = wxGetApp().GetMainFrame()->GetSetupPage() ;
874 ctOutputWindow* configurePage = wxGetApp().GetMainFrame()->GetConfigurePage() ;
875
876 wxString setupStr = doc->GenerateSetup();
877 wxString configureStr = doc->GenerateConfigureCommand();
878
879 setupPage->SetText(setupStr);
880 configurePage->SetText(configureStr);
881 }
882
883 /// Regenerate if selected a tab
884 void ctConfigToolView::OnTabSelect(wxNotebookEvent& event)
885 {
886 if (wxGetApp().GetMainFrame()->GetMainNotebook() != event.GetEventObject())
887 {
888 event.Skip();
889 return;
890 }
891
892 if (event.GetSelection() > 0)
893 {
894 RegenerateSetup();
895 }
896 }
897
898 void ctConfigToolView::OnSaveSetupFile(wxCommandEvent& event)
899 {
900 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
901 wxString setupStr = doc->GenerateSetup();
902
903 wxString filename = _T("setup.h");
904 wxString path = doc->GetFrameworkDir(FALSE);
905 wxString wildcard = _T("Header files (*.h)|*.h|All files (*.*)|*.*");
906
907 wxFileDialog dialog(wxTheApp->GetTopWindow(),
908 _("Save Setup File As"),
909 path, filename ,
910 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
911
912 if (dialog.ShowModal() == wxID_OK)
913 {
914 wxString fullPath = dialog.GetPath();
915
916 // TODO: save last saved path in settings.
917
918 wxFileOutputStream stream(fullPath);
919 if (!stream.Ok())
920 {
921 wxMessageBox(_("Sorry, could not save this file."), _("Save Setup File"), wxICON_EXCLAMATION|wxOK);
922 return;
923 }
924
925 stream << setupStr;
926 }
927 }
928
929 void ctConfigToolView::OnSaveConfigureCommand(wxCommandEvent& event)
930 {
931 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
932 wxString configureStr = doc->GenerateConfigureCommand();
933
934 wxString filename = _T("configurewx.sh");
935 wxString path = doc->GetFrameworkDir(FALSE);
936 wxString wildcard = _T("Shell script files (*.sh)|*.sh|All files (*.*)|*.*");
937
938 wxFileDialog dialog(wxTheApp->GetTopWindow(),
939 _("Save Configure Command File As"),
940 path, filename ,
941 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
942
943 if (dialog.ShowModal() == wxID_OK)
944 {
945 wxString fullPath = dialog.GetPath();
946
947 // TODO: save last saved path in settings.
948
949 wxFileOutputStream stream(fullPath);
950 if (!stream.Ok())
951 {
952 wxMessageBox(_("Sorry, could not save this file."), _("Save Configure Command File"), wxICON_EXCLAMATION|wxOK);
953 return;
954 }
955
956 stream << configureStr;
957 }
958 }
959
960 void ctConfigToolView::OnUpdateSaveSetupFile(wxUpdateUIEvent& event)
961 {
962 event.Enable(TRUE);
963 }
964
965 void ctConfigToolView::OnUpdateSaveConfigureCommand(wxUpdateUIEvent& event)
966 {
967 event.Enable(TRUE);
968 }