1 /////////////////////////////////////////////////////////////////////////////
2 // Name: configtooldoc.h
3 // Purpose: Document class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "configtooldoc.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/process.h"
26 #include "wx/mimetype.h"
27 #include "wx/process.h"
31 #include "wx/textfile.h"
32 #include "wx/wfstream.h"
33 #include "wx/config.h"
34 #include "configtooldoc.h"
35 #include "configtoolview.h"
36 #include "configtree.h"
37 #include "mainframe.h"
39 #include "wxconfigtool.h"
40 #include "htmlparser.h"
42 IMPLEMENT_DYNAMIC_CLASS(ctConfigToolDoc
, wxDocument
)
45 ctConfigToolDoc::ctConfigToolDoc()
48 m_clipboardItem
= NULL
;
52 ctConfigToolDoc::~ctConfigToolDoc()
56 if (GetCommandProcessor())
57 GetCommandProcessor()->SetEditMenu(NULL
);
60 // Delete all the items not already deleted
61 void ctConfigToolDoc::DeleteItems()
68 /// Clears the clipboard item.
69 void ctConfigToolDoc::ClearClipboard()
72 delete m_clipboardItem
;
73 m_clipboardItem
= NULL
;
76 /// Sets the clipboard item.
77 void ctConfigToolDoc::SetClipboardItem(ctConfigItem
* item
)
80 delete m_clipboardItem
;
81 m_clipboardItem
= item
;
85 // Closes and clears the document
86 bool ctConfigToolDoc::OnCloseDocument()
88 if (wxDocument::OnCloseDocument())
90 ctConfigToolHint
hint(NULL
, ctClear
);
91 UpdateAllViews (NULL
, & hint
);
103 bool ctConfigToolDoc::Save()
105 if (!IsModified() && m_savedYet
) return true;
107 bool ret
= (m_documentFile
== wxT("") || !m_savedYet
) ?
109 OnSaveDocument(m_documentFile
);
111 SetDocumentSaved(true);
115 // Create the document
116 bool ctConfigToolDoc::OnCreate(const wxString
& path
, long flags
)
118 GetCommandProcessor()->SetEditMenu(wxGetApp().GetMainFrame()->GetEditMenu());
119 GetCommandProcessor()->Initialize();
120 GetCommandProcessor()->ClearCommands();
122 // wxGetApp().m_currentDoc = this;
124 if (flags
& wxDOC_NEW
)
126 ctConfigItem
* rootItem
= new ctConfigItem(NULL
, ctTypeGroup
, _T("Configuration"));
127 //rootItem->InitProperties();
128 rootItem
->GetProperties().AddProperty(
130 wxT("The item description."),
131 wxVariant(wxT(""), wxT("description")),
134 rootItem
->SetPropertyString(_T("description"),
135 _T("<B>This is the top-level configuration item.</B>"));
138 SetTopItem(rootItem
);
141 SetDocumentSaved(false);
143 wxString
rootName(wxT("untitled"));
144 wxStripExtension(rootName
);
145 SetFilename(wxGetApp().GetSettings().GenerateFilename(rootName
));
148 // Creates the view, so do any view updating after this
149 bool success
= wxDocument::OnCreate(path
, flags
);
153 if (flags
& wxDOC_NEW
)
157 ctConfigToolHint
hint(NULL
, ctInitialUpdate
);
158 UpdateAllViews (NULL
, & hint
);
160 SetFilename(GetFilename(), true);
167 bool ctConfigToolDoc::OnSaveDocument(const wxString
& filename
)
171 const wxString
strOldPath(GetFilename());
173 // Do some backing up first
175 // This is the backup filename
176 wxString
backupFilename(filename
);
177 backupFilename
+= wxT(".bak");
179 // This is the temporary copy of the backup
180 wxString
tempFilename(filename
);
181 tempFilename
+= wxT(".tmp");
182 if (wxFileExists(tempFilename
))
183 wxRemoveFile(tempFilename
);
185 bool leaveBackup
= true;
187 bool saved
= DoSave(tempFilename
);
191 // Remove the old .bak file
192 if (wxFileExists(backupFilename
))
194 wxRemoveFile(backupFilename
);
197 // Copy the old file to the .bak
201 if (wxFileExists(filename
))
203 if (!wxRenameFile(filename
, backupFilename
))
205 wxCopyFile(filename
, backupFilename
);
206 wxRemoveFile(filename
);
212 if (wxFileExists(filename
))
213 wxRemoveFile(filename
);
216 // Finally, copy the temporary file to the proper filename
217 if (!wxRenameFile(tempFilename
, filename
))
219 wxCopyFile(tempFilename
, filename
);
220 wxRemoveFile(tempFilename
);
224 ((ctConfigToolView
*)GetFirstView())->OnChangeFilename();
225 SetDocumentSaved(true);
226 SetFilename(filename
);
227 wxGetApp().GetSettings().m_lastFilename
= filename
;
230 SetFilename(strOldPath
);
232 wxGetApp().GetMainFrame()->UpdateFrameTitle();
237 bool ctConfigToolDoc::OnOpenDocument(const wxString
& filename
)
241 bool opened
= DoOpen(filename
);
245 SetFilename(filename
);
246 wxGetApp().GetSettings().m_lastFilename
= filename
;
248 ((ctConfigToolView
*)GetFirstView())->OnChangeFilename();
250 RefreshDependencies();
252 // ctConfigToolHint hint(NULL, ctFilenameChanged);
253 ctConfigToolHint
hint(NULL
, ctInitialUpdate
);
254 UpdateAllViews (NULL
, & hint
);
257 SetDocumentSaved(true); // Necessary or it will pop up the Save As dialog
262 /// Save the settings file
263 bool ctConfigToolDoc::DoSave(const wxString
& filename
)
265 wxFileOutputStream
stream(filename
);
269 stream
<< wxT("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
270 stream
<< wxT("<settings xmlns=\"http://www.wxwidgets.org/wxs\" version=\"2.5.0.1\">");
272 DoSave(m_topItem
, stream
, 1);
274 stream
<< wxT("\n</settings>\n");
279 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
281 wxString str
= wxT("\n");
282 for (int i
= 0; i
< indent
; i
++)
283 str
<< wxT(' ') << wxT(' ');
287 /// Recursive helper function for file saving
288 bool ctConfigToolDoc::DoSave(ctConfigItem
* item
, wxOutputStream
& stream
, int indent
)
290 OutputIndentation(stream
, indent
*2);
292 wxString
name(item
->GetName());
295 if (item
->GetType() == ctTypeGroup
)
296 typeStr
= wxT("group");
297 else if (item
->GetType() == ctTypeCheckGroup
)
298 typeStr
= wxT("check-group");
299 else if (item
->GetType() == ctTypeRadioGroup
)
300 typeStr
= wxT("radio-group");
301 else if (item
->GetType() == ctTypeString
)
302 typeStr
= wxT("string");
303 else if (item
->GetType() == ctTypeBoolCheck
)
304 typeStr
= wxT("bool-check");
305 else if (item
->GetType() == ctTypeBoolRadio
)
306 typeStr
= wxT("bool-radio");
307 else if (item
->GetType() == ctTypeInteger
)
308 typeStr
= wxT("integer");
310 typeStr
= wxT("unknown");
312 stream
<< wxT("<setting type=\"") << typeStr
<< wxT("\">");
316 OutputIndentation(stream
, indent
*2);
317 if (item
->IsActive())
318 stream
<< wxT("<active>1</active>");
320 stream
<< wxT("<active>0</active>");
321 OutputIndentation(stream
, indent
*2);
322 if (item
->IsEnabled())
323 stream
<< wxT("<enabled>1</enabled>");
325 stream
<< wxT("<enabled>0</enabled>");
328 wxObjectList::compatibility_iterator node
= item
->GetProperties().GetList().GetFirst();
331 ctProperty
* prop
= (ctProperty
*) node
->GetData();
332 OutputIndentation(stream
, indent
*2);
333 stream
<< wxT("<") << prop
->GetName() ;
335 if (prop
->IsCustom())
337 stream
<< wxT(" custom=\"true\"");
338 stream
<< wxT(" type=\"") << prop
->GetVariant().GetType() << wxT("\"");
339 stream
<< wxT(" editor-type=\"") << prop
->GetEditorType() << wxT("\"");
340 stream
<< wxT(" description=\"") << prop
->GetDescription() << wxT("\"");
341 if (prop
->GetChoices().GetCount() > 0)
344 ctConfigItem::ArrayToString(prop
->GetChoices(), choices
);
345 stream
<< wxT(" choices=\"") << choices
<< wxT("\"");
351 stream
<< ctEscapeHTMLCharacters(prop
->GetVariant().GetString()) ;
352 stream
<< wxT("</") << prop
->GetName() << wxT(">");
354 node
= node
->GetNext();
358 node
= item
->GetChildren().GetFirst();
361 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
362 DoSave(child
, stream
, indent
);
364 node
= node
->GetNext();
369 OutputIndentation(stream
, indent
*2);
370 stream
<< wxT("</setting>");
375 /// Open the settings file
376 bool ctConfigToolDoc::DoOpen(const wxString
& filename
)
378 wxSimpleHtmlParser parser
;
379 if (parser
.ParseFile(filename
))
381 ctConfigToolHint
hint(NULL
, ctClear
);
382 UpdateAllViews (NULL
, & hint
);
385 if (parser
.GetTopLevelTag()->GetChildren())
387 wxSimpleHtmlTag
* settingsTag
= parser
.GetTopLevelTag()->GetChildren()->FindTag(wxT("settings"));
388 if (settingsTag
&& settingsTag
->GetChildren())
390 wxSimpleHtmlTag
* firstSettingTag
= settingsTag
->GetChildren();
392 DoOpen(firstSettingTag
, NULL
);
401 static bool GetHtmlBoolValue(const wxString
& value
)
403 if (value
== wxT("true") || value
== wxT("TRUE") || value
== wxT("1"))
409 static int GetHtmlIntegerValue(const wxString
& value
)
411 return wxAtoi(value
);
414 static double GetHtmlDoubleValue(const wxString
& value
)
416 return wxAtof(value
);
419 bool ctConfigToolDoc::DoOpen(wxSimpleHtmlTag
* tag
, ctConfigItem
* parent
)
421 ctConfigItem
* newItem
= NULL
;
422 if (tag
->NameIs(wxT("setting")))
424 wxSimpleHtmlAttribute
* attr
= tag
->FindAttribute(wxT("type"));
427 ctConfigType type
= ctTypeUnknown
;
428 wxString
typeStr(attr
->GetValue());
429 if (typeStr
== wxT("group"))
431 else if (typeStr
== wxT("option-group") || typeStr
== wxT("check-group"))
432 type
= ctTypeCheckGroup
;
433 else if (typeStr
== wxT("radio-group"))
434 type
= ctTypeRadioGroup
;
435 else if (typeStr
== wxT("bool-check"))
436 type
= ctTypeBoolCheck
;
437 else if (typeStr
== wxT("bool-radio"))
438 type
= ctTypeBoolRadio
;
439 else if (typeStr
== wxT("string"))
441 else if (typeStr
== wxT("integer"))
442 type
= ctTypeInteger
;
445 wxLogError(wxT("Unknown type %s"), (const wxChar
*) typeStr
);
447 if (type
!= ctTypeUnknown
)
449 newItem
= new ctConfigItem(parent
, type
, wxT(""));
450 newItem
->InitProperties();
456 wxSimpleHtmlTag
* childTag
= tag
->GetChildren();
460 if (childTag
->GetType() == wxSimpleHtmlTag_Open
)
462 if (childTag
->GetName() == wxT("setting"))
464 DoOpen(childTag
, newItem
);
466 else if (childTag
->GetName() == wxT("name"))
470 wxString
name(childTag
->GetNext()->GetTagText());
471 newItem
->SetName(name
);
474 else if (childTag
->GetName() == wxT("active"))
477 newItem
->SetActive(GetHtmlBoolValue(childTag
->GetNext()->GetTagText()));
479 else if (childTag
->GetName() == wxT("enabled"))
482 newItem
->Enable(GetHtmlBoolValue(childTag
->GetNext()->GetTagText()));
488 ctProperty
* prop
= newItem
->GetProperties().FindProperty(childTag
->GetName());
491 // A custom property, else an obsolete
492 // property that we should ignore.
494 if (childTag
->GetAttributeValue(isCustom
, wxT("custom")) &&
495 isCustom
== wxT("true"))
497 prop
= new ctProperty
;
499 wxString
name(childTag
->GetName());
500 wxString
type(wxT("string"));
502 wxString
editorType(wxT("string"));
503 wxString
description(wxT(""));
504 childTag
->GetAttributeValue(type
, wxT("type"));
505 childTag
->GetAttributeValue(type
, wxT("editor-type"));
506 childTag
->GetAttributeValue(type
, wxT("choices"));
507 childTag
->GetAttributeValue(description
, wxT("description"));
509 if (type
== wxT("bool"))
510 prop
->GetVariant() = wxVariant(false, name
);
511 else if (type
== wxT("double"))
512 prop
->GetVariant() = wxVariant((double) 0.0, name
);
513 else if (type
== wxT("long"))
514 prop
->GetVariant() = wxVariant((long) 0, name
);
516 prop
->GetVariant() = wxVariant(wxT(""), name
);
517 prop
->SetDescription(description
);
518 prop
->SetCustom(true);
519 prop
->SetEditorType(editorType
);
520 if (!choices
.IsEmpty())
523 ctConfigItem::StringToArray(choices
, arr
);
524 prop
->SetChoices(arr
);
526 newItem
->GetProperties().AddProperty(prop
);
531 if (prop
->GetVariant().GetType() == wxT("string"))
532 prop
->GetVariant() = childTag
->GetNext()->GetTagText();
533 else if (prop
->GetVariant().GetType() == wxT("long"))
534 prop
->GetVariant() = (long) GetHtmlIntegerValue(childTag
->GetNext()->GetTagText());
535 else if (prop
->GetVariant().GetType() == wxT("bool"))
536 prop
->GetVariant() = GetHtmlBoolValue(childTag
->GetNext()->GetTagText());
537 else if (prop
->GetVariant().GetType() == wxT("double"))
538 prop
->GetVariant() = (double) GetHtmlDoubleValue(childTag
->GetNext()->GetTagText());
543 childTag
= childTag
->GetNext();
548 /// Clear dependencies
549 void ctConfigToolDoc::ClearDependencies(ctConfigItem
* item
)
554 item
->GetDependents().Clear();
555 for ( wxObjectList::compatibility_iterator node
= item
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
557 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
558 ClearDependencies(child
);
562 /// Refresh dependencies
563 void ctConfigToolDoc::RefreshDependencies()
565 ClearDependencies(GetTopItem());
566 RefreshDependencies(GetTopItem());
569 /// Refresh dependencies
570 void ctConfigToolDoc::RefreshDependencies(ctConfigItem
* item
)
572 wxArrayString requiresArr
;
573 wxString
requires = item
->GetPropertyString(wxT("requires"));
574 wxString precludes
= item
->GetPropertyString(wxT("precludes"));
575 wxString enabledIf
= item
->GetPropertyString(wxT("enabled-if"));
576 wxString enabledIfNot
= item
->GetPropertyString(wxT("enabled-if-not"));
577 wxString indeterminateIf
= item
->GetPropertyString(wxT("indeterminate-if"));
578 wxString context
= item
->GetPropertyString(wxT("context"));
580 if (!requires.IsEmpty())
581 item
->StringToArray(requires, requiresArr
);
583 if (!precludes
.IsEmpty())
584 item
->StringToArray(precludes
, requiresArr
);
586 if (!enabledIfNot
.IsEmpty())
587 item
->StringToArray(enabledIfNot
, requiresArr
);
589 if (!enabledIf
.IsEmpty())
590 item
->StringToArray(enabledIf
, requiresArr
);
592 if (!indeterminateIf
.IsEmpty())
593 item
->StringToArray(indeterminateIf
, requiresArr
);
595 // Add the parent to the list of dependencies, if the
596 // parent is a check or radio group.
597 ctConfigItem
* parent
= item
->GetParent();
599 (parent
->GetType() == ctTypeCheckGroup
||
600 parent
->GetType() == ctTypeRadioGroup
))
601 requiresArr
.Add(parent
->GetName());
603 // Also look in 'context' since these items
604 // are another kind of dependency (switching to
605 // a different platform may cause the dependencies
606 // to be evaluated differently).
607 if (!context
.IsEmpty())
608 item
->StringToArray(context
, requiresArr
);
611 for (i
= 0; i
< requiresArr
.GetCount(); i
++)
613 wxString
itemName(requiresArr
[i
]);
614 ctConfigItem
* otherItem
= GetTopItem()->FindItem(itemName
);
615 if (otherItem
&& !otherItem
->GetDependents().Member(item
))
617 otherItem
->GetDependents().Append(item
);
620 for ( wxObjectList::compatibility_iterator node
= item
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
622 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
623 RefreshDependencies(child
);
627 /// Generate the text of a setup.h
628 wxString
ctConfigToolDoc::GenerateSetup()
631 str
<< wxT("/*\n * setup.h\n * Generated by wxConfigTool\n *\n */\n\n");
633 GenerateSetup(GetTopItem(), str
);
639 void ctConfigToolDoc::GenerateSetup(ctConfigItem
* item
, wxString
& str
)
641 // Generate the setup.h entries for this item
642 wxString name
= item
->GetName();
644 // We don't process the platform choice
645 if (item
->GetName() == wxT("Target"))
648 if (item
->IsInActiveContext() &&
649 (item
->GetType() == ctTypeCheckGroup
||
650 item
->GetType() == ctTypeRadioGroup
||
651 item
->GetType() == ctTypeBoolCheck
||
652 item
->GetType() == ctTypeBoolRadio
))
654 // TODO: write description
655 wxString name
= item
->GetName();
656 if (name
.Left(6) == wxT("wxUSE_") ||
657 name
== wxT("REMOVE_UNUSED_ARG") || // Hack alert: change to wxUSE_UNUSED_ARG_REMOVAL
658 name
.Find(wxT("COMPATIBILITY")) != wxNOT_FOUND
)
660 str
<< wxT("#define ") << name
;
661 if (item
->IsEnabled())
669 for ( wxObjectList::compatibility_iterator node
= item
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
671 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
672 GenerateSetup(child
, str
);
677 /// Generate a configure command
678 wxString
ctConfigToolDoc::GenerateConfigureCommand()
681 str
<< wxT("# configurewx\n# Generated by wxConfigTool\n\n");
683 wxString path
= GetFrameworkDir(true);
684 bool makeUnix
= true;
690 path
+= wxFILE_SEP_PATH
;
693 str
<< path
<< wxT("configure");
695 // Find the target to use
696 ctConfigItem
* platformsFolder
= GetTopItem()->FindItem(wxT("Target"));
699 for ( wxObjectList::compatibility_iterator node
= platformsFolder
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
701 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
702 if (child
->GetType() == ctTypeBoolRadio
&& child
->IsEnabled())
704 wxString configureCommand
= child
->GetPropertyString(wxT("configure-command"));
705 if (!configureCommand
.IsEmpty())
706 str
<< wxT(" ") << configureCommand
;
711 GenerateConfigureCommand(GetTopItem(), str
);
716 void ctConfigToolDoc::GenerateConfigureCommand(ctConfigItem
* item
, wxString
& str
)
718 // We don't process the platform group, since we've
720 if (item
->GetName() == wxT("Target"))
723 if (item
->IsInActiveContext() &&
724 (item
->GetType() == ctTypeCheckGroup
||
725 item
->GetType() == ctTypeRadioGroup
||
726 item
->GetType() == ctTypeBoolCheck
||
727 item
->GetType() == ctTypeBoolRadio
))
729 wxString name
= item
->GetName();
730 wxString configureCommand
= item
->GetPropertyString(wxT("configure-command"));
731 if (!configureCommand
.IsEmpty())
733 if (!item
->IsEnabled())
735 // Replace 'enable' with 'disable' if this
737 configureCommand
.Replace(wxT("--enable-"), wxT("--disable-"));
738 configureCommand
.Replace(wxT("--with-"), wxT("--without-"));
740 ctProperty
* prop
= item
->GetProperties().FindProperty(wxT("builtin"));
741 if (prop
&& prop
->GetVariant().GetType() == wxT("bool"))
743 bool builtin
= prop
->GetVariant().GetBool();
744 str
<< wxT(" ") << configureCommand
;
746 str
<< wxT("=builtin");
752 ctProperty
* prop
= item
->GetProperties().FindProperty(wxT("value"));
753 if (prop
&& prop
->GetVariant().GetType() == wxT("string"))
755 wxString val
= prop
->GetVariant().GetString();
756 if (item
->IsEnabled() && !val
.IsEmpty())
758 str
<< wxT(" ") << configureCommand
;
759 str
<< wxT("=\"") << val
<< wxT("\"");
761 // If the string is empty, ignore this parameter,
762 // since it's obviously intended to be supplied
763 // only if there's a string to use and it's enabled.
767 str
<< wxT(" ") << configureCommand
;
773 for ( wxObjectList::compatibility_iterator node
= item
->GetChildren().GetFirst(); node
; node
= node
->GetNext() )
775 ctConfigItem
* child
= (ctConfigItem
*) node
->GetData();
776 GenerateConfigureCommand(child
, str
);
780 /// Gets the current framework directory
781 wxString
ctConfigToolDoc::GetFrameworkDir(bool makeUnix
)
783 wxString path
= wxGetApp().GetSettings().m_frameworkDir
;
784 if (wxGetApp().GetSettings().m_useEnvironmentVariable
)
786 // Should probably allow other variables
787 // to be used, and maybe expand variables within m_frameworkDir
788 wxString
pathEnv(wxGetenv(wxT("WXWIN")));
792 path
.Replace(wxT("\\"), wxT("/"));
798 /// Finds the next item in the tree
799 ctConfigItem
* ctConfigToolDoc::FindNextItem(ctConfigItem
* item
, bool wrap
)
804 // First, try to find the first child
805 if (item
->GetChildCount() > 0)
807 return item
->GetChild(0);
811 ctConfigItem
* p
= item
;
814 ctConfigItem
* toFind
= FindNextSibling(p
);
821 // Finally, wrap around to the root.
828 /// Finds the next sibling in the tree
829 ctConfigItem
* ctConfigToolDoc::FindNextSibling(ctConfigItem
* item
)
831 if (item
->GetParent())
833 wxObjectList::compatibility_iterator node
= item
->GetParent()->GetChildren().Member(item
);
834 if (node
&& node
->GetNext())
836 ctConfigItem
* nextItem
= (ctConfigItem
*) node
->GetNext()->GetData();
845 * Implements a document editing command.
848 ctConfigCommand::ctConfigCommand(const wxString
& name
, int cmdId
,
849 ctConfigItem
* activeState
, ctConfigItem
* savedState
,
850 ctConfigItem
* parent
, ctConfigItem
* insertBefore
,
851 bool ignoreFirstTime
): wxCommand(true, name
)
853 m_activeState
= activeState
;
854 m_savedState
= savedState
;
855 m_ignoreThis
= ignoreFirstTime
;
859 m_insertBefore
= insertBefore
;
862 ctConfigCommand::ctConfigCommand(const wxString
& name
, int cmdId
,
863 ctConfigItem
* activeState
, ctProperties
* properties
,
864 bool ignoreFirstTime
): wxCommand(true, name
)
866 m_activeState
= activeState
;
868 m_properties
= properties
;
869 m_ignoreThis
= ignoreFirstTime
;
871 m_properties
= properties
;
873 m_insertBefore
= NULL
;
876 ctConfigCommand::~ctConfigCommand()
884 bool ctConfigCommand::Do()
886 return DoAndUndo(true);
889 bool ctConfigCommand::Undo()
891 return DoAndUndo(false);
894 // Combine Do and Undo into one
895 bool ctConfigCommand::DoAndUndo(bool doCmd
)
903 wxASSERT(m_savedState
== NULL
);
904 wxASSERT(m_activeState
!= NULL
);
906 ctConfigItem
* newItem
= m_activeState
->DeepClone();
907 ctConfigToolDoc
* doc
= m_activeState
->GetDocument();
909 // This will delete the old clipboard contents, if any.
910 doc
->SetClipboardItem(newItem
);
912 m_parent
= m_activeState
->GetParent();
913 m_insertBefore
= m_activeState
->FindNextSibling();
915 m_activeState
->Detach();
916 m_savedState
= m_activeState
;
917 m_activeState
= NULL
;
919 m_savedState
->GetDocument()->Modify(true);
920 ctConfigToolView
* view
= (ctConfigToolView
*) m_savedState
->GetDocument()->GetFirstView();
921 view
->OnChangeFilename();
925 wxASSERT(m_savedState
!= NULL
);
926 wxASSERT(m_activeState
== NULL
);
928 m_savedState
->GetDocument()->Modify(true);
929 m_savedState
->Attach(m_parent
, m_insertBefore
);
930 ctConfigToolView
* view
= (ctConfigToolView
*) m_savedState
->GetDocument()->GetFirstView();
931 view
->AddItems(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), m_savedState
);
932 m_activeState
= m_savedState
;
935 m_insertBefore
= NULL
;
936 view
->OnChangeFilename();
944 wxASSERT(m_savedState
!= NULL
);
945 wxASSERT(m_activeState
== NULL
);
947 m_savedState
->GetDocument()->Modify(true);
948 m_savedState
->Attach(m_parent
, m_insertBefore
);
949 ctConfigToolView
* view
= (ctConfigToolView
*) m_savedState
->GetDocument()->GetFirstView();
950 view
->AddItems(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), m_savedState
);
951 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->SelectItem(m_savedState
->GetTreeItemId());
952 m_activeState
= m_savedState
;
954 view
->OnChangeFilename();
958 wxASSERT(m_savedState
== NULL
);
959 wxASSERT(m_activeState
!= NULL
);
961 m_activeState
->GetDocument()->Modify(true);
962 ctConfigToolView
* view
= (ctConfigToolView
*) m_activeState
->GetDocument()->GetFirstView();
963 m_activeState
->Detach();
964 m_savedState
= m_activeState
;
965 m_activeState
= NULL
;
966 view
->OnChangeFilename();
970 case ctCMD_NEW_ELEMENT
:
974 wxASSERT(m_savedState
!= NULL
);
975 wxASSERT(m_activeState
== NULL
);
977 m_savedState
->GetDocument()->Modify(true);
978 m_savedState
->Attach(m_parent
, m_insertBefore
);
979 ctConfigToolView
* view
= (ctConfigToolView
*) m_savedState
->GetDocument()->GetFirstView();
980 view
->AddItems(wxGetApp().GetMainFrame()->GetConfigTreeCtrl(), m_savedState
);
981 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->SelectItem(m_savedState
->GetTreeItemId());
983 m_activeState
= m_savedState
;
988 wxASSERT(m_savedState
== NULL
);
989 wxASSERT(m_activeState
!= NULL
);
991 m_activeState
->GetDocument()->Modify(true);
992 m_activeState
->Detach();
993 m_savedState
= m_activeState
;
994 m_activeState
= NULL
;
998 case ctCMD_APPLY_PROPERTY
:
1000 wxASSERT(m_properties
!= NULL
);
1001 wxASSERT(m_activeState
!= NULL
);
1003 // Don't update the properties editor first time
1004 // around since it will be done one property at a time
1005 // initially (and no property editor update required)
1008 // Just swap the saved and current properties.
1009 ctProperties propsTemp
= m_activeState
->GetProperties() ;
1010 m_activeState
->GetProperties() = (* m_properties
);
1011 (* m_properties
) = propsTemp
;
1013 // Apply only those that need applying
1014 // (those properties in activeState that are not in propsTemp)
1015 wxObjectList::compatibility_iterator node
= m_activeState
->GetProperties().GetList().GetFirst();
1018 ctProperty
* prop
= (ctProperty
*) node
->GetData();
1019 ctProperty
* otherProp
= propsTemp
.FindProperty(prop
->GetName());
1020 if (otherProp
&& ((*prop
) != (*otherProp
)))
1022 m_activeState
->ApplyProperty(prop
, otherProp
->GetVariant());
1024 node
= node
->GetNext();
1026 m_activeState
->GetDocument()->Modify(true);
1027 ctConfigToolView
* view
= (ctConfigToolView
*) m_activeState
->GetDocument()->GetFirstView();
1030 ctConfigToolHint
hint(NULL
, ctValueChanged
);
1031 m_activeState
->GetDocument()->UpdateAllViews (NULL
, & hint
);
1034 m_ignoreThis
= false;
1042 IMPLEMENT_CLASS(ctConfiguration
, wxObject
)
1044 ctConfiguration::ctConfiguration()
1046 m_treeItemId
= wxTreeItemId();
1051 ctConfiguration::ctConfiguration(ctConfiguration
* parent
, const wxString
& name
)
1053 m_treeItemId
= wxTreeItemId();
1057 parent
->AddChild(this);
1060 ctConfiguration::~ctConfiguration()
1063 ctConfigTreeCtrl* treeCtrl = wxGetApp().GetMainFrame()->GetConfigTreeCtrl();
1064 if (m_treeItemId.IsOk() && treeCtrl)
1066 ctTreeItemData* data = (ctTreeItemData*) treeCtrl->GetItemData(m_treeItemId);
1068 data->SetConfigItem(NULL);
1071 GetParent()->RemoveChild(this);
1074 if (wxGetApp().GetMainFrame()->GetDocument() &&
1075 wxGetApp().GetMainFrame()->GetDocument()->GetTopItem() == this)
1076 wxGetApp().GetMainFrame()->GetDocument()->SetTopItem(NULL);
1083 /// Assignment operator.
1084 void ctConfiguration::operator= (const ctConfiguration
& configuration
)
1086 m_name
= configuration
.m_name
;
1087 m_description
= configuration
.m_description
;
1091 void ctConfiguration::Clear()
1093 wxObjectList::compatibility_iterator node
= m_children
.GetFirst();
1096 wxObjectList::compatibility_iterator next
= node
->GetNext();
1097 ctConfiguration
* child
= (ctConfiguration
*) node
->GetData();
1099 // This should delete 'node' too, assuming
1100 // child's m_parent points to 'this'. If not,
1101 // it'll be cleaned up by m_children.Clear().
1109 // Get the nth child
1110 ctConfiguration
* ctConfiguration::GetChild(int n
) const
1112 wxASSERT ( n
< GetChildCount() && n
> -1 );
1114 if ( n
< GetChildCount() && n
> -1 )
1116 ctConfiguration
* child
= wxDynamicCast(m_children
.Item(n
)->GetData(), ctConfiguration
);
1123 // Get the child count
1124 int ctConfiguration::GetChildCount() const
1126 return m_children
.GetCount();
1130 void ctConfiguration::AddChild(ctConfiguration
* configuration
)
1132 m_children
.Append(configuration
);
1133 configuration
->SetParent(this);
1136 /// Remove (but don't delete) a child
1137 void ctConfiguration::RemoveChild(ctConfiguration
* configuration
)
1139 m_children
.DeleteObject(configuration
);
1140 configuration
->SetParent(NULL
);
1143 /// Get the associated document (currently, assumes
1144 /// there's only ever one document active)
1145 ctConfigToolDoc
* ctConfiguration::GetDocument()
1147 ctConfigToolDoc
* doc
= wxGetApp().GetMainFrame()->GetDocument();
1151 /// Find an item in this hierarchy
1152 // TODO: ensure that names are unique, somehow.
1153 ctConfiguration
* ctConfiguration::FindConfiguration(const wxString
& name
)
1155 if (GetName() == name
)
1158 for ( wxObjectList::compatibility_iterator node
= GetChildren().GetFirst(); node
; node
= node
->GetNext() )
1160 ctConfiguration
* child
= (ctConfiguration
*) node
->GetData();
1161 ctConfiguration
* found
= child
->FindConfiguration(name
);
1168 /// Find the next sibling
1169 ctConfiguration
* ctConfiguration::FindNextSibling()
1173 wxObjectList::compatibility_iterator node
= GetParent()->GetChildren().Member(this);
1174 if (node
&& node
->GetNext())
1176 return (ctConfiguration
*) node
->GetNext()->GetData();
1181 /// Find the previous sibling
1182 ctConfiguration
* ctConfiguration::FindPreviousSibling()
1186 wxObjectList::compatibility_iterator node
= GetParent()->GetChildren().Member(this);
1187 if (node
&& node
->GetPrevious())
1189 return (ctConfiguration
*) node
->GetPrevious()->GetData();
1194 /// Create a clone of this and children
1195 ctConfiguration
* ctConfiguration::DeepClone()
1197 ctConfiguration
* newItem
= Clone();
1199 for ( wxObjectList::compatibility_iterator node
= GetChildren().GetFirst(); node
; node
= node
->GetNext() )
1201 ctConfiguration
* child
= (ctConfiguration
*) node
->GetData();
1202 ctConfiguration
* newChild
= child
->DeepClone();
1203 newItem
->AddChild(newChild
);
1208 /// Detach: remove from parent, and remove tree items
1209 void ctConfiguration::Detach()
1213 GetParent()->RemoveChild(this);
1215 GetDocument()->SetTopItem(NULL
);
1219 wxTreeItemId treeItem = GetTreeItemId();
1223 // Will delete the branch, but not the config items.
1224 wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->Delete(treeItem);
1228 /// Hide from tree: make sure tree deletions won't delete
1229 /// the config items
1230 void ctConfiguration::DetachFromTree()
1233 wxTreeItemId item = GetTreeItemId();
1236 ctTreeItemData* data = (ctTreeItemData*) wxGetApp().GetMainFrame()->GetConfigTreeCtrl()->GetItemData(item);
1237 data->SetConfigItem(NULL);
1238 m_treeItemId = wxTreeItemId();
1240 for ( wxNode* node = GetChildren().GetFirst(); node; node = node->GetNext() )
1242 ctConfiguration* child = (ctConfiguration*) node->GetData();
1243 child->DetachFromTree();