]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/tools/XRCed/xrced.py
Allow AutoCompletion for items in collections too
[wxWidgets.git] / wxPython / wx / tools / XRCed / xrced.py
index e05dca1d9eee7847b14407c2dcd733f89c81a024..e4614d4deac1bfeb9361977350f152b300e491e7 100644 (file)
@@ -6,7 +6,7 @@
 
 """
 
 
 """
 
-xrced -- Simple resource editor for XRC format used by wxWindows/wxPython
+xrced -- Simple resource editor for XRC format used by wxWidgets/wxPython
          GUI toolkit.
 
 Usage:
          GUI toolkit.
 
 Usage:
@@ -20,9 +20,9 @@ Options:
     -v          output version info and exit
 """
 
     -v          output version info and exit
 """
 
-
 from globals import *
 from globals import *
-import os, sys, getopt, re, traceback
+import os, sys, getopt, re, traceback, tempfile, shutil, cPickle
+from xml.parsers import expat
 
 # Local modules
 from tree import *                      # imports xxx which imports params
 
 # Local modules
 from tree import *                      # imports xxx which imports params
@@ -54,34 +54,43 @@ Consult README file for the details.</HTML>
 """
 
 defaultIDs = {xxxPanel:'PANEL', xxxDialog:'DIALOG', xxxFrame:'FRAME',
 """
 
 defaultIDs = {xxxPanel:'PANEL', xxxDialog:'DIALOG', xxxFrame:'FRAME',
-              xxxMenuBar:'MENUBAR', xxxMenu:'MENU', xxxToolBar:'TOOLBAR'}
+              xxxMenuBar:'MENUBAR', xxxMenu:'MENU', xxxToolBar:'TOOLBAR',
+              xxxWizard:'WIZARD', xxxBitmap:'BITMAP', xxxIcon:'ICON'}
+
+defaultName = 'UNTITLED.xrc'
 
 ################################################################################
 
 # ScrolledMessageDialog - modified from wxPython lib to set fixed-width font
 
 ################################################################################
 
 # ScrolledMessageDialog - modified from wxPython lib to set fixed-width font
-class ScrolledMessageDialog(wxDialog):
-    def __init__(self, parent, msg, caption, pos = wxDefaultPosition, size = (500,300)):
-        from wxPython.lib.layoutf import Layoutf
-        wxDialog.__init__(self, parent, -1, caption, pos, size)
-        text = wxTextCtrl(self, -1, msg, wxDefaultPosition,
-                             wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY)
-        text.SetFont(modernFont)
-        dc = wxWindowDC(text)
+class ScrolledMessageDialog(wx.Dialog):
+    def __init__(self, parent, msg, caption, pos = wx.DefaultPosition, size = (500,300)):
+        from wx.lib.layoutf import Layoutf
+        wx.Dialog.__init__(self, parent, -1, caption, pos, size)
+        text = wx.TextCtrl(self, -1, msg, wx.DefaultPosition,
+                             wx.DefaultSize, wx.TE_MULTILINE | wx.TE_READONLY)
+        text.SetFont(g.modernFont())
+        dc = wx.WindowDC(text)
         # !!! possible bug - GetTextExtent without font returns sysfont dims
         # !!! possible bug - GetTextExtent without font returns sysfont dims
-        w, h = dc.GetFullTextExtent(' ', modernFont)[:2]
-        ok = wxButton(self, wxID_OK, "OK")
+        w, h = dc.GetFullTextExtent(' ', g.modernFont())[:2]
+        ok = wx.Button(self, wx.ID_OK, "OK")
         text.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok)))
         text.SetSize((w * 80 + 30, h * 40))
         text.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok)))
         text.SetSize((w * 80 + 30, h * 40))
+        text.ShowPosition(1)
         ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,)))
         self.SetAutoLayout(True)
         self.Fit()
         ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,)))
         self.SetAutoLayout(True)
         self.Fit()
-        self.CenterOnScreen(wxBOTH)
+        self.CenterOnScreen(wx.BOTH)
 
 ################################################################################
 
 
 ################################################################################
 
-class Frame(wxFrame):
+# Event handler for using during location
+class Locator(wx.EvtHandler):
+    def ProcessEvent(self, evt):
+        print evt
+
+class Frame(wx.Frame):
     def __init__(self, pos, size):
     def __init__(self, pos, size):
-        wxFrame.__init__(self, None, -1, '', pos, size)
+        wx.Frame.__init__(self, None, -1, '', pos, size)
         global frame
         frame = g.frame = self
         bar = self.CreateStatusBar(2)
         global frame
         frame = g.frame = self
         bar = self.CreateStatusBar(2)
@@ -91,130 +100,174 @@ class Frame(wxFrame):
         # Idle flag
         self.inIdle = False
 
         # Idle flag
         self.inIdle = False
 
+        # Load our own resources
+        self.res = xrc.XmlResource('')
+        # !!! Blocking of assert failure occurring in older unicode builds
+        try:
+            quietlog = wx.LogNull()
+            self.res.Load(os.path.join(basePath, 'xrced.xrc'))
+        except wx._core.PyAssertionError:
+            print 'PyAssertionError was ignored'
+
         # Make menus
         # Make menus
-        menuBar = wxMenuBar()
+        menuBar = wx.MenuBar()
 
 
-        menu = wxMenu()
-        menu.Append(wxID_NEW, '&New\tCtrl-N', 'New file')
-        menu.Append(wxID_OPEN, '&Open...\tCtrl-O', 'Open XRC file')
-        menu.Append(wxID_SAVE, '&Save\tCtrl-S', 'Save XRC file')
-        menu.Append(wxID_SAVEAS, 'Save &As...', 'Save XRC file under different name')
+        menu = wx.Menu()
+        menu.Append(wx.ID_NEW, '&New\tCtrl-N', 'New file')
+        menu.AppendSeparator()
+        menu.Append(wx.ID_OPEN, '&Open...\tCtrl-O', 'Open XRC file')
+        self.recentMenu = wx.Menu()
+        self.AppendRecent(self.recentMenu)
+        menu.AppendMenu(-1, 'Open Recent', self.recentMenu, 'Open a recent file')
         menu.AppendSeparator()
         menu.AppendSeparator()
-        menu.Append(wxID_EXIT, '&Quit\tCtrl-Q', 'Exit application')
+        menu.Append(wx.ID_SAVE, '&Save\tCtrl-S', 'Save XRC file')
+        menu.Append(wx.ID_SAVEAS, 'Save &As...', 'Save XRC file under different name')
+        self.ID_GENERATE_PYTHON = wx.NewId()
+        menu.Append(self.ID_GENERATE_PYTHON, '&Generate Python...', 
+                    'Generate a Python module that uses this XRC')
+        menu.AppendSeparator()
+        menu.Append(wx.ID_EXIT, '&Quit\tCtrl-Q', 'Exit application')
+
         menuBar.Append(menu, '&File')
 
         menuBar.Append(menu, '&File')
 
-        menu = wxMenu()
-        menu.Append(wxID_UNDO, '&Undo\tCtrl-Z', 'Undo')
-        menu.Append(wxID_REDO, '&Redo\tCtrl-Y', 'Redo')
+        menu = wx.Menu()
+        menu.Append(wx.ID_UNDO, '&Undo\tCtrl-Z', 'Undo')
+        menu.Append(wx.ID_REDO, '&Redo\tCtrl-Y', 'Redo')
         menu.AppendSeparator()
         menu.AppendSeparator()
-        menu.Append(wxID_CUT, 'Cut\tCtrl-X', 'Cut to the clipboard')
-        menu.Append(wxID_COPY, '&Copy\tCtrl-C', 'Copy to the clipboard')
-        menu.Append(wxID_PASTE, '&Paste\tCtrl-V', 'Paste from the clipboard')
-        self.ID_DELETE = wxNewId()
+        menu.Append(wx.ID_CUT, 'Cut\tCtrl-X', 'Cut to the clipboard')
+        menu.Append(wx.ID_COPY, '&Copy\tCtrl-C', 'Copy to the clipboard')
+        menu.Append(wx.ID_PASTE, '&Paste\tCtrl-V', 'Paste from the clipboard')
+        self.ID_DELETE = wx.NewId()
         menu.Append(self.ID_DELETE, '&Delete\tCtrl-D', 'Delete object')
         menu.Append(self.ID_DELETE, '&Delete\tCtrl-D', 'Delete object')
-#        menu.AppendSeparator()
-        ID_SELECT = wxNewId()
-#        menu.Append(ID_SELECT, '&Select', 'Select object')
+        menu.AppendSeparator()
+        self.ID_LOCATE = wx.NewId()
+        self.ID_TOOL_LOCATE = wx.NewId()
+        self.ID_TOOL_PASTE = wx.NewId()
+        menu.Append(self.ID_LOCATE, '&Locate\tCtrl-L', 'Locate control in test window and select it')
         menuBar.Append(menu, '&Edit')
 
         menuBar.Append(menu, '&Edit')
 
-        menu = wxMenu()
-        self.ID_EMBED_PANEL = wxNewId()
+        menu = wx.Menu()
+        self.ID_EMBED_PANEL = wx.NewId()
         menu.Append(self.ID_EMBED_PANEL, '&Embed Panel',
                     'Toggle embedding properties panel in the main window', True)
         menu.Check(self.ID_EMBED_PANEL, conf.embedPanel)
         menu.Append(self.ID_EMBED_PANEL, '&Embed Panel',
                     'Toggle embedding properties panel in the main window', True)
         menu.Check(self.ID_EMBED_PANEL, conf.embedPanel)
-        self.ID_SHOW_TOOLS = wxNewId()
+        self.ID_SHOW_TOOLS = wx.NewId()
         menu.Append(self.ID_SHOW_TOOLS, 'Show &Tools', 'Toggle tools', True)
         menu.Check(self.ID_SHOW_TOOLS, conf.showTools)
         menu.AppendSeparator()
         menu.Append(self.ID_SHOW_TOOLS, 'Show &Tools', 'Toggle tools', True)
         menu.Check(self.ID_SHOW_TOOLS, conf.showTools)
         menu.AppendSeparator()
-        self.ID_TEST = wxNewId()
-        menu.Append(self.ID_TEST, '&Test\tF5', 'Test window')
-        self.ID_REFRESH = wxNewId()
+        self.ID_TEST = wx.NewId()
+        menu.Append(self.ID_TEST, '&Test\tF5', 'Show test window')
+        self.ID_REFRESH = wx.NewId()
         menu.Append(self.ID_REFRESH, '&Refresh\tCtrl-R', 'Refresh test window')
         menu.Append(self.ID_REFRESH, '&Refresh\tCtrl-R', 'Refresh test window')
-        self.ID_AUTO_REFRESH = wxNewId()
+        self.ID_AUTO_REFRESH = wx.NewId()
         menu.Append(self.ID_AUTO_REFRESH, '&Auto-refresh\tCtrl-A',
                     'Toggle auto-refresh mode', True)
         menu.Check(self.ID_AUTO_REFRESH, conf.autoRefresh)
         menu.Append(self.ID_AUTO_REFRESH, '&Auto-refresh\tCtrl-A',
                     'Toggle auto-refresh mode', True)
         menu.Check(self.ID_AUTO_REFRESH, conf.autoRefresh)
+        self.ID_TEST_HIDE = wx.NewId()
+        menu.Append(self.ID_TEST_HIDE, '&Hide\tCtrl-H', 'Close test window')
         menuBar.Append(menu, '&View')
 
         menuBar.Append(menu, '&View')
 
-        menu = wxMenu()
-        menu.Append(wxID_ABOUT, '&About...', 'About XCRed')
-        self.ID_README = wxNewId()
+        menu = wx.Menu()
+        menu.Append(wx.ID_ABOUT, '&About...', 'About XCRed')
+        self.ID_README = wx.NewId()
         menu.Append(self.ID_README, '&Readme...', 'View the README file')
         if debug:
         menu.Append(self.ID_README, '&Readme...', 'View the README file')
         if debug:
-            self.ID_DEBUG_CMD = wxNewId()
+            self.ID_DEBUG_CMD = wx.NewId()
             menu.Append(self.ID_DEBUG_CMD, 'CMD', 'Python command line')
             menu.Append(self.ID_DEBUG_CMD, 'CMD', 'Python command line')
-            EVT_MENU(self, self.ID_DEBUG_CMD, self.OnDebugCMD)
+            wx.EVT_MENU(self, self.ID_DEBUG_CMD, self.OnDebugCMD)
         menuBar.Append(menu, '&Help')
 
         self.menuBar = menuBar
         self.SetMenuBar(menuBar)
 
         # Create toolbar
         menuBar.Append(menu, '&Help')
 
         self.menuBar = menuBar
         self.SetMenuBar(menuBar)
 
         # Create toolbar
-        tb = self.CreateToolBar(wxTB_HORIZONTAL | wxNO_BORDER | wxTB_FLAT)
-        tb.SetToolBitmapSize((24, 23))
-        tb.AddSimpleTool(wxID_NEW, images.getNewBitmap(), 'New', 'New file')
-        tb.AddSimpleTool(wxID_OPEN, images.getOpenBitmap(), 'Open', 'Open file')
-        tb.AddSimpleTool(wxID_SAVE, images.getSaveBitmap(), 'Save', 'Save file')
-        tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL))
-        tb.AddSimpleTool(wxID_UNDO, images.getUndoBitmap(), 'Undo', 'Undo')
-        tb.AddSimpleTool(wxID_REDO, images.getRedoBitmap(), 'Redo', 'Redo')
-        tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL))
-        tb.AddSimpleTool(wxID_CUT, images.getCutBitmap(), 'Cut', 'Cut')
-        tb.AddSimpleTool(wxID_COPY, images.getCopyBitmap(), 'Copy', 'Copy')
-        tb.AddSimpleTool(wxID_PASTE, images.getPasteBitmap(), 'Paste', 'Paste')
-        tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL))
+        tb = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)
+        tb.SetToolBitmapSize((24,24))
+        new_bmp  = wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_TOOLBAR)
+        open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR)
+        save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR)
+        undo_bmp = wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR)
+        redo_bmp = wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR)
+        cut_bmp  = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR)
+        copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR)
+        paste_bmp= wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR)
+        
+        tb.AddSimpleTool(wx.ID_NEW, new_bmp, 'New', 'New file')
+        tb.AddSimpleTool(wx.ID_OPEN, open_bmp, 'Open', 'Open file')
+        tb.AddSimpleTool(wx.ID_SAVE, save_bmp, 'Save', 'Save file')
+        tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL))
+        tb.AddSimpleTool(wx.ID_UNDO, undo_bmp, 'Undo', 'Undo')
+        tb.AddSimpleTool(wx.ID_REDO, redo_bmp, 'Redo', 'Redo')
+        tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL))
+        tb.AddSimpleTool(wx.ID_CUT, cut_bmp, 'Cut', 'Cut')
+        tb.AddSimpleTool(wx.ID_COPY, copy_bmp, 'Copy', 'Copy')
+        tb.AddSimpleTool(self.ID_TOOL_PASTE, paste_bmp, 'Paste', 'Paste')
+        tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL))
+        tb.AddSimpleTool(self.ID_TOOL_LOCATE,
+                        images.getLocateBitmap(), #images.getLocateArmedBitmap(),
+                        'Locate', 'Locate control in test window and select it', True)
+        tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL))
         tb.AddSimpleTool(self.ID_TEST, images.getTestBitmap(), 'Test', 'Test window')
         tb.AddSimpleTool(self.ID_REFRESH, images.getRefreshBitmap(),
                          'Refresh', 'Refresh view')
         tb.AddSimpleTool(self.ID_AUTO_REFRESH, images.getAutoRefreshBitmap(),
                          'Auto-refresh', 'Toggle auto-refresh mode', True)
         tb.AddSimpleTool(self.ID_TEST, images.getTestBitmap(), 'Test', 'Test window')
         tb.AddSimpleTool(self.ID_REFRESH, images.getRefreshBitmap(),
                          'Refresh', 'Refresh view')
         tb.AddSimpleTool(self.ID_AUTO_REFRESH, images.getAutoRefreshBitmap(),
                          'Auto-refresh', 'Toggle auto-refresh mode', True)
-        if wxPlatform == '__WXGTK__':
-            tb.AddSeparator()   # otherwise auto-refresh sticks in status line
+#        if wx.Platform == '__WXGTK__':
+#            tb.AddSeparator()   # otherwise auto-refresh sticks in status line
         tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh)
         tb.Realize()
         tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh)
         tb.Realize()
         self.tb = tb
         self.minWidth = tb.GetSize()[0] # minimal width is the size of toolbar
 
         # File
         self.tb = tb
         self.minWidth = tb.GetSize()[0] # minimal width is the size of toolbar
 
         # File
-        EVT_MENU(self, wxID_NEW, self.OnNew)
-        EVT_MENU(self, wxID_OPEN, self.OnOpen)
-        EVT_MENU(self, wxID_SAVE, self.OnSaveOrSaveAs)
-        EVT_MENU(self, wxID_SAVEAS, self.OnSaveOrSaveAs)
-        EVT_MENU(self, wxID_EXIT, self.OnExit)
+        wx.EVT_MENU(self, wx.ID_NEW, self.OnNew)
+        wx.EVT_MENU(self, wx.ID_OPEN, self.OnOpen)
+        wx.EVT_MENU(self, wx.ID_SAVE, self.OnSaveOrSaveAs)
+        wx.EVT_MENU(self, wx.ID_SAVEAS, self.OnSaveOrSaveAs)
+        wx.EVT_MENU(self, self.ID_GENERATE_PYTHON, self.OnGeneratePython)
+        wx.EVT_MENU(self, wx.ID_EXIT, self.OnExit)
         # Edit
         # Edit
-        EVT_MENU(self, wxID_UNDO, self.OnUndo)
-        EVT_MENU(self, wxID_REDO, self.OnRedo)
-        EVT_MENU(self, wxID_CUT, self.OnCutDelete)
-        EVT_MENU(self, wxID_COPY, self.OnCopy)
-        EVT_MENU(self, wxID_PASTE, self.OnPaste)
-        EVT_MENU(self, self.ID_DELETE, self.OnCutDelete)
-        EVT_MENU(self, ID_SELECT, self.OnSelect)
+        wx.EVT_MENU(self, wx.ID_UNDO, self.OnUndo)
+        wx.EVT_MENU(self, wx.ID_REDO, self.OnRedo)
+        wx.EVT_MENU(self, wx.ID_CUT, self.OnCutDelete)
+        wx.EVT_MENU(self, wx.ID_COPY, self.OnCopy)
+        wx.EVT_MENU(self, wx.ID_PASTE, self.OnPaste)
+        wx.EVT_MENU(self, self.ID_TOOL_PASTE, self.OnPaste)
+        wx.EVT_MENU(self, self.ID_DELETE, self.OnCutDelete)
+        wx.EVT_MENU(self, self.ID_LOCATE, self.OnLocate)
+        wx.EVT_MENU(self, self.ID_TOOL_LOCATE, self.OnLocate)
         # View
         # View
-        EVT_MENU(self, self.ID_EMBED_PANEL, self.OnEmbedPanel)
-        EVT_MENU(self, self.ID_SHOW_TOOLS, self.OnShowTools)
-        EVT_MENU(self, self.ID_TEST, self.OnTest)
-        EVT_MENU(self, self.ID_REFRESH, self.OnRefresh)
-        EVT_MENU(self, self.ID_AUTO_REFRESH, self.OnAutoRefresh)
+        wx.EVT_MENU(self, self.ID_EMBED_PANEL, self.OnEmbedPanel)
+        wx.EVT_MENU(self, self.ID_SHOW_TOOLS, self.OnShowTools)
+        wx.EVT_MENU(self, self.ID_TEST, self.OnTest)
+        wx.EVT_MENU(self, self.ID_REFRESH, self.OnRefresh)
+        wx.EVT_MENU(self, self.ID_AUTO_REFRESH, self.OnAutoRefresh)
+        wx.EVT_MENU(self, self.ID_TEST_HIDE, self.OnTestHide)
         # Help
         # Help
-        EVT_MENU(self, wxID_ABOUT, self.OnAbout)
-        EVT_MENU(self, self.ID_README, self.OnReadme)
+        wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
+        wx.EVT_MENU(self, self.ID_README, self.OnReadme)
 
         # Update events
 
         # Update events
