]> git.saurik.com Git - wxWidgets.git/blob - utils/configtool/src/configtoolview.cpp
Small fixes
[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.cpp"
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 msg;
452 msg.Printf(_("Delete %s?"), sel->GetName());
453 if (wxYES == wxMessageBox(msg, _("Delete item"), wxICON_QUESTION|wxYES_NO))
454 {
455 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Delete(sel->GetTreeItemId());
456 }
457 }
458 }
459
460 /// Rename an item
461 void ctConfigToolView::OnRenameItem(wxCommandEvent& event)
462 {
463 ctConfigItem* sel = GetSelection();
464 if (sel)
465 {
466 wxString name = wxGetTextFromUser(_("Please enter a new name for the item."),
467 _("Rename item"), sel->GetName());
468 if (!name.IsEmpty())
469 {
470 sel->SetName(name);
471 SyncItem(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), sel);
472
473 ctConfigToolHint hint(NULL, ctSelChanged);
474 GetDocument()->UpdateAllViews (NULL, & hint);
475 }
476 }
477 }
478
479 /// Copy an item to the clipboard
480 void ctConfigToolView::OnCopy(wxCommandEvent& event)
481 {
482 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
483 ctConfigItem* sel = GetSelection();
484 if (sel)
485 {
486 doc->SetClipboardItem(sel->DeepClone());
487 }
488 }
489
490 /// Copy an item to the clipboard and cut the item
491 void ctConfigToolView::OnCut(wxCommandEvent& event)
492 {
493 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
494 ctConfigItem* sel = GetSelection();
495 if (sel)
496 {
497 {
498 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
499 sel, (ctConfigItem*) NULL);
500 doc->GetCommandProcessor()->Submit(cmd);
501 }
502 }
503 }
504
505 /// Paste an item from the clipboard to the tree
506 void ctConfigToolView::OnPaste(wxCommandEvent& event)
507 {
508 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
509 ctConfigItem* sel = GetSelection();
510 if (sel && doc->GetClipboardItem())
511 {
512 ctConfigItem* parent ;
513 ctConfigItem* insertBefore ;
514 if (sel->CanHaveChildren())
515 {
516 parent = sel;
517 insertBefore = NULL;
518 }
519 else
520 {
521 parent = sel->GetParent();
522 insertBefore = sel->FindNextSibling();
523 }
524
525 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
526 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
527 NULL, newItem, parent, insertBefore);
528 doc->GetCommandProcessor()->Submit(cmd);
529 }
530 }
531
532 /// Update for copy command
533 void ctConfigToolView::OnUpdateCopy(wxUpdateUIEvent& event)
534 {
535 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
536 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
537 }
538
539 /// Update for cut
540 void ctConfigToolView::OnUpdateCut(wxUpdateUIEvent& event)
541 {
542 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
543 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
544 }
545
546 /// Update for paste
547 void ctConfigToolView::OnUpdatePaste(wxUpdateUIEvent& event)
548 {
549 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
550 event.Enable( doc && doc->GetClipboardItem() && GetSelection() );
551 }
552
553 /// Copy an item to the clipboard
554 void ctConfigToolView::OnContextCopy(wxCommandEvent& event)
555 {
556 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
557 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
558 if (doc && sel)
559 {
560 doc->SetClipboardItem(sel->DeepClone());
561 }
562 }
563
564 /// Copy an item to the clipboard and cut the item
565 void ctConfigToolView::OnContextCut(wxCommandEvent& event)
566 {
567 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
568 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
569 if (sel)
570 {
571 {
572 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
573 sel, (ctConfigItem*) NULL);
574 doc->GetCommandProcessor()->Submit(cmd);
575 }
576 }
577 }
578
579 /// Paste an item from the clipboard to the tree
580 void ctConfigToolView::OnContextPasteBefore(wxCommandEvent& event)
581 {
582 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
583 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
584 if (sel && doc->GetClipboardItem())
585 {
586 ctConfigItem* parent = sel->GetParent();
587 ctConfigItem* insertBefore = sel;
588
589 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
590 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
591 NULL, newItem, parent, insertBefore);
592 doc->GetCommandProcessor()->Submit(cmd);
593 }
594 }
595
596 /// Paste an item from the clipboard to the tree
597 void ctConfigToolView::OnContextPasteAfter(wxCommandEvent& event)
598 {
599 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
600 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
601 if (sel && doc->GetClipboardItem())
602 {
603 ctConfigItem* parent = sel->GetParent();
604 ctConfigItem* insertBefore = sel->FindNextSibling();
605
606 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
607 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
608 NULL, newItem, parent, insertBefore);
609 doc->GetCommandProcessor()->Submit(cmd);
610 }
611 }
612
613 /// Paste an item from the clipboard to the tree
614 void ctConfigToolView::OnContextPasteAsChild(wxCommandEvent& event)
615 {
616 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
617 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
618 if (sel && doc->GetClipboardItem())
619 {
620 if (sel->CanHaveChildren())
621 {
622 ctConfigItem* parent = sel;
623 ctConfigItem* insertBefore = NULL;
624
625 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
626 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
627 NULL, newItem, parent, insertBefore);
628 doc->GetCommandProcessor()->Submit(cmd);
629 }
630 }
631 }
632
633 /// Copy an item to the clipboard
634 void ctConfigToolView::OnUpdateContextCopy(wxUpdateUIEvent& event)
635 {
636 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
637 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
638 event.Enable( doc && sel && sel->GetParent() );
639 }
640
641 /// Copy an item to the clipboard and cut the item
642 void ctConfigToolView::OnUpdateContextCut(wxUpdateUIEvent& event)
643 {
644 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
645 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
646 event.Enable( doc && sel && sel->GetParent() );
647 }
648
649 /// Paste an item from the clipboard to the tree
650 void ctConfigToolView::OnUpdateContextPasteBefore(wxUpdateUIEvent& event)
651 {
652 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
653 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
654 event.Enable( doc && sel && sel->GetParent() && doc->GetClipboardItem() );
655 }
656
657 /// Paste an item from the clipboard to the tree
658 void ctConfigToolView::OnUpdateContextPasteAfter(wxUpdateUIEvent& event)
659 {
660 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
661 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
662 event.Enable( doc && sel && sel->GetParent() && doc->GetClipboardItem() );
663 }
664
665 /// Paste an item from the clipboard to the tree
666 void ctConfigToolView::OnUpdateContextPasteAsChild(wxUpdateUIEvent& event)
667 {
668 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
669 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
670 event.Enable( doc && sel && sel->CanHaveChildren() && doc->GetClipboardItem() );
671 }
672
673 /// Item help
674 void ctConfigToolView::OnItemHelp(wxCommandEvent& event)
675 {
676 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
677 if ( doc && GetSelection() )
678 {
679 wxString helpTopic = GetSelection()->GetPropertyString(wxT("help-topic"));
680 if (!helpTopic.IsEmpty())
681 {
682 wxGetApp().GetReferenceHelpController().DisplaySection(helpTopic);
683 }
684 }
685 }
686
687 /// Item help update
688 void ctConfigToolView::OnUpdateItemHelp(wxUpdateUIEvent& event)
689 {
690 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
691 event.Enable( doc && GetSelection() );
692 }
693
694 /// Add a custom property
695 void ctConfigToolView::OnAddCustomProperty(wxCommandEvent& event)
696 {
697 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
698 ctConfigItem* sel = GetSelection();
699 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
700 if (doc && sel && editor)
701 {
702 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
703 -1, _("Add a custom property"));
704 if (dialog.ShowModal() == wxID_OK)
705 {
706 wxString name = dialog.GetPropertyName();
707 wxString type = dialog.GetPropertyType();
708 wxString descr = dialog.GetPropertyDescription();
709 wxString editorType = dialog.GetEditorType();
710 wxArrayString choices = dialog.GetChoices();
711
712 if (sel->GetProperties().FindProperty(name))
713 {
714 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
715 wxOK|wxICON_INFORMATION);
716 return;
717 }
718 ctProperty* property = new ctProperty;
719 if (type == wxT("bool"))
720 property->GetVariant() = wxVariant((bool) FALSE, name);
721 else if (type == wxT("double"))
722 property->GetVariant() = wxVariant((double) 0.0, name);
723 else if (type == wxT("long"))
724 property->GetVariant() = wxVariant((long) 0, name);
725 else
726 property->GetVariant() = wxVariant(wxT(""), name);
727 property->SetCustom(TRUE);
728 property->SetDescription(descr);
729 property->SetChoices(choices);
730 property->SetEditorType(editorType);
731
732 sel->GetProperties().AddProperty(property);
733 editor->ShowItem(sel);
734 OnChangeFilename();
735 }
736 }
737 }
738
739 /// Edit a custom property
740 void ctConfigToolView::OnEditCustomProperty(wxCommandEvent& event)
741 {
742 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
743 ctConfigItem* sel = GetSelection();
744 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
745 if (doc && sel && editor)
746 {
747 int row;
748 ctProperty* property = editor->FindSelectedProperty(row) ;
749 if (property && property->IsCustom())
750 {
751 wxString oldName = property->GetName();
752 wxString oldDescription = property->GetDescription();
753 wxString oldType = property->GetVariant().GetType();
754 wxString oldEditorType = property->GetEditorType();
755 wxArrayString oldChoices = property->GetChoices();
756
757 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
758 -1, _("Edit custom property"));
759 dialog.SetPropertyName(oldName);
760 dialog.SetPropertyType(oldType);
761 dialog.SetPropertyDescription(oldDescription);
762 if (dialog.ShowModal() == wxID_OK)
763 {
764 wxString name = dialog.GetPropertyName();
765 wxString type = dialog.GetPropertyType();
766 wxString editorType = dialog.GetEditorType();
767 wxArrayString choices = dialog.GetChoices();
768 wxString descr = dialog.GetPropertyDescription();
769
770 if (name != oldName && sel->GetProperties().FindProperty(name))
771 {
772 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
773 wxOK|wxICON_INFORMATION);
774 return;
775 }
776 if (type != oldType)
777 {
778 if (type == wxT("bool"))
779 property->GetVariant() = wxVariant((bool) FALSE, name);
780 else if (type == wxT("double"))
781 property->GetVariant() = wxVariant((double) 0.0, name);
782 else if (type == wxT("long"))
783 property->GetVariant() = wxVariant((long) 0, name);
784 else
785 property->GetVariant() = wxVariant(wxT(""), name);
786 }
787 if (name != oldName)
788 property->GetVariant().SetName(name);
789
790 if (choices != oldChoices)
791 property->SetChoices(choices);
792
793 if (editorType != oldEditorType)
794 property->SetEditorType(editorType);
795
796 if (name != oldName)
797 property->GetVariant().SetName(name);
798
799 property->SetCustom(TRUE);
800
801 if (descr != oldDescription)
802 property->SetDescription(descr);
803
804 editor->ShowItem(sel);
805 OnChangeFilename();
806 }
807 }
808 }
809 }
810
811 /// Delete a custom property
812 void ctConfigToolView::OnDeleteCustomProperty(wxCommandEvent& event)
813 {
814 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
815 ctConfigItem* sel = GetSelection();
816 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
817 if (doc && sel && editor)
818 {
819 int row;
820 ctProperty* property = editor->FindSelectedProperty(row) ;
821 if (property && property->IsCustom())
822 {
823 wxString name(property->GetName());
824 wxString msg;
825 msg.Printf(_("Delete custom property '%s'?"), (const wxChar*) name);
826 if (wxYES == wxMessageBox(msg, _("Delete property"), wxICON_EXCLAMATION|wxYES_NO))
827 {
828 sel->GetProperties().RemoveProperty(property);
829 editor->ShowItem(sel);
830 delete property;
831 OnChangeFilename();
832 }
833 }
834 }
835 }
836
837 /// Add a custom property: update event
838 void ctConfigToolView::OnUpdateAddCustomProperty(wxUpdateUIEvent& event)
839 {
840 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
841 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
842 }
843
844 /// Edit a custom property: update event
845 void ctConfigToolView::OnUpdateEditCustomProperty(wxUpdateUIEvent& event)
846 {
847 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
848 ctConfigItem* sel = GetSelection();
849 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
850 int row;
851 event.Enable( doc && sel && sel->GetParent() && editor &&
852 editor->FindSelectedProperty(row) &&
853 editor->FindSelectedProperty(row)->IsCustom() );
854 }
855
856 /// Delete a custom property: update event
857 void ctConfigToolView::OnUpdateDeleteCustomProperty(wxUpdateUIEvent& event)
858 {
859 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
860 ctConfigItem* sel = GetSelection();
861 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
862 int row;
863 event.Enable( doc && sel && sel->GetParent() && editor &&
864 editor->FindSelectedProperty(row) &&
865 editor->FindSelectedProperty(row)->IsCustom() );
866 }
867
868 /// Regenerate setup.h and configure command
869 void ctConfigToolView::RegenerateSetup()
870 {
871 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
872 ctOutputWindow* setupPage = wxGetApp().GetMainFrame()->GetSetupPage() ;
873 ctOutputWindow* configurePage = wxGetApp().GetMainFrame()->GetConfigurePage() ;
874
875 wxString setupStr = doc->GenerateSetup();
876 wxString configureStr = doc->GenerateConfigureCommand();
877
878 setupPage->SetText(setupStr);
879 configurePage->SetText(configureStr);
880 }
881
882 /// Regenerate if selected a tab
883 void ctConfigToolView::OnTabSelect(wxNotebookEvent& event)
884 {
885 if (wxGetApp().GetMainFrame()->GetMainNotebook() != event.GetEventObject())
886 {
887 event.Skip();
888 return;
889 }
890
891 if (event.GetSelection() > 0)
892 {
893 RegenerateSetup();
894 }
895 }
896
897 void ctConfigToolView::OnSaveSetupFile(wxCommandEvent& event)
898 {
899 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
900 wxString setupStr = doc->GenerateSetup();
901
902 wxString filename = _T("setup.h");
903 wxString path = doc->GetFrameworkDir(FALSE);
904 wxString wildcard = _T("Header files (*.h)|*.h|All files (*.*)|*.*");
905
906 wxFileDialog dialog(wxTheApp->GetTopWindow(),
907 _("Save Setup File As"),
908 path, filename ,
909 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
910
911 if (dialog.ShowModal() == wxID_OK)
912 {
913 wxString fullPath = dialog.GetPath();
914
915 // TODO: save last saved path in settings.
916
917 wxFileOutputStream stream(fullPath);
918 if (!stream.Ok())
919 {
920 wxMessageBox(_("Sorry, could not save this file."), _("Save Setup File"), wxICON_EXCLAMATION|wxOK);
921 return;
922 }
923
924 stream << setupStr;
925 }
926 }
927
928 void ctConfigToolView::OnSaveConfigureCommand(wxCommandEvent& event)
929 {
930 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
931 wxString configureStr = doc->GenerateConfigureCommand();
932
933 wxString filename = _T("configurewx.sh");
934 wxString path = doc->GetFrameworkDir(FALSE);
935 wxString wildcard = _T("Shell script files (*.sh)|*.sh|All files (*.*)|*.*");
936
937 wxFileDialog dialog(wxTheApp->GetTopWindow(),
938 _("Save Configure Command File As"),
939 path, filename ,
940 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
941
942 if (dialog.ShowModal() == wxID_OK)
943 {
944 wxString fullPath = dialog.GetPath();
945
946 // TODO: save last saved path in settings.
947
948 wxFileOutputStream stream(fullPath);
949 if (!stream.Ok())
950 {
951 wxMessageBox(_("Sorry, could not save this file."), _("Save Configure Command File"), wxICON_EXCLAMATION|wxOK);
952 return;
953 }
954
955 stream << configureStr;
956 }
957 }
958
959 void ctConfigToolView::OnUpdateSaveSetupFile(wxUpdateUIEvent& event)
960 {
961 event.Enable(TRUE);
962 }
963
964 void ctConfigToolView::OnUpdateSaveConfigureCommand(wxUpdateUIEvent& event)
965 {
966 event.Enable(TRUE);
967 }