]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | # Name: xrced.py |
2 | # Purpose: XRC editor, main module | |
3 | # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be> | |
4 | # Created: 20.08.2001 | |
5 | # RCS-ID: $Id$ | |
1fded56b | 6 | |
d14a1e28 | 7 | """ |
1fded56b | 8 | |
29a41103 | 9 | xrced -- Simple resource editor for XRC format used by wxWidgets/wxPython |
d14a1e28 RD |
10 | GUI toolkit. |
11 | ||
12 | Usage: | |
13 | ||
14 | xrced [ -h ] [ -v ] [ XRC-file ] | |
15 | ||
16 | Options: | |
17 | ||
18 | -h output short usage info and exit | |
19 | ||
20 | -v output version info and exit | |
21 | """ | |
22 | ||
d14a1e28 | 23 | from globals import * |
af52e185 | 24 | import os, sys, getopt, re, traceback, tempfile, shutil, cPickle |
28e65e0f | 25 | from xml.parsers import expat |
d14a1e28 RD |
26 | |
27 | # Local modules | |
28 | from tree import * # imports xxx which imports params | |
29 | from panel import * | |
30 | from tools import * | |
75aa1946 | 31 | from params import genericStyles |
d14a1e28 RD |
32 | # Cleanup recursive import sideeffects, otherwise we can't create undoMan |
33 | import undo | |
34 | undo.ParamPage = ParamPage | |
35 | undoMan = g.undoMan = UndoManager() | |
36 | ||
37 | # Set application path for loading resources | |
38 | if __name__ == '__main__': | |
39 | basePath = os.path.dirname(sys.argv[0]) | |
40 | else: | |
41 | basePath = os.path.dirname(__file__) | |
42 | ||
c0d5ae74 RR |
43 | # Remember system path |
44 | sys_path = sys.path | |
45 | ||
d14a1e28 RD |
46 | # 1 adds CMD command to Help menu |
47 | debug = 0 | |
48 | ||
49 | g.helpText = """\ | |
50 | <HTML><H2>Welcome to XRC<font color="blue">ed</font></H2><H3><font color="green">DON'T PANIC :)</font></H3> | |
51 | Read this note before clicking on anything!<P> | |
52 | To start select tree root, then popup menu with your right mouse button, | |
53 | select "Append Child", and then any command.<P> | |
54 | Or just press one of the buttons on the tools palette.<P> | |
55 | Enter XML ID, change properties, create children.<P> | |
56 | To test your interface select Test command (View menu).<P> | |
c0d5ae74 | 57 | Consult README.txt file for the details.</HTML> |
d14a1e28 RD |
58 | """ |
59 | ||
60 | defaultIDs = {xxxPanel:'PANEL', xxxDialog:'DIALOG', xxxFrame:'FRAME', | |
64bce500 | 61 | xxxMenuBar:'MENUBAR', xxxMenu:'MENU', xxxToolBar:'TOOLBAR', |
306b6fe9 | 62 | xxxWizard:'WIZARD', xxxBitmap:'BITMAP', xxxIcon:'ICON'} |
d14a1e28 | 63 | |
6cb85701 RR |
64 | defaultName = 'UNTITLED.xrc' |
65 | ||
d14a1e28 RD |
66 | ################################################################################ |
67 | ||
68 | # ScrolledMessageDialog - modified from wxPython lib to set fixed-width font | |
29a41103 RD |
69 | class ScrolledMessageDialog(wx.Dialog): |
70 | def __init__(self, parent, msg, caption, pos = wx.DefaultPosition, size = (500,300)): | |
71 | from wx.lib.layoutf import Layoutf | |
72 | wx.Dialog.__init__(self, parent, -1, caption, pos, size) | |
73 | text = wx.TextCtrl(self, -1, msg, wx.DefaultPosition, | |
74 | wx.DefaultSize, wx.TE_MULTILINE | wx.TE_READONLY) | |
289128a4 | 75 | text.SetFont(g.modernFont()) |
29a41103 | 76 | dc = wx.WindowDC(text) |
289128a4 | 77 | w, h = dc.GetFullTextExtent(' ', g.modernFont())[:2] |
29a41103 | 78 | ok = wx.Button(self, wx.ID_OK, "OK") |
c0d5ae74 | 79 | ok.SetDefault() |
d14a1e28 RD |
80 | text.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok))) |
81 | text.SetSize((w * 80 + 30, h * 40)) | |
c0d5ae74 RR |
82 | text.ShowPosition(1) # scroll to the first line |
83 | ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!35', (self,))) | |
d14a1e28 RD |
84 | self.SetAutoLayout(True) |
85 | self.Fit() | |
29a41103 | 86 | self.CenterOnScreen(wx.BOTH) |
d14a1e28 RD |
87 | |
88 | ################################################################################ | |
89 | ||
fd919451 | 90 | # Event handler for using during location |
29a41103 | 91 | class Locator(wx.EvtHandler): |
fd919451 RR |
92 | def ProcessEvent(self, evt): |
93 | print evt | |
94 | ||
29a41103 | 95 | class Frame(wx.Frame): |
d14a1e28 | 96 | def __init__(self, pos, size): |
29a41103 | 97 | wx.Frame.__init__(self, None, -1, '', pos, size) |
d14a1e28 RD |
98 | global frame |
99 | frame = g.frame = self | |
100 | bar = self.CreateStatusBar(2) | |
101 | bar.SetStatusWidths([-1, 40]) | |
102 | self.SetIcon(images.getIconIcon()) | |
103 | ||
104 | # Idle flag | |
105 | self.inIdle = False | |
106 | ||
64bce500 | 107 | # Load our own resources |
37867b24 | 108 | self.res = xrc.EmptyXmlResource() |
d6922577 | 109 | # !!! Blocking of assert failure occurring in older unicode builds |
64bce500 | 110 | try: |
f65bb0f8 | 111 | quietlog = wx.LogNull() |
64bce500 RR |
112 | self.res.Load(os.path.join(basePath, 'xrced.xrc')) |
113 | except wx._core.PyAssertionError: | |
114 | print 'PyAssertionError was ignored' | |
115 | ||
d14a1e28 | 116 | # Make menus |
29a41103 | 117 | menuBar = wx.MenuBar() |
d14a1e28 | 118 | |
29a41103 RD |
119 | menu = wx.Menu() |
120 | menu.Append(wx.ID_NEW, '&New\tCtrl-N', 'New file') | |
80389ff7 | 121 | menu.AppendSeparator() |
29a41103 | 122 | menu.Append(wx.ID_OPEN, '&Open...\tCtrl-O', 'Open XRC file') |
52cd0643 | 123 | |
29a41103 | 124 | self.recentMenu = wx.Menu() |
52cd0643 RD |
125 | g.fileHistory.UseMenu(self.recentMenu) |
126 | g.fileHistory.AddFilesToMenu() | |
127 | self.Bind(wx.EVT_MENU, self.OnRecentFile, id=wx.ID_FILE1, id2=wx.ID_FILE9) | |
128 | menu.AppendMenu(-1, 'Open &Recent', self.recentMenu, 'Open a recent file') | |
129 | ||
80389ff7 | 130 | menu.AppendSeparator() |
29a41103 RD |
131 | menu.Append(wx.ID_SAVE, '&Save\tCtrl-S', 'Save XRC file') |
132 | menu.Append(wx.ID_SAVEAS, 'Save &As...', 'Save XRC file under different name') | |
133 | self.ID_GENERATE_PYTHON = wx.NewId() | |
73b2a9a7 RD |
134 | menu.Append(self.ID_GENERATE_PYTHON, '&Generate Python...', |
135 | 'Generate a Python module that uses this XRC') | |
d14a1e28 | 136 | menu.AppendSeparator() |
5050b626 RR |
137 | self.ID_PREFS = wx.NewId() |
138 | menu.Append(self.ID_PREFS, 'Preferences...', 'Change XRCed settings') | |
139 | menu.AppendSeparator() | |
29a41103 | 140 | menu.Append(wx.ID_EXIT, '&Quit\tCtrl-Q', 'Exit application') |
80389ff7 | 141 | |
d14a1e28 RD |
142 | menuBar.Append(menu, '&File') |
143 | ||
29a41103 RD |
144 | menu = wx.Menu() |
145 | menu.Append(wx.ID_UNDO, '&Undo\tCtrl-Z', 'Undo') | |
146 | menu.Append(wx.ID_REDO, '&Redo\tCtrl-Y', 'Redo') | |
d14a1e28 | 147 | menu.AppendSeparator() |
29a41103 RD |
148 | menu.Append(wx.ID_CUT, 'Cut\tCtrl-X', 'Cut to the clipboard') |
149 | menu.Append(wx.ID_COPY, '&Copy\tCtrl-C', 'Copy to the clipboard') | |
150 | menu.Append(wx.ID_PASTE, '&Paste\tCtrl-V', 'Paste from the clipboard') | |
151 | self.ID_DELETE = wx.NewId() | |
d14a1e28 | 152 | menu.Append(self.ID_DELETE, '&Delete\tCtrl-D', 'Delete object') |
64bce500 | 153 | menu.AppendSeparator() |
29a41103 RD |
154 | self.ID_LOCATE = wx.NewId() |
155 | self.ID_TOOL_LOCATE = wx.NewId() | |
156 | self.ID_TOOL_PASTE = wx.NewId() | |
fd919451 | 157 | menu.Append(self.ID_LOCATE, '&Locate\tCtrl-L', 'Locate control in test window and select it') |
d14a1e28 RD |
158 | menuBar.Append(menu, '&Edit') |
159 | ||
29a41103 RD |
160 | menu = wx.Menu() |
161 | self.ID_EMBED_PANEL = wx.NewId() | |
d14a1e28 RD |
162 | menu.Append(self.ID_EMBED_PANEL, '&Embed Panel', |
163 | 'Toggle embedding properties panel in the main window', True) | |
164 | menu.Check(self.ID_EMBED_PANEL, conf.embedPanel) | |
29a41103 | 165 | self.ID_SHOW_TOOLS = wx.NewId() |
d14a1e28 RD |
166 | menu.Append(self.ID_SHOW_TOOLS, 'Show &Tools', 'Toggle tools', True) |
167 | menu.Check(self.ID_SHOW_TOOLS, conf.showTools) | |
168 | menu.AppendSeparator() | |
29a41103 | 169 | self.ID_TEST = wx.NewId() |
fd919451 | 170 | menu.Append(self.ID_TEST, '&Test\tF5', 'Show test window') |
29a41103 | 171 | self.ID_REFRESH = wx.NewId() |
d14a1e28 | 172 | menu.Append(self.ID_REFRESH, '&Refresh\tCtrl-R', 'Refresh test window') |
29a41103 | 173 | self.ID_AUTO_REFRESH = wx.NewId() |
5050b626 | 174 | menu.Append(self.ID_AUTO_REFRESH, '&Auto-refresh\tAlt-A', |
d14a1e28 RD |
175 | 'Toggle auto-refresh mode', True) |
176 | menu.Check(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
29a41103 | 177 | self.ID_TEST_HIDE = wx.NewId() |
4ecdf8fc | 178 | menu.Append(self.ID_TEST_HIDE, '&Hide\tF6', 'Close test window') |
d14a1e28 RD |
179 | menuBar.Append(menu, '&View') |
180 | ||
75aa1946 RR |
181 | menu = wx.Menu() |
182 | self.ID_MOVEUP = wx.NewId() | |
183 | menu.Append(self.ID_MOVEUP, '&Up', 'Move before previous sibling') | |
184 | self.ID_MOVEDOWN = wx.NewId() | |
185 | menu.Append(self.ID_MOVEDOWN, '&Down', 'Move after next sibling') | |
186 | self.ID_MOVELEFT = wx.NewId() | |
187 | menu.Append(self.ID_MOVELEFT, '&Make sibling', 'Make sibling of parent') | |
188 | self.ID_MOVERIGHT = wx.NewId() | |
189 | menu.Append(self.ID_MOVERIGHT, '&Make child', 'Make child of previous sibling') | |
190 | menuBar.Append(menu, '&Move') | |
191 | ||
29a41103 RD |
192 | menu = wx.Menu() |
193 | menu.Append(wx.ID_ABOUT, '&About...', 'About XCRed') | |
194 | self.ID_README = wx.NewId() | |
c0d5ae74 | 195 | menu.Append(self.ID_README, '&Readme...\tF1', 'View the README file') |
d14a1e28 | 196 | if debug: |
29a41103 | 197 | self.ID_DEBUG_CMD = wx.NewId() |
d14a1e28 | 198 | menu.Append(self.ID_DEBUG_CMD, 'CMD', 'Python command line') |
29a41103 | 199 | wx.EVT_MENU(self, self.ID_DEBUG_CMD, self.OnDebugCMD) |
d14a1e28 RD |
200 | menuBar.Append(menu, '&Help') |
201 | ||
202 | self.menuBar = menuBar | |
203 | self.SetMenuBar(menuBar) | |
204 | ||
205 | # Create toolbar | |
29a41103 | 206 | tb = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT) |
5c3a23e1 | 207 | tb.SetToolBitmapSize((24,24)) |
6ac94a48 | 208 | new_bmp = wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_TOOLBAR) |
5c3a23e1 RD |
209 | open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR) |
210 | save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR) | |
211 | undo_bmp = wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR) | |
212 | redo_bmp = wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR) | |
213 | cut_bmp = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) | |
214 | copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR) | |
215 | paste_bmp= wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR) | |
6c75a4cf | 216 | |
29a41103 RD |
217 | tb.AddSimpleTool(wx.ID_NEW, new_bmp, 'New', 'New file') |
218 | tb.AddSimpleTool(wx.ID_OPEN, open_bmp, 'Open', 'Open file') | |
219 | tb.AddSimpleTool(wx.ID_SAVE, save_bmp, 'Save', 'Save file') | |
220 | tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) | |
221 | tb.AddSimpleTool(wx.ID_UNDO, undo_bmp, 'Undo', 'Undo') | |
222 | tb.AddSimpleTool(wx.ID_REDO, redo_bmp, 'Redo', 'Redo') | |
223 | tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) | |
224 | tb.AddSimpleTool(wx.ID_CUT, cut_bmp, 'Cut', 'Cut') | |
225 | tb.AddSimpleTool(wx.ID_COPY, copy_bmp, 'Copy', 'Copy') | |
6c75a4cf | 226 | tb.AddSimpleTool(self.ID_TOOL_PASTE, paste_bmp, 'Paste', 'Paste') |
29a41103 | 227 | tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) |
9a69d0aa RR |
228 | tb.AddSimpleTool(self.ID_TOOL_LOCATE, |
229 | images.getLocateBitmap(), #images.getLocateArmedBitmap(), | |
230 | 'Locate', 'Locate control in test window and select it', True) | |
29a41103 | 231 | tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) |
d14a1e28 RD |
232 | tb.AddSimpleTool(self.ID_TEST, images.getTestBitmap(), 'Test', 'Test window') |
233 | tb.AddSimpleTool(self.ID_REFRESH, images.getRefreshBitmap(), | |
234 | 'Refresh', 'Refresh view') | |
235 | tb.AddSimpleTool(self.ID_AUTO_REFRESH, images.getAutoRefreshBitmap(), | |
236 | 'Auto-refresh', 'Toggle auto-refresh mode', True) | |
75aa1946 RR |
237 | tb.AddControl(wx.StaticLine(tb, -1, size=(-1,23), style=wx.LI_VERTICAL)) |
238 | tb.AddSimpleTool(self.ID_MOVEUP, images.getToolMoveUpBitmap(), | |
239 | 'Up', 'Move before previous sibling') | |
240 | tb.AddSimpleTool(self.ID_MOVEDOWN, images.getToolMoveDownBitmap(), | |
241 | 'Down', 'Move after next sibling') | |
242 | tb.AddSimpleTool(self.ID_MOVELEFT, images.getToolMoveLeftBitmap(), | |
243 | 'Make Sibling', 'Make sibling of parent') | |
244 | tb.AddSimpleTool(self.ID_MOVERIGHT, images.getToolMoveRightBitmap(), | |
245 | 'Make Child', 'Make child of previous sibling') | |
29a41103 | 246 | # if wx.Platform == '__WXGTK__': |
306b6fe9 | 247 | # tb.AddSeparator() # otherwise auto-refresh sticks in status line |
d14a1e28 RD |
248 | tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh) |
249 | tb.Realize() | |
64bce500 | 250 | |
d14a1e28 RD |
251 | self.tb = tb |
252 | self.minWidth = tb.GetSize()[0] # minimal width is the size of toolbar | |
253 | ||
254 | # File | |
29a41103 RD |
255 | wx.EVT_MENU(self, wx.ID_NEW, self.OnNew) |
256 | wx.EVT_MENU(self, wx.ID_OPEN, self.OnOpen) | |
257 | wx.EVT_MENU(self, wx.ID_SAVE, self.OnSaveOrSaveAs) | |
258 | wx.EVT_MENU(self, wx.ID_SAVEAS, self.OnSaveOrSaveAs) | |
259 | wx.EVT_MENU(self, self.ID_GENERATE_PYTHON, self.OnGeneratePython) | |
5050b626 | 260 | wx.EVT_MENU(self, self.ID_PREFS, self.OnPrefs) |
29a41103 | 261 | wx.EVT_MENU(self, wx.ID_EXIT, self.OnExit) |
d14a1e28 | 262 | # Edit |
29a41103 RD |
263 | wx.EVT_MENU(self, wx.ID_UNDO, self.OnUndo) |
264 | wx.EVT_MENU(self, wx.ID_REDO, self.OnRedo) | |
265 | wx.EVT_MENU(self, wx.ID_CUT, self.OnCutDelete) | |
266 | wx.EVT_MENU(self, wx.ID_COPY, self.OnCopy) | |
267 | wx.EVT_MENU(self, wx.ID_PASTE, self.OnPaste) | |
268 | wx.EVT_MENU(self, self.ID_TOOL_PASTE, self.OnPaste) | |
269 | wx.EVT_MENU(self, self.ID_DELETE, self.OnCutDelete) | |
270 | wx.EVT_MENU(self, self.ID_LOCATE, self.OnLocate) | |
271 | wx.EVT_MENU(self, self.ID_TOOL_LOCATE, self.OnLocate) | |
d14a1e28 | 272 | # View |
29a41103 RD |
273 | wx.EVT_MENU(self, self.ID_EMBED_PANEL, self.OnEmbedPanel) |
274 | wx.EVT_MENU(self, self.ID_SHOW_TOOLS, self.OnShowTools) | |
275 | wx.EVT_MENU(self, self.ID_TEST, self.OnTest) | |
276 | wx.EVT_MENU(self, self.ID_REFRESH, self.OnRefresh) | |
277 | wx.EVT_MENU(self, self.ID_AUTO_REFRESH, self.OnAutoRefresh) | |
278 | wx.EVT_MENU(self, self.ID_TEST_HIDE, self.OnTestHide) | |
75aa1946 RR |
279 | # Move |
280 | wx.EVT_MENU(self, self.ID_MOVEUP, self.OnMoveUp) | |
281 | wx.EVT_MENU(self, self.ID_MOVEDOWN, self.OnMoveDown) | |
282 | wx.EVT_MENU(self, self.ID_MOVELEFT, self.OnMoveLeft) | |
283 | wx.EVT_MENU(self, self.ID_MOVERIGHT, self.OnMoveRight) | |
d14a1e28 | 284 | # Help |
29a41103 RD |
285 | wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout) |
286 | wx.EVT_MENU(self, self.ID_README, self.OnReadme) | |
d14a1e28 RD |
287 | |
288 | # Update events | |
29a41103 RD |
289 | wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.OnUpdateUI) |
290 | wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.OnUpdateUI) | |
291 | wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.OnUpdateUI) | |
292 | wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.OnUpdateUI) | |
293 | wx.EVT_UPDATE_UI(self, self.ID_LOCATE, self.OnUpdateUI) | |
294 | wx.EVT_UPDATE_UI(self, self.ID_TOOL_LOCATE, self.OnUpdateUI) | |
295 | wx.EVT_UPDATE_UI(self, self.ID_TOOL_PASTE, self.OnUpdateUI) | |
296 | wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.OnUpdateUI) | |
297 | wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.OnUpdateUI) | |
298 | wx.EVT_UPDATE_UI(self, self.ID_DELETE, self.OnUpdateUI) | |
299 | wx.EVT_UPDATE_UI(self, self.ID_TEST, self.OnUpdateUI) | |
300 | wx.EVT_UPDATE_UI(self, self.ID_REFRESH, self.OnUpdateUI) | |
d14a1e28 RD |
301 | |
302 | # Build interface | |
29a41103 | 303 | sizer = wx.BoxSizer(wx.VERTICAL) |
da7d0696 | 304 | #sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) |
d14a1e28 | 305 | # Horizontal sizer for toolbar and splitter |
29a41103 RD |
306 | self.toolsSizer = sizer1 = wx.BoxSizer() |
307 | splitter = wx.SplitterWindow(self, -1, style=wx.SP_3DSASH) | |
d14a1e28 RD |
308 | self.splitter = splitter |
309 | splitter.SetMinimumPaneSize(100) | |
310 | # Create tree | |
311 | global tree | |
312 | g.tree = tree = XML_Tree(splitter, -1) | |
313 | ||
314 | # Init pull-down menu data | |
315 | global pullDownMenu | |
316 | g.pullDownMenu = pullDownMenu = PullDownMenu(self) | |
317 | ||
318 | # Vertical toolbar for GUI buttons | |
319 | g.tools = tools = Tools(self) | |
320 | tools.Show(conf.showTools) | |
29a41103 | 321 | if conf.showTools: sizer1.Add(tools, 0, wx.EXPAND) |
d14a1e28 RD |
322 | |
323 | tree.RegisterKeyEvents() | |
324 | ||
7df24e78 RR |
325 | # Miniframe for split mode |
326 | miniFrame = wx.MiniFrame(self, -1, 'Properties & Style', | |
327 | (conf.panelX, conf.panelY), | |
328 | (conf.panelWidth, conf.panelHeight)) | |
d14a1e28 | 329 | self.miniFrame = miniFrame |
29a41103 | 330 | sizer2 = wx.BoxSizer() |
d14a1e28 RD |
331 | miniFrame.SetAutoLayout(True) |
332 | miniFrame.SetSizer(sizer2) | |
29a41103 | 333 | wx.EVT_CLOSE(self.miniFrame, self.OnCloseMiniFrame) |
d14a1e28 RD |
334 | # Create panel for parameters |
335 | global panel | |
336 | if conf.embedPanel: | |
337 | panel = Panel(splitter) | |
338 | # Set plitter windows | |
339 | splitter.SplitVertically(tree, panel, conf.sashPos) | |
340 | else: | |
341 | panel = Panel(miniFrame) | |
29a41103 | 342 | sizer2.Add(panel, 1, wx.EXPAND) |
d14a1e28 RD |
343 | miniFrame.Show(True) |
344 | splitter.Initialize(tree) | |
29a41103 RD |
345 | sizer1.Add(splitter, 1, wx.EXPAND) |
346 | sizer.Add(sizer1, 1, wx.EXPAND) | |
d14a1e28 RD |
347 | self.SetAutoLayout(True) |
348 | self.SetSizer(sizer) | |
349 | ||
d14a1e28 | 350 | # Other events |
29a41103 RD |
351 | wx.EVT_IDLE(self, self.OnIdle) |
352 | wx.EVT_CLOSE(self, self.OnCloseWindow) | |
353 | wx.EVT_KEY_DOWN(self, tools.OnKeyDown) | |
354 | wx.EVT_KEY_UP(self, tools.OnKeyUp) | |
355 | wx.EVT_ICONIZE(self, self.OnIconize) | |
80389ff7 | 356 | |
80389ff7 RR |
357 | def OnRecentFile(self,evt): |
358 | # open recently used file | |
359 | if not self.AskSave(): return | |
29a41103 | 360 | wx.BeginBusyCursor() |
52cd0643 RD |
361 | |
362 | # get the pathname based on the menu ID | |
363 | fileNum = evt.GetId() - wx.ID_FILE1 | |
364 | path = g.fileHistory.GetHistoryFile(fileNum) | |
365 | ||
366 | if self.Open(path): | |
367 | self.SetStatusText('Data loaded') | |
368 | # add it back to the history so it will be moved up the list | |
369 | self.SaveRecent(path) | |
370 | else: | |
371 | self.SetStatusText('Failed') | |
372 | ||
29a41103 | 373 | wx.EndBusyCursor() |
d14a1e28 RD |
374 | |
375 | def OnNew(self, evt): | |
376 | if not self.AskSave(): return | |
377 | self.Clear() | |
378 | ||
379 | def OnOpen(self, evt): | |
380 | if not self.AskSave(): return | |
29a41103 RD |
381 | dlg = wx.FileDialog(self, 'Open', os.path.dirname(self.dataFile), |
382 | '', '*.xrc', wx.OPEN | wx.CHANGE_DIR) | |
383 | if dlg.ShowModal() == wx.ID_OK: | |
d14a1e28 RD |
384 | path = dlg.GetPath() |
385 | self.SetStatusText('Loading...') | |
29a41103 | 386 | wx.BeginBusyCursor() |
016f67ba RR |
387 | try: |
388 | if self.Open(path): | |
389 | self.SetStatusText('Data loaded') | |
52cd0643 | 390 | self.SaveRecent(path) |
016f67ba RR |
391 | else: |
392 | self.SetStatusText('Failed') | |
016f67ba | 393 | finally: |
29a41103 | 394 | wx.EndBusyCursor() |
d14a1e28 RD |
395 | dlg.Destroy() |
396 | ||
397 | def OnSaveOrSaveAs(self, evt): | |
29a41103 | 398 | if evt.GetId() == wx.ID_SAVEAS or not self.dataFile: |
6cb85701 RR |
399 | if self.dataFile: name = '' |
400 | else: name = defaultName | |
207ebbbd | 401 | dirname = os.path.abspath(os.path.dirname(self.dataFile)) |
29a41103 RD |
402 | dlg = wx.FileDialog(self, 'Save As', dirname, name, '*.xrc', |
403 | wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) | |
404 | if dlg.ShowModal() == wx.ID_OK: | |
d14a1e28 | 405 | path = dlg.GetPath() |
cc202d9b RD |
406 | if isinstance(path, unicode): |
407 | path = path.encode(sys.getfilesystemencoding()) | |
d14a1e28 RD |
408 | dlg.Destroy() |
409 | else: | |
410 | dlg.Destroy() | |
411 | return | |
73b2a9a7 RD |
412 | |
413 | if conf.localconf: | |
414 | # if we already have a localconf then it needs to be | |
415 | # copied to a new config with the new name | |
416 | lc = conf.localconf | |
417 | nc = self.CreateLocalConf(path) | |
418 | flag, key, idx = lc.GetFirstEntry() | |
419 | while flag: | |
420 | nc.Write(key, lc.Read(key)) | |
421 | flag, key, idx = lc.GetNextEntry(idx) | |
422 | conf.localconf = nc | |
423 | else: | |
424 | # otherwise create a new one | |
425 | conf.localconf = self.CreateLocalConf(path) | |
d14a1e28 RD |
426 | else: |
427 | path = self.dataFile | |
428 | self.SetStatusText('Saving...') | |
29a41103 | 429 | wx.BeginBusyCursor() |
d14a1e28 | 430 | try: |
016f67ba RR |
431 | try: |
432 | tmpFile,tmpName = tempfile.mkstemp(prefix='xrced-') | |
433 | os.close(tmpFile) | |
434 | self.Save(tmpName) # save temporary file first | |
435 | shutil.move(tmpName, path) | |
436 | self.dataFile = path | |
52cd0643 | 437 | self.SetModified(False) |
73b2a9a7 RD |
438 | if conf.localconf.ReadBool("autogenerate", False): |
439 | pypath = conf.localconf.Read("filename") | |
440 | embed = conf.localconf.ReadBool("embedResource", False) | |
7e05216c RD |
441 | genGettext = conf.localconf.ReadBool("genGettext", False) |
442 | self.GeneratePython(self.dataFile, pypath, embed, genGettext) | |
73b2a9a7 | 443 | |
016f67ba RR |
444 | self.SetStatusText('Data saved') |
445 | self.SaveRecent(path) | |
446 | except IOError: | |
447 | self.SetStatusText('Failed') | |
448 | finally: | |
29a41103 | 449 | wx.EndBusyCursor() |
80389ff7 RR |
450 | |
451 | def SaveRecent(self,path): | |
452 | # append to recently used files | |
52cd0643 | 453 | g.fileHistory.AddFileToHistory(path) |
d14a1e28 | 454 | |
7e05216c | 455 | def GeneratePython(self, dataFile, pypath, embed, genGettext): |
73b2a9a7 RD |
456 | try: |
457 | import wx.tools.pywxrc | |
458 | rescomp = wx.tools.pywxrc.XmlResourceCompiler() | |
0fe9c329 | 459 | rescomp.MakePythonModule([dataFile], pypath, embed, genGettext) |
73b2a9a7 RD |
460 | except: |
461 | inf = sys.exc_info() | |
29a41103 RD |
462 | wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1]) |
463 | wx.LogError('Error generating python code : %s' % pypath) | |
73b2a9a7 RD |
464 | raise |
465 | ||
466 | ||
467 | def OnGeneratePython(self, evt): | |
468 | if self.modified or not conf.localconf: | |
469 | wx.MessageBox("Save the XRC file first!", "Error") | |
470 | return | |
471 | ||
472 | dlg = PythonOptions(self, conf.localconf, self.dataFile) | |
473 | dlg.ShowModal() | |
474 | dlg.Destroy() | |
5050b626 RR |
475 | |
476 | def OnPrefs(self, evt): | |
477 | dlg = PrefsDialog(self) | |
478 | if dlg.ShowModal() == wx.ID_OK: | |
479 | # Fetch new preferences | |
480 | for id,cdp in dlg.checkControls.items(): | |
481 | c,d,p = cdp | |
482 | if dlg.FindWindowById(id).IsChecked(): | |
483 | d[p] = str(c.GetValue()) | |
484 | elif p in d: del d[p] | |
7058b794 | 485 | g.conf.allowExec = ('ask', 'yes', 'no')[dlg.radio_allow_exec.GetSelection()] |
5050b626 | 486 | dlg.Destroy() |
73b2a9a7 | 487 | |
d14a1e28 RD |
488 | def OnExit(self, evt): |
489 | self.Close() | |
490 | ||
491 | def OnUndo(self, evt): | |
492 | # Extra check to not mess with idle updating | |
493 | if undoMan.CanUndo(): | |
494 | undoMan.Undo() | |
a023b456 RR |
495 | g.panel.SetModified(False) |
496 | if not undoMan.CanUndo(): | |
497 | self.SetModified(False) | |
d14a1e28 RD |
498 | |
499 | def OnRedo(self, evt): | |
500 | if undoMan.CanRedo(): | |
501 | undoMan.Redo() | |
a023b456 | 502 | self.SetModified(True) |
d14a1e28 RD |
503 | |
504 | def OnCopy(self, evt): | |
505 | selected = tree.selection | |
506 | if not selected: return # key pressed event | |
507 | xxx = tree.GetPyData(selected) | |
f65bb0f8 | 508 | if wx.TheClipboard.Open(): |
b372319f RR |
509 | if xxx.isElement: |
510 | data = wx.CustomDataObject('XRCED') | |
511 | # Set encoding in header | |
512 | # (False,True) | |
513 | s = xxx.node.toxml(encoding=expat.native_encoding) | |
514 | else: | |
515 | data = wx.CustomDataObject('XRCED_node') | |
516 | s = xxx.node.data | |
ebaaf8f6 | 517 | data.SetData(cPickle.dumps(s)) |
f65bb0f8 RD |
518 | wx.TheClipboard.SetData(data) |
519 | wx.TheClipboard.Close() | |
520 | self.SetStatusText('Copied') | |
521 | else: | |
522 | wx.MessageBox("Unable to open the clipboard", "Error") | |
d14a1e28 RD |
523 | |
524 | def OnPaste(self, evt): | |
525 | selected = tree.selection | |
526 | if not selected: return # key pressed event | |
527 | # For pasting with Ctrl pressed | |
016f67ba | 528 | appendChild = True |
d14a1e28 | 529 | if evt.GetId() == pullDownMenu.ID_PASTE_SIBLING: appendChild = False |
016f67ba RR |
530 | elif evt.GetId() == self.ID_TOOL_PASTE: |
531 | if g.tree.ctrl: appendChild = False | |
532 | else: appendChild = not tree.NeedInsert(selected) | |
d14a1e28 RD |
533 | else: appendChild = not tree.NeedInsert(selected) |
534 | xxx = tree.GetPyData(selected) | |
535 | if not appendChild: | |
536 | # If has next item, insert, else append to parent | |
537 | nextItem = tree.GetNextSibling(selected) | |
538 | parentLeaf = tree.GetItemParent(selected) | |
539 | # Expanded container (must have children) | |
540 | elif tree.IsExpanded(selected) and tree.GetChildrenCount(selected, False): | |
541 | # Insert as first child | |
a4c013b2 | 542 | nextItem = tree.GetFirstChild(selected)[0] |
d14a1e28 RD |
543 | parentLeaf = selected |
544 | else: | |
545 | # No children or unexpanded item - appendChild stays True | |
29a41103 | 546 | nextItem = wx.TreeItemId() # no next item |
d14a1e28 RD |
547 | parentLeaf = selected |
548 | parent = tree.GetPyData(parentLeaf).treeObject() | |
549 | ||
af52e185 | 550 | # Create a copy of clipboard pickled element |
b372319f | 551 | success = success_node = False |
f65bb0f8 | 552 | if wx.TheClipboard.Open(): |
c0d5ae74 RR |
553 | try: |
554 | data = wx.CustomDataObject('XRCED') | |
b372319f | 555 | if wx.TheClipboard.IsSupported(data.GetFormat()): |
c0d5ae74 RR |
556 | try: |
557 | success = wx.TheClipboard.GetData(data) | |
558 | except: | |
559 | # there is a problem if XRCED_node is in clipboard | |
560 | # but previous SetData was for XRCED | |
561 | pass | |
562 | if not success: # try other format | |
563 | data = wx.CustomDataObject('XRCED_node') | |
564 | if wx.TheClipboard.IsSupported(data.GetFormat()): | |
565 | success_node = wx.TheClipboard.GetData(data) | |
566 | finally: | |
567 | wx.TheClipboard.Close() | |
f65bb0f8 | 568 | |
b372319f | 569 | if not success and not success_node: |
f65bb0f8 RD |
570 | wx.MessageBox( |
571 | "There is no data in the clipboard in the required format", | |
572 | "Error") | |
af52e185 | 573 | return |
f65bb0f8 | 574 | |
1893848e | 575 | xml = cPickle.loads(data.GetData()) # xml representation of element |
b372319f RR |
576 | if success: |
577 | elem = minidom.parseString(xml).childNodes[0] | |
578 | else: | |
579 | elem = g.tree.dom.createComment(xml) | |
75aa1946 | 580 | |
d14a1e28 RD |
581 | # Tempopary xxx object to test things |
582 | xxx = MakeXXXFromDOM(parent, elem) | |
75aa1946 | 583 | |
d14a1e28 | 584 | # Check compatibility |
75aa1946 | 585 | if not self.ItemsAreCompatible(parent, xxx.treeObject()): return |
d14a1e28 RD |
586 | |
587 | # Check parent and child relationships. | |
588 | # If parent is sizer or notebook, child is of wrong class or | |
589 | # parent is normal window, child is child container then detach child. | |
590 | isChildContainer = isinstance(xxx, xxxChildContainer) | |
306b6fe9 | 591 | parentIsBook = parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook] |
d14a1e28 RD |
592 | if isChildContainer and \ |
593 | ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \ | |
306b6fe9 RR |
594 | (parentIsBook and not isinstance(xxx, xxxPage)) or \ |
595 | not (parent.isSizer or parentIsBook)): | |
b372319f | 596 | elem.removeChild(xxx.child.node) # detach child |
d14a1e28 | 597 | elem.unlink() # delete child container |
b372319f | 598 | elem = xxx.child.node # replace |
d14a1e28 RD |
599 | # This may help garbage collection |
600 | xxx.child.parent = None | |
601 | isChildContainer = False | |
602 | # Parent is sizer or notebook, child is not child container | |
603 | if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer): | |
604 | # Create sizer item element | |
64b9ac75 | 605 | sizerItemElem = MakeEmptyDOM(parent.itemTag) |
d14a1e28 RD |
606 | sizerItemElem.appendChild(elem) |
607 | elem = sizerItemElem | |
608 | elif isinstance(parent, xxxNotebook) and not isChildContainer: | |
609 | pageElem = MakeEmptyDOM('notebookpage') | |
610 | pageElem.appendChild(elem) | |
611 | elem = pageElem | |
306b6fe9 RR |
612 | elif isinstance(parent, xxxChoicebook) and not isChildContainer: |
613 | pageElem = MakeEmptyDOM('choicebookpage') | |
614 | pageElem.appendChild(elem) | |
615 | elem = pageElem | |
616 | elif isinstance(parent, xxxListbook) and not isChildContainer: | |
617 | pageElem = MakeEmptyDOM('listbookpage') | |
618 | pageElem.appendChild(elem) | |
619 | elem = pageElem | |
d14a1e28 RD |
620 | # Insert new node, register undo |
621 | newItem = tree.InsertNode(parentLeaf, parent, elem, nextItem) | |
622 | undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) | |
623 | # Scroll to show new item (!!! redundant?) | |
624 | tree.EnsureVisible(newItem) | |
625 | tree.SelectItem(newItem) | |
626 | if not tree.IsVisible(newItem): | |
627 | tree.ScrollTo(newItem) | |
628 | tree.Refresh() | |
629 | # Update view? | |
630 | if g.testWin and tree.IsHighlatable(newItem): | |
631 | if conf.autoRefresh: | |
632 | tree.needUpdate = True | |
633 | tree.pendingHighLight = newItem | |
634 | else: | |
635 | tree.pendingHighLight = None | |
6cb85701 | 636 | self.SetModified() |
d14a1e28 RD |
637 | self.SetStatusText('Pasted') |
638 | ||
75aa1946 RR |
639 | |
640 | def ItemsAreCompatible(self, parent, child): | |
641 | # Check compatibility | |
642 | error = False | |
b372319f RR |
643 | # Comments are always compatible |
644 | if child.__class__ == xxxComment: | |
645 | return True | |
75aa1946 RR |
646 | # Top-level |
647 | if child.__class__ in [xxxDialog, xxxFrame, xxxWizard]: | |
648 | # Top-level classes | |
649 | if parent.__class__ != xxxMainNode: error = True | |
650 | elif child.__class__ == xxxMenuBar: | |
651 | # Menubar can be put in frame or dialog | |
652 | if parent.__class__ not in [xxxMainNode, xxxFrame, xxxDialog]: error = True | |
653 | elif child.__class__ == xxxToolBar: | |
654 | # Toolbar can be top-level of child of panel or frame | |
655 | if parent.__class__ not in [xxxMainNode, xxxPanel, xxxFrame] and \ | |
656 | not parent.isSizer: error = True | |
657 | elif child.__class__ == xxxPanel and parent.__class__ == xxxMainNode: | |
658 | pass | |
659 | elif child.__class__ == xxxSpacer: | |
660 | if not parent.isSizer: error = True | |
661 | elif child.__class__ == xxxSeparator: | |
662 | if not parent.__class__ in [xxxMenu, xxxToolBar]: error = True | |
663 | elif child.__class__ == xxxTool: | |
664 | if parent.__class__ != xxxToolBar: error = True | |
665 | elif child.__class__ == xxxMenu: | |
666 | if not parent.__class__ in [xxxMainNode, xxxMenuBar, xxxMenu]: error = True | |
667 | elif child.__class__ == xxxMenuItem: | |
668 | if not parent.__class__ in [xxxMenuBar, xxxMenu]: error = True | |
669 | elif child.isSizer and parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]: | |
670 | error = True | |
671 | else: # normal controls can be almost anywhere | |
672 | if parent.__class__ == xxxMainNode or \ | |
673 | parent.__class__ in [xxxMenuBar, xxxMenu]: error = True | |
674 | if error: | |
675 | if parent.__class__ == xxxMainNode: parentClass = 'root' | |
676 | else: parentClass = parent.className | |
677 | wx.LogError('Incompatible parent/child: parent is %s, child is %s!' % | |
678 | (parentClass, child.className)) | |
679 | return False | |
680 | return True | |
681 | ||
682 | def OnMoveUp(self, evt): | |
683 | selected = tree.selection | |
684 | if not selected: return | |
685 | ||
686 | index = tree.ItemIndex(selected) | |
687 | if index == 0: return # No previous sibling found | |
688 | ||
2fe11431 | 689 | # Remove highlight, update testWin |
09a68e88 | 690 | if g.testWin and g.testWin.highLight: |
2fe11431 RR |
691 | g.testWin.highLight.Remove() |
692 | tree.needUpdate = True | |
693 | ||
75aa1946 RR |
694 | # Undo info |
695 | self.lastOp = 'MOVEUP' | |
696 | status = 'Moved before previous sibling' | |
697 | ||
698 | # Prepare undo data | |
699 | panel.Apply() | |
2fe11431 | 700 | tree.UnselectAll() |
75aa1946 RR |
701 | |
702 | parent = tree.GetItemParent(selected) | |
703 | elem = tree.RemoveLeaf(selected) | |
704 | nextItem = tree.GetFirstChild(parent)[0] | |
705 | for i in range(index - 1): nextItem = tree.GetNextSibling(nextItem) | |
706 | selected = tree.InsertNode(parent, tree.GetPyData(parent).treeObject(), elem, nextItem) | |
707 | newIndex = tree.ItemIndex(selected) | |
708 | tree.SelectItem(selected) | |
709 | ||
710 | undoMan.RegisterUndo(UndoMove(parent, index, parent, newIndex)) | |
711 | ||
712 | self.modified = True | |
713 | self.SetStatusText(status) | |
714 | ||
715 | return | |
716 | ||
717 | def OnMoveDown(self, evt): | |
718 | selected = tree.selection | |
719 | if not selected: return | |
720 | ||
721 | index = tree.ItemIndex(selected) | |
722 | next = tree.GetNextSibling(selected) | |
723 | if not next: return | |
724 | ||
2fe11431 | 725 | # Remove highlight, update testWin |
09a68e88 | 726 | if g.testWin and g.testWin.highLight: |
2fe11431 RR |
727 | g.testWin.highLight.Remove() |
728 | tree.needUpdate = True | |
729 | ||
75aa1946 RR |
730 | # Undo info |
731 | self.lastOp = 'MOVEDOWN' | |
732 | status = 'Moved after next sibling' | |
733 | ||
734 | # Prepare undo data | |
735 | panel.Apply() | |
2fe11431 | 736 | tree.UnselectAll() |
75aa1946 RR |
737 | |
738 | parent = tree.GetItemParent(selected) | |
739 | elem = tree.RemoveLeaf(selected) | |
740 | nextItem = tree.GetFirstChild(parent)[0] | |
741 | for i in range(index + 1): nextItem = tree.GetNextSibling(nextItem) | |
742 | selected = tree.InsertNode(parent, tree.GetPyData(parent).treeObject(), elem, nextItem) | |
743 | newIndex = tree.ItemIndex(selected) | |
744 | tree.SelectItem(selected) | |
745 | ||
746 | undoMan.RegisterUndo(UndoMove(parent, index, parent, newIndex)) | |
747 | ||
748 | self.modified = True | |
749 | self.SetStatusText(status) | |
750 | ||
751 | return | |
752 | ||
753 | def OnMoveLeft(self, evt): | |
754 | selected = tree.selection | |
755 | if not selected: return | |
756 | ||
757 | oldParent = tree.GetItemParent(selected) | |
758 | if not oldParent: return | |
759 | pparent = tree.GetItemParent(oldParent) | |
760 | if not pparent: return | |
761 | ||
762 | # Check compatibility | |
763 | if not self.ItemsAreCompatible(tree.GetPyData(pparent).treeObject(), tree.GetPyData(selected).treeObject()): return | |
764 | ||
2fe11431 | 765 | # Remove highlight, update testWin |
09a68e88 | 766 | if g.testWin and g.testWin.highLight: |
2fe11431 RR |
767 | g.testWin.highLight.Remove() |
768 | tree.needUpdate = True | |
769 | ||
75aa1946 RR |
770 | # Undo info |
771 | self.lastOp = 'MOVELEFT' | |
772 | status = 'Made next sibling of parent' | |
773 | ||
774 | oldIndex = tree.ItemIndex(selected) | |
775 | elem = tree.RemoveLeaf(selected) | |
776 | nextItem = tree.GetFirstChild(pparent)[0] | |
777 | parentIndex = tree.ItemIndex(oldParent) | |
778 | for i in range(parentIndex + 1): nextItem = tree.GetNextSibling(nextItem) | |
779 | ||
780 | # Check parent and child relationships. | |
781 | # If parent is sizer or notebook, child is of wrong class or | |
782 | # parent is normal window, child is child container then detach child. | |
783 | parent = tree.GetPyData(pparent).treeObject() | |
784 | xxx = MakeXXXFromDOM(parent, elem) | |
785 | isChildContainer = isinstance(xxx, xxxChildContainer) | |
786 | if isChildContainer and \ | |
787 | ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \ | |
788 | (isinstance(parent, xxxNotebook) and not isinstance(xxx, xxxNotebookPage)) or \ | |
789 | not (parent.isSizer or isinstance(parent, xxxNotebook))): | |
b372319f | 790 | elem.removeChild(xxx.child.node) # detach child |
75aa1946 | 791 | elem.unlink() # delete child container |
b372319f | 792 | elem = xxx.child.node # replace |
75aa1946 RR |
793 | # This may help garbage collection |
794 | xxx.child.parent = None | |
795 | isChildContainer = False | |
796 | # Parent is sizer or notebook, child is not child container | |
797 | if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer): | |
798 | # Create sizer item element | |
799 | sizerItemElem = MakeEmptyDOM('sizeritem') | |
800 | sizerItemElem.appendChild(elem) | |
801 | elem = sizerItemElem | |
802 | elif isinstance(parent, xxxNotebook) and not isChildContainer: | |
803 | pageElem = MakeEmptyDOM('notebookpage') | |
804 | pageElem.appendChild(elem) | |
805 | elem = pageElem | |
806 | ||
807 | selected = tree.InsertNode(pparent, tree.GetPyData(pparent).treeObject(), elem, nextItem) | |
808 | newIndex = tree.ItemIndex(selected) | |
2fe11431 | 809 | tree.oldItem = None |
75aa1946 RR |
810 | tree.SelectItem(selected) |
811 | ||
812 | undoMan.RegisterUndo(UndoMove(oldParent, oldIndex, pparent, newIndex)) | |
813 | ||
814 | self.modified = True | |
815 | self.SetStatusText(status) | |
816 | ||
817 | def OnMoveRight(self, evt): | |
818 | selected = tree.selection | |
819 | if not selected: return | |
820 | ||
821 | oldParent = tree.GetItemParent(selected) | |
822 | if not oldParent: return | |
823 | ||
824 | newParent = tree.GetPrevSibling(selected) | |
825 | if not newParent: return | |
826 | ||
827 | parent = tree.GetPyData(newParent).treeObject() | |
828 | ||
829 | # Check compatibility | |
830 | if not self.ItemsAreCompatible(parent, tree.GetPyData(selected).treeObject()): return | |
831 | ||
2fe11431 | 832 | # Remove highlight, update testWin |
09a68e88 | 833 | if g.testWin and g.testWin.highLight: |
2fe11431 RR |
834 | g.testWin.highLight.Remove() |
835 | tree.needUpdate = True | |
836 | ||
75aa1946 RR |
837 | # Undo info |
838 | self.lastOp = 'MOVERIGHT' | |
839 | status = 'Made last child of previous sibling' | |
840 | ||
841 | oldIndex = tree.ItemIndex(selected) | |
842 | elem = tree.RemoveLeaf(selected) | |
843 | ||
844 | # Check parent and child relationships. | |
845 | # If parent is sizer or notebook, child is of wrong class or | |
846 | # parent is normal window, child is child container then detach child. | |
847 | xxx = MakeXXXFromDOM(parent, elem) | |
848 | isChildContainer = isinstance(xxx, xxxChildContainer) | |
849 | if isChildContainer and \ | |
850 | ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \ | |
851 | (isinstance(parent, xxxNotebook) and not isinstance(xxx, xxxNotebookPage)) or \ | |
852 | not (parent.isSizer or isinstance(parent, xxxNotebook))): | |
b372319f | 853 | elem.removeChild(xxx.child.node) # detach child |
75aa1946 | 854 | elem.unlink() # delete child container |
b372319f | 855 | elem = xxx.child.node # replace |
75aa1946 RR |
856 | # This may help garbage collection |
857 | xxx.child.parent = None | |
858 | isChildContainer = False | |
859 | # Parent is sizer or notebook, child is not child container | |
860 | if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer): | |
861 | # Create sizer item element | |
862 | sizerItemElem = MakeEmptyDOM('sizeritem') | |
863 | sizerItemElem.appendChild(elem) | |
864 | elem = sizerItemElem | |
865 | elif isinstance(parent, xxxNotebook) and not isChildContainer: | |
866 | pageElem = MakeEmptyDOM('notebookpage') | |
867 | pageElem.appendChild(elem) | |
868 | elem = pageElem | |
869 | ||
870 | selected = tree.InsertNode(newParent, tree.GetPyData(newParent).treeObject(), elem, wx.TreeItemId()) | |
871 | ||
872 | newIndex = tree.ItemIndex(selected) | |
2fe11431 | 873 | tree.oldItem = None |
75aa1946 RR |
874 | tree.SelectItem(selected) |
875 | ||
876 | undoMan.RegisterUndo(UndoMove(oldParent, oldIndex, newParent, newIndex)) | |
2fe11431 | 877 | |
75aa1946 RR |
878 | self.modified = True |
879 | self.SetStatusText(status) | |
880 | ||
f65bb0f8 | 881 | |
d14a1e28 RD |
882 | def OnCutDelete(self, evt): |
883 | selected = tree.selection | |
884 | if not selected: return # key pressed event | |
885 | # Undo info | |
29a41103 | 886 | if evt.GetId() == wx.ID_CUT: |
d14a1e28 RD |
887 | self.lastOp = 'CUT' |
888 | status = 'Removed to clipboard' | |
889 | else: | |
890 | self.lastOp = 'DELETE' | |
891 | status = 'Deleted' | |
892 | # Delete testWin? | |
893 | if g.testWin: | |
894 | # If deleting top-level item, delete testWin | |
895 | if selected == g.testWin.item: | |
896 | g.testWin.Destroy() | |
897 | g.testWin = None | |
898 | else: | |
899 | # Remove highlight, update testWin | |
900 | if g.testWin.highLight: | |
901 | g.testWin.highLight.Remove() | |
902 | tree.needUpdate = True | |
903 | # Prepare undo data | |
904 | panel.Apply() | |
905 | index = tree.ItemFullIndex(selected) | |
b372319f | 906 | xxx = tree.GetPyData(selected) |
d14a1e28 RD |
907 | parent = tree.GetPyData(tree.GetItemParent(selected)).treeObject() |
908 | elem = tree.RemoveLeaf(selected) | |
909 | undoMan.RegisterUndo(UndoCutDelete(index, parent, elem)) | |
29a41103 | 910 | if evt.GetId() == wx.ID_CUT: |
f65bb0f8 | 911 | if wx.TheClipboard.Open(): |
b372319f RR |
912 | if xxx.isElement: |
913 | data = wx.CustomDataObject('XRCED') | |
914 | # (False, True) | |
915 | s = elem.toxml(encoding=expat.native_encoding) | |
916 | else: | |
917 | data = wx.CustomDataObject('XRCED_node') | |
918 | s = xxx.node.data | |
ebaaf8f6 | 919 | data.SetData(cPickle.dumps(s)) |
f65bb0f8 RD |
920 | wx.TheClipboard.SetData(data) |
921 | wx.TheClipboard.Close() | |
922 | else: | |
923 | wx.MessageBox("Unable to open the clipboard", "Error") | |
d14a1e28 | 924 | tree.pendingHighLight = None |
6cb85701 RR |
925 | tree.UnselectAll() |
926 | tree.selection = None | |
927 | # Update tools | |
928 | g.tools.UpdateUI() | |
d14a1e28 | 929 | panel.Clear() |
6cb85701 | 930 | self.SetModified() |
d14a1e28 RD |
931 | self.SetStatusText(status) |
932 | ||
2481bf3c RR |
933 | def OnSubclass(self, evt): |
934 | selected = tree.selection | |
935 | xxx = tree.GetPyData(selected).treeObject() | |
b372319f | 936 | elem = xxx.node |
2481bf3c | 937 | subclass = xxx.subclass |
29a41103 RD |
938 | dlg = wx.TextEntryDialog(self, 'Subclass:', defaultValue=subclass) |
939 | if dlg.ShowModal() == wx.ID_OK: | |
2481bf3c RR |
940 | subclass = dlg.GetValue() |
941 | if subclass: | |
942 | elem.setAttribute('subclass', subclass) | |
2481bf3c RR |
943 | elif elem.hasAttribute('subclass'): |
944 | elem.removeAttribute('subclass') | |
6cb85701 | 945 | self.SetModified() |
2481bf3c RR |
946 | xxx.subclass = elem.getAttribute('subclass') |
947 | tree.SetItemText(selected, xxx.treeName()) | |
948 | panel.pages[0].box.SetLabel(xxx.panelName()) | |
949 | dlg.Destroy() | |
950 | ||
d14a1e28 RD |
951 | def OnEmbedPanel(self, evt): |
952 | conf.embedPanel = evt.IsChecked() | |
953 | if conf.embedPanel: | |
954 | # Remember last dimentions | |
955 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
956 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
957 | size = self.GetSize() | |
958 | pos = self.GetPosition() | |
959 | sizePanel = panel.GetSize() | |
960 | panel.Reparent(self.splitter) | |
80389ff7 | 961 | self.miniFrame.GetSizer().Remove(panel) |
d14a1e28 RD |
962 | # Widen |
963 | self.SetDimensions(pos.x, pos.y, size.width + sizePanel.width, size.height) | |
964 | self.splitter.SplitVertically(tree, panel, conf.sashPos) | |
965 | self.miniFrame.Show(False) | |
966 | else: | |
967 | conf.sashPos = self.splitter.GetSashPosition() | |
968 | pos = self.GetPosition() | |
969 | size = self.GetSize() | |
970 | sizePanel = panel.GetSize() | |
971 | self.splitter.Unsplit(panel) | |
972 | sizer = self.miniFrame.GetSizer() | |
973 | panel.Reparent(self.miniFrame) | |
974 | panel.Show(True) | |
29a41103 | 975 | sizer.Add(panel, 1, wx.EXPAND) |
d14a1e28 RD |
976 | self.miniFrame.Show(True) |
977 | self.miniFrame.SetDimensions(conf.panelX, conf.panelY, | |
978 | conf.panelWidth, conf.panelHeight) | |
7df24e78 | 979 | self.miniFrame.Layout() |
d14a1e28 RD |
980 | # Reduce width |
981 | self.SetDimensions(pos.x, pos.y, | |
982 | max(size.width - sizePanel.width, self.minWidth), size.height) | |
983 | ||
984 | def OnShowTools(self, evt): | |
985 | conf.showTools = evt.IsChecked() | |
986 | g.tools.Show(conf.showTools) | |
987 | if conf.showTools: | |
29a41103 | 988 | self.toolsSizer.Prepend(g.tools, 0, wx.EXPAND) |
d14a1e28 RD |
989 | else: |
990 | self.toolsSizer.Remove(g.tools) | |
991 | self.toolsSizer.Layout() | |
992 | ||
993 | def OnTest(self, evt): | |
994 | if not tree.selection: return # key pressed event | |
995 | tree.ShowTestWindow(tree.selection) | |
996 | ||
64bce500 RR |
997 | def OnTestHide(self, evt): |
998 | tree.CloseTestWindow() | |
999 | ||
fd919451 RR |
1000 | # Find object by relative position |
1001 | def FindObject(self, item, obj): | |
1002 | # We simply perform depth-first traversal, sinse it's too much | |
1003 | # hassle to deal with all sizer/window combinations | |
1004 | w = tree.FindNodeObject(item) | |
29a41103 | 1005 | if w == obj or isinstance(w, wx.GBSizerItem) and w.GetWindow() == obj: |
fd919451 RR |
1006 | return item |
1007 | if tree.ItemHasChildren(item): | |
1008 | child = tree.GetFirstChild(item)[0] | |
1009 | while child: | |
1010 | found = self.FindObject(child, obj) | |
1011 | if found: return found | |
1012 | child = tree.GetNextSibling(child) | |
1013 | return None | |
1014 | ||
da7d0696 | 1015 | # Click event after locate activated |
fd919451 | 1016 | def OnTestWinLeftDown(self, evt): |
da7d0696 | 1017 | # Restore normal event processing |
fd919451 | 1018 | self.SetHandler(g.testWin) |
29a41103 | 1019 | g.testWin.Disconnect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN) |
fd919451 RR |
1020 | item = self.FindObject(g.testWin.item, evt.GetEventObject()) |
1021 | if item: | |
20002db0 | 1022 | tree.EnsureVisible(item) |
fd919451 | 1023 | tree.SelectItem(item) |
016f67ba | 1024 | self.tb.ToggleTool(self.ID_TOOL_LOCATE, False) |
64bce500 RR |
1025 | if item: |
1026 | self.SetStatusText('Selected %s' % tree.GetItemText(item)) | |
1027 | else: | |
1028 | self.SetStatusText('Locate failed!') | |
fd919451 RR |
1029 | |
1030 | def SetHandler(self, w, h=None): | |
1031 | if h: | |
1032 | w.SetEventHandler(h) | |
29a41103 | 1033 | w.SetCursor(wx.CROSS_CURSOR) |
fd919451 RR |
1034 | else: |
1035 | w.SetEventHandler(w) | |
29a41103 | 1036 | w.SetCursor(wx.NullCursor) |
fd919451 RR |
1037 | for ch in w.GetChildren(): |
1038 | self.SetHandler(ch, h) | |
1039 | ||
1040 | def OnLocate(self, evt): | |
1041 | if g.testWin: | |
64bce500 | 1042 | if evt.GetId() == self.ID_LOCATE or \ |
016f67ba | 1043 | evt.GetId() == self.ID_TOOL_LOCATE and evt.IsChecked(): |
64bce500 | 1044 | self.SetHandler(g.testWin, g.testWin) |
29a41103 | 1045 | g.testWin.Connect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN, self.OnTestWinLeftDown) |
64bce500 | 1046 | if evt.GetId() == self.ID_LOCATE: |
016f67ba RR |
1047 | self.tb.ToggleTool(self.ID_TOOL_LOCATE, True) |
1048 | elif evt.GetId() == self.ID_TOOL_LOCATE and not evt.IsChecked(): | |
64bce500 | 1049 | self.SetHandler(g.testWin, None) |
29a41103 | 1050 | g.testWin.Disconnect(wx.ID_ANY, wx.ID_ANY, wx.wxEVT_LEFT_DOWN) |
64bce500 | 1051 | self.SetStatusText('Click somewhere in your test window now') |
fd919451 | 1052 | |
d14a1e28 RD |
1053 | def OnRefresh(self, evt): |
1054 | # If modified, apply first | |
1055 | selection = tree.selection | |
1056 | if selection: | |
1057 | xxx = tree.GetPyData(selection) | |
1058 | if xxx and panel.IsModified(): | |
1059 | tree.Apply(xxx, selection) | |
1060 | if g.testWin: | |
1061 | # (re)create | |
1062 | tree.CreateTestWin(g.testWin.item) | |
1063 | panel.modified = False | |
1064 | tree.needUpdate = False | |
1065 | ||
1066 | def OnAutoRefresh(self, evt): | |
1067 | conf.autoRefresh = evt.IsChecked() | |
1068 | self.menuBar.Check(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
1069 | self.tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
1070 | ||
1071 | def OnAbout(self, evt): | |
1072 | str = '''\ | |
1073 | XRCed version %s | |
1074 | ||
1075 | (c) Roman Rolinsky <rollrom@users.sourceforge.net> | |
1076 | Homepage: http://xrced.sourceforge.net\ | |
1077 | ''' % version | |
29a41103 | 1078 | dlg = wx.MessageDialog(self, str, 'About XRCed', wx.OK | wx.CENTRE) |
d14a1e28 RD |
1079 | dlg.ShowModal() |
1080 | dlg.Destroy() | |
1081 | ||
1082 | def OnReadme(self, evt): | |
1083 | text = open(os.path.join(basePath, 'README.txt'), 'r').read() | |
1084 | dlg = ScrolledMessageDialog(self, text, "XRCed README") | |
1085 | dlg.ShowModal() | |
1086 | dlg.Destroy() | |
1087 | ||
1088 | # Simple emulation of python command line | |
1089 | def OnDebugCMD(self, evt): | |
d14a1e28 RD |
1090 | while 1: |
1091 | try: | |
1092 | exec raw_input('C:\> ') | |
1093 | except EOFError: | |
1094 | print '^D' | |
1095 | break | |
1096 | except: | |
1097 | (etype, value, tb) =sys.exc_info() | |
1098 | tblist =traceback.extract_tb(tb)[1:] | |
1099 | msg =' '.join(traceback.format_exception_only(etype, value) | |
1100 | +traceback.format_list(tblist)) | |
1101 | print msg | |
1102 | ||
1103 | def OnCreate(self, evt): | |
ed650677 RR |
1104 | # Ignore fake events generated while dragging |
1105 | if g.tools.drag: | |
1106 | g.tools.drag = False | |
1107 | return | |
d14a1e28 RD |
1108 | selected = tree.selection |
1109 | if tree.ctrl: appendChild = False | |
1110 | else: appendChild = not tree.NeedInsert(selected) | |
1111 | xxx = tree.GetPyData(selected) | |
1112 | if not appendChild: | |
1113 | # If insert before | |
1114 | if tree.shift: | |
1115 | # If has previous item, insert after it, else append to parent | |
1116 | nextItem = selected | |
1117 | parentLeaf = tree.GetItemParent(selected) | |
1118 | else: | |
1119 | # If has next item, insert, else append to parent | |
1120 | nextItem = tree.GetNextSibling(selected) | |
1121 | parentLeaf = tree.GetItemParent(selected) | |
1122 | # Expanded container (must have children) | |
1123 | elif tree.shift and tree.IsExpanded(selected) \ | |
1124 | and tree.GetChildrenCount(selected, False): | |
a4c013b2 | 1125 | nextItem = tree.GetFirstChild(selected)[0] |
d14a1e28 RD |
1126 | parentLeaf = selected |
1127 | else: | |
29a41103 | 1128 | nextItem = wx.TreeItemId() |
d14a1e28 RD |
1129 | parentLeaf = selected |
1130 | parent = tree.GetPyData(parentLeaf) | |
1131 | if parent.hasChild: parent = parent.child | |
1132 | ||
da7d0696 RR |
1133 | self.CreateXXX(parent, parentLeaf, nextItem, evt.GetId()) |
1134 | ||
1135 | # Actual method to create object and add to XML and wx trees | |
1136 | def CreateXXX(self, parent, parentLeaf, nextItem, id): | |
1137 | selected = tree.selection | |
6cb85701 | 1138 | # Create object_ref? |
da7d0696 | 1139 | if id == ID_NEW.REF: |
29a41103 | 1140 | ref = wx.GetTextFromUser('Create reference to:', 'Create reference') |
6cb85701 RR |
1141 | if not ref: return |
1142 | xxx = MakeEmptyRefXXX(parent, ref) | |
da7d0696 | 1143 | elif id == ID_NEW.COMMENT: |
b372319f | 1144 | xxx = MakeEmptyCommentXXX(parent) |
6cb85701 RR |
1145 | else: |
1146 | # Create empty element | |
da7d0696 RR |
1147 | if id >= ID_NEW.CUSTOM: |
1148 | className = pullDownMenu.customMap[id] | |
c0d5ae74 | 1149 | else: |
da7d0696 | 1150 | className = pullDownMenu.createMap[id] |
6cb85701 | 1151 | xxx = MakeEmptyXXX(parent, className) |
d14a1e28 | 1152 | |
d14a1e28 | 1153 | # Insert new node, register undo |
b372319f RR |
1154 | if xxx.isElement: # true object |
1155 | # Set default name for top-level windows | |
1156 | if parent.__class__ == xxxMainNode: | |
1157 | cl = xxx.treeObject().__class__ | |
1158 | frame.maxIDs[cl] += 1 | |
1159 | xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl])) | |
1160 | # And for some other standard controls | |
1161 | elif parent.__class__ == xxxStdDialogButtonSizer: | |
da7d0696 RR |
1162 | # ... we can even set automatically tree name |
1163 | xxx.setTreeName(pullDownMenu.stdButtonIDs[id][0]) | |
b372319f | 1164 | obj = xxx.treeObject() |
da7d0696 | 1165 | # ... and label |
b372319f | 1166 | elem = g.tree.dom.createElement('label') |
da7d0696 | 1167 | elem.appendChild(g.tree.dom.createTextNode(pullDownMenu.stdButtonIDs[id][1])) |
b372319f RR |
1168 | obj.params['label'] = xxxParam(elem) |
1169 | xxx.treeObject().node.appendChild(elem) | |
da7d0696 RR |
1170 | # Else, set label if exists to class name |
1171 | elif 'label' in xxx.treeObject().allParams: | |
1172 | label = className | |
1173 | if label[:2] == 'wx': label = label[2:] | |
1174 | xxx.treeObject().set('label', label.upper()) | |
1175 | # For comment nodes, simply add node | |
1176 | newItem = tree.InsertNode(parentLeaf, parent, xxx.node, nextItem) | |
d14a1e28 RD |
1177 | undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) |
1178 | tree.EnsureVisible(newItem) | |
1179 | tree.SelectItem(newItem) | |
1180 | if not tree.IsVisible(newItem): | |
1181 | tree.ScrollTo(newItem) | |
1182 | tree.Refresh() | |
1183 | # Update view? | |
b372319f | 1184 | if xxx.isElement and g.testWin and tree.IsHighlatable(newItem): |
d14a1e28 RD |
1185 | if conf.autoRefresh: |
1186 | tree.needUpdate = True | |
1187 | tree.pendingHighLight = newItem | |
1188 | else: | |
1189 | tree.pendingHighLight = None | |
1190 | tree.SetFocus() | |
fe295b0d RR |
1191 | if not xxx.isElement: |
1192 | tree.EditLabel(newItem) | |
6cb85701 | 1193 | self.SetModified() |
da7d0696 | 1194 | return xxx |
6cb85701 | 1195 | |
d14a1e28 RD |
1196 | # Replace one object with another |
1197 | def OnReplace(self, evt): | |
1198 | selected = tree.selection | |
1199 | xxx = tree.GetPyData(selected).treeObject() | |
b372319f | 1200 | elem = xxx.node |
d14a1e28 | 1201 | parent = elem.parentNode |
baba4aa5 | 1202 | undoMan.RegisterUndo(UndoReplace(selected)) |
d14a1e28 RD |
1203 | # New class |
1204 | className = pullDownMenu.createMap[evt.GetId() - 1000] | |
75aa1946 | 1205 | |
d14a1e28 RD |
1206 | # Create temporary empty node (with default values) |
1207 | dummy = MakeEmptyDOM(className) | |
baba4aa5 RR |
1208 | if className == 'spacer' and xxx.className != 'spacer': |
1209 | klass = xxxSpacer | |
1210 | elif xxx.className == 'spacer' and className != 'spacer': | |
1211 | klass = xxxSizerItem | |
1212 | else: | |
1213 | klass = xxxDict[className] | |
d14a1e28 | 1214 | # Remove non-compatible children |
baba4aa5 | 1215 | if tree.ItemHasChildren(selected) and not klass.hasChildren: |
d14a1e28 RD |
1216 | tree.DeleteChildren(selected) |
1217 | nodes = elem.childNodes[:] | |
1218 | tags = [] | |
1219 | for node in nodes: | |
64bce500 | 1220 | if node.nodeType != minidom.Node.ELEMENT_NODE: continue |
d14a1e28 RD |
1221 | remove = False |
1222 | tag = node.tagName | |
1223 | if tag == 'object': | |
baba4aa5 RR |
1224 | if not klass.hasChildren: remove = True |
1225 | elif tag not in klass.allParams and \ | |
1226 | (not klass.hasStyle or tag not in klass.styles): | |
d14a1e28 RD |
1227 | remove = True |
1228 | else: | |
1229 | tags.append(tag) | |
1230 | if remove: | |
1231 | elem.removeChild(node) | |
1232 | node.unlink() | |
1233 | ||
baba4aa5 RR |
1234 | # Remove sizeritem child if spacer |
1235 | if className == 'spacer' and xxx.className != 'spacer': | |
1236 | sizeritem = elem.parentNode | |
1237 | assert sizeritem.getAttribute('class') == 'sizeritem' | |
1238 | sizeritem.removeChild(elem) | |
1239 | elem.unlink() | |
1240 | elem = sizeritem | |
75aa1946 | 1241 | tree.GetPyData(selected).hasChild = False |
baba4aa5 RR |
1242 | elif xxx.className == 'spacer' and className != 'spacer': |
1243 | # Create sizeritem element | |
1244 | assert xxx.parent.isSizer | |
1245 | elem.setAttribute('class', 'sizeritem') | |
1246 | node = MakeEmptyDOM(className) | |
1247 | elem.appendChild(node) | |
1248 | # Replace to point to new object | |
1249 | xxx = xxxSizerItem(xxx.parent, elem) | |
1250 | elem = node | |
1251 | tree.SetPyData(selected, xxx) | |
1252 | xxx = xxx.child | |
1253 | else: | |
1254 | # Copy parameters present in dummy but not in elem | |
1255 | for node in dummy.childNodes: | |
1256 | if node.tagName not in tags: elem.appendChild(node.cloneNode(True)) | |
d14a1e28 | 1257 | dummy.unlink() |
baba4aa5 | 1258 | |
d14a1e28 | 1259 | # Change class name |
baba4aa5 | 1260 | elem.setAttribute('class', className) |
64bce500 RR |
1261 | if elem.hasAttribute('subclass'): |
1262 | elem.removeAttribute('subclass') # clear subclassing | |
d14a1e28 | 1263 | # Re-create xxx element |
baba4aa5 | 1264 | xxx = MakeXXXFromDOM(xxx.parent, elem) |
75aa1946 RR |
1265 | # Remove incompatible style flags |
1266 | if 'style' in xxx.params: | |
1267 | styles = map(string.strip, xxx.params['style'].value().split('|')) | |
1268 | newStyles = [s for s in styles if s in klass.winStyles or s in genericStyles] | |
1269 | if newStyles != styles: | |
1270 | if newStyles: | |
1271 | value = reduce(lambda a,b: a+'|'+b, newStyles) | |
1272 | else: | |
1273 | value = '' | |
1274 | xxx.params['style'].update(value) | |
1275 | ||
d14a1e28 RD |
1276 | # Update parent in child objects |
1277 | if tree.ItemHasChildren(selected): | |
a4c013b2 | 1278 | i, cookie = tree.GetFirstChild(selected) |
d14a1e28 RD |
1279 | while i.IsOk(): |
1280 | x = tree.GetPyData(i) | |
1281 | x.parent = xxx | |
1282 | if x.hasChild: x.child.parent = xxx | |
1283 | i, cookie = tree.GetNextChild(selected, cookie) | |
baba4aa5 | 1284 | |
d14a1e28 RD |
1285 | # Update tree |
1286 | if tree.GetPyData(selected).hasChild: # child container | |
1287 | container = tree.GetPyData(selected) | |
75aa1946 | 1288 | container.resetChild(xxx) |
baba4aa5 | 1289 | xxx = container |
d14a1e28 RD |
1290 | else: |
1291 | tree.SetPyData(selected, xxx) | |
1292 | tree.SetItemText(selected, xxx.treeName()) | |
1293 | tree.SetItemImage(selected, xxx.treeImage()) | |
1294 | ||
1295 | # Set default name for top-level windows | |
1296 | if parent.__class__ == xxxMainNode: | |
1297 | cl = xxx.treeObject().__class__ | |
1298 | frame.maxIDs[cl] += 1 | |
64b9ac75 | 1299 | xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl])) |
d14a1e28 RD |
1300 | |
1301 | # Update panel | |
1302 | g.panel.SetData(xxx) | |
1303 | # Update tools | |
1304 | g.tools.UpdateUI() | |
1305 | ||
57c48fc4 | 1306 | #undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) |
d14a1e28 RD |
1307 | # Update view? |
1308 | if g.testWin and tree.IsHighlatable(selected): | |
1309 | if conf.autoRefresh: | |
1310 | tree.needUpdate = True | |
1311 | tree.pendingHighLight = selected | |
1312 | else: | |
1313 | tree.pendingHighLight = None | |
1314 | tree.SetFocus() | |
6cb85701 | 1315 | self.SetModified() |
d14a1e28 RD |
1316 | |
1317 | # Expand/collapse subtree | |
1318 | def OnExpand(self, evt): | |
1319 | if tree.selection: tree.ExpandAll(tree.selection) | |
1320 | else: tree.ExpandAll(tree.root) | |
1321 | def OnCollapse(self, evt): | |
1322 | if tree.selection: tree.CollapseAll(tree.selection) | |
1323 | else: tree.CollapseAll(tree.root) | |
1324 | ||
1325 | def OnPullDownHighlight(self, evt): | |
1326 | menuId = evt.GetMenuId() | |
1327 | if menuId != -1: | |
1328 | menu = evt.GetEventObject() | |
1329 | help = menu.GetHelpString(menuId) | |
1330 | self.SetStatusText(help) | |
1331 | else: | |
1332 | self.SetStatusText('') | |
1333 | ||
1334 | def OnUpdateUI(self, evt): | |
29a41103 | 1335 | if evt.GetId() in [wx.ID_CUT, wx.ID_COPY, self.ID_DELETE]: |
d14a1e28 | 1336 | evt.Enable(tree.selection is not None and tree.selection != tree.root) |
29a41103 | 1337 | elif evt.GetId() == wx.ID_SAVE: |
6cb85701 | 1338 | evt.Enable(self.modified) |
29a41103 | 1339 | elif evt.GetId() in [wx.ID_PASTE, self.ID_TOOL_PASTE]: |
af52e185 | 1340 | evt.Enable(tree.selection is not None) |
d14a1e28 RD |
1341 | elif evt.GetId() == self.ID_TEST: |
1342 | evt.Enable(tree.selection is not None and tree.selection != tree.root) | |
016f67ba | 1343 | elif evt.GetId() in [self.ID_LOCATE, self.ID_TOOL_LOCATE]: |
64bce500 | 1344 | evt.Enable(g.testWin is not None) |
29a41103 RD |
1345 | elif evt.GetId() == wx.ID_UNDO: evt.Enable(undoMan.CanUndo()) |
1346 | elif evt.GetId() == wx.ID_REDO: evt.Enable(undoMan.CanRedo()) | |
d14a1e28 RD |
1347 | |
1348 | def OnIdle(self, evt): | |
1349 | if self.inIdle: return # Recursive call protection | |
1350 | self.inIdle = True | |
306b6fe9 RR |
1351 | try: |
1352 | if tree.needUpdate: | |
1353 | if conf.autoRefresh: | |
1354 | if g.testWin: | |
da7d0696 | 1355 | #self.SetStatusText('Refreshing test window...') |
306b6fe9 RR |
1356 | # (re)create |
1357 | tree.CreateTestWin(g.testWin.item) | |
da7d0696 | 1358 | #self.SetStatusText('') |
306b6fe9 RR |
1359 | tree.needUpdate = False |
1360 | elif tree.pendingHighLight: | |
1361 | try: | |
1362 | tree.HighLight(tree.pendingHighLight) | |
1363 | except: | |
1364 | # Remove highlight if any problem | |
09a68e88 | 1365 | if g.testWin and g.testWin.highLight: |
306b6fe9 RR |
1366 | g.testWin.highLight.Remove() |
1367 | tree.pendingHighLight = None | |
1368 | raise | |
1369 | else: | |
1370 | evt.Skip() | |
1371 | finally: | |
1372 | self.inIdle = False | |
d14a1e28 RD |
1373 | |
1374 | # We don't let close panel window | |
1375 | def OnCloseMiniFrame(self, evt): | |
1376 | return | |
1377 | ||
4483a6ed | 1378 | def OnIconize(self, evt): |
7df24e78 RR |
1379 | if evt.Iconized(): |
1380 | conf.x, conf.y = self.GetPosition() | |
1381 | conf.width, conf.height = self.GetSize() | |
1382 | if conf.embedPanel: | |
1383 | conf.sashPos = self.splitter.GetSashPosition() | |
1384 | else: | |
1385 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
1386 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
1387 | self.miniFrame.Iconize() | |
4483a6ed | 1388 | else: |
7df24e78 RR |
1389 | if not conf.embedPanel: |
1390 | self.miniFrame.Iconize(False) | |
4483a6ed RR |
1391 | evt.Skip() |
1392 | ||
d14a1e28 RD |
1393 | def OnCloseWindow(self, evt): |
1394 | if not self.AskSave(): return | |
1395 | if g.testWin: g.testWin.Destroy() | |
d14a1e28 RD |
1396 | if not panel.GetPageCount() == 2: |
1397 | panel.page2.Destroy() | |
80389ff7 RR |
1398 | else: |
1399 | # If we don't do this, page does not get destroyed (a bug?) | |
1400 | panel.RemovePage(1) | |
3429f8d0 RD |
1401 | if not self.IsIconized(): |
1402 | conf.x, conf.y = self.GetPosition() | |
1403 | conf.width, conf.height = self.GetSize() | |
1404 | if conf.embedPanel: | |
1405 | conf.sashPos = self.splitter.GetSashPosition() | |
1406 | else: | |
1407 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
1408 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
d14a1e28 RD |
1409 | evt.Skip() |
1410 | ||
73b2a9a7 RD |
1411 | def CreateLocalConf(self, path): |
1412 | name = os.path.splitext(path)[0] | |
1413 | name += '.xcfg' | |
1414 | return wx.FileConfig(localFilename=name) | |
1415 | ||
d14a1e28 RD |
1416 | def Clear(self): |
1417 | self.dataFile = '' | |
73b2a9a7 | 1418 | conf.localconf = None |
d14a1e28 | 1419 | undoMan.Clear() |
6cb85701 | 1420 | self.SetModified(False) |
d14a1e28 RD |
1421 | tree.Clear() |
1422 | panel.Clear() | |
1423 | if g.testWin: | |
1424 | g.testWin.Destroy() | |
1425 | g.testWin = None | |
d14a1e28 RD |
1426 | # Numbers for new controls |
1427 | self.maxIDs = {} | |
306b6fe9 RR |
1428 | for cl in [xxxPanel, xxxDialog, xxxFrame, |
1429 | xxxMenuBar, xxxMenu, xxxToolBar, | |
1430 | xxxWizard, xxxBitmap, xxxIcon]: | |
1431 | self.maxIDs[cl] = 0 | |
8c64c153 RR |
1432 | # Restore handlers, menu, etc. to initial |
1433 | setHandlers(self.handlers[:]) | |
1434 | g.pullDownMenu.custom = self.custom[:] | |
1435 | # Remove modules imported from comment directives | |
aea9f6d5 | 1436 | map(sys.modules.pop, [m for m in sys.modules if m not in self.modules]) |
7058b794 RR |
1437 | xxxParamComment.locals = {} # clear local namespace |
1438 | xxxParamComment.allow = None # clear execution state | |
d14a1e28 | 1439 | |
6cb85701 RR |
1440 | def SetModified(self, state=True): |
1441 | self.modified = state | |
1442 | name = os.path.basename(self.dataFile) | |
1443 | if not name: name = defaultName | |
1444 | if state: | |
1445 | self.SetTitle(progname + ': ' + name + ' *') | |
1446 | else: | |
1447 | self.SetTitle(progname + ': ' + name) | |
1448 | ||
d14a1e28 RD |
1449 | def Open(self, path): |
1450 | if not os.path.exists(path): | |
29a41103 | 1451 | wx.LogError('File does not exists: %s' % path) |
d14a1e28 RD |
1452 | return False |
1453 | # Try to read the file | |
1454 | try: | |
1455 | f = open(path) | |
1456 | self.Clear() | |
d14a1e28 | 1457 | dom = minidom.parse(f) |
d14a1e28 | 1458 | f.close() |
016f67ba RR |
1459 | # Set encoding global variable and default encoding |
1460 | if dom.encoding: | |
1461 | g.currentEncoding = dom.encoding | |
1462 | wx.SetDefaultPyEncoding(g.currentEncoding.encode()) | |
9a69d0aa RR |
1463 | else: |
1464 | g.currentEncoding = '' | |
d14a1e28 | 1465 | # Change dir |
fd919451 | 1466 | self.dataFile = path = os.path.abspath(path) |
d14a1e28 RD |
1467 | dir = os.path.dirname(path) |
1468 | if dir: os.chdir(dir) | |
c0d5ae74 RR |
1469 | # Allow importing modules from the same directory |
1470 | sys.path = sys_path + [dir] | |
d14a1e28 | 1471 | tree.SetData(dom) |
d14a1e28 | 1472 | self.SetTitle(progname + ': ' + os.path.basename(path)) |
73b2a9a7 | 1473 | conf.localconf = self.CreateLocalConf(self.dataFile) |
d14a1e28 RD |
1474 | except: |
1475 | # Nice exception printing | |
1476 | inf = sys.exc_info() | |
29a41103 RD |
1477 | wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1]) |
1478 | wx.LogError('Error reading file: %s' % path) | |
016f67ba | 1479 | if debug: raise |
d14a1e28 RD |
1480 | return False |
1481 | return True | |
1482 | ||
1483 | def Indent(self, node, indent = 0): | |
b372319f RR |
1484 | if node.nodeType == minidom.Node.COMMENT_NODE: |
1485 | text = self.domCopy.createTextNode('\n' + ' ' * indent) | |
1486 | node.parentNode.insertBefore(text, node) | |
1487 | return # no children | |
d14a1e28 RD |
1488 | # Copy child list because it will change soon |
1489 | children = node.childNodes[:] | |
1490 | # Main node doesn't need to be indented | |
1491 | if indent: | |
1492 | text = self.domCopy.createTextNode('\n' + ' ' * indent) | |
1493 | node.parentNode.insertBefore(text, node) | |
1494 | if children: | |
1495 | # Append newline after last child, except for text nodes | |
1496 | if children[-1].nodeType == minidom.Node.ELEMENT_NODE: | |
1497 | text = self.domCopy.createTextNode('\n' + ' ' * indent) | |
1498 | node.appendChild(text) | |
1499 | # Indent children which are elements | |
1500 | for n in children: | |
b372319f RR |
1501 | if n.nodeType == minidom.Node.ELEMENT_NODE or \ |
1502 | n.nodeType == minidom.Node.COMMENT_NODE: | |
d14a1e28 RD |
1503 | self.Indent(n, indent + 2) |
1504 | ||
1505 | def Save(self, path): | |
1506 | try: | |
7353d818 | 1507 | import codecs |
d14a1e28 RD |
1508 | # Apply changes |
1509 | if tree.selection and panel.IsModified(): | |
29a41103 | 1510 | self.OnRefresh(wx.CommandEvent()) |
016f67ba | 1511 | if g.currentEncoding: |
9a69d0aa | 1512 | f = codecs.open(path, 'wt', g.currentEncoding) |
016f67ba | 1513 | else: |
9a69d0aa | 1514 | f = codecs.open(path, 'wt') |
d14a1e28 RD |
1515 | # Make temporary copy for formatting it |
1516 | # !!! We can't clone dom node, it works only once | |
1517 | #self.domCopy = tree.dom.cloneNode(True) | |
1518 | self.domCopy = MyDocument() | |
1519 | mainNode = self.domCopy.appendChild(tree.mainNode.cloneNode(True)) | |
34b29ae7 RR |
1520 | # Remove first child (test element) |
1521 | testElem = mainNode.firstChild | |
1522 | mainNode.removeChild(testElem) | |
1523 | testElem.unlink() | |
d14a1e28 | 1524 | self.Indent(mainNode) |
7353d818 | 1525 | self.domCopy.writexml(f, encoding = g.currentEncoding) |
d14a1e28 RD |
1526 | f.close() |
1527 | self.domCopy.unlink() | |
1528 | self.domCopy = None | |
6cb85701 | 1529 | self.SetModified(False) |
d14a1e28 | 1530 | panel.SetModified(False) |
73b2a9a7 | 1531 | conf.localconf.Flush() |
d14a1e28 | 1532 | except: |
4eb5bfc6 | 1533 | inf = sys.exc_info() |
29a41103 RD |
1534 | wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1]) |
1535 | wx.LogError('Error writing file: %s' % path) | |
d14a1e28 | 1536 | raise |
73b2a9a7 | 1537 | |
d14a1e28 RD |
1538 | def AskSave(self): |
1539 | if not (self.modified or panel.IsModified()): return True | |
29a41103 RD |
1540 | flags = wx.ICON_EXCLAMATION | wx.YES_NO | wx.CANCEL | wx.CENTRE |
1541 | dlg = wx.MessageDialog( self, 'File is modified. Save before exit?', | |
d14a1e28 RD |
1542 | 'Save before too late?', flags ) |
1543 | say = dlg.ShowModal() | |
1544 | dlg.Destroy() | |
29a41103 RD |
1545 | wx.Yield() |
1546 | if say == wx.ID_YES: | |
1547 | self.OnSaveOrSaveAs(wx.CommandEvent(wx.ID_SAVE)) | |
d14a1e28 RD |
1548 | # If save was successful, modified flag is unset |
1549 | if not self.modified: return True | |
29a41103 | 1550 | elif say == wx.ID_NO: |
6cb85701 | 1551 | self.SetModified(False) |
d14a1e28 RD |
1552 | panel.SetModified(False) |
1553 | return True | |
1554 | return False | |
1555 | ||
d14a1e28 RD |
1556 | ################################################################################ |
1557 | ||
73b2a9a7 RD |
1558 | class PythonOptions(wx.Dialog): |
1559 | ||
1560 | def __init__(self, parent, cfg, dataFile): | |
1561 | pre = wx.PreDialog() | |
1562 | g.frame.res.LoadOnDialog(pre, parent, "PYTHON_OPTIONS") | |
1563 | self.PostCreate(pre) | |
1564 | ||
1565 | self.cfg = cfg | |
1566 | self.dataFile = dataFile | |
1567 | ||
29a41103 RD |
1568 | self.AutoGenerateCB = xrc.XRCCTRL(self, "AutoGenerateCB") |
1569 | self.EmbedCB = xrc.XRCCTRL(self, "EmbedCB") | |
1570 | self.GettextCB = xrc.XRCCTRL(self, "GettextCB") | |
1571 | self.MakeXRSFileCB = xrc.XRCCTRL(self, "MakeXRSFileCB") | |
1572 | self.FileNameTC = xrc.XRCCTRL(self, "FileNameTC") | |
1573 | self.BrowseBtn = xrc.XRCCTRL(self, "BrowseBtn") | |
1574 | self.GenerateBtn = xrc.XRCCTRL(self, "GenerateBtn") | |
1575 | self.SaveOptsBtn = xrc.XRCCTRL(self, "SaveOptsBtn") | |
73b2a9a7 RD |
1576 | |
1577 | self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.BrowseBtn) | |
1578 | self.Bind(wx.EVT_BUTTON, self.OnGenerate, self.GenerateBtn) | |
1579 | self.Bind(wx.EVT_BUTTON, self.OnSaveOpts, self.SaveOptsBtn) | |
1580 | ||
1581 | if self.cfg.Read("filename", "") != "": | |
1582 | self.FileNameTC.SetValue(self.cfg.Read("filename")) | |
1583 | else: | |
93894584 | 1584 | name = os.path.splitext(os.path.split(dataFile)[1])[0] |
73b2a9a7 RD |
1585 | name += '_xrc.py' |
1586 | self.FileNameTC.SetValue(name) | |
1587 | self.AutoGenerateCB.SetValue(self.cfg.ReadBool("autogenerate", False)) | |
1588 | self.EmbedCB.SetValue(self.cfg.ReadBool("embedResource", False)) | |
1589 | self.MakeXRSFileCB.SetValue(self.cfg.ReadBool("makeXRS", False)) | |
1590 | self.GettextCB.SetValue(self.cfg.ReadBool("genGettext", False)) | |
1591 | ||
1592 | ||
1593 | def OnBrowse(self, evt): | |
1594 | path = self.FileNameTC.GetValue() | |
1595 | dirname = os.path.abspath(os.path.dirname(path)) | |
1596 | name = os.path.split(path)[1] | |
29a41103 RD |
1597 | dlg = wx.FileDialog(self, 'Save As', dirname, name, '*.py', |
1598 | wx.SAVE | wx.OVERWRITE_PROMPT) | |
1599 | if dlg.ShowModal() == wx.ID_OK: | |
73b2a9a7 RD |
1600 | path = dlg.GetPath() |
1601 | self.FileNameTC.SetValue(path) | |
1602 | dlg.Destroy() | |
1603 | ||
1604 | ||
1605 | def OnGenerate(self, evt): | |
1606 | pypath = self.FileNameTC.GetValue() | |
1607 | embed = self.EmbedCB.GetValue() | |
7e05216c RD |
1608 | genGettext = self.GettextCB.GetValue() |
1609 | frame.GeneratePython(self.dataFile, pypath, embed, genGettext) | |
73b2a9a7 RD |
1610 | self.OnSaveOpts() |
1611 | ||
1612 | ||
1613 | def OnSaveOpts(self, evt=None): | |
1614 | self.cfg.Write("filename", self.FileNameTC.GetValue()) | |
1615 | self.cfg.WriteBool("autogenerate", self.AutoGenerateCB.GetValue()) | |
1616 | self.cfg.WriteBool("embedResource", self.EmbedCB.GetValue()) | |
1617 | self.cfg.WriteBool("makeXRS", self.MakeXRSFileCB.GetValue()) | |
1618 | self.cfg.WriteBool("genGettext", self.GettextCB.GetValue()) | |
1619 | ||
1620 | self.EndModal(wx.ID_OK) | |
1621 | ||
5050b626 RR |
1622 | ################################################################################ |
1623 | ||
1624 | class PrefsDialog(wx.Dialog): | |
1625 | ||
1626 | def __init__(self, parent): | |
1627 | pre = wx.PreDialog() | |
1628 | g.frame.res.LoadOnDialog(pre, parent, "DIALOG_PREFS") | |
1629 | self.PostCreate(pre) | |
1630 | self.checkControls = {} # map of check IDs to (control,dict,param) | |
1631 | ||
cbfc9df6 RD |
1632 | ##xxx = sys.modules['xxx'] |
1633 | import xxx | |
5050b626 RR |
1634 | d = xxx.xxxSizerItem.defaults_panel |
1635 | ||
1636 | self.check_proportion_panel = xrc.XRCCTRL(self, 'check_proportion_panel') | |
1637 | id = self.check_proportion_panel.GetId() | |
1638 | wx.EVT_CHECKBOX(self, id, self.OnCheck) | |
1639 | self.checkControls[id] = (xrc.XRCCTRL(self, 'spin_proportion_panel'), | |
1640 | d, 'option') | |
1641 | ||
1642 | self.check_flag_panel = xrc.XRCCTRL(self, 'check_flag_panel') | |
1643 | id = self.check_flag_panel.GetId() | |
1644 | wx.EVT_CHECKBOX(self, id, self.OnCheck) | |
1645 | self.checkControls[id] = (xrc.XRCCTRL(self, 'text_flag_panel'), | |
1646 | d, 'flag') | |
1647 | ||
1648 | d = xxx.xxxSizerItem.defaults_control | |
1649 | ||
1650 | self.check_proportion_panel = xrc.XRCCTRL(self, 'check_proportion_control') | |
1651 | id = self.check_proportion_panel.GetId() | |
1652 | wx.EVT_CHECKBOX(self, id, self.OnCheck) | |
1653 | self.checkControls[id] = (xrc.XRCCTRL(self, 'spin_proportion_control'), | |
1654 | d, 'option') | |
1655 | ||
1656 | self.check_flag_panel = xrc.XRCCTRL(self, 'check_flag_control') | |
1657 | id = self.check_flag_panel.GetId() | |
1658 | wx.EVT_CHECKBOX(self, id, self.OnCheck) | |
1659 | self.checkControls[id] = (xrc.XRCCTRL(self, 'text_flag_control'), | |
1660 | d, 'flag') | |
1661 | ||
1662 | for id,cdp in self.checkControls.items(): | |
1663 | c,d,p = cdp | |
1664 | try: | |
1665 | if isinstance(c, wx.SpinCtrl): | |
1666 | c.SetValue(int(d[p])) | |
1667 | else: | |
1668 | c.SetValue(d[p]) | |
1669 | self.FindWindowById(id).SetValue(True) | |
1670 | except KeyError: | |
1671 | c.Enable(False) | |
1672 | ||
7058b794 RR |
1673 | self.radio_allow_exec = xrc.XRCCTRL(self, 'radio_allow_exec') |
1674 | try: | |
1675 | radio = {'ask': 0, 'yes':1, 'no':2}[g.conf.allowExec] | |
1676 | except KeyError: | |
1677 | radio = 0 | |
1678 | self.radio_allow_exec.SetSelection(radio) | |
1679 | ||
5050b626 RR |
1680 | def OnCheck(self, evt): |
1681 | self.checkControls[evt.GetId()][0].Enable(evt.IsChecked()) | |
1682 | evt.Skip() | |
1683 | ||
73b2a9a7 RD |
1684 | ################################################################################ |
1685 | ||
c1dda21b RR |
1686 | # Parse string in form var1=val1[,var2=val2]* as dictionary |
1687 | def ReadDictFromString(s): | |
1688 | d = {} | |
1689 | for vv in s.split(','): | |
1690 | var,val = vv.split(':') | |
1691 | d[var.strip()] = val | |
1692 | return d | |
1693 | ||
1694 | # Transform dictionary with strings into one string | |
1695 | def DictToString(d): | |
1696 | return ','.join(map(':'.join, d.items())) | |
1697 | ||
d14a1e28 RD |
1698 | def usage(): |
1699 | print >> sys.stderr, 'usage: xrced [-dhiv] [file]' | |
1700 | ||
29a41103 | 1701 | class App(wx.App): |
d14a1e28 | 1702 | def OnInit(self): |
306b6fe9 | 1703 | # Check version |
29a41103 RD |
1704 | if wx.VERSION[:3] < MinWxVersion: |
1705 | wx.LogWarning('''\ | |
1706 | This version of XRCed may not work correctly on your version of wxWidgets. \ | |
1707 | Please upgrade wxWidgets to %d.%d.%d or higher.''' % MinWxVersion) | |
d14a1e28 RD |
1708 | global debug |
1709 | # Process comand-line | |
364a2be0 | 1710 | opts = args = None |
d14a1e28 RD |
1711 | try: |
1712 | opts, args = getopt.getopt(sys.argv[1:], 'dhiv') | |
1c01bc8e RD |
1713 | for o,a in opts: |
1714 | if o == '-h': | |
1715 | usage() | |
1716 | sys.exit(0) | |
1717 | elif o == '-d': | |
1718 | debug = True | |
1719 | elif o == '-v': | |
1720 | print 'XRCed version', version | |
1721 | sys.exit(0) | |
1722 | ||
d14a1e28 | 1723 | except getopt.GetoptError: |
29a41103 | 1724 | if wx.Platform != '__WXMAC__': # macs have some extra parameters |
d14a1e28 RD |
1725 | print >> sys.stderr, 'Unknown option' |
1726 | usage() | |
1727 | sys.exit(1) | |
d14a1e28 RD |
1728 | |
1729 | self.SetAppName('xrced') | |
1730 | # Settings | |
1731 | global conf | |
29a41103 | 1732 | conf = g.conf = wx.Config(style = wx.CONFIG_USE_LOCAL_FILE) |
73b2a9a7 | 1733 | conf.localconf = None |
d14a1e28 RD |
1734 | conf.autoRefresh = conf.ReadInt('autorefresh', True) |
1735 | pos = conf.ReadInt('x', -1), conf.ReadInt('y', -1) | |
1736 | size = conf.ReadInt('width', 800), conf.ReadInt('height', 600) | |
1737 | conf.embedPanel = conf.ReadInt('embedPanel', True) | |
1738 | conf.showTools = conf.ReadInt('showTools', True) | |
1739 | conf.sashPos = conf.ReadInt('sashPos', 200) | |
52cd0643 | 1740 | |
80389ff7 | 1741 | # read recently used files |
52cd0643 RD |
1742 | g.fileHistory = wx.FileHistory() |
1743 | g.fileHistory.Load(conf) | |
1744 | ||
d14a1e28 RD |
1745 | if not conf.embedPanel: |
1746 | conf.panelX = conf.ReadInt('panelX', -1) | |
1747 | conf.panelY = conf.ReadInt('panelY', -1) | |
1748 | else: | |
1749 | conf.panelX = conf.panelY = -1 | |
1750 | conf.panelWidth = conf.ReadInt('panelWidth', 200) | |
1751 | conf.panelHeight = conf.ReadInt('panelHeight', 200) | |
1752 | conf.panic = not conf.HasEntry('nopanic') | |
c1dda21b | 1753 | # Preferences |
7058b794 | 1754 | conf.allowExec = conf.Read('Prefs/allowExec', 'ask') |
c1dda21b | 1755 | p = 'Prefs/sizeritem_defaults_panel' |
cbfc9df6 | 1756 | import xxx |
c1dda21b | 1757 | if conf.HasEntry(p): |
cbfc9df6 RD |
1758 | ##sys.modules['xxx'].xxxSizerItem.defaults_panel = ReadDictFromString(conf.Read(p)) |
1759 | xxx.xxxSizerItem.defaults_panel = ReadDictFromString(conf.Read(p)) | |
c1dda21b RR |
1760 | p = 'Prefs/sizeritem_defaults_control' |
1761 | if conf.HasEntry(p): | |
cbfc9df6 RD |
1762 | ##sys.modules['xxx'].xxxSizerItem.defaults_control = ReadDictFromString(conf.Read(p)) |
1763 | xxx.xxxSizerItem.defaults_control = ReadDictFromString(conf.Read(p)) | |
1764 | ||
d14a1e28 | 1765 | # Add handlers |
29a41103 | 1766 | wx.FileSystem.AddHandler(wx.MemoryFSHandler()) |
d14a1e28 RD |
1767 | # Create main frame |
1768 | frame = Frame(pos, size) | |
1769 | frame.Show(True) | |
8c64c153 RR |
1770 | |
1771 | # Load plugins | |
1772 | plugins = os.getenv('XRCEDPATH') | |
1773 | if plugins: | |
1774 | cwd = os.getcwd() | |
1775 | try: | |
1776 | for dir in plugins.split(':'): | |
a023b456 RR |
1777 | if os.path.isdir(dir) and \ |
1778 | os.path.isfile(os.path.join(dir, '__init__.py')): | |
8c64c153 RR |
1779 | # Normalize |
1780 | dir = os.path.abspath(os.path.normpath(dir)) | |
1781 | sys.path = sys_path + [os.path.dirname(dir)] | |
1782 | try: | |
1783 | os.chdir(dir) | |
1784 | __import__(os.path.basename(dir), globals(), locals(), ['*']) | |
1785 | except: | |
1786 | print traceback.print_exc() | |
1787 | finally: | |
1788 | os.chdir(cwd) | |
1789 | # Store important data | |
1790 | frame.handlers = getHandlers()[:] | |
1791 | frame.custom = g.pullDownMenu.custom[:] | |
da7d0696 | 1792 | frame.modules = sys.modules.copy() |
8c64c153 RR |
1793 | |
1794 | # Initialize | |
1795 | frame.Clear() | |
d14a1e28 RD |
1796 | |
1797 | # Load file after showing | |
1798 | if args: | |
1799 | conf.panic = False | |
1800 | frame.open = frame.Open(args[0]) | |
1801 | ||
1802 | return True | |
1803 | ||
1804 | def OnExit(self): | |
1805 | # Write config | |
1806 | global conf | |
73b2a9a7 | 1807 | wc = conf |
d14a1e28 RD |
1808 | wc.WriteInt('autorefresh', conf.autoRefresh) |
1809 | wc.WriteInt('x', conf.x) | |
1810 | wc.WriteInt('y', conf.y) | |
1811 | wc.WriteInt('width', conf.width) | |
1812 | wc.WriteInt('height', conf.height) | |
1813 | wc.WriteInt('embedPanel', conf.embedPanel) | |
1814 | wc.WriteInt('showTools', conf.showTools) | |
1815 | if not conf.embedPanel: | |
1816 | wc.WriteInt('panelX', conf.panelX) | |
1817 | wc.WriteInt('panelY', conf.panelY) | |
1818 | wc.WriteInt('sashPos', conf.sashPos) | |
1819 | wc.WriteInt('panelWidth', conf.panelWidth) | |
1820 | wc.WriteInt('panelHeight', conf.panelHeight) | |
7058b794 | 1821 | wc.WriteInt('nopanic', 1) |
52cd0643 | 1822 | g.fileHistory.Save(wc) |
c1dda21b | 1823 | # Preferences |
5050b626 | 1824 | wc.DeleteGroup('Prefs') |
7058b794 | 1825 | wc.Write('Prefs/allowExec', conf.allowExec) |
cbfc9df6 RD |
1826 | import xxx |
1827 | ##v = sys.modules['xxx'].xxxSizerItem.defaults_panel | |
1828 | v = xxx.xxxSizerItem.defaults_panel | |
c1dda21b | 1829 | if v: wc.Write('Prefs/sizeritem_defaults_panel', DictToString(v)) |
cbfc9df6 RD |
1830 | ###v = sys.modules['xxx'].xxxSizerItem.defaults_control |
1831 | v = xxx.xxxSizerItem.defaults_control | |
c1dda21b | 1832 | if v: wc.Write('Prefs/sizeritem_defaults_control', DictToString(v)) |
7058b794 | 1833 | |
d14a1e28 RD |
1834 | wc.Flush() |
1835 | ||
1836 | def main(): | |
1837 | app = App(0, useBestVisual=False) | |
29a41103 | 1838 | #app.SetAssertMode(wx.PYAPP_ASSERT_LOG) |
d14a1e28 RD |
1839 | app.MainLoop() |
1840 | app.OnExit() | |
1841 | global conf | |
1842 | del conf | |
1843 | ||
1844 | if __name__ == '__main__': | |
1845 | main() |