📄 xrced.py
字号:
# Name: xrced.py# Purpose: XRC editor, main module# Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be># Created: 20.08.2001# RCS-ID: $Id: xrced.py 47172 2007-07-05 21:51:46Z ROL $"""xrced -- Simple resource editor for XRC format used by wxWidgets/wxPython GUI toolkit.Usage: xrced [ -h ] [ -v ] [ XRC-file ]Options: -h output short usage info and exit -v output version info and exit"""from globals import *import os, sys, getopt, re, traceback, tempfile, shutil, cPicklefrom xml.parsers import expat# Local modulesfrom tree import * # imports xxx which imports paramsfrom panel import *from tools import *from params import genericStyles# Cleanup recursive import sideeffects, otherwise we can't create undoManimport undoundo.ParamPage = ParamPageundoMan = g.undoMan = UndoManager()# Set application path for loading resourcesif __name__ == '__main__': basePath = os.path.dirname(sys.argv[0])else: basePath = os.path.dirname(__file__)# Remember system pathsys_path = sys.path# 1 adds CMD command to Help menudebug = 0g.helpText = """\<HTML><H2>Welcome to XRC<font color="blue">ed</font></H2><H3><font color="green">DON'T PANIC :)</font></H3>Read this note before clicking on anything!<P>To start select tree root, then popup menu with your right mouse button,select "Append Child", and then any command.<P>Or just press one of the buttons on the tools palette.<P>Enter XML ID, change properties, create children.<P>To test your interface select Test command (View menu).<P>Consult README.txt file for the details.</HTML>"""defaultIDs = {xxxPanel:'PANEL', xxxDialog:'DIALOG', xxxFrame:'FRAME', xxxMenuBar:'MENUBAR', xxxMenu:'MENU', xxxToolBar:'TOOLBAR', xxxWizard:'WIZARD', xxxBitmap:'BITMAP', xxxIcon:'ICON'}defaultName = 'UNTITLED.xrc'################################################################################# ScrolledMessageDialog - modified from wxPython lib to set fixed-width fontclass 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) w, h = dc.GetFullTextExtent(' ', g.modernFont())[:2] ok = wx.Button(self, wx.ID_OK, "OK") ok.SetDefault() 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) # scroll to the first line ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!35', (self,))) self.SetAutoLayout(True) self.Fit() self.CenterOnScreen(wx.BOTH)################################################################################# Event handler for using during locationclass Locator(wx.EvtHandler): def ProcessEvent(self, evt): print evtclass TaskBarIcon(wx.TaskBarIcon): def __init__(self, frame): wx.TaskBarIcon.__init__(self) self.frame = frame # Set the image self.SetIcon(images.getIconIcon(), "XRCed")# ArtProvider for toolbar iconsclass ToolArtProvider(wx.ArtProvider): def __init__(self): wx.ArtProvider.__init__(self) self.images = { 'ART_LOCATE': images.getLocateImage(), 'ART_TEST': images.getTestImage(), 'ART_REFRESH': images.getRefreshImage(), 'ART_AUTO_REFRESH': images.getAutoRefreshImage(), 'ART_MOVEUP': images.getMoveUpImage(), 'ART_MOVEDOWN': images.getMoveDownImage(), 'ART_MOVELEFT': images.getMoveLeftImage(), 'ART_MOVERIGHT': images.getMoveRightImage() } if wx.Platform in ['__WXMAC__', '__WXMSW__']: self.images.update({ wx.ART_NORMAL_FILE: images.getNewImage(), wx.ART_FILE_OPEN: images.getOpenImage(), wx.ART_FILE_SAVE: images.getSaveImage(), wx.ART_UNDO: images.getUndoImage(), wx.ART_REDO: images.getRedoImage(), wx.ART_CUT: images.getCutImage(), wx.ART_COPY: images.getCopyImage(), wx.ART_PASTE: images.getPasteImage() }) def CreateBitmap(self, id, client, size): bmp = wx.NullBitmap if id in self.images: img = self.images[id] # Alpha not implemented completely there if wx.Platform in ['__WXMAC__', '__WXMSW__']: img.ConvertAlphaToMask() bmp = wx.BitmapFromImage(img) return bmpclass Frame(wx.Frame): def __init__(self, pos, size): wx.Frame.__init__(self, None, -1, '', pos, size) global frame frame = g.frame = self bar = self.CreateStatusBar(2) bar.SetStatusWidths([-1, 40]) self.SetIcon(images.getIconIcon()) try: self.tbicon = TaskBarIcon(self) except: self.tbicon = None # Idle flag self.inIdle = False # Load our own resources self.res = xrc.EmptyXmlResource() # !!! 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 menuBar = wx.MenuBar() 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() g.fileHistory.UseMenu(self.recentMenu) g.fileHistory.AddFilesToMenu() self.Bind(wx.EVT_MENU, self.OnRecentFile, id=wx.ID_FILE1, id2=wx.ID_FILE9) menu.AppendMenu(-1, 'Open &Recent', self.recentMenu, 'Open a recent file') menu.AppendSeparator() 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() self.ID_PREFS = wx.NewId() menu.Append(self.ID_PREFS, 'Preferences...', 'Change XRCed settings') menu.AppendSeparator() menu.Append(wx.ID_EXIT, '&Quit\tCtrl-Q', 'Exit application') menuBar.Append(menu, '&File') menu = wx.Menu() menu.Append(wx.ID_UNDO, '&Undo\tCtrl-Z', 'Undo') menu.Append(wx.ID_REDO, '&Redo\tCtrl-Y', 'Redo') menu.AppendSeparator() 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.AppendSeparator() self.ID_LOCATE = wx.NewId() self.ID_TOOL_LOCATE = wx.NewId() self.ART_LOCATE = 'ART_LOCATE' 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') 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) 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() self.ID_TEST = wx.NewId() self.ART_TEST = 'ART_TEST' menu.Append(self.ID_TEST, '&Test\tF5', 'Show test window') self.ID_REFRESH = wx.NewId() self.ART_REFRESH = 'ART_REFRESH' menu.Append(self.ID_REFRESH, '&Refresh\tCtrl-R', 'Refresh test window') self.ID_AUTO_REFRESH = wx.NewId() self.ART_AUTO_REFRESH = 'ART_AUTO_REFRESH' menu.Append(self.ID_AUTO_REFRESH, '&Auto-refresh\tAlt-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\tF6', 'Close test window') menuBar.Append(menu, '&View') menu = wx.Menu() self.ID_MOVEUP = wx.NewId() self.ART_MOVEUP = 'ART_MOVEUP' menu.Append(self.ID_MOVEUP, '&Up', 'Move before previous sibling') self.ID_MOVEDOWN = wx.NewId() self.ART_MOVEDOWN = 'ART_MOVEDOWN' menu.Append(self.ID_MOVEDOWN, '&Down', 'Move after next sibling') self.ID_MOVELEFT = wx.NewId() self.ART_MOVELEFT = 'ART_MOVELEFT' menu.Append(self.ID_MOVELEFT, '&Make sibling', 'Make sibling of parent') self.ID_MOVERIGHT = wx.NewId() self.ART_MOVERIGHT = 'ART_MOVERIGHT' menu.Append(self.ID_MOVERIGHT, '&Make child', 'Make child of previous sibling') menuBar.Append(menu, '&Move') menu = wx.Menu() menu.Append(wx.ID_ABOUT, '&About...', 'About XCRed') self.ID_README = wx.NewId() menu.Append(self.ID_README, '&Readme...\tF1', 'View the README file') if debug: self.ID_DEBUG_CMD = wx.NewId() menu.Append(self.ID_DEBUG_CMD, 'CMD', 'Python command line') wx.EVT_MENU(self, self.ID_DEBUG_CMD, self.OnDebugCMD) menuBar.Append(menu, '&Help') self.menuBar = menuBar self.SetMenuBar(menuBar) # Create toolbar tb = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT) if wx.Platform != '__WXMAC__': # Redefine AddSeparator on wxGTK and wxMSW to add vertical line def _AddSeparator(): tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) tb.AddSeparator = _AddSeparator # Use tango icons and slightly wider bitmap size on Mac if wx.Platform in ['__WXMAC__', '__WXMSW__']: tb.SetToolBitmapSize((26,26)) else: 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.AddSeparator() tb.AddSimpleTool(wx.ID_UNDO, undo_bmp, 'Undo', 'Undo') tb.AddSimpleTool(wx.ID_REDO, redo_bmp, 'Redo', 'Redo') tb.AddSeparator() 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.AddSeparator() bmp = wx.ArtProvider.GetBitmap(self.ART_LOCATE, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_TOOL_LOCATE, bmp, 'Locate', 'Locate control in test window and select it', True) bmp = wx.ArtProvider.GetBitmap(self.ART_TEST, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_TEST, bmp, 'Test', 'Test window') bmp = wx.ArtProvider.GetBitmap(self.ART_REFRESH, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_REFRESH, bmp, 'Refresh', 'Refresh view') bmp = wx.ArtProvider.GetBitmap(self.ART_AUTO_REFRESH, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_AUTO_REFRESH, bmp, 'Auto-refresh', 'Toggle auto-refresh mode', True) tb.AddSeparator() bmp = wx.ArtProvider.GetBitmap(self.ART_MOVEUP, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_MOVEUP, bmp, 'Up', 'Move before previous sibling') bmp = wx.ArtProvider.GetBitmap(self.ART_MOVEDOWN, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_MOVEDOWN, bmp, 'Down', 'Move after next sibling') bmp = wx.ArtProvider.GetBitmap(self.ART_MOVELEFT, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_MOVELEFT, bmp, 'Make Sibling', 'Make sibling of parent') bmp = wx.ArtProvider.GetBitmap(self.ART_MOVERIGHT, wx.ART_TOOLBAR) tb.AddSimpleTool(self.ID_MOVERIGHT, bmp, 'Make Child', 'Make child of previous sibling') 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 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -