wxrcedit bugfixes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8807 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík 2000-11-24 17:19:48 +00:00
parent fda09b3f66
commit 26607f41ae
9 changed files with 208 additions and 10 deletions

View File

@ -98,6 +98,7 @@ BEGIN_EVENT_TABLE(EditorFrame, wxFrame)
EVT_TOOL_RANGE(ID_PREVIEW, ID_EXIT, EditorFrame::OnToolbar) EVT_TOOL_RANGE(ID_PREVIEW, ID_EXIT, EditorFrame::OnToolbar)
EVT_MENU_RANGE(ID_NEWDIALOG, ID_NEWSYBNODE + 1000, EditorFrame::OnNewNode) EVT_MENU_RANGE(ID_NEWDIALOG, ID_NEWSYBNODE + 1000, EditorFrame::OnNewNode)
EVT_MENU_RANGE(ID_CUT, ID_COPY, EditorFrame::OnClipboardAction) EVT_MENU_RANGE(ID_CUT, ID_COPY, EditorFrame::OnClipboardAction)
EVT_CLOSE(EditorFrame::OnCloseWindow)
END_EVENT_TABLE() END_EVENT_TABLE()
@ -126,6 +127,7 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
ms_Instance = this; ms_Instance = this;
m_Clipboard = NULL; m_Clipboard = NULL;
m_Modified = FALSE;
wxConfigBase *cfg = wxConfigBase::Get(); wxConfigBase *cfg = wxConfigBase::Get();
@ -224,10 +226,13 @@ EditorFrame::~EditorFrame()
void EditorFrame::LoadFile(const wxString& filename) void EditorFrame::LoadFile(const wxString& filename)
{ {
if (!AskToSave()) return;
delete m_Resource; delete m_Resource;
m_FileName = ""; m_FileName = "";
m_Resource = new wxXmlDocument; m_Resource = new wxXmlDocument;
m_Modified = FALSE;
if (!m_Resource->Load(filename)) if (!m_Resource->Load(filename))
{ {
@ -240,8 +245,8 @@ void EditorFrame::LoadFile(const wxString& filename)
{ {
m_FileName = filename; m_FileName = filename;
RefreshTree(); RefreshTree();
SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName));
} }
RefreshTitle();
} }
@ -249,24 +254,42 @@ void EditorFrame::LoadFile(const wxString& filename)
void EditorFrame::SaveFile(const wxString& filename) void EditorFrame::SaveFile(const wxString& filename)
{ {
m_FileName = filename; m_FileName = filename;
SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName));
if (!m_Resource->Save(filename, wxXML_IO_LIBXML)) if (!m_Resource->Save(filename, wxXML_IO_LIBXML))
wxLogError("Error saving " + filename); wxLogError(_("Error saving ") + filename);
else
m_Modified = FALSE;
RefreshTitle();
} }
void EditorFrame::NewFile() void EditorFrame::NewFile()
{ {
if (!AskToSave()) return;
delete m_Resource; delete m_Resource;
m_FileName = ""; m_FileName = "";
m_Resource = new wxXmlDocument; m_Resource = new wxXmlDocument;
m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, "resource")); m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, _("resource")));
m_Modified = FALSE;
RefreshTree(); RefreshTree();
SetTitle("unnamed"); RefreshTitle();
}
void EditorFrame::RefreshTitle()
{
wxString s;
if (m_Modified) s << _T("* ");
s << _("wxrcedit");
if (!m_FileName)
s << _T(" - ") << wxFileNameFromPath(m_FileName);
SetTitle(s);
} }
@ -356,6 +379,14 @@ void EditorFrame::NotifyChanged(int change_type)
int icon = NodeHandler::Find(m_SelectedNode)->GetTreeIcon(m_SelectedNode); int icon = NodeHandler::Find(m_SelectedNode)->GetTreeIcon(m_SelectedNode);
m_TreeCtrl->SetItemImage(sel, icon); m_TreeCtrl->SetItemImage(sel, icon);
} }
if (!m_Modified)
{
m_Modified = TRUE;
RefreshTitle();
}
PreviewFrame::Get()->MakeDirty();
} }
@ -421,7 +452,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
case ID_OPEN : case ID_OPEN :
{ {
wxString cwd = wxGetCwd(); // workaround for 2.2
wxString name = wxFileSelector(_("Open XML resource"), _T(""), _T(""), _T(""), _("XML resources (*.xrc)|*.xrc"), wxOPEN | wxFILE_MUST_EXIST); wxString name = wxFileSelector(_("Open XML resource"), _T(""), _T(""), _T(""), _("XML resources (*.xrc)|*.xrc"), wxOPEN | wxFILE_MUST_EXIST);
wxSetWorkingDirectory(cwd);
if (!name.IsEmpty()) if (!name.IsEmpty())
LoadFile(name); LoadFile(name);
break; break;
@ -433,7 +466,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
case ID_SAVEAS : case ID_SAVEAS :
{ {
wxString cwd = wxGetCwd(); // workaround for 2.2
wxString name = wxFileSelector(_("Save as"), _T(""), m_FileName, _T(""), _("XML resources (*.xrc)|*.xrc"), wxSAVE | wxOVERWRITE_PROMPT); wxString name = wxFileSelector(_("Save as"), _T(""), m_FileName, _T(""), _("XML resources (*.xrc)|*.xrc"), wxSAVE | wxOVERWRITE_PROMPT);
wxSetWorkingDirectory(cwd);
if (!name.IsEmpty()) if (!name.IsEmpty())
SaveFile((m_FileName = name)); SaveFile((m_FileName = name));
break; break;
@ -664,3 +699,27 @@ void EditorFrame::OnClipboardAction(wxCommandEvent& event)
} }
} }
bool EditorFrame::AskToSave()
// asks the user to save current document (if modified)
// returns FALSE if user cancelled the action, TRUE of he choosed
// 'yes' or 'no'
{
if (!m_Modified) return TRUE;
int res = wxMessageBox(_("File modified. Do you want to save changes?"), _("Save changes"),
wxYES_NO | wxCANCEL | wxCENTRE | wxICON_QUESTION);
if (res == wxYES)
SaveFile(m_FileName);
return (res != wxCANCEL);
}
void EditorFrame::OnCloseWindow(wxCloseEvent&)
{
if (!AskToSave()) return;
Destroy();
}

View File

@ -61,6 +61,7 @@ class EditorFrame : public wxFrame
wxString GetFileName() { return m_FileName; } wxString GetFileName() { return m_FileName; }
void RefreshTree(); void RefreshTree();
void RefreshTitle();
bool SelectNode(wxXmlNode *node, wxTreeItemId *root = NULL); bool SelectNode(wxXmlNode *node, wxTreeItemId *root = NULL);
wxTreeItemId CreateTreeNode(wxTreeCtrl *treectrl, wxTreeItemId parent, wxXmlNode *node); wxTreeItemId CreateTreeNode(wxTreeCtrl *treectrl, wxTreeItemId parent, wxXmlNode *node);
@ -81,6 +82,11 @@ class EditorFrame : public wxFrame
wxString m_FileName; wxString m_FileName;
wxXmlDocument *m_Resource; wxXmlDocument *m_Resource;
bool m_Modified;
bool AskToSave();
void DeleteSelectedNode();
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
void OnTreeSel(wxTreeEvent& event); void OnTreeSel(wxTreeEvent& event);
void OnToolbar(wxCommandEvent& event); void OnToolbar(wxCommandEvent& event);
@ -88,8 +94,7 @@ class EditorFrame : public wxFrame
void OnNewNode(wxCommandEvent& event); void OnNewNode(wxCommandEvent& event);
void OnRightClickTree(wxPoint pos); void OnRightClickTree(wxPoint pos);
void OnClipboardAction(wxCommandEvent& event); void OnClipboardAction(wxCommandEvent& event);
void OnCloseWindow(wxCloseEvent&);
void DeleteSelectedNode();
}; };

View File

@ -96,7 +96,11 @@ void PropEditCtrlChoice::WriteValue()
void PropEditCtrlChoice::OnChoice(wxCommandEvent& event) void PropEditCtrlChoice::OnChoice(wxCommandEvent& event)
{ {
if (CanSave()) WriteValue(); if (CanSave())
{
WriteValue();
EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
}
} }
@ -193,3 +197,66 @@ void PropEditCtrlFlags::OnDetails()
} }
wxString PropEditCtrlFile::GetFileTypes()
{
return m_PropInfo->MoreInfo;
}
void PropEditCtrlFile::OnDetails()
{
wxString txt = m_TextCtrl->GetValue();
txt = wxPathOnly(EditorFrame::Get()->GetFileName()) + _T("/") + txt;
wxString name = wxFileSelector(_("Choose file"),
wxPathOnly(txt),
wxFileNameFromPath(txt),
_T(""),
GetFileTypes(),
wxOPEN | wxFILE_MUST_EXIST);
if (!name) return;
// compute relative path:
wxArrayString axrc, afile;
wxStringTokenizer tkn;
tkn.SetString(name, _T("/\\"));
while (tkn.HasMoreTokens()) afile.Add(tkn.GetNextToken());
tkn.SetString(EditorFrame::Get()->GetFileName(), _T("/\\"));
while (tkn.HasMoreTokens()) axrc.Add(tkn.GetNextToken());
if (afile.GetCount() == 0 || axrc.GetCount() == 0)
txt = name;
else
{
while (axrc[0] == afile[0])
{
afile.Remove(0u);
axrc.Remove(0u);
}
size_t i;
txt.Empty();
for (i = 0; i < axrc.GetCount()-1/*w/o filename*/; i++) txt << _T("../");
for (i = 0; i < afile.GetCount(); i++) txt << afile[i] << _T("/");
txt.RemoveLast();
}
m_TextCtrl->SetValue(txt);
WriteValue();
}
wxString PropEditCtrlImageFile::GetFileTypes()
{
return _("GIF files (*.gif)|*.gif|"
"JPEG files (*.jpg)|*.jpg|"
"PNG files (*.png)|*.png|"
"BMP files (*.bmp)|*.bmp|"
"All files (*)|*");
}

View File

@ -76,6 +76,26 @@ class PropEditCtrlFlags : public PropEditCtrlTxt
class PropEditCtrlFile : public PropEditCtrlTxt
{
public:
PropEditCtrlFile(PropertiesFrame *propFrame)
: PropEditCtrlTxt(propFrame) {}
virtual bool HasDetails() { return TRUE; }
virtual void OnDetails();
virtual wxString GetFileTypes();
};
class PropEditCtrlImageFile : public PropEditCtrlFile
{
public:
PropEditCtrlImageFile(PropertiesFrame *propFrame)
: PropEditCtrlFile(propFrame) {}
virtual wxString GetFileTypes();
};
#endif #endif

View File

@ -40,7 +40,11 @@ wxWindow *PropEditCtrlTxt::CreateEditCtrl()
void PropEditCtrlTxt::OnText(wxCommandEvent& event) void PropEditCtrlTxt::OnText(wxCommandEvent& event)
{ {
if (CanSave()) WriteValue(); if (CanSave())
{
WriteValue();
EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
}
} }
@ -121,7 +125,11 @@ wxString PropEditCtrlBool::GetValueAsText(wxTreeItemId ti)
void PropEditCtrlBool::OnChoice(wxCommandEvent& event) void PropEditCtrlBool::OnChoice(wxCommandEvent& event)
{ {
if (CanSave()) WriteValue(); if (CanSave())
{
WriteValue();
EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
}
} }
@ -410,6 +418,7 @@ void PropEditCtrlXMLID::OnDetails()
if (!s) return; if (!s) return;
m_TextCtrl->SetValue(s); m_TextCtrl->SetValue(s);
WriteValue(); WriteValue();
EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
} }

View File

@ -63,6 +63,7 @@ PreviewFrame *PreviewFrame::Get()
PreviewFrame::PreviewFrame() PreviewFrame::PreviewFrame()
: wxFrame(NULL, -1, _("Preview")) : wxFrame(NULL, -1, _("Preview"))
{ {
m_Dirty = FALSE;
ms_Instance = this; ms_Instance = this;
m_Node = NULL; m_Node = NULL;
@ -109,6 +110,18 @@ PreviewFrame::~PreviewFrame()
void PreviewFrame::MakeDirty()
{
if (m_Node == NULL) return;
if (m_Dirty) return;
m_Dirty = TRUE;
m_LogCtrl->Clear();
m_LogCtrl->SetValue(_("Resource modified.\n"
"Move mouse cursor over the preview window to refresh it."));
}
void PreviewFrame::Preview(wxXmlNode *node) void PreviewFrame::Preview(wxXmlNode *node)
{ {
while (node->GetParent()->GetParent() != NULL) node = node->GetParent(); while (node->GetParent()->GetParent() != NULL) node = node->GetParent();
@ -152,6 +165,8 @@ void PreviewFrame::Preview(wxXmlNode *node)
wxSetWorkingDirectory(oldcwd); wxSetWorkingDirectory(oldcwd);
wxLog::SetActiveTarget(oldlog); wxLog::SetActiveTarget(oldlog);
m_Dirty = FALSE;
} }
@ -193,3 +208,14 @@ void PreviewFrame::PreviewPanel()
0, 0, TRUE); 0, 0, TRUE);
} }
} }
BEGIN_EVENT_TABLE(PreviewFrame, wxFrame)
EVT_ENTER_WINDOW(PreviewFrame::OnMouseEnter)
END_EVENT_TABLE()
void PreviewFrame::OnMouseEnter(wxMouseEvent& event)
{
if (m_Dirty) Preview(m_Node);
}

View File

@ -32,6 +32,9 @@ class PreviewFrame : public wxFrame
~PreviewFrame(); ~PreviewFrame();
void Preview(wxXmlNode *node); void Preview(wxXmlNode *node);
void MakeDirty();
// current node updated, needs preview refresh
// (will be done once mouse enters preview win)
static PreviewFrame *Get(); static PreviewFrame *Get();
@ -49,6 +52,11 @@ class PreviewFrame : public wxFrame
wxXmlResource *m_RC; wxXmlResource *m_RC;
wxString m_TmpFile; wxString m_TmpFile;
bool m_Dirty;
DECLARE_EVENT_TABLE()
void OnMouseEnter(wxMouseEvent& event);
}; };

View File

@ -22,6 +22,7 @@
#include "propframe.h" #include "propframe.h"
#include "propedit.h" #include "propedit.h"
#include "xmlhelpr.h" #include "xmlhelpr.h"
#include "editor.h"
enum enum
{ {
@ -44,6 +45,7 @@ void PropEditCtrl::OnButtonDetails(wxCommandEvent& event)
void PropEditCtrl::OnButtonClear(wxCommandEvent& event) void PropEditCtrl::OnButtonClear(wxCommandEvent& event)
{ {
Clear(); Clear();
EditorFrame::Get()->NotifyChanged(CHANGED_PROPS);
} }

View File

@ -228,6 +228,8 @@ PropertiesFrame::PropertiesFrame()
m_EditCtrls.Put(_T("xmlid"), new PropEditCtrlXMLID(this)); m_EditCtrls.Put(_T("xmlid"), new PropEditCtrlXMLID(this));
m_EditCtrls.Put(_T("font"), new PropEditCtrlFont(this)); m_EditCtrls.Put(_T("font"), new PropEditCtrlFont(this));
m_EditCtrls.Put(_T("choice"), new PropEditCtrlChoice(this)); m_EditCtrls.Put(_T("choice"), new PropEditCtrlChoice(this));
m_EditCtrls.Put(_T("file"), new PropEditCtrlFile(this));
m_EditCtrls.Put(_T("imagefile"), new PropEditCtrlImageFile(this));
m_EditCtrls.Put(_T(""), new PropEditCtrlNull(this)); m_EditCtrls.Put(_T(""), new PropEditCtrlNull(this));
ClearProps(); ClearProps();