-        EVT_UPDATE_UI(self, wxID_CUT, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, wxID_COPY, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, wxID_PASTE, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, wxID_UNDO, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, wxID_REDO, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, self.ID_DELETE, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, self.ID_TEST, self.OnUpdateUI)
-        EVT_UPDATE_UI(self, self.ID_REFRESH, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_LOCATE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_TOOL_LOCATE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_TOOL_PASTE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_DELETE, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_TEST, self.OnUpdateUI)
+        wx.EVT_UPDATE_UI(self, self.ID_REFRESH, self.OnUpdateUI)
 
         # Build interface
 
         # Build interface
-        sizer = wxBoxSizer(wxVERTICAL)
-        sizer.Add(wxStaticLine(self, -1), 0, wxEXPAND)
+        sizer = wx.BoxSizer(wx.VERTICAL)
+        sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
         # Horizontal sizer for toolbar and splitter
         # Horizontal sizer for toolbar and splitter
-        self.toolsSizer = sizer1 = wxBoxSizer()
-        splitter = wxSplitterWindow(self, -1, style=wxSP_3DSASH)
+        self.toolsSizer = sizer1 = wx.BoxSizer()
+        splitter = wx.SplitterWindow(self, -1, style=wx.SP_3DSASH)
         self.splitter = splitter
         splitter.SetMinimumPaneSize(100)
         # Create tree
         self.splitter = splitter
         splitter.SetMinimumPaneSize(100)
         # Create tree
@@ -228,20 +281,20 @@ class Frame(wxFrame):
         # Vertical toolbar for GUI buttons
         g.tools = tools = Tools(self)
         tools.Show(conf.showTools)
         # Vertical toolbar for GUI buttons
         g.tools = tools = Tools(self)
         tools.Show(conf.showTools)
-        if conf.showTools: sizer1.Add(tools, 0, wxEXPAND)
+        if conf.showTools: sizer1.Add(tools, 0, wx.EXPAND)
 
         tree.RegisterKeyEvents()
 
         # !!! frame styles are broken
         # Miniframe for not embedded mode
 
         tree.RegisterKeyEvents()
 
         # !!! frame styles are broken
         # Miniframe for not embedded mode
-        miniFrame = wxFrame(self, -1, 'Properties Panel',
+        miniFrame = wx.Frame(self, -1, 'Properties & Style',
                             (conf.panelX, conf.panelY),
                             (conf.panelWidth, conf.panelHeight))
         self.miniFrame = miniFrame
                             (conf.panelX, conf.panelY),
                             (conf.panelWidth, conf.panelHeight))
         self.miniFrame = miniFrame
-        sizer2 = wxBoxSizer()
+        sizer2 = wx.BoxSizer()
         miniFrame.SetAutoLayout(True)
         miniFrame.SetSizer(sizer2)
         miniFrame.SetAutoLayout(True)
         miniFrame.SetSizer(sizer2)
-        EVT_CLOSE(self.miniFrame, self.OnCloseMiniFrame)
+        wx.EVT_CLOSE(self.miniFrame, self.OnCloseMiniFrame)
         # Create panel for parameters
         global panel
         if conf.embedPanel:
         # Create panel for parameters
         global panel
         if conf.embedPanel:
@@ -250,24 +303,44 @@ class Frame(wxFrame):
             splitter.SplitVertically(tree, panel, conf.sashPos)
         else:
             panel = Panel(miniFrame)
             splitter.SplitVertically(tree, panel, conf.sashPos)
         else:
             panel = Panel(miniFrame)
-            sizer2.Add(panel, 1, wxEXPAND)
+            sizer2.Add(panel, 1, wx.EXPAND)
             miniFrame.Show(True)
             splitter.Initialize(tree)
             miniFrame.Show(True)
             splitter.Initialize(tree)
-        sizer1.Add(splitter, 1, wxEXPAND)
-        sizer.Add(sizer1, 1, wxEXPAND)
+        sizer1.Add(splitter, 1, wx.EXPAND)
+        sizer.Add(sizer1, 1, wx.EXPAND)
         self.SetAutoLayout(True)
         self.SetSizer(sizer)
 
         # Initialize
         self.SetAutoLayout(True)
         self.SetSizer(sizer)
 
         # Initialize
-        self.clipboard = None
         self.Clear()
 
         # Other events
         self.Clear()
 
         # Other events
-        EVT_IDLE(self, self.OnIdle)
-        EVT_CLOSE(self, self.OnCloseWindow)
-        EVT_LEFT_DOWN(self, self.OnLeftDown)
-        EVT_KEY_DOWN(self, tools.OnKeyDown)
-        EVT_KEY_UP(self, tools.OnKeyUp)
+        wx.EVT_IDLE(self, self.OnIdle)
+        wx.EVT_CLOSE(self, self.OnCloseWindow)
+        wx.EVT_KEY_DOWN(self, tools.OnKeyDown)
+        wx.EVT_KEY_UP(self, tools.OnKeyUp)
+        wx.EVT_ICONIZE(self, self.OnIconize)
+    
+    def AppendRecent(self, menu):
+        # add recently used files to the menu
+        for id,name in conf.recentfiles.iteritems():
+            menu.Append(id,name)
+            wx.EVT_MENU(self,id,self.OnRecentFile)
+        return 
+        
+    def OnRecentFile(self,evt):
+        # open recently used file
+        if not self.AskSave(): return
+        wx.BeginBusyCursor()
+        try:
+            path=conf.recentfiles[evt.GetId()]
+            if self.Open(path):
+                self.SetStatusText('Data loaded')
+            else:
+                self.SetStatusText('Failed')
+        except KeyError:
+            self.SetStatusText('No such file')
+        wx.EndBusyCursor()
 
     def OnNew(self, evt):
         if not self.AskSave(): return
 
     def OnNew(self, evt):
         if not self.AskSave(): return
@@ -275,46 +348,103 @@ class Frame(wxFrame):
 
     def OnOpen(self, evt):
         if not self.AskSave(): return
 
     def OnOpen(self, evt):
         if not self.AskSave(): return
-        dlg = wxFileDialog(self, 'Open', os.path.dirname(self.dataFile),
-                           '', '*.xrc', wxOPEN | wxCHANGE_DIR)
-        if dlg.ShowModal() == wxID_OK:
+        dlg = wx.FileDialog(self, 'Open', os.path.dirname(self.dataFile),
+                           '', '*.xrc', wx.OPEN | wx.CHANGE_DIR)
+        if dlg.ShowModal() == wx.ID_OK:
             path = dlg.GetPath()
             self.SetStatusText('Loading...')
             path = dlg.GetPath()
             self.SetStatusText('Loading...')
-            wxYield()
-            wxBeginBusyCursor()
-            if self.Open(path):
-                self.SetStatusText('Data loaded')
-            else:
-                self.SetStatusText('Failed')
-            wxEndBusyCursor()
+            wx.BeginBusyCursor()
+            try:
+                if self.Open(path):
+                    self.SetStatusText('Data loaded')
+                else:
+                    self.SetStatusText('Failed')
+                self.SaveRecent(path)
+            finally:
+                wx.EndBusyCursor()
         dlg.Destroy()
 
     def OnSaveOrSaveAs(self, evt):
         dlg.Destroy()
 
     def OnSaveOrSaveAs(self, evt):
-        if evt.GetId() == wxID_SAVEAS or not self.dataFile:
-            if self.dataFile: defaultName = ''
-            else: defaultName = 'UNTITLED.xrc'
-            dlg = wxFileDialog(self, 'Save As', os.path.dirname(self.dataFile),
-                               defaultName, '*.xrc',
-                               wxSAVE | wxOVERWRITE_PROMPT | wxCHANGE_DIR)
-            if dlg.ShowModal() == wxID_OK:
+        if evt.GetId() == wx.ID_SAVEAS or not self.dataFile:
+            if self.dataFile: name = ''
+            else: name = defaultName
+            dirname = os.path.abspath(os.path.dirname(self.dataFile))
+            dlg = wx.FileDialog(self, 'Save As', dirname, name, '*.xrc',
+                               wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
+            if dlg.ShowModal() == wx.ID_OK:
                 path = dlg.GetPath()
                 dlg.Destroy()
             else:
                 dlg.Destroy()
                 return
                 path = dlg.GetPath()
                 dlg.Destroy()
             else:
                 dlg.Destroy()
                 return
+
+            if conf.localconf:
+                # if we already have a localconf then it needs to be
+                # copied to a new config with the new name
+                lc = conf.localconf
+                nc = self.CreateLocalConf(path)
+                flag, key, idx = lc.GetFirstEntry()
+                while flag:
+                    nc.Write(key, lc.Read(key))
+                    flag, key, idx = lc.GetNextEntry(idx)
+                conf.localconf = nc
+            else:
+                # otherwise create a new one
+                conf.localconf = self.CreateLocalConf(path)
         else:
             path = self.dataFile
         self.SetStatusText('Saving...')
         else:
             path = self.dataFile
         self.SetStatusText('Saving...')
-        wxYield()
-        wxBeginBusyCursor()
+        wx.BeginBusyCursor()
         try:
         try:
-            self.Save(path)
-            self.dataFile = path
-            self.SetStatusText('Data saved')
-        except IOError:
-            self.SetStatusText('Failed')
-        wxEndBusyCursor()
+            try:
+                tmpFile,tmpName = tempfile.mkstemp(prefix='xrced-')
+                os.close(tmpFile)
+                self.Save(tmpName) # save temporary file first
+                shutil.move(tmpName, path)
+                self.dataFile = path
+                if conf.localconf.ReadBool("autogenerate", False):
+                    pypath = conf.localconf.Read("filename")
+                    embed = conf.localconf.ReadBool("embedResource", False)
+                    genGettext = conf.localconf.ReadBool("genGettext", False)
+                    self.GeneratePython(self.dataFile, pypath, embed, genGettext)
+                    
+                self.SetStatusText('Data saved')
+                self.SaveRecent(path)
+            except IOError:
+                self.SetStatusText('Failed')
+        finally:
+            wx.EndBusyCursor()        
+
+    def SaveRecent(self,path):
+        # append to recently used files
+        if path not in conf.recentfiles.values():
+            newid = wx.NewId()
+            self.recentMenu.Append(newid, path)
+            wx.EVT_MENU(self, newid, self.OnRecentFile)
+            conf.recentfiles[newid] = path
+
+    def GeneratePython(self, dataFile, pypath, embed, genGettext):
+        try:
+            import wx.tools.pywxrc
+            rescomp = wx.tools.pywxrc.XmlResourceCompiler()
+            rescomp.MakePythonModule([dataFile], pypath, embed, genGettext)
+        except:
+            inf = sys.exc_info()
+            wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
+            wx.LogError('Error generating python code : %s' % pypath)
+            raise
+        
+
+    def OnGeneratePython(self, evt):
+        if self.modified or not conf.localconf:
+            wx.MessageBox("Save the XRC file first!", "Error")
+            return
+        
+        dlg = PythonOptions(self, conf.localconf, self.dataFile)
+        dlg.ShowModal()
+        dlg.Destroy()
 
 
+        
     def OnExit(self, evt):
         self.Close()
 
     def OnExit(self, evt):
         self.Close()
 
@@ -331,14 +461,27 @@ class Frame(wxFrame):
         selected = tree.selection
         if not selected: return         # key pressed event
         xxx = tree.GetPyData(selected)
         selected = tree.selection
         if not selected: return         # key pressed event
         xxx = tree.GetPyData(selected)
-        self.clipboard = xxx.element.cloneNode(True)
-        self.SetStatusText('Copied')
+        if wx.TheClipboard.Open():
+            data = wx.CustomDataObject('XRCED')
+            # Set encoding in header
+            # (False,True)
+            s = xxx.element.toxml(encoding=expat.native_encoding)
+            data.SetData(cPickle.dumps(s))
+            wx.TheClipboard.SetData(data)
+            wx.TheClipboard.Close()
+            self.SetStatusText('Copied')
+        else:
+            wx.MessageBox("Unable to open the clipboard", "Error")
 
     def OnPaste(self, evt):
         selected = tree.selection
         if not selected: return         # key pressed event
         # For pasting with Ctrl pressed
 
     def OnPaste(self, evt):
         selected = tree.selection
         if not selected: return         # key pressed event
         # For pasting with Ctrl pressed
+        appendChild = True
         if evt.GetId() == pullDownMenu.ID_PASTE_SIBLING: appendChild = False
         if evt.GetId() == pullDownMenu.ID_PASTE_SIBLING: appendChild = False
+        elif evt.GetId() == self.ID_TOOL_PASTE:
+            if g.tree.ctrl: appendChild = False
+            else: appendChild = not tree.NeedInsert(selected)
         else: appendChild = not tree.NeedInsert(selected)
         xxx = tree.GetPyData(selected)
         if not appendChild:
         else: appendChild = not tree.NeedInsert(selected)
         xxx = tree.GetPyData(selected)
         if not appendChild:
@@ -348,29 +491,46 @@ class Frame(wxFrame):
         # Expanded container (must have children)
         elif tree.IsExpanded(selected) and tree.GetChildrenCount(selected, False):
             # Insert as first child
         # Expanded container (must have children)
         elif tree.IsExpanded(selected) and tree.GetChildrenCount(selected, False):
             # Insert as first child
-            nextItem = tree.GetFirstChild(selected, 0)[0]
+            nextItem = tree.GetFirstChild(selected)[0]
             parentLeaf = selected
         else:
             # No children or unexpanded item - appendChild stays True
             parentLeaf = selected
         else:
             # No children or unexpanded item - appendChild stays True
-            nextItem = wxTreeItemId()   # no next item
+            nextItem = wx.TreeItemId()   # no next item
             parentLeaf = selected
         parent = tree.GetPyData(parentLeaf).treeObject()
 
             parentLeaf = selected
         parent = tree.GetPyData(parentLeaf).treeObject()
 
-        # Create a copy of clipboard element
-        elem = self.clipboard.cloneNode(True)
+        # Create a copy of clipboard pickled element
+        success = False
+        if wx.TheClipboard.Open():
+            data = wx.CustomDataObject('XRCED')
+            if wx.TheClipboard.IsSupported(data.GetFormat()):
+                success = wx.TheClipboard.GetData(data)
+            wx.TheClipboard.Close()
+
+        if not success:
+            wx.MessageBox(
+                "There is no data in the clipboard in the required format",
+                "Error")
+            return
+
+        xml = cPickle.loads(data.GetData()) # xml representation of element
+        elem = minidom.parseString(xml).childNodes[0]
         # Tempopary xxx object to test things
         xxx = MakeXXXFromDOM(parent, elem)
         # Tempopary xxx object to test things
         xxx = MakeXXXFromDOM(parent, elem)
-
         # Check compatibility
         error = False
         # Top-level
         x = xxx.treeObject()
         # Check compatibility
         error = False
         # Top-level
         x = xxx.treeObject()
-        if x.__class__ in [xxxDialog, xxxFrame, xxxMenuBar]:
+        if x.__class__ in [xxxDialog, xxxFrame, xxxWizard]:
             # Top-level classes
             if parent.__class__ != xxxMainNode: error = True
             # Top-level classes
             if parent.__class__ != xxxMainNode: error = True
+        elif x.__class__ == xxxMenuBar:
+            # Menubar can be put in frame or dialog
+            if parent.__class__ not in [xxxMainNode, xxxFrame, xxxDialog]: error = True
         elif x.__class__ == xxxToolBar:
             # Toolbar can be top-level of child of panel or frame
         elif x.__class__ == xxxToolBar:
             # Toolbar can be top-level of child of panel or frame
-            if parent.__class__ not in [xxxMainNode, xxxPanel, xxxFrame]: error = True
+            if parent.__class__ not in [xxxMainNode, xxxPanel, xxxFrame] and \
+               not parent.isSizer: error = True
         elif x.__class__ == xxxPanel and parent.__class__ == xxxMainNode:
             pass
         elif x.__class__ == xxxSpacer:
         elif x.__class__ == xxxPanel and parent.__class__ == xxxMainNode:
             pass
         elif x.__class__ == xxxSpacer:
@@ -383,14 +543,15 @@ class Frame(wxFrame):
             if not parent.__class__ in [xxxMainNode, xxxMenuBar, xxxMenu]: error = True
         elif x.__class__ == xxxMenuItem:
             if not parent.__class__ in [xxxMenuBar, xxxMenu]: error = True
             if not parent.__class__ in [xxxMainNode, xxxMenuBar, xxxMenu]: error = True
         elif x.__class__ == xxxMenuItem:
             if not parent.__class__ in [xxxMenuBar, xxxMenu]: error = True
-        elif x.isSizer and parent.__class__ == xxxNotebook: error = True
+        elif x.isSizer and parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
+            error = True
         else:                           # normal controls can be almost anywhere
             if parent.__class__ == xxxMainNode or \
                parent.__class__ in [xxxMenuBar, xxxMenu]: error = True
         if error:
             if parent.__class__ == xxxMainNode: parentClass = 'root'
             else: parentClass = parent.className
         else:                           # normal controls can be almost anywhere
             if parent.__class__ == xxxMainNode or \
                parent.__class__ in [xxxMenuBar, xxxMenu]: error = True
         if error:
             if parent.__class__ == xxxMainNode: parentClass = 'root'
             else: parentClass = parent.className
-            wxLogError('Incompatible parent/child: parent is %s, child is %s!' %
+            wx.LogError('Incompatible parent/child: parent is %s, child is %s!' %
                        (parentClass, x.className))
             return
 
                        (parentClass, x.className))
             return
 
@@ -398,10 +559,11 @@ class Frame(wxFrame):
         # If parent is sizer or notebook, child is of wrong class or
         # parent is normal window, child is child container then detach child.
         isChildContainer = isinstance(xxx, xxxChildContainer)
         # If parent is sizer or notebook, child is of wrong class or
         # parent is normal window, child is child container then detach child.
         isChildContainer = isinstance(xxx, xxxChildContainer)
+        parentIsBook = parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]
         if isChildContainer and \
            ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \
         if isChildContainer and \
            ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \
-            (isinstance(parent, xxxNotebook) and not isinstance(xxx, xxxNotebookPage)) or \
-           not (parent.isSizer or isinstance(parent, xxxNotebook))):
+            (parentIsBook and not isinstance(xxx, xxxPage)) or \
+           not (parent.isSizer or parentIsBook)):
             elem.removeChild(xxx.child.element) # detach child
             elem.unlink()           # delete child container
             elem = xxx.child.element # replace
             elem.removeChild(xxx.child.element) # detach child
             elem.unlink()           # delete child container
             elem = xxx.child.element # replace
@@ -411,13 +573,21 @@ class Frame(wxFrame):
         # Parent is sizer or notebook, child is not child container
         if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer):
             # Create sizer item element
         # Parent is sizer or notebook, child is not child container
         if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer):
             # Create sizer item element
-            sizerItemElem = MakeEmptyDOM('sizeritem')
+            sizerItemElem = MakeEmptyDOM(parent.itemTag)
             sizerItemElem.appendChild(elem)
             elem = sizerItemElem
         elif isinstance(parent, xxxNotebook) and not isChildContainer:
             pageElem = MakeEmptyDOM('notebookpage')
             pageElem.appendChild(elem)
             elem = pageElem
             sizerItemElem.appendChild(elem)
             elem = sizerItemElem
         elif isinstance(parent, xxxNotebook) and not isChildContainer:
             pageElem = MakeEmptyDOM('notebookpage')
             pageElem.appendChild(elem)
             elem = pageElem
+        elif isinstance(parent, xxxChoicebook) and not isChildContainer:
+            pageElem = MakeEmptyDOM('choicebookpage')
+            pageElem.appendChild(elem)
+            elem = pageElem
+        elif isinstance(parent, xxxListbook) and not isChildContainer:
+            pageElem = MakeEmptyDOM('listbookpage')
+            pageElem.appendChild(elem)
+            elem = pageElem
         # Insert new node, register undo
         newItem = tree.InsertNode(parentLeaf, parent, elem, nextItem)
         undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected))
         # Insert new node, register undo
         newItem = tree.InsertNode(parentLeaf, parent, elem, nextItem)
         undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected))
@@ -434,14 +604,15 @@ class Frame(wxFrame):
                 tree.pendingHighLight = newItem
             else:
                 tree.pendingHighLight = None
                 tree.pendingHighLight = newItem
             else:
                 tree.pendingHighLight = None
-        self.modified = True
+        self.SetModified()
         self.SetStatusText('Pasted')
 
         self.SetStatusText('Pasted')
 
+        
     def OnCutDelete(self, evt):
         selected = tree.selection
         if not selected: return         # key pressed event
         # Undo info
     def OnCutDelete(self, evt):
         selected = tree.selection
         if not selected: return         # key pressed event
         # Undo info
-        if evt.GetId() == wxID_CUT:
+        if evt.GetId() == wx.ID_CUT:
             self.lastOp = 'CUT'
             status = 'Removed to clipboard'
         else:
             self.lastOp = 'CUT'
             status = 'Removed to clipboard'
         else:
@@ -464,25 +635,42 @@ class Frame(wxFrame):
         parent = tree.GetPyData(tree.GetItemParent(selected)).treeObject()
         elem = tree.RemoveLeaf(selected)
         undoMan.RegisterUndo(UndoCutDelete(index, parent, elem))
         parent = tree.GetPyData(tree.GetItemParent(selected)).treeObject()
         elem = tree.RemoveLeaf(selected)
         undoMan.RegisterUndo(UndoCutDelete(index, parent, elem))
-        if evt.GetId() == wxID_CUT:
-            if self.clipboard: self.clipboard.unlink()
-            self.clipboard = elem.cloneNode(True)
+        if evt.GetId() == wx.ID_CUT:
+            if wx.TheClipboard.Open():
+                data = wx.CustomDataObject('XRCED')
+                # (False, True)
+                s = elem.toxml(encoding=expat.native_encoding)
+                data.SetData(cPickle.dumps(s))
+                wx.TheClipboard.SetData(data)
+                wx.TheClipboard.Close()
+            else:
+                wx.MessageBox("Unable to open the clipboard", "Error")
         tree.pendingHighLight = None
         tree.pendingHighLight = None
-        tree.Unselect()
+        tree.UnselectAll()
+        tree.selection = None
+        # Update tools
+        g.tools.UpdateUI()
         panel.Clear()
         panel.Clear()
-        self.modified = True
+        self.SetModified()
         self.SetStatusText(status)
 
         self.SetStatusText(status)
 
-    def OnSelect(self, evt):
-        print >> sys.stderr, 'Xperimental function!'
-        wxYield()
-        self.SetCursor(wxCROSS_CURSOR)
-        self.CaptureMouse()
-
-    def OnLeftDown(self, evt):
-        pos = evt.GetPosition()
-        self.SetCursor(wxNullCursor)
-        self.ReleaseMouse()
+    def OnSubclass(self, evt):
+        selected = tree.selection
+        xxx = tree.GetPyData(selected).treeObject()
+        elem = xxx.element
+        subclass = xxx.subclass
+        dlg = wx.TextEntryDialog(self, 'Subclass:', defaultValue=subclass)
+        if dlg.ShowModal() == wx.ID_OK:
+            subclass = dlg.GetValue()
+            if subclass:
+                elem.setAttribute('subclass', subclass)
+            elif elem.hasAttribute('subclass'):
+                elem.removeAttribute('subclass')
+            self.SetModified()
+            xxx.subclass = elem.getAttribute('subclass')
+            tree.SetItemText(selected, xxx.treeName())
+            panel.pages[0].box.SetLabel(xxx.panelName())
+        dlg.Destroy()
 
     def OnEmbedPanel(self, evt):
         conf.embedPanel = evt.IsChecked()
 
     def OnEmbedPanel(self, evt):
         conf.embedPanel = evt.IsChecked()
@@ -494,8 +682,7 @@ class Frame(wxFrame):
             pos = self.GetPosition()
             sizePanel = panel.GetSize()
             panel.Reparent(self.splitter)
             pos = self.GetPosition()
             sizePanel = panel.GetSize()
             panel.Reparent(self.splitter)
-            self.miniFrame.GetSizer().RemoveWindow(panel)
-            wxYield()
+            self.miniFrame.GetSizer().Remove(panel)
             # Widen
             self.SetDimensions(pos.x, pos.y, size.width + sizePanel.width, size.height)
             self.splitter.SplitVertically(tree, panel, conf.sashPos)
             # Widen
             self.SetDimensions(pos.x, pos.y, size.width + sizePanel.width, size.height)
             self.splitter.SplitVertically(tree, panel, conf.sashPos)
@@ -509,11 +696,10 @@ class Frame(wxFrame):
             sizer = self.miniFrame.GetSizer()
             panel.Reparent(self.miniFrame)
             panel.Show(True)
             sizer = self.miniFrame.GetSizer()
             panel.Reparent(self.miniFrame)
             panel.Show(True)
-            sizer.Add(panel, 1, wxEXPAND)
+            sizer.Add(panel, 1, wx.EXPAND)
             self.miniFrame.Show(True)
             self.miniFrame.SetDimensions(conf.panelX, conf.panelY,
                                          conf.panelWidth, conf.panelHeight)
             self.miniFrame.Show(True)
             self.miniFrame.SetDimensions(conf.panelX, conf.panelY,
                                          conf.panelWidth, conf.panelHeight)
-            wxYield()
             # Reduce width
             self.SetDimensions(pos.x, pos.y,
                                max(size.width - sizePanel.width, self.minWidth), size.height)
             # Reduce width
             self.SetDimensions(pos.x, pos.y,
                                max(size.width - sizePanel.width, self.minWidth), size.height)
@@ -522,7 +708,7 @@ class Frame(wxFrame):
         conf.showTools = evt.IsChecked()
         g.tools.Show(conf.showTools)
         if conf.showTools:
         conf.showTools = evt.IsChecked()
         g.tools.Show(conf.showTools)
         if conf.showTools:
-            self.toolsSizer.Prepend(g.tools, 0, wxEXPAND)
+            self.toolsSizer.Prepend(g.tools, 0, wx.EXPAND)
         else:
             self.toolsSizer.Remove(g.tools)
         self.toolsSizer.Layout()
         else:
             self.toolsSizer.Remove(g.tools)
         self.toolsSizer.Layout()
@@ -531,6 +717,61 @@ class Frame(wxFrame):
         if not tree.selection: return   # key pressed event
         tree.ShowTestWindow(tree.selection)
 
         if not tree.selection: return   # key pressed event
         tree.ShowTestWindow(tree.selection)
 
+    def OnTestHide(self, evt):
+        tree.CloseTestWindow()
+
+    # Find object by relative position
+    def FindObject(self, item, obj):
+        # We simply perform depth-first traversal, sinse it's too much
+        # hassle to deal with all sizer/window combinations
+        w = tree.FindNodeObject(item)
+        if w == obj or isinstance(w, wx.GBSizerItem) and w.GetWindow() == obj:
+            return item
+        if tree.ItemHasChildren(item):
+            child = tree.GetFirstChild(item)[0]
+            while child:
+                found = self.FindObject(child, obj)
+                if found: return found
+                child = tree.GetNextSibling(child)
+        return None
+
+    def OnTestWinLeftDown(self, evt):
+        pos = evt.GetPosition()
+        self.SetHandler(g.testWin)
+        g.testWin.Disconnect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN)
+        item = self.FindObject(g.testWin.item, evt.GetEventObject())
+        if item:
+            tree.EnsureVisible(item)
+            tree.SelectItem(item)
+        self.tb.ToggleTool(self.ID_TOOL_LOCATE, False)
+        if item:
+            self.SetStatusText('Selected %s' % tree.GetItemText(item))
+        else:
+            self.SetStatusText('Locate failed!')
+
+    def SetHandler(self, w, h=None):
+        if h:
+            w.SetEventHandler(h)
+            w.SetCursor(wx.CROSS_CURSOR)
+        else:
+            w.SetEventHandler(w)
+            w.SetCursor(wx.NullCursor)
+        for ch in w.GetChildren():
+            self.SetHandler(ch, h)
+
+    def OnLocate(self, evt):
+        if g.testWin:
+            if evt.GetId() == self.ID_LOCATE or \
+               evt.GetId() == self.ID_TOOL_LOCATE and evt.IsChecked():
+                self.SetHandler(g.testWin, g.testWin)
+                g.testWin.Connect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN, self.OnTestWinLeftDown)
+                if evt.GetId() == self.ID_LOCATE:
+                    self.tb.ToggleTool(self.ID_TOOL_LOCATE, True)
+            elif evt.GetId() == self.ID_TOOL_LOCATE and not evt.IsChecked():
+                self.SetHandler(g.testWin, None)
+                g.testWin.Disconnect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN)
+            self.SetStatusText('Click somewhere in your test window now')
+
     def OnRefresh(self, evt):
         # If modified, apply first
         selection = tree.selection
     def OnRefresh(self, evt):
         # If modified, apply first
         selection = tree.selection
@@ -556,7 +797,7 @@ XRCed version %s
 (c) Roman Rolinsky <rollrom@users.sourceforge.net>
 Homepage: http://xrced.sourceforge.net\
 ''' % version
 (c) Roman Rolinsky <rollrom@users.sourceforge.net>
 Homepage: http://xrced.sourceforge.net\
 ''' % version
-        dlg = wxMessageDialog(self, str, 'About XRCed', wxOK | wxCENTRE)
+        dlg = wx.MessageDialog(self, str, 'About XRCed', wx.OK | wx.CENTRE)
         dlg.ShowModal()
         dlg.Destroy()
 
         dlg.ShowModal()
         dlg.Destroy()
 
@@ -568,7 +809,6 @@ Homepage: http://xrced.sourceforge.net\
 
     # Simple emulation of python command line
     def OnDebugCMD(self, evt):
 
     # Simple emulation of python command line
     def OnDebugCMD(self, evt):
-        import traceback
         while 1:
             try:
                 exec raw_input('C:\> ')
         while 1:
             try:
                 exec raw_input('C:\> ')
@@ -600,24 +840,38 @@ Homepage: http://xrced.sourceforge.net\
         # Expanded container (must have children)
         elif tree.shift and tree.IsExpanded(selected) \
            and tree.GetChildrenCount(selected, False):
         # Expanded container (must have children)
         elif tree.shift and tree.IsExpanded(selected) \
            and tree.GetChildrenCount(selected, False):
