]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tree.py
Don't accidentally instal the MSVCRT.dll from XP as it wont' work on
[wxWidgets.git] / wxPython / wx / tools / XRCed / tree.py
1 # Name: tree.py
2 # Purpose: XRC editor, XML_tree class
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 02.12.2002
5 # RCS-ID: $Id$
6
7 from xxx import * # xxx imports globals and params
8 import types
9 import traceback
10
11 # Constant to define standart window name
12 STD_NAME = '_XRCED_T_W'
13
14 # Icons
15 import images
16
17 class MemoryFile:
18 def __init__(self, name):
19 self.name = name
20 self.buffer = ''
21 def write(self, data):
22 self.buffer += data.encode()
23 def close(self):
24 wxMemoryFSHandler_AddFile(self.name, self.buffer)
25
26 ################################################################################
27
28 # Redefine writing to include encoding
29 class MyDocument(minidom.Document):
30 def __init__(self):
31 minidom.Document.__init__(self)
32 self.encoding = ''
33 def writexml(self, writer, indent="", addindent="", newl="", encoding=""):
34 if encoding: encdstr = 'encoding="%s"' % encoding
35 else: encdstr = ''
36 writer.write('<?xml version="1.0" %s?>\n' % encdstr)
37 for node in self.childNodes:
38 node.writexml(writer, indent, addindent, newl)
39
40 ################################################################################
41
42 # Ids for menu commands
43 class ID_NEW:
44 PANEL = wxNewId()
45 DIALOG = wxNewId()
46 FRAME = wxNewId()
47 TOOL_BAR = wxNewId()
48 TOOL = wxNewId()
49 MENU_BAR = wxNewId()
50 MENU = wxNewId()
51
52 STATIC_TEXT = wxNewId()
53 TEXT_CTRL = wxNewId()
54
55 BUTTON = wxNewId()
56 BITMAP_BUTTON = wxNewId()
57 RADIO_BUTTON = wxNewId()
58 SPIN_BUTTON = wxNewId()
59
60 STATIC_BOX = wxNewId()
61 CHECK_BOX = wxNewId()
62 RADIO_BOX = wxNewId()
63 COMBO_BOX = wxNewId()
64 LIST_BOX = wxNewId()
65
66 STATIC_LINE = wxNewId()
67 STATIC_BITMAP = wxNewId()
68 CHOICE = wxNewId()
69 SLIDER = wxNewId()
70 GAUGE = wxNewId()
71 SCROLL_BAR = wxNewId()
72 TREE_CTRL = wxNewId()
73 LIST_CTRL = wxNewId()
74 CHECK_LIST = wxNewId()
75 NOTEBOOK = wxNewId()
76 SCROLLED_WINDOW = wxNewId()
77 HTML_WINDOW = wxNewId()
78 CALENDAR_CTRL = wxNewId()
79 GENERIC_DIR_CTRL = wxNewId()
80 SPIN_CTRL = wxNewId()
81 UNKNOWN = wxNewId()
82
83 BOX_SIZER = wxNewId()
84 STATIC_BOX_SIZER = wxNewId()
85 GRID_SIZER = wxNewId()
86 FLEX_GRID_SIZER = wxNewId()
87 GRID_BAG_SIZER = wxNewId()
88 SPACER = wxNewId()
89 TOOL_BAR = wxNewId()
90 TOOL = wxNewId()
91 MENU = wxNewId()
92 MENU_ITEM = wxNewId()
93 SEPARATOR = wxNewId()
94 LAST = wxNewId()
95
96 class PullDownMenu:
97 ID_EXPAND = wxNewId()
98 ID_COLLAPSE = wxNewId()
99 ID_PASTE_SIBLING = wxNewId()
100 ID_SUBCLASS = wxNewId()
101
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)
112
113 # Mapping from IDs to element names
114 self.createMap = {
115 ID_NEW.PANEL: 'wxPanel',
116 ID_NEW.DIALOG: 'wxDialog',
117 ID_NEW.FRAME: 'wxFrame',
118 ID_NEW.TOOL_BAR: 'wxToolBar',
119 ID_NEW.TOOL: 'tool',
120 ID_NEW.MENU_BAR: 'wxMenuBar',
121 ID_NEW.MENU: 'wxMenu',
122 ID_NEW.MENU_ITEM: 'wxMenuItem',
123 ID_NEW.SEPARATOR: 'separator',
124
125 ID_NEW.STATIC_TEXT: 'wxStaticText',
126 ID_NEW.TEXT_CTRL: 'wxTextCtrl',
127
128 ID_NEW.BUTTON: 'wxButton',
129 ID_NEW.BITMAP_BUTTON: 'wxBitmapButton',
130 ID_NEW.RADIO_BUTTON: 'wxRadioButton',
131 ID_NEW.SPIN_BUTTON: 'wxSpinButton',
132
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',
138
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: 'wxCheckListBox',
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',
154
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',
162 }
163 self.topLevel = [
164 (ID_NEW.PANEL, 'Panel', 'Create panel'),
165 (ID_NEW.DIALOG, 'Dialog', 'Create dialog'),
166 (ID_NEW.FRAME, 'Frame', 'Create frame'),
167 None,
168 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
169 (ID_NEW.MENU_BAR, 'MenuBar', 'Create menubar'),
170 (ID_NEW.MENU, 'Menu', 'Create menu')
171 ]
172 self.containers = [
173 (ID_NEW.PANEL, 'Panel', 'Create panel'),
174 (ID_NEW.NOTEBOOK, 'Notebook', 'Create notebook control'),
175 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
176 ]
177 self.sizers = [
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'),
187 ]
188 self.controls = [
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'),
207 ],
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'),
213 ],
214 ['box', 'Boxes',
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'),
220 ],
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'),
225 ],
226 ['sizer', 'Sizers',
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'),
236 ]
237 ]
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'),
242 ]
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'),
257 ],
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 ],
264 ['box', 'Boxes',
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'),
270 ],
271 ]
272
273 ################################################################################
274
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):
280 for l in list:
281 if type(l) == types.TupleType:
282 apply(m.Append, l)
283 elif type(l) == types.ListType:
284 subMenu = wxMenu()
285 SetMenu(subMenu, l[2:])
286 m.AppendMenu(wxNewId(), l[0], subMenu, l[1])
287 else: # separator
288 m.AppendSeparator()
289 # Same, but adds 1000 to all IDs
290 def SetMenu2(m, list):
291 for l in list:
292 if type(l) == types.TupleType:
293 # Shift ID
294 l = (1000 + l[0],) + l[1:]
295 apply(m.Append, l)
296 elif type(l) == types.ListType:
297 subMenu = wxMenu()
298 SetMenu2(subMenu, l[2:])
299 m.AppendMenu(wxNewId(), l[0], subMenu, l[1])
300 else: # separator
301 m.AppendSeparator()
302
303 ################################################################################
304
305 class HighLightBox:
306 def __init__(self, pos, size):
307 if size.width == -1: size.width = 0
308 if size.height == -1: size.height = 0
309 w = g.testWin.panel
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)
327 # Remove it
328 def Remove(self):
329 map(wxWindow.Destroy, self.lines)
330 g.testWin.highLight = None
331
332 ################################################################################
333
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))
338 # Register events
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)
343 else:
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)
348
349 self.selection = None
350 self.needUpdate = False
351 self.pendingHighLight = None
352 self.ctrl = self.shift = False
353 self.dom = None
354 # Create image list
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())
373 self.il = il
374 self.SetImageList(il)
375
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)
381
382 def Unselect(self):
383 self.selection = None
384 wxTreeCtrl.Unselect(self)
385 g.tools.UpdateUI()
386
387 def ExpandAll(self, item):
388 if self.ItemHasChildren(item):
389 self.Expand(item)
390 i, cookie = self.GetFirstChild(item)
391 children = []
392 while i.IsOk():
393 children.append(i)
394 i, cookie = self.GetNextChild(item, cookie)
395 for i in children:
396 self.ExpandAll(i)
397 def CollapseAll(self, item):
398 if self.ItemHasChildren(item):
399 i, cookie = self.GetFirstChild(item)
400 children = []
401 while i.IsOk():
402 children.append(i)
403 i, cookie = self.GetNextChild(item, cookie)
404 for i in children:
405 self.CollapseAll(i)
406 self.Collapse(item)
407
408 # Clear tree
409 def Clear(self):
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')
415 # Create main 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)
422 self.Unselect()
423 self.Expand(self.root)
424
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()
430 self.dom = dom
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[:]
439 for node in nodes:
440 if IsObject(node):
441 self.AddNode(self.root, None, node)
442 else:
443 self.mainNode.removeChild(node)
444 node.unlink()
445 self.Expand(self.root)
446 self.Unselect()
447
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
452 try:
453 xxx = MakeXXXFromDOM(xxxParent, node)
454 except:
455 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent, node)
456 raise
457 treeObj = xxx.treeObject()
458 # Append tree item
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[:]
465 for n in nodes:
466 if IsObject(n):
467 self.AddNode(item, treeObj, n)
468 elif n.nodeType != minidom.Node.ELEMENT_NODE:
469 treeObj.element.removeChild(n)
470 n.unlink()
471
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
477 if nextItem.IsOk():
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)
485 else:
486 parent.element.appendChild(elem)
487 newItem = self.AppendItem(itemParent, xxx.treeName(), image=xxx.treeImage(),
488 data=wxTreeItemData(xxx))
489 # Add children items
490 if xxx.hasChildren:
491 treeObj = xxx.treeObject()
492 for n in treeObj.element.childNodes:
493 if IsObject(n):
494 self.AddNode(newItem, treeObj, n)
495 return newItem
496
497 # Remove leaf of tree, return it's data object
498 def RemoveLeaf(self, leaf):
499 xxx = self.GetPyData(leaf)
500 node = xxx.element
501 parent = node.parentNode
502 parent.removeChild(node)
503 self.Delete(leaf)
504 # Reset selection object
505 self.selection = None
506 return node
507 # Find position relative to the top-level window
508 def FindNodePos(self, item):
509 # Root at (0,0)
510 if item == g.testWin.item: return wxPoint(0, 0)
511 itemParent = self.GetItemParent(item)
512 # Select NB page
513 obj = self.FindNodeObject(item)
514 if self.GetPyData(itemParent).treeObject().__class__ == xxxNotebook:
515 notebook = self.FindNodeObject(itemParent)
516 # Find position
517 for i in range(notebook.GetPageCount()):
518 if notebook.GetPage(i) == obj:
519 if notebook.GetSelection() != i: notebook.SetSelection(i)
520 break
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
530
531 # Find window (or sizer) corresponding to a tree item.
532 def FindNodeObject(self, item):
533 testWin = g.testWin
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()
552 return child
553
554 def OnSelChanged(self, evt):
555 # Apply changes
556 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
557 #oldItem = evt.GetOldItem()
558 status = ''
559 oldItem = self.selection
560 if oldItem:
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:
566 if g.testWin:
567 if g.testWin.highLight:
568 g.testWin.highLight.Remove()
569 self.needUpdate = True
570 status = 'Changes were applied'
571 g.frame.SetStatusText(status)
572 # Generate view
573 self.selection = evt.GetItem()
574 if not self.selection.IsOk():
575 self.selection = None
576 return
577 xxx = self.GetPyData(self.selection)
578 # Update panel
579 g.panel.SetData(xxx)
580 # Update tools
581 g.tools.UpdateUI()
582 # Hightlighting is done in OnIdle
583 self.pendingHighLight = self.selection
584
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
591 return False
592
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]:
599 return
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()
603 return
604 # Get window/sizer object
605 obj, pos = self.FindNodeObject(item), self.FindNodePos(item)
606 size = obj.GetSize()
607 # Highlight
608 # Nagative positions are not working wuite well
609 if g.testWin.highLight:
610 g.testWin.highLight.Replace(pos, size)
611 else:
612 g.testWin.highLight = HighLightBox(pos, size)
613 g.testWin.highLight.item = item
614
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)')
623 return
624 # Show item in bold
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)
630
631 # Double-click on Linux
632 def OnItemActivated(self, evt):
633 if evt.GetItem() != self.root:
634 self.ShowTestWindow(evt.GetItem())
635
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)
641 else:
642 evt.Skip()
643
644 def OnItemExpandedCollapsed(self, evt):
645 # Update tool palette
646 g.tools.UpdateUI()
647 evt.Skip()
648
649 # (re)create test window
650 def CreateTestWin(self, item):
651 testWin = g.testWin
652 # Create a window with this resource
653 xxx = self.GetPyData(item).treeObject()
654
655 # If frame
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')
663 # wxEndBusyCursor()
664 # self.CreateTestWin(child)
665 # return
666
667 wxBeginBusyCursor()
668 wxYield()
669 # Close old window, remember where it was
670 highLight = None
671 if testWin:
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()
682 testWin.panel = None
683 else:
684 testWin.Destroy()
685 testWin = g.testWin = None
686 else:
687 testWin.Destroy()
688 testWin = g.testWin = None
689 else:
690 pos = g.testWinPos
691 # Save in memory FS
692 memFile = MemoryFile('xxx.xrc')
693 # Create partial XML file - faster for big files
694
695 dom = MyDocument()
696 mainNode = dom.createElement('resource')
697 dom.appendChild(mainNode)
698
699 # Remove temporarily from old parent
700 elem = xxx.element
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.
704 if not xxx.name:
705 name = 'noname'
706 else:
707 name = xxx.name
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())
715 # Put back in place
716 mainNode.removeChild(elem)
717 dom.unlink()
718 parent.replaceChild(elem, self.dummyNode)
719 # Remove temporary name or restore changed
720 if not xxx.name:
721 elem.removeAttribute('name')
722 else:
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')
731 try:
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')
739 # wxEndBusyCursor()
740 # self.CreateTestWin(child)
741 # return
742 # This currently works under GTK, but not under MSW
743 testWin = g.testWin = wxPreFrame()
744 res.LoadOnFrame(testWin, g.frame, STD_NAME)
745 # Create status bar
746 testWin.panel = testWin
747 testWin.CreateStatusBar()
748 testWin.SetClientSize(testWin.GetBestSize())
749 testWin.panel = testWin
750 testWin.SetPosition(pos)
751 testWin.Show(True)
752 elif xxx.__class__ == xxxPanel:
753 # Create new frame
754 if not testWin:
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())
759 testWin.Show(True)
760 elif xxx.__class__ == xxxDialog:
761 testWin = g.testWin = res.LoadDialog(None, STD_NAME)
762 testWin.panel = testWin
763 testWin.Layout()
764 testWin.SetPosition(pos)
765 testWin.Show(True)
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)
772 testWin.panel = None
773 # Set status bar to display help
774 testWin.CreateStatusBar()
775 testWin.menuBar = res.LoadMenuBar(STD_NAME)
776 testWin.SetMenuBar(testWin.menuBar)
777 testWin.Show(True)
778 elif xxx.__class__ == xxxToolBar:
779 testWin = g.testWin = wxFrame(g.frame, -1, 'ToolBar: ' + name,
780 pos=pos, name=STD_NAME)
781 testWin.panel = None
782 # Set status bar to display help
783 testWin.CreateStatusBar()
784 testWin.toolBar = res.LoadToolBar(testWin, STD_NAME)
785 testWin.SetToolBar(testWin.toolBar)
786 testWin.Show(True)
787 testWin.item = item
788 EVT_CLOSE(testWin, self.OnCloseTestWin)
789 testWin.highLight = None
790 if highLight and not self.pendingHighLight:
791 self.HighLight(highLight)
792 except:
793 if g.testWin:
794 self.SetItemBold(item, False)
795 g.testWinPos = g.testWin.GetPosition()
796 g.testWin.Destroy()
797 g.testWin = None
798 inf = sys.exc_info()
799 wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1])
800 wxLogError('Error loading resource')
801 wxMemoryFSHandler_RemoveFile('xxx.xrc')
802 wxEndBusyCursor()
803
804 def OnCloseTestWin(self, evt):
805 self.SetItemBold(g.testWin.item, False)
806 g.testWinPos = g.testWin.GetPosition()
807 g.testWin.Destroy()
808 g.testWin = None
809
810 # Return item index in parent
811 def ItemIndex(self, item):
812 n = 0 # index of sibling
813 prev = self.GetPrevSibling(item)
814 while prev.IsOk():
815 prev = self.GetPrevSibling(prev)
816 n += 1
817 return n
818
819 # Full tree index of an item - list of positions
820 def ItemFullIndex(self, item):
821 if not item.IsOk(): return None
822 l = []
823 while item != self.root:
824 l.insert(0, self.ItemIndex(item))
825 item = self.GetItemParent(item)
826 return l
827 # Get item position from full index
828 def ItemAtFullIndex(self, index):
829 if index is None: return wxTreeItemId()
830 item = self.root
831 for i in index:
832 item = self.GetFirstChild(item)[0]
833 for k in range(i): item = self.GetNextSibling(item)
834 return item
835
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):
841 return False
842 return not (self.IsExpanded(item) and self.GetChildrenCount(item, False))
843
844 # Pull-down
845 def OnRightDown(self, evt):
846 pullDownMenu = g.pullDownMenu
847 # select this item
848 pt = evt.GetPosition();
849 item, flags = self.HitTest(pt)
850 if item.Ok() and flags & wxTREE_HITTEST_ONITEM:
851 self.SelectItem(item)
852
853 # Setup menu
854 menu = wxMenu()
855
856 item = self.selection
857 if not item:
858 menu.Append(g.pullDownMenu.ID_EXPAND, 'Expand', 'Expand tree')
859 menu.Append(g.pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse tree')
860 else:
861 # self.ctrl = evt.ControlDown() # save Ctrl state
862 # self.shift = evt.ShiftDown() # and Shift too
863 m = wxMenu() # create menu
864 if self.ctrl:
865 needInsert = True
866 else:
867 needInsert = self.NeedInsert(item)
868 if item == self.root or needInsert and self.GetItemParent(item) == self.root:
869 SetMenu(m, pullDownMenu.topLevel)
870 else:
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)
881 else:
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
888 if not needInsert:
889 if self.shift:
890 menu.AppendMenu(wxNewId(), 'Insert Child', m,
891 'Create child object as the first child')
892 else:
893 menu.AppendMenu(wxNewId(), 'Append Child', m,
894 'Create child object as the last child')
895 else:
896 if self.shift:
897 menu.AppendMenu(wxNewId(), 'Create Sibling', m,
898 'Create sibling before selected object')
899 else:
900 menu.AppendMenu(wxNewId(), 'Create Sibling', m,
901 'Create sibling after selected object')
902 # Build replace menu
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:
912 SetMenu2(m, [])
913 elif xxx.__class__ in [xxxFrame, xxxDialog, xxxPanel]:
914 SetMenu2(m, [
915 (ID_NEW.PANEL, 'Panel', 'Create panel'),
916 (ID_NEW.DIALOG, 'Dialog', 'Create dialog'),
917 (ID_NEW.FRAME, 'Frame', 'Create frame')])
918 elif xxx.isSizer:
919 SetMenu2(m, pullDownMenu.sizers)
920 else:
921 SetMenu2(m, pullDownMenu.controls)
922 id = wxNewId()
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')
934 else:
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())
943 menu.Destroy()
944
945 # Apply changes
946 def Apply(self, xxx, item):
947 g.panel.Apply()
948 # Update tree view
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
960