]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/utils/wxrcedit/editor.cpp
added test for wxProgressDialog::Resume
[wxWidgets.git] / contrib / utils / wxrcedit / editor.cpp
index 15aa1fa98f331aef90e404f3190c1c74789ae183..affbc9e01bcd7b614db6c55851fa3d9ca8f88e45 100644 (file)
 #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 "wx/listctrl.h"
+#include "wx/imaglist.h"
 
 #include "treedt.h"
 #include "editor.h"
 #include "nodehnd.h"
 #include "xmlhelpr.h"
+#include "preview.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
 {
@@ -70,23 +134,21 @@ enum
     ID_DELETE_NODE,
     ID_EXIT,
     ID_TREE,    
-    ID_XMLIDEDIT,
-    ID_XMLIDPICK,
-    ID_EDITCODE,
-    ID_PROPSLIST,
-    ID_CLEARPROP,
+    
+    ID_CUT,
+    ID_PASTE_SYBLING,
+    ID_PASTE_CHILD,
+    ID_COPY,
 
     ID_NEWDIALOG,
     ID_NEWPANEL,
     ID_NEWMENU,
     ID_NEWMENUBAR,
     ID_NEWTOOLBAR,   
-    ID_NEWNODE = wxID_HIGHEST + 1000,
-    ID_NEWSYBNODE = ID_NEWNODE + 2000
+    ID_NEWNODE = wxID_HIGHEST + 10000, // safely out of XRCID range :)
+    ID_NEWSYBNODE = ID_NEWNODE + 20000
 };
 
-#ifdef __UNIX__
-#endif
 
 
 
@@ -95,16 +157,13 @@ BEGIN_EVENT_TABLE(EditorFrame, wxFrame)
     EVT_TREE_SEL_CHANGED(ID_TREE, EditorFrame::OnTreeSel)
     EVT_TOOL_RANGE(ID_PREVIEW, ID_EXIT, EditorFrame::OnToolbar)
     EVT_MENU_RANGE(ID_NEWDIALOG, ID_NEWSYBNODE + 1000, EditorFrame::OnNewNode)
-    EVT_TEXT(ID_XMLIDEDIT, EditorFrame::OnXMLIDEdit)
-    EVT_BUTTON(ID_XMLIDPICK, EditorFrame::OnXMLIDPick)
-    EVT_BUTTON(ID_EDITCODE, EditorFrame::OnEditCode)
-    EVT_BUTTON(ID_CLEARPROP, EditorFrame::OnClearProp)
-    EVT_LIST_ITEM_SELECTED(ID_PROPSLIST, EditorFrame::OnPropSel)
+    EVT_MENU_RANGE(ID_CUT, ID_COPY, EditorFrame::OnClipboardAction)
+    EVT_CLOSE(EditorFrame::OnCloseWindow)
 END_EVENT_TABLE()
 
 
 
-#ifdef __UNIX__
+#if defined(__UNIX__)
 #include "bitmaps/preview.xpm"
 #include "bitmaps/close.xpm"
 #include "bitmaps/save.xpm"
@@ -114,9 +173,8 @@ END_EVENT_TABLE()
 #include "bitmaps/vsizer.xpm"
 #include "bitmaps/hsizer.xpm"
 #include "bitmaps/panel.xpm"
-
-#include "bitmaps/unused.xpm"
-#include "bitmaps/used.xpm"
+#include "bitmaps/gsizer.xpm"
+#include "bitmaps/resicon.xpm"
 #endif
 
 
@@ -127,6 +185,9 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
     : wxFrame(parent, -1, filename + _("- wxWindows resources editor"))
 {
     ms_Instance = this;
+
+    m_Clipboard = NULL;
+    m_Modified = FALSE;
     
     wxConfigBase *cfg = wxConfigBase::Get();
     
@@ -136,8 +197,6 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
     m_SelectedNode = NULL;
     m_Resource = NULL;
     m_FileName = wxEmptyString;
-    m_Preview = NULL;
-    m_SelectedProp = -1;
 
     wxMenu *menuFile = new wxMenu;
     menuFile->Append(ID_NEW, "&New");
@@ -146,25 +205,27 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
     menuFile->Append(ID_SAVEAS, "Save &as...");
     menuFile->AppendSeparator();
     menuFile->Append(ID_EXIT, "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->AppendSeparator();
+    menuEdit->Append(ID_DELETE_NODE,  "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");
     SetMenuBar(menuBar);
-
-    // handlers:
-    m_Handlers.DeleteContents(TRUE);
-    RegisterHandlers(".");
-    RegisterHandlers("./df");
-    m_Handlers.Append(new NodeHandlerUnknown(this));
-    
-    #ifdef __UNIX__
-    RegisterHandlers(wxGetHomeDir() + "/.wxrcedit");
-    //FIXME - add $(prefix)/share/wx/wxrcedit
-    #endif
-    
+  
     // Create toolbar:
     wxToolBar *toolBar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL | wxTB_FLAT);
     toolBar->SetMargins(2, 2);
+    toolBar->SetToolBitmapSize(wxSize(24, 24));
     toolBar -> AddTool(ID_EXIT, wxBITMAP(close), wxNullBitmap,
                        FALSE, -1, -1, (wxObject *) NULL,
                        _("Quit the editor"));
@@ -178,78 +239,20 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
                        FALSE, -1, -1, (wxObject *) NULL,
                        _("Preview"));   
     toolBar -> Realize();
-    
-    // Create layout:    
-    wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
-    wxPanel *p = new wxPanel(this);
-    sizer->Add(p, 1, wxEXPAND);
-    wxSizer *sizer2 = new wxBoxSizer(wxVERTICAL);
-
-    m_Splitter = new wxSplitterWindow(p);
-    sizer2->Add(m_Splitter, 1, wxEXPAND);
-    
 
-    // Create tree control:
-    m_TreeCtrl = new EditorTreeCtrl(m_Splitter, ID_TREE, this);
-    m_ImgList = new wxImageList(16, 16);
-    m_ImgList->Add(wxICON(control));
-    m_ImgList->Add(wxICON(panel));
-    m_ImgList->Add(wxICON(vsizer));
-    m_ImgList->Add(wxICON(hsizer));
-
-    m_TreeCtrl->SetImageList(m_ImgList);
+    wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
     
-    // Create properties panel:
-    m_Splitter2 = new wxSplitterWindow(m_Splitter);
-    m_PropsPanel = new wxPanel(m_Splitter2, -1, wxDefaultPosition,
-                           wxDefaultSize, wxTAB_TRAVERSAL);
-
-    wxSizer *sizer3 = new wxBoxSizer(wxVERTICAL);
-    
-    wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
-
-    sizer3->Add(new wxButton(m_PropsPanel, ID_EDITCODE, "Edit XML code"), 
-            0, wxALL | wxEXPAND, 2);
-    sz->Add(new wxStaticText(m_PropsPanel, -1, _("XMLID name:")),
-            0, wxLEFT | wxALIGN_CENTER_VERTICAL, 2);
-    m_XMLIDCtrl = new wxTextCtrl(m_PropsPanel, ID_XMLIDEDIT, "");
-    sz->Add(m_XMLIDCtrl, 1, wxLEFT|wxRIGHT, 2);
-    sz->Add(new wxButton(m_PropsPanel, ID_XMLIDPICK, "...", wxDefaultPosition, wxSize(16,-1)), 
-            0, wxRIGHT, 2);  
-    sizer3->Add(sz, 0, wxTOP|wxEXPAND, 2); 
-    
-    m_PropsList = new wxListCtrl(m_PropsPanel, ID_PROPSLIST, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL); 
-
-    m_ImgListProp = new wxImageList(16, 16);
-    m_ImgListProp->Add(wxICON(unused));
-    m_ImgListProp->Add(wxICON(used));
-    m_PropsList->SetImageList(m_ImgListProp, wxIMAGE_LIST_SMALL);
-
-    m_PropsList->InsertColumn(0, _("Property"));
-    m_PropsList->InsertColumn(1, _("Value"));
-    m_PropsList->SetColumnWidth(0, cfg->Read("editor_col0", wxLIST_AUTOSIZE_USEHEADER));
-    m_PropsList->SetColumnWidth(1, cfg->Read("editor_col1", wxLIST_AUTOSIZE_USEHEADER));
-
-    sizer3->Add(m_PropsList, 1, wxALL | wxEXPAND, 2);
-       
-    m_PropsPanel->SetAutoLayout(TRUE);
-    m_PropsPanel->SetSizer(sizer3);
-    m_PropsPanel->Layout();
-
-    m_PropsEditPanel = new wxScrolledWindow(m_Splitter2, -1, wxDefaultPosition,
-                           wxDefaultSize, wxTAB_TRAVERSAL);
-
-    m_Splitter->SplitVertically(m_TreeCtrl, m_Splitter2);
-    m_Splitter->SetSashPosition(cfg->Read("editor_sash", 140));
-
-    m_Splitter2->SplitHorizontally(m_PropsPanel, m_PropsEditPanel);
-    m_Splitter2->SetSashPosition(cfg->Read("editor_sash2", 100));
-                                      
-    p->SetAutoLayout(TRUE);
-    p->SetSizer(sizer2);
-
-
+    // Create tree control:
+    m_TreeCtrl = new EditorTreeCtrl(this, ID_TREE, this);
+    wxImageList *imgList = new wxImageList(16, 16);
+    imgList->Add(wxICON(control));
+    imgList->Add(wxICON(panel));
+    imgList->Add(wxICON(vsizer));
+    imgList->Add(wxICON(hsizer));
+    imgList->Add(wxICON(gsizer));
+    imgList->Add(wxICON(resicon));
+    m_TreeCtrl->AssignImageList(imgList);
+    sizer->Add(m_TreeCtrl, 1, wxEXPAND);
 
     SetAutoLayout(TRUE);
     SetSizer(sizer);