-            nextItem = tree.GetFirstChild(selected, 0)[0]
+            nextItem = tree.GetFirstChild(selected)[0]
             parentLeaf = selected
         else:
             parentLeaf = selected
         else:
-            nextItem = wxTreeItemId()
+            nextItem = wx.TreeItemId()
             parentLeaf = selected
         parent = tree.GetPyData(parentLeaf)
         if parent.hasChild: parent = parent.child
 
             parentLeaf = selected
         parent = tree.GetPyData(parentLeaf)
         if parent.hasChild: parent = parent.child
 
-        # Create element
-        className = pullDownMenu.createMap[evt.GetId()]
-        xxx = MakeEmptyXXX(parent, className)
+        # Create object_ref?
+        if evt.GetId() == ID_NEW.REF:
+            ref = wx.GetTextFromUser('Create reference to:', 'Create reference')
+            if not ref: return
+            xxx = MakeEmptyRefXXX(parent, ref)
+        else:
+            # Create empty element
+            className = pullDownMenu.createMap[evt.GetId()]
+            xxx = MakeEmptyXXX(parent, className)
 
         # Set default name for top-level windows
         if parent.__class__ == xxxMainNode:
             cl = xxx.treeObject().__class__
             frame.maxIDs[cl] += 1
 
         # Set default name for top-level windows
         if parent.__class__ == xxxMainNode:
             cl = xxx.treeObject().__class__
             frame.maxIDs[cl] += 1
-            xxx.treeObject().name = '%s%d' % (defaultIDs[cl], frame.maxIDs[cl])
-            xxx.treeObject().element.setAttribute('name', xxx.treeObject().name)
+            xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl]))
+        # And for some other standard controls
+        elif parent.__class__ == xxxStdDialogButtonSizer:
+            xxx.setTreeName(pullDownMenu.stdButtonIDs[evt.GetId()][0])
+            # We can even set label
+            obj = xxx.treeObject()
+            elem = g.tree.dom.createElement('label')
+            elem.appendChild(g.tree.dom.createTextNode(pullDownMenu.stdButtonIDs[evt.GetId()][1]))
+            obj.params['label'] = xxxParam(elem)
+            xxx.treeObject().element.appendChild(elem)
 
         # Insert new node, register undo
         elem = xxx.element
 
         # Insert new node, register undo
         elem = xxx.element
@@ -636,7 +890,7 @@ Homepage: http://xrced.sourceforge.net\
             else:
                 tree.pendingHighLight = None
         tree.SetFocus()
             else:
                 tree.pendingHighLight = None
         tree.SetFocus()
-        self.modified = True
+        self.SetModified()
 
     # Replace one object with another
     def OnReplace(self, evt):
 
     # Replace one object with another
     def OnReplace(self, evt):
@@ -644,25 +898,30 @@ Homepage: http://xrced.sourceforge.net\
         xxx = tree.GetPyData(selected).treeObject()
         elem = xxx.element
         parent = elem.parentNode
         xxx = tree.GetPyData(selected).treeObject()
         elem = xxx.element
         parent = elem.parentNode
-        parentXXX = xxx.parent
+        undoMan.RegisterUndo(UndoReplace(selected))
         # New class
         className = pullDownMenu.createMap[evt.GetId() - 1000]
         # Create temporary empty node (with default values)
         dummy = MakeEmptyDOM(className)
         # New class
         className = pullDownMenu.createMap[evt.GetId() - 1000]
         # Create temporary empty node (with default values)
         dummy = MakeEmptyDOM(className)
-        xxxClass = xxxDict[className]
+        if className == 'spacer' and xxx.className != 'spacer':
+            klass = xxxSpacer
+        elif xxx.className == 'spacer' and className != 'spacer':
+            klass = xxxSizerItem
+        else:
+            klass = xxxDict[className]
         # Remove non-compatible children
         # Remove non-compatible children
-        if tree.ItemHasChildren(selected) and not xxxClass.hasChildren:
+        if tree.ItemHasChildren(selected) and not klass.hasChildren:
             tree.DeleteChildren(selected)
         nodes = elem.childNodes[:]
         tags = []
         for node in nodes:
             tree.DeleteChildren(selected)
         nodes = elem.childNodes[:]
         tags = []
         for node in nodes:
+            if node.nodeType != minidom.Node.ELEMENT_NODE: continue
             remove = False
             tag = node.tagName
             if tag == 'object':
             remove = False
             tag = node.tagName
             if tag == 'object':
-                if not xxxClass.hasChildren:
-                    remove = True
-            elif tag not in xxxClass.allParams and \
-                     (not xxxClass.hasStyle or tag not in xxxClass.styles):
+                if not klass.hasChildren:  remove = True
+            elif tag not in klass.allParams and \
+                     (not klass.hasStyle or tag not in klass.styles):
                 remove = True
             else:
                 tags.append(tag)
                 remove = True
             else:
                 tags.append(tag)
@@ -670,31 +929,53 @@ Homepage: http://xrced.sourceforge.net\
                 elem.removeChild(node)
                 node.unlink()
         
                 elem.removeChild(node)
                 node.unlink()
         
-        # Copy parameters present in dummy but not in elem
-        for node in dummy.childNodes:
-            tag = node.tagName
-            if tag not in tags:
-                elem.appendChild(node.cloneNode(True))
+        # Remove sizeritem child if spacer
+        if className == 'spacer' and xxx.className != 'spacer':
+            sizeritem = elem.parentNode
+            assert sizeritem.getAttribute('class') == 'sizeritem'
+            sizeritem.removeChild(elem)
+            elem.unlink()
+            elem = sizeritem
+            tree.GetPyData(selected).hasChild = false
+        elif xxx.className == 'spacer' and className != 'spacer':
+            # Create sizeritem element
+            assert xxx.parent.isSizer
+            elem.setAttribute('class', 'sizeritem')
+            node = MakeEmptyDOM(className)
+            elem.appendChild(node)
+            # Replace to point to new object
+            xxx = xxxSizerItem(xxx.parent, elem)
+            elem = node
+            tree.SetPyData(selected, xxx)
+            xxx = xxx.child
+        else:
+            # Copy parameters present in dummy but not in elem
+            for node in dummy.childNodes:
+                if node.tagName not in tags:  elem.appendChild(node.cloneNode(True))
         dummy.unlink()
         dummy.unlink()
+        
         # Change class name
         # Change class name
-        elem.setAttribute('class', className)        
+        elem.setAttribute('class', className)
+        if elem.hasAttribute('subclass'):
+            elem.removeAttribute('subclass') # clear subclassing
         # Re-create xxx element
         # Re-create xxx element
-        xxx = MakeXXXFromDOM(parentXXX, elem)
+        xxx = MakeXXXFromDOM(xxx.parent, elem)
         # Update parent in child objects
         if tree.ItemHasChildren(selected):
         # Update parent in child objects
         if tree.ItemHasChildren(selected):
-            i, cookie = tree.GetFirstChild(selected, 0)
+            i, cookie = tree.GetFirstChild(selected)
             while i.IsOk():
                 x = tree.GetPyData(i)
                 x.parent = xxx
                 if x.hasChild: x.child.parent = xxx
                 i, cookie = tree.GetNextChild(selected, cookie)
             while i.IsOk():
                 x = tree.GetPyData(i)
                 x.parent = xxx
                 if x.hasChild: x.child.parent = xxx
                 i, cookie = tree.GetNextChild(selected, cookie)
-    
+
         # Update tree
         if tree.GetPyData(selected).hasChild: # child container
             container = tree.GetPyData(selected)
             container.child = xxx
             container.hasChildren = xxx.hasChildren
             container.isSizer = xxx.isSizer
         # Update tree
         if tree.GetPyData(selected).hasChild: # child container
             container = tree.GetPyData(selected)
             container.child = xxx
             container.hasChildren = xxx.hasChildren
             container.isSizer = xxx.isSizer
+            xxx = container
         else:
             tree.SetPyData(selected, xxx)
         tree.SetItemText(selected, xxx.treeName())
         else:
             tree.SetPyData(selected, xxx)
         tree.SetItemText(selected, xxx.treeName())
@@ -704,8 +985,7 @@ Homepage: http://xrced.sourceforge.net\
         if parent.__class__ == xxxMainNode:
             cl = xxx.treeObject().__class__
             frame.maxIDs[cl] += 1
         if parent.__class__ == xxxMainNode:
             cl = xxx.treeObject().__class__
             frame.maxIDs[cl] += 1
-            xxx.treeObject().name = '%s%d' % (defaultIDs[cl], frame.maxIDs[cl])
-            xxx.treeObject().element.setAttribute('name', xxx.treeObject().name)
+            xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl]))
 
         # Update panel
         g.panel.SetData(xxx)
 
         # Update panel
         g.panel.SetData(xxx)
@@ -721,7 +1001,7 @@ Homepage: http://xrced.sourceforge.net\
             else:
                 tree.pendingHighLight = None
         tree.SetFocus()
             else:
                 tree.pendingHighLight = None
         tree.SetFocus()
-        self.modified = True
+        self.SetModified()
 
     # Expand/collapse subtree
     def OnExpand(self, evt):
 
     # Expand/collapse subtree
     def OnExpand(self, evt):
@@ -741,44 +1021,50 @@ Homepage: http://xrced.sourceforge.net\
             self.SetStatusText('')
 
     def OnUpdateUI(self, evt):
             self.SetStatusText('')
 
     def OnUpdateUI(self, evt):
-        if evt.GetId() in [wxID_CUT, wxID_COPY, self.ID_DELETE]:
+        if evt.GetId() in [wx.ID_CUT, wx.ID_COPY, self.ID_DELETE]:
             evt.Enable(tree.selection is not None and tree.selection != tree.root)
             evt.Enable(tree.selection is not None and tree.selection != tree.root)
-        elif evt.GetId() == wxID_PASTE:
-            evt.Enable((self.clipboard and tree.selection) != None)
+        elif evt.GetId() == wx.ID_SAVE:
+            evt.Enable(self.modified)
+        elif evt.GetId() in [wx.ID_PASTE, self.ID_TOOL_PASTE]:
+            evt.Enable(tree.selection is not None)
         elif evt.GetId() == self.ID_TEST:
             evt.Enable(tree.selection is not None and tree.selection != tree.root)
         elif evt.GetId() == self.ID_TEST:
             evt.Enable(tree.selection is not None and tree.selection != tree.root)
