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