]> git.saurik.com Git - wxWidgets.git/blame - utils/configtool/src/configtoolview.cpp
Old API depreciated. Use of new API. Source cleaning.
[wxWidgets.git] / utils / configtool / src / configtoolview.cpp
CommitLineData
d7463f75
JS
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
71ada1a5 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f8105809 13 #pragma implementation "configtoolview.h"
d7463f75
JS
14#endif
15
d9ab621e
WS
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
d7463f75
JS
18
19#ifdef __BORLANDC__
d9ab621e 20#pragma hdrstop
d7463f75
JS
21#endif
22
23#include "wx/wfstream.h"
d7463f75
JS
24#include "configtoolview.h"
25#include "configtooldoc.h"
26#include "configtree.h"
27#include "wxconfigtool.h"
28#include "mainframe.h"
29#include "propeditor.h"
30#include "utils.h"
31#include "custompropertydialog.h"
32
33IMPLEMENT_DYNAMIC_CLASS(ctConfigToolView, wxView)
34
35BEGIN_EVENT_TABLE(ctConfigToolView, wxView)
36 EVT_MENU(ctID_ADD_ITEM_CHECKBOX, ctConfigToolView::OnAddCheckBoxItem)
37 EVT_MENU(ctID_ADD_ITEM_RADIOBUTTON, ctConfigToolView::OnAddRadioButtonItem)
38 EVT_MENU(ctID_ADD_ITEM_GROUP, ctConfigToolView::OnAddGroupItem)
39 EVT_MENU(ctID_ADD_ITEM_CHECK_GROUP, ctConfigToolView::OnAddCheckGroupItem)
40 EVT_MENU(ctID_ADD_ITEM_RADIO_GROUP, ctConfigToolView::OnAddRadioGroupItem)
41 EVT_MENU(ctID_ADD_ITEM_STRING, ctConfigToolView::OnAddStringItem)
42 EVT_MENU(ctID_RENAME_ITEM, ctConfigToolView::OnRenameItem)
43 EVT_MENU(ctID_DELETE_ITEM, ctConfigToolView::OnDeleteItem)
44
45 EVT_MENU(wxID_COPY, ctConfigToolView::OnCopy)
46 EVT_MENU(wxID_CUT, ctConfigToolView::OnCut)
47 EVT_MENU(wxID_PASTE, ctConfigToolView::OnPaste)
48
49 EVT_UPDATE_UI(wxID_COPY, ctConfigToolView::OnUpdateCopy)
50 EVT_UPDATE_UI(wxID_CUT, ctConfigToolView::OnUpdateCut)
51 EVT_UPDATE_UI(wxID_PASTE, ctConfigToolView::OnUpdatePaste)
52
53 EVT_MENU(ctID_ITEM_HELP, ctConfigToolView::OnItemHelp)
54
55 EVT_UPDATE_UI(ctID_ADD_ITEM_CHECKBOX, ctConfigToolView::OnUpdateAddItem)
56 EVT_UPDATE_UI(ctID_ADD_ITEM_RADIOBUTTON, ctConfigToolView::OnUpdateAddItem)
57 EVT_UPDATE_UI(ctID_ADD_ITEM_GROUP, ctConfigToolView::OnUpdateAddItem)
58 EVT_UPDATE_UI(ctID_ADD_ITEM_CHECK_GROUP, ctConfigToolView::OnUpdateAddItem)
59 EVT_UPDATE_UI(ctID_ADD_ITEM_RADIO_GROUP, ctConfigToolView::OnUpdateAddItem)
60 EVT_UPDATE_UI(ctID_ADD_ITEM_STRING, ctConfigToolView::OnUpdateAddItem)
61 EVT_UPDATE_UI(ctID_RENAME_ITEM, ctConfigToolView::OnUpdateCut)
62 EVT_UPDATE_UI(ctID_DELETE_ITEM, ctConfigToolView::OnUpdateCut)
63 EVT_UPDATE_UI(ctID_ITEM_HELP, ctConfigToolView::OnUpdateItemHelp)
64
65 EVT_MENU(ctID_TREE_COPY, ctConfigToolView::OnContextCopy)
66 EVT_MENU(ctID_TREE_CUT, ctConfigToolView::OnContextCut)
67 EVT_MENU(ctID_TREE_PASTE_BEFORE, ctConfigToolView::OnContextPasteBefore)
68 EVT_MENU(ctID_TREE_PASTE_AFTER, ctConfigToolView::OnContextPasteAfter)
69 EVT_MENU(ctID_TREE_PASTE_AS_CHILD, ctConfigToolView::OnContextPasteAsChild)
70 EVT_UPDATE_UI(ctID_TREE_COPY, ctConfigToolView::OnUpdateContextCopy)
71 EVT_UPDATE_UI(ctID_TREE_CUT, ctConfigToolView::OnUpdateContextCut)
72 EVT_UPDATE_UI(ctID_TREE_PASTE_BEFORE, ctConfigToolView::OnUpdateContextPasteBefore)
73 EVT_UPDATE_UI(ctID_TREE_PASTE_AFTER, ctConfigToolView::OnUpdateContextPasteAfter)
74 EVT_UPDATE_UI(ctID_TREE_PASTE_AS_CHILD, ctConfigToolView::OnUpdateContextPasteAsChild)
75
76 EVT_MENU(ctID_ADD_CUSTOM_PROPERTY, ctConfigToolView::OnAddCustomProperty)
77 EVT_MENU(ctID_EDIT_CUSTOM_PROPERTY, ctConfigToolView::OnEditCustomProperty)
78 EVT_MENU(ctID_DELETE_CUSTOM_PROPERTY, ctConfigToolView::OnDeleteCustomProperty)
79 EVT_UPDATE_UI(ctID_ADD_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateAddCustomProperty)
80 EVT_UPDATE_UI(ctID_EDIT_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateEditCustomProperty)
81 EVT_UPDATE_UI(ctID_DELETE_CUSTOM_PROPERTY, ctConfigToolView::OnUpdateDeleteCustomProperty)
82
4fe30bce 83 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, ctConfigToolView::OnTabSelect)
d7463f75
JS
84
85 EVT_MENU(ctID_SAVE_SETUP_FILE, ctConfigToolView::OnSaveSetupFile)
86 EVT_MENU(ctID_SAVE_CONFIGURE_COMMAND, ctConfigToolView::OnSaveConfigureCommand)
87 EVT_UPDATE_UI(ctID_SAVE_SETUP_FILE, ctConfigToolView::OnUpdateSaveSetupFile)
88 EVT_UPDATE_UI(ctID_SAVE_CONFIGURE_COMMAND, ctConfigToolView::OnUpdateSaveConfigureCommand)
e7767867
JS
89
90 EVT_MENU(wxID_FIND, ctConfigToolView::OnFind)
91 EVT_UPDATE_UI(wxID_FIND, ctConfigToolView::OnUpdateFind)
92
afc51590
JS
93 EVT_MENU(ctID_GO, ctConfigToolView::OnGo)
94 EVT_UPDATE_UI(ctID_GO, ctConfigToolView::OnUpdateGo)
95
d7463f75
JS
96END_EVENT_TABLE()
97
d7463f75
JS
98// What to do when a view is created. Creates actual
99// windows for displaying the view.
100bool ctConfigToolView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
101{
4fe30bce 102 wxGetApp().GetDocManager()->ActivateView(this, true);
d7463f75
JS
103 wxGetApp().GetMainFrame()->SetDocument((ctConfigToolDoc*) doc);
104 wxGetApp().GetMainFrame()->GetSetupPage()->SetDocument((ctConfigToolDoc*) doc) ;
105 wxGetApp().GetMainFrame()->GetConfigurePage()->SetDocument((ctConfigToolDoc*) doc) ;
106
4fe30bce 107 return true;
d7463f75
JS
108}
109
69da0d99 110void ctConfigToolView::OnDraw(wxDC *WXUNUSED(dc))
d7463f75
JS
111{
112}
113
114void ctConfigToolView::OnUpdate(wxView *WXUNUSED(sender), wxObject *hintObj)
115{
116 ctConfigToolDoc* doc = wxDynamicCast(GetDocument(), ctConfigToolDoc);
117 ctConfigTreeCtrl* treeCtrl = wxGetApp().GetMainFrame()->GetConfigTreeCtrl();
118 if (!treeCtrl)
119 return;
254a2129 120
d7463f75 121 wxASSERT (doc != NULL);
254a2129 122
d7463f75 123 ctConfigItem* selItem = NULL;
254a2129 124
d7463f75
JS
125 wxTreeItemId sel = treeCtrl->GetSelection();
126 if (sel.IsOk())
127 {
128 ctTreeItemData* data = (ctTreeItemData*) treeCtrl->GetItemData(sel);
129 if (data)
130 selItem = data->GetConfigItem() ;
131 }
254a2129 132
d7463f75
JS
133 ctConfigToolHint* hint = (ctConfigToolHint*) hintObj;
134 int hintOp = ctNoHint;
135 if (hint)
136 hintOp = hint->m_op;
254a2129 137
d7463f75
JS
138 switch (hintOp)
139 {
140 case ctInitialUpdate:
141 {
142 if (doc && doc->GetTopItem())
143 {
144 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->DeleteAllItems();
145 AddItems(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), doc->GetTopItem());
146 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Expand(doc->GetTopItem()->GetTreeItemId());
147 }
148 }
149 break;
254a2129 150 case ctSelChanged:
d7463f75
JS
151 {
152 if (selItem)
153 {
154 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(selItem);
254a2129 155 }
d7463f75
JS
156 }
157 break;
158 case ctAllSaved:
159 {
160 // TODO: update windows and frame title
161 }
162 break;
163 case ctFilenameChanged:
164 {
165 wxGetApp().GetMainFrame()->UpdateFrameTitle();
166 }
167 break;
168 case ctClear:
169 {
170 // TODO: update windows
171 treeCtrl->DeleteAllItems();
172 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(NULL);
173 break;
254a2129 174 }
d7463f75
JS
175 case ctValueChanged:
176 {
177 // ctConfigItem& ti = *(ctConfigItem *)hint->m_item;
178 wxGetApp().GetMainFrame()->GetPropertyEditor()->ShowItem(selItem);
254a2129 179 }
d7463f75 180 break;
254a2129 181
d7463f75
JS
182 default:
183 break;
184 }
185}
186
187// Clean up windows used for displaying the view.
69da0d99 188bool ctConfigToolView::OnClose(bool WXUNUSED(deleteWindow))
d7463f75
JS
189{
190 if (!GetDocument()->Close())
4fe30bce 191 return false;
d7463f75
JS
192
193 ctConfigToolHint hint(NULL, ctClear);
194 GetDocument()->UpdateAllViews (NULL, & hint);
195
4fe30bce 196 wxGetApp().GetDocManager()->ActivateView(this, false);
d7463f75 197
4fe30bce 198 Activate(false);
254a2129 199
d7463f75
JS
200 wxGetApp().GetMainFrame()->SetDocument(NULL);
201 wxGetApp().GetMainFrame()->GetSetupPage()->SetDocument(NULL) ;
202 wxGetApp().GetMainFrame()->GetConfigurePage()->SetDocument(NULL) ;
203
4fe30bce 204 return true;
d7463f75
JS
205}
206
207void ctConfigToolView::OnChangeFilename()
208{
209 if (wxGetApp().GetTopWindow() && GetDocument())
210 {
211 wxString docTitle(wxFileNameFromPath(GetDocument()->GetFilename()));
212 wxStripExtension(docTitle);
213 GetDocument()->SetTitle(docTitle);
214
215 wxString name(GetDocument()->GetFilename());
216 wxStripExtension(name);
217
218 wxString title;
219
220 wxString modifiedMarker;
221 if (GetDocument()->IsModified())
222 modifiedMarker = wxT("*");
223
224 title = docTitle + modifiedMarker + wxString(wxT(" - ")) + wxGetApp().GetSettings().GetAppName() ;
225
226 ((wxFrame*) wxGetApp().GetTopWindow())->SetTitle(title);
227 }
228}
229
230// General disabler
231void ctConfigToolView::OnUpdateDisable(wxUpdateUIEvent& event)
232{
4fe30bce 233 event.Enable( false );
d7463f75
JS
234}
235
236void ctConfigToolView::OnUpdateAddItem(wxUpdateUIEvent& event)
237{
4fe30bce 238 event.Enable( true );
d7463f75
JS
239}
240
241/// Add item and its children to the tree
242void ctConfigToolView::AddItems(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
243{
244 SyncItem(treeControl, item);
245
246 int count = item->GetChildCount();
247 int i;
248 for (i = 0; i < count; i++)
249 {
250 ctConfigItem* child = item->GetChild(i);
251 AddItems(treeControl, child);
252 }
253}
254
255/// Gets the tree item in sync with the item
256void ctConfigToolView::SyncItem(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
257{
258 if (!item->GetTreeItemId().IsOk())
259 {
260 if (!item->GetParent())
261 {
262 item->SetTreeItem(treeControl->AddRoot(_(""), -1, -1, new ctTreeItemData(item)));
263 }
264 else
265 {
266 // Find the item to insert the new item after.
267 ctConfigItem* previousItem = item->FindPreviousSibling();
268 if (previousItem && previousItem->GetTreeItemId().IsOk())
269 {
270 item->SetTreeItem(treeControl->InsertItem(item->GetParent()->GetTreeItemId(),
271 previousItem->GetTreeItemId(),
272 item->GetName(), -1, -1, new ctTreeItemData(item)));
273 }
274 else if (!previousItem && item->GetParent()->GetChildCount() > 1
275 )
276 {
277 // Insert at the beginning
278 item->SetTreeItem(treeControl->InsertItem(item->GetParent()->GetTreeItemId(),
279 (size_t) 0, // Insert at first position
280 item->GetName(), -1, -1, new ctTreeItemData(item)));
281 }
282 else
283 {
284 item->SetTreeItem(treeControl->AppendItem(item->GetParent()->GetTreeItemId(),
285 item->GetName(), -1, -1, new ctTreeItemData(item)));
286 }
287 }
288 }
289
290 if (item->GetTreeItemId().IsOk())
291 {
292 treeControl->SetItemText(item->GetTreeItemId(), item->GetName());
293
294 int iconId = 0;
295
296 if (item->GetType() == ctTypeGroup)
297 {
298 iconId = treeControl->GetIconTable().GetIconId(wxT("Group"), 0, item->IsActive());
299 }
300 else if (item->GetType() == ctTypeCheckGroup)
301 {
302 iconId = treeControl->GetIconTable().GetIconId(wxT("CheckGroup"), item->IsEnabled() ? 0 : 1, item->IsActive());
303 }
304 else if (item->GetType() == ctTypeRadioGroup)
305 {
306 iconId = treeControl->GetIconTable().GetIconId(wxT("RadioGroup"), item->IsEnabled() ? 0 : 1, item->IsActive());
307 }
308 else if (item->GetType() == ctTypeBoolCheck)
309 {
310 iconId = treeControl->GetIconTable().GetIconId(wxT("Checkbox"), item->IsEnabled() ? 0 : 1, item->IsActive());
311 }
312 else if (item->GetType() == ctTypeBoolRadio)
313 {
314 iconId = treeControl->GetIconTable().GetIconId(wxT("Radiobutton"), item->IsEnabled() ? 0 : 1, item->IsActive());
315 }
316 treeControl->SetItemImage(item->GetTreeItemId(), iconId, wxTreeItemIcon_Normal);
317 treeControl->SetItemImage(item->GetTreeItemId(), iconId, wxTreeItemIcon_Selected);
318
319 if (treeControl->GetSelection() == item->GetTreeItemId())
320 {
321 if (wxGetApp().GetMainFrame()->GetPropertyEditor()->GetItem())
322 wxGetApp().GetMainFrame()->GetPropertyEditor()->UpdateTitle();
323 }
324 }
325}
326
327/// Clicked an icon
328void ctConfigToolView::OnIconLeftDown(ctConfigTreeCtrl* treeControl, ctConfigItem* item)
329{
330 if (!item->IsActive())
331 return;
332
333 if (item->GetType() == ctTypeCheckGroup ||
334 item->GetType() == ctTypeBoolCheck ||
335 item->GetType() == ctTypeBoolRadio ||
336 item->GetType() == ctTypeRadioGroup
337 )
338 {
339 // Don't toggle a radio button that's already
340 // enabled.
341 if ((item->GetType() == ctTypeBoolRadio || item->GetType() == ctTypeRadioGroup)
342 && item->IsEnabled())
343 return;
344
345 item->Enable(!item->IsEnabled());
346
4fe30bce 347 GetDocument()->Modify(true);
d7463f75
JS
348 OnChangeFilename();
349
350 SyncItem(treeControl, item);
351
352 wxList considered;
d7463f75
JS
353 if ((item->GetType() == ctTypeBoolRadio || item->GetType() == ctTypeRadioGroup) && item->IsEnabled())
354 {
355 item->PropagateRadioButton(considered);
356 }
afc51590
JS
357 item->PropagateChange(considered);
358
359 // Update the setup.h and configure text
360 if (wxGetApp().GetMainFrame()->GetMainNotebook()->GetSelection() > 0)
361 {
362 RegenerateSetup();
363 }
d7463f75
JS
364 }
365}
366
367/// Returns the selected config item, if any.
368ctConfigItem* ctConfigToolView::GetSelection()
369{
370 wxTreeCtrl* treeCtrl = wxGetApp().GetMainFrame()->GetConfigTreeCtrl();
371 if (!treeCtrl)
372 return NULL;
373
374 wxTreeItemId sel = treeCtrl->GetSelection();
375 if (sel.IsOk())
376 {
377 ctTreeItemData* data = (ctTreeItemData*) treeCtrl->GetItemData(sel);
378 if (data)
379 return data->GetConfigItem() ;
380 }
381 return NULL;
382}
383
384/// Add a checkbox item
69da0d99 385void ctConfigToolView::OnAddCheckBoxItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
386{
387 AddItem(ctTypeBoolCheck, _("New checkbox item"));
388}
389
390/// Add a radiobutton item
69da0d99 391void ctConfigToolView::OnAddRadioButtonItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
392{
393 AddItem(ctTypeBoolRadio, _("New radio button item"));
394}
395
396/// Add a group item
69da0d99 397void ctConfigToolView::OnAddGroupItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
398{
399 AddItem(ctTypeGroup, _("New group item"));
400}
401
402/// Add a group option item
69da0d99 403void ctConfigToolView::OnAddCheckGroupItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
404{
405 AddItem(ctTypeCheckGroup, _("New check group item"));
406}
407
408/// Add a group option item
69da0d99 409void ctConfigToolView::OnAddRadioGroupItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
410{
411 AddItem(ctTypeRadioGroup, _("New radio group item"));
412}
413
414/// Add a string item
69da0d99 415void ctConfigToolView::OnAddStringItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
416{
417 AddItem(ctTypeString, _("New string item"));
418}
419
420/// Add an item
421void ctConfigToolView::AddItem(ctConfigType type, const wxString& msg)
422{
423 ctConfigItem* sel = GetSelection();
424 if (sel)
425 {
426 wxString name = wxGetTextFromUser(_("Please enter a name for the new item."),
427 msg, wxT(""));
428 if (!name.IsEmpty())
429 {
430 ctConfigItem* parent ;
431 ctConfigItem* insertBefore ;
432 if (sel->CanHaveChildren())
433 {
434 parent = sel;
435 insertBefore = NULL;
436 }
437 else
438 {
439 parent = sel->GetParent();
440 insertBefore = sel->FindNextSibling();
441 }
254a2129 442
d7463f75
JS
443 ctConfigItem* newItem = new ctConfigItem(NULL, type, name);
444 newItem->InitProperties();
445
446 newItem->GetDocument()->GetCommandProcessor()->Submit(
447 new ctConfigCommand(msg, ctCMD_NEW_ELEMENT, NULL, newItem,
448 parent, insertBefore));
449 }
450 }
451}
452
453/// Delete an item
69da0d99 454void ctConfigToolView::OnDeleteItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
455{
456 ctConfigItem* sel = GetSelection();
457 if (sel)
458 {
f8105809 459 wxString name(sel->GetName());
d7463f75 460 wxString msg;
f8105809 461 msg.Printf(_("Delete %s?"), (const wxChar*) name);
d7463f75
JS
462 if (wxYES == wxMessageBox(msg, _("Delete item"), wxICON_QUESTION|wxYES_NO))
463 {
464 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Delete(sel->GetTreeItemId());
465 }
466 }
467}
468
469/// Rename an item
69da0d99 470void ctConfigToolView::OnRenameItem(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
471{
472 ctConfigItem* sel = GetSelection();
473 if (sel)
474 {
475 wxString name = wxGetTextFromUser(_("Please enter a new name for the item."),
476 _("Rename item"), sel->GetName());
477 if (!name.IsEmpty())
478 {
479 sel->SetName(name);
480 SyncItem(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), sel);
481
482 ctConfigToolHint hint(NULL, ctSelChanged);
483 GetDocument()->UpdateAllViews (NULL, & hint);
484 }
485 }
486}
487
488/// Copy an item to the clipboard
69da0d99 489void ctConfigToolView::OnCopy(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
490{
491 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
492 ctConfigItem* sel = GetSelection();
493 if (sel)
494 {
495 doc->SetClipboardItem(sel->DeepClone());
496 }
497}
498
499/// Copy an item to the clipboard and cut the item
69da0d99 500void ctConfigToolView::OnCut(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
501{
502 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
503 ctConfigItem* sel = GetSelection();
504 if (sel)
505 {
506 {
507 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
508 sel, (ctConfigItem*) NULL);
509 doc->GetCommandProcessor()->Submit(cmd);
510 }
511 }
512}
513
514/// Paste an item from the clipboard to the tree
69da0d99 515void ctConfigToolView::OnPaste(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
516{
517 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
518 ctConfigItem* sel = GetSelection();
519 if (sel && doc->GetClipboardItem())
520 {
521 ctConfigItem* parent ;
522 ctConfigItem* insertBefore ;
523 if (sel->CanHaveChildren())
524 {
525 parent = sel;
526 insertBefore = NULL;
527 }
528 else
529 {
530 parent = sel->GetParent();
531 insertBefore = sel->FindNextSibling();
532 }
533
534 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
535 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
536 NULL, newItem, parent, insertBefore);
537 doc->GetCommandProcessor()->Submit(cmd);
538 }
539}
540
541/// Update for copy command
542void ctConfigToolView::OnUpdateCopy(wxUpdateUIEvent& event)
543{
544 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
545 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
546}
547
548/// Update for cut
549void ctConfigToolView::OnUpdateCut(wxUpdateUIEvent& event)
550{
551 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
552 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
553}
554
555/// Update for paste
556void ctConfigToolView::OnUpdatePaste(wxUpdateUIEvent& event)
557{
558 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
559 event.Enable( doc && doc->GetClipboardItem() && GetSelection() );
560}
561
562/// Copy an item to the clipboard
69da0d99 563void ctConfigToolView::OnContextCopy(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
564{
565 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
566 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
567 if (doc && sel)
568 {
569 doc->SetClipboardItem(sel->DeepClone());
570 }
571}
572
573/// Copy an item to the clipboard and cut the item
69da0d99 574void ctConfigToolView::OnContextCut(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
575{
576 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
577 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
578 if (sel)
579 {
580 {
581 ctConfigCommand* cmd = new ctConfigCommand(wxT("Cut Config Item"), ctCMD_CUT,
582 sel, (ctConfigItem*) NULL);
583 doc->GetCommandProcessor()->Submit(cmd);
584 }
585 }
586}
587
588/// Paste an item from the clipboard to the tree
69da0d99 589void ctConfigToolView::OnContextPasteBefore(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
590{
591 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
592 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
593 if (sel && doc->GetClipboardItem())
594 {
595 ctConfigItem* parent = sel->GetParent();
596 ctConfigItem* insertBefore = sel;
597
598 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
599 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
600 NULL, newItem, parent, insertBefore);
601 doc->GetCommandProcessor()->Submit(cmd);
602 }
603}
604
605/// Paste an item from the clipboard to the tree
69da0d99 606void ctConfigToolView::OnContextPasteAfter(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
607{
608 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
609 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
610 if (sel && doc->GetClipboardItem())
611 {
612 ctConfigItem* parent = sel->GetParent();
613 ctConfigItem* insertBefore = sel->FindNextSibling();
614
615 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
616 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
617 NULL, newItem, parent, insertBefore);
618 doc->GetCommandProcessor()->Submit(cmd);
619 }
620}
621
622/// Paste an item from the clipboard to the tree
69da0d99 623void ctConfigToolView::OnContextPasteAsChild(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
624{
625 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
626 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
627 if (sel && doc->GetClipboardItem())
628 {
629 if (sel->CanHaveChildren())
630 {
631 ctConfigItem* parent = sel;
632 ctConfigItem* insertBefore = NULL;
254a2129 633
d7463f75
JS
634 ctConfigItem* newItem = doc->GetClipboardItem()->DeepClone();
635 ctConfigCommand* cmd = new ctConfigCommand(wxT("Paste Config Item"), ctCMD_PASTE,
636 NULL, newItem, parent, insertBefore);
637 doc->GetCommandProcessor()->Submit(cmd);
638 }
639 }
640}
641
642/// Copy an item to the clipboard
643void ctConfigToolView::OnUpdateContextCopy(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/// Copy an item to the clipboard and cut the item
651void ctConfigToolView::OnUpdateContextCut(wxUpdateUIEvent& event)
652{
653 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
654 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
655 event.Enable( doc && sel && sel->GetParent() );
656}
657
658/// Paste an item from the clipboard to the tree
659void ctConfigToolView::OnUpdateContextPasteBefore(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
667void ctConfigToolView::OnUpdateContextPasteAfter(wxUpdateUIEvent& event)
668{
669 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
670 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
671 event.Enable( doc && sel && sel->GetParent() && doc->GetClipboardItem() );
672}
673
674/// Paste an item from the clipboard to the tree
675void ctConfigToolView::OnUpdateContextPasteAsChild(wxUpdateUIEvent& event)
676{
677 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
678 ctConfigItem* sel = wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetContextItem();
679 event.Enable( doc && sel && sel->CanHaveChildren() && doc->GetClipboardItem() );
680}
681
682/// Item help
69da0d99 683void ctConfigToolView::OnItemHelp(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
684{
685 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
686 if ( doc && GetSelection() )
687 {
688 wxString helpTopic = GetSelection()->GetPropertyString(wxT("help-topic"));
689 if (!helpTopic.IsEmpty())
690 {
691 wxGetApp().GetReferenceHelpController().DisplaySection(helpTopic);
692 }
693 }
694}
695
696/// Item help update
697void ctConfigToolView::OnUpdateItemHelp(wxUpdateUIEvent& event)
698{
699 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
700 event.Enable( doc && GetSelection() );
701}
702
703/// Add a custom property
69da0d99 704void ctConfigToolView::OnAddCustomProperty(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
705{
706 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
707 ctConfigItem* sel = GetSelection();
708 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
709 if (doc && sel && editor)
710 {
711 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
4fe30bce 712 wxID_ANY, _("Add a custom property"));
d7463f75
JS
713 if (dialog.ShowModal() == wxID_OK)
714 {
715 wxString name = dialog.GetPropertyName();
716 wxString type = dialog.GetPropertyType();
717 wxString descr = dialog.GetPropertyDescription();
718 wxString editorType = dialog.GetEditorType();
719 wxArrayString choices = dialog.GetChoices();
720
721 if (sel->GetProperties().FindProperty(name))
722 {
723 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
724 wxOK|wxICON_INFORMATION);
725 return;
726 }
727 ctProperty* property = new ctProperty;
728 if (type == wxT("bool"))
4fe30bce 729 property->GetVariant() = wxVariant(false, name);
d7463f75
JS
730 else if (type == wxT("double"))
731 property->GetVariant() = wxVariant((double) 0.0, name);
732 else if (type == wxT("long"))
733 property->GetVariant() = wxVariant((long) 0, name);
734 else
735 property->GetVariant() = wxVariant(wxT(""), name);
4fe30bce 736 property->SetCustom(true);
d7463f75
JS
737 property->SetDescription(descr);
738 property->SetChoices(choices);
739 property->SetEditorType(editorType);
740
741 sel->GetProperties().AddProperty(property);
742 editor->ShowItem(sel);
743 OnChangeFilename();
744 }
745 }
746}
747
748/// Edit a custom property
69da0d99 749void ctConfigToolView::OnEditCustomProperty(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
750{
751 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
752 ctConfigItem* sel = GetSelection();
753 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
754 if (doc && sel && editor)
755 {
756 int row;
757 ctProperty* property = editor->FindSelectedProperty(row) ;
758 if (property && property->IsCustom())
759 {
760 wxString oldName = property->GetName();
761 wxString oldDescription = property->GetDescription();
762 wxString oldType = property->GetVariant().GetType();
763 wxString oldEditorType = property->GetEditorType();
764 wxArrayString oldChoices = property->GetChoices();
254a2129 765
d7463f75 766 ctCustomPropertyDialog dialog(wxGetApp().GetMainFrame(),
4fe30bce 767 wxID_ANY, _("Edit custom property"));
d7463f75
JS
768 dialog.SetPropertyName(oldName);
769 dialog.SetPropertyType(oldType);
770 dialog.SetPropertyDescription(oldDescription);
771 if (dialog.ShowModal() == wxID_OK)
772 {
773 wxString name = dialog.GetPropertyName();
774 wxString type = dialog.GetPropertyType();
775 wxString editorType = dialog.GetEditorType();
776 wxArrayString choices = dialog.GetChoices();
777 wxString descr = dialog.GetPropertyDescription();
254a2129 778
d7463f75
JS
779 if (name != oldName && sel->GetProperties().FindProperty(name))
780 {
781 wxMessageBox(_("Sorry, this name already exists."), _T("Add custom property"),
782 wxOK|wxICON_INFORMATION);
783 return;
784 }
785 if (type != oldType)
786 {
787 if (type == wxT("bool"))
4fe30bce 788 property->GetVariant() = wxVariant(false, name);
d7463f75
JS
789 else if (type == wxT("double"))
790 property->GetVariant() = wxVariant((double) 0.0, name);
791 else if (type == wxT("long"))
792 property->GetVariant() = wxVariant((long) 0, name);
793 else
794 property->GetVariant() = wxVariant(wxT(""), name);
795 }
796 if (name != oldName)
797 property->GetVariant().SetName(name);
798
799 if (choices != oldChoices)
800 property->SetChoices(choices);
801
802 if (editorType != oldEditorType)
803 property->SetEditorType(editorType);
804
805 if (name != oldName)
806 property->GetVariant().SetName(name);
807
4fe30bce 808 property->SetCustom(true);
d7463f75
JS
809
810 if (descr != oldDescription)
811 property->SetDescription(descr);
254a2129 812
d7463f75
JS
813 editor->ShowItem(sel);
814 OnChangeFilename();
815 }
816 }
817 }
818}
819
820/// Delete a custom property
69da0d99 821void ctConfigToolView::OnDeleteCustomProperty(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
822{
823 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
824 ctConfigItem* sel = GetSelection();
825 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
826 if (doc && sel && editor)
827 {
828 int row;
829 ctProperty* property = editor->FindSelectedProperty(row) ;
830 if (property && property->IsCustom())
831 {
832 wxString name(property->GetName());
833 wxString msg;
834 msg.Printf(_("Delete custom property '%s'?"), (const wxChar*) name);
835 if (wxYES == wxMessageBox(msg, _("Delete property"), wxICON_EXCLAMATION|wxYES_NO))
836 {
837 sel->GetProperties().RemoveProperty(property);
838 editor->ShowItem(sel);
839 delete property;
840 OnChangeFilename();
841 }
842 }
843 }
844}
845
846/// Add a custom property: update event
847void ctConfigToolView::OnUpdateAddCustomProperty(wxUpdateUIEvent& event)
848{
849 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
850 event.Enable( doc && GetSelection() && GetSelection()->GetParent() );
851}
852
853/// Edit a custom property: update event
854void ctConfigToolView::OnUpdateEditCustomProperty(wxUpdateUIEvent& event)
855{
856 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
857 ctConfigItem* sel = GetSelection();
858 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
859 int row;
860 event.Enable( doc && sel && sel->GetParent() && editor &&
861 editor->FindSelectedProperty(row) &&
862 editor->FindSelectedProperty(row)->IsCustom() );
863}
864
865/// Delete a custom property: update event
866void ctConfigToolView::OnUpdateDeleteCustomProperty(wxUpdateUIEvent& event)
867{
868 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
869 ctConfigItem* sel = GetSelection();
870 ctPropertyEditor* editor = wxGetApp().GetMainFrame()->GetPropertyEditor();
871 int row;
872 event.Enable( doc && sel && sel->GetParent() && editor &&
873 editor->FindSelectedProperty(row) &&
874 editor->FindSelectedProperty(row)->IsCustom() );
875}
876
877/// Regenerate setup.h and configure command
878void ctConfigToolView::RegenerateSetup()
879{
880 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
881 ctOutputWindow* setupPage = wxGetApp().GetMainFrame()->GetSetupPage() ;
882 ctOutputWindow* configurePage = wxGetApp().GetMainFrame()->GetConfigurePage() ;
883
884 wxString setupStr = doc->GenerateSetup();
885 wxString configureStr = doc->GenerateConfigureCommand();
886
887 setupPage->SetText(setupStr);
888 configurePage->SetText(configureStr);
889}
890
891/// Regenerate if selected a tab
892void ctConfigToolView::OnTabSelect(wxNotebookEvent& event)
893{
894 if (wxGetApp().GetMainFrame()->GetMainNotebook() != event.GetEventObject())
895 {
896 event.Skip();
897 return;
898 }
899
900 if (event.GetSelection() > 0)
901 {
902 RegenerateSetup();
903 }
904}
905
69da0d99 906void ctConfigToolView::OnSaveSetupFile(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
907{
908 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
909 wxString setupStr = doc->GenerateSetup();
910
911 wxString filename = _T("setup.h");
afc51590
JS
912 wxString path = wxGetApp().GetSettings().m_lastSetupSaveDir;
913 if (path.IsEmpty())
4fe30bce 914 path = doc->GetFrameworkDir(false);
d7463f75 915 wxString wildcard = _T("Header files (*.h)|*.h|All files (*.*)|*.*");
254a2129 916
d7463f75
JS
917 wxFileDialog dialog(wxTheApp->GetTopWindow(),
918 _("Save Setup File As"),
919 path, filename ,
920 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
254a2129 921
d7463f75
JS
922 if (dialog.ShowModal() == wxID_OK)
923 {
924 wxString fullPath = dialog.GetPath();
afc51590 925 wxGetApp().GetSettings().m_lastSetupSaveDir = wxPathOnly(fullPath);
d7463f75
JS
926
927 wxFileOutputStream stream(fullPath);
928 if (!stream.Ok())
929 {
930 wxMessageBox(_("Sorry, could not save this file."), _("Save Setup File"), wxICON_EXCLAMATION|wxOK);
931 return;
932 }
933
934 stream << setupStr;
254a2129 935 }
d7463f75
JS
936}
937
69da0d99 938void ctConfigToolView::OnSaveConfigureCommand(wxCommandEvent& WXUNUSED(event))
d7463f75
JS
939{
940 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
941 wxString configureStr = doc->GenerateConfigureCommand();
942
943 wxString filename = _T("configurewx.sh");
afc51590
JS
944 wxString path = wxGetApp().GetSettings().m_lastSetupSaveDir;
945 if (path.IsEmpty())
4fe30bce 946 path = doc->GetFrameworkDir(false);
d7463f75 947 wxString wildcard = _T("Shell script files (*.sh)|*.sh|All files (*.*)|*.*");
254a2129 948
d7463f75
JS
949 wxFileDialog dialog(wxTheApp->GetTopWindow(),
950 _("Save Configure Command File As"),
951 path, filename ,
952 wildcard, wxSAVE|wxOVERWRITE_PROMPT);
254a2129 953
d7463f75
JS
954 if (dialog.ShowModal() == wxID_OK)
955 {
956 wxString fullPath = dialog.GetPath();
afc51590 957 wxGetApp().GetSettings().m_lastSetupSaveDir = wxPathOnly(fullPath);
d7463f75
JS
958
959 wxFileOutputStream stream(fullPath);
960 if (!stream.Ok())
961 {
962 wxMessageBox(_("Sorry, could not save this file."), _("Save Configure Command File"), wxICON_EXCLAMATION|wxOK);
963 return;
964 }
965
966 stream << configureStr;
254a2129 967 }
d7463f75
JS
968}
969
970void ctConfigToolView::OnUpdateSaveSetupFile(wxUpdateUIEvent& event)
971{
4fe30bce 972 event.Enable(true);
d7463f75
JS
973}
974
975void ctConfigToolView::OnUpdateSaveConfigureCommand(wxUpdateUIEvent& event)
976{
4fe30bce 977 event.Enable(true);
d7463f75 978}
e7767867
JS
979
980/// Find text
69da0d99 981void ctConfigToolView::OnFind(wxCommandEvent& WXUNUSED(event))
e7767867
JS
982{
983 ctFindReplaceDialog* dialog = wxGetApp().GetMainFrame()->GetFindDialog();
984 if (dialog)
985 {
986 dialog->Raise();
987 }
988
989 if (!dialog)
990 {
991 int style = wxFR_NOUPDOWN;
992 wxString caption(wxT("Find text in settings"));
993 int flags = wxFR_DOWN;
994 if (wxGetApp().GetSettings().m_matchCase)
995 flags|=wxFR_MATCHCASE;
996 if (wxGetApp().GetSettings().m_matchWholeWord)
997 flags|=wxFR_WHOLEWORD;
998
999 ctFindReplaceDialog::sm_findData.SetFlags(flags);
1000
1001 dialog = new ctFindReplaceDialog(wxGetApp().GetMainFrame(), caption, style);
4fe30bce 1002 dialog->Show(true);
e7767867
JS
1003 }
1004}
1005
1006/// Update find text
1007void ctConfigToolView::OnUpdateFind(wxUpdateUIEvent& event)
1008{
4fe30bce 1009 event.Enable(true);
e7767867
JS
1010}
1011
afc51590 1012/// Save default file type
69da0d99 1013void ctConfigToolView::OnGo(wxCommandEvent& WXUNUSED(event))
afc51590
JS
1014{
1015 ctConfigToolDoc* doc = (ctConfigToolDoc*) GetDocument();
1016 wxString path = wxGetApp().GetSettings().m_lastSetupSaveDir;
1017 if (!path.IsEmpty())
1018 {
1019 if (wxGetApp().GetSettings().m_defaultFileKind == wxT("Setup file"))
1020 {
1021 // setup.h
1022 wxString setupStr = doc->GenerateSetup();
1023
1024 wxString fullPath = path + wxFILE_SEP_PATH + wxT("setup.h");
1025 if (wxFileExists(fullPath))
1026 {
1027 wxString msg;
1028 msg.Printf(wxT("Overwrite existing file %s?"), (const wxChar*) fullPath);
1029 int ans = wxMessageBox(msg, _("Save Setup File"), wxICON_QUESTION|wxYES_NO|wxCANCEL);
1030 if (ans == wxCANCEL)
1031 return;
1032 if (ans == wxNO)
1033 return;
1034 }
1035 wxFileOutputStream stream(fullPath);
1036 if (!stream.Ok())
1037 {
1038 wxMessageBox(_("Sorry, could not save this file."), _("Save Setup File"), wxICON_EXCLAMATION|wxOK);
1039 return;
1040 }
1041 stream << setupStr;
1042 }
1043 else if (wxGetApp().GetSettings().m_defaultFileKind == wxT("Configure script"))
1044 {
1045 // configurewx.sh
1046 wxString configureStr = doc->GenerateConfigureCommand();
1047
1048 wxString fullPath = path + wxFILE_SEP_PATH + wxT("configurewx.sh");
1049 if (wxFileExists(fullPath))
1050 {
1051 wxString msg;
1052 msg.Printf(wxT("Overwrite existing file %s?"), (const wxChar*) fullPath);
1053 int ans = wxMessageBox(msg, _("Save Configure Script"), wxICON_QUESTION|wxYES_NO|wxCANCEL);
1054 if (ans == wxCANCEL)
1055 return;
1056 if (ans == wxNO)
1057 return;
1058 }
1059 wxFileOutputStream stream(fullPath);
1060 if (!stream.Ok())
1061 {
1062 wxMessageBox(_("Sorry, could not save this file."), _("Save Configure Script"), wxICON_EXCLAMATION|wxOK);
1063 return;
1064 }
1065 stream << configureStr;
1066 }
1067 else
1068 {
1069 wxMessageBox(wxT("Unrecognised default file type."));
1070 }
1071 }
1072}
1073
1074/// Update
1075void ctConfigToolView::OnUpdateGo(wxUpdateUIEvent& event)
1076{
1077 wxString path = wxGetApp().GetSettings().m_lastSetupSaveDir;
1078 event.Enable(!path.IsEmpty());
1079}
1080
e7767867
JS
1081//----------------------------------------------------------------------------
1082// ctFindReplaceDialog
1083//----------------------------------------------------------------------------
1084
1085BEGIN_EVENT_TABLE(ctFindReplaceDialog, wxFindReplaceDialog)
4fe30bce
WS
1086 EVT_FIND(wxID_ANY, ctFindReplaceDialog::OnFind)
1087 EVT_FIND_NEXT(wxID_ANY, ctFindReplaceDialog::OnFind)
1088 EVT_FIND_CLOSE(wxID_ANY, ctFindReplaceDialog::OnClose)
e7767867
JS
1089END_EVENT_TABLE()
1090
1091wxFindReplaceData ctFindReplaceDialog::sm_findData;
1092wxString ctFindReplaceDialog::sm_currentItem = wxEmptyString;
1093
1094ctFindReplaceDialog::ctFindReplaceDialog( wxWindow *parent, const wxString& title,
1095 long style):
1096 wxFindReplaceDialog( parent, & sm_findData, title, style )
1097{
1098 sm_currentItem = wxEmptyString;
1099
1100 if (parent)
1101 ((ctMainFrame*) parent)->SetFindDialog(this);
1102}
1103
1104void ctFindReplaceDialog::OnFind(wxFindDialogEvent& event)
1105{
1106 wxString textToFind = event.GetFindString();
1107 bool matchCase = ((event.GetFlags() & wxFR_MATCHCASE) != 0);
1108 bool wholeWord = ((event.GetFlags() & wxFR_WHOLEWORD) != 0);
1109
1110 wxGetApp().GetSettings().m_matchCase = matchCase;
1111 wxGetApp().GetSettings().m_matchWholeWord = wholeWord;
1112
1113 if (!DoFind(textToFind, matchCase, wholeWord))
1114 {
1115 wxMessageBox(wxT("No more matches."), wxT("Search"), wxOK|wxICON_INFORMATION, this);
1116 }
1117}
1118
1119bool ctFindReplaceDialog::DoFind(const wxString& textToFind, bool matchCase, bool wholeWord, bool wrap)
1120{
1121 ctConfigToolDoc* doc = wxGetApp().GetMainFrame()->GetDocument();
1122 if (!doc)
4fe30bce 1123 return false;
e7767867
JS
1124 ctConfigToolView* view = (ctConfigToolView*) doc->GetFirstView();
1125
1126 ctConfigItem* currentItem = NULL;
1127 ctConfigItem* focusItem = view->GetSelection();
1128 if (!focusItem)
1129 {
1130 focusItem = doc->GetTopItem();
1131 if (!focusItem)
4fe30bce 1132 return false;
e7767867
JS
1133 }
1134
1135 if (!sm_currentItem.IsEmpty())
1136 {
1137 currentItem = doc->GetTopItem()->FindItem(sm_currentItem);
1138 }
1139
1140 // If we were at this item last time, skip the first one.
1141 bool skipFirstItem = (currentItem == focusItem);
1142 currentItem = FindNextItem(doc, currentItem, textToFind, matchCase, wholeWord, wrap,
1143 skipFirstItem);
1144
1145 if (currentItem)
1146 {
1147 sm_currentItem = currentItem->GetName();
1148 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->SelectItem(currentItem->GetTreeItemId());
4fe30bce 1149 return true;
e7767867
JS
1150 }
1151 else
1152 {
1153 sm_currentItem = wxEmptyString;
1154 }
1155
4fe30bce 1156 return false;
e7767867
JS
1157}
1158
1159ctConfigItem* ctFindReplaceDialog::FindNextItem(ctConfigToolDoc* doc,
1160 ctConfigItem* item,
1161 const wxString& text,
1162 bool matchCase,
1163 bool matchWordOnly,
1164 bool wrap,
1165 bool skipFirst)
1166{
1167 ctConfigItem* firstInDoc = NULL;
1168
1169 wxString text2(text);
1170 if (!matchCase)
1171 text2.MakeLower();
1172
1173 ctConfigItem* found = NULL;
1174 ctConfigItem* next = item;
1175
1176 int i = 0;
1177 do
1178 {
1179 // If starting the search from beginning, we can now
1180 // set the value of 'item' in the 2nd iteration without immediately
1181 // dropping out of the while loop because card == next
1182 if (!item && (i > 0))
1183 item = firstInDoc;
1184
1185 // We might want to start from this item if skipFirst is false.
1186 if ((i == 0) && !skipFirst && next)
1187 {
1188 }
1189 else
1190 next = doc->FindNextItem(next, wrap);
1191
1192 // Save to be used in iteration 2
1193 if ((i == 0) && !item)
1194 firstInDoc = next;
1195
1196 if (next)
1197 {
1198 wxString str(next->GetName());
1199 wxString description(next->GetPropertyString(wxT("description")));
1200 wxString notes(next->GetPropertyString(wxT("notes")));
1201 if (!matchCase)
1202 {
1203 str.MakeLower();
1204 description.MakeLower();
1205 notes.MakeLower();
1206 }
1207 if (ctMatchString(str, text2, matchWordOnly) ||
1208 ctMatchString(description, text2, matchWordOnly) ||
1209 ctMatchString(notes, text2, matchWordOnly))
1210 {
1211 found = next;
1212 }
1213 }
1214 else
1215 break; // Didn't find an item at all
1216
1217 i ++;
1218 }
1219 while (!found && item != next);
1220
1221 if (item == found && !firstInDoc)
1222 return NULL;
1223 else
1224 return found;
1225}
1226
1227void ctFindReplaceDialog::OnClose(wxFindDialogEvent& event)
1228{
1229 bool matchCase = ((event.GetFlags() & wxFR_MATCHCASE) != 0);
1230 bool wholeWord = ((event.GetFlags() & wxFR_WHOLEWORD) != 0);
1231 wxGetApp().GetSettings().m_matchCase = matchCase;
1232 wxGetApp().GetSettings().m_matchWholeWord = wholeWord;
1233
1234 this->Destroy();
1235 ((ctMainFrame*) GetParent())->SetFindDialog(NULL);
1236}