]>
Commit | Line | Data |
---|---|---|
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$ | |
6 | ||
7 | """ | |
8 | ||
9 | xrced -- Simple resource editor for XRC format used by wxWindows/wxPython | |
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 | ||
23 | from globals import * | |
24 | import os, sys, getopt, re, traceback, tempfile, shutil, cPickle | |
25 | from xml.parsers import expat | |
26 | ||
27 | # Local modules | |
28 | from tree import * # imports xxx which imports params | |
29 | from panel import * | |
30 | from tools import * | |
31 | # Cleanup recursive import sideeffects, otherwise we can't create undoMan | |
32 | import undo | |
33 | undo.ParamPage = ParamPage | |
34 | undoMan = g.undoMan = UndoManager() | |
35 | ||
36 | # Set application path for loading resources | |
37 | if __name__ == '__main__': | |
38 | basePath = os.path.dirname(sys.argv[0]) | |
39 | else: | |
40 | basePath = os.path.dirname(__file__) | |
41 | ||
42 | # 1 adds CMD command to Help menu | |
43 | debug = 0 | |
44 | ||
45 | g.helpText = """\ | |
46 | <HTML><H2>Welcome to XRC<font color="blue">ed</font></H2><H3><font color="green">DON'T PANIC :)</font></H3> | |
47 | Read this note before clicking on anything!<P> | |
48 | To start select tree root, then popup menu with your right mouse button, | |
49 | select "Append Child", and then any command.<P> | |
50 | Or just press one of the buttons on the tools palette.<P> | |
51 | Enter XML ID, change properties, create children.<P> | |
52 | To test your interface select Test command (View menu).<P> | |
53 | Consult README file for the details.</HTML> | |
54 | """ | |
55 | ||
56 | defaultIDs = {xxxPanel:'PANEL', xxxDialog:'DIALOG', xxxFrame:'FRAME', | |
57 | xxxMenuBar:'MENUBAR', xxxMenu:'MENU', xxxToolBar:'TOOLBAR', | |
58 | xxxWizard:'WIZARD', xxxBitmap:'BITMAP', xxxIcon:'ICON'} | |
59 | ||
60 | defaultName = 'UNTITLED.xrc' | |
61 | ||
62 | ################################################################################ | |
63 | ||
64 | # ScrolledMessageDialog - modified from wxPython lib to set fixed-width font | |
65 | class ScrolledMessageDialog(wxDialog): | |
66 | def __init__(self, parent, msg, caption, pos = wxDefaultPosition, size = (500,300)): | |
67 | from wxPython.lib.layoutf import Layoutf | |
68 | wxDialog.__init__(self, parent, -1, caption, pos, size) | |
69 | text = wxTextCtrl(self, -1, msg, wxDefaultPosition, | |
70 | wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY) | |
71 | text.SetFont(g.modernFont()) | |
72 | dc = wxWindowDC(text) | |
73 | # !!! possible bug - GetTextExtent without font returns sysfont dims | |
74 | w, h = dc.GetFullTextExtent(' ', g.modernFont())[:2] | |
75 | ok = wxButton(self, wxID_OK, "OK") | |
76 | text.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok))) | |
77 | text.SetSize((w * 80 + 30, h * 40)) | |
78 | text.ShowPosition(1) | |
79 | ok.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,))) | |
80 | self.SetAutoLayout(True) | |
81 | self.Fit() | |
82 | self.CenterOnScreen(wxBOTH) | |
83 | ||
84 | ################################################################################ | |
85 | ||
86 | # Event handler for using during location | |
87 | class Locator(wxEvtHandler): | |
88 | def ProcessEvent(self, evt): | |
89 | print evt | |
90 | ||
91 | class Frame(wxFrame): | |
92 | def __init__(self, pos, size): | |
93 | wxFrame.__init__(self, None, -1, '', pos, size) | |
94 | global frame | |
95 | frame = g.frame = self | |
96 | bar = self.CreateStatusBar(2) | |
97 | bar.SetStatusWidths([-1, 40]) | |
98 | self.SetIcon(images.getIconIcon()) | |
99 | ||
100 | # Idle flag | |
101 | self.inIdle = False | |
102 | ||
103 | # Load our own resources | |
104 | self.res = wxXmlResource('') | |
105 | # !!! Blocking of assert failure occurring in older unicode builds | |
106 | try: | |
107 | quietlog = wx.LogNull() | |
108 | self.res.Load(os.path.join(basePath, 'xrced.xrc')) | |
109 | except wx._core.PyAssertionError: | |
110 | print 'PyAssertionError was ignored' | |
111 | ||
112 | # Make menus | |
113 | menuBar = wxMenuBar() | |
114 | ||
115 | menu = wxMenu() | |
116 | menu.Append(wxID_NEW, '&New\tCtrl-N', 'New file') | |
117 | menu.AppendSeparator() | |
118 | menu.Append(wxID_OPEN, '&Open...\tCtrl-O', 'Open XRC file') | |
119 | self.recentMenu = wxMenu() | |
120 | self.AppendRecent(self.recentMenu) | |
121 | menu.AppendMenu(-1, 'Open Recent', self.recentMenu, 'Open a recent file') | |
122 | menu.AppendSeparator() | |
123 | menu.Append(wxID_SAVE, '&Save\tCtrl-S', 'Save XRC file') | |
124 | menu.Append(wxID_SAVEAS, 'Save &As...', 'Save XRC file under different name') | |
125 | self.ID_GENERATE_PYTHON = wxNewId() | |
126 | menu.Append(self.ID_GENERATE_PYTHON, '&Generate Python...', | |
127 | 'Generate a Python module that uses this XRC') | |
128 | menu.AppendSeparator() | |
129 | menu.Append(wxID_EXIT, '&Quit\tCtrl-Q', 'Exit application') | |
130 | ||
131 | menuBar.Append(menu, '&File') | |
132 | ||
133 | menu = wxMenu() | |
134 | menu.Append(wxID_UNDO, '&Undo\tCtrl-Z', 'Undo') | |
135 | menu.Append(wxID_REDO, '&Redo\tCtrl-Y', 'Redo') | |
136 | menu.AppendSeparator() | |
137 | menu.Append(wxID_CUT, 'Cut\tCtrl-X', 'Cut to the clipboard') | |
138 | menu.Append(wxID_COPY, '&Copy\tCtrl-C', 'Copy to the clipboard') | |
139 | menu.Append(wxID_PASTE, '&Paste\tCtrl-V', 'Paste from the clipboard') | |
140 | self.ID_DELETE = wxNewId() | |
141 | menu.Append(self.ID_DELETE, '&Delete\tCtrl-D', 'Delete object') | |
142 | menu.AppendSeparator() | |
143 | self.ID_LOCATE = wxNewId() | |
144 | self.ID_TOOL_LOCATE = wxNewId() | |
145 | self.ID_TOOL_PASTE = wxNewId() | |
146 | menu.Append(self.ID_LOCATE, '&Locate\tCtrl-L', 'Locate control in test window and select it') | |
147 | menuBar.Append(menu, '&Edit') | |
148 | ||
149 | menu = wxMenu() | |
150 | self.ID_EMBED_PANEL = wxNewId() | |
151 | menu.Append(self.ID_EMBED_PANEL, '&Embed Panel', | |
152 | 'Toggle embedding properties panel in the main window', True) | |
153 | menu.Check(self.ID_EMBED_PANEL, conf.embedPanel) | |
154 | self.ID_SHOW_TOOLS = wxNewId() | |
155 | menu.Append(self.ID_SHOW_TOOLS, 'Show &Tools', 'Toggle tools', True) | |
156 | menu.Check(self.ID_SHOW_TOOLS, conf.showTools) | |
157 | menu.AppendSeparator() | |
158 | self.ID_TEST = wxNewId() | |
159 | menu.Append(self.ID_TEST, '&Test\tF5', 'Show test window') | |
160 | self.ID_REFRESH = wxNewId() | |
161 | menu.Append(self.ID_REFRESH, '&Refresh\tCtrl-R', 'Refresh test window') | |
162 | self.ID_AUTO_REFRESH = wxNewId() | |
163 | menu.Append(self.ID_AUTO_REFRESH, '&Auto-refresh\tCtrl-A', | |
164 | 'Toggle auto-refresh mode', True) | |
165 | menu.Check(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
166 | self.ID_TEST_HIDE = wxNewId() | |
167 | menu.Append(self.ID_TEST_HIDE, '&Hide\tCtrl-H', 'Close test window') | |
168 | menuBar.Append(menu, '&View') | |
169 | ||
170 | menu = wxMenu() | |
171 | menu.Append(wxID_ABOUT, '&About...', 'About XCRed') | |
172 | self.ID_README = wxNewId() | |
173 | menu.Append(self.ID_README, '&Readme...', 'View the README file') | |
174 | if debug: | |
175 | self.ID_DEBUG_CMD = wxNewId() | |
176 | menu.Append(self.ID_DEBUG_CMD, 'CMD', 'Python command line') | |
177 | EVT_MENU(self, self.ID_DEBUG_CMD, self.OnDebugCMD) | |
178 | menuBar.Append(menu, '&Help') | |
179 | ||
180 | self.menuBar = menuBar | |
181 | self.SetMenuBar(menuBar) | |
182 | ||
183 | # Create toolbar | |
184 | tb = self.CreateToolBar(wxTB_HORIZONTAL | wxNO_BORDER | wxTB_FLAT) | |
185 | tb.SetToolBitmapSize((24,24)) | |
186 | new_bmp = wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_TOOLBAR) | |
187 | open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR) | |
188 | save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR) | |
189 | undo_bmp = wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR) | |
190 | redo_bmp = wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR) | |
191 | cut_bmp = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) | |
192 | copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR) | |
193 | paste_bmp= wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR) | |
194 | ||
195 | tb.AddSimpleTool(wxID_NEW, new_bmp, 'New', 'New file') | |
196 | tb.AddSimpleTool(wxID_OPEN, open_bmp, 'Open', 'Open file') | |
197 | tb.AddSimpleTool(wxID_SAVE, save_bmp, 'Save', 'Save file') | |
198 | tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL)) | |
199 | tb.AddSimpleTool(wxID_UNDO, undo_bmp, 'Undo', 'Undo') | |
200 | tb.AddSimpleTool(wxID_REDO, redo_bmp, 'Redo', 'Redo') | |
201 | tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL)) | |
202 | tb.AddSimpleTool(wxID_CUT, cut_bmp, 'Cut', 'Cut') | |
203 | tb.AddSimpleTool(wxID_COPY, copy_bmp, 'Copy', 'Copy') | |
204 | tb.AddSimpleTool(self.ID_TOOL_PASTE, paste_bmp, 'Paste', 'Paste') | |
205 | tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL)) | |
206 | tb.AddSimpleTool(self.ID_TOOL_LOCATE, | |
207 | images.getLocateBitmap(), #images.getLocateArmedBitmap(), | |
208 | 'Locate', 'Locate control in test window and select it', True) | |
209 | tb.AddControl(wxStaticLine(tb, -1, size=(-1,23), style=wxLI_VERTICAL)) | |
210 | tb.AddSimpleTool(self.ID_TEST, images.getTestBitmap(), 'Test', 'Test window') | |
211 | tb.AddSimpleTool(self.ID_REFRESH, images.getRefreshBitmap(), | |
212 | 'Refresh', 'Refresh view') | |
213 | tb.AddSimpleTool(self.ID_AUTO_REFRESH, images.getAutoRefreshBitmap(), | |
214 | 'Auto-refresh', 'Toggle auto-refresh mode', True) | |
215 | # if wxPlatform == '__WXGTK__': | |
216 | # tb.AddSeparator() # otherwise auto-refresh sticks in status line | |
217 | tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
218 | tb.Realize() | |
219 | ||
220 | self.tb = tb | |
221 | self.minWidth = tb.GetSize()[0] # minimal width is the size of toolbar | |
222 | ||
223 | # File | |
224 | EVT_MENU(self, wxID_NEW, self.OnNew) | |
225 | EVT_MENU(self, wxID_OPEN, self.OnOpen) | |
226 | EVT_MENU(self, wxID_SAVE, self.OnSaveOrSaveAs) | |
227 | EVT_MENU(self, wxID_SAVEAS, self.OnSaveOrSaveAs) | |
228 | EVT_MENU(self, self.ID_GENERATE_PYTHON, self.OnGeneratePython) | |
229 | EVT_MENU(self, wxID_EXIT, self.OnExit) | |
230 | # Edit | |
231 | EVT_MENU(self, wxID_UNDO, self.OnUndo) | |
232 | EVT_MENU(self, wxID_REDO, self.OnRedo) | |
233 | EVT_MENU(self, wxID_CUT, self.OnCutDelete) | |
234 | EVT_MENU(self, wxID_COPY, self.OnCopy) | |
235 | EVT_MENU(self, wxID_PASTE, self.OnPaste) | |
236 | EVT_MENU(self, self.ID_TOOL_PASTE, self.OnPaste) | |
237 | EVT_MENU(self, self.ID_DELETE, self.OnCutDelete) | |
238 | EVT_MENU(self, self.ID_LOCATE, self.OnLocate) | |
239 | EVT_MENU(self, self.ID_TOOL_LOCATE, self.OnLocate) | |
240 | # View | |
241 | EVT_MENU(self, self.ID_EMBED_PANEL, self.OnEmbedPanel) | |
242 | EVT_MENU(self, self.ID_SHOW_TOOLS, self.OnShowTools) | |
243 | EVT_MENU(self, self.ID_TEST, self.OnTest) | |
244 | EVT_MENU(self, self.ID_REFRESH, self.OnRefresh) | |
245 | EVT_MENU(self, self.ID_AUTO_REFRESH, self.OnAutoRefresh) | |
246 | EVT_MENU(self, self.ID_TEST_HIDE, self.OnTestHide) | |
247 | # Help | |
248 | EVT_MENU(self, wxID_ABOUT, self.OnAbout) | |
249 | EVT_MENU(self, self.ID_README, self.OnReadme) | |
250 | ||
251 | # Update events | |
252 | EVT_UPDATE_UI(self, wxID_SAVE, self.OnUpdateUI) | |
253 | EVT_UPDATE_UI(self, wxID_CUT, self.OnUpdateUI) | |
254 | EVT_UPDATE_UI(self, wxID_COPY, self.OnUpdateUI) | |
255 | EVT_UPDATE_UI(self, wxID_PASTE, self.OnUpdateUI) | |
256 | EVT_UPDATE_UI(self, self.ID_LOCATE, self.OnUpdateUI) | |
257 | EVT_UPDATE_UI(self, self.ID_TOOL_LOCATE, self.OnUpdateUI) | |
258 | EVT_UPDATE_UI(self, self.ID_TOOL_PASTE, self.OnUpdateUI) | |
259 | EVT_UPDATE_UI(self, wxID_UNDO, self.OnUpdateUI) | |
260 | EVT_UPDATE_UI(self, wxID_REDO, self.OnUpdateUI) | |
261 | EVT_UPDATE_UI(self, self.ID_DELETE, self.OnUpdateUI) | |
262 | EVT_UPDATE_UI(self, self.ID_TEST, self.OnUpdateUI) | |
263 | EVT_UPDATE_UI(self, self.ID_REFRESH, self.OnUpdateUI) | |
264 | ||
265 | # Build interface | |
266 | sizer = wxBoxSizer(wxVERTICAL) | |
267 | sizer.Add(wxStaticLine(self, -1), 0, wxEXPAND) | |
268 | # Horizontal sizer for toolbar and splitter | |
269 | self.toolsSizer = sizer1 = wxBoxSizer() | |
270 | splitter = wxSplitterWindow(self, -1, style=wxSP_3DSASH) | |
271 | self.splitter = splitter | |
272 | splitter.SetMinimumPaneSize(100) | |
273 | # Create tree | |
274 | global tree | |
275 | g.tree = tree = XML_Tree(splitter, -1) | |
276 | ||
277 | # Init pull-down menu data | |
278 | global pullDownMenu | |
279 | g.pullDownMenu = pullDownMenu = PullDownMenu(self) | |
280 | ||
281 | # Vertical toolbar for GUI buttons | |
282 | g.tools = tools = Tools(self) | |
283 | tools.Show(conf.showTools) | |
284 | if conf.showTools: sizer1.Add(tools, 0, wxEXPAND) | |
285 | ||
286 | tree.RegisterKeyEvents() | |
287 | ||
288 | # !!! frame styles are broken | |
289 | # Miniframe for not embedded mode | |
290 | miniFrame = wxFrame(self, -1, 'Properties & Style', | |
291 | (conf.panelX, conf.panelY), | |
292 | (conf.panelWidth, conf.panelHeight)) | |
293 | self.miniFrame = miniFrame | |
294 | sizer2 = wxBoxSizer() | |
295 | miniFrame.SetAutoLayout(True) | |
296 | miniFrame.SetSizer(sizer2) | |
297 | EVT_CLOSE(self.miniFrame, self.OnCloseMiniFrame) | |
298 | # Create panel for parameters | |
299 | global panel | |
300 | if conf.embedPanel: | |
301 | panel = Panel(splitter) | |
302 | # Set plitter windows | |
303 | splitter.SplitVertically(tree, panel, conf.sashPos) | |
304 | else: | |
305 | panel = Panel(miniFrame) | |
306 | sizer2.Add(panel, 1, wxEXPAND) | |
307 | miniFrame.Show(True) | |
308 | splitter.Initialize(tree) | |
309 | sizer1.Add(splitter, 1, wxEXPAND) | |
310 | sizer.Add(sizer1, 1, wxEXPAND) | |
311 | self.SetAutoLayout(True) | |
312 | self.SetSizer(sizer) | |
313 | ||
314 | # Initialize | |
315 | self.Clear() | |
316 | ||
317 | # Other events | |
318 | EVT_IDLE(self, self.OnIdle) | |
319 | EVT_CLOSE(self, self.OnCloseWindow) | |
320 | EVT_KEY_DOWN(self, tools.OnKeyDown) | |
321 | EVT_KEY_UP(self, tools.OnKeyUp) | |
322 | EVT_ICONIZE(self, self.OnIconize) | |
323 | ||
324 | def AppendRecent(self, menu): | |
325 | # add recently used files to the menu | |
326 | for id,name in conf.recentfiles.iteritems(): | |
327 | menu.Append(id,name) | |
328 | EVT_MENU(self,id,self.OnRecentFile) | |
329 | return | |
330 | ||
331 | def OnRecentFile(self,evt): | |
332 | # open recently used file | |
333 | if not self.AskSave(): return | |
334 | wxBeginBusyCursor() | |
335 | try: | |
336 | path=conf.recentfiles[evt.GetId()] | |
337 | if self.Open(path): | |
338 | self.SetStatusText('Data loaded') | |
339 | else: | |
340 | self.SetStatusText('Failed') | |
341 | except KeyError: | |
342 | self.SetStatusText('No such file') | |
343 | wxEndBusyCursor() | |
344 | ||
345 | def OnNew(self, evt): | |
346 | if not self.AskSave(): return | |
347 | self.Clear() | |
348 | ||
349 | def OnOpen(self, evt): | |
350 | if not self.AskSave(): return | |
351 | dlg = wxFileDialog(self, 'Open', os.path.dirname(self.dataFile), | |
352 | '', '*.xrc', wxOPEN | wxCHANGE_DIR) | |
353 | if dlg.ShowModal() == wxID_OK: | |
354 | path = dlg.GetPath() | |
355 | self.SetStatusText('Loading...') | |
356 | wxBeginBusyCursor() | |
357 | try: | |
358 | if self.Open(path): | |
359 | self.SetStatusText('Data loaded') | |
360 | else: | |
361 | self.SetStatusText('Failed') | |
362 | self.SaveRecent(path) | |
363 | finally: | |
364 | wxEndBusyCursor() | |
365 | dlg.Destroy() | |
366 | ||
367 | def OnSaveOrSaveAs(self, evt): | |
368 | if evt.GetId() == wxID_SAVEAS or not self.dataFile: | |
369 | if self.dataFile: name = '' | |
370 | else: name = defaultName | |
371 | dirname = os.path.abspath(os.path.dirname(self.dataFile)) | |
372 | dlg = wxFileDialog(self, 'Save As', dirname, name, '*.xrc', | |
373 | wxSAVE | wxOVERWRITE_PROMPT | wxCHANGE_DIR) | |
374 | if dlg.ShowModal() == wxID_OK: | |
375 | path = dlg.GetPath() | |
376 | dlg.Destroy() | |
377 | else: | |
378 | dlg.Destroy() | |
379 | return | |
380 | ||
381 | if conf.localconf: | |
382 | # if we already have a localconf then it needs to be | |
383 | # copied to a new config with the new name | |
384 | lc = conf.localconf | |
385 | nc = self.CreateLocalConf(path) | |
386 | flag, key, idx = lc.GetFirstEntry() | |
387 | while flag: | |
388 | nc.Write(key, lc.Read(key)) | |
389 | flag, key, idx = lc.GetNextEntry(idx) | |
390 | conf.localconf = nc | |
391 | else: | |
392 | # otherwise create a new one | |
393 | conf.localconf = self.CreateLocalConf(path) | |
394 | else: | |
395 | path = self.dataFile | |
396 | self.SetStatusText('Saving...') | |
397 | wxBeginBusyCursor() | |
398 | try: | |
399 | try: | |
400 | tmpFile,tmpName = tempfile.mkstemp(prefix='xrced-') | |
401 | os.close(tmpFile) | |
402 | self.Save(tmpName) # save temporary file first | |
403 | shutil.move(tmpName, path) | |
404 | self.dataFile = path | |
405 | if conf.localconf.ReadBool("autogenerate", False): | |
406 | pypath = conf.localconf.Read("filename") | |
407 | embed = conf.localconf.ReadBool("embedResource", False) | |
408 | self.GeneratePython(self.dataFile, pypath, embed) | |
409 | ||
410 | self.SetStatusText('Data saved') | |
411 | self.SaveRecent(path) | |
412 | except IOError: | |
413 | self.SetStatusText('Failed') | |
414 | finally: | |
415 | wxEndBusyCursor() | |
416 | ||
417 | def SaveRecent(self,path): | |
418 | # append to recently used files | |
419 | if path not in conf.recentfiles.values(): | |
420 | newid = wxNewId() | |
421 | self.recentMenu.Append(newid, path) | |
422 | EVT_MENU(self, newid, self.OnRecentFile) | |
423 | conf.recentfiles[newid] = path | |
424 | ||
425 | def GeneratePython(self, dataFile, pypath, embed): | |
426 | try: | |
427 | import wx.tools.pywxrc | |
428 | rescomp = wx.tools.pywxrc.XmlResourceCompiler() | |
429 | rescomp.MakePythonModule(dataFile, pypath, embed) | |
430 | except: | |
431 | inf = sys.exc_info() | |
432 | wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1]) | |
433 | wxLogError('Error generating python code : %s' % pypath) | |
434 | raise | |
435 | ||
436 | ||
437 | def OnGeneratePython(self, evt): | |
438 | if self.modified or not conf.localconf: | |
439 | wx.MessageBox("Save the XRC file first!", "Error") | |
440 | return | |
441 | ||
442 | dlg = PythonOptions(self, conf.localconf, self.dataFile) | |
443 | dlg.ShowModal() | |
444 | dlg.Destroy() | |
445 | ||
446 | ||
447 | def OnExit(self, evt): | |
448 | self.Close() | |
449 | ||
450 | def OnUndo(self, evt): | |
451 | # Extra check to not mess with idle updating | |
452 | if undoMan.CanUndo(): | |
453 | undoMan.Undo() | |
454 | ||
455 | def OnRedo(self, evt): | |
456 | if undoMan.CanRedo(): | |
457 | undoMan.Redo() | |
458 | ||
459 | def OnCopy(self, evt): | |
460 | selected = tree.selection | |
461 | if not selected: return # key pressed event | |
462 | xxx = tree.GetPyData(selected) | |
463 | if wx.TheClipboard.Open(): | |
464 | data = wx.CustomDataObject('XRCED') | |
465 | # Set encoding in header | |
466 | # (False,True) | |
467 | s = xxx.element.toxml(encoding=expat.native_encoding) | |
468 | data.SetData(cPickle.dumps(s)) | |
469 | wx.TheClipboard.SetData(data) | |
470 | wx.TheClipboard.Close() | |
471 | self.SetStatusText('Copied') | |
472 | else: | |
473 | wx.MessageBox("Unable to open the clipboard", "Error") | |
474 | ||
475 | def OnPaste(self, evt): | |
476 | selected = tree.selection | |
477 | if not selected: return # key pressed event | |
478 | # For pasting with Ctrl pressed | |
479 | appendChild = True | |
480 | if evt.GetId() == pullDownMenu.ID_PASTE_SIBLING: appendChild = False | |
481 | elif evt.GetId() == self.ID_TOOL_PASTE: | |
482 | if g.tree.ctrl: appendChild = False | |
483 | else: appendChild = not tree.NeedInsert(selected) | |
484 | else: appendChild = not tree.NeedInsert(selected) | |
485 | xxx = tree.GetPyData(selected) | |
486 | if not appendChild: | |
487 | # If has next item, insert, else append to parent | |
488 | nextItem = tree.GetNextSibling(selected) | |
489 | parentLeaf = tree.GetItemParent(selected) | |
490 | # Expanded container (must have children) | |
491 | elif tree.IsExpanded(selected) and tree.GetChildrenCount(selected, False): | |
492 | # Insert as first child | |
493 | nextItem = tree.GetFirstChild(selected)[0] | |
494 | parentLeaf = selected | |
495 | else: | |
496 | # No children or unexpanded item - appendChild stays True | |
497 | nextItem = wxTreeItemId() # no next item | |
498 | parentLeaf = selected | |
499 | parent = tree.GetPyData(parentLeaf).treeObject() | |
500 | ||
501 | # Create a copy of clipboard pickled element | |
502 | success = False | |
503 | if wx.TheClipboard.Open(): | |
504 | data = wx.CustomDataObject('XRCED') | |
505 | if wx.TheClipboard.IsSupported(data.GetFormat()): | |
506 | success = wx.TheClipboard.GetData(data) | |
507 | wx.TheClipboard.Close() | |
508 | ||
509 | if not success: | |
510 | wx.MessageBox( | |
511 | "There is no data in the clipboard in the required format", | |
512 | "Error") | |
513 | return | |
514 | ||
515 | xml = cPickle.loads(data.GetData()) # xml representation of element | |
516 | elem = minidom.parseString(xml).childNodes[0] | |
517 | # Tempopary xxx object to test things | |
518 | xxx = MakeXXXFromDOM(parent, elem) | |
519 | # Check compatibility | |
520 | error = False | |
521 | # Top-level | |
522 | x = xxx.treeObject() | |
523 | if x.__class__ in [xxxDialog, xxxFrame, xxxWizard]: | |
524 | # Top-level classes | |
525 | if parent.__class__ != xxxMainNode: error = True | |
526 | elif x.__class__ == xxxMenuBar: | |
527 | # Menubar can be put in frame or dialog | |
528 | if parent.__class__ not in [xxxMainNode, xxxFrame, xxxDialog]: error = True | |
529 | elif x.__class__ == xxxToolBar: | |
530 | # Toolbar can be top-level of child of panel or frame | |
531 | if parent.__class__ not in [xxxMainNode, xxxPanel, xxxFrame] and \ | |
532 | not parent.isSizer: error = True | |
533 | elif x.__class__ == xxxPanel and parent.__class__ == xxxMainNode: | |
534 | pass | |
535 | elif x.__class__ == xxxSpacer: | |
536 | if not parent.isSizer: error = True | |
537 | elif x.__class__ == xxxSeparator: | |
538 | if not parent.__class__ in [xxxMenu, xxxToolBar]: error = True | |
539 | elif x.__class__ == xxxTool: | |
540 | if parent.__class__ != xxxToolBar: error = True | |
541 | elif x.__class__ == xxxMenu: | |
542 | if not parent.__class__ in [xxxMainNode, xxxMenuBar, xxxMenu]: error = True | |
543 | elif x.__class__ == xxxMenuItem: | |
544 | if not parent.__class__ in [xxxMenuBar, xxxMenu]: error = True | |
545 | elif x.isSizer and parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]: | |
546 | error = True | |
547 | else: # normal controls can be almost anywhere | |
548 | if parent.__class__ == xxxMainNode or \ | |
549 | parent.__class__ in [xxxMenuBar, xxxMenu]: error = True | |
550 | if error: | |
551 | if parent.__class__ == xxxMainNode: parentClass = 'root' | |
552 | else: parentClass = parent.className | |
553 | wxLogError('Incompatible parent/child: parent is %s, child is %s!' % | |
554 | (parentClass, x.className)) | |
555 | return | |
556 | ||
557 | # Check parent and child relationships. | |
558 | # If parent is sizer or notebook, child is of wrong class or | |
559 | # parent is normal window, child is child container then detach child. | |
560 | isChildContainer = isinstance(xxx, xxxChildContainer) | |
561 | parentIsBook = parent.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook] | |
562 | if isChildContainer and \ | |
563 | ((parent.isSizer and not isinstance(xxx, xxxSizerItem)) or \ | |
564 | (parentIsBook and not isinstance(xxx, xxxPage)) or \ | |
565 | not (parent.isSizer or parentIsBook)): | |
566 | elem.removeChild(xxx.child.element) # detach child | |
567 | elem.unlink() # delete child container | |
568 | elem = xxx.child.element # replace | |
569 | # This may help garbage collection | |
570 | xxx.child.parent = None | |
571 | isChildContainer = False | |
572 | # Parent is sizer or notebook, child is not child container | |
573 | if parent.isSizer and not isChildContainer and not isinstance(xxx, xxxSpacer): | |
574 | # Create sizer item element | |
575 | sizerItemElem = MakeEmptyDOM(parent.itemTag) | |
576 | sizerItemElem.appendChild(elem) | |
577 | elem = sizerItemElem | |
578 | elif isinstance(parent, xxxNotebook) and not isChildContainer: | |
579 | pageElem = MakeEmptyDOM('notebookpage') | |
580 | pageElem.appendChild(elem) | |
581 | elem = pageElem | |
582 | elif isinstance(parent, xxxChoicebook) and not isChildContainer: | |
583 | pageElem = MakeEmptyDOM('choicebookpage') | |
584 | pageElem.appendChild(elem) | |
585 | elem = pageElem | |
586 | elif isinstance(parent, xxxListbook) and not isChildContainer: | |
587 | pageElem = MakeEmptyDOM('listbookpage') | |
588 | pageElem.appendChild(elem) | |
589 | elem = pageElem | |
590 | # Insert new node, register undo | |
591 | newItem = tree.InsertNode(parentLeaf, parent, elem, nextItem) | |
592 | undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) | |
593 | # Scroll to show new item (!!! redundant?) | |
594 | tree.EnsureVisible(newItem) | |
595 | tree.SelectItem(newItem) | |
596 | if not tree.IsVisible(newItem): | |
597 | tree.ScrollTo(newItem) | |
598 | tree.Refresh() | |
599 | # Update view? | |
600 | if g.testWin and tree.IsHighlatable(newItem): | |
601 | if conf.autoRefresh: | |
602 | tree.needUpdate = True | |
603 | tree.pendingHighLight = newItem | |
604 | else: | |
605 | tree.pendingHighLight = None | |
606 | self.SetModified() | |
607 | self.SetStatusText('Pasted') | |
608 | ||
609 | ||
610 | def OnCutDelete(self, evt): | |
611 | selected = tree.selection | |
612 | if not selected: return # key pressed event | |
613 | # Undo info | |
614 | if evt.GetId() == wxID_CUT: | |
615 | self.lastOp = 'CUT' | |
616 | status = 'Removed to clipboard' | |
617 | else: | |
618 | self.lastOp = 'DELETE' | |
619 | status = 'Deleted' | |
620 | # Delete testWin? | |
621 | if g.testWin: | |
622 | # If deleting top-level item, delete testWin | |
623 | if selected == g.testWin.item: | |
624 | g.testWin.Destroy() | |
625 | g.testWin = None | |
626 | else: | |
627 | # Remove highlight, update testWin | |
628 | if g.testWin.highLight: | |
629 | g.testWin.highLight.Remove() | |
630 | tree.needUpdate = True | |
631 | # Prepare undo data | |
632 | panel.Apply() | |
633 | index = tree.ItemFullIndex(selected) | |
634 | parent = tree.GetPyData(tree.GetItemParent(selected)).treeObject() | |
635 | elem = tree.RemoveLeaf(selected) | |
636 | undoMan.RegisterUndo(UndoCutDelete(index, parent, elem)) | |
637 | if evt.GetId() == wxID_CUT: | |
638 | if wx.TheClipboard.Open(): | |
639 | data = wx.CustomDataObject('XRCED') | |
640 | # (False, True) | |
641 | s = elem.toxml(encoding=expat.native_encoding) | |
642 | data.SetData(cPickle.dumps(s)) | |
643 | wx.TheClipboard.SetData(data) | |
644 | wx.TheClipboard.Close() | |
645 | else: | |
646 | wx.MessageBox("Unable to open the clipboard", "Error") | |
647 | tree.pendingHighLight = None | |
648 | tree.UnselectAll() | |
649 | tree.selection = None | |
650 | # Update tools | |
651 | g.tools.UpdateUI() | |
652 | panel.Clear() | |
653 | self.SetModified() | |
654 | self.SetStatusText(status) | |
655 | ||
656 | def OnSubclass(self, evt): | |
657 | selected = tree.selection | |
658 | xxx = tree.GetPyData(selected).treeObject() | |
659 | elem = xxx.element | |
660 | subclass = xxx.subclass | |
661 | dlg = wxTextEntryDialog(self, 'Subclass:', defaultValue=subclass) | |
662 | if dlg.ShowModal() == wxID_OK: | |
663 | subclass = dlg.GetValue() | |
664 | if subclass: | |
665 | elem.setAttribute('subclass', subclass) | |
666 | elif elem.hasAttribute('subclass'): | |
667 | elem.removeAttribute('subclass') | |
668 | self.SetModified() | |
669 | xxx.subclass = elem.getAttribute('subclass') | |
670 | tree.SetItemText(selected, xxx.treeName()) | |
671 | panel.pages[0].box.SetLabel(xxx.panelName()) | |
672 | dlg.Destroy() | |
673 | ||
674 | def OnEmbedPanel(self, evt): | |
675 | conf.embedPanel = evt.IsChecked() | |
676 | if conf.embedPanel: | |
677 | # Remember last dimentions | |
678 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
679 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
680 | size = self.GetSize() | |
681 | pos = self.GetPosition() | |
682 | sizePanel = panel.GetSize() | |
683 | panel.Reparent(self.splitter) | |
684 | self.miniFrame.GetSizer().Remove(panel) | |
685 | # Widen | |
686 | self.SetDimensions(pos.x, pos.y, size.width + sizePanel.width, size.height) | |
687 | self.splitter.SplitVertically(tree, panel, conf.sashPos) | |
688 | self.miniFrame.Show(False) | |
689 | else: | |
690 | conf.sashPos = self.splitter.GetSashPosition() | |
691 | pos = self.GetPosition() | |
692 | size = self.GetSize() | |
693 | sizePanel = panel.GetSize() | |
694 | self.splitter.Unsplit(panel) | |
695 | sizer = self.miniFrame.GetSizer() | |
696 | panel.Reparent(self.miniFrame) | |
697 | panel.Show(True) | |
698 | sizer.Add(panel, 1, wxEXPAND) | |
699 | self.miniFrame.Show(True) | |
700 | self.miniFrame.SetDimensions(conf.panelX, conf.panelY, | |
701 | conf.panelWidth, conf.panelHeight) | |
702 | # Reduce width | |
703 | self.SetDimensions(pos.x, pos.y, | |
704 | max(size.width - sizePanel.width, self.minWidth), size.height) | |
705 | ||
706 | def OnShowTools(self, evt): | |
707 | conf.showTools = evt.IsChecked() | |
708 | g.tools.Show(conf.showTools) | |
709 | if conf.showTools: | |
710 | self.toolsSizer.Prepend(g.tools, 0, wxEXPAND) | |
711 | else: | |
712 | self.toolsSizer.Remove(g.tools) | |
713 | self.toolsSizer.Layout() | |
714 | ||
715 | def OnTest(self, evt): | |
716 | if not tree.selection: return # key pressed event | |
717 | tree.ShowTestWindow(tree.selection) | |
718 | ||
719 | def OnTestHide(self, evt): | |
720 | tree.CloseTestWindow() | |
721 | ||
722 | # Find object by relative position | |
723 | def FindObject(self, item, obj): | |
724 | # We simply perform depth-first traversal, sinse it's too much | |
725 | # hassle to deal with all sizer/window combinations | |
726 | w = tree.FindNodeObject(item) | |
727 | if w == obj or isinstance(w, wxGBSizerItem) and w.GetWindow() == obj: | |
728 | return item | |
729 | if tree.ItemHasChildren(item): | |
730 | child = tree.GetFirstChild(item)[0] | |
731 | while child: | |
732 | found = self.FindObject(child, obj) | |
733 | if found: return found | |
734 | child = tree.GetNextSibling(child) | |
735 | return None | |
736 | ||
737 | def OnTestWinLeftDown(self, evt): | |
738 | pos = evt.GetPosition() | |
739 | self.SetHandler(g.testWin) | |
740 | g.testWin.Disconnect(wxID_ANY, wxID_ANY, wxEVT_LEFT_DOWN) | |
741 | item = self.FindObject(g.testWin.item, evt.GetEventObject()) | |
742 | if item: | |
743 | tree.EnsureVisible(item) | |
744 | tree.SelectItem(item) | |
745 | self.tb.ToggleTool(self.ID_TOOL_LOCATE, False) | |
746 | if item: | |
747 | self.SetStatusText('Selected %s' % tree.GetItemText(item)) | |
748 | else: | |
749 | self.SetStatusText('Locate failed!') | |
750 | ||
751 | def SetHandler(self, w, h=None): | |
752 | if h: | |
753 | w.SetEventHandler(h) | |
754 | w.SetCursor(wxCROSS_CURSOR) | |
755 | else: | |
756 | w.SetEventHandler(w) | |
757 | w.SetCursor(wxNullCursor) | |
758 | for ch in w.GetChildren(): | |
759 | self.SetHandler(ch, h) | |
760 | ||
761 | def OnLocate(self, evt): | |
762 | if g.testWin: | |
763 | if evt.GetId() == self.ID_LOCATE or \ | |
764 | evt.GetId() == self.ID_TOOL_LOCATE and evt.IsChecked(): | |
765 | self.SetHandler(g.testWin, g.testWin) | |
766 | g.testWin.Connect(wxID_ANY, wxID_ANY, wxEVT_LEFT_DOWN, self.OnTestWinLeftDown) | |
767 | if evt.GetId() == self.ID_LOCATE: | |
768 | self.tb.ToggleTool(self.ID_TOOL_LOCATE, True) | |
769 | elif evt.GetId() == self.ID_TOOL_LOCATE and not evt.IsChecked(): | |
770 | self.SetHandler(g.testWin, None) | |
771 | g.testWin.Disconnect(wxID_ANY, wxID_ANY, wxEVT_LEFT_DOWN) | |
772 | self.SetStatusText('Click somewhere in your test window now') | |
773 | ||
774 | def OnRefresh(self, evt): | |
775 | # If modified, apply first | |
776 | selection = tree.selection | |
777 | if selection: | |
778 | xxx = tree.GetPyData(selection) | |
779 | if xxx and panel.IsModified(): | |
780 | tree.Apply(xxx, selection) | |
781 | if g.testWin: | |
782 | # (re)create | |
783 | tree.CreateTestWin(g.testWin.item) | |
784 | panel.modified = False | |
785 | tree.needUpdate = False | |
786 | ||
787 | def OnAutoRefresh(self, evt): | |
788 | conf.autoRefresh = evt.IsChecked() | |
789 | self.menuBar.Check(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
790 | self.tb.ToggleTool(self.ID_AUTO_REFRESH, conf.autoRefresh) | |
791 | ||
792 | def OnAbout(self, evt): | |
793 | str = '''\ | |
794 | XRCed version %s | |
795 | ||
796 | (c) Roman Rolinsky <rollrom@users.sourceforge.net> | |
797 | Homepage: http://xrced.sourceforge.net\ | |
798 | ''' % version | |
799 | dlg = wxMessageDialog(self, str, 'About XRCed', wxOK | wxCENTRE) | |
800 | dlg.ShowModal() | |
801 | dlg.Destroy() | |
802 | ||
803 | def OnReadme(self, evt): | |
804 | text = open(os.path.join(basePath, 'README.txt'), 'r').read() | |
805 | dlg = ScrolledMessageDialog(self, text, "XRCed README") | |
806 | dlg.ShowModal() | |
807 | dlg.Destroy() | |
808 | ||
809 | # Simple emulation of python command line | |
810 | def OnDebugCMD(self, evt): | |
811 | while 1: | |
812 | try: | |
813 | exec raw_input('C:\> ') | |
814 | except EOFError: | |
815 | print '^D' | |
816 | break | |
817 | except: | |
818 | (etype, value, tb) =sys.exc_info() | |
819 | tblist =traceback.extract_tb(tb)[1:] | |
820 | msg =' '.join(traceback.format_exception_only(etype, value) | |
821 | +traceback.format_list(tblist)) | |
822 | print msg | |
823 | ||
824 | def OnCreate(self, evt): | |
825 | selected = tree.selection | |
826 | if tree.ctrl: appendChild = False | |
827 | else: appendChild = not tree.NeedInsert(selected) | |
828 | xxx = tree.GetPyData(selected) | |
829 | if not appendChild: | |
830 | # If insert before | |
831 | if tree.shift: | |
832 | # If has previous item, insert after it, else append to parent | |
833 | nextItem = selected | |
834 | parentLeaf = tree.GetItemParent(selected) | |
835 | else: | |
836 | # If has next item, insert, else append to parent | |
837 | nextItem = tree.GetNextSibling(selected) | |
838 | parentLeaf = tree.GetItemParent(selected) | |
839 | # Expanded container (must have children) | |
840 | elif tree.shift and tree.IsExpanded(selected) \ | |
841 | and tree.GetChildrenCount(selected, False): | |
842 | nextItem = tree.GetFirstChild(selected)[0] | |
843 | parentLeaf = selected | |
844 | else: | |
845 | nextItem = wxTreeItemId() | |
846 | parentLeaf = selected | |
847 | parent = tree.GetPyData(parentLeaf) | |
848 | if parent.hasChild: parent = parent.child | |
849 | ||
850 | # Create object_ref? | |
851 | if evt.GetId() == ID_NEW.REF: | |
852 | ref = wxGetTextFromUser('Create reference to:', 'Create reference') | |
853 | if not ref: return | |
854 | xxx = MakeEmptyRefXXX(parent, ref) | |
855 | else: | |
856 | # Create empty element | |
857 | className = pullDownMenu.createMap[evt.GetId()] | |
858 | xxx = MakeEmptyXXX(parent, className) | |
859 | ||
860 | # Set default name for top-level windows | |
861 | if parent.__class__ == xxxMainNode: | |
862 | cl = xxx.treeObject().__class__ | |
863 | frame.maxIDs[cl] += 1 | |
864 | xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl])) | |
865 | # And for some other standard controls | |
866 | elif parent.__class__ == xxxStdDialogButtonSizer: | |
867 | xxx.setTreeName(pullDownMenu.stdButtonIDs[evt.GetId()][0]) | |
868 | # We can even set label | |
869 | obj = xxx.treeObject() | |
870 | elem = g.tree.dom.createElement('label') | |
871 | elem.appendChild(g.tree.dom.createTextNode(pullDownMenu.stdButtonIDs[evt.GetId()][1])) | |
872 | obj.params['label'] = xxxParam(elem) | |
873 | xxx.treeObject().element.appendChild(elem) | |
874 | ||
875 | # Insert new node, register undo | |
876 | elem = xxx.element | |
877 | newItem = tree.InsertNode(parentLeaf, parent, elem, nextItem) | |
878 | undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) | |
879 | tree.EnsureVisible(newItem) | |
880 | tree.SelectItem(newItem) | |
881 | if not tree.IsVisible(newItem): | |
882 | tree.ScrollTo(newItem) | |
883 | tree.Refresh() | |
884 | # Update view? | |
885 | if g.testWin and tree.IsHighlatable(newItem): | |
886 | if conf.autoRefresh: | |
887 | tree.needUpdate = True | |
888 | tree.pendingHighLight = newItem | |
889 | else: | |
890 | tree.pendingHighLight = None | |
891 | tree.SetFocus() | |
892 | self.SetModified() | |
893 | ||
894 | # Replace one object with another | |
895 | def OnReplace(self, evt): | |
896 | selected = tree.selection | |
897 | xxx = tree.GetPyData(selected).treeObject() | |
898 | elem = xxx.element | |
899 | parent = elem.parentNode | |
900 | undoMan.RegisterUndo(UndoReplace(selected)) | |
901 | # New class | |
902 | className = pullDownMenu.createMap[evt.GetId() - 1000] | |
903 | # Create temporary empty node (with default values) | |
904 | dummy = MakeEmptyDOM(className) | |
905 | if className == 'spacer' and xxx.className != 'spacer': | |
906 | klass = xxxSpacer | |
907 | elif xxx.className == 'spacer' and className != 'spacer': | |
908 | klass = xxxSizerItem | |
909 | else: | |
910 | klass = xxxDict[className] | |
911 | # Remove non-compatible children | |
912 | if tree.ItemHasChildren(selected) and not klass.hasChildren: | |
913 | tree.DeleteChildren(selected) | |
914 | nodes = elem.childNodes[:] | |
915 | tags = [] | |
916 | for node in nodes: | |
917 | if node.nodeType != minidom.Node.ELEMENT_NODE: continue | |
918 | remove = False | |
919 | tag = node.tagName | |
920 | if tag == 'object': | |
921 | if not klass.hasChildren: remove = True | |
922 | elif tag not in klass.allParams and \ | |
923 | (not klass.hasStyle or tag not in klass.styles): | |
924 | remove = True | |
925 | else: | |
926 | tags.append(tag) | |
927 | if remove: | |
928 | elem.removeChild(node) | |
929 | node.unlink() | |
930 | ||
931 | # Remove sizeritem child if spacer | |
932 | if className == 'spacer' and xxx.className != 'spacer': | |
933 | sizeritem = elem.parentNode | |
934 | assert sizeritem.getAttribute('class') == 'sizeritem' | |
935 | sizeritem.removeChild(elem) | |
936 | elem.unlink() | |
937 | elem = sizeritem | |
938 | tree.GetPyData(selected).hasChild = false | |
939 | elif xxx.className == 'spacer' and className != 'spacer': | |
940 | # Create sizeritem element | |
941 | assert xxx.parent.isSizer | |
942 | elem.setAttribute('class', 'sizeritem') | |
943 | node = MakeEmptyDOM(className) | |
944 | elem.appendChild(node) | |
945 | # Replace to point to new object | |
946 | xxx = xxxSizerItem(xxx.parent, elem) | |
947 | elem = node | |
948 | tree.SetPyData(selected, xxx) | |
949 | xxx = xxx.child | |
950 | else: | |
951 | # Copy parameters present in dummy but not in elem | |
952 | for node in dummy.childNodes: | |
953 | if node.tagName not in tags: elem.appendChild(node.cloneNode(True)) | |
954 | dummy.unlink() | |
955 | ||
956 | # Change class name | |
957 | elem.setAttribute('class', className) | |
958 | if elem.hasAttribute('subclass'): | |
959 | elem.removeAttribute('subclass') # clear subclassing | |
960 | # Re-create xxx element | |
961 | xxx = MakeXXXFromDOM(xxx.parent, elem) | |
962 | # Update parent in child objects | |
963 | if tree.ItemHasChildren(selected): | |
964 | i, cookie = tree.GetFirstChild(selected) | |
965 | while i.IsOk(): | |
966 | x = tree.GetPyData(i) | |
967 | x.parent = xxx | |
968 | if x.hasChild: x.child.parent = xxx | |
969 | i, cookie = tree.GetNextChild(selected, cookie) | |
970 | ||
971 | # Update tree | |
972 | if tree.GetPyData(selected).hasChild: # child container | |
973 | container = tree.GetPyData(selected) | |
974 | container.child = xxx | |
975 | container.hasChildren = xxx.hasChildren | |
976 | container.isSizer = xxx.isSizer | |
977 | xxx = container | |
978 | else: | |
979 | tree.SetPyData(selected, xxx) | |
980 | tree.SetItemText(selected, xxx.treeName()) | |
981 | tree.SetItemImage(selected, xxx.treeImage()) | |
982 | ||
983 | # Set default name for top-level windows | |
984 | if parent.__class__ == xxxMainNode: | |
985 | cl = xxx.treeObject().__class__ | |
986 | frame.maxIDs[cl] += 1 | |
987 | xxx.setTreeName('%s%d' % (defaultIDs[cl], frame.maxIDs[cl])) | |
988 | ||
989 | # Update panel | |
990 | g.panel.SetData(xxx) | |
991 | # Update tools | |
992 | g.tools.UpdateUI() | |
993 | ||
994 | #undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected)) | |
995 | # Update view? | |
996 | if g.testWin and tree.IsHighlatable(selected): | |
997 | if conf.autoRefresh: | |
998 | tree.needUpdate = True | |
999 | tree.pendingHighLight = selected | |
1000 | else: | |
1001 | tree.pendingHighLight = None | |
1002 | tree.SetFocus() | |
1003 | self.SetModified() | |
1004 | ||
1005 | # Expand/collapse subtree | |
1006 | def OnExpand(self, evt): | |
1007 | if tree.selection: tree.ExpandAll(tree.selection) | |
1008 | else: tree.ExpandAll(tree.root) | |
1009 | def OnCollapse(self, evt): | |
1010 | if tree.selection: tree.CollapseAll(tree.selection) | |
1011 | else: tree.CollapseAll(tree.root) | |
1012 | ||
1013 | def OnPullDownHighlight(self, evt): | |
1014 | menuId = evt.GetMenuId() | |
1015 | if menuId != -1: | |
1016 | menu = evt.GetEventObject() | |
1017 | help = menu.GetHelpString(menuId) | |
1018 | self.SetStatusText(help) | |
1019 | else: | |
1020 | self.SetStatusText('') | |
1021 | ||
1022 | def OnUpdateUI(self, evt): | |
1023 | if evt.GetId() in [wxID_CUT, wxID_COPY, self.ID_DELETE]: | |
1024 | evt.Enable(tree.selection is not None and tree.selection != tree.root) | |
1025 | elif evt.GetId() == wxID_SAVE: | |
1026 | evt.Enable(self.modified) | |
1027 | elif evt.GetId() in [wxID_PASTE, self.ID_TOOL_PASTE]: | |
1028 | evt.Enable(tree.selection is not None) | |
1029 | elif evt.GetId() == self.ID_TEST: | |
1030 | evt.Enable(tree.selection is not None and tree.selection != tree.root) | |
1031 | elif evt.GetId() in [self.ID_LOCATE, self.ID_TOOL_LOCATE]: | |
1032 | evt.Enable(g.testWin is not None) | |
1033 | elif evt.GetId() == wxID_UNDO: evt.Enable(undoMan.CanUndo()) | |
1034 | elif evt.GetId() == wxID_REDO: evt.Enable(undoMan.CanRedo()) | |
1035 | ||
1036 | def OnIdle(self, evt): | |
1037 | if self.inIdle: return # Recursive call protection | |
1038 | self.inIdle = True | |
1039 | try: | |
1040 | if tree.needUpdate: | |
1041 | if conf.autoRefresh: | |
1042 | if g.testWin: | |
1043 | self.SetStatusText('Refreshing test window...') | |
1044 | # (re)create | |
1045 | tree.CreateTestWin(g.testWin.item) | |
1046 | self.SetStatusText('') | |
1047 | tree.needUpdate = False | |
1048 | elif tree.pendingHighLight: | |
1049 | try: | |
1050 | tree.HighLight(tree.pendingHighLight) | |
1051 | except: | |
1052 | # Remove highlight if any problem | |
1053 | if g.testWin.highLight: | |
1054 | g.testWin.highLight.Remove() | |
1055 | tree.pendingHighLight = None | |
1056 | raise | |
1057 | else: | |
1058 | evt.Skip() | |
1059 | finally: | |
1060 | self.inIdle = False | |
1061 | ||
1062 | # We don't let close panel window | |
1063 | def OnCloseMiniFrame(self, evt): | |
1064 | return | |
1065 | ||
1066 | def OnIconize(self, evt): | |
1067 | conf.x, conf.y = self.GetPosition() | |
1068 | conf.width, conf.height = self.GetSize() | |
1069 | if conf.embedPanel: | |
1070 | conf.sashPos = self.splitter.GetSashPosition() | |
1071 | else: | |
1072 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
1073 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
1074 | self.miniFrame.Iconize() | |
1075 | evt.Skip() | |
1076 | ||
1077 | def OnCloseWindow(self, evt): | |
1078 | if not self.AskSave(): return | |
1079 | if g.testWin: g.testWin.Destroy() | |
1080 | if not panel.GetPageCount() == 2: | |
1081 | panel.page2.Destroy() | |
1082 | else: | |
1083 | # If we don't do this, page does not get destroyed (a bug?) | |
1084 | panel.RemovePage(1) | |
1085 | if not self.IsIconized(): | |
1086 | conf.x, conf.y = self.GetPosition() | |
1087 | conf.width, conf.height = self.GetSize() | |
1088 | if conf.embedPanel: | |
1089 | conf.sashPos = self.splitter.GetSashPosition() | |
1090 | else: | |
1091 | conf.panelX, conf.panelY = self.miniFrame.GetPosition() | |
1092 | conf.panelWidth, conf.panelHeight = self.miniFrame.GetSize() | |
1093 | evt.Skip() | |
1094 | ||
1095 | ||
1096 | def CreateLocalConf(self, path): | |
1097 | name = os.path.splitext(path)[0] | |
1098 | name += '.xcfg' | |
1099 | return wx.FileConfig(localFilename=name) | |
1100 | ||
1101 | ||
1102 | def Clear(self): | |
1103 | self.dataFile = '' | |
1104 | conf.localconf = None | |
1105 | undoMan.Clear() | |
1106 | self.SetModified(False) | |
1107 | tree.Clear() | |
1108 | panel.Clear() | |
1109 | if g.testWin: | |
1110 | g.testWin.Destroy() | |
1111 | g.testWin = None | |
1112 | # Numbers for new controls | |
1113 | self.maxIDs = {} | |
1114 | for cl in [xxxPanel, xxxDialog, xxxFrame, | |
1115 | xxxMenuBar, xxxMenu, xxxToolBar, | |
1116 | xxxWizard, xxxBitmap, xxxIcon]: | |
1117 | self.maxIDs[cl] = 0 | |
1118 | ||
1119 | def SetModified(self, state=True): | |
1120 | self.modified = state | |
1121 | name = os.path.basename(self.dataFile) | |
1122 | if not name: name = defaultName | |
1123 | if state: | |
1124 | self.SetTitle(progname + ': ' + name + ' *') | |
1125 | else: | |
1126 | self.SetTitle(progname + ': ' + name) | |
1127 | ||
1128 | def Open(self, path): | |
1129 | if not os.path.exists(path): | |
1130 | wxLogError('File does not exists: %s' % path) | |
1131 | return False | |
1132 | # Try to read the file | |
1133 | try: | |
1134 | f = open(path) | |
1135 | self.Clear() | |
1136 | dom = minidom.parse(f) | |
1137 | f.close() | |
1138 | # Set encoding global variable and default encoding | |
1139 | if dom.encoding: | |
1140 | g.currentEncoding = dom.encoding | |
1141 | wx.SetDefaultPyEncoding(g.currentEncoding.encode()) | |
1142 | else: | |
1143 | g.currentEncoding = '' | |
1144 | # Change dir | |
1145 | self.dataFile = path = os.path.abspath(path) | |
1146 | dir = os.path.dirname(path) | |
1147 | if dir: os.chdir(dir) | |
1148 | tree.SetData(dom) | |
1149 | self.SetTitle(progname + ': ' + os.path.basename(path)) | |
1150 | conf.localconf = self.CreateLocalConf(self.dataFile) | |
1151 | except: | |
1152 | # Nice exception printing | |
1153 | inf = sys.exc_info() | |
1154 | wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1]) | |
1155 | wxLogError('Error reading file: %s' % path) | |
1156 | if debug: raise | |
1157 | return False | |
1158 | return True | |
1159 | ||
1160 | def Indent(self, node, indent = 0): | |
1161 | # Copy child list because it will change soon | |
1162 | children = node.childNodes[:] | |
1163 | # Main node doesn't need to be indented | |
1164 | if indent: | |
1165 | text = self.domCopy.createTextNode('\n' + ' ' * indent) | |
1166 | node.parentNode.insertBefore(text, node) | |
1167 | if children: | |
1168 | # Append newline after last child, except for text nodes | |
1169 | if children[-1].nodeType == minidom.Node.ELEMENT_NODE: | |
1170 | text = self.domCopy.createTextNode('\n' + ' ' * indent) | |
1171 | node.appendChild(text) | |
1172 | # Indent children which are elements | |
1173 | for n in children: | |
1174 | if n.nodeType == minidom.Node.ELEMENT_NODE: | |
1175 | self.Indent(n, indent + 2) | |
1176 | ||
1177 | def Save(self, path): | |
1178 | try: | |
1179 | import codecs | |
1180 | # Apply changes | |
1181 | if tree.selection and panel.IsModified(): | |
1182 | self.OnRefresh(wxCommandEvent()) | |
1183 | if g.currentEncoding: | |
1184 | f = codecs.open(path, 'wt', g.currentEncoding) | |
1185 | else: | |
1186 | f = codecs.open(path, 'wt') | |
1187 | # Make temporary copy for formatting it | |
1188 | # !!! We can't clone dom node, it works only once | |
1189 | #self.domCopy = tree.dom.cloneNode(True) | |
1190 | self.domCopy = MyDocument() | |
1191 | mainNode = self.domCopy.appendChild(tree.mainNode.cloneNode(True)) | |
1192 | # Remove first child (test element) | |
1193 | testElem = mainNode.firstChild | |
1194 | mainNode.removeChild(testElem) | |
1195 | testElem.unlink() | |
1196 | self.Indent(mainNode) | |
1197 | self.domCopy.writexml(f, encoding = g.currentEncoding) | |
1198 | f.close() | |
1199 | self.domCopy.unlink() | |
1200 | self.domCopy = None | |
1201 | self.SetModified(False) | |
1202 | panel.SetModified(False) | |
1203 | conf.localconf.Flush() | |
1204 | except: | |
1205 | inf = sys.exc_info() | |
1206 | wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1]) | |
1207 | wxLogError('Error writing file: %s' % path) | |
1208 | raise | |
1209 | ||
1210 | def AskSave(self): | |
1211 | if not (self.modified or panel.IsModified()): return True | |
1212 | flags = wxICON_EXCLAMATION | wxYES_NO | wxCANCEL | wxCENTRE | |
1213 | dlg = wxMessageDialog( self, 'File is modified. Save before exit?', | |
1214 | 'Save before too late?', flags ) | |
1215 | say = dlg.ShowModal() | |
1216 | dlg.Destroy() | |
1217 | wxYield() | |
1218 | if say == wxID_YES: | |
1219 | self.OnSaveOrSaveAs(wxCommandEvent(wxID_SAVE)) | |
1220 | # If save was successful, modified flag is unset | |
1221 | if not self.modified: return True | |
1222 | elif say == wxID_NO: | |
1223 | self.SetModified(False) | |
1224 | panel.SetModified(False) | |
1225 | return True | |
1226 | return False | |
1227 | ||
1228 | def SaveUndo(self): | |
1229 | pass # !!! | |
1230 | ||
1231 | ################################################################################ | |
1232 | ||
1233 | class PythonOptions(wx.Dialog): | |
1234 | ||
1235 | def __init__(self, parent, cfg, dataFile): | |
1236 | pre = wx.PreDialog() | |
1237 | g.frame.res.LoadOnDialog(pre, parent, "PYTHON_OPTIONS") | |
1238 | self.PostCreate(pre) | |
1239 | ||
1240 | self.cfg = cfg | |
1241 | self.dataFile = dataFile | |
1242 | ||
1243 | self.AutoGenerateCB = XRCCTRL(self, "AutoGenerateCB") | |
1244 | self.EmbedCB = XRCCTRL(self, "EmbedCB") | |
1245 | self.GettextCB = XRCCTRL(self, "GettextCB") | |
1246 | self.MakeXRSFileCB = XRCCTRL(self, "MakeXRSFileCB") | |
1247 | self.FileNameTC = XRCCTRL(self, "FileNameTC") | |
1248 | self.BrowseBtn = XRCCTRL(self, "BrowseBtn") | |
1249 | self.GenerateBtn = XRCCTRL(self, "GenerateBtn") | |
1250 | self.SaveOptsBtn = XRCCTRL(self, "SaveOptsBtn") | |
1251 | ||
1252 | self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.BrowseBtn) | |
1253 | self.Bind(wx.EVT_BUTTON, self.OnGenerate, self.GenerateBtn) | |
1254 | self.Bind(wx.EVT_BUTTON, self.OnSaveOpts, self.SaveOptsBtn) | |
1255 | ||
1256 | if self.cfg.Read("filename", "") != "": | |
1257 | self.FileNameTC.SetValue(self.cfg.Read("filename")) | |
1258 | else: | |
1259 | name = os.path.splitext(dataFile)[0] | |
1260 | name += '_xrc.py' | |
1261 | self.FileNameTC.SetValue(name) | |
1262 | self.AutoGenerateCB.SetValue(self.cfg.ReadBool("autogenerate", False)) | |
1263 | self.EmbedCB.SetValue(self.cfg.ReadBool("embedResource", False)) | |
1264 | self.MakeXRSFileCB.SetValue(self.cfg.ReadBool("makeXRS", False)) | |
1265 | self.GettextCB.SetValue(self.cfg.ReadBool("genGettext", False)) | |
1266 | ||
1267 | ||
1268 | def OnBrowse(self, evt): | |
1269 | path = self.FileNameTC.GetValue() | |
1270 | dirname = os.path.abspath(os.path.dirname(path)) | |
1271 | name = os.path.split(path)[1] | |
1272 | dlg = wxFileDialog(self, 'Save As', dirname, name, '*.py', | |
1273 | wxSAVE | wxOVERWRITE_PROMPT) | |
1274 | if dlg.ShowModal() == wxID_OK: | |
1275 | path = dlg.GetPath() | |
1276 | self.FileNameTC.SetValue(path) | |
1277 | dlg.Destroy() | |
1278 | ||
1279 | ||
1280 | def OnGenerate(self, evt): | |
1281 | pypath = self.FileNameTC.GetValue() | |
1282 | embed = self.EmbedCB.GetValue() | |
1283 | frame.GeneratePython(self.dataFile, pypath, embed) | |
1284 | self.OnSaveOpts() | |
1285 | ||
1286 | ||
1287 | def OnSaveOpts(self, evt=None): | |
1288 | self.cfg.Write("filename", self.FileNameTC.GetValue()) | |
1289 | self.cfg.WriteBool("autogenerate", self.AutoGenerateCB.GetValue()) | |
1290 | self.cfg.WriteBool("embedResource", self.EmbedCB.GetValue()) | |
1291 | self.cfg.WriteBool("makeXRS", self.MakeXRSFileCB.GetValue()) | |
1292 | self.cfg.WriteBool("genGettext", self.GettextCB.GetValue()) | |
1293 | ||
1294 | self.EndModal(wx.ID_OK) | |
1295 | ||
1296 | ||
1297 | ################################################################################ | |
1298 | ||
1299 | def usage(): | |
1300 | print >> sys.stderr, 'usage: xrced [-dhiv] [file]' | |
1301 | ||
1302 | class App(wxApp): | |
1303 | def OnInit(self): | |
1304 | # Check version | |
1305 | if wxVERSION[:3] < MinWxVersion: | |
1306 | wxLogWarning('''\ | |
1307 | This version of XRCed may not work correctly on your version of wxWindows. \ | |
1308 | Please upgrade wxWindows to %d.%d.%d or higher.''' % MinWxVersion) | |
1309 | global debug | |
1310 | # Process comand-line | |
1311 | opts = args = None | |
1312 | try: | |
1313 | opts, args = getopt.getopt(sys.argv[1:], 'dhiv') | |
1314 | for o,a in opts: | |
1315 | if o == '-h': | |
1316 | usage() | |
1317 | sys.exit(0) | |
1318 | elif o == '-d': | |
1319 | debug = True | |
1320 | elif o == '-v': | |
1321 | print 'XRCed version', version | |
1322 | sys.exit(0) | |
1323 | ||
1324 | except getopt.GetoptError: | |
1325 | if wxPlatform != '__WXMAC__': # macs have some extra parameters | |
1326 | print >> sys.stderr, 'Unknown option' | |
1327 | usage() | |
1328 | sys.exit(1) | |
1329 | ||
1330 | self.SetAppName('xrced') | |
1331 | # Settings | |
1332 | global conf | |
1333 | conf = g.conf = wxConfig(style = wxCONFIG_USE_LOCAL_FILE) | |
1334 | conf.localconf = None | |
1335 | conf.autoRefresh = conf.ReadInt('autorefresh', True) | |
1336 | pos = conf.ReadInt('x', -1), conf.ReadInt('y', -1) | |
1337 | size = conf.ReadInt('width', 800), conf.ReadInt('height', 600) | |
1338 | conf.embedPanel = conf.ReadInt('embedPanel', True) | |
1339 | conf.showTools = conf.ReadInt('showTools', True) | |
1340 | conf.sashPos = conf.ReadInt('sashPos', 200) | |
1341 | # read recently used files | |
1342 | recentfiles=conf.Read('recentFiles','') | |
1343 | conf.recentfiles={} | |
1344 | if recentfiles: | |
1345 | for fil in recentfiles.split('|'): | |
1346 | conf.recentfiles[wxNewId()]=fil | |
1347 | if not conf.embedPanel: | |
1348 | conf.panelX = conf.ReadInt('panelX', -1) | |
1349 | conf.panelY = conf.ReadInt('panelY', -1) | |
1350 | else: | |
1351 | conf.panelX = conf.panelY = -1 | |
1352 | conf.panelWidth = conf.ReadInt('panelWidth', 200) | |
1353 | conf.panelHeight = conf.ReadInt('panelHeight', 200) | |
1354 | conf.panic = not conf.HasEntry('nopanic') | |
1355 | # Add handlers | |
1356 | wxFileSystem_AddHandler(wxMemoryFSHandler()) | |
1357 | # Create main frame | |
1358 | frame = Frame(pos, size) | |
1359 | frame.Show(True) | |
1360 | ||
1361 | # Load file after showing | |
1362 | if args: | |
1363 | conf.panic = False | |
1364 | frame.open = frame.Open(args[0]) | |
1365 | ||
1366 | return True | |
1367 | ||
1368 | def OnExit(self): | |
1369 | # Write config | |
1370 | global conf | |
1371 | wc = conf | |
1372 | wc.WriteInt('autorefresh', conf.autoRefresh) | |
1373 | wc.WriteInt('x', conf.x) | |
1374 | wc.WriteInt('y', conf.y) | |
1375 | wc.WriteInt('width', conf.width) | |
1376 | wc.WriteInt('height', conf.height) | |
1377 | wc.WriteInt('embedPanel', conf.embedPanel) | |
1378 | wc.WriteInt('showTools', conf.showTools) | |
1379 | if not conf.embedPanel: | |
1380 | wc.WriteInt('panelX', conf.panelX) | |
1381 | wc.WriteInt('panelY', conf.panelY) | |
1382 | wc.WriteInt('sashPos', conf.sashPos) | |
1383 | wc.WriteInt('panelWidth', conf.panelWidth) | |
1384 | wc.WriteInt('panelHeight', conf.panelHeight) | |
1385 | wc.WriteInt('nopanic', True) | |
1386 | wc.Write('recentFiles', '|'.join(conf.recentfiles.values()[-5:])) | |
1387 | wc.Flush() | |
1388 | ||
1389 | def main(): | |
1390 | app = App(0, useBestVisual=False) | |
1391 | #app.SetAssertMode(wxPYAPP_ASSERT_LOG) | |
1392 | app.MainLoop() | |
1393 | app.OnExit() | |
1394 | global conf | |
1395 | del conf | |
1396 | ||
1397 | if __name__ == '__main__': | |
1398 | main() |