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()
113 ID_EXPAND
= wxNewId()
114 ID_COLLAPSE
= wxNewId()
115 ID_PASTE_SIBLING
= wxNewId()
116 ID_TOOL_PASTE
= wxNewId()
117 ID_SUBCLASS
= wxNewId()
119 def __init__(self
, parent
):
120 self
.ID_DELETE
= parent
.ID_DELETE
121 EVT_MENU_RANGE(parent
, ID_NEW
.PANEL
, ID_NEW
.LAST
, parent
.OnCreate
)
122 EVT_MENU_RANGE(parent
, 1000 + ID_NEW
.PANEL
, 1000 + ID_NEW
.LAST
, parent
.OnReplace
)
123 EVT_MENU(parent
, self
.ID_COLLAPSE
, parent
.OnCollapse
)
124 EVT_MENU(parent
, self
.ID_EXPAND
, parent
.OnExpand
)
125 EVT_MENU(parent
, self
.ID_PASTE_SIBLING
, parent
.OnPaste
)
126 EVT_MENU(parent
, self
.ID_SUBCLASS
, parent
.OnSubclass
)
127 # We connect to tree, but process in frame
128 EVT_MENU_HIGHLIGHT_ALL(g
.tree
, parent
.OnPullDownHighlight
)
130 # Mapping from IDs to element names
132 ID_NEW
.PANEL
: 'wxPanel',
133 ID_NEW
.DIALOG
: 'wxDialog',
134 ID_NEW
.FRAME
: 'wxFrame',
135 ID_NEW
.WIZARD
: 'wxWizard',
136 ID_NEW
.WIZARD_PAGE
: 'wxWizardPage',
137 ID_NEW
.WIZARD_PAGE_SIMPLE
: 'wxWizardPageSimple',
138 ID_NEW
.TOOL_BAR
: 'wxToolBar',
140 ID_NEW
.MENU_BAR
: 'wxMenuBar',
141 ID_NEW
.MENU
: 'wxMenu',
142 ID_NEW
.MENU_ITEM
: 'wxMenuItem',
143 ID_NEW
.SEPARATOR
: 'separator',
145 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
146 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
148 ID_NEW
.BUTTON
: 'wxButton',
149 ID_NEW
.BITMAP_BUTTON
: 'wxBitmapButton',
150 ID_NEW
.RADIO_BUTTON
: 'wxRadioButton',
151 ID_NEW
.SPIN_BUTTON
: 'wxSpinButton',
152 ID_NEW
.TOGGLE_BUTTON
: 'wxToggleButton',
154 ID_NEW
.STATIC_BOX
: 'wxStaticBox',
155 ID_NEW
.CHECK_BOX
: 'wxCheckBox',
156 ID_NEW
.RADIO_BOX
: 'wxRadioBox',
157 ID_NEW
.COMBO_BOX
: 'wxComboBox',
158 ID_NEW
.LIST_BOX
: 'wxListBox',
160 ID_NEW
.STATIC_LINE
: 'wxStaticLine',
161 ID_NEW
.STATIC_BITMAP
: 'wxStaticBitmap',
162 ID_NEW
.CHOICE
: 'wxChoice',
163 ID_NEW
.SLIDER
: 'wxSlider',
164 ID_NEW
.GAUGE
: 'wxGauge',
165 ID_NEW
.SCROLL_BAR
: 'wxScrollBar',
166 ID_NEW
.TREE_CTRL
: 'wxTreeCtrl',
167 ID_NEW
.LIST_CTRL
: 'wxListCtrl',
168 ID_NEW
.CHECK_LIST
: 'wxCheckListBox',
169 ID_NEW
.NOTEBOOK
: 'wxNotebook',
170 ID_NEW
.SPLITTER_WINDOW
: 'wxSplitterWindow',
171 ID_NEW
.SCROLLED_WINDOW
: 'wxScrolledWindow',
172 ID_NEW
.HTML_WINDOW
: 'wxHtmlWindow',
173 ID_NEW
.CALENDAR_CTRL
: 'wxCalendarCtrl',
174 ID_NEW
.GENERIC_DIR_CTRL
: 'wxGenericDirCtrl',
175 ID_NEW
.SPIN_CTRL
: 'wxSpinCtrl',
177 ID_NEW
.BOX_SIZER
: 'wxBoxSizer',
178 ID_NEW
.STATIC_BOX_SIZER
: 'wxStaticBoxSizer',
179 ID_NEW
.GRID_SIZER
: 'wxGridSizer',
180 ID_NEW
.FLEX_GRID_SIZER
: 'wxFlexGridSizer',
181 ID_NEW
.GRID_BAG_SIZER
: 'wxGridBagSizer',
182 ID_NEW
.STD_DIALOG_BUTTON_SIZER
: 'wxStdDialogButtonSizer',
183 ID_NEW
.SPACER
: 'spacer',
184 ID_NEW
.UNKNOWN
: 'unknown',
187 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
188 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
189 (ID_NEW
.FRAME
, 'Frame', 'Create frame'),
190 (ID_NEW
.WIZARD
, 'Wizard', 'Create wizard'),
192 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
193 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
194 (ID_NEW
.MENU
, 'Menu', 'Create menu')
197 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
198 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
199 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
200 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
201 # (ID_NEW.WIZARD_PAGE, 'WizardPage', 'Create wizard page'),
202 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
205 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
206 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
207 'Create static box sizer'),
208 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
209 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
210 'Create flexgrid sizer'),
211 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
212 'Create gridbag sizer'),
213 # (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
214 # 'Create standard button sizer'),
215 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
218 ['control', 'Various controls',
219 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
220 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
221 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
222 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
223 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
224 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
225 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
226 (ID_NEW
.SPIN_CTRL
, 'SpinCtrl', 'Create spin'),
227 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
228 (ID_NEW
.TREE_CTRL
, 'TreeCtrl', 'Create tree'),
229 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list'),
230 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
231 (ID_NEW
.SCROLLED_WINDOW
, 'ScrolledWindow', 'Create scrolled window'),
232 (ID_NEW
.HTML_WINDOW
, 'HtmlWindow', 'Create HTML window'),
233 (ID_NEW
.CALENDAR_CTRL
, 'CalendarCtrl', 'Create calendar control'),
234 (ID_NEW
.GENERIC_DIR_CTRL
, 'GenericDirCtrl', 'Create generic dir control'),
235 (ID_NEW
.UNKNOWN
, 'Unknown', 'Create custom control placeholder'),
237 ['button', 'Buttons',
238 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
239 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
240 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
241 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
242 (ID_NEW
.TOGGLE_BUTTON
, 'ToggleButton', 'Create toggle button'),
245 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
246 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
247 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
248 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
249 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
251 ['container', 'Containers',
252 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
253 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
254 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
255 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
256 # (ID_NEW.WIZARD_PAGE, 'Wizard Page', 'Create wizard page'),
257 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
260 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
261 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
262 'Create static box sizer'),
263 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
264 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
265 'Create flexgrid sizer'),
266 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
267 'Create gridbag sizer'),
268 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
269 (ID_NEW
.STD_DIALOG_BUTTON_SIZER
, 'StdDialogButtonSizer',
270 'Create standard button sizer'),
273 self
.menuControls
= [
274 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
275 (ID_NEW
.MENU_ITEM
, 'MenuItem', 'Create menu item'),
276 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
278 self
.toolBarControls
= [
279 (ID_NEW
.TOOL
, 'Tool', 'Create tool'),
280 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
281 ['control', 'Various controls',
282 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
283 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
284 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
285 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
286 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
287 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
288 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
289 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
290 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list control'),
291 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
293 ['button', 'Buttons',
294 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
295 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
296 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
297 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
300 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
301 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
302 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
303 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
304 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
308 ################################################################################
310 # Set menu to list items.
311 # Each menu command is a tuple (id, label, help)
312 # submenus are lists [id, label, help, submenu]
313 # and separators are any other type
314 def SetMenu(m
, list):
316 if type(l
) == types
.TupleType
:
318 elif type(l
) == types
.ListType
:
320 SetMenu(subMenu
, l
[2:])
321 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
324 # Same, but adds 1000 to all IDs
325 def SetMenu2(m
, list):
327 if type(l
) == types
.TupleType
:
329 l
= (1000 + l
[0],) + l
[1:]
331 elif type(l
) == types
.ListType
:
333 SetMenu2(subMenu
, l
[2:])
334 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
338 ################################################################################
341 def __init__(self
, pos
, size
):
342 if size
.width
== -1: size
.width
= 0
343 if size
.height
== -1: size
.height
= 0
345 l1
= wxWindow(w
, -1, pos
, wxSize(size
.width
, 2))
346 l1
.SetBackgroundColour(wxRED
)
347 l2
= wxWindow(w
, -1, pos
, wxSize(2, size
.height
))
348 l2
.SetBackgroundColour(wxRED
)
349 l3
= wxWindow(w
, -1, wxPoint(pos
.x
+ size
.width
- 2, pos
.y
), wxSize(2, size
.height
))
350 l3
.SetBackgroundColour(wxRED
)
351 l4
= wxWindow(w
, -1, wxPoint(pos
.x
, pos
.y
+ size
.height
- 2), wxSize(size
.width
, 2))
352 l4
.SetBackgroundColour(wxRED
)
353 self
.lines
= [l1
, l2
, l3
, l4
]
354 # Move highlight to a new position
355 def Replace(self
, pos
, size
):
356 if size
.width
== -1: size
.width
= 0
357 if size
.height
== -1: size
.height
= 0
358 self
.lines
[0].SetDimensions(pos
.x
, pos
.y
, size
.width
, 2)
359 self
.lines
[1].SetDimensions(pos
.x
, pos
.y
, 2, size
.height
)
360 self
.lines
[2].SetDimensions(pos
.x
+ size
.width
- 2, pos
.y
, 2, size
.height
)
361 self
.lines
[3].SetDimensions(pos
.x
, pos
.y
+ size
.height
- 2, size
.width
, 2)
364 map(wxWindow
.Destroy
, self
.lines
)
365 g
.testWin
.highLight
= None
367 map(wxWindow
.Refresh
, self
.lines
)
369 ################################################################################
371 class XML_Tree(wxTreeCtrl
):
372 def __init__(self
, parent
, id):
373 wxTreeCtrl
.__init
__(self
, parent
, id, style
= wxTR_HAS_BUTTONS
)
374 self
.SetBackgroundColour(wxColour(224, 248, 224))
376 EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
377 # One works on Linux, another on Windows
378 if wxPlatform
== '__WXGTK__':
379 EVT_TREE_ITEM_ACTIVATED(self
, self
.GetId(), self
.OnItemActivated
)
381 EVT_LEFT_DCLICK(self
, self
.OnDClick
)
382 EVT_RIGHT_DOWN(self
, self
.OnRightDown
)
383 EVT_TREE_ITEM_EXPANDED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
384 EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
386 self
.selection
= None
387 self
.needUpdate
= False
388 self
.pendingHighLight
= None
389 self
.ctrl
= self
.shift
= False
392 il
= wxImageList(16, 16, True)
393 self
.rootImage
= il
.Add(images
.getTreeRootImage().Scale(16,16).ConvertToBitmap())
394 xxxObject
.image
= il
.Add(images
.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
395 xxxPanel
.image
= il
.Add(images
.getTreePanelImage().Scale(16,16).ConvertToBitmap())
396 xxxDialog
.image
= il
.Add(images
.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
397 xxxFrame
.image
= il
.Add(images
.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
398 xxxMenuBar
.image
= il
.Add(images
.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
399 xxxMenu
.image
= il
.Add(images
.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
400 xxxMenuItem
.image
= il
.Add(images
.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
401 xxxToolBar
.image
= il
.Add(images
.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
402 xxxTool
.image
= il
.Add(images
.getTreeToolImage().Scale(16,16).ConvertToBitmap())
403 xxxSeparator
.image
= il
.Add(images
.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
404 xxxSizer
.imageH
= il
.Add(images
.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
405 xxxSizer
.imageV
= il
.Add(images
.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
406 xxxStaticBoxSizer
.imageH
= il
.Add(images
.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
407 xxxStaticBoxSizer
.imageV
= il
.Add(images
.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
408 xxxGridSizer
.image
= il
.Add(images
.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
409 xxxFlexGridSizer
.image
= il
.Add(images
.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
411 self
.SetImageList(il
)
413 def RegisterKeyEvents(self
):
414 EVT_KEY_DOWN(self
, g
.tools
.OnKeyDown
)
415 EVT_KEY_UP(self
, g
.tools
.OnKeyUp
)
416 EVT_ENTER_WINDOW(self
, g
.tools
.OnMouse
)
417 EVT_LEAVE_WINDOW(self
, g
.tools
.OnMouse
)
420 self
.selection
= None
421 wxTreeCtrl
.Unselect(self
)
424 def ExpandAll(self
, item
):
425 if self
.ItemHasChildren(item
):
427 i
, cookie
= self
.GetFirstChild(item
)
431 i
, cookie
= self
.GetNextChild(item
, cookie
)
434 def CollapseAll(self
, item
):
435 if self
.ItemHasChildren(item
):
436 i
, cookie
= self
.GetFirstChild(item
)
440 i
, cookie
= self
.GetNextChild(item
, cookie
)
447 self
.DeleteAllItems()
448 # Add minimal structure
449 if self
.dom
: self
.dom
.unlink()
450 self
.dom
= MyDocument()
451 self
.dummyNode
= self
.dom
.createComment('dummy node')
453 self
.mainNode
= self
.dom
.createElement('resource')
454 self
.dom
.appendChild(self
.mainNode
)
455 self
.rootObj
= xxxMainNode(self
.dom
)
456 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
457 data
=wxTreeItemData(self
.rootObj
))
458 self
.SetItemHasChildren(self
.root
)
460 self
.Expand(self
.root
)
462 # Clear old data and set new
463 def SetData(self
, dom
):
464 self
.DeleteAllItems()
465 # Add minimal structure
466 if self
.dom
: self
.dom
.unlink()
468 self
.dummyNode
= self
.dom
.createComment('dummy node')
469 # Find 'resource' child, add it's children
470 self
.mainNode
= dom
.documentElement
471 self
.rootObj
= xxxMainNode(self
.dom
)
472 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
473 data
=wxTreeItemData(self
.rootObj
))
474 self
.SetItemHasChildren(self
.root
)
475 nodes
= self
.mainNode
.childNodes
[:]
478 self
.AddNode(self
.root
, None, node
)
480 self
.mainNode
.removeChild(node
)
482 self
.Expand(self
.root
)
485 # Add tree item for given parent item if node is DOM element node with
486 # 'object' tag. xxxParent is parent xxx object
487 def AddNode(self
, itemParent
, xxxParent
, node
):
488 # Set item data to current node
490 xxx
= MakeXXXFromDOM(xxxParent
, node
)
492 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
494 treeObj
= xxx
.treeObject()
496 item
= self
.AppendItem(itemParent
, treeObj
.treeName(),
497 image
=treeObj
.treeImage(),
498 data
=wxTreeItemData(xxx
))
499 # Try to find children objects
500 if treeObj
.hasChildren
:
501 nodes
= treeObj
.element
.childNodes
[:]
504 self
.AddNode(item
, treeObj
, n
)
505 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
506 treeObj
.element
.removeChild(n
)
509 # Insert new item at specific position
510 def InsertNode(self
, itemParent
, parent
, elem
, nextItem
):
511 # Insert in XML tree and wxWin
512 xxx
= MakeXXXFromDOM(parent
, elem
)
513 # If nextItem is None, we append to parent, otherwise insert before it
515 node
= self
.GetPyData(nextItem
).element
516 parent
.element
.insertBefore(elem
, node
)
517 # Inserting before is difficult, se we insert after or first child
518 index
= self
.ItemIndex(nextItem
)
519 newItem
= self
.InsertItemBefore(itemParent
, index
,
520 xxx
.treeName(), image
=xxx
.treeImage())
521 self
.SetPyData(newItem
, xxx
)
523 parent
.element
.appendChild(elem
)
524 newItem
= self
.AppendItem(itemParent
, xxx
.treeName(), image
=xxx
.treeImage(),
525 data
=wxTreeItemData(xxx
))
528 treeObj
= xxx
.treeObject()
529 for n
in treeObj
.element
.childNodes
:
531 self
.AddNode(newItem
, treeObj
, n
)
534 # Remove leaf of tree, return it's data object
535 def RemoveLeaf(self
, leaf
):
536 xxx
= self
.GetPyData(leaf
)
538 parent
= node
.parentNode
539 parent
.removeChild(node
)
541 # Reset selection object
542 self
.selection
= None
544 # Find position relative to the top-level window
545 def FindNodePos(self
, item
):
547 if item
== g
.testWin
.item
: return wxPoint(0, 0)
548 itemParent
= self
.GetItemParent(item
)
550 obj
= self
.FindNodeObject(item
)
551 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
552 notebook
= self
.FindNodeObject(itemParent
)
554 for i
in range(notebook
.GetPageCount()):
555 if notebook
.GetPage(i
) == obj
:
556 if notebook
.GetSelection() != i
:
557 notebook
.SetSelection(i
)
558 # Remove highlight - otherwise highlight window won't be visible
559 if g
.testWin
.highLight
:
560 g
.testWin
.highLight
.Remove()
562 # Find first ancestor which is a wxWindow (not a sizer)
563 winParent
= itemParent
564 while self
.GetPyData(winParent
).isSizer
:
565 winParent
= self
.GetItemParent(winParent
)
566 # Notebook children are layed out in a little strange way
567 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
568 parentPos
= wxPoint(0,0)
570 parentPos
= self
.FindNodePos(winParent
)
571 # Position (-1,-1) is really (0,0)
572 pos
= obj
.GetPosition()
573 if pos
== (-1,-1): pos
= (0,0)
574 return parentPos
+ pos
576 # Find window (or sizer) corresponding to a tree item.
577 def FindNodeObject(self
, item
):
579 # If top-level, return testWin (or panel its panel)
580 if item
== testWin
.item
: return testWin
.panel
581 itemParent
= self
.GetItemParent(item
)
582 xxx
= self
.GetPyData(item
).treeObject()
583 parentWin
= self
.FindNodeObject(itemParent
)
584 # Top-level sizer? return window's sizer
585 if xxx
.isSizer
and isinstance(parentWin
, wxWindow
):
586 return parentWin
.GetSizer()
587 elif isinstance(xxx
.parent
, xxxToolBar
):
588 # How to get tool from toolbar?
589 return parentWin
.GetChildren()[0]
590 # Otherwise get parent's object and it's child
591 child
= parentWin
.GetChildren()[self
.ItemIndex(item
)]
592 # Return window or sizer for sizer items
593 if child
.GetClassName() == 'wxSizerItem':
594 if child
.IsWindow(): child
= child
.GetWindow()
595 elif child
.IsSizer():
596 child
= child
.GetSizer()
597 # Test for notebook sizers
598 if isinstance(child
, wxNotebookSizer
):
599 child
= child
.GetNotebook()
602 def OnSelChanged(self
, evt
):
603 self
.ChangeSelection(evt
.GetItem())
605 def ChangeSelection(self
, item
):
607 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
608 #oldItem = evt.GetOldItem()
610 oldItem
= self
.selection
612 xxx
= self
.GetPyData(oldItem
)
613 # If some data was modified, apply changes
614 if g
.panel
.IsModified():
615 self
.Apply(xxx
, oldItem
)
616 #if conf.autoRefresh:
618 if g
.testWin
.highLight
:
619 g
.testWin
.highLight
.Remove()
620 self
.needUpdate
= True
621 status
= 'Changes were applied'
622 g
.frame
.SetStatusText(status
)
624 self
.selection
= item
625 if not self
.selection
.IsOk():
626 self
.selection
= None
628 xxx
= self
.GetPyData(self
.selection
)
633 # Highlighting is done in OnIdle
634 self
.pendingHighLight
= self
.selection
636 # Check if item is in testWin subtree
637 def IsHighlatable(self
, item
):
638 if item
== g
.testWin
.item
: return False
639 while item
!= self
.root
:
640 item
= self
.GetItemParent(item
)
641 if item
== g
.testWin
.item
: return True
644 # Highlight selected item
645 def HighLight(self
, item
):
646 self
.pendingHighLight
= None
647 # Can highlight only with some top-level windows
648 if not g
.testWin
or self
.GetPyData(g
.testWin
.item
).treeObject().__class
__ \
649 not in [xxxDialog
, xxxPanel
, xxxFrame
]:
651 # If a control from another window is selected, remove highlight
652 if not self
.IsHighlatable(item
):
653 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
655 # Get window/sizer object
656 obj
, pos
= self
.FindNodeObject(item
), self
.FindNodePos(item
)
659 # Negative positions are not working quite well
660 if g
.testWin
.highLight
:
661 g
.testWin
.highLight
.Replace(pos
, size
)
663 g
.testWin
.highLight
= HighLightBox(pos
, size
)
664 g
.testWin
.highLight
.Refresh()
665 g
.testWin
.highLight
.item
= item
667 def ShowTestWindow(self
, item
):
668 xxx
= self
.GetPyData(item
)
669 if g
.panel
.IsModified():
670 self
.Apply(xxx
, item
) # apply changes
671 treeObj
= xxx
.treeObject()
672 if treeObj
.className
not in ['wxFrame', 'wxPanel', 'wxDialog',
673 'wxMenuBar', 'wxToolBar', 'wxWizard',
674 'wxWizardPageSimple']:
675 wxLogMessage('No view for this element (yet)')
678 if g
.testWin
: # Reset old
679 self
.SetItemBold(g
.testWin
.item
, False)
680 self
.CreateTestWin(item
)
681 # Maybe an error occurred, so we need to test
682 if g
.testWin
: self
.SetItemBold(g
.testWin
.item
)
684 # Double-click on Linux
685 def OnItemActivated(self
, evt
):
686 if evt
.GetItem() != self
.root
:
687 self
.ShowTestWindow(evt
.GetItem())
689 # Double-click on Windows
690 def OnDClick(self
, evt
):
691 item
, flags
= self
.HitTest(evt
.GetPosition())
692 if flags
in [wxTREE_HITTEST_ONITEMBUTTON
, wxTREE_HITTEST_ONITEMLABEL
]:
693 if item
!= self
.root
: self
.ShowTestWindow(item
)
697 def OnItemExpandedCollapsed(self
, evt
):
698 # Update tool palette
702 # (re)create test window
703 def CreateTestWin(self
, item
):
705 # Create a window with this resource
706 xxx
= self
.GetPyData(item
).treeObject()
709 # if xxx.__class__ == xxxFrame:
710 # Frame can't have many children,
711 # but it's first child possibly can...
712 # child = self.GetFirstChild(item)[0]
713 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
714 # # Clean-up before recursive call or error
715 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
717 # self.CreateTestWin(child)
722 # Close old window, remember where it was
725 pos
= testWin
.GetPosition()
726 if item
== testWin
.item
:
727 # Remember highlight if same top-level window
728 if testWin
.highLight
:
729 highLight
= testWin
.highLight
.item
730 if xxx
.className
== 'wxPanel':
731 if testWin
.highLight
:
732 testWin
.pendingHighLight
= highLight
733 testWin
.highLight
.Remove()
734 testWin
.panel
.Destroy()
738 testWin
= g
.testWin
= None
741 testWin
= g
.testWin
= None
745 memFile
= MemoryFile('xxx.xrc')
746 # Create partial XML file - faster for big files
749 mainNode
= dom
.createElement('resource')
750 dom
.appendChild(mainNode
)
752 # Remove temporarily from old parent
754 # Change window id to _XRCED_T_W. This gives some name for
755 # unnamed windows, and for named gives the possibility to
756 # write sawfish scripts.
761 elem
.setAttribute('name', STD_NAME
)
762 # Replace wizard page class temporarily
763 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
764 oldCl
= elem
.getAttribute('class')
765 elem
.setAttribute('class', 'wxPanel')
766 parent
= elem
.parentNode
767 next
= elem
.nextSibling
768 parent
.replaceChild(self
.dummyNode
, elem
)
769 # Append to new DOM, write it
770 mainNode
.appendChild(elem
)
771 dom
.writexml(memFile
, encoding
=self
.rootObj
.params
['encoding'].value())
773 mainNode
.removeChild(elem
)
775 parent
.replaceChild(elem
, self
.dummyNode
)
776 # Remove temporary name or restore changed
778 elem
.removeAttribute('name')
780 elem
.setAttribute('name', xxx
.name
)
781 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
782 elem
.setAttribute('class', oldCl
)
783 memFile
.close() # write to wxMemoryFS
784 xmlFlags
= wxXRC_NO_SUBCLASSING
785 # Use translations if encoding is not specified
786 if not g
.currentEncoding
:
787 xmlFlags
!= wxXRC_USE_LOCALE
788 res
= wxXmlResource('', xmlFlags
)
789 res
.Load('memory:xxx.xrc')
791 if xxx
.__class
__ == xxxFrame
:
792 # Frame can't have many children,
793 # but it's first child possibly can...
794 # child = self.GetFirstChild(item)[0]
795 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
796 # # Clean-up before recursive call or error
797 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
799 # self.CreateTestWin(child)
801 # This currently works under GTK, but not under MSW
802 testWin
= g
.testWin
= wxPreFrame()
803 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
805 testWin
.panel
= testWin
806 testWin
.CreateStatusBar()
807 testWin
.SetClientSize(testWin
.GetBestSize())
808 testWin
.SetPosition(pos
)
810 elif xxx
.__class
__ == xxxPanel
:
813 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Panel: ' + name
,
814 pos
=pos
, name
=STD_NAME
)
815 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
816 testWin
.SetClientSize(testWin
.GetBestSize())
818 elif xxx
.__class
__ == xxxDialog
:
819 testWin
= g
.testWin
= res
.LoadDialog(None, STD_NAME
)
820 testWin
.panel
= testWin
822 testWin
.SetPosition(pos
)
824 # Dialog's default code does not produce EVT_CLOSE
825 EVT_BUTTON(testWin
, wxID_OK
, self
.OnCloseTestWin
)
826 EVT_BUTTON(testWin
, wxID_CANCEL
, self
.OnCloseTestWin
)
827 elif xxx
.__class
__ == xxxWizard
:
829 res
.LoadOnObject(wiz
, None, STD_NAME
, 'wxWizard')
830 # Find first page (don't know better way)
832 for w
in wiz
.GetChildren():
833 if isinstance(w
, wxWizardPage
):
837 wxLogError('Wizard is empty')
839 # Wizard should be modal
840 self
.SetItemBold(item
)
842 self
.SetItemBold(item
, False)
844 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
847 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Wizard page: ' + name
,
848 pos
=pos
, name
=STD_NAME
)
849 testWin
.panel
= wxPrePanel()
850 res
.LoadOnObject(testWin
.panel
, testWin
, STD_NAME
, 'wxPanel')
851 testWin
.SetClientSize(testWin
.GetBestSize())
853 elif xxx
.__class
__ == xxxMenuBar
:
854 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'MenuBar: ' + name
,
855 pos
=pos
, name
=STD_NAME
)
857 # Set status bar to display help
858 testWin
.CreateStatusBar()
859 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
860 testWin
.SetMenuBar(testWin
.menuBar
)
862 elif xxx
.__class
__ == xxxToolBar
:
863 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'ToolBar: ' + name
,
864 pos
=pos
, name
=STD_NAME
)
866 # Set status bar to display help
867 testWin
.CreateStatusBar()
868 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
869 testWin
.SetToolBar(testWin
.toolBar
)
873 EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
874 testWin
.highLight
= None
875 if highLight
and not self
.pendingHighLight
:
876 self
.HighLight(highLight
)
879 self
.SetItemBold(item
, False)
880 g
.testWinPos
= g
.testWin
.GetPosition()
884 wxLogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
885 wxLogError('Error loading resource')
886 wxMemoryFSHandler_RemoveFile('xxx.xrc')
889 def CloseTestWindow(self
):
890 if not g
.testWin
: return
891 self
.SetItemBold(g
.testWin
.item
, False)
892 g
.frame
.tb
.ToggleTool(g
.frame
.ID_TOOL_LOCATE
, False)
893 g
.testWinPos
= g
.testWin
.GetPosition()
897 def OnCloseTestWin(self
, evt
):
898 self
.CloseTestWindow()
900 # Return item index in parent
901 def ItemIndex(self
, item
):
902 n
= 0 # index of sibling
903 prev
= self
.GetPrevSibling(item
)
905 prev
= self
.GetPrevSibling(prev
)
909 # Full tree index of an item - list of positions
910 def ItemFullIndex(self
, item
):
911 if not item
.IsOk(): return None
913 while item
!= self
.root
:
914 l
.insert(0, self
.ItemIndex(item
))
915 item
= self
.GetItemParent(item
)
917 # Get item position from full index
918 def ItemAtFullIndex(self
, index
):
919 if index
is None: return wxTreeItemId()
922 item
= self
.GetFirstChild(item
)[0]
923 for k
in range(i
): item
= self
.GetNextSibling(item
)
926 # True if next item should be inserted after current (vs. appended to it)
927 def NeedInsert(self
, item
):
928 xxx
= self
.GetPyData(item
)
929 if item
== self
.root
: return False # root item
930 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
932 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
935 def OnRightDown(self
, evt
):
936 pullDownMenu
= g
.pullDownMenu
938 pt
= evt
.GetPosition();
939 item
, flags
= self
.HitTest(pt
)
940 if item
.Ok() and flags
& wxTREE_HITTEST_ONITEM
:
941 self
.SelectItem(item
)
946 item
= self
.selection
948 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
949 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
951 # self.ctrl = evt.ControlDown() # save Ctrl state
952 # self.shift = evt.ShiftDown() # and Shift too
953 m
= wxMenu() # create menu
957 needInsert
= self
.NeedInsert(item
)
958 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
959 SetMenu(m
, pullDownMenu
.topLevel
)
961 xxx
= self
.GetPyData(item
).treeObject()
962 # Check parent for possible child nodes if inserting sibling
963 if needInsert
: xxx
= xxx
.parent
964 if xxx
.__class
__ == xxxMenuBar
:
965 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
966 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
967 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
968 SetMenu(m
, pullDownMenu
.toolBarControls
)
969 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
970 SetMenu(m
, pullDownMenu
.menuControls
)
972 SetMenu(m
, pullDownMenu
.controls
)
973 if xxx
.__class
__ == xxxNotebook
:
974 m
.Enable(m
.FindItem('sizer'), False)
975 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
976 m
.Enable(ID_NEW
.SPACER
, False)
977 # Select correct label for create menu
980 menu
.AppendMenu(wxNewId(), 'Insert Child', m
,
981 'Create child object as the first child')
983 menu
.AppendMenu(wxNewId(), 'Append Child', m
,
984 'Create child object as the last child')
987 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
988 'Create sibling before selected object')
990 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
991 'Create sibling after selected object')
993 if item
!= self
.root
:
994 xxx
= self
.GetPyData(item
).treeObject()
995 m
= wxMenu() # create replace menu
996 if xxx
.__class
__ == xxxMenuBar
:
997 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
998 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
999 SetMenu2(m
, pullDownMenu
.menuControls
)
1000 elif xxx
.__class
__ == xxxToolBar
and \
1001 self
.GetItemParent(item
) == self
.root
:
1003 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
1005 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
1006 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
1007 (ID_NEW
.FRAME
, 'Frame', 'Create frame')])
1009 SetMenu2(m
, pullDownMenu
.sizers
)
1011 SetMenu2(m
, pullDownMenu
.controls
)
1013 menu
.AppendMenu(id, 'Replace With', m
)
1014 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
1015 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
1016 'Set subclass property')
1017 menu
.AppendSeparator()
1018 # Not using standart IDs because we don't want to show shortcuts
1019 menu
.Append(wxID_CUT
, 'Cut', 'Cut to the clipboard')
1020 menu
.Append(wxID_COPY
, 'Copy', 'Copy to the clipboard')
1021 if self
.ctrl
and item
!= self
.root
:
1022 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
1023 'Paste from the clipboard as a sibling')
1025 menu
.Append(wxID_PASTE
, 'Paste', 'Paste from the clipboard')
1026 menu
.Append(pullDownMenu
.ID_DELETE
,
1027 'Delete', 'Delete object')
1028 if self
.ItemHasChildren(item
):
1029 menu
.AppendSeparator()
1030 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
1031 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
1032 self
.PopupMenu(menu
, evt
.GetPosition())
1036 def Apply(self
, xxx
, item
):
1039 xxx
= xxx
.treeObject()
1040 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
1041 self
.SetItemText(item
, xxx
.treeName())
1042 # Item width may have changed
1043 # !!! Tric to update tree width (wxGTK, ??)
1044 self
.SetIndent(self
.GetIndent())
1045 # Change tree icon for sizers
1046 if isinstance(xxx
, xxxBoxSizer
):
1047 self
.SetItemImage(item
, xxx
.treeImage())
1048 # Set global modified state
1049 g
.frame
.modified
= True