-        elif evt.GetId() == wxID_UNDO:  evt.Enable(undoMan.CanUndo())
-        elif evt.GetId() == wxID_REDO:  evt.Enable(undoMan.CanRedo())
+        elif evt.GetId() in [self.ID_LOCATE, self.ID_TOOL_LOCATE]:
+            evt.Enable(g.testWin is not None)
+        elif evt.GetId() == wx.ID_UNDO:  evt.Enable(undoMan.CanUndo())
+        elif evt.GetId() == wx.ID_REDO:  evt.Enable(undoMan.CanRedo())
 
     def OnIdle(self, evt):
         if self.inIdle: return          # Recursive call protection
         self.inIdle = True
 
     def OnIdle(self, evt):
         if self.inIdle: return          # Recursive call protection
         self.inIdle = True
-        if tree.needUpdate:
-            if conf.autoRefresh:
-                if g.testWin:
-                    self.SetStatusText('Refreshing test window...')
-                    # (re)create
-                    tree.CreateTestWin(g.testWin.item)
-                    wxYield()
-                    self.SetStatusText('')
-                tree.needUpdate = False
-        elif tree.pendingHighLight:
-            tree.HighLight(tree.pendingHighLight)
-        else:
-            evt.Skip()
-        self.inIdle = False
+        try:
+            if tree.needUpdate:
+                if conf.autoRefresh:
+                    if g.testWin:
+                        self.SetStatusText('Refreshing test window...')
+                        # (re)create
+                        tree.CreateTestWin(g.testWin.item)
+                        self.SetStatusText('')
+                    tree.needUpdate = False
+            elif tree.pendingHighLight:
+                try:
+                    tree.HighLight(tree.pendingHighLight)
+                except:
+                    # Remove highlight if any problem
+                    if g.testWin.highLight:
+                        g.testWin.highLight.Remove()
+                    tree.pendingHighLight = None
+                    raise
+            else:
+                evt.Skip()
+        finally:
+            self.inIdle = False
 
     # We don't let close panel window
     def OnCloseMiniFrame(self, evt):
         return
 
 
     # We don't let close panel window
     def OnCloseMiniFrame(self, evt):
         return
 
-    def OnCloseWindow(self, evt):
-        if not self.AskSave(): return
-        if g.testWin: g.testWin.Destroy()
-        # Destroy cached windows
-        panel.cacheParent.Destroy()
-        if not panel.GetPageCount() == 2:
-            panel.page2.Destroy()
+    def OnIconize(self, evt):
         conf.x, conf.y = self.GetPosition()
         conf.width, conf.height = self.GetSize()
         if conf.embedPanel:
         conf.x, conf.y = self.GetPosition()
         conf.width, conf.height = self.GetSize()
         if conf.embedPanel:
@@ -786,60 +1072,89 @@ Homepage: http://xrced.sourceforge.net\
         else:
             conf.panelX, conf.panelY = self.miniFrame.GetPosition()
             conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize()
         else:
             conf.panelX, conf.panelY = self.miniFrame.GetPosition()
             conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize()
+            self.miniFrame.Iconize()
+        evt.Skip()
+
+    def OnCloseWindow(self, evt):
+        if not self.AskSave(): return
+        if g.testWin: g.testWin.Destroy()
+        if not panel.GetPageCount() == 2:
+            panel.page2.Destroy()
+        else:
+            # If we don't do this, page does not get destroyed (a bug?)
+            panel.RemovePage(1)
+        if not self.IsIconized():
+            conf.x, conf.y = self.GetPosition()
+            conf.width, conf.height = self.GetSize()
+            if conf.embedPanel:
+                conf.sashPos = self.splitter.GetSashPosition()
+            else:
+                conf.panelX, conf.panelY = self.miniFrame.GetPosition()
+                conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize()
         evt.Skip()
 
         evt.Skip()
 
+
+    def CreateLocalConf(self, path):
+        name = os.path.splitext(path)[0]
+        name += '.xcfg'
+        return wx.FileConfig(localFilename=name)
+
+
     def Clear(self):
         self.dataFile = ''
     def Clear(self):
         self.dataFile = ''
-        if self.clipboard:
-            self.clipboard.unlink()
-            self.clipboard = None
+        conf.localconf = None
         undoMan.Clear()
         undoMan.Clear()
-        self.modified = False
+        self.SetModified(False)
         tree.Clear()
         panel.Clear()
         if g.testWin:
             g.testWin.Destroy()
             g.testWin = None
         tree.Clear()
         panel.Clear()
         if g.testWin:
             g.testWin.Destroy()
             g.testWin = None
-        self.SetTitle(progname)
         # Numbers for new controls
         self.maxIDs = {}
         # Numbers for new controls
         self.maxIDs = {}
-        self.maxIDs[xxxPanel] = self.maxIDs[xxxDialog] = self.maxIDs[xxxFrame] = \
-        self.maxIDs[xxxMenuBar] = self.maxIDs[xxxMenu] = self.maxIDs[xxxToolBar] = 0
+        for cl in [xxxPanel, xxxDialog, xxxFrame,
+                   xxxMenuBar, xxxMenu, xxxToolBar,
+                   xxxWizard, xxxBitmap, xxxIcon]:
+            self.maxIDs[cl] = 0
+
+    def SetModified(self, state=True):
+        self.modified = state
+        name = os.path.basename(self.dataFile)
+        if not name: name = defaultName
+        if state:
+            self.SetTitle(progname + ': ' + name + ' *')
+        else:
+            self.SetTitle(progname + ': ' + name)
 
     def Open(self, path):
         if not os.path.exists(path):
 
     def Open(self, path):
         if not os.path.exists(path):
-            wxLogError('File does not exists: %s' % path)
+            wx.LogError('File does not exists: %s' % path)
             return False
         # Try to read the file
         try:
             f = open(path)
             self.Clear()
             return False
         # Try to read the file
         try:
             f = open(path)
             self.Clear()
-            # Parse first line to get encoding (!! hack, I don't know a better way)
-            line = f.readline()
-            mo = re.match(r'^<\?xml ([^<>]* )?encoding="(?P<encd>[^<>].*)"\?>', line)
-            # Build wx tree
-            f.seek(0)
             dom = minidom.parse(f)
             dom = minidom.parse(f)
-            # Set encoding global variable and document encoding property
-            if mo:
-                dom.encoding = g.currentEncoding = mo.group('encd')
-                if dom.encoding not in ['ascii', sys.getdefaultencoding()]:
-                    wxLogWarning('Encoding is different from system default')
-            else:
-                g.currentEncoding = 'ascii'
-                dom.encoding = ''
             f.close()
             f.close()
+            # Set encoding global variable and default encoding
+            if dom.encoding:
+                g.currentEncoding = dom.encoding
+                wx.SetDefaultPyEncoding(g.currentEncoding.encode())
+            else:
+                g.currentEncoding = ''
             # Change dir
             # Change dir
+            self.dataFile = path = os.path.abspath(path)
             dir = os.path.dirname(path)
             if dir: os.chdir(dir)
             tree.SetData(dom)
             dir = os.path.dirname(path)
             if dir: os.chdir(dir)
             tree.SetData(dom)
-            self.dataFile = path
             self.SetTitle(progname + ': ' + os.path.basename(path))
             self.SetTitle(progname + ': ' + os.path.basename(path))
+            conf.localconf = self.CreateLocalConf(self.dataFile)
         except:
             # Nice exception printing
             inf = sys.exc_info()
         except:
             # Nice exception printing
             inf = sys.exc_info()
-            wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1])
-            wxLogError('Error reading file: %s' % path)
+            wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
+            wx.LogError('Error reading file: %s' % path)
+            if debug: raise
             return False
         return True
 
             return False
         return True
 
@@ -862,39 +1177,51 @@ Homepage: http://xrced.sourceforge.net\
 
     def Save(self, path):
         try:
 
     def Save(self, path):
         try:
+            import codecs
             # Apply changes
             if tree.selection and panel.IsModified():
             # Apply changes
             if tree.selection and panel.IsModified():
-                self.OnRefresh(wxCommandEvent())
-            f = open(path, 'w')
+                self.OnRefresh(wx.CommandEvent())
+            if g.currentEncoding:
+                f = codecs.open(path, 'wt', g.currentEncoding)
+            else:
+                f = codecs.open(path, 'wt')
             # Make temporary copy for formatting it
             # !!! We can't clone dom node, it works only once
             #self.domCopy = tree.dom.cloneNode(True)
             self.domCopy = MyDocument()
             mainNode = self.domCopy.appendChild(tree.mainNode.cloneNode(True))
             # Make temporary copy for formatting it
             # !!! We can't clone dom node, it works only once
             #self.domCopy = tree.dom.cloneNode(True)
             self.domCopy = MyDocument()
             mainNode = self.domCopy.appendChild(tree.mainNode.cloneNode(True))
+            # Remove first child (test element)
+            testElem = mainNode.firstChild
+            mainNode.removeChild(testElem)
+            testElem.unlink()
             self.Indent(mainNode)
             self.Indent(mainNode)
-            self.domCopy.writexml(f, encoding=tree.rootObj.params['encoding'].value())
+            self.domCopy.writexml(f, encoding = g.currentEncoding)
             f.close()
             self.domCopy.unlink()
             self.domCopy = None
             f.close()
             self.domCopy.unlink()
             self.domCopy = None
-            self.modified = False
+            self.SetModified(False)
             panel.SetModified(False)
             panel.SetModified(False)
+            conf.localconf.Flush()
         except:
         except:
-            wxLogError('Error writing file: %s' % path)
+            inf = sys.exc_info()
+            wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
+            wx.LogError('Error writing file: %s' % path)
             raise
             raise
-
+            
     def AskSave(self):
         if not (self.modified or panel.IsModified()): return True
     def AskSave(self):
         if not (self.modified or panel.IsModified()): return True
-        flags = wxICON_EXCLAMATION | wxYES_NO | wxCANCEL | wxCENTRE
-        dlg = wxMessageDialog( self, 'File is modified. Save before exit?',
+        flags = wx.ICON_EXCLAMATION | wx.YES_NO | wx.CANCEL | wx.CENTRE
+        dlg = wx.MessageDialog( self, 'File is modified. Save before exit?',
                                'Save before too late?', flags )
         say = dlg.ShowModal()
         dlg.Destroy()
                                'Save before too late?', flags )
         say = dlg.ShowModal()
         dlg.Destroy()
