2 # Purpose: XRC editor, main module
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
9 xrced -- Simple resource editor for XRC format used by wxWindows/wxPython
14 xrced [ -h ] [ -v ] [ XRC-file ]
18 -h output short usage info and exit
20 -v output version info and exit
25 import os
, sys
, getopt
, re
, traceback
28 from tree
import * # imports xxx which imports params
31 # Cleanup recursive import sideeffects, otherwise we can't create undoMan
33 undo
.ParamPage
= ParamPage
34 undoMan
= g
.undoMan
= UndoManager()
36 # Set application path for loading resources
37 if __name__
== '__main__':
38 basePath
= os
.path
.dirname(sys
.argv
[0])
40 basePath
= os
.path
.dirname(__file__
)
42 # 1 adds CMD command to Help menu
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>
56 defaultIDs
= {xxxPanel
:'PANEL', xxxDialog
:'DIALOG', xxxFrame
:'FRAME',
57 xxxMenuBar
:'MENUBAR', xxxMenu
:'MENU', xxxToolBar
:'TOOLBAR'}
59 ################################################################################
61 # ScrolledMessageDialog - modified from wxPython lib to set fixed-width font
62 class ScrolledMessageDialog(wxDialog
):
63 def __init__(self
, parent
, msg
, caption
, pos
= wxDefaultPosition
, size
= (500,300)):
64 from wxPython
.lib
.layoutf
import Layoutf
65 wxDialog
.__init
__(self
, parent
, -1, caption
, pos
, size
)
66 text
= wxTextCtrl(self
, -1, msg
, wxDefaultPosition
,
67 wxDefaultSize
, wxTE_MULTILINE | wxTE_READONLY
)
68 text
.SetFont(modernFont
)
70 # !!! possible bug - GetTextExtent without font returns sysfont dims
71 w
, h
= dc
.GetFullTextExtent(' ', modernFont
)[:2]
72 ok
= wxButton(self
, wxID_OK
, "OK")
73 text
.SetConstraints(Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self
,ok
)))
74 text
.SetSize((w
* 80 + 30, h
* 40))
75 ok
.SetConstraints(Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self
,)))
76 self
.SetAutoLayout(True)
78 self
.CenterOnScreen(wxBOTH
)
80 ################################################################################
83 def __init__(self
, pos
, size
):
84 wxFrame
.__init
__(self
, None, -1, '', pos
, size
)
86 frame
= g
.frame
= self
87 bar
= self
.CreateStatusBar(2)
88 bar
.SetStatusWidths([-1, 40])
89 self
.SetIcon(images
.getIconIcon())
98 menu
.Append(wxID_NEW
, '&New\tCtrl-N', 'New file')
99 menu
.Append(wxID_OPEN
, '&Open...\tCtrl-O', 'Open XRC file')
100 menu
.Append(wxID_SAVE
, '&Save\tCtrl-S', 'Save XRC file')
101 menu
.Append(wxID_SAVEAS
, 'Save &As...', 'Save XRC file under different name')
102 menu
.AppendSeparator()
103 menu
.Append(wxID_EXIT
, '&Quit\tCtrl-Q', 'Exit application')
104 menuBar
.Append(menu
, '&File')
107 menu
.Append(wxID_UNDO
, '&Undo\tCtrl-Z', 'Undo')
108 menu
.Append(wxID_REDO
, '&Redo\tCtrl-Y', 'Redo')
109 menu
.AppendSeparator()
110 menu
.Append(wxID_CUT
, 'Cut\tCtrl-X', 'Cut to the clipboard')
111 menu
.Append(wxID_COPY
, '&Copy\tCtrl-C', 'Copy to the clipboard')
112 menu
.Append(wxID_PASTE
, '&Paste\tCtrl-V', 'Paste from the clipboard')
113 self
.ID_DELETE
= wxNewId()
114 menu
.Append(self
.ID_DELETE
, '&Delete\tCtrl-D', 'Delete object')
115 # menu.AppendSeparator()
116 ID_SELECT
= wxNewId()
117 # menu.Append(ID_SELECT, '&Select', 'Select object')
118 menuBar
.Append(menu
, '&Edit')
121 self
.ID_EMBED_PANEL
= wxNewId()
122 menu
.Append(self
.ID_EMBED_PANEL
, '&Embed Panel',
123 'Toggle embedding properties panel in the main window', True)
124 menu
.Check(self
.ID_EMBED_PANEL
, conf
.embedPanel
)
125 self
.ID_SHOW_TOOLS
= wxNewId()
126 menu
.Append(self
.ID_SHOW_TOOLS
, 'Show &Tools', 'Toggle tools', True)
127 menu
.Check(self
.ID_SHOW_TOOLS
, conf
.showTools
)
128 menu
.AppendSeparator()
129 self
.ID_TEST
= wxNewId()
130 menu
.Append(self
.ID_TEST
, '&Test\tF5', 'Test window')
131 self
.ID_REFRESH
= wxNewId()
132 menu
.Append(self
.ID_REFRESH
, '&Refresh\tCtrl-R', 'Refresh test window')
133 self
.ID_AUTO_REFRESH
= wxNewId()
134 menu
.Append(self
.ID_AUTO_REFRESH
, '&Auto-refresh\tCtrl-A',
135 'Toggle auto-refresh mode', True)
136 menu
.Check(self
.ID_AUTO_REFRESH
, conf
.autoRefresh
)
137 menuBar
.Append(menu
, '&View')
140 menu
.Append(wxID_ABOUT
, '&About...', 'About XCRed')
141 self
.ID_README
= wxNewId()
142 menu
.Append(self
.ID_README
, '&Readme...', 'View the README file')
144 self
.ID_DEBUG_CMD
= wxNewId()
145 menu
.Append(self
.ID_DEBUG_CMD
, 'CMD', 'Python command line')
146 EVT_MENU(self
, self
.ID_DEBUG_CMD
, self
.OnDebugCMD
)
147 menuBar
.Append(menu
, '&Help')
149 self
.menuBar
= menuBar
150 self
.SetMenuBar(menuBar
)
153 tb
= self
.CreateToolBar(wxTB_HORIZONTAL | wxNO_BORDER | wxTB_FLAT
)
154 tb
.SetToolBitmapSize((24, 23))
155 tb
.AddSimpleTool(wxID_NEW
, images
.getNewBitmap(), 'New', 'New file')
156 tb
.AddSimpleTool(wxID_OPEN
, images
.getOpenBitmap(), 'Open', 'Open file')
157 tb
.AddSimpleTool(wxID_SAVE
, images
.getSaveBitmap(), 'Save', 'Save file')
158 tb
.AddControl(wxStaticLine(tb
, -1, size
=(-1,23), style
=wxLI_VERTICAL
))
159 tb
.AddSimpleTool(wxID_UNDO
, images
.getUndoBitmap(), 'Undo', 'Undo')
160 tb
.AddSimpleTool(wxID_REDO
, images
.getRedoBitmap(), 'Redo', 'Redo')
161 tb
.AddControl(wxStaticLine(tb
, -1, size
=(-1,23), style
=wxLI_VERTICAL
))
162 tb
.AddSimpleTool(wxID_CUT
, images
.getCutBitmap(), 'Cut', 'Cut')
163 tb
.AddSimpleTool(wxID_COPY
, images
.getCopyBitmap(), 'Copy', 'Copy')
164 tb
.AddSimpleTool(wxID_PASTE
, images
.getPasteBitmap(), 'Paste', 'Paste')
165 tb
.AddControl(wxStaticLine(tb
, -1, size
=(-1,23), style
=wxLI_VERTICAL
))
166 tb
.AddSimpleTool(self
.ID_TEST
, images
.getTestBitmap(), 'Test', 'Test window')
167 tb
.AddSimpleTool(self
.ID_REFRESH
, images
.getRefreshBitmap(),
168 'Refresh', 'Refresh view')
169 tb
.AddSimpleTool(self
.ID_AUTO_REFRESH
, images
.getAutoRefreshBitmap(),
170 'Auto-refresh', 'Toggle auto-refresh mode', True)
171 if wxPlatform
== '__WXGTK__':
172 tb
.AddSeparator() # otherwise auto-refresh sticks in status line
173 tb
.ToggleTool(self
.ID_AUTO_REFRESH
, conf
.autoRefresh
)
176 self
.minWidth
= tb
.GetSize()[0] # minimal width is the size of toolbar
179 EVT_MENU(self
, wxID_NEW
, self
.OnNew
)
180 EVT_MENU(self
, wxID_OPEN
, self
.OnOpen
)
181 EVT_MENU(self
, wxID_SAVE
, self
.OnSaveOrSaveAs
)
182 EVT_MENU(self
, wxID_SAVEAS
, self
.OnSaveOrSaveAs
)
183 EVT_MENU(self
, wxID_EXIT
, self
.OnExit
)
185 EVT_MENU(self
, wxID_UNDO
, self
.OnUndo
)
186 EVT_MENU(self
, wxID_REDO
, self
.OnRedo
)
187 EVT_MENU(self
, wxID_CUT
, self
.OnCutDelete
)
188 EVT_MENU(self
, wxID_COPY
, self
.OnCopy
)
189 EVT_MENU(self
, wxID_PASTE
, self
.OnPaste
)
190 EVT_MENU(self
, self
.ID_DELETE
, self
.OnCutDelete
)
191 EVT_MENU(self
, ID_SELECT
, self
.OnSelect
)
193 EVT_MENU(self
, self
.ID_EMBED_PANEL
, self
.OnEmbedPanel
)
194 EVT_MENU(self
, self
.ID_SHOW_TOOLS
, self
.OnShowTools
)
195 EVT_MENU(self
, self
.ID_TEST
, self
.OnTest
)
196 EVT_MENU(self
, self
.ID_REFRESH
, self
.OnRefresh
)
197 EVT_MENU(self
, self
.ID_AUTO_REFRESH
, self
.OnAutoRefresh
)
199 EVT_MENU(self
, wxID_ABOUT
, self
.OnAbout
)
200 EVT_MENU(self
, self
.ID_README
, self
.OnReadme
)
203 EVT_UPDATE_UI(self
, wxID_CUT
, self
.OnUpdateUI
)
204 EVT_UPDATE_UI(self
, wxID_COPY
, self
.OnUpdateUI
)
205 EVT_UPDATE_UI(self
, wxID_PASTE
, self
.OnUpdateUI
)
206 EVT_UPDATE_UI(self
, wxID_UNDO
, self
.OnUpdateUI
)
207 EVT_UPDATE_UI(self
, wxID_REDO
, self
.OnUpdateUI
)
208 EVT_UPDATE_UI(self
, self
.ID_DELETE
, self
.OnUpdateUI
)
209 EVT_UPDATE_UI(self
, self
.ID_TEST
, self
.OnUpdateUI
)
210 EVT_UPDATE_UI(self
, self
.ID_REFRESH
, self
.OnUpdateUI
)
213 sizer
= wxBoxSizer(wxVERTICAL
)
214 sizer
.Add(wxStaticLine(self
, -1), 0, wxEXPAND
)
215 # Horizontal sizer for toolbar and splitter
216 self
.toolsSizer
= sizer1
= wxBoxSizer()
217 splitter
= wxSplitterWindow(self
, -1, style
=wxSP_3DSASH
)
218 self
.splitter
= splitter
219 splitter
.SetMinimumPaneSize(100)
222 g
.tree
= tree
= XML_Tree(splitter
, -1)
224 # Init pull-down menu data
226 g
.pullDownMenu
= pullDownMenu
= PullDownMenu(self
)
228 # Vertical toolbar for GUI buttons
229 g
.tools
= tools
= Tools(self
)
230 tools
.Show(conf
.showTools
)
231 if conf
.showTools
: sizer1
.Add(tools
, 0, wxEXPAND
)
233 tree
.RegisterKeyEvents()
235 # !!! frame styles are broken
236 # Miniframe for not embedded mode
237 miniFrame
= wxFrame(self
, -1, 'Properties Panel',
238 (conf
.panelX
, conf
.panelY
),
239 (conf
.panelWidth
, conf
.panelHeight
))
240 self
.miniFrame
= miniFrame
241 sizer2
= wxBoxSizer()
242 miniFrame
.SetAutoLayout(True)
243 miniFrame
.SetSizer(sizer2
)
244 EVT_CLOSE(self
.miniFrame
, self
.OnCloseMiniFrame
)
245 # Create panel for parameters
248 panel
= Panel(splitter
)
249 # Set plitter windows
250 splitter
.SplitVertically(tree
, panel
, conf
.sashPos
)
252 panel
= Panel(miniFrame
)
253 sizer2
.Add(panel
, 1, wxEXPAND
)
255 splitter
.Initialize(tree
)
256 sizer1
.Add(splitter
, 1, wxEXPAND
)
257 sizer
.Add(sizer1
, 1, wxEXPAND
)
258 self
.SetAutoLayout(True)
262 self
.clipboard
= None
266 EVT_IDLE(self
, self
.OnIdle
)
267 EVT_CLOSE(self
, self
.OnCloseWindow
)
268 EVT_LEFT_DOWN(self
, self
.OnLeftDown
)
269 EVT_KEY_DOWN(self
, tools
.OnKeyDown
)
270 EVT_KEY_UP(self
, tools
.OnKeyUp
)
272 def OnNew(self
, evt
):
273 if not self
.AskSave(): return
276 def OnOpen(self
, evt
):
277 if not self
.AskSave(): return
278 dlg
= wxFileDialog(self
, 'Open', os
.path
.dirname(self
.dataFile
),
279 '', '*.xrc', wxOPEN | wxCHANGE_DIR
)
280 if dlg
.ShowModal() == wxID_OK
:
282 self
.SetStatusText('Loading...')
286 self
.SetStatusText('Data loaded')
288 self
.SetStatusText('Failed')
292 def OnSaveOrSaveAs(self
, evt
):
293 if evt
.GetId() == wxID_SAVEAS
or not self
.dataFile
:
294 if self
.dataFile
: defaultName
= ''
295 else: defaultName
= 'UNTITLED.xrc'
296 dlg
= wxFileDialog(self
, 'Save As', os
.path
.dirname(self
.dataFile
),
297 defaultName
, '*.xrc',
298 wxSAVE | wxOVERWRITE_PROMPT | wxCHANGE_DIR
)
299 if dlg
.ShowModal() == wxID_OK
:
307 self
.SetStatusText('Saving...')
313 self
.SetStatusText('Data saved')
315 self
.SetStatusText('Failed')
318 def OnExit(self
, evt
):
321 def OnUndo(self
, evt
):
322 # Extra check to not mess with idle updating
323 if undoMan
.CanUndo():
326 def OnRedo(self
, evt
):
327 if undoMan
.CanRedo():
330 def OnCopy(self
, evt
):
331 selected
= tree
.selection
332 if not selected
: return # key pressed event
333 xxx
= tree
.GetPyData(selected
)
334 self
.clipboard
= xxx
.element
.cloneNode(True)
335 self
.SetStatusText('Copied')
337 def OnPaste(self
, evt
):
338 selected
= tree
.selection
339 if not selected
: return # key pressed event
340 # For pasting with Ctrl pressed
341 if evt
.GetId() == pullDownMenu
.ID_PASTE_SIBLING
: appendChild
= False
342 else: appendChild
= not tree
.NeedInsert(selected
)
343 xxx
= tree
.GetPyData(selected
)
345 # If has next item, insert, else append to parent
346 nextItem
= tree
.GetNextSibling(selected
)
347 parentLeaf
= tree
.GetItemParent(selected
)
348 # Expanded container (must have children)
349 elif tree
.IsExpanded(selected
) and tree
.GetChildrenCount(selected
, False):
350 # Insert as first child
351 nextItem
= tree
.GetFirstChild(selected
, 0)[0]
352 parentLeaf
= selected
354 # No children or unexpanded item - appendChild stays True
355 nextItem
= wxTreeItemId() # no next item
356 parentLeaf
= selected
357 parent
= tree
.GetPyData(parentLeaf
).treeObject()
359 # Create a copy of clipboard element
360 elem
= self
.clipboard
.cloneNode(True)
361 # Tempopary xxx object to test things
362 xxx
= MakeXXXFromDOM(parent
, elem
)
364 # Check compatibility
368 if x
.__class
__ in [xxxDialog
, xxxFrame
, xxxMenuBar
]:
370 if parent
.__class
__ != xxxMainNode
: error
= True
371 elif x
.__class
__ == xxxToolBar
:
372 # Toolbar can be top-level of child of panel or frame
373 if parent
.__class
__ not in [xxxMainNode
, xxxPanel
, xxxFrame
]: error
= True
374 elif x
.__class
__ == xxxPanel
and parent
.__class
__ == xxxMainNode
:
376 elif x
.__class
__ == xxxSpacer
:
377 if not parent
.isSizer
: error
= True
378 elif x
.__class
__ == xxxSeparator
:
379 if not parent
.__class
__ in [xxxMenu
, xxxToolBar
]: error
= True
380 elif x
.__class
__ == xxxTool
:
381 if parent
.__class
__ != xxxToolBar
: error
= True
382 elif x
.__class
__ == xxxMenu
:
383 if not parent
.__class
__ in [xxxMainNode
, xxxMenuBar
, xxxMenu
]: error
= True
384 elif x
.__class
__ == xxxMenuItem
:
385 if not parent
.__class
__ in [xxxMenuBar
, xxxMenu
]: error
= True
386 elif x
.isSizer
and parent
.__class
__ == xxxNotebook
: error
= True
387 else: # normal controls can be almost anywhere
388 if parent
.__class
__ == xxxMainNode
or \
389 parent
.__class
__ in [xxxMenuBar
, xxxMenu
]: error
= True
391 if parent
.__class
__ == xxxMainNode
: parentClass
= 'root'
392 else: parentClass
= parent
.className
393 wxLogError('Incompatible parent/child: parent is %s, child is %s!' %
394 (parentClass
, x
.className
))
397 # Check parent and child relationships.
398 # If parent is sizer or notebook, child is of wrong class or
399 # parent is normal window, child is child container then detach child.
400 isChildContainer
= isinstance(xxx
, xxxChildContainer
)
401 if isChildContainer
and \
402 ((parent
.isSizer
and not isinstance(xxx
, xxxSizerItem
)) or \
403 (isinstance(parent
, xxxNotebook
) and not isinstance(xxx
, xxxNotebookPage
)) or \
404 not (parent
.isSizer
or isinstance(parent
, xxxNotebook
))):
405 elem
.removeChild(xxx
.child
.element
) # detach child
406 elem
.unlink() # delete child container
407 elem
= xxx
.child
.element
# replace
408 # This may help garbage collection
409 xxx
.child
.parent
= None
410 isChildContainer
= False
411 # Parent is sizer or notebook, child is not child container
412 if parent
.isSizer
and not isChildContainer
and not isinstance(xxx
, xxxSpacer
):
413 # Create sizer item element
414 sizerItemElem
= MakeEmptyDOM('sizeritem')
415 sizerItemElem
.appendChild(elem
)
417 elif isinstance(parent
, xxxNotebook
) and not isChildContainer
:
418 pageElem
= MakeEmptyDOM('notebookpage')
419 pageElem
.appendChild(elem
)
421 # Insert new node, register undo
422 newItem
= tree
.InsertNode(parentLeaf
, parent
, elem
, nextItem
)
423 undoMan
.RegisterUndo(UndoPasteCreate(parentLeaf
, parent
, newItem
, selected
))
424 # Scroll to show new item (!!! redundant?)
425 tree
.EnsureVisible(newItem
)
426 tree
.SelectItem(newItem
)
427 if not tree
.IsVisible(newItem
):
428 tree
.ScrollTo(newItem
)
431 if g
.testWin
and tree
.IsHighlatable(newItem
):
433 tree
.needUpdate
= True
434 tree
.pendingHighLight
= newItem
436 tree
.pendingHighLight
= None
438 self
.SetStatusText('Pasted')
440 def OnCutDelete(self
, evt
):
441 selected
= tree
.selection
442 if not selected
: return # key pressed event
444 if evt
.GetId() == wxID_CUT
:
446 status
= 'Removed to clipboard'
448 self
.lastOp
= 'DELETE'
452 # If deleting top-level item, delete testWin
453 if selected
== g
.testWin
.item
:
457 # Remove highlight, update testWin
458 if g
.testWin
.highLight
:
459 g
.testWin
.highLight
.Remove()
460 tree
.needUpdate
= True
463 index
= tree
.ItemFullIndex(selected
)
464 parent
= tree
.GetPyData(tree
.GetItemParent(selected
)).treeObject()
465 elem
= tree
.RemoveLeaf(selected
)
466 undoMan
.RegisterUndo(UndoCutDelete(index
, parent
, elem
))
467 if evt
.GetId() == wxID_CUT
:
468 if self
.clipboard
: self
.clipboard
.unlink()
469 self
.clipboard
= elem
.cloneNode(True)
470 tree
.pendingHighLight
= None
474 self
.SetStatusText(status
)
476 def OnSelect(self
, evt
):
477 print >> sys
.stderr
, 'Xperimental function!'
479 self
.SetCursor(wxCROSS_CURSOR
)
482 def OnLeftDown(self
, evt
):
483 pos
= evt
.GetPosition()
484 self
.SetCursor(wxNullCursor
)
487 def OnEmbedPanel(self
, evt
):
488 conf
.embedPanel
= evt
.IsChecked()
490 # Remember last dimentions
491 conf
.panelX
, conf
.panelY
= self
.miniFrame
.GetPosition()
492 conf
.panelWidth
, conf
.panelHeight
= self
.miniFrame
.GetSize()
493 size
= self
.GetSize()
494 pos
= self
.GetPosition()
495 sizePanel
= panel
.GetSize()
496 panel
.Reparent(self
.splitter
)
497 self
.miniFrame
.GetSizer().RemoveWindow(panel
)
500 self
.SetDimensions(pos
.x
, pos
.y
, size
.x
+ sizePanel
.x
, size
.y
)
501 self
.splitter
.SplitVertically(tree
, panel
, conf
.sashPos
)
502 self
.miniFrame
.Show(False)
504 conf
.sashPos
= self
.splitter
.GetSashPosition()
505 pos
= self
.GetPosition()
506 size
= self
.GetSize()
507 sizePanel
= panel
.GetSize()
508 self
.splitter
.Unsplit(panel
)
509 sizer
= self
.miniFrame
.GetSizer()
510 panel
.Reparent(self
.miniFrame
)
512 sizer
.Add(panel
, 1, wxEXPAND
)
513 self
.miniFrame
.Show(True)
514 self
.miniFrame
.SetDimensions(conf
.panelX
, conf
.panelY
,
515 conf
.panelWidth
, conf
.panelHeight
)
518 self
.SetDimensions(pos
.x
, pos
.y
,
519 max(size
.x
- sizePanel
.x
, self
.minWidth
), size
.y
)
521 def OnShowTools(self
, evt
):
522 conf
.showTools
= evt
.IsChecked()
523 g
.tools
.Show(conf
.showTools
)
525 self
.toolsSizer
.Prepend(g
.tools
, 0, wxEXPAND
)
527 self
.toolsSizer
.Remove(g
.tools
)
528 self
.toolsSizer
.Layout()
530 def OnTest(self
, evt
):
531 if not tree
.selection
: return # key pressed event
532 tree
.ShowTestWindow(tree
.selection
)
534 def OnRefresh(self
, evt
):
535 # If modified, apply first
536 selection
= tree
.selection
538 xxx
= tree
.GetPyData(selection
)
539 if xxx
and panel
.IsModified():
540 tree
.Apply(xxx
, selection
)
543 tree
.CreateTestWin(g
.testWin
.item
)
544 panel
.modified
= False
545 tree
.needUpdate
= False
547 def OnAutoRefresh(self
, evt
):
548 conf
.autoRefresh
= evt
.IsChecked()
549 self
.menuBar
.Check(self
.ID_AUTO_REFRESH
, conf
.autoRefresh
)
550 self
.tb
.ToggleTool(self
.ID_AUTO_REFRESH
, conf
.autoRefresh
)
552 def OnAbout(self
, evt
):
556 (c) Roman Rolinsky <rollrom@users.sourceforge.net>
557 Homepage: http://xrced.sourceforge.net\
559 dlg
= wxMessageDialog(self
, str, 'About XRCed', wxOK | wxCENTRE
)
563 def OnReadme(self
, evt
):
564 text
= open(os
.path
.join(basePath
, 'README.txt'), 'r').read()
565 dlg
= ScrolledMessageDialog(self
, text
, "XRCed README")
569 # Simple emulation of python command line
570 def OnDebugCMD(self
, evt
):
574 exec raw_input('C:\> ')
579 (etype
, value
, tb
) =sys
.exc_info()
580 tblist
=traceback
.extract_tb(tb
)[1:]
581 msg
=' '.join(traceback
.format_exception_only(etype
, value
)
582 +traceback
.format_list(tblist
))
585 def OnCreate(self
, evt
):
586 selected
= tree
.selection
587 if tree
.ctrl
: appendChild
= False
588 else: appendChild
= not tree
.NeedInsert(selected
)
589 xxx
= tree
.GetPyData(selected
)
593 # If has previous item, insert after it, else append to parent
595 parentLeaf
= tree
.GetItemParent(selected
)
597 # If has next item, insert, else append to parent
598 nextItem
= tree
.GetNextSibling(selected
)
599 parentLeaf
= tree
.GetItemParent(selected
)
600 # Expanded container (must have children)
601 elif tree
.shift
and tree
.IsExpanded(selected
) \
602 and tree
.GetChildrenCount(selected
, False):
603 nextItem
= tree
.GetFirstChild(selected
, 0)[0]
604 parentLeaf
= selected
606 nextItem
= wxTreeItemId()
607 parentLeaf
= selected
608 parent
= tree
.GetPyData(parentLeaf
)
609 if parent
.hasChild
: parent
= parent
.child
612 className
= pullDownMenu
.createMap
[evt
.GetId()]
613 xxx
= MakeEmptyXXX(parent
, className
)
615 # Set default name for top-level windows
616 if parent
.__class
__ == xxxMainNode
:
617 cl
= xxx
.treeObject().__class
__
618 frame
.maxIDs
[cl
] += 1
619 xxx
.treeObject().name
= '%s%d' % (defaultIDs
[cl
], frame
.maxIDs
[cl
])
620 xxx
.treeObject().element
.setAttribute('name', xxx
.treeObject().name
)
622 # Insert new node, register undo
624 newItem
= tree
.InsertNode(parentLeaf
, parent
, elem
, nextItem
)
625 undoMan
.RegisterUndo(UndoPasteCreate(parentLeaf
, parent
, newItem
, selected
))
626 tree
.EnsureVisible(newItem
)
627 tree
.SelectItem(newItem
)
628 if not tree
.IsVisible(newItem
):
629 tree
.ScrollTo(newItem
)
632 if g
.testWin
and tree
.IsHighlatable(newItem
):
634 tree
.needUpdate
= True
635 tree
.pendingHighLight
= newItem
637 tree
.pendingHighLight
= None
641 # Replace one object with another
642 def OnReplace(self
, evt
):
643 selected
= tree
.selection
644 xxx
= tree
.GetPyData(selected
).treeObject()
646 parent
= elem
.parentNode
647 parentXXX
= xxx
.parent
649 className
= pullDownMenu
.createMap
[evt
.GetId() - 1000]
650 # Create temporary empty node (with default values)
651 dummy
= MakeEmptyDOM(className
)
652 xxxClass
= xxxDict
[className
]
653 # Remove non-compatible children
654 if tree
.ItemHasChildren(selected
) and not xxxClass
.hasChildren
:
655 tree
.DeleteChildren(selected
)
656 nodes
= elem
.childNodes
[:]
662 if not xxxClass
.hasChildren
:
664 elif tag
not in xxxClass
.allParams
and \
665 (not xxxClass
.hasStyle
or tag
not in xxxClass
.styles
):
670 elem
.removeChild(node
)
673 # Copy parameters present in dummy but not in elem
674 for node
in dummy
.childNodes
:
677 elem
.appendChild(node
.cloneNode(True))
680 elem
.setAttribute('class', className
)
681 # Re-create xxx element
682 xxx
= MakeXXXFromDOM(parentXXX
, elem
)
683 # Update parent in child objects
684 if tree
.ItemHasChildren(selected
):
685 i
, cookie
= tree
.GetFirstChild(selected
, 0)
687 x
= tree
.GetPyData(i
)
689 if x
.hasChild
: x
.child
.parent
= xxx
690 i
, cookie
= tree
.GetNextChild(selected
, cookie
)
693 if tree
.GetPyData(selected
).hasChild
: # child container
694 container
= tree
.GetPyData(selected
)
695 container
.child
= xxx
696 container
.hasChildren
= xxx
.hasChildren
697 container
.isSizer
= xxx
.isSizer
699 tree
.SetPyData(selected
, xxx
)
700 tree
.SetItemText(selected
, xxx
.treeName())
701 tree
.SetItemImage(selected
, xxx
.treeImage())
703 # Set default name for top-level windows
704 if parent
.__class
__ == xxxMainNode
:
705 cl
= xxx
.treeObject().__class
__
706 frame
.maxIDs
[cl
] += 1
707 xxx
.treeObject().name
= '%s%d' % (defaultIDs
[cl
], frame
.maxIDs
[cl
])
708 xxx
.treeObject().element
.setAttribute('name', xxx
.treeObject().name
)
715 #undoMan.RegisterUndo(UndoPasteCreate(parentLeaf, parent, newItem, selected))
717 if g
.testWin
and tree
.IsHighlatable(selected
):
719 tree
.needUpdate
= True
720 tree
.pendingHighLight
= selected
722 tree
.pendingHighLight
= None
726 # Expand/collapse subtree
727 def OnExpand(self
, evt
):
728 if tree
.selection
: tree
.ExpandAll(tree
.selection
)
729 else: tree
.ExpandAll(tree
.root
)
730 def OnCollapse(self
, evt
):
731 if tree
.selection
: tree
.CollapseAll(tree
.selection
)
732 else: tree
.CollapseAll(tree
.root
)
734 def OnPullDownHighlight(self
, evt
):
735 menuId
= evt
.GetMenuId()
737 menu
= evt
.GetEventObject()
738 help = menu
.GetHelpString(menuId
)
739 self
.SetStatusText(help)
741 self
.SetStatusText('')
743 def OnUpdateUI(self
, evt
):
744 if evt
.GetId() in [wxID_CUT
, wxID_COPY
, self
.ID_DELETE
]:
745 evt
.Enable(tree
.selection
is not None and tree
.selection
!= tree
.root
)
746 elif evt
.GetId() == wxID_PASTE
:
747 evt
.Enable((self
.clipboard
and tree
.selection
) != None)
748 elif evt
.GetId() == self
.ID_TEST
:
749 evt
.Enable(tree
.selection
is not None and tree
.selection
!= tree
.root
)
750 elif evt
.GetId() == wxID_UNDO
: evt
.Enable(undoMan
.CanUndo())
751 elif evt
.GetId() == wxID_REDO
: evt
.Enable(undoMan
.CanRedo())
753 def OnIdle(self
, evt
):
754 if self
.inIdle
: return # Recursive call protection
759 self
.SetStatusText('Refreshing test window...')
761 tree
.CreateTestWin(g
.testWin
.item
)
763 self
.SetStatusText('')
764 tree
.needUpdate
= False
765 elif tree
.pendingHighLight
:
766 tree
.HighLight(tree
.pendingHighLight
)
771 # We don't let close panel window
772 def OnCloseMiniFrame(self
, evt
):
775 def OnCloseWindow(self
, evt
):
776 if not self
.AskSave(): return
777 if g
.testWin
: g
.testWin
.Destroy()
778 # Destroy cached windows
779 panel
.cacheParent
.Destroy()
780 if not panel
.GetPageCount() == 2:
781 panel
.page2
.Destroy()
782 conf
.x
, conf
.y
= self
.GetPosition()
783 conf
.width
, conf
.height
= self
.GetSize()
785 conf
.sashPos
= self
.splitter
.GetSashPosition()
787 conf
.panelX
, conf
.panelY
= self
.miniFrame
.GetPosition()
788 conf
.panelWidth
, conf
.panelHeight
= self
.miniFrame
.GetSize()
794 self
.clipboard
.unlink()
795 self
.clipboard
= None
797 self
.modified
= False
803 self
.SetTitle(progname
)
804 # Numbers for new controls
806 self
.maxIDs
[xxxPanel
] = self
.maxIDs
[xxxDialog
] = self
.maxIDs
[xxxFrame
] = \
807 self
.maxIDs
[xxxMenuBar
] = self
.maxIDs
[xxxMenu
] = self
.maxIDs
[xxxToolBar
] = 0
809 def Open(self
, path
):
810 if not os
.path
.exists(path
):
811 wxLogError('File does not exists: %s' % path
)
813 # Try to read the file
817 # Parse first line to get encoding (!! hack, I don't know a better way)
819 mo
= re
.match(r
'^<\?xml ([^<>]* )?encoding="(?P<encd>[^<>].*)"\?>', line
)
822 dom
= minidom
.parse(f
)
823 # Set encoding global variable and document encoding property
825 dom
.encoding
= g
.currentEncoding
= mo
.group('encd')
826 if dom
.encoding
not in ['ascii', sys
.getdefaultencoding()]:
827 wxLogWarning('Encoding is different from system default')
829 g
.currentEncoding
= 'ascii'
833 dir = os
.path
.dirname(path
)
834 if dir: os
.chdir(dir)
837 self
.SetTitle(progname
+ ': ' + os
.path
.basename(path
))
839 # Nice exception printing
841 wxLogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
842 wxLogError('Error reading file: %s' % path
)
846 def Indent(self
, node
, indent
= 0):
847 # Copy child list because it will change soon
848 children
= node
.childNodes
[:]
849 # Main node doesn't need to be indented
851 text
= self
.domCopy
.createTextNode('\n' + ' ' * indent
)
852 node
.parentNode
.insertBefore(text
, node
)
854 # Append newline after last child, except for text nodes
855 if children
[-1].nodeType
== minidom
.Node
.ELEMENT_NODE
:
856 text
= self
.domCopy
.createTextNode('\n' + ' ' * indent
)
857 node
.appendChild(text
)
858 # Indent children which are elements
860 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
861 self
.Indent(n
, indent
+ 2)
863 def Save(self
, path
):
866 if tree
.selection
and panel
.IsModified():
867 self
.OnRefresh(wxCommandEvent())
869 # Make temporary copy for formatting it
870 # !!! We can't clone dom node, it works only once
871 #self.domCopy = tree.dom.cloneNode(True)
872 self
.domCopy
= MyDocument()
873 mainNode
= self
.domCopy
.appendChild(tree
.mainNode
.cloneNode(True))
874 self
.Indent(mainNode
)
875 self
.domCopy
.writexml(f
, encoding
=tree
.rootObj
.params
['encoding'].value())
877 self
.domCopy
.unlink()
879 self
.modified
= False
880 panel
.SetModified(False)
882 wxLogError('Error writing file: %s' % path
)
886 if not (self
.modified
or panel
.IsModified()): return True
887 flags
= wxICON_EXCLAMATION | wxYES_NO | wxCANCEL | wxCENTRE
888 dlg
= wxMessageDialog( self
, 'File is modified. Save before exit?',
889 'Save before too late?', flags
)
890 say
= dlg
.ShowModal()
893 self
.OnSaveOrSaveAs(wxCommandEvent(wxID_SAVE
))
894 # If save was successful, modified flag is unset
895 if not self
.modified
: return True
897 self
.modified
= False
898 panel
.SetModified(False)
905 ################################################################################
908 print >> sys
.stderr
, 'usage: xrced [-dhiv] [file]'
913 # Process comand-line
915 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'dhiv')
916 except getopt
.GetoptError
:
917 if wxPlatform
!= '__WXMAC__': # macs have some extra parameters
918 print >> sys
.stderr
, 'Unknown option'
928 print 'XRCed version', version
931 self
.SetAppName('xrced')
934 conf
= g
.conf
= wxConfig(style
= wxCONFIG_USE_LOCAL_FILE
)
935 conf
.autoRefresh
= conf
.ReadInt('autorefresh', True)
936 pos
= conf
.ReadInt('x', -1), conf
.ReadInt('y', -1)
937 size
= conf
.ReadInt('width', 800), conf
.ReadInt('height', 600)
938 conf
.embedPanel
= conf
.ReadInt('embedPanel', True)
939 conf
.showTools
= conf
.ReadInt('showTools', True)
940 conf
.sashPos
= conf
.ReadInt('sashPos', 200)
941 if not conf
.embedPanel
:
942 conf
.panelX
= conf
.ReadInt('panelX', -1)
943 conf
.panelY
= conf
.ReadInt('panelY', -1)
945 conf
.panelX
= conf
.panelY
= -1
946 conf
.panelWidth
= conf
.ReadInt('panelWidth', 200)
947 conf
.panelHeight
= conf
.ReadInt('panelHeight', 200)
948 conf
.panic
= not conf
.HasEntry('nopanic')
950 wxFileSystem_AddHandler(wxMemoryFSHandler())
951 wxInitAllImageHandlers()
953 frame
= Frame(pos
, size
)
955 # Load resources from XRC file (!!! should be transformed to .py later?)
956 frame
.res
= wxXmlResource('')
957 frame
.res
.Load(os
.path
.join(basePath
, 'xrced.xrc'))
959 # Load file after showing
962 frame
.open = frame
.Open(args
[0])
969 wc
= wxConfigBase_Get()
970 wc
.WriteInt('autorefresh', conf
.autoRefresh
)
971 wc
.WriteInt('x', conf
.x
)
972 wc
.WriteInt('y', conf
.y
)
973 wc
.WriteInt('width', conf
.width
)
974 wc
.WriteInt('height', conf
.height
)
975 wc
.WriteInt('embedPanel', conf
.embedPanel
)
976 wc
.WriteInt('showTools', conf
.showTools
)
977 if not conf
.embedPanel
:
978 wc
.WriteInt('panelX', conf
.panelX
)
979 wc
.WriteInt('panelY', conf
.panelY
)
980 wc
.WriteInt('sashPos', conf
.sashPos
)
981 wc
.WriteInt('panelWidth', conf
.panelWidth
)
982 wc
.WriteInt('panelHeight', conf
.panelHeight
)
983 wc
.WriteInt('nopanic', True)
987 app
= App(0, useBestVisual
=False)
993 if __name__
== '__main__':