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
):
22 self
.buffer += data
.encode()
24 wxMemoryFSHandler_AddFile(self
.name
, self
.buffer)
26 ################################################################################
28 # Redefine writing to include encoding
29 class MyDocument(minidom
.Document
):
31 minidom
.Document
.__init
__(self
)
33 def writexml(self
, writer
, indent
="", addindent
="", newl
="", encoding
=""):
34 if encoding
: encdstr
= 'encoding="%s"' % encoding
36 writer
.write('<?xml version="1.0" %s?>\n' % encdstr
)
37 for node
in self
.childNodes
:
38 node
.writexml(writer
, indent
, addindent
, newl
)
40 ################################################################################
42 # Ids for menu commands
52 STATIC_TEXT
= wxNewId()
56 BITMAP_BUTTON
= wxNewId()
57 RADIO_BUTTON
= wxNewId()
58 SPIN_BUTTON
= wxNewId()
60 STATIC_BOX
= wxNewId()
66 STATIC_LINE
= wxNewId()
67 STATIC_BITMAP
= wxNewId()
71 SCROLL_BAR
= wxNewId()
74 CHECK_LIST
= wxNewId()
76 SCROLLED_WINDOW
= wxNewId()
77 HTML_WINDOW
= wxNewId()
78 CALENDAR_CTRL
= wxNewId()
79 GENERIC_DIR_CTRL
= wxNewId()
84 STATIC_BOX_SIZER
= wxNewId()
85 GRID_SIZER
= wxNewId()
86 FLEX_GRID_SIZER
= wxNewId()
87 GRID_BAG_SIZER
= wxNewId()
98 ID_COLLAPSE
= wxNewId()
99 ID_PASTE_SIBLING
= wxNewId()
100 ID_SUBCLASS
= wxNewId()
102 def __init__(self
, parent
):
103 self
.ID_DELETE
= parent
.ID_DELETE
104 EVT_MENU_RANGE(parent
, ID_NEW
.PANEL
, ID_NEW
.LAST
, parent
.OnCreate
)
105 EVT_MENU_RANGE(parent
, 1000 + ID_NEW
.PANEL
, 1000 + ID_NEW
.LAST
, parent
.OnReplace
)
106 EVT_MENU(parent
, self
.ID_COLLAPSE
, parent
.OnCollapse
)
107 EVT_MENU(parent
, self
.ID_EXPAND
, parent
.OnExpand
)
108 EVT_MENU(parent
, self
.ID_PASTE_SIBLING
, parent
.OnPaste
)
109 EVT_MENU(parent
, self
.ID_SUBCLASS
, parent
.OnSubclass
)
110 # We connect to tree, but process in frame
111 EVT_MENU_HIGHLIGHT_ALL(g
.tree
, parent
.OnPullDownHighlight
)
113 # Mapping from IDs to element names
115 ID_NEW
.PANEL
: 'wxPanel',
116 ID_NEW
.DIALOG
: 'wxDialog',
117 ID_NEW
.FRAME
: 'wxFrame',
118 ID_NEW
.TOOL_BAR
: 'wxToolBar',
120 ID_NEW
.MENU_BAR
: 'wxMenuBar',
121 ID_NEW
.MENU
: 'wxMenu',
122 ID_NEW
.MENU_ITEM
: 'wxMenuItem',
123 ID_NEW
.SEPARATOR
: 'separator',
125 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
126 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
128 ID_NEW
.BUTTON
: 'wxButton',
129 ID_NEW
.BITMAP_BUTTON
: 'wxBitmapButton',
130 ID_NEW
.RADIO_BUTTON
: 'wxRadioButton',
131 ID_NEW
.SPIN_BUTTON
: 'wxSpinButton',
133 ID_NEW
.STATIC_BOX
: 'wxStaticBox',
134 ID_NEW
.CHECK_BOX
: 'wxCheckBox',
135 ID_NEW
.RADIO_BOX
: 'wxRadioBox',
136 ID_NEW
.COMBO_BOX
: 'wxComboBox',
137 ID_NEW
.LIST_BOX
: 'wxListBox',
139 ID_NEW
.STATIC_LINE
: 'wxStaticLine',
140 ID_NEW
.STATIC_BITMAP
: 'wxStaticBitmap',
141 ID_NEW
.CHOICE
: 'wxChoice',
142 ID_NEW
.SLIDER
: 'wxSlider',
143 ID_NEW
.GAUGE
: 'wxGauge',
144 ID_NEW
.SCROLL_BAR
: 'wxScrollBar',
145 ID_NEW
.TREE_CTRL
: 'wxTreeCtrl',
146 ID_NEW
.LIST_CTRL
: 'wxListCtrl',
147 ID_NEW
.CHECK_LIST
: 'wxCheckList',
148 ID_NEW
.NOTEBOOK
: 'wxNotebook',
149 ID_NEW
.SCROLLED_WINDOW
: 'wxScrolledWindow',
150 ID_NEW
.HTML_WINDOW
: 'wxHtmlWindow',
151 ID_NEW
.CALENDAR_CTRL
: 'wxCalendarCtrl',
152 ID_NEW
.GENERIC_DIR_CTRL
: 'wxGenericDirCtrl',
153 ID_NEW
.SPIN_CTRL
: 'wxSpinCtrl',
155 ID_NEW
.BOX_SIZER
: 'wxBoxSizer',
156 ID_NEW
.STATIC_BOX_SIZER
: 'wxStaticBoxSizer',
157 ID_NEW
.GRID_SIZER
: 'wxGridSizer',
158 ID_NEW
.FLEX_GRID_SIZER
: 'wxFlexGridSizer',
159 ID_NEW
.GRID_BAG_SIZER
: 'wxGridBagSizer',
160 ID_NEW
.SPACER
: 'spacer',
161 ID_NEW
.UNKNOWN
: 'unknown',
164 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
165 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
166 (ID_NEW
.FRAME
, 'Frame', 'Create frame'),
168 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
169 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
170 (ID_NEW
.MENU
, 'Menu', 'Create menu')
173 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
174 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
175 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
178 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
179 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
180 'Create static box sizer'),
181 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
182 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
183 'Create flexgrid sizer'),
184 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
185 'Create gridbag sizer'),
186 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
189 ['control', 'Various controls',
190 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
191 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
192 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
193 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
194 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
195 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
196 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
197 (ID_NEW
.SPIN_CTRL
, 'SpinCtrl', 'Create spin'),
198 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
199 (ID_NEW
.TREE_CTRL
, 'TreeCtrl', 'Create tree'),
200 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list'),
201 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
202 (ID_NEW
.SCROLLED_WINDOW
, 'ScrolledWindow', 'Create scrolled window'),
203 (ID_NEW
.HTML_WINDOW
, 'HtmlWindow', 'Create HTML window'),
204 (ID_NEW
.CALENDAR_CTRL
, 'CalendarCtrl', 'Create calendar control'),
205 (ID_NEW
.GENERIC_DIR_CTRL
, 'GenericDirCtrl', 'Create generic dir control'),
206 (ID_NEW
.UNKNOWN
, 'Unknown', 'Create custom control placeholder'),
208 ['button', 'Buttons',
209 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
210 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
211 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
212 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
215 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
216 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
217 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
218 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
219 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
221 ['container', 'Containers',
222 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
223 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
224 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
227 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
228 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
229 'Create static box sizer'),
230 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
231 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
232 'Create flexgrid sizer'),
233 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
234 'Create gridbag sizer'),
235 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
238 self
.menuControls
= [
239 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
240 (ID_NEW
.MENU_ITEM
, 'MenuItem', 'Create menu item'),
241 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
243 self
.toolBarControls
= [
244 (ID_NEW
.TOOL
, 'Tool', 'Create tool'),
245 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
246 ['control', 'Various controls',
247 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
248 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
249 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
250 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
251 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
252 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
253 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
254 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
255 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list control'),
256 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
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'),
265 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
266 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
267 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
268 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
269 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
273 ################################################################################
275 # Set menu to list items.
276 # Each menu command is a tuple (id, label, help)
277 # submenus are lists [id, label, help, submenu]
278 # and separators are any other type
279 def SetMenu(m
, list):
281 if type(l
) == types
.TupleType
:
283 elif type(l
) == types
.ListType
:
285 SetMenu(subMenu
, l
[2:])
286 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
289 # Same, but adds 1000 to all IDs
290 def SetMenu2(m
, list):
292 if type(l
) == types
.TupleType
:
294 l
= (1000 + l
[0],) + l
[1:]
296 elif type(l
) == types
.ListType
:
298 SetMenu2(subMenu
, l
[2:])
299 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
303 ################################################################################
306 def __init__(self
, pos
, size
):
307 if size
.width
== -1: size
.width
= 0
308 if size
.height
== -1: size
.height
= 0
310 l1
= wxWindow(w
, -1, pos
, wxSize(size
.width
, 2))
311 l1
.SetBackgroundColour(wxRED
)
312 l2
= wxWindow(w
, -1, pos
, wxSize(2, size
.height
))
313 l2
.SetBackgroundColour(wxRED
)
314 l3
= wxWindow(w
, -1, wxPoint(pos
.x
+ size
.width
- 2, pos
.y
), wxSize(2, size
.height
))
315 l3
.SetBackgroundColour(wxRED
)
316 l4
= wxWindow(w
, -1, wxPoint(pos
.x
, pos
.y
+ size
.height
- 2), wxSize(size
.width
, 2))
317 l4
.SetBackgroundColour(wxRED
)
318 self
.lines
= [l1
, l2
, l3
, l4
]
319 # Move highlight to a new position
320 def Replace(self
, pos
, size
):
321 if size
.width
== -1: size
.width
= 0
322 if size
.height
== -1: size
.height
= 0
323 self
.lines
[0].SetDimensions(pos
.x
, pos
.y
, size
.width
, 2)
324 self
.lines
[1].SetDimensions(pos
.x
, pos
.y
, 2, size
.height
)
325 self
.lines
[2].SetDimensions(pos
.x
+ size
.width
- 2, pos
.y
, 2, size
.height
)
326 self
.lines
[3].SetDimensions(pos
.x
, pos
.y
+ size
.height
- 2, size
.width
, 2)
329 map(wxWindow
.Destroy
, self
.lines
)
330 g
.testWin
.highLight
= None
332 ################################################################################
334 class XML_Tree(wxTreeCtrl
):
335 def __init__(self
, parent
, id):
336 wxTreeCtrl
.__init
__(self
, parent
, id, style
= wxTR_HAS_BUTTONS
)
337 self
.SetBackgroundColour(wxColour(224, 248, 224))
339 EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
340 # One works on Linux, another on Windows
341 if wxPlatform
== '__WXGTK__':
342 EVT_TREE_ITEM_ACTIVATED(self
, self
.GetId(), self
.OnItemActivated
)
344 EVT_LEFT_DCLICK(self
, self
.OnDClick
)
345 EVT_RIGHT_DOWN(self
, self
.OnRightDown
)
346 EVT_TREE_ITEM_EXPANDED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
347 EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
349 self
.selection
= None
350 self
.needUpdate
= False
351 self
.pendingHighLight
= None
352 self
.ctrl
= self
.shift
= False
355 il
= wxImageList(16, 16, True)
356 self
.rootImage
= il
.Add(images
.getTreeRootImage().Scale(16,16).ConvertToBitmap())
357 xxxObject
.image
= il
.Add(images
.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
358 xxxPanel
.image
= il
.Add(images
.getTreePanelImage().Scale(16,16).ConvertToBitmap())
359 xxxDialog
.image
= il
.Add(images
.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
360 xxxFrame
.image
= il
.Add(images
.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
361 xxxMenuBar
.image
= il
.Add(images
.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
362 xxxMenu
.image
= il
.Add(images
.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
363 xxxMenuItem
.image
= il
.Add(images
.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
364 xxxToolBar
.image
= il
.Add(images
.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
365 xxxTool
.image
= il
.Add(images
.getTreeToolImage().Scale(16,16).ConvertToBitmap())
366 xxxSeparator
.image
= il
.Add(images
.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
367 xxxSizer
.imageH
= il
.Add(images
.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
368 xxxSizer
.imageV
= il
.Add(images
.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
369 xxxStaticBoxSizer
.imageH
= il
.Add(images
.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
370 xxxStaticBoxSizer
.imageV
= il
.Add(images
.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
371 xxxGridSizer
.image
= il
.Add(images
.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
372 xxxFlexGridSizer
.image
= il
.Add(images
.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
374 self
.SetImageList(il
)
376 def RegisterKeyEvents(self
):
377 EVT_KEY_DOWN(self
, g
.tools
.OnKeyDown
)
378 EVT_KEY_UP(self
, g
.tools
.OnKeyUp
)
379 EVT_ENTER_WINDOW(self
, g
.tools
.OnMouse
)
380 EVT_LEAVE_WINDOW(self
, g
.tools
.OnMouse
)
383 self
.selection
= None
384 wxTreeCtrl
.Unselect(self
)
387 def ExpandAll(self
, item
):
388 if self
.ItemHasChildren(item
):
390 i
, cookie
= self
.GetFirstChild(item
)
394 i
, cookie
= self
.GetNextChild(item
, cookie
)
397 def CollapseAll(self
, item
):
398 if self
.ItemHasChildren(item
):
399 i
, cookie
= self
.GetFirstChild(item
)
403 i
, cookie
= self
.GetNextChild(item
, cookie
)
410 self
.DeleteAllItems()
411 # Add minimal structure
412 if self
.dom
: self
.dom
.unlink()
413 self
.dom
= MyDocument()
414 self
.dummyNode
= self
.dom
.createComment('dummy node')
416 self
.mainNode
= self
.dom
.createElement('resource')
417 self
.dom
.appendChild(self
.mainNode
)
418 self
.rootObj
= xxxMainNode(self
.dom
)
419 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
420 data
=wxTreeItemData(self
.rootObj
))
421 self
.SetItemHasChildren(self
.root
)
423 self
.Expand(self
.root
)
425 # Clear old data and set new
426 def SetData(self
, dom
):
427 self
.DeleteAllItems()
428 # Add minimal structure
429 if self
.dom
: self
.dom
.unlink()
431 self
.dummyNode
= self
.dom
.createComment('dummy node')
432 # Find 'resource' child, add it's children
433 self
.mainNode
= dom
.documentElement
434 self
.rootObj
= xxxMainNode(self
.dom
)
435 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
436 data
=wxTreeItemData(self
.rootObj
))
437 self
.SetItemHasChildren(self
.root
)
438 nodes
= self
.mainNode
.childNodes
[:]
441 self
.AddNode(self
.root
, None, node
)
443 self
.mainNode
.removeChild(node
)
445 self
.Expand(self
.root
)
448 # Add tree item for given parent item if node is DOM element node with
449 # 'object' tag. xxxParent is parent xxx object
450 def AddNode(self
, itemParent
, xxxParent
, node
):
451 # Set item data to current node
453 xxx
= MakeXXXFromDOM(xxxParent
, node
)
455 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
457 treeObj
= xxx
.treeObject()
459 item
= self
.AppendItem(itemParent
, treeObj
.treeName(),
460 image
=treeObj
.treeImage(),
461 data
=wxTreeItemData(xxx
))
462 # Try to find children objects
463 if treeObj
.hasChildren
:
464 nodes
= treeObj
.element
.childNodes
[:]
467 self
.AddNode(item
, treeObj
, n
)
468 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
469 treeObj
.element
.removeChild(n
)
472 # Insert new item at specific position
473 def InsertNode(self
, itemParent
, parent
, elem
, nextItem
):
474 # Insert in XML tree and wxWin
475 xxx
= MakeXXXFromDOM(parent
, elem
)
476 # If nextItem is None, we append to parent, otherwise insert before it
478 node
= self
.GetPyData(nextItem
).element
479 parent
.element
.insertBefore(elem
, node
)
480 # Inserting before is difficult, se we insert after or first child
481 index
= self
.ItemIndex(nextItem
)
482 newItem
= self
.InsertItemBefore(itemParent
, index
,
483 xxx
.treeName(), image
=xxx
.treeImage())
484 self
.SetPyData(newItem
, xxx
)
486 parent
.element
.appendChild(elem
)
487 newItem
= self
.AppendItem(itemParent
, xxx
.treeName(), image
=xxx
.treeImage(),
488 data
=wxTreeItemData(xxx
))
491 treeObj
= xxx
.treeObject()
492 for n
in treeObj
.element
.childNodes
:
494 self
.AddNode(newItem
, treeObj
, n
)
497 # Remove leaf of tree, return it's data object
498 def RemoveLeaf(self
, leaf
):
499 xxx
= self
.GetPyData(leaf
)
501 parent
= node
.parentNode
502 parent
.removeChild(node
)
504 # Reset selection object
505 self
.selection
= None
507 # Find position relative to the top-level window
508 def FindNodePos(self
, item
):
510 if item
== g
.testWin
.item
: return wxPoint(0, 0)
511 itemParent
= self
.GetItemParent(item
)
513 obj
= self
.FindNodeObject(item
)
514 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
515 notebook
= self
.FindNodeObject(itemParent
)
517 for i
in range(notebook
.GetPageCount()):
518 if notebook
.GetPage(i
) == obj
:
519 if notebook
.GetSelection() != i
: notebook
.SetSelection(i
)
521 # Find first ancestor which is a wxWindow (not a sizer)
522 winParent
= itemParent
523 while self
.GetPyData(winParent
).isSizer
:
524 winParent
= self
.GetItemParent(winParent
)
525 parentPos
= self
.FindNodePos(winParent
)
526 # Position (-1,-1) is really (0,0)
527 pos
= obj
.GetPosition()
528 if pos
== (-1,-1): pos
= (0,0)
529 return parentPos
+ pos
531 # Find window (or sizer) corresponding to a tree item.
532 def FindNodeObject(self
, item
):
534 # If top-level, return testWin (or panel its panel)
535 if item
== testWin
.item
: return testWin
.panel
536 itemParent
= self
.GetItemParent(item
)
537 xxx
= self
.GetPyData(item
).treeObject()
538 parentWin
= self
.FindNodeObject(itemParent
)
539 # Top-level sizer? return window's sizer
540 if xxx
.isSizer
and isinstance(parentWin
, wxWindow
):
541 return parentWin
.GetSizer()
542 # Otherwise get parent's object and it's child
543 child
= parentWin
.GetChildren()[self
.ItemIndex(item
)]
544 # Return window or sizer for sizer items
545 if child
.GetClassName() == 'wxSizerItem':
546 if child
.IsWindow(): child
= child
.GetWindow()
547 elif child
.IsSizer():
548 child
= child
.GetSizer()
549 # Test for notebook sizers
550 if isinstance(child
, wxNotebookSizer
):
551 child
= child
.GetNotebook()
554 def OnSelChanged(self
, evt
):
556 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
557 #oldItem = evt.GetOldItem()
559 oldItem
= self
.selection
561 xxx
= self
.GetPyData(oldItem
)
562 # If some data was modified, apply changes
563 if g
.panel
.IsModified():
564 self
.Apply(xxx
, oldItem
)
565 #if conf.autoRefresh:
567 if g
.testWin
.highLight
:
568 g
.testWin
.highLight
.Remove()
569 self
.needUpdate
= True
570 status
= 'Changes were applied'
571 g
.frame
.SetStatusText(status
)
573 self
.selection
= evt
.GetItem()
574 if not self
.selection
.IsOk():
575 self
.selection
= None
577 xxx
= self
.GetPyData(self
.selection
)
582 # Hightlighting is done in OnIdle
583 self
.pendingHighLight
= self
.selection
585 # Check if item is in testWin subtree
586 def IsHighlatable(self
, item
):
587 if item
== g
.testWin
.item
: return False
588 while item
!= self
.root
:
589 item
= self
.GetItemParent(item
)
590 if item
== g
.testWin
.item
: return True
593 # Highlight selected item
594 def HighLight(self
, item
):
595 self
.pendingHighLight
= None
596 # Can highlight only with some top-level windows
597 if not g
.testWin
or self
.GetPyData(g
.testWin
.item
).treeObject().__class
__ \
598 not in [xxxDialog
, xxxPanel
, xxxFrame
]:
600 # If a control from another window is selected, remove highlight
601 if not self
.IsHighlatable(item
):
602 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
604 # Get window/sizer object
605 obj
, pos
= self
.FindNodeObject(item
), self
.FindNodePos(item
)
608 # Nagative positions are not working wuite well
609 if g
.testWin
.highLight
:
610 g
.testWin
.highLight
.Replace(pos
, size
)
612 g
.testWin
.highLight
= HighLightBox(pos
, size
)
613 g
.testWin
.highLight
.item
= item
615 def ShowTestWindow(self
, item
):
616 xxx
= self
.GetPyData(item
)
617 if g
.panel
.IsModified():
618 self
.Apply(xxx
, item
) # apply changes
619 treeObj
= xxx
.treeObject()
620 if treeObj
.className
not in ['wxFrame', 'wxPanel', 'wxDialog',
621 'wxMenuBar', 'wxToolBar']:
622 wxLogMessage('No view for this element (yet)')
625 if g
.testWin
: # Reset old
626 self
.SetItemBold(g
.testWin
.item
, False)
627 self
.CreateTestWin(item
)
628 # Maybe an error occured, so we need to test
629 if g
.testWin
: self
.SetItemBold(g
.testWin
.item
)
631 # Double-click on Linux
632 def OnItemActivated(self
, evt
):
633 if evt
.GetItem() != self
.root
:
634 self
.ShowTestWindow(evt
.GetItem())
636 # Double-click on Windows
637 def OnDClick(self
, evt
):
638 item
, flags
= self
.HitTest(evt
.GetPosition())
639 if flags
in [wxTREE_HITTEST_ONITEMBUTTON
, wxTREE_HITTEST_ONITEMLABEL
]:
640 if item
!= self
.root
: self
.ShowTestWindow(item
)
644 def OnItemExpandedCollapsed(self
, evt
):
645 # Update tool palette
649 # (re)create test window
650 def CreateTestWin(self
, item
):
652 # Create a window with this resource
653 xxx
= self
.GetPyData(item
).treeObject()
656 # if xxx.__class__ == xxxFrame:
657 # Frame can't have many children,
658 # but it's first child possibly can...
659 # child = self.GetFirstChild(item)[0]
660 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
661 # # Clean-up before recursive call or error
662 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
664 # self.CreateTestWin(child)
669 # Close old window, remember where it was
672 pos
= testWin
.GetPosition()
673 if item
== testWin
.item
:
674 # Remember highlight if same top-level window
675 if testWin
.highLight
:
676 highLight
= testWin
.highLight
.item
677 if xxx
.className
== 'wxPanel':
678 if testWin
.highLight
:
679 testWin
.pendingHighLight
= highLight
680 testWin
.highLight
.Remove()
681 testWin
.panel
.Destroy()
685 testWin
= g
.testWin
= None
688 testWin
= g
.testWin
= None
692 memFile
= MemoryFile('xxx.xrc')
693 # Create partial XML file - faster for big files
696 mainNode
= dom
.createElement('resource')
697 dom
.appendChild(mainNode
)
699 # Remove temporarily from old parent
701 # Change window id to _XRCED_T_W. This gives some name for
702 # unnamed windows, and for named gives the possibility to
703 # write sawfish scripts.
708 elem
.setAttribute('name', STD_NAME
)
709 parent
= elem
.parentNode
710 next
= elem
.nextSibling
711 parent
.replaceChild(self
.dummyNode
, elem
)
712 # Append to new DOM, write it
713 mainNode
.appendChild(elem
)
714 dom
.writexml(memFile
, encoding
=self
.rootObj
.params
['encoding'].value())
716 mainNode
.removeChild(elem
)
718 parent
.replaceChild(elem
, self
.dummyNode
)
719 # Remove temporary name or restore changed
721 elem
.removeAttribute('name')
723 elem
.setAttribute('name', xxx
.name
)
724 memFile
.close() # write to wxMemoryFS
725 xmlFlags
= wxXRC_NO_SUBCLASSING
726 # Use translations if encoding is not specified
727 if g
.currentEncoding
== 'ascii':
728 xmlFlags
!= wxXRC_USE_LOCALE
729 res
= wxXmlResource('', xmlFlags
)
730 res
.Load('memory:xxx.xrc')
732 if xxx
.__class
__ == xxxFrame
:
733 # Frame can't have many children,
734 # but it's first child possibly can...
735 # child = self.GetFirstChild(item)[0]
736 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
737 # # Clean-up before recursive call or error
738 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
740 # self.CreateTestWin(child)
742 # This currently works under GTK, but not under MSW
743 testWin
= g
.testWin
= wxPreFrame()
744 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
746 testWin
.panel
= testWin
747 testWin
.CreateStatusBar()
748 testWin
.SetClientSize(testWin
.GetBestSize())
749 testWin
.panel
= testWin
750 testWin
.SetPosition(pos
)
752 elif xxx
.__class
__ == xxxPanel
:
755 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Panel: ' + name
,
756 pos
=pos
, name
=STD_NAME
)
757 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
758 testWin
.SetClientSize(testWin
.GetBestSize())
760 elif xxx
.__class
__ == xxxDialog
:
761 testWin
= g
.testWin
= res
.LoadDialog(None, STD_NAME
)
762 testWin
.panel
= testWin
764 testWin
.SetPosition(pos
)
766 # Dialog's default code does not produce EVT_CLOSE
767 EVT_BUTTON(testWin
, wxID_OK
, self
.OnCloseTestWin
)
768 EVT_BUTTON(testWin
, wxID_CANCEL
, self
.OnCloseTestWin
)
769 elif xxx
.__class
__ == xxxMenuBar
:
770 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'MenuBar: ' + name
,
771 pos
=pos
, name
=STD_NAME
)
773 # Set status bar to display help
774 testWin
.CreateStatusBar()
775 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
776 testWin
.SetMenuBar(testWin
.menuBar
)
778 elif xxx
.__class
__ == xxxToolBar
:
779 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'ToolBar: ' + name
,
780 pos
=pos
, name
=STD_NAME
)
782 # Set status bar to display help
783 testWin
.CreateStatusBar()
784 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
785 testWin
.SetToolBar(testWin
.toolBar
)
788 EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
789 testWin
.highLight
= None
790 if highLight
and not self
.pendingHighLight
:
791 self
.HighLight(highLight
)
794 self
.SetItemBold(item
, False)
795 g
.testWinPos
= g
.testWin
.GetPosition()
799 wxLogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
800 wxLogError('Error loading resource')
801 wxMemoryFSHandler_RemoveFile('xxx.xrc')
804 def OnCloseTestWin(self
, evt
):
805 self
.SetItemBold(g
.testWin
.item
, False)
806 g
.testWinPos
= g
.testWin
.GetPosition()
810 # Return item index in parent
811 def ItemIndex(self
, item
):
812 n
= 0 # index of sibling
813 prev
= self
.GetPrevSibling(item
)
815 prev
= self
.GetPrevSibling(prev
)
819 # Full tree index of an item - list of positions
820 def ItemFullIndex(self
, item
):
821 if not item
.IsOk(): return None
823 while item
!= self
.root
:
824 l
.insert(0, self
.ItemIndex(item
))
825 item
= self
.GetItemParent(item
)
827 # Get item position from full index
828 def ItemAtFullIndex(self
, index
):
829 if index
is None: return wxTreeItemId()
832 item
= self
.GetFirstChild(item
)[0]
833 for k
in range(i
): item
= self
.GetNextSibling(item
)
836 # True if next item should be inserted after current (vs. appended to it)
837 def NeedInsert(self
, item
):
838 xxx
= self
.GetPyData(item
)
839 if item
== self
.root
: return False # root item
840 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
842 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
845 def OnRightDown(self
, evt
):
846 pullDownMenu
= g
.pullDownMenu
848 pt
= evt
.GetPosition();
849 item
, flags
= self
.HitTest(pt
)
850 if item
.Ok() and flags
& wxTREE_HITTEST_ONITEM
:
851 self
.SelectItem(item
)
856 item
= self
.selection
858 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
859 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
861 # self.ctrl = evt.ControlDown() # save Ctrl state
862 # self.shift = evt.ShiftDown() # and Shift too
863 m
= wxMenu() # create menu
867 needInsert
= self
.NeedInsert(item
)
868 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
869 SetMenu(m
, pullDownMenu
.topLevel
)
871 xxx
= self
.GetPyData(item
).treeObject()
872 # Check parent for possible child nodes if inserting sibling
873 if needInsert
: xxx
= xxx
.parent
874 if xxx
.__class
__ == xxxMenuBar
:
875 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
876 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
877 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
878 SetMenu(m
, pullDownMenu
.toolBarControls
)
879 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
880 SetMenu(m
, pullDownMenu
.menuControls
)
882 SetMenu(m
, pullDownMenu
.controls
)
883 if xxx
.__class
__ == xxxNotebook
:
884 m
.Enable(m
.FindItem('sizer'), False)
885 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
886 m
.Enable(ID_NEW
.SPACER
, False)
887 # Select correct label for create menu
890 menu
.AppendMenu(wxNewId(), 'Insert Child', m
,
891 'Create child object as the first child')
893 menu
.AppendMenu(wxNewId(), 'Append Child', m
,
894 'Create child object as the last child')
897 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
898 'Create sibling before selected object')
900 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
901 'Create sibling after selected object')
903 if item
!= self
.root
:
904 xxx
= self
.GetPyData(item
).treeObject()
905 m
= wxMenu() # create replace menu
906 if xxx
.__class
__ == xxxMenuBar
:
907 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
908 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
909 SetMenu2(m
, pullDownMenu
.menuControls
)
910 elif xxx
.__class
__ == xxxToolBar
and \
911 self
.GetItemParent(item
) == self
.root
:
913 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
915 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
916 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
917 (ID_NEW
.FRAME
, 'Frame', 'Create frame')])
919 SetMenu2(m
, pullDownMenu
.sizers
)
921 SetMenu2(m
, pullDownMenu
.controls
)
923 menu
.AppendMenu(id, 'Replace With', m
)
924 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
925 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
926 'Set subclass property')
927 menu
.AppendSeparator()
928 # Not using standart IDs because we don't want to show shortcuts
929 menu
.Append(wxID_CUT
, 'Cut', 'Cut to the clipboard')
930 menu
.Append(wxID_COPY
, 'Copy', 'Copy to the clipboard')
931 if self
.ctrl
and item
!= self
.root
:
932 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
933 'Paste from the clipboard as a sibling')
935 menu
.Append(wxID_PASTE
, 'Paste', 'Paste from the clipboard')
936 menu
.Append(pullDownMenu
.ID_DELETE
,
937 'Delete', 'Delete object')
938 if self
.ItemHasChildren(item
):
939 menu
.AppendSeparator()
940 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
941 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
942 self
.PopupMenu(menu
, evt
.GetPosition())
946 def Apply(self
, xxx
, item
):
949 xxx
= xxx
.treeObject()
950 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
951 self
.SetItemText(item
, xxx
.treeName())
952 # Item width may have changed
953 # !!! Tric to update tree width (wxGTK, ??)
954 self
.SetIndent(self
.GetIndent())
955 # Change tree icon for sizers
956 if isinstance(xxx
, xxxBoxSizer
):
957 self
.SetItemImage(item
, xxx
.treeImage())
958 # Set global modified state
959 g
.frame
.modified
= True