2 # Purpose: XRC editor, XML_tree class
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
7 from xxx
import * # xxx imports globals and params
11 # Constant to define standart window name
12 STD_NAME
= '_XRCED_T_W'
18 def __init__(self
, name
):
21 def write(self
, data
):
23 encoding
= g
.currentEncoding
25 encoding
= wxGetDefaultPyEncoding()
27 self
.buffer += data
.encode(encoding
)
28 except UnicodeEncodeError:
29 self
.buffer += data
.encode(encoding
, 'xmlcharrefreplace')
32 wxMemoryFSHandler_AddFile(self
.name
, self
.buffer)
34 ################################################################################
36 # Redefine writing to include encoding
37 class MyDocument(minidom
.Document
):
39 minidom
.Document
.__init
__(self
)
41 def writexml(self
, writer
, indent
="", addindent
="", newl
="", encoding
=""):
42 if encoding
: encdstr
= 'encoding="%s"' % encoding
44 writer
.write('<?xml version="1.0" %s?>\n' % encdstr
)
45 for node
in self
.childNodes
:
46 node
.writexml(writer
, indent
, addindent
, newl
)
48 ################################################################################
50 # Ids for menu commands
60 STATIC_TEXT
= wxNewId()
64 BITMAP_BUTTON
= wxNewId()
65 RADIO_BUTTON
= wxNewId()
66 SPIN_BUTTON
= wxNewId()
67 TOGGLE_BUTTON
= wxNewId()
69 STATIC_BOX
= wxNewId()
75 STATIC_LINE
= wxNewId()
76 STATIC_BITMAP
= wxNewId()
80 SCROLL_BAR
= wxNewId()
83 CHECK_LIST
= wxNewId()
85 SPLITTER_WINDOW
= wxNewId()
86 SCROLLED_WINDOW
= wxNewId()
87 HTML_WINDOW
= wxNewId()
88 CALENDAR_CTRL
= wxNewId()
89 GENERIC_DIR_CTRL
= wxNewId()
93 WIZARD_PAGE
= wxNewId()
94 WIZARD_PAGE_SIMPLE
= wxNewId()
95 STATUS_BAR
= wxNewId()
98 STATIC_BOX_SIZER
= wxNewId()
99 GRID_SIZER
= wxNewId()
100 FLEX_GRID_SIZER
= wxNewId()
101 GRID_BAG_SIZER
= wxNewId()
102 STD_DIALOG_BUTTON_SIZER
= wxNewId()
108 MENU_ITEM
= wxNewId()
109 SEPARATOR
= wxNewId()
111 OK_BUTTON
= wxNewId()
112 YES_BUTTON
= wxNewId()
113 SAVE_BUTTON
= wxNewId()
114 APPLY_BUTTON
= wxNewId()
115 NO_BUTTON
= wxNewId()
116 CANCEL_BUTTON
= wxNewId()
117 HELP_BUTTON
= wxNewId()
118 CONTEXT_HELP_BUTTON
= wxNewId()
125 ID_EXPAND
= wxNewId()
126 ID_COLLAPSE
= wxNewId()
127 ID_PASTE_SIBLING
= wxNewId()
128 ID_TOOL_PASTE
= wxNewId()
129 ID_SUBCLASS
= wxNewId()
131 def __init__(self
, parent
):
132 self
.ID_DELETE
= parent
.ID_DELETE
133 EVT_MENU_RANGE(parent
, ID_NEW
.PANEL
, ID_NEW
.LAST
, parent
.OnCreate
)
134 EVT_MENU_RANGE(parent
, 1000 + ID_NEW
.PANEL
, 1000 + ID_NEW
.LAST
, parent
.OnReplace
)
135 EVT_MENU(parent
, self
.ID_COLLAPSE
, parent
.OnCollapse
)
136 EVT_MENU(parent
, self
.ID_EXPAND
, parent
.OnExpand
)
137 EVT_MENU(parent
, self
.ID_PASTE_SIBLING
, parent
.OnPaste
)
138 EVT_MENU(parent
, self
.ID_SUBCLASS
, parent
.OnSubclass
)
139 # We connect to tree, but process in frame
140 EVT_MENU_HIGHLIGHT_ALL(g
.tree
, parent
.OnPullDownHighlight
)
142 # Mapping from IDs to element names
144 ID_NEW
.PANEL
: 'wxPanel',
145 ID_NEW
.DIALOG
: 'wxDialog',
146 ID_NEW
.FRAME
: 'wxFrame',
147 ID_NEW
.WIZARD
: 'wxWizard',
148 ID_NEW
.WIZARD_PAGE
: 'wxWizardPage',
149 ID_NEW
.WIZARD_PAGE_SIMPLE
: 'wxWizardPageSimple',
150 ID_NEW
.TOOL_BAR
: 'wxToolBar',
152 ID_NEW
.MENU_BAR
: 'wxMenuBar',
153 ID_NEW
.MENU
: 'wxMenu',
154 ID_NEW
.MENU_ITEM
: 'wxMenuItem',
155 ID_NEW
.SEPARATOR
: 'separator',
157 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
158 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
160 ID_NEW
.BUTTON
: 'wxButton',
161 ID_NEW
.BITMAP_BUTTON
: 'wxBitmapButton',
162 ID_NEW
.RADIO_BUTTON
: 'wxRadioButton',
163 ID_NEW
.SPIN_BUTTON
: 'wxSpinButton',
164 ID_NEW
.TOGGLE_BUTTON
: 'wxToggleButton',
166 ID_NEW
.STATIC_BOX
: 'wxStaticBox',
167 ID_NEW
.CHECK_BOX
: 'wxCheckBox',
168 ID_NEW
.RADIO_BOX
: 'wxRadioBox',
169 ID_NEW
.COMBO_BOX
: 'wxComboBox',
170 ID_NEW
.LIST_BOX
: 'wxListBox',
172 ID_NEW
.STATIC_LINE
: 'wxStaticLine',
173 ID_NEW
.STATIC_BITMAP
: 'wxStaticBitmap',
174 ID_NEW
.CHOICE
: 'wxChoice',
175 ID_NEW
.SLIDER
: 'wxSlider',
176 ID_NEW
.GAUGE
: 'wxGauge',
177 ID_NEW
.SCROLL_BAR
: 'wxScrollBar',
178 ID_NEW
.TREE_CTRL
: 'wxTreeCtrl',
179 ID_NEW
.LIST_CTRL
: 'wxListCtrl',
180 ID_NEW
.CHECK_LIST
: 'wxCheckListBox',
181 ID_NEW
.NOTEBOOK
: 'wxNotebook',
182 ID_NEW
.SPLITTER_WINDOW
: 'wxSplitterWindow',
183 ID_NEW
.SCROLLED_WINDOW
: 'wxScrolledWindow',
184 ID_NEW
.HTML_WINDOW
: 'wxHtmlWindow',
185 ID_NEW
.CALENDAR_CTRL
: 'wxCalendarCtrl',
186 ID_NEW
.GENERIC_DIR_CTRL
: 'wxGenericDirCtrl',
187 ID_NEW
.SPIN_CTRL
: 'wxSpinCtrl',
189 ID_NEW
.BOX_SIZER
: 'wxBoxSizer',
190 ID_NEW
.STATIC_BOX_SIZER
: 'wxStaticBoxSizer',
191 ID_NEW
.GRID_SIZER
: 'wxGridSizer',
192 ID_NEW
.FLEX_GRID_SIZER
: 'wxFlexGridSizer',
193 ID_NEW
.GRID_BAG_SIZER
: 'wxGridBagSizer',
194 ID_NEW
.STD_DIALOG_BUTTON_SIZER
: 'wxStdDialogButtonSizer',
195 ID_NEW
.SPACER
: 'spacer',
196 ID_NEW
.UNKNOWN
: 'unknown',
198 ID_NEW
.OK_BUTTON
: 'wxButton',
199 ID_NEW
.YES_BUTTON
: 'wxButton',
200 ID_NEW
.SAVE_BUTTON
: 'wxButton',
201 ID_NEW
.APPLY_BUTTON
: 'wxButton',
202 ID_NEW
.NO_BUTTON
: 'wxButton',
203 ID_NEW
.CANCEL_BUTTON
: 'wxButton',
204 ID_NEW
.HELP_BUTTON
: 'wxButton',
205 ID_NEW
.CONTEXT_HELP_BUTTON
: 'wxButton',
208 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
209 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
210 (ID_NEW
.FRAME
, 'Frame', 'Create frame'),
211 (ID_NEW
.WIZARD
, 'Wizard', 'Create wizard'),
213 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
214 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
215 (ID_NEW
.MENU
, 'Menu', 'Create menu')
218 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
219 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
220 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
221 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
222 # (ID_NEW.WIZARD_PAGE, 'WizardPage', 'Create wizard page'),
223 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
226 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
227 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
228 'Create static box sizer'),
229 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
230 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
231 'Create flexgrid sizer'),
232 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
233 'Create gridbag sizer'),
234 # (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
235 # 'Create standard button sizer'),
236 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
239 ['control', 'Various controls',
240 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
241 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
242 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
243 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
244 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
245 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
246 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
247 (ID_NEW
.SPIN_CTRL
, 'SpinCtrl', 'Create spin'),
248 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
249 (ID_NEW
.TREE_CTRL
, 'TreeCtrl', 'Create tree'),
250 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list'),
251 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
252 (ID_NEW
.SCROLLED_WINDOW
, 'ScrolledWindow', 'Create scrolled window'),
253 (ID_NEW
.HTML_WINDOW
, 'HtmlWindow', 'Create HTML window'),
254 (ID_NEW
.CALENDAR_CTRL
, 'CalendarCtrl', 'Create calendar control'),
255 (ID_NEW
.GENERIC_DIR_CTRL
, 'GenericDirCtrl', 'Create generic dir control'),
256 (ID_NEW
.UNKNOWN
, 'Unknown', 'Create custom control placeholder'),
258 ['button', 'Buttons',
259 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
260 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
261 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
262 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
263 (ID_NEW
.TOGGLE_BUTTON
, 'ToggleButton', 'Create toggle button'),
266 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
267 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
268 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
269 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
270 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
272 ['container', 'Containers',
273 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
274 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
275 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
276 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
277 # (ID_NEW.WIZARD_PAGE, 'Wizard Page', 'Create wizard page'),
278 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
281 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
282 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
283 'Create static box sizer'),
284 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
285 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
286 'Create flexgrid sizer'),
287 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
288 'Create gridbag sizer'),
289 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
290 (ID_NEW
.STD_DIALOG_BUTTON_SIZER
, 'StdDialogButtonSizer',
291 'Create standard button sizer'),
294 self
.menuControls
= [
295 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
296 (ID_NEW
.MENU_ITEM
, 'MenuItem', 'Create menu item'),
297 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
299 self
.toolBarControls
= [
300 (ID_NEW
.TOOL
, 'Tool', 'Create tool'),
301 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
302 ['control', 'Various controls',
303 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
304 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
305 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
306 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
307 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
308 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
309 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
310 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
311 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list control'),
312 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
314 ['button', 'Buttons',
315 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
316 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
317 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
318 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
321 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
322 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
323 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
324 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
325 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
329 (ID_NEW
.OK_BUTTON
, 'OK Button', 'Create standard button'),
330 (ID_NEW
.YES_BUTTON
, 'YES Button', 'Create standard button'),
331 (ID_NEW
.SAVE_BUTTON
, 'SAVE Button', 'Create standard button'),
332 (ID_NEW
.APPLY_BUTTON
, 'APPLY Button', 'Create standard button'),
333 (ID_NEW
.NO_BUTTON
, 'NO Button', 'Create standard button'),
334 (ID_NEW
.CANCEL_BUTTON
, 'CANCEL Button', 'Create standard button'),
335 (ID_NEW
.HELP_BUTTON
, 'HELP Button', 'Create standard button'),
336 (ID_NEW
.CONTEXT_HELP_BUTTON
, 'CONTEXT HELP Button', 'Create standard button'),
338 self
.stdButtonIDs
= {
339 ID_NEW
.OK_BUTTON
: ('wxID_OK', '&Ok'),
340 ID_NEW
.YES_BUTTON
: ('wxID_YES', '&Yes'),
341 ID_NEW
.SAVE_BUTTON
: ('wxID_SAVE', '&Save'),
342 ID_NEW
.APPLY_BUTTON
: ('wxID_APPLY', '&Apply'),
343 ID_NEW
.NO_BUTTON
: ('wxID_NO', '&No'),
344 ID_NEW
.CANCEL_BUTTON
: ('wxID_CANCEL', '&Cancel'),
345 ID_NEW
.HELP_BUTTON
: ('wxID_HELP', '&Help'),
346 ID_NEW
.CONTEXT_HELP_BUTTON
: ('wxID_CONTEXT_HELP', '&Help'),
351 ################################################################################
353 # Set menu to list items.
354 # Each menu command is a tuple (id, label, help)
355 # submenus are lists [id, label, help, submenu]
356 # and separators are any other type. Shift is for making
357 # alternative sets of IDs. (+1000).
358 def SetMenu(m
, list, shift
=False):
360 if type(l
) == types
.TupleType
:
362 if shift
: l
= (1000 + l
[0],) + l
[1:]
364 elif type(l
) == types
.ListType
:
366 SetMenu(subMenu
, l
[2:])
367 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
371 ################################################################################
374 def __init__(self
, pos
, size
):
375 if size
.width
== -1: size
.width
= 0
376 if size
.height
== -1: size
.height
= 0
378 l1
= wxWindow(w
, -1, pos
, wxSize(size
.width
, 2))
379 l1
.SetBackgroundColour(wxRED
)
380 l2
= wxWindow(w
, -1, pos
, wxSize(2, size
.height
))
381 l2
.SetBackgroundColour(wxRED
)
382 l3
= wxWindow(w
, -1, wxPoint(pos
.x
+ size
.width
- 2, pos
.y
), wxSize(2, size
.height
))
383 l3
.SetBackgroundColour(wxRED
)
384 l4
= wxWindow(w
, -1, wxPoint(pos
.x
, pos
.y
+ size
.height
- 2), wxSize(size
.width
, 2))
385 l4
.SetBackgroundColour(wxRED
)
386 self
.lines
= [l1
, l2
, l3
, l4
]
387 # Move highlight to a new position
388 def Replace(self
, pos
, size
):
389 if size
.width
== -1: size
.width
= 0
390 if size
.height
== -1: size
.height
= 0
391 self
.lines
[0].SetDimensions(pos
.x
, pos
.y
, size
.width
, 2)
392 self
.lines
[1].SetDimensions(pos
.x
, pos
.y
, 2, size
.height
)
393 self
.lines
[2].SetDimensions(pos
.x
+ size
.width
- 2, pos
.y
, 2, size
.height
)
394 self
.lines
[3].SetDimensions(pos
.x
, pos
.y
+ size
.height
- 2, size
.width
, 2)
397 map(wxWindow
.Destroy
, self
.lines
)
398 g
.testWin
.highLight
= None
400 map(wxWindow
.Refresh
, self
.lines
)
402 ################################################################################
404 class XML_Tree(wxTreeCtrl
):
405 def __init__(self
, parent
, id):
406 wxTreeCtrl
.__init
__(self
, parent
, id, style
= wxTR_HAS_BUTTONS
)
407 self
.SetBackgroundColour(wxColour(224, 248, 224))
409 EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
410 # One works on Linux, another on Windows
411 if wxPlatform
== '__WXGTK__':
412 EVT_TREE_ITEM_ACTIVATED(self
, self
.GetId(), self
.OnItemActivated
)
414 EVT_LEFT_DCLICK(self
, self
.OnDClick
)
415 EVT_RIGHT_DOWN(self
, self
.OnRightDown
)
416 EVT_TREE_ITEM_EXPANDED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
417 EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
419 self
.selection
= None
420 self
.needUpdate
= False
421 self
.pendingHighLight
= None
422 self
.ctrl
= self
.shift
= False
425 il
= wxImageList(16, 16, True)
426 self
.rootImage
= il
.Add(images
.getTreeRootImage().Scale(16,16).ConvertToBitmap())
427 xxxObject
.image
= il
.Add(images
.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
428 xxxPanel
.image
= il
.Add(images
.getTreePanelImage().Scale(16,16).ConvertToBitmap())
429 xxxDialog
.image
= il
.Add(images
.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
430 xxxFrame
.image
= il
.Add(images
.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
431 xxxMenuBar
.image
= il
.Add(images
.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
432 xxxMenu
.image
= il
.Add(images
.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
433 xxxMenuItem
.image
= il
.Add(images
.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
434 xxxToolBar
.image
= il
.Add(images
.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
435 xxxTool
.image
= il
.Add(images
.getTreeToolImage().Scale(16,16).ConvertToBitmap())
436 xxxSeparator
.image
= il
.Add(images
.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
437 xxxSizer
.imageH
= il
.Add(images
.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
438 xxxSizer
.imageV
= il
.Add(images
.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
439 xxxStaticBoxSizer
.imageH
= il
.Add(images
.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
440 xxxStaticBoxSizer
.imageV
= il
.Add(images
.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
441 xxxGridSizer
.image
= il
.Add(images
.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
442 xxxFlexGridSizer
.image
= il
.Add(images
.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
444 self
.SetImageList(il
)
446 def RegisterKeyEvents(self
):
447 EVT_KEY_DOWN(self
, g
.tools
.OnKeyDown
)
448 EVT_KEY_UP(self
, g
.tools
.OnKeyUp
)
449 EVT_ENTER_WINDOW(self
, g
.tools
.OnMouse
)
450 EVT_LEAVE_WINDOW(self
, g
.tools
.OnMouse
)
453 self
.selection
= None
454 wxTreeCtrl
.Unselect(self
)
457 def ExpandAll(self
, item
):
458 if self
.ItemHasChildren(item
):
460 i
, cookie
= self
.GetFirstChild(item
)
464 i
, cookie
= self
.GetNextChild(item
, cookie
)
467 def CollapseAll(self
, item
):
468 if self
.ItemHasChildren(item
):
469 i
, cookie
= self
.GetFirstChild(item
)
473 i
, cookie
= self
.GetNextChild(item
, cookie
)
480 self
.DeleteAllItems()
481 # Add minimal structure
482 if self
.dom
: self
.dom
.unlink()
483 self
.dom
= MyDocument()
484 self
.dummyNode
= self
.dom
.createComment('dummy node')
486 self
.mainNode
= self
.dom
.createElement('resource')
487 self
.dom
.appendChild(self
.mainNode
)
488 self
.rootObj
= xxxMainNode(self
.dom
)
489 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
490 data
=wxTreeItemData(self
.rootObj
))
491 self
.SetItemHasChildren(self
.root
)
493 self
.Expand(self
.root
)
495 # Clear old data and set new
496 def SetData(self
, dom
):
497 self
.DeleteAllItems()
498 # Add minimal structure
499 if self
.dom
: self
.dom
.unlink()
501 self
.dummyNode
= self
.dom
.createComment('dummy node')
502 # Find 'resource' child, add it's children
503 self
.mainNode
= dom
.documentElement
504 self
.rootObj
= xxxMainNode(self
.dom
)
505 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
506 data
=wxTreeItemData(self
.rootObj
))
507 self
.SetItemHasChildren(self
.root
)
508 nodes
= self
.mainNode
.childNodes
[:]
511 self
.AddNode(self
.root
, None, node
)
513 self
.mainNode
.removeChild(node
)
515 self
.Expand(self
.root
)
518 # Add tree item for given parent item if node is DOM element node with
519 # 'object' tag. xxxParent is parent xxx object
520 def AddNode(self
, itemParent
, xxxParent
, node
):
521 # Set item data to current node
523 xxx
= MakeXXXFromDOM(xxxParent
, node
)
525 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
527 treeObj
= xxx
.treeObject()
529 item
= self
.AppendItem(itemParent
, treeObj
.treeName(),
530 image
=treeObj
.treeImage(),
531 data
=wxTreeItemData(xxx
))
532 # Try to find children objects
533 if treeObj
.hasChildren
:
534 nodes
= treeObj
.element
.childNodes
[:]
537 self
.AddNode(item
, treeObj
, n
)
538 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
539 treeObj
.element
.removeChild(n
)
542 # Insert new item at specific position
543 def InsertNode(self
, itemParent
, parent
, elem
, nextItem
):
544 # Insert in XML tree and wxWin
545 xxx
= MakeXXXFromDOM(parent
, elem
)
546 # If nextItem is None, we append to parent, otherwise insert before it
548 node
= self
.GetPyData(nextItem
).element
549 parent
.element
.insertBefore(elem
, node
)
550 # Inserting before is difficult, se we insert after or first child
551 index
= self
.ItemIndex(nextItem
)
552 newItem
= self
.InsertItemBefore(itemParent
, index
,
553 xxx
.treeName(), image
=xxx
.treeImage())
554 self
.SetPyData(newItem
, xxx
)
556 parent
.element
.appendChild(elem
)
557 newItem
= self
.AppendItem(itemParent
, xxx
.treeName(), image
=xxx
.treeImage(),
558 data
=wxTreeItemData(xxx
))
561 treeObj
= xxx
.treeObject()
562 for n
in treeObj
.element
.childNodes
:
564 self
.AddNode(newItem
, treeObj
, n
)
567 # Remove leaf of tree, return it's data object
568 def RemoveLeaf(self
, leaf
):
569 xxx
= self
.GetPyData(leaf
)
571 parent
= node
.parentNode
572 parent
.removeChild(node
)
574 # Reset selection object
575 self
.selection
= None
577 # Find position relative to the top-level window
578 def FindNodePos(self
, item
, obj
=None):
580 if item
== g
.testWin
.item
: return wxPoint(0, 0)
581 itemParent
= self
.GetItemParent(item
)
583 if not obj
: obj
= self
.FindNodeObject(item
)
584 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
585 notebook
= self
.FindNodeObject(itemParent
)
587 for i
in range(notebook
.GetPageCount()):
588 if notebook
.GetPage(i
) == obj
:
589 if notebook
.GetSelection() != i
:
590 notebook
.SetSelection(i
)
591 # Remove highlight - otherwise highlight window won't be visible
592 if g
.testWin
.highLight
:
593 g
.testWin
.highLight
.Remove()
595 # Find first ancestor which is a wxWindow (not a sizer)
596 winParent
= itemParent
597 while self
.GetPyData(winParent
).isSizer
:
598 winParent
= self
.GetItemParent(winParent
)
599 # Notebook children are layed out in a little strange way
600 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
601 parentPos
= wxPoint(0,0)
603 parentPos
= self
.FindNodePos(winParent
)
604 # Position (-1,-1) is really (0,0)
605 pos
= obj
.GetPosition()
606 if pos
== (-1,-1): pos
= (0,0)
607 return parentPos
+ pos
609 # Find window (or sizer) corresponding to a tree item.
610 def FindNodeObject(self
, item
):
612 # If top-level, return testWin (or panel its panel)
613 if item
== testWin
.item
: return testWin
.panel
614 itemParent
= self
.GetItemParent(item
)
615 xxx
= self
.GetPyData(item
).treeObject()
616 parentWin
= self
.FindNodeObject(itemParent
)
617 # Top-level sizer? return window's sizer
618 if xxx
.isSizer
and isinstance(parentWin
, wxWindow
):
619 return parentWin
.GetSizer()
620 elif isinstance(xxx
.parent
, xxxToolBar
):
621 # How to get tool from toolbar?
622 return parentWin
.GetChildren()[0]
623 elif isinstance(xxx
.parent
, xxxStdDialogButtonSizer
):
624 # This sizer returns non-existing children
625 for ch
in parentWin
.GetChildren():
626 if ch
.GetWindow() and ch
.GetWindow().GetName() == xxx
.name
:
627 return ch
.GetWindow()
629 # Otherwise get parent's object and it's child
630 child
= parentWin
.GetChildren()[self
.ItemIndex(item
)]
631 # Return window or sizer for sizer items
632 if child
.GetClassName() == 'wxSizerItem':
633 if child
.IsWindow(): child
= child
.GetWindow()
634 elif child
.IsSizer():
635 child
= child
.GetSizer()
636 # Test for notebook sizers
637 if isinstance(child
, wxNotebookSizer
):
638 child
= child
.GetNotebook()
641 def OnSelChanged(self
, evt
):
642 self
.ChangeSelection(evt
.GetItem())
644 def ChangeSelection(self
, item
):
646 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
647 #oldItem = evt.GetOldItem()
649 oldItem
= self
.selection
651 xxx
= self
.GetPyData(oldItem
)
652 # If some data was modified, apply changes
653 if g
.panel
.IsModified():
654 self
.Apply(xxx
, oldItem
)
655 #if conf.autoRefresh:
657 if g
.testWin
.highLight
:
658 g
.testWin
.highLight
.Remove()
659 self
.needUpdate
= True
660 status
= 'Changes were applied'
661 g
.frame
.SetStatusText(status
)
663 self
.selection
= item
664 if not self
.selection
.IsOk():
665 self
.selection
= None
667 xxx
= self
.GetPyData(self
.selection
)
672 # Highlighting is done in OnIdle
673 self
.pendingHighLight
= self
.selection
675 # Check if item is in testWin subtree
676 def IsHighlatable(self
, item
):
677 if item
== g
.testWin
.item
: return False
678 while item
!= self
.root
:
679 item
= self
.GetItemParent(item
)
680 if item
== g
.testWin
.item
: return True
683 # Highlight selected item
684 def HighLight(self
, item
):
685 self
.pendingHighLight
= None
686 # Can highlight only with some top-level windows
687 if not g
.testWin
or self
.GetPyData(g
.testWin
.item
).treeObject().__class
__ \
688 not in [xxxDialog
, xxxPanel
, xxxFrame
]:
690 # If a control from another window is selected, remove highlight
691 if not self
.IsHighlatable(item
):
692 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
694 # Get window/sizer object
695 obj
= self
.FindNodeObject(item
)
697 pos
= self
.FindNodePos(item
, obj
)
700 # Negative positions are not working quite well
701 if g
.testWin
.highLight
:
702 g
.testWin
.highLight
.Replace(pos
, size
)
704 g
.testWin
.highLight
= HighLightBox(pos
, size
)
705 g
.testWin
.highLight
.Refresh()
706 g
.testWin
.highLight
.item
= item
708 def ShowTestWindow(self
, item
):
709 xxx
= self
.GetPyData(item
)
710 if g
.panel
.IsModified():
711 self
.Apply(xxx
, item
) # apply changes
712 treeObj
= xxx
.treeObject()
713 if treeObj
.className
not in ['wxFrame', 'wxPanel', 'wxDialog',
714 'wxMenuBar', 'wxToolBar', 'wxWizard',
715 'wxWizardPageSimple']:
716 wxLogMessage('No view for this element (yet)')
719 if g
.testWin
: # Reset old
720 self
.SetItemBold(g
.testWin
.item
, False)
721 self
.CreateTestWin(item
)
722 # Maybe an error occurred, so we need to test
723 if g
.testWin
: self
.SetItemBold(g
.testWin
.item
)
725 # Double-click on Linux
726 def OnItemActivated(self
, evt
):
727 if evt
.GetItem() != self
.root
:
728 self
.ShowTestWindow(evt
.GetItem())
730 # Double-click on Windows
731 def OnDClick(self
, evt
):
732 item
, flags
= self
.HitTest(evt
.GetPosition())
733 if flags
in [wxTREE_HITTEST_ONITEMBUTTON
, wxTREE_HITTEST_ONITEMLABEL
]:
734 if item
!= self
.root
: self
.ShowTestWindow(item
)
738 def OnItemExpandedCollapsed(self
, evt
):
739 # Update tool palette
743 # (re)create test window
744 def CreateTestWin(self
, item
):
746 # Create a window with this resource
747 xxx
= self
.GetPyData(item
).treeObject()
750 # if xxx.__class__ == xxxFrame:
751 # Frame can't have many children,
752 # but it's first child possibly can...
753 # child = self.GetFirstChild(item)[0]
754 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
755 # # Clean-up before recursive call or error
756 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
758 # self.CreateTestWin(child)
763 # Close old window, remember where it was
766 pos
= testWin
.GetPosition()
767 if item
== testWin
.item
:
768 # Remember highlight if same top-level window
769 if testWin
.highLight
:
770 highLight
= testWin
.highLight
.item
771 if xxx
.className
== 'wxPanel':
772 if testWin
.highLight
:
773 testWin
.pendingHighLight
= highLight
774 testWin
.highLight
.Remove()
775 testWin
.panel
.Destroy()
779 testWin
= g
.testWin
= None
782 testWin
= g
.testWin
= None
786 memFile
= MemoryFile('xxx.xrc')
787 # Create partial XML file - faster for big files
790 mainNode
= dom
.createElement('resource')
791 dom
.appendChild(mainNode
)
793 # Remove temporarily from old parent
795 # Change window id to _XRCED_T_W. This gives some name for
796 # unnamed windows, and for named gives the possibility to
797 # write sawfish scripts.
802 elem
.setAttribute('name', STD_NAME
)
803 # Replace wizard page class temporarily
804 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
805 oldCl
= elem
.getAttribute('class')
806 elem
.setAttribute('class', 'wxPanel')
807 parent
= elem
.parentNode
808 next
= elem
.nextSibling
809 parent
.replaceChild(self
.dummyNode
, elem
)
810 # Append to new DOM, write it
811 mainNode
.appendChild(elem
)
812 dom
.writexml(memFile
, encoding
=self
.rootObj
.params
['encoding'].value())
814 mainNode
.removeChild(elem
)
816 parent
.replaceChild(elem
, self
.dummyNode
)
817 # Remove temporary name or restore changed
819 elem
.removeAttribute('name')
821 elem
.setAttribute('name', xxx
.name
)
822 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
823 elem
.setAttribute('class', oldCl
)
824 memFile
.close() # write to wxMemoryFS
825 xmlFlags
= wxXRC_NO_SUBCLASSING
826 # Use translations if encoding is not specified
827 if not g
.currentEncoding
:
828 xmlFlags
!= wxXRC_USE_LOCALE
829 res
= wxXmlResource('', xmlFlags
)
830 res
.Load('memory:xxx.xrc')
832 if xxx
.__class
__ == xxxFrame
:
833 # Frame can't have many children,
834 # but it's first child possibly can...
835 # child = self.GetFirstChild(item)[0]
836 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
837 # # Clean-up before recursive call or error
838 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
840 # self.CreateTestWin(child)
842 # This currently works under GTK, but not under MSW
843 testWin
= g
.testWin
= wxPreFrame()
844 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
846 testWin
.panel
= testWin
847 testWin
.CreateStatusBar()
848 testWin
.SetClientSize(testWin
.GetBestSize())
849 testWin
.SetPosition(pos
)
851 elif xxx
.__class
__ == xxxPanel
:
854 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Panel: ' + name
,
855 pos
=pos
, name
=STD_NAME
)
856 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
857 testWin
.SetClientSize(testWin
.GetBestSize())
859 elif xxx
.__class
__ == xxxDialog
:
860 testWin
= g
.testWin
= res
.LoadDialog(None, STD_NAME
)
861 testWin
.panel
= testWin
863 testWin
.SetPosition(pos
)
865 # Dialog's default code does not produce EVT_CLOSE
866 EVT_BUTTON(testWin
, wxID_OK
, self
.OnCloseTestWin
)
867 EVT_BUTTON(testWin
, wxID_CANCEL
, self
.OnCloseTestWin
)
868 elif xxx
.__class
__ == xxxWizard
:
870 res
.LoadOnObject(wiz
, None, STD_NAME
, 'wxWizard')
871 # Find first page (don't know better way)
873 for w
in wiz
.GetChildren():
874 if isinstance(w
, wxWizardPage
):
878 wxLogError('Wizard is empty')
880 # Wizard should be modal
881 self
.SetItemBold(item
)
883 self
.SetItemBold(item
, False)
885 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
888 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Wizard page: ' + name
,
889 pos
=pos
, name
=STD_NAME
)
890 testWin
.panel
= wxPrePanel()
891 res
.LoadOnObject(testWin
.panel
, testWin
, STD_NAME
, 'wxPanel')
892 testWin
.SetClientSize(testWin
.GetBestSize())
894 elif xxx
.__class
__ == xxxMenuBar
:
895 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'MenuBar: ' + name
,
896 pos
=pos
, name
=STD_NAME
)
898 # Set status bar to display help
899 testWin
.CreateStatusBar()
900 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
901 testWin
.SetMenuBar(testWin
.menuBar
)
903 elif xxx
.__class
__ == xxxToolBar
:
904 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'ToolBar: ' + name
,
905 pos
=pos
, name
=STD_NAME
)
907 # Set status bar to display help
908 testWin
.CreateStatusBar()
909 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
910 testWin
.SetToolBar(testWin
.toolBar
)
914 EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
915 testWin
.highLight
= None
916 if highLight
and not self
.pendingHighLight
:
917 self
.HighLight(highLight
)
920 self
.SetItemBold(item
, False)
921 g
.testWinPos
= g
.testWin
.GetPosition()
925 wxLogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
926 wxLogError('Error loading resource')
927 wxMemoryFSHandler_RemoveFile('xxx.xrc')
930 def CloseTestWindow(self
):
931 if not g
.testWin
: return
932 self
.SetItemBold(g
.testWin
.item
, False)
933 g
.frame
.tb
.ToggleTool(g
.frame
.ID_TOOL_LOCATE
, False)
934 g
.testWinPos
= g
.testWin
.GetPosition()
938 def OnCloseTestWin(self
, evt
):
939 self
.CloseTestWindow()
941 # Return item index in parent
942 def ItemIndex(self
, item
):
943 n
= 0 # index of sibling
944 prev
= self
.GetPrevSibling(item
)
946 prev
= self
.GetPrevSibling(prev
)
950 # Full tree index of an item - list of positions
951 def ItemFullIndex(self
, item
):
952 if not item
.IsOk(): return None
954 while item
!= self
.root
:
955 l
.insert(0, self
.ItemIndex(item
))
956 item
= self
.GetItemParent(item
)
958 # Get item position from full index
959 def ItemAtFullIndex(self
, index
):
960 if index
is None: return wxTreeItemId()
963 item
= self
.GetFirstChild(item
)[0]
964 for k
in range(i
): item
= self
.GetNextSibling(item
)
967 # True if next item should be inserted after current (vs. appended to it)
968 def NeedInsert(self
, item
):
969 xxx
= self
.GetPyData(item
)
970 if item
== self
.root
: return False # root item
971 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
973 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
976 def OnRightDown(self
, evt
):
977 pullDownMenu
= g
.pullDownMenu
979 pt
= evt
.GetPosition();
980 item
, flags
= self
.HitTest(pt
)
981 if item
.Ok() and flags
& wxTREE_HITTEST_ONITEM
:
982 self
.SelectItem(item
)
987 item
= self
.selection
989 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
990 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
992 # self.ctrl = evt.ControlDown() # save Ctrl state
993 # self.shift = evt.ShiftDown() # and Shift too
994 m
= wxMenu() # create menu
998 needInsert
= self
.NeedInsert(item
)
999 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
1000 SetMenu(m
, pullDownMenu
.topLevel
)
1002 xxx
= self
.GetPyData(item
).treeObject()
1003 # Check parent for possible child nodes if inserting sibling
1004 if needInsert
: xxx
= xxx
.parent
1005 if xxx
.__class
__ == xxxMenuBar
:
1006 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
1007 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
1008 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
1009 SetMenu(m
, pullDownMenu
.toolBarControls
)
1010 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1011 SetMenu(m
, pullDownMenu
.menuControls
)
1012 elif xxx
.__class
__ == xxxStdDialogButtonSizer
:
1013 SetMenu(m
, pullDownMenu
.stdButtons
)
1015 SetMenu(m
, pullDownMenu
.controls
)
1016 if xxx
.__class
__ == xxxNotebook
:
1017 m
.Enable(m
.FindItem('sizer'), False)
1018 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
1019 m
.Enable(ID_NEW
.SPACER
, False)
1020 # Select correct label for create menu
1023 menu
.AppendMenu(wxNewId(), 'Insert Child', m
,
1024 'Create child object as the first child')
1026 menu
.AppendMenu(wxNewId(), 'Append Child', m
,
1027 'Create child object as the last child')
1030 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
1031 'Create sibling before selected object')
1033 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
1034 'Create sibling after selected object')
1035 # Build replace menu
1036 if item
!= self
.root
:
1037 xxx
= self
.GetPyData(item
).treeObject()
1038 m
= wxMenu() # create replace menu
1039 if xxx
.__class
__ == xxxMenuBar
:
1040 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
1041 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1042 SetMenu(m
, pullDownMenu
.menuControls
, shift
=True)
1043 elif xxx
.__class
__ == xxxToolBar
and \
1044 self
.GetItemParent(item
) == self
.root
:
1045 SetMenu(m
, [], shift
=True)
1046 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
1048 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
1049 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
1050 (ID_NEW
.FRAME
, 'Frame', 'Create frame')], shift
=True)
1052 SetMenu(m
, pullDownMenu
.sizers
, shift
=True)
1054 SetMenu(m
, pullDownMenu
.controls
, shift
=True)
1056 menu
.AppendMenu(id, 'Replace With', m
)
1057 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
1058 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
1059 'Set subclass property')
1060 menu
.AppendSeparator()
1061 # Not using standart IDs because we don't want to show shortcuts
1062 menu
.Append(wxID_CUT
, 'Cut', 'Cut to the clipboard')
1063 menu
.Append(wxID_COPY
, 'Copy', 'Copy to the clipboard')
1064 if self
.ctrl
and item
!= self
.root
:
1065 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
1066 'Paste from the clipboard as a sibling')
1068 menu
.Append(wxID_PASTE
, 'Paste', 'Paste from the clipboard')
1069 menu
.Append(pullDownMenu
.ID_DELETE
,
1070 'Delete', 'Delete object')
1071 if self
.ItemHasChildren(item
):
1072 menu
.AppendSeparator()
1073 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
1074 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
1075 self
.PopupMenu(menu
, evt
.GetPosition())
1079 def Apply(self
, xxx
, item
):
1082 xxx
= xxx
.treeObject()
1083 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
1084 self
.SetItemText(item
, xxx
.treeName())
1085 # Item width may have changed
1086 # !!! Tric to update tree width (wxGTK, ??)
1087 self
.SetIndent(self
.GetIndent())
1088 # Change tree icon for sizers
1089 if isinstance(xxx
, xxxBoxSizer
):
1090 self
.SetItemImage(item
, xxx
.treeImage())
1091 # Set global modified state
1092 g
.frame
.modified
= True