]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/utils/wxrcedit/editor.cpp
added wxGenericDirCtrl handler to XRC
[wxWidgets.git] / contrib / utils / wxrcedit / editor.cpp
index c340043ff1a7420950ee2e8f009d6fcd0f695df7..cb9a3e8e2696821a7d80b3768ab04d0a7ee12eb5 100644 (file)
@@ -19,8 +19,8 @@
 #endif
 
 #include "wx/wx.h"
-#include "wx/xml/xml.h"
-#include "wx/xml/xmlres.h"
+#include "wx/xrc/xml.h"
+#include "wx/xrc/xmlres.h"
 #include "wx/splitter.h"
 #include "wx/config.h"
 #include "wx/dir.h"
 #include "propframe.h"
 
 
+void wxXmlRcEditDocument::UpgradeNodeValue(wxXmlNode *node)
+{
+    wxXmlNode *n = node;
+    if (n == NULL) return;
+    n = n->GetChildren();
+
+    while (n)
+    {
+        if (n->GetType() == wxXML_TEXT_NODE ||
+            n->GetType() == wxXML_CDATA_SECTION_NODE)
+        {
+            wxString str1 = n->GetContent();
+            const wxChar *dt;
+
+            for (dt = str1.c_str(); *dt; dt++)
+            {
+                // Remap amp_char to &, map double amp_char to amp_char (for things
+                // like "&File..." -- this is illegal in XML, so we use "_File..."):
+                if (*dt == '$')
+                {
+                    if ( *(++dt) != '$' )
+                        str1[size_t(dt-str1.c_str()-1)] = '_';
+                }
+            }
+            n->SetContent(str1);
+        }
+        n = n->GetNext();
+    }
+}
+
+void wxXmlRcEditDocument::UpgradeNode(wxXmlNode *node)
+{
+    if (node)
+    {
+        UpgradeNodeValue(node);
+        UpgradeNode(node->GetNext());
+        UpgradeNode(node->GetChildren());
+    }
+}
+
+void wxXmlRcEditDocument::Upgrade()
+{
+    int v1,v2,v3,v4;
+    long version;
+    wxXmlNode *node = GetRoot();
+    wxString verstr = wxT("0.0.0.0");
+    node->GetPropVal(wxT("version"),verstr);
+    if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
+        &v1, &v2, &v3, &v4) == 4)
+        version = v1*256*256*256+v2*256*256+v3*256+v4;
+    else
+        version = 0;
+    if (!version)
+    {
+        UpgradeNode(node);
+    }
+    node->DeleteProperty(wxT("version"));
+    node->AddProperty(wxT("version"), wxT(WX_XMLRES_CURRENT_VERSION_STRING));
+}
+
 
 class EditorTreeCtrl : public wxTreeCtrl
 {
@@ -98,6 +158,7 @@ BEGIN_EVENT_TABLE(EditorFrame, wxFrame)
     EVT_TOOL_RANGE(ID_PREVIEW, ID_EXIT, EditorFrame::OnToolbar)
     EVT_MENU_RANGE(ID_NEWDIALOG, ID_NEWSYBNODE + 1000, EditorFrame::OnNewNode)
     EVT_MENU_RANGE(ID_CUT, ID_COPY, EditorFrame::OnClipboardAction)
+    EVT_CLOSE(EditorFrame::OnCloseWindow)
 END_EVENT_TABLE()
 
 
@@ -126,6 +187,7 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
     ms_Instance = this;
 
     m_Clipboard = NULL;
+    m_Modified = FALSE;
     
     wxConfigBase *cfg = wxConfigBase::Get();
     
@@ -224,10 +286,16 @@ EditorFrame::~EditorFrame()
 
 void EditorFrame::LoadFile(const wxString& filename)
 {
+    if (!AskToSave()) return;
+
     delete m_Resource;
     
+     // create new resource in order to handle version differences properly
+    PreviewFrame::Get()->ResetResource();
+    
     m_FileName = "";
-    m_Resource = new wxXmlDocument;
+    m_Resource = new wxXmlRcEditDocument;
+    m_Modified = FALSE;
     
     if (!m_Resource->Load(filename))
     {
@@ -239,9 +307,12 @@ void EditorFrame::LoadFile(const wxString& filename)
     else
     {
         m_FileName = filename;
+
+        // Upgrades old versions
+        m_Resource->Upgrade();
         RefreshTree();
-        SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName));
     }
+    RefreshTitle();
 }
 
 
@@ -249,24 +320,43 @@ void EditorFrame::LoadFile(const wxString& filename)
 void EditorFrame::SaveFile(const wxString& filename)
 {
     m_FileName = filename;
-    SetTitle("wxrcedit - " + wxFileNameFromPath(m_FileName));
+    
+    // save it:
+    if (!m_Resource->Save(filename))
+        wxLogError(_("Error saving ") + filename);
+    else
+        m_Modified = FALSE;
 
-    if (!m_Resource->Save(filename, wxXML_IO_LIBXML))
-        wxLogError("Error saving " + filename);
+    RefreshTitle();
 }
 
 
 
 void EditorFrame::NewFile()
 {  
+    if (!AskToSave()) return;
+
     delete m_Resource;
     
     m_FileName = "";
-    m_Resource = new wxXmlDocument;
-    m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, "resource"));    
+    m_Resource = new wxXmlRcEditDocument;
+    m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, _("resource")));
     
+    m_Modified = FALSE;
     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);
 }
 
 
@@ -292,6 +382,19 @@ void EditorFrame::RefreshTree()
 
 
 
+
+static void RecursivelyExpand(wxTreeCtrl *t, wxTreeItemId item)
+{
+    t->Expand(item);
+    long cookie;
+    wxTreeItemId id = t->GetFirstChild(item, cookie);
+    while (id.IsOk())
+    {
+        RecursivelyExpand(t, id);
+        id = t->GetNextChild(item, cookie);
+    }
+}
+
 bool EditorFrame::SelectNode(wxXmlNode *node, wxTreeItemId *root)
 {
     if (root == NULL)
@@ -312,14 +415,16 @@ bool EditorFrame::SelectNode(wxXmlNode *node, wxTreeItemId *root)
         nd = (dt) ? dt->Node : NULL;
         if (nd == node) 
         {
+            RecursivelyExpand(m_TreeCtrl, *root);
             m_TreeCtrl->SelectItem(item);
             m_TreeCtrl->EnsureVisible(item);
-            return TRUE;
+            return TRUE; 
         }
         if (m_TreeCtrl->ItemHasChildren(item) && SelectNode(node, &item)) 
-            return TRUE;
+            return TRUE; 
         item = m_TreeCtrl->GetNextChild(*root, cookie);
     }
+
     return FALSE;
 }
 
@@ -356,22 +461,18 @@ void EditorFrame::NotifyChanged(int change_type)
         int icon = NodeHandler::Find(m_SelectedNode)->GetTreeIcon(m_SelectedNode);
         m_TreeCtrl->SetItemImage(sel, icon);
     }
-}
-
-
-
-static void RecursivelyExpand(wxTreeCtrl *t, wxTreeItemId item)
-{
-    t->Expand(item);
-    long cookie;
-    wxTreeItemId id = t->GetFirstChild(item, cookie);
-    while (id.IsOk())
+    
+    if (!m_Modified)
     {
-        RecursivelyExpand(t, id);
-        id = t->GetNextChild(item, cookie);
+        m_Modified = TRUE;
+        RefreshTitle();
     }
+    
+    PreviewFrame::Get()->MakeDirty();
 }
 
+
+
 void EditorFrame::OnTreeSel(wxTreeEvent& event)
 {
     XmlTreeData *dt = (XmlTreeData*)(m_TreeCtrl->GetItemData(event.GetItem()));
@@ -393,7 +494,8 @@ void EditorFrame::OnTreeSel(wxTreeEvent& event)
         }
         RecursivelyExpand(m_TreeCtrl, event.GetItem());
 
-        PreviewFrame::Get()->Preview(node);
+        PreviewFrame::Get()->Preview(node,m_Resource->GetRoot()->GetPropVal(
+                                      wxT("version"), wxT("0.0.0.0")));
     }
 }
 
@@ -407,7 +509,8 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
             {
             XmlTreeData* dt = (XmlTreeData*)m_TreeCtrl->GetItemData(m_TreeCtrl->GetSelection());;
             if (dt != NULL && dt->Node != NULL)
-                PreviewFrame::Get()->Preview(dt->Node);
+                PreviewFrame::Get()->Preview(dt->Node,m_Resource->GetRoot()->GetPropVal(
+                                      wxT("version"), wxT("0.0.0.0")));
             break;
             }
 
@@ -421,7 +524,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
         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);
+            wxSetWorkingDirectory(cwd);
             if (!name.IsEmpty())
                 LoadFile(name);
             break;
@@ -433,7 +538,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
         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);
+            wxSetWorkingDirectory(cwd);
             if (!name.IsEmpty())
                 SaveFile((m_FileName = name));
             break;
@@ -664,3 +771,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();
+}