@@ -265,68 +268,36 @@ EditorFrame::EditorFrame(wxFrame *parent, const wxString& filename)
 
 EditorFrame::~EditorFrame()
 {
+    PreviewFrame::Get()->Close();
+    PropertiesFrame::Get()->Close();
+
     wxConfigBase *cfg = wxConfigBase::Get();
     
-    cfg->Write("editor_x", (long)GetPosition().x);
-    cfg->Write("editor_y", (long)GetPosition().y);
-    cfg->Write("editor_w", (long)GetSize().x);
-    cfg->Write("editor_h", (long)GetSize().y);
-    cfg->Write("editor_sash", (long)m_Splitter->GetSashPosition());
-    cfg->Write("editor_sash2", (long)m_Splitter2->GetSashPosition());
-    cfg->Write("editor_col0", (long)m_PropsList->GetColumnWidth(0));
-    cfg->Write("editor_col1", (long)m_PropsList->GetColumnWidth(1));
-
-    delete m_ImgList;
-    delete m_ImgListProp;
-    RefreshProps(NULL);
-}
-
+    cfg->Write(_T("editor_x"), (long)GetPosition().x);
+    cfg->Write(_T("editor_y"), (long)GetPosition().y);
+    cfg->Write(_T("editor_w"), (long)GetSize().x);
+    cfg->Write(_T("editor_h"), (long)GetSize().y);
 
-
-NodeHandler *EditorFrame::FindHandler(wxXmlNode *node)
-{
-    wxNode *n = m_Handlers.GetFirst();
-    while (n)
-    {
-        NodeHandler *h = (NodeHandler*) n->GetData();
-        if (h->CanHandle(node))
-            return h;
-        n = n->GetNext();
-    }
-    return NULL;
+    delete m_Clipboard;
 }
 
 
 
-void EditorFrame::RegisterHandlers(const wxString& dirname)
-{
-    if (!wxDirExists(dirname)) return;
-    
-    wxDir dir(dirname);
-    wxString filename;
-    bool cont;
-    NodeHandler *hnd;
-    
-    cont = dir.GetFirst(&filename, "*.df");
-    while (cont)
-    {
-        hnd = NodeHandler::CreateFromFile(filename, this);
-        if (hnd) m_Handlers.Append(hnd);
-        cont = dir.GetNext(&filename);
-    }
-    
-}
-
-
 
 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))
+    if (!m_Resource->Load(filename,  wxLocale::GetSystemEncodingName()))
     {
         delete m_Resource;
         m_Resource = NULL;
@@ -336,9 +307,12 @@ void EditorFrame::LoadFile(const wxString& filename)
     else
     {
         m_FileName = filename;
+
+        // Upgrades old versions
+        m_Resource->Upgrade();
         RefreshTree();
-        SetTitle(m_FileName);
     }
+    RefreshTitle();
 }
 
 
@@ -346,64 +320,43 @@ void EditorFrame::LoadFile(const wxString& filename)
 void EditorFrame::SaveFile(const wxString& filename)
 {
     m_FileName = filename;
-    SetTitle(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::RefreshPreview(wxXmlNode *node)
+void EditorFrame::RefreshTitle()
 {
-    wxBusyCursor bcur;
-    wxXmlResource *res = new wxXmlResource;
-    wxString tempfile;
-    wxPoint pos = wxDefaultPosition;
-    
-    while (node->GetParent() != m_Resource->GetRoot())
-        node = node->GetParent();
-    
-    if (m_Preview) pos = m_Preview->GetPosition();
-    
-    res->InitAllHandlers();
-    
-    wxGetTempFileName("xmleditor", tempfile);
-    m_Resource->Save(tempfile, wxXML_IO_BIN);
-    res->Load(tempfile, wxXML_BINARY);
-    
-    if (node->GetName() == "dialog")
-    {
-        wxDialog *dlg = new wxDialog;
-        if (res->LoadDialog(dlg, this, node->GetPropVal("name", "-1")))
-        {
-            if (pos.x != -1) dlg->Move(pos);
-            dlg->Show(TRUE);
-            if (m_Preview) m_Preview->Close(TRUE);
-            m_Preview = dlg;
-        }
-        else
-        {
-            delete dlg;
-            wxLogError(_("Cannot preview the dialog -- XML resource corrupted."));
-        }
-    }
-    delete res;
-    wxRemoveFile(tempfile);
+    wxString s;
+    if (m_Modified) s << _T("* ");
+    s << _("wxrcedit");
+    if (m_FileName != "")
+        s << _T(" - ") << wxFileNameFromPath(m_FileName);
+    SetTitle(s);
 }
 
 
@@ -413,7 +366,7 @@ void EditorFrame::RefreshTree()
     wxXmlNode *sel = m_SelectedNode;
     
     m_TreeCtrl->DeleteAllItems(); 
-    wxTreeItemId root = m_TreeCtrl->AddRoot("Resource: " + m_FileName);
+    wxTreeItemId root = m_TreeCtrl->AddRoot("Resource: " + wxFileNameFromPath(m_FileName), 5, 5);
 
     wxXmlNode *n = m_Resource->GetRoot()->GetChildren();  
     while (n)
@@ -428,57 +381,20 @@ void EditorFrame::RefreshTree()
 }
 
 
-void EditorFrame::RefreshProps(wxXmlNode *node)
-{
-    m_SelectedProp = -1;
-    
-    for (int i = 0; i < m_PropsList->GetItemCount(); i++)
-        delete (wxObject*)(m_PropsList->GetItemData(i));   
-    
-    m_PropsList->DeleteAllItems();
 
-    if (node == NULL) return;
-    
-    m_XMLIDCtrl->SetValue(FindHandler(node)->GetRealNode(node)->
-                            GetPropVal("name", "-1"));
-    CreatePropsList(m_PropsList, node);
 
-    RefreshPropsEdit();
-}
-
-
-
-void EditorFrame::RefreshPropsEdit()
+static void RecursivelyExpand(wxTreeCtrl *t, wxTreeItemId item)
 {
-    m_PropsEditPanel->DestroyChildren();
-    m_PropsEditPanel->SetSizer(NULL);
-    
-    if (!m_SelectedNode || m_SelectedProp == -1 ||
-        m_PropsList->GetItemData(m_SelectedProp) == 0) return;
-    
-    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
-    
-    sizer->Add(new wxButton(m_PropsEditPanel, ID_CLEARPROP, _("Clear")),
-               0, wxALL, 5);
-        
-    sizer->Add(
-                FindHandler(m_SelectedNode)->CreatePropEditPanel(m_PropsEditPanel, m_PropsList, m_SelectedProp),
-                1,  wxEXPAND, 0);
-    
-    m_PropsEditPanel->SetAutoLayout(TRUE);
-    m_PropsEditPanel->SetSizer(sizer);
-    m_PropsEditPanel->Layout();
-
-    wxSize winsz = m_PropsEditPanel->GetSize();
-    sizer->SetMinSize(winsz.x, winsz.y);
-    
-    wxSize minsz = sizer->GetMinSize();
-
-    m_PropsEditPanel->SetScrollbars(8, 8, 1/*minsz.x/8*/, minsz.y/8);
+    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)
@@ -499,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;
 }
 
@@ -520,16 +438,7 @@ wxTreeItemId EditorFrame::CreateTreeNode(wxTreeCtrl *treectrl, wxTreeItemId pare
         return invalid;
     }
 
-    return FindHandler(node)->CreateTreeNode(treectrl, parent, node);
-}
-
-
-
-void EditorFrame::CreatePropsList(wxListCtrl *treectrl, wxXmlNode *node)
-{
-    if (!node) return;
-
-    FindHandler(node)->CreatePropsList(treectrl, node);
+    return NodeHandler::Find(node)->CreateTreeNode(treectrl, parent, node);
 }
 
 
@@ -543,18 +452,23 @@ void EditorFrame::NotifyChanged(int change_type)
     {
         wxTreeItemId sel = m_TreeCtrl->GetSelection();
         m_TreeCtrl->SetItemText(sel, 
-             FindHandler(m_SelectedNode)->GetTreeString(m_SelectedNode));
+             NodeHandler::Find(m_SelectedNode)->GetTreeString(m_SelectedNode));
     }
 
     if (change_type & CHANGED_TREE_SELECTED_ICON)
     {
         wxTreeItemId sel = m_TreeCtrl->GetSelection();
-        int icon = FindHandler(m_SelectedNode)->GetTreeIcon(m_SelectedNode);
+        int icon = NodeHandler::Find(m_SelectedNode)->GetTreeIcon(m_SelectedNode);
         m_TreeCtrl->SetItemImage(sel, icon);
     }