-        if say == wxID_YES:
-            self.OnSaveOrSaveAs(wxCommandEvent(wxID_SAVE))
+        wx.Yield()
+        if say == wx.ID_YES:
+            self.OnSaveOrSaveAs(wx.CommandEvent(wx.ID_SAVE))
             # If save was successful, modified flag is unset
             if not self.modified: return True
             # If save was successful, modified flag is unset
             if not self.modified: return True
-        elif say == wxID_NO:
-            self.modified = False
+        elif say == wx.ID_NO:
+            self.SetModified(False)
             panel.SetModified(False)
             return True
         return False
             panel.SetModified(False)
             return True
         return False
@@ -904,40 +1231,121 @@ Homepage: http://xrced.sourceforge.net\
 
 ################################################################################
 
 
 ################################################################################
 
+class PythonOptions(wx.Dialog):
+
+    def __init__(self, parent, cfg, dataFile):
+        pre = wx.PreDialog()
+        g.frame.res.LoadOnDialog(pre, parent, "PYTHON_OPTIONS")
+        self.PostCreate(pre)
+
+        self.cfg = cfg
+        self.dataFile = dataFile
+
+        self.AutoGenerateCB = xrc.XRCCTRL(self, "AutoGenerateCB")
+        self.EmbedCB = xrc.XRCCTRL(self, "EmbedCB")
+        self.GettextCB = xrc.XRCCTRL(self, "GettextCB")
+        self.MakeXRSFileCB = xrc.XRCCTRL(self, "MakeXRSFileCB")
+        self.FileNameTC = xrc.XRCCTRL(self, "FileNameTC")
+        self.BrowseBtn = xrc.XRCCTRL(self, "BrowseBtn")
+        self.GenerateBtn = xrc.XRCCTRL(self, "GenerateBtn")
+        self.SaveOptsBtn = xrc.XRCCTRL(self, "SaveOptsBtn")
+
+        self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.BrowseBtn)
+        self.Bind(wx.EVT_BUTTON, self.OnGenerate, self.GenerateBtn)
+        self.Bind(wx.EVT_BUTTON, self.OnSaveOpts, self.SaveOptsBtn)
+
+        if self.cfg.Read("filename", "") != "":
+            self.FileNameTC.SetValue(self.cfg.Read("filename"))
+        else:
+            name = os.path.splitext(dataFile)[0]
+            name += '_xrc.py'
+            self.FileNameTC.SetValue(name)
+        self.AutoGenerateCB.SetValue(self.cfg.ReadBool("autogenerate", False))
+        self.EmbedCB.SetValue(self.cfg.ReadBool("embedResource", False))
+        self.MakeXRSFileCB.SetValue(self.cfg.ReadBool("makeXRS", False))
+        self.GettextCB.SetValue(self.cfg.ReadBool("genGettext", False))
+        
+                  
+    def OnBrowse(self, evt):
+        path = self.FileNameTC.GetValue()
+        dirname = os.path.abspath(os.path.dirname(path))
+        name = os.path.split(path)[1]
+        dlg = wx.FileDialog(self, 'Save As', dirname, name, '*.py',
+                               wx.SAVE | wx.OVERWRITE_PROMPT)
+        if dlg.ShowModal() == wx.ID_OK:
+            path = dlg.GetPath()
+            self.FileNameTC.SetValue(path)
+        dlg.Destroy()
+    
+
+    def OnGenerate(self, evt):
+        pypath = self.FileNameTC.GetValue()
+        embed = self.EmbedCB.GetValue()
+        genGettext = self.GettextCB.GetValue()
+        frame.GeneratePython(self.dataFile, pypath, embed, genGettext)
+        self.OnSaveOpts()
+
+    
+    def OnSaveOpts(self, evt=None):
+        self.cfg.Write("filename", self.FileNameTC.GetValue())
+        self.cfg.WriteBool("autogenerate", self.AutoGenerateCB.GetValue())
+        self.cfg.WriteBool("embedResource", self.EmbedCB.GetValue())
+        self.cfg.WriteBool("makeXRS", self.MakeXRSFileCB.GetValue())
+        self.cfg.WriteBool("genGettext", self.GettextCB.GetValue())
+
+        self.EndModal(wx.ID_OK)
+    
+        
+################################################################################
+
 def usage():
     print >> sys.stderr, 'usage: xrced [-dhiv] [file]'
 
 def usage():
     print >> sys.stderr, 'usage: xrced [-dhiv] [file]'
 
-class App(wxApp):
+class App(wx.App):
     def OnInit(self):
     def OnInit(self):
+        # Check version
+        if wx.VERSION[:3] < MinWxVersion:
+            wx.LogWarning('''\
+This version of XRCed may not work correctly on your version of wxWidgets. \
+Please upgrade wxWidgets to %d.%d.%d or higher.''' % MinWxVersion)
         global debug
         # Process comand-line
         global debug
         # Process comand-line
+        opts = args = None
         try:
             opts, args = getopt.getopt(sys.argv[1:], 'dhiv')
         try:
             opts, args = getopt.getopt(sys.argv[1:], 'dhiv')
+            for o,a in opts:
+                if o == '-h':
+                    usage()
+                    sys.exit(0)
+                elif o == '-d':
+                    debug = True
+                elif o == '-v':
+                    print 'XRCed version', version
+                    sys.exit(0)
+            
         except getopt.GetoptError:
         except getopt.GetoptError:
-            if wxPlatform != '__WXMAC__': # macs have some extra parameters
+            if wx.Platform != '__WXMAC__': # macs have some extra parameters
                 print >> sys.stderr, 'Unknown option'
                 usage()
                 sys.exit(1)
                 print >> sys.stderr, 'Unknown option'
                 usage()
                 sys.exit(1)
-        for o,a in opts:
-            if o == '-h':
-                usage()
-                sys.exit(0)
-            elif o == '-d':
-                debug = True
-            elif o == '-v':
-                print 'XRCed version', version
-                sys.exit(0)
 
         self.SetAppName('xrced')
         # Settings
         global conf
 
         self.SetAppName('xrced')
         # Settings
         global conf
-        conf = g.conf = wxConfig(style = wxCONFIG_USE_LOCAL_FILE)
+        conf = g.conf = wx.Config(style = wx.CONFIG_USE_LOCAL_FILE)
+        conf.localconf = None
         conf.autoRefresh = conf.ReadInt('autorefresh', True)
         pos = conf.ReadInt('x', -1), conf.ReadInt('y', -1)
         size = conf.ReadInt('width', 800), conf.ReadInt('height', 600)
         conf.embedPanel = conf.ReadInt('embedPanel', True)
         conf.showTools = conf.ReadInt('showTools', True)
         conf.sashPos = conf.ReadInt('sashPos', 200)
         conf.autoRefresh = conf.ReadInt('autorefresh', True)
         pos = conf.ReadInt('x', -1), conf.ReadInt('y', -1)
         size = conf.ReadInt('width', 800), conf.ReadInt('height', 600)
         conf.embedPanel = conf.ReadInt('embedPanel', True)
         conf.showTools = conf.ReadInt('showTools', True)
         conf.sashPos = conf.ReadInt('sashPos', 200)
+        # read recently used files
+        recentfiles=conf.Read('recentFiles','')
+        conf.recentfiles={}
+        if recentfiles:
+            for fil in recentfiles.split('|'):
+                conf.recentfiles[wx.NewId()]=fil
         if not conf.embedPanel:
             conf.panelX = conf.ReadInt('panelX', -1)
             conf.panelY = conf.ReadInt('panelY', -1)
         if not conf.embedPanel:
             conf.panelX = conf.ReadInt('panelX', -1)
             conf.panelY = conf.ReadInt('panelY', -1)
@@ -947,14 +1355,10 @@ class App(wxApp):
         conf.panelHeight = conf.ReadInt('panelHeight', 200)
         conf.panic = not conf.HasEntry('nopanic')
         # Add handlers
         conf.panelHeight = conf.ReadInt('panelHeight', 200)
         conf.panic = not conf.HasEntry('nopanic')
         # Add handlers
-        wxFileSystem_AddHandler(wxMemoryFSHandler())
-        wxInitAllImageHandlers()
+        wx.FileSystem.AddHandler(wx.MemoryFSHandler())
         # Create main frame
         frame = Frame(pos, size)
         frame.Show(True)
         # Create main frame
         frame = Frame(pos, size)
         frame.Show(True)
-        # Load resources from XRC file (!!! should be transformed to .py later?)
-        frame.res = wxXmlResource('')
-        frame.res.Load(os.path.join(basePath, 'xrced.xrc'))
 
         # Load file after showing
         if args:
 
         # Load file after showing
         if args:
@@ -966,7 +1370,7 @@ class App(wxApp):
     def OnExit(self):
         # Write config
         global conf
     def OnExit(self):
         # Write config
         global conf
-        wc = wxConfigBase_Get()
+        wc = conf
         wc.WriteInt('autorefresh', conf.autoRefresh)
         wc.WriteInt('x', conf.x)
         wc.WriteInt('y', conf.y)
         wc.WriteInt('autorefresh', conf.autoRefresh)
         wc.WriteInt('x', conf.x)
         wc.WriteInt('y', conf.y)
@@ -981,10 +1385,12 @@ class App(wxApp):
         wc.WriteInt('panelWidth', conf.panelWidth)
         wc.WriteInt('panelHeight', conf.panelHeight)
         wc.WriteInt('nopanic', True)
         wc.WriteInt('panelWidth', conf.panelWidth)
         wc.WriteInt('panelHeight', conf.panelHeight)
         wc.WriteInt('nopanic', True)
+        wc.Write('recentFiles', '|'.join(conf.recentfiles.values()[-5:]))
         wc.Flush()
 
 def main():
     app = App(0, useBestVisual=False)
         wc.Flush()
 
 def main():
     app = App(0, useBestVisual=False)
+    #app.SetAssertMode(wx.PYAPP_ASSERT_LOG)
     app.MainLoop()
     app.OnExit()
     global conf
     app.MainLoop()
     app.OnExit()
     global conf