📄 configtooldoc.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: configtooldoc.h
// Purpose: Document class
// Author: Julian Smart
// Modified by:
// Created: 2003-06-04
// RCS-ID: $Id: configtooldoc.cpp,v 1.16 2006/01/16 16:05:58 ABX Exp $
// Copyright: (c) Julian Smart
// Licence:
/////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/process.h"
#include "wx/mimetype.h"
#include "wx/process.h"
#endif
#include "wx/textfile.h"
#include "wx/txtstrm.h"
#include "wx/wfstream.h"
#include "wx/config.h"
#include "configtooldoc.h"
#include "configtoolview.h"
#include "configtree.h"
#include "mainframe.h"
#include "utils.h"
#include "wxconfigtool.h"
#include "htmlparser.h"
IMPLEMENT_DYNAMIC_CLASS(ctConfigToolDoc, wxDocument)
// Ctor
ctConfigToolDoc::ctConfigToolDoc()
{
m_topItem = NULL;
m_clipboardItem = NULL;
}
// Dtor
ctConfigToolDoc::~ctConfigToolDoc()
{
DeleteItems();
ClearClipboard();
if (GetCommandProcessor())
GetCommandProcessor()->SetEditMenu(NULL);
}
// Delete all the items not already deleted
void ctConfigToolDoc::DeleteItems()
{
if (m_topItem)
delete m_topItem;
m_topItem = NULL;
}
/// Clears the clipboard item.
void ctConfigToolDoc::ClearClipboard()
{
if (m_clipboardItem)
delete m_clipboardItem;
m_clipboardItem = NULL;
}
/// Sets the clipboard item.
void ctConfigToolDoc::SetClipboardItem(ctConfigItem* item)
{
if (m_clipboardItem)
delete m_clipboardItem;
m_clipboardItem = item;
}
// Closes and clears the document
bool ctConfigToolDoc::OnCloseDocument()
{
if (wxDocument::OnCloseDocument())
{
ctConfigToolHint hint(NULL, ctClear);
UpdateAllViews (NULL, & hint);
DeleteItems();
return true;
}
else
{
return false;
}
}
// Saves the doc
bool ctConfigToolDoc::Save()
{
if (!IsModified() && m_savedYet) return true;
bool ret = (m_documentFile.empty() || !m_savedYet) ?
SaveAs() :
OnSaveDocument(m_documentFile);
if ( ret )
SetDocumentSaved(true);
return ret;
}
// Create the document
bool ctConfigToolDoc::OnCreate(const wxString& path, long flags)
{
GetCommandProcessor()->SetEditMenu(wxGetApp().GetMainFrame()->GetEditMenu());
GetCommandProcessor()->Initialize();
GetCommandProcessor()->ClearCommands();
// wxGetApp().m_currentDoc = this;
if (flags & wxDOC_NEW)
{
ctConfigItem* rootItem = new ctConfigItem(NULL, ctTypeGroup, _T("Configuration"));
//rootItem->InitProperties();
rootItem->GetProperties().AddProperty(
new ctProperty(
wxT("The item description."),
wxVariant(wxEmptyString, wxT("description")),
wxT("multiline")));
rootItem->SetPropertyString(_T("description"),
_T("<B>This is the top-level configuration item.</B>"));
SetTopItem(rootItem);
Modify(false);
SetDocumentSaved(false);
wxString rootName(wxT("untitled"));
wxStripExtension(rootName);
SetFilename(wxGetApp().GetSettings().GenerateFilename(rootName));
}
// Creates the view, so do any view updating after this
bool success = wxDocument::OnCreate(path, flags);
if (success)
{
if (flags & wxDOC_NEW)
{
wxBusyCursor wait;
ctConfigToolHint hint(NULL, ctInitialUpdate);
UpdateAllViews (NULL, & hint);
SetFilename(GetFilename(), true);
}
}
return success;
}
// Save the document
bool ctConfigToolDoc::OnSaveDocument(const wxString& filename)
{
wxBusyCursor cursor;
const wxString strOldPath(GetFilename());
// Do some backing up first
// This is the backup filename
wxString backupFilename(filename);
backupFilename += wxT(".bak");
// This is the temporary copy of the backup
wxString tempFilename(filename);
tempFilename += wxT(".tmp");
if (wxFileExists(tempFilename))
wxRemoveFile(tempFilename);
bool leaveBackup = true;
bool saved = DoSave(tempFilename);
if (saved)
{
// Remove the old .bak file
if (wxFileExists(backupFilename))
{
wxRemoveFile(backupFilename);
}
// Copy the old file to the .bak
if (leaveBackup)
{
if (wxFileExists(filename))
{
if (!wxRenameFile(filename, backupFilename))
{
wxCopyFile(filename, backupFilename);
wxRemoveFile(filename);
}
}
}
else
{
if (wxFileExists(filename))
wxRemoveFile(filename);
}
// Finally, copy the temporary file to the proper filename
if (!wxRenameFile(tempFilename, filename))
{
wxCopyFile(tempFilename, filename);
wxRemoveFile(tempFilename);
}
Modify(false);
((ctConfigToolView*)GetFirstView())->OnChangeFilename();
SetDocumentSaved(true);
SetFilename(filename);
wxGetApp().GetSettings().m_lastFilename = filename;
} else
{
SetFilename(strOldPath);
}
wxGetApp().GetMainFrame()->UpdateFrameTitle();
return saved;
}
// Open the document
bool ctConfigToolDoc::OnOpenDocument(const wxString& filename)
{
wxBusyCursor cursor;
bool opened = DoOpen(filename);
if (opened)
{
SetFilename(filename);
wxGetApp().GetSettings().m_lastFilename = filename;
((ctConfigToolView*)GetFirstView())->OnChangeFilename();
RefreshDependencies();
// ctConfigToolHint hint(NULL, ctFilenameChanged);
ctConfigToolHint hint(NULL, ctInitialUpdate);
UpdateAllViews (NULL, & hint);
}
SetDocumentSaved(true); // Necessary or it will pop up the Save As dialog
return opened;
}
/// Save the settings file
bool ctConfigToolDoc::DoSave(const wxString& filename)
{
wxFileOutputStream osFile(filename);
if (!osFile.Ok())
return false;
wxTextOutputStream stream(osFile);
stream << wxT("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
stream << wxT("<settings xmlns=\"http://www.wxwidgets.org/wxs\" version=\"2.5.0.1\">");
DoSave(m_topItem, osFile, 1);
stream << wxT("\n</settings>\n");
return true;
}
inline static void OutputIndentation(wxOutputStream& osFile, int indent)
{
wxTextOutputStream stream(osFile);
wxString str = wxT("\n");
for (int i = 0; i < indent; i++)
str << wxT(" ");
stream << str ;
}
/// Recursive helper function for file saving
bool ctConfigToolDoc::DoSave(ctConfigItem* item, wxOutputStream& osFile, int indent)
{
OutputIndentation(osFile, indent*2);
wxTextOutputStream stream(osFile);
wxString name(item->GetName());
wxString s;
wxString typeStr;
if (item->GetType() == ctTypeGroup)
typeStr = wxT("group");
else if (item->GetType() == ctTypeCheckGroup)
typeStr = wxT("check-group");
else if (item->GetType() == ctTypeRadioGroup)
typeStr = wxT("radio-group");
else if (item->GetType() == ctTypeString)
typeStr = wxT("string");
else if (item->GetType() == ctTypeBoolCheck)
typeStr = wxT("bool-check");
else if (item->GetType() == ctTypeBoolRadio)
typeStr = wxT("bool-radio");
else if (item->GetType() == ctTypeInteger)
typeStr = wxT("integer");
else
typeStr = wxT("unknown");
stream << wxT("<setting type=\"") << typeStr << wxT("\">");
indent ++;
OutputIndentation(osFile, indent*2);
if (item->IsActive())
stream << wxT("<active>1</active>");
else
stream << wxT("<active>0</active>");
OutputIndentation(osFile, indent*2);
if (item->IsEnabled())
stream << wxT("<enabled>1</enabled>");
else
stream << wxT("<enabled>0</enabled>");
// Output properties
wxObjectList::compatibility_iterator node = item->GetProperties().GetList().GetFirst();
while (node)
{
ctProperty* prop = (ctProperty*) node->GetData();
OutputIndentation(osFile, indent*2);
stream << wxT("<") << prop->GetName() ;
if (prop->IsCustom())
{
stream << wxT(" custom=\"true\"");
stream << wxT(" type=\"") << prop->GetVariant().GetType() << wxT("\"");
stream << wxT(" editor-type=\"") << prop->GetEditorType() << wxT("\"");
stream << wxT(" description=\"") << prop->GetDescription() << wxT("\"");
if (prop->GetChoices().GetCount() > 0)
{
wxString choices;
ctConfigItem::ArrayToString(prop->GetChoices(), choices);
stream << wxT(" choices=\"") << choices << wxT("\"");
}
}
stream << wxT(">");
stream << ctEscapeHTMLCharacters(prop->GetVariant().GetString()) ;
stream << wxT("</") << prop->GetName() << wxT(">");
node = node->GetNext();
}
// Output children
node = item->GetChildren().GetFirst();
while (node)
{
ctConfigItem* child = (ctConfigItem*) node->GetData();
DoSave(child, osFile, indent);
node = node->GetNext();
}
indent --;
OutputIndentation(osFile, indent*2);
stream << wxT("</setting>");
return true;
}
/// Open the settings file
bool ctConfigToolDoc::DoOpen(const wxString& filename)
{
wxSimpleHtmlParser parser;
if (parser.ParseFile(filename))
{
ctConfigToolHint hint(NULL, ctClear);
UpdateAllViews (NULL, & hint);
m_topItem = NULL;
if (parser.GetTopLevelTag()->GetChildren())
{
wxSimpleHtmlTag* settingsTag = parser.GetTopLevelTag()->GetChildren()->FindTag(wxT("settings"));
if (settingsTag && settingsTag->GetChildren())
{
wxSimpleHtmlTag* firstSettingTag = settingsTag->GetChildren();
if (firstSettingTag)
DoOpen(firstSettingTag, NULL);
return true;
}
return true;
}
}
return false;
}
static bool GetHtmlBoolValue(const wxString& value)
{
if (value.IsSameAs(wxT("true"),false) || value == wxT("1"))
return true;
else
return false;
}
static int GetHtmlIntegerValue(const wxString& value)
{
return wxAtoi(value);
}
static double GetHtmlDoubleValue(const wxString& value)
{
return wxAtof(value);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -