]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/utils/wxrcedit/editor.cpp
Typos fixed and further clarification of the Python situation on Panther
[wxWidgets.git] / contrib / utils / wxrcedit / editor.cpp
index 9e313c75973747b72c01a51bc804d2f845a488a1..7afb2b2377341a1c133351f7e967c9c6af38c092 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "wx/wx.h"
 #include "wx/xml/xml.h"
-#include "wx/xml/xmlres.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"), WX_XMLRES_CURRENT_VERSION_STRING);
+}
+
 
 class EditorTreeCtrl : public wxTreeCtrl
 {
@@ -81,11 +141,12 @@ enum
     ID_COPY,
 
     ID_NEWDIALOG,
+       ID_NEWFRAME,
     ID_NEWPANEL,
     ID_NEWMENU,
     ID_NEWMENUBAR,
     ID_NEWTOOLBAR,   
-    ID_NEWNODE = wxID_HIGHEST + 10000, // safely out of XMLID range :)
+    ID_NEWNODE = wxID_HIGHEST + 10000, // safely out of XRCID range :)
     ID_NEWSYBNODE = ID_NEWNODE + 20000
 };
 
@@ -131,35 +192,35 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
     
     wxConfigBase *cfg = wxConfigBase::Get();
     
-    SetSize(wxRect(wxPoint(cfg->Read("editor_x", -1), cfg->Read("editor_y", -1)),
-            wxSize(cfg->Read("editor_w", 400), cfg->Read("editor_h", 400))));
+    SetSize(wxRect(wxPoint(cfg->Read(_T("editor_x"), -1), cfg->Read(_T("editor_y"), -1)),
+            wxSize(cfg->Read(_T("editor_w"), 400), cfg->Read(_T("editor_h"), 400))));
 
     m_SelectedNode = NULL;
     m_Resource = NULL;
     m_FileName = wxEmptyString;
 
     wxMenu *menuFile = new wxMenu;
-    menuFile->Append(ID_NEW, "&New");
-    menuFile->Append(ID_OPEN, "&Open\tCtrl-O");
-    menuFile->Append(ID_SAVE, "&Save\tCtrl-S");
-    menuFile->Append(ID_SAVEAS, "Save &as...");
+    menuFile->Append(ID_NEW, _T("&New"));
+    menuFile->Append(ID_OPEN, _T("&Open\tCtrl-O"));
+    menuFile->Append(ID_SAVE, _T("&Save\tCtrl-S"));
+    menuFile->Append(ID_SAVEAS, _T("Save &as..."));
     menuFile->AppendSeparator();
-    menuFile->Append(ID_EXIT, "E&xit\tAlt-X");
+    menuFile->Append(ID_EXIT, _T("E&xit\tAlt-X"));
 
     wxMenu *menuEdit = new wxMenu;
-    menuEdit->Append(ID_CUT, "Cut\tCtrl-X");
-    menuEdit->Append(ID_COPY, "Copy\tCtrl-C");
-    menuEdit->Append(ID_PASTE_SYBLING, "Paste as sybling\tCtrl-V");
-    menuEdit->Append(ID_PASTE_CHILD, "Paste as child");
+    menuEdit->Append(ID_CUT, _T("Cut\tCtrl-X"));
+    menuEdit->Append(ID_COPY, _T("Copy\tCtrl-C"));
+    menuEdit->Append(ID_PASTE_SYBLING, _T("Paste as sybling\tCtrl-V"));
+    menuEdit->Append(ID_PASTE_CHILD, _T("Paste as child"));
     menuEdit->AppendSeparator();
-    menuEdit->Append(ID_DELETE_NODE,  "Delete");
+    menuEdit->Append(ID_DELETE_NODE,  _T("Delete"));
 
     menuEdit->Enable(ID_PASTE_SYBLING, FALSE);
     menuEdit->Enable(ID_PASTE_CHILD, FALSE);
     
     wxMenuBar *menuBar = new wxMenuBar();
-    menuBar->Append(menuFile, "&File");
-    menuBar->Append(menuEdit, "&Edit");
+    menuBar->Append(menuFile, _T("&File"));
+    menuBar->Append(menuEdit, _T("&Edit"));
     SetMenuBar(menuBar);
   
     // Create toolbar:
@@ -230,20 +291,26 @@ void EditorFrame::LoadFile(const wxString& filename)
 
     delete m_Resource;
     
-    m_FileName = "";
-    m_Resource = new wxXmlDocument;
+     // create new resource in order to handle version differences properly
+    PreviewFrame::Get()->ResetResource();
+    
+    m_FileName = wxEmptyString;
+    m_Resource = new wxXmlRcEditDocument;
     m_Modified = FALSE;
     
-    if (!m_Resource->Load(filename))
+    if (!m_Resource->Load(filename,  wxLocale::GetSystemEncodingName()))
     {
         delete m_Resource;
         m_Resource = NULL;
         NewFile();
-        wxLogError("Error parsing " + filename);
+        wxLogError(_T("Error parsing ") + filename);
     }
     else
     {
         m_FileName = filename;
+
+        // Upgrades old versions
+        m_Resource->Upgrade();
         RefreshTree();
     }
     RefreshTitle();
@@ -254,8 +321,9 @@ void EditorFrame::LoadFile(const wxString& filename)
 void EditorFrame::SaveFile(const wxString& filename)
 {
     m_FileName = filename;
-
-    if (!m_Resource->Save(filename, wxXML_IO_LIBXML))
+    
+    // save it:
+    if (!m_Resource->Save(filename))
         wxLogError(_("Error saving ") + filename);
     else
         m_Modified = FALSE;
@@ -271,10 +339,18 @@ void EditorFrame::NewFile()
 
     delete m_Resource;
     
-    m_FileName = "";
-    m_Resource = new wxXmlDocument;
+    m_FileName = wxEmptyString;
+    m_Resource = new wxXmlRcEditDocument;
     m_Resource->SetRoot(new wxXmlNode(wxXML_ELEMENT_NODE, _("resource")));
+       
+       m_Resource->SetFileEncoding(_T("utf-8"));
+#if !wxUSE_UNICODE
+    m_Resource->SetEncoding(wxLocale::GetSystemEncodingName());
+#endif
     
+       m_Resource->GetRoot()->AddProperty(_T("version"),
+                                       WX_XMLRES_CURRENT_VERSION_STRING);
+
     m_Modified = FALSE;
     RefreshTree();
     RefreshTitle();
@@ -287,7 +363,7 @@ void EditorFrame::RefreshTitle()
     wxString s;
     if (m_Modified) s << _T("* ");
     s << _("wxrcedit");
-    if (!m_FileName)
+    if (m_FileName != wxEmptyString)
         s << _T(" - ") << wxFileNameFromPath(m_FileName);
     SetTitle(s);
 }
@@ -299,7 +375,8 @@ void EditorFrame::RefreshTree()
     wxXmlNode *sel = m_SelectedNode;
     
     m_TreeCtrl->DeleteAllItems(); 
-    wxTreeItemId root = m_TreeCtrl->AddRoot("Resource: " + wxFileNameFromPath(m_FileName), 5, 5);
+
+    wxTreeItemId root = m_TreeCtrl->AddRoot(_T("Resource: ") + wxFileNameFromPath(m_FileName), 5, 5);
 
     wxXmlNode *n = m_Resource->GetRoot()->GetChildren();  
     while (n)
@@ -315,6 +392,19 @@ void EditorFrame::RefreshTree()
 
 
 
+
+static void RecursivelyExpand(wxTreeCtrl *t, wxTreeItemId item)
+{
+    t->Expand(item);
+    void* 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)
@@ -326,7 +416,7 @@ bool EditorFrame::SelectNode(wxXmlNode *node, wxTreeItemId *root)
     wxTreeItemId item;
     XmlTreeData *dt;
     wxXmlNode *nd;
-    long cookie;
+    void* cookie;
     
     item = m_TreeCtrl->GetFirstChild(*root, cookie);
     while (item.IsOk())
@@ -335,14 +425,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;
 }
 
@@ -391,18 +483,6 @@ void EditorFrame::NotifyChanged(int change_type)
 
 
 
-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);
-    }
-}
-
 void EditorFrame::OnTreeSel(wxTreeEvent& event)
 {
     XmlTreeData *dt = (XmlTreeData*)(m_TreeCtrl->GetItemData(event.GetItem()));
@@ -412,19 +492,19 @@ void EditorFrame::OnTreeSel(wxTreeEvent& event)
     if (node)
         PropertiesFrame::Get()->ShowProps(node);
 
-    if (m_TreeCtrl->GetParent(event.GetItem()) == m_TreeCtrl->GetRootItem())
+    if (m_TreeCtrl->GetItemParent(event.GetItem()) == m_TreeCtrl->GetRootItem())
     {
         wxTreeItemId it = event.GetOldItem();
 
         if (it.IsOk() && m_TreeCtrl->GetRootItem() != it)
         {
-            while (m_TreeCtrl->GetParent(it) != m_TreeCtrl->GetRootItem())
-                it = m_TreeCtrl->GetParent(it);
+            while (m_TreeCtrl->GetItemParent(it) != m_TreeCtrl->GetRootItem())
+                it = m_TreeCtrl->GetItemParent(it);
             m_TreeCtrl->Collapse(it);
         }
         RecursivelyExpand(m_TreeCtrl, event.GetItem());
 
-        PreviewFrame::Get()->Preview(node);
+        PreviewFrame::Get()->Preview(node,m_Resource);
     }
 }
 
@@ -438,7 +518,7 @@ 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);
             break;
             }
 
@@ -461,7 +541,7 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
             }
 
         case ID_SAVE :
-            if (m_FileName != "") { SaveFile(m_FileName); break;}
+            if (m_FileName != wxEmptyString) { SaveFile(m_FileName); break;}
             // else go to SAVEAS
 
         case ID_SAVEAS :
@@ -487,7 +567,7 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 void EditorFrame::DeleteSelectedNode()
 {
     XmlTreeData *dt = (XmlTreeData*)
-            (m_TreeCtrl->GetItemData(m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+            (m_TreeCtrl->GetItemData(m_TreeCtrl->GetItemParent(m_TreeCtrl->GetSelection())));
     wxXmlNode *n = (dt) ? dt->Node : NULL;
 
     m_SelectedNode->GetParent()->RemoveChild(m_SelectedNode);
@@ -503,7 +583,7 @@ void EditorFrame::OnNewNode(wxCommandEvent& event)
     {
         XmlTreeData *pardt = 
             (XmlTreeData*)(m_TreeCtrl->GetItemData(
-                m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+                m_TreeCtrl->GetItemParent(m_TreeCtrl->GetSelection())));
 
         if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
         {
@@ -543,6 +623,7 @@ void EditorFrame::OnNewNode(wxCommandEvent& event)
         switch (event.GetId())
         {
             case ID_NEWDIALOG : name = _T("wxDialog"); break;
+                       case ID_NEWFRAME : name = _T("wxFrame"); break;
             case ID_NEWPANEL : name = _T("wxPanel"); break;
             case ID_NEWMENU : name = _T("wxMenu"); break;
             case ID_NEWMENUBAR : name = _T("wxMenuBar"); break;
@@ -567,6 +648,7 @@ void EditorFrame::OnRightClickTree(wxPoint pos)
     if (m_SelectedNode == NULL || m_SelectedNode == m_Resource->GetRoot())
     {
         popup->Append(ID_NEWDIALOG, _("New wxDialog"));
+               popup->Append(ID_NEWFRAME, _("New wxFrame"));
         popup->Append(ID_NEWPANEL, _("New wxPanel"));
         popup->Append(ID_NEWMENU, _("New wxMenu"));
         popup->Append(ID_NEWMENUBAR, _("New wxMenuBar"));
@@ -607,7 +689,7 @@ void EditorFrame::OnRightClickTree(wxPoint pos)
 
         XmlTreeData *pardt = 
             (XmlTreeData*)(m_TreeCtrl->GetItemData(
-                m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+                m_TreeCtrl->GetItemParent(m_TreeCtrl->GetSelection())));
         if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
         {
             wxXmlNode *nd = pardt->Node;
@@ -672,7 +754,7 @@ void EditorFrame::OnClipboardAction(wxCommandEvent& event)
             {
             XmlTreeData *pardt = 
                 (XmlTreeData*)(m_TreeCtrl->GetItemData(
-                    m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+                    m_TreeCtrl->GetItemParent(m_TreeCtrl->GetSelection())));
 
             if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
             {