-
-    if (change_type & CHANGED_PROPS_PANEL)
-        RefreshProps(m_SelectedNode);
+    
+    if (!m_Modified)
+    {
+        m_Modified = TRUE;
+        RefreshTitle();
+    }
+    
+    PreviewFrame::Get()->MakeDirty();
 }
 
 
@@ -562,146 +476,27 @@ void EditorFrame::NotifyChanged(int change_type)
 void EditorFrame::OnTreeSel(wxTreeEvent& event)
 {
     XmlTreeData *dt = (XmlTreeData*)(m_TreeCtrl->GetItemData(event.GetItem()));
-    wxXmlNode *node = (dt) ? dt->Node : NULL;
-        
+    wxXmlNode *node = (dt) ? dt->Node : NULL;      
+            
     m_SelectedNode = node;
-    RefreshProps(node);
-}
-
-
-
-void EditorFrame::OnXMLIDEdit(wxCommandEvent& event)
-{
-    if (!m_SelectedNode) return;
-    wxXmlNode *node = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
-
-    node->DeleteProperty("name");
-    wxString s = m_XMLIDCtrl->GetValue();
-    if (!(s == "-1")) node->AddProperty("name", s);
-    NotifyChanged(CHANGED_TREE_SELECTED);
-}
-
-
-
-void EditorFrame::OnXMLIDPick(wxCommandEvent& event)
-{
-    if (!m_SelectedNode) return;
-    wxXmlNode *node = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
-
-    wxString choices[] = {wxString("-1")
-    #define stdID(id) , wxString(#id)
-    stdID(wxID_OK) stdID(wxID_CANCEL)
-    stdID(wxID_YES) stdID(wxID_NO)
-    stdID(wxID_APPLY) stdID(wxID_HELP) 
-    stdID(wxID_HELP_CONTEXT)
-
-    stdID(wxID_OPEN) stdID(wxID_CLOSE) stdID(wxID_NEW)
-    stdID(wxID_SAVE) stdID(wxID_SAVEAS) stdID(wxID_REVERT)
-    stdID(wxID_EXIT) stdID(wxID_UNDO) stdID(wxID_REDO)
-    stdID(wxID_PRINT) stdID(wxID_PRINT_SETUP)
-    stdID(wxID_PREVIEW) stdID(wxID_ABOUT) stdID(wxID_HELP_CONTENTS)
-    stdID(wxID_HELP_COMMANDS) stdID(wxID_HELP_PROCEDURES)
-    stdID(wxID_CUT) stdID(wxID_COPY) stdID(wxID_PASTE)
-    stdID(wxID_CLEAR) stdID(wxID_FIND) stdID(wxID_DUPLICATE)
-    stdID(wxID_SELECTALL) 
-    stdID(wxID_STATIC) stdID(wxID_FORWARD) stdID(wxID_BACKWARD)
-    stdID(wxID_DEFAULT) stdID(wxID_MORE) stdID(wxID_SETUP)
-    stdID(wxID_RESET) 
-    #undef stdID
-    };
-
-    wxString s = 
-      wxGetSingleChoice(_("Choose from predefined IDs:"), _("XMLID"), 
-                        38/*sizeof choices*/, choices);
-    if (!s) return;
-
-    m_XMLIDCtrl->SetValue(s);
-    node->DeleteProperty("name");
-    if (!(s == "-1")) node->AddProperty("name", s);
-    NotifyChanged(CHANGED_TREE_SELECTED);
-}
-
-
-
-void EditorFrame::OnEditCode(wxCommandEvent& event)
-{
-    if (!m_SelectedNode) return;
-
-    wxBusyCursor bcur;
-    wxDialog dlg(this, -1, _("XML code editor"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
-    wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
-    wxTextCtrl *tc = new wxTextCtrl(&dlg, -1, "", wxDefaultPosition,
-                         wxDefaultSize, wxTE_MULTILINE);
-    sizer->Add(tc, 1, wxEXPAND | wxALL, 10);
-
-    wxSizer *sz2 = new wxBoxSizer(wxHORIZONTAL);
-
-    sz2->Add(new wxButton(&dlg, wxID_OK, _("Save")), 0);
-    sz2->Add(new wxButton(&dlg, wxID_CANCEL, _("Cancel")), 0, wxLEFT, 10);
-
-    sizer->Add(sz2, 0, wxALIGN_RIGHT | wxRIGHT|wxBOTTOM, 10);
-
-    dlg.SetAutoLayout(TRUE);
-    dlg.SetSizer(sizer);
-
-    wxConfigBase *cfg = wxConfigBase::Get();
-    
-    dlg.SetSize(wxRect(wxPoint(cfg->Read("xmleditor_x", -1), cfg->Read("xmleditor_y", -1)),
-            wxSize(cfg->Read("xmleditor_w", 400), cfg->Read("xmleditor_h", 400))));
-
-    wxString tempfile;
-    wxGetTempFileName("xmleditor", tempfile);
+    if (node)
+        PropertiesFrame::Get()->ShowProps(node);
 
+    if (m_TreeCtrl->GetParent(event.GetItem()) == m_TreeCtrl->GetRootItem())
     {
-        wxXmlDocument doc;
-        doc.SetRoot(new wxXmlNode(*m_SelectedNode));
-        doc.Save(tempfile, wxXML_IO_LIBXML);
-    }
-    tc->LoadFile(tempfile);
+        wxTreeItemId it = event.GetOldItem();
 
-    if (dlg.ShowModal() == wxID_OK)
-    {
-        tc->SaveFile(tempfile);
-        wxXmlDocument doc;
-        if (doc.Load(tempfile))
+        if (it.IsOk() && m_TreeCtrl->GetRootItem() != it)
         {
-            (*m_SelectedNode) = *doc.GetRoot();
-            NotifyChanged(CHANGED_TREE);
-            //FIXME-instead, regenerate only children
+            while (m_TreeCtrl->GetParent(it) != m_TreeCtrl->GetRootItem())
+                it = m_TreeCtrl->GetParent(it);
+            m_TreeCtrl->Collapse(it);
         }
-        else wxLogError(_("Illegal XML file, canceled."));
-    }
-    wxRemoveFile(tempfile);
-
-    cfg->Write("xmleditor_x", (long)dlg.GetPosition().x);
-    cfg->Write("xmleditor_y", (long)dlg.GetPosition().y);
-    cfg->Write("xmleditor_w", (long)dlg.GetSize().x);
-    cfg->Write("xmleditor_h", (long)dlg.GetSize().y);
-}
+        RecursivelyExpand(m_TreeCtrl, event.GetItem());
 
-
-
-void EditorFrame::OnClearProp(wxCommandEvent& event)
-{
-    m_PropsList->SetItemImage(m_SelectedProp, 0, 0);
-    m_PropsList->SetItem(m_SelectedProp, 1, "");
-    
-    PropsListInfo *pli = (PropsListInfo*)m_PropsList->GetItemData(m_SelectedProp);
-    
-    wxXmlNode *nd = XmlFindNode(pli->m_Node, pli->m_PropInfo->Name);
-    
-    if (nd == NULL) return;
-    nd->GetParent()->RemoveChild(nd);
-    delete nd;
-    RefreshPropsEdit();
-}
-
-
-
-void EditorFrame::OnPropSel(wxListEvent& event)
-{
-    m_SelectedProp = event.GetIndex();
-    RefreshPropsEdit();
+        PreviewFrame::Get()->Preview(node,m_Resource->GetRoot()->GetPropVal(
+                                      wxT("version"), wxT("0.0.0.0")));
+    }
 }
 
 
@@ -714,7 +509,8 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
             {
             XmlTreeData* dt = (XmlTreeData*)m_TreeCtrl->GetItemData(m_TreeCtrl->GetSelection());;
             if (dt != NULL && dt->Node != NULL)
-                RefreshPreview(dt->Node);
+                PreviewFrame::Get()->Preview(dt->Node,m_Resource->GetRoot()->GetPropVal(
+                                      wxT("version"), wxT("0.0.0.0")));
             break;
             }
 
@@ -728,7 +524,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
         case ID_OPEN :
             {
-            wxString name = wxFileSelector("Open XML resource", "", "", "", "XML resources|*.xml", wxOPEN | wxFILE_MUST_EXIST);
+            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;
@@ -740,7 +538,9 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
         case ID_SAVEAS :
             {
-            wxString name = wxFileSelector("Save as", "", m_FileName, "", "XML resources|*.xml", wxSAVE | wxOVERWRITE_PROMPT);
+            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;
@@ -748,13 +548,7 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
         case ID_DELETE_NODE :
             {
-            XmlTreeData *dt = (XmlTreeData*)
-                    (m_TreeCtrl->GetItemData(m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
-            wxXmlNode *n = (dt) ? dt->Node : NULL;
-
-            m_SelectedNode->GetParent()->RemoveChild(m_SelectedNode);
-            NotifyChanged(CHANGED_TREE);
-            SelectNode(n);
+            DeleteSelectedNode();
             break;
             }
     }
@@ -762,6 +556,19 @@ void EditorFrame::OnToolbar(wxCommandEvent& event)
 
 
 
+void EditorFrame::DeleteSelectedNode()
+{
+    XmlTreeData *dt = (XmlTreeData*)
+            (m_TreeCtrl->GetItemData(m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+    wxXmlNode *n = (dt) ? dt->Node : NULL;
+
+    m_SelectedNode->GetParent()->RemoveChild(m_SelectedNode);
+    NotifyChanged(CHANGED_TREE);
+    SelectNode(n);
+}
+
+
+
 void EditorFrame::OnNewNode(wxCommandEvent& event)
 {
     if (event.GetId() >= ID_NEWSYBNODE)
@@ -774,11 +581,13 @@ void EditorFrame::OnNewNode(wxCommandEvent& event)
         {
             wxXmlNode *nd = pardt->Node;
 
-            wxXmlNode *realnode = FindHandler(nd)->GetRealNode(nd);
-            NodeHandler *hnd = FindHandler(realnode);
+            wxXmlNode *realnode = NodeHandler::Find(nd)->GetRealNode(nd);
+            NodeHandler *hnd = NodeHandler::Find(realnode);
             wxString name = hnd->GetChildTypes()[event.GetId()-ID_NEWSYBNODE];
 
-            wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
+            wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, _T("object"));
+            node->AddProperty(_T("class"), name);
+
             hnd->InsertNode(realnode, node, m_SelectedNode);
             wxTreeItemId root = m_TreeCtrl->GetSelection();
             SelectNode(node, &root);
@@ -788,11 +597,13 @@ void EditorFrame::OnNewNode(wxCommandEvent& event)
 
     else if (event.GetId() >= ID_NEWNODE)
     {
-        wxXmlNode *realnode = FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode);
-        NodeHandler *hnd = FindHandler(realnode);
+        wxXmlNode *realnode = NodeHandler::Find(m_SelectedNode)->GetRealNode(m_SelectedNode);
+        NodeHandler *hnd = NodeHandler::Find(realnode);
         wxString name = hnd->GetChildTypes()[event.GetId()-ID_NEWNODE];
 
-        wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
+        wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, _T("object"));
+        node->AddProperty(_T("class"), name);
+
         hnd->InsertNode(realnode, node);
         wxTreeItemId root = m_TreeCtrl->GetSelection();
         SelectNode(node, &root);
@@ -803,15 +614,16 @@ void EditorFrame::OnNewNode(wxCommandEvent& event)
         wxString name;
         switch (event.GetId())
         {
-            case ID_NEWDIALOG : name = "dialog"; break;
-            case ID_NEWPANEL : name = "panel"; break;
-            case ID_NEWMENU : name = "menu"; break;
-            case ID_NEWMENUBAR : name = "menubar"; break;
-            case ID_NEWTOOLBAR : name = "toolbar"; break;
+            case ID_NEWDIALOG : name = _T("wxDialog"); break;
+            case ID_NEWPANEL : name = _T("wxPanel"); break;
+            case ID_NEWMENU : name = _T("wxMenu"); break;
+            case ID_NEWMENUBAR : name = _T("wxMenuBar"); break;
+            case ID_NEWTOOLBAR : name = _T("wxToolBar"); break;
             default : return; // never occurs
         }
         
-        wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, name);
+        wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, _T("object"));
+        node->AddProperty(_T("class"), name);
         m_Resource->GetRoot()->AddChild(node);
         NotifyChanged(CHANGED_TREE);
         SelectNode(node);
@@ -826,27 +638,39 @@ void EditorFrame::OnRightClickTree(wxPoint pos)
     
     if (m_SelectedNode == NULL || m_SelectedNode == m_Resource->GetRoot())
     {
-        popup->Append(ID_NEWDIALOG, _("New dialog"));
-        popup->Append(ID_NEWPANEL, _("New panel"));
-        popup->Append(ID_NEWMENU, _("New menu"));
-        popup->Append(ID_NEWMENUBAR, _("New menubar"));
-        popup->Append(ID_NEWTOOLBAR, _("New toolbar"));
+        popup->Append(ID_NEWDIALOG, _("New wxDialog"));
+        popup->Append(ID_NEWPANEL, _("New wxPanel"));
+        popup->Append(ID_NEWMENU, _("New wxMenu"));
+        popup->Append(ID_NEWMENUBAR, _("New wxMenuBar"));
+        popup->Append(ID_NEWTOOLBAR, _("New wxToolBar"));
     }
     
     else
     {   
+        bool has_children;
         {
             wxArrayString& arr = 
-                FindHandler(FindHandler(m_SelectedNode)->GetRealNode(m_SelectedNode))->
+                NodeHandler::Find(NodeHandler::Find(m_SelectedNode)->GetRealNode(m_SelectedNode))->
                     GetChildTypes();
 
+            has_children = !arr.IsEmpty();
             if (!arr.IsEmpty())
             {
                 wxMenu *news = new wxMenu;
+                wxMenu *news2 = news;
                 for (size_t i = 0; i < arr.GetCount(); i++)
                 {
-                    news->Append(i + ID_NEWNODE, arr[i]);
-                    if (i % 16 == 15) news->Break();
+                    news2->Append(i + ID_NEWNODE, arr[i]);
+#ifdef __WXGTK__ // doesn't support Break
+                    if (i % 20 == 19) 
+                    {
+                        wxMenu *m = new wxMenu;
+                        news2->Append(ID_NEWNODE+arr.GetCount(), _("More..."), m);
+                        news2 = m;
+                    }
+#else
+                    if (i % 16 == 15) news2->Break();
+#endif
                 }
                 popup->Append(ID_NEWNODE-1, _("New child"), news);
             }
@@ -860,26 +684,114 @@ void EditorFrame::OnRightClickTree(wxPoint pos)
         {
             wxXmlNode *nd = pardt->Node;
             wxArrayString& arr = 
-                FindHandler(FindHandler(nd)->GetRealNode(nd))->
+                NodeHandler::Find(NodeHandler::Find(nd)->GetRealNode(nd))->
                     GetChildTypes();
 
             if (!arr.IsEmpty())
             {
                 wxMenu *news = new wxMenu;
+                wxMenu *news2 = news;
                 for (size_t i = 0; i < arr.GetCount(); i++)
                 {
-                    news->Append(i + ID_NEWSYBNODE, arr[i]);
-                    if (i % 16 == 15) news->Break();
+                    news2->Append(i + ID_NEWSYBNODE, arr[i]);
+#ifdef __WXGTK__ // doesn't support Break
+                    if (i % 20 == 19) 
+                    {
+                        wxMenu *m = new wxMenu;
+                        news2->Append(ID_NEWSYBNODE+arr.GetCount(), _("More..."), m);
+                        news2 = m;
+                    }
+#else
+                    if (i % 16 == 15) news2->Break();
+#endif
                 }
                 popup->Append(ID_NEWSYBNODE-1, _("New sybling"), news);
             }
         }
 
 
+        popup->AppendSeparator();
+        popup->Append(ID_CUT, _("Cut"));
+        popup->Append(ID_COPY, _("Copy"));
+        popup->Append(ID_PASTE_SYBLING, _("Paste as sybling"));
+        popup->Append(ID_PASTE_CHILD, _("Paste as child"));
         popup->AppendSeparator();
         popup->Append(ID_DELETE_NODE, _("Delete"));
+        popup->Enable(ID_PASTE_SYBLING, m_Clipboard != NULL);
+        popup->Enable(ID_PASTE_CHILD, has_children && m_Clipboard != NULL);
     }
     
     m_TreeCtrl->PopupMenu(popup, pos);
     delete popup;
 }
+
+
+
+void EditorFrame::OnClipboardAction(wxCommandEvent& event)
+{
+    switch (event.GetId())
+    {
+        case ID_COPY:
+        case ID_CUT:
+            delete m_Clipboard;
+            m_Clipboard = new wxXmlNode(*m_SelectedNode);
+            GetMenuBar()->Enable(ID_PASTE_SYBLING, TRUE);
+            GetMenuBar()->Enable(ID_PASTE_CHILD, TRUE);
+            if (event.GetId() == ID_CUT) DeleteSelectedNode();
+            break;
+            
+        case ID_PASTE_SYBLING:
+            {
+            XmlTreeData *pardt = 
+                (XmlTreeData*)(m_TreeCtrl->GetItemData(
+                    m_TreeCtrl->GetParent(m_TreeCtrl->GetSelection())));
+
+            if (pardt && pardt->Node && pardt->Node != m_Resource->GetRoot())
+            {
+                wxXmlNode *nd = pardt->Node;
+
+                wxXmlNode *realnode = NodeHandler::Find(nd)->GetRealNode(nd);
+                NodeHandler *hnd = NodeHandler::Find(realnode);
+                wxXmlNode *node = new wxXmlNode(*m_Clipboard);
+                hnd->InsertNode(realnode, node, m_SelectedNode);
+                wxTreeItemId root = m_TreeCtrl->GetSelection();
+                SelectNode(node, &root);
+            }
+            }
+            break;
+            
+        case ID_PASTE_CHILD:
+            wxXmlNode *realnode = NodeHandler::Find(m_SelectedNode)->GetRealNode(m_SelectedNode);
+            NodeHandler *hnd = NodeHandler::Find(realnode);
+            wxXmlNode *node = new wxXmlNode(*m_Clipboard);
+            hnd->InsertNode(realnode, node);
+            wxTreeItemId root = m_TreeCtrl->GetSelection();
+            SelectNode(node, &root);
+            break;
+    }
+}
+
+
+
+
+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();
+}