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 self
.buffer += data
.encode(g
.currentEncoding
)
25 self
.buffer += data
.encode()
27 wxMemoryFSHandler_AddFile(self
.name
, self
.buffer)
29 ################################################################################
31 # Redefine writing to include encoding
32 class MyDocument(minidom
.Document
):
34 minidom
.Document
.__init
__(self
)
36 def writexml(self
, writer
, indent
="", addindent
="", newl
="", encoding
=""):
37 if encoding
: encdstr
= 'encoding="%s"' % encoding
39 writer
.write('<?xml version="1.0" %s?>\n' % encdstr
)
40 for node
in self
.childNodes
:
41 node
.writexml(writer
, indent
, addindent
, newl
)
43 ################################################################################
45 # Ids for menu commands
55 STATIC_TEXT
= wxNewId()
59 BITMAP_BUTTON
= wxNewId()
60 RADIO_BUTTON
= wxNewId()
61 SPIN_BUTTON
= wxNewId()
62 TOGGLE_BUTTON
= wxNewId()
64 STATIC_BOX
= wxNewId()
70 STATIC_LINE
= wxNewId()
71 STATIC_BITMAP
= wxNewId()
75 SCROLL_BAR
= wxNewId()
78 CHECK_LIST
= wxNewId()
80 SPLITTER_WINDOW
= wxNewId()
81 SCROLLED_WINDOW
= wxNewId()
82 HTML_WINDOW
= wxNewId()
83 CALENDAR_CTRL
= wxNewId()
84 GENERIC_DIR_CTRL
= wxNewId()
88 WIZARD_PAGE
= wxNewId()
89 WIZARD_PAGE_SIMPLE
= wxNewId()
90 STATUS_BAR
= wxNewId()
93 STATIC_BOX_SIZER
= wxNewId()
94 GRID_SIZER
= wxNewId()
95 FLEX_GRID_SIZER
= wxNewId()
96 GRID_BAG_SIZER
= wxNewId()
97 STD_DIALOG_BUTTON_SIZER
= wxNewId()
103 MENU_ITEM
= wxNewId()
104 SEPARATOR
= wxNewId()
108 ID_EXPAND
= wxNewId()
109 ID_COLLAPSE
= wxNewId()
110 ID_PASTE_SIBLING
= wxNewId()
111 ID_TOOL_PASTE
= wxNewId()
112 ID_SUBCLASS
= wxNewId()
114 def __init__(self
, parent
):
115 self
.ID_DELETE
= parent
.ID_DELETE
116 EVT_MENU_RANGE(parent
, ID_NEW
.PANEL
, ID_NEW
.LAST
, parent
.OnCreate
)
117 EVT_MENU_RANGE(parent
, 1000 + ID_NEW
.PANEL
, 1000 + ID_NEW
.LAST
, parent
.OnReplace
)
118 EVT_MENU(parent
, self
.ID_COLLAPSE
, parent
.OnCollapse
)
119 EVT_MENU(parent
, self
.ID_EXPAND
, parent
.OnExpand
)
120 EVT_MENU(parent
, self
.ID_PASTE_SIBLING
, parent
.OnPaste
)
121 EVT_MENU(parent
, self
.ID_SUBCLASS
, parent
.OnSubclass
)
122 # We connect to tree, but process in frame
123 EVT_MENU_HIGHLIGHT_ALL(g
.tree
, parent
.OnPullDownHighlight
)
125 # Mapping from IDs to element names
127 ID_NEW
.PANEL
: 'wxPanel',
128 ID_NEW
.DIALOG
: 'wxDialog',
129 ID_NEW
.FRAME
: 'wxFrame',
130 ID_NEW
.WIZARD
: 'wxWizard',
131 ID_NEW
.WIZARD_PAGE
: 'wxWizardPage',
132 ID_NEW
.WIZARD_PAGE_SIMPLE
: 'wxWizardPageSimple',
133 ID_NEW
.TOOL_BAR
: 'wxToolBar',
135 ID_NEW
.MENU_BAR
: 'wxMenuBar',
136 ID_NEW
.MENU
: 'wxMenu',
137 ID_NEW
.MENU_ITEM
: 'wxMenuItem',
138 ID_NEW
.SEPARATOR
: 'separator',
140 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
141 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
143 ID_NEW
.BUTTON
: 'wxButton',
144 ID_NEW
.BITMAP_BUTTON
: 'wxBitmapButton',
145 ID_NEW
.RADIO_BUTTON
: 'wxRadioButton',
146 ID_NEW
.SPIN_BUTTON
: 'wxSpinButton',
147 ID_NEW
.TOGGLE_BUTTON
: 'wxToggleButton',
149 ID_NEW
.STATIC_BOX
: 'wxStaticBox',
150 ID_NEW
.CHECK_BOX
: 'wxCheckBox',
151 ID_NEW
.RADIO_BOX
: 'wxRadioBox',
152 ID_NEW
.COMBO_BOX
: 'wxComboBox',
153 ID_NEW
.LIST_BOX
: 'wxListBox',
155 ID_NEW
.STATIC_LINE
: 'wxStaticLine',
156 ID_NEW
.STATIC_BITMAP
: 'wxStaticBitmap',
157 ID_NEW
.CHOICE
: 'wxChoice',
158 ID_NEW
.SLIDER
: 'wxSlider',
159 ID_NEW
.GAUGE
: 'wxGauge',
160 ID_NEW
.SCROLL_BAR
: 'wxScrollBar',
161 ID_NEW
.TREE_CTRL
: 'wxTreeCtrl',
162 ID_NEW
.LIST_CTRL
: 'wxListCtrl',
163 ID_NEW
.CHECK_LIST
: 'wxCheckListBox',
164 ID_NEW
.NOTEBOOK
: 'wxNotebook',
165 ID_NEW
.SPLITTER_WINDOW
: 'wxSplitterWindow',
166 ID_NEW
.SCROLLED_WINDOW
: 'wxScrolledWindow',
167 ID_NEW
.HTML_WINDOW
: 'wxHtmlWindow',
168 ID_NEW
.CALENDAR_CTRL
: 'wxCalendarCtrl',
169 ID_NEW
.GENERIC_DIR_CTRL
: 'wxGenericDirCtrl',
170 ID_NEW
.SPIN_CTRL
: 'wxSpinCtrl',
172 ID_NEW
.BOX_SIZER
: 'wxBoxSizer',
173 ID_NEW
.STATIC_BOX_SIZER
: 'wxStaticBoxSizer',
174 ID_NEW
.GRID_SIZER
: 'wxGridSizer',
175 ID_NEW
.FLEX_GRID_SIZER
: 'wxFlexGridSizer',
176 ID_NEW
.GRID_BAG_SIZER
: 'wxGridBagSizer',
177 ID_NEW
.STD_DIALOG_BUTTON_SIZER
: 'wxStdDialogButtonSizer',
178 ID_NEW
.SPACER
: 'spacer',
179 ID_NEW
.UNKNOWN
: 'unknown',
182 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
183 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
184 (ID_NEW
.FRAME
, 'Frame', 'Create frame'),
185 (ID_NEW
.WIZARD
, 'Wizard', 'Create wizard'),
187 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
188 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
189 (ID_NEW
.MENU
, 'Menu', 'Create menu')
192 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
193 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
194 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
195 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
196 # (ID_NEW.WIZARD_PAGE, 'WizardPage', 'Create wizard page'),
197 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
200 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
201 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
202 'Create static box sizer'),
203 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
204 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
205 'Create flexgrid sizer'),
206 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
207 'Create gridbag sizer'),
208 # (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
209 # 'Create standard button sizer'),
210 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
213 ['control', 'Various controls',
214 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
215 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
216 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
217 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
218 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
219 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
220 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
221 (ID_NEW
.SPIN_CTRL
, 'SpinCtrl', 'Create spin'),
222 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
223 (ID_NEW
.TREE_CTRL
, 'TreeCtrl', 'Create tree'),
224 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list'),
225 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
226 (ID_NEW
.SCROLLED_WINDOW
, 'ScrolledWindow', 'Create scrolled window'),
227 (ID_NEW
.HTML_WINDOW
, 'HtmlWindow', 'Create HTML window'),
228 (ID_NEW
.CALENDAR_CTRL
, 'CalendarCtrl', 'Create calendar control'),
229 (ID_NEW
.GENERIC_DIR_CTRL
, 'GenericDirCtrl', 'Create generic dir control'),
230 (ID_NEW
.UNKNOWN
, 'Unknown', 'Create custom control placeholder'),
232 ['button', 'Buttons',
233 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
234 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
235 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
236 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
237 (ID_NEW
.TOGGLE_BUTTON
, 'ToggleButton', 'Create toggle button'),
240 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
241 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
242 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
243 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
244 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
246 ['container', 'Containers',
247 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
248 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
249 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
250 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
251 # (ID_NEW.WIZARD_PAGE, 'Wizard Page', 'Create wizard page'),
252 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
255 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
256 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
257 'Create static box sizer'),
258 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
259 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
260 'Create flexgrid sizer'),
261 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
262 'Create gridbag sizer'),
263 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
264 (ID_NEW
.STD_DIALOG_BUTTON_SIZER
, 'StdDialogButtonSizer',
265 'Create standard button sizer'),
268 self
.menuControls
= [
269 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
270 (ID_NEW
.MENU_ITEM
, 'MenuItem', 'Create menu item'),
271 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
273 self
.toolBarControls
= [
274 (ID_NEW
.TOOL
, 'Tool', 'Create tool'),
275 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
276 ['control', 'Various controls',
277 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
278 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
279 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
280 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
281 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
282 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
283 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
284 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
285 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list control'),
286 (ID_NEW
.CHECK_LIST
, 'CheckList', 'Create check list'),
288 ['button', 'Buttons',
289 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
290 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
291 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
292 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
295 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
296 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
297 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
298 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
299 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
303 ################################################################################
305 # Set menu to list items.
306 # Each menu command is a tuple (id, label, help)
307 # submenus are lists [id, label, help, submenu]
308 # and separators are any other type
309 def SetMenu(m
, list):
311 if type(l
) == types
.TupleType
:
313 elif type(l
) == types
.ListType
:
315 SetMenu(subMenu
, l
[2:])
316 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
319 # Same, but adds 1000 to all IDs
320 def SetMenu2(m
, list):
322 if type(l
) == types
.TupleType
:
324 l
= (1000 + l
[0],) + l
[1:]
326 elif type(l
) == types
.ListType
:
328 SetMenu2(subMenu
, l
[2:])
329 m
.AppendMenu(wxNewId(), l
[0], subMenu
, l
[1])
333 ################################################################################
336 def __init__(self
, pos
, size
):
337 if size
.width
== -1: size
.width
= 0
338 if size
.height
== -1: size
.height
= 0
340 l1
= wxWindow(w
, -1, pos
, wxSize(size
.width
, 2))
341 l1
.SetBackgroundColour(wxRED
)
342 l2
= wxWindow(w
, -1, pos
, wxSize(2, size
.height
))
343 l2
.SetBackgroundColour(wxRED
)
344 l3
= wxWindow(w
, -1, wxPoint(pos
.x
+ size
.width
- 2, pos
.y
), wxSize(2, size
.height
))
345 l3
.SetBackgroundColour(wxRED
)
346 l4
= wxWindow(w
, -1, wxPoint(pos
.x
, pos
.y
+ size
.height
- 2), wxSize(size
.width
, 2))
347 l4
.SetBackgroundColour(wxRED
)
348 self
.lines
= [l1
, l2
, l3
, l4
]
349 # Move highlight to a new position
350 def Replace(self
, pos
, size
):
351 if size
.width
== -1: size
.width
= 0
352 if size
.height
== -1: size
.height
= 0
353 self
.lines
[0].SetDimensions(pos
.x
, pos
.y
, size
.width
, 2)
354 self
.lines
[1].SetDimensions(pos
.x
, pos
.y
, 2, size
.height
)
355 self
.lines
[2].SetDimensions(pos
.x
+ size
.width
- 2, pos
.y
, 2, size
.height
)
356 self
.lines
[3].SetDimensions(pos
.x
, pos
.y
+ size
.height
- 2, size
.width
, 2)
359 map(wxWindow
.Destroy
, self
.lines
)
360 g
.testWin
.highLight
= None
362 map(wxWindow
.Refresh
, self
.lines
)
364 ################################################################################
366 class XML_Tree(wxTreeCtrl
):
367 def __init__(self
, parent
, id):
368 wxTreeCtrl
.__init
__(self
, parent
, id, style
= wxTR_HAS_BUTTONS
)
369 self
.SetBackgroundColour(wxColour(224, 248, 224))
371 EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
372 # One works on Linux, another on Windows
373 if wxPlatform
== '__WXGTK__':
374 EVT_TREE_ITEM_ACTIVATED(self
, self
.GetId(), self
.OnItemActivated
)
376 EVT_LEFT_DCLICK(self
, self
.OnDClick
)
377 EVT_RIGHT_DOWN(self
, self
.OnRightDown
)
378 EVT_TREE_ITEM_EXPANDED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
379 EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
381 self
.selection
= None
382 self
.needUpdate
= False
383 self
.pendingHighLight
= None
384 self
.ctrl
= self
.shift
= False
387 il
= wxImageList(16, 16, True)
388 self
.rootImage
= il
.Add(images
.getTreeRootImage().Scale(16,16).ConvertToBitmap())
389 xxxObject
.image
= il
.Add(images
.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
390 xxxPanel
.image
= il
.Add(images
.getTreePanelImage().Scale(16,16).ConvertToBitmap())
391 xxxDialog
.image
= il
.Add(images
.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
392 xxxFrame
.image
= il
.Add(images
.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
393 xxxMenuBar
.image
= il
.Add(images
.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
394 xxxMenu
.image
= il
.Add(images
.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
395 xxxMenuItem
.image
= il
.Add(images
.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
396 xxxToolBar
.image
= il
.Add(images
.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
397 xxxTool
.image
= il
.Add(images
.getTreeToolImage().Scale(16,16).ConvertToBitmap())
398 xxxSeparator
.image
= il
.Add(images
.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
399 xxxSizer
.imageH
= il
.Add(images
.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
400 xxxSizer
.imageV
= il
.Add(images
.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
401 xxxStaticBoxSizer
.imageH
= il
.Add(images
.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
402 xxxStaticBoxSizer
.imageV
= il
.Add(images
.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
403 xxxGridSizer
.image
= il
.Add(images
.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
404 xxxFlexGridSizer
.image
= il
.Add(images
.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
406 self
.SetImageList(il
)
408 def RegisterKeyEvents(self
):
409 EVT_KEY_DOWN(self
, g
.tools
.OnKeyDown
)
410 EVT_KEY_UP(self
, g
.tools
.OnKeyUp
)
411 EVT_ENTER_WINDOW(self
, g
.tools
.OnMouse
)
412 EVT_LEAVE_WINDOW(self
, g
.tools
.OnMouse
)
415 self
.selection
= None
416 wxTreeCtrl
.Unselect(self
)
419 def ExpandAll(self
, item
):
420 if self
.ItemHasChildren(item
):
422 i
, cookie
= self
.GetFirstChild(item
)
426 i
, cookie
= self
.GetNextChild(item
, cookie
)
429 def CollapseAll(self
, item
):
430 if self
.ItemHasChildren(item
):
431 i
, cookie
= self
.GetFirstChild(item
)
435 i
, cookie
= self
.GetNextChild(item
, cookie
)
442 self
.DeleteAllItems()
443 # Add minimal structure
444 if self
.dom
: self
.dom
.unlink()
445 self
.dom
= MyDocument()
446 self
.dummyNode
= self
.dom
.createComment('dummy node')
448 self
.mainNode
= self
.dom
.createElement('resource')
449 self
.dom
.appendChild(self
.mainNode
)
450 self
.rootObj
= xxxMainNode(self
.dom
)
451 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
452 data
=wxTreeItemData(self
.rootObj
))
453 self
.SetItemHasChildren(self
.root
)
455 self
.Expand(self
.root
)
457 # Clear old data and set new
458 def SetData(self
, dom
):
459 self
.DeleteAllItems()
460 # Add minimal structure
461 if self
.dom
: self
.dom
.unlink()
463 self
.dummyNode
= self
.dom
.createComment('dummy node')
464 # Find 'resource' child, add it's children
465 self
.mainNode
= dom
.documentElement
466 self
.rootObj
= xxxMainNode(self
.dom
)
467 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
468 data
=wxTreeItemData(self
.rootObj
))
469 self
.SetItemHasChildren(self
.root
)
470 nodes
= self
.mainNode
.childNodes
[:]
473 self
.AddNode(self
.root
, None, node
)
475 self
.mainNode
.removeChild(node
)
477 self
.Expand(self
.root
)
480 # Add tree item for given parent item if node is DOM element node with
481 # 'object' tag. xxxParent is parent xxx object
482 def AddNode(self
, itemParent
, xxxParent
, node
):
483 # Set item data to current node
485 xxx
= MakeXXXFromDOM(xxxParent
, node
)
487 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
489 treeObj
= xxx
.treeObject()
491 item
= self
.AppendItem(itemParent
, treeObj
.treeName(),
492 image
=treeObj
.treeImage(),
493 data
=wxTreeItemData(xxx
))
494 # Try to find children objects
495 if treeObj
.hasChildren
:
496 nodes
= treeObj
.element
.childNodes
[:]
499 self
.AddNode(item
, treeObj
, n
)
500 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
501 treeObj
.element
.removeChild(n
)
504 # Insert new item at specific position
505 def InsertNode(self
, itemParent
, parent
, elem
, nextItem
):
506 # Insert in XML tree and wxWin
507 xxx
= MakeXXXFromDOM(parent
, elem
)
508 # If nextItem is None, we append to parent, otherwise insert before it
510 node
= self
.GetPyData(nextItem
).element
511 parent
.element
.insertBefore(elem
, node
)
512 # Inserting before is difficult, se we insert after or first child
513 index
= self
.ItemIndex(nextItem
)
514 newItem
= self
.InsertItemBefore(itemParent
, index
,
515 xxx
.treeName(), image
=xxx
.treeImage())
516 self
.SetPyData(newItem
, xxx
)
518 parent
.element
.appendChild(elem
)
519 newItem
= self
.AppendItem(itemParent
, xxx
.treeName(), image
=xxx
.treeImage(),
520 data
=wxTreeItemData(xxx
))
523 treeObj
= xxx
.treeObject()
524 for n
in treeObj
.element
.childNodes
:
526 self
.AddNode(newItem
, treeObj
, n
)
529 # Remove leaf of tree, return it's data object
530 def RemoveLeaf(self
, leaf
):
531 xxx
= self
.GetPyData(leaf
)
533 parent
= node
.parentNode
534 parent
.removeChild(node
)
536 # Reset selection object
537 self
.selection
= None
539 # Find position relative to the top-level window
540 def FindNodePos(self
, item
):
542 if item
== g
.testWin
.item
: return wxPoint(0, 0)
543 itemParent
= self
.GetItemParent(item
)
545 obj
= self
.FindNodeObject(item
)
546 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
547 notebook
= self
.FindNodeObject(itemParent
)
549 for i
in range(notebook
.GetPageCount()):
550 if notebook
.GetPage(i
) == obj
:
551 if notebook
.GetSelection() != i
:
552 notebook
.SetSelection(i
)
553 # Remove highlight - otherwise highlight window won't be visible
554 if g
.testWin
.highLight
:
555 g
.testWin
.highLight
.Remove()
557 # Find first ancestor which is a wxWindow (not a sizer)
558 winParent
= itemParent
559 while self
.GetPyData(winParent
).isSizer
:
560 winParent
= self
.GetItemParent(winParent
)
561 # Notebook children are layed out in a little strange way
562 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
563 parentPos
= wxPoint(0,0)
565 parentPos
= self
.FindNodePos(winParent
)
566 # Position (-1,-1) is really (0,0)
567 pos
= obj
.GetPosition()
568 if pos
== (-1,-1): pos
= (0,0)
569 return parentPos
+ pos
571 # Find window (or sizer) corresponding to a tree item.
572 def FindNodeObject(self
, item
):
574 # If top-level, return testWin (or panel its panel)
575 if item
== testWin
.item
: return testWin
.panel
576 itemParent
= self
.GetItemParent(item
)
577 xxx
= self
.GetPyData(item
).treeObject()
578 parentWin
= self
.FindNodeObject(itemParent
)
579 # Top-level sizer? return window's sizer
580 if xxx
.isSizer
and isinstance(parentWin
, wxWindow
):
581 return parentWin
.GetSizer()
582 # Otherwise get parent's object and it's child
583 child
= parentWin
.GetChildren()[self
.ItemIndex(item
)]
584 # Return window or sizer for sizer items
585 if child
.GetClassName() == 'wxSizerItem':
586 if child
.IsWindow(): child
= child
.GetWindow()
587 elif child
.IsSizer():
588 child
= child
.GetSizer()
589 # Test for notebook sizers
590 if isinstance(child
, wxNotebookSizer
):
591 child
= child
.GetNotebook()
594 def OnSelChanged(self
, evt
):
595 self
.ChangeSelection(evt
.GetItem())
597 def ChangeSelection(self
, item
):
599 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
600 #oldItem = evt.GetOldItem()
602 oldItem
= self
.selection
604 xxx
= self
.GetPyData(oldItem
)
605 # If some data was modified, apply changes
606 if g
.panel
.IsModified():
607 self
.Apply(xxx
, oldItem
)
608 #if conf.autoRefresh:
610 if g
.testWin
.highLight
:
611 g
.testWin
.highLight
.Remove()
612 self
.needUpdate
= True
613 status
= 'Changes were applied'
614 g
.frame
.SetStatusText(status
)
616 self
.selection
= item
617 if not self
.selection
.IsOk():
618 self
.selection
= None
620 xxx
= self
.GetPyData(self
.selection
)
625 # Highlighting is done in OnIdle
626 self
.pendingHighLight
= self
.selection
628 # Check if item is in testWin subtree
629 def IsHighlatable(self
, item
):
630 if item
== g
.testWin
.item
: return False
631 while item
!= self
.root
:
632 item
= self
.GetItemParent(item
)
633 if item
== g
.testWin
.item
: return True
636 # Highlight selected item
637 def HighLight(self
, item
):
638 self
.pendingHighLight
= None
639 # Can highlight only with some top-level windows
640 if not g
.testWin
or self
.GetPyData(g
.testWin
.item
).treeObject().__class
__ \
641 not in [xxxDialog
, xxxPanel
, xxxFrame
]:
643 # If a control from another window is selected, remove highlight
644 if not self
.IsHighlatable(item
):
645 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
647 # Get window/sizer object
648 obj
, pos
= self
.FindNodeObject(item
), self
.FindNodePos(item
)
651 # Negative positions are not working quite well
652 if g
.testWin
.highLight
:
653 g
.testWin
.highLight
.Replace(pos
, size
)
655 g
.testWin
.highLight
= HighLightBox(pos
, size
)
656 g
.testWin
.highLight
.Refresh()
657 g
.testWin
.highLight
.item
= item
659 def ShowTestWindow(self
, item
):
660 xxx
= self
.GetPyData(item
)
661 if g
.panel
.IsModified():
662 self
.Apply(xxx
, item
) # apply changes
663 treeObj
= xxx
.treeObject()
664 if treeObj
.className
not in ['wxFrame', 'wxPanel', 'wxDialog',
665 'wxMenuBar', 'wxToolBar', 'wxWizard',
666 'wxWizardPageSimple']:
667 wxLogMessage('No view for this element (yet)')
670 if g
.testWin
: # Reset old
671 self
.SetItemBold(g
.testWin
.item
, False)
672 self
.CreateTestWin(item
)
673 # Maybe an error occurred, so we need to test
674 if g
.testWin
: self
.SetItemBold(g
.testWin
.item
)
676 # Double-click on Linux
677 def OnItemActivated(self
, evt
):
678 if evt
.GetItem() != self
.root
:
679 self
.ShowTestWindow(evt
.GetItem())
681 # Double-click on Windows
682 def OnDClick(self
, evt
):
683 item
, flags
= self
.HitTest(evt
.GetPosition())
684 if flags
in [wxTREE_HITTEST_ONITEMBUTTON
, wxTREE_HITTEST_ONITEMLABEL
]:
685 if item
!= self
.root
: self
.ShowTestWindow(item
)
689 def OnItemExpandedCollapsed(self
, evt
):
690 # Update tool palette
694 # (re)create test window
695 def CreateTestWin(self
, item
):
697 # Create a window with this resource
698 xxx
= self
.GetPyData(item
).treeObject()
701 # if xxx.__class__ == xxxFrame:
702 # Frame can't have many children,
703 # but it's first child possibly can...
704 # child = self.GetFirstChild(item)[0]
705 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
706 # # Clean-up before recursive call or error
707 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
709 # self.CreateTestWin(child)
714 # Close old window, remember where it was
717 pos
= testWin
.GetPosition()
718 if item
== testWin
.item
:
719 # Remember highlight if same top-level window
720 if testWin
.highLight
:
721 highLight
= testWin
.highLight
.item
722 if xxx
.className
== 'wxPanel':
723 if testWin
.highLight
:
724 testWin
.pendingHighLight
= highLight
725 testWin
.highLight
.Remove()
726 testWin
.panel
.Destroy()
730 testWin
= g
.testWin
= None
733 testWin
= g
.testWin
= None
737 memFile
= MemoryFile('xxx.xrc')
738 # Create partial XML file - faster for big files
741 mainNode
= dom
.createElement('resource')
742 dom
.appendChild(mainNode
)
744 # Remove temporarily from old parent
746 # Change window id to _XRCED_T_W. This gives some name for
747 # unnamed windows, and for named gives the possibility to
748 # write sawfish scripts.
753 elem
.setAttribute('name', STD_NAME
)
754 # Replace wizard page class temporarily
755 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
756 oldCl
= elem
.getAttribute('class')
757 elem
.setAttribute('class', 'wxPanel')
758 parent
= elem
.parentNode
759 next
= elem
.nextSibling
760 parent
.replaceChild(self
.dummyNode
, elem
)
761 # Append to new DOM, write it
762 mainNode
.appendChild(elem
)
763 dom
.writexml(memFile
, encoding
=self
.rootObj
.params
['encoding'].value())
765 mainNode
.removeChild(elem
)
767 parent
.replaceChild(elem
, self
.dummyNode
)
768 # Remove temporary name or restore changed
770 elem
.removeAttribute('name')
772 elem
.setAttribute('name', xxx
.name
)
773 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
774 elem
.setAttribute('class', oldCl
)
775 memFile
.close() # write to wxMemoryFS
776 xmlFlags
= wxXRC_NO_SUBCLASSING
777 # Use translations if encoding is not specified
778 if not g
.currentEncoding
:
779 xmlFlags
!= wxXRC_USE_LOCALE
780 res
= wxXmlResource('', xmlFlags
)
781 res
.Load('memory:xxx.xrc')
783 if xxx
.__class
__ == xxxFrame
:
784 # Frame can't have many children,
785 # but it's first child possibly can...
786 # child = self.GetFirstChild(item)[0]
787 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
788 # # Clean-up before recursive call or error
789 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
791 # self.CreateTestWin(child)
793 # This currently works under GTK, but not under MSW
794 testWin
= g
.testWin
= wxPreFrame()
795 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
797 testWin
.panel
= testWin
798 testWin
.CreateStatusBar()
799 testWin
.SetClientSize(testWin
.GetBestSize())
800 testWin
.SetPosition(pos
)
802 elif xxx
.__class
__ == xxxPanel
:
805 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Panel: ' + name
,
806 pos
=pos
, name
=STD_NAME
)
807 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
808 testWin
.SetClientSize(testWin
.GetBestSize())
810 elif xxx
.__class
__ == xxxDialog
:
811 testWin
= g
.testWin
= res
.LoadDialog(None, STD_NAME
)
812 testWin
.panel
= testWin
814 testWin
.SetPosition(pos
)
816 # Dialog's default code does not produce EVT_CLOSE
817 EVT_BUTTON(testWin
, wxID_OK
, self
.OnCloseTestWin
)
818 EVT_BUTTON(testWin
, wxID_CANCEL
, self
.OnCloseTestWin
)
819 elif xxx
.__class
__ == xxxWizard
:
821 res
.LoadOnObject(wiz
, None, STD_NAME
, 'wxWizard')
822 # Find first page (don't know better way)
824 for w
in wiz
.GetChildren():
825 if isinstance(w
, wxWizardPage
):
829 wxLogError('Wizard is empty')
831 # Wizard should be modal
832 self
.SetItemBold(item
)
834 self
.SetItemBold(item
, False)
836 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
839 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'Wizard page: ' + name
,
840 pos
=pos
, name
=STD_NAME
)
841 testWin
.panel
= wxPrePanel()
842 res
.LoadOnObject(testWin
.panel
, testWin
, STD_NAME
, 'wxPanel')
843 testWin
.SetClientSize(testWin
.GetBestSize())
845 elif xxx
.__class
__ == xxxMenuBar
:
846 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'MenuBar: ' + name
,
847 pos
=pos
, name
=STD_NAME
)
849 # Set status bar to display help
850 testWin
.CreateStatusBar()
851 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
852 testWin
.SetMenuBar(testWin
.menuBar
)
854 elif xxx
.__class
__ == xxxToolBar
:
855 testWin
= g
.testWin
= wxFrame(g
.frame
, -1, 'ToolBar: ' + name
,
856 pos
=pos
, name
=STD_NAME
)
858 # Set status bar to display help
859 testWin
.CreateStatusBar()
860 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
861 testWin
.SetToolBar(testWin
.toolBar
)
865 EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
866 testWin
.highLight
= None
867 if highLight
and not self
.pendingHighLight
:
868 self
.HighLight(highLight
)
871 self
.SetItemBold(item
, False)
872 g
.testWinPos
= g
.testWin
.GetPosition()
876 wxLogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
877 wxLogError('Error loading resource')
878 wxMemoryFSHandler_RemoveFile('xxx.xrc')
881 def CloseTestWindow(self
):
882 if not g
.testWin
: return
883 self
.SetItemBold(g
.testWin
.item
, False)
884 g
.frame
.tb
.ToggleTool(g
.frame
.ID_TOOL_LOCATE
, False)
885 g
.testWinPos
= g
.testWin
.GetPosition()
889 def OnCloseTestWin(self
, evt
):
890 self
.CloseTestWindow()
892 # Return item index in parent
893 def ItemIndex(self
, item
):
894 n
= 0 # index of sibling
895 prev
= self
.GetPrevSibling(item
)
897 prev
= self
.GetPrevSibling(prev
)
901 # Full tree index of an item - list of positions
902 def ItemFullIndex(self
, item
):
903 if not item
.IsOk(): return None
905 while item
!= self
.root
:
906 l
.insert(0, self
.ItemIndex(item
))
907 item
= self
.GetItemParent(item
)
909 # Get item position from full index
910 def ItemAtFullIndex(self
, index
):
911 if index
is None: return wxTreeItemId()
914 item
= self
.GetFirstChild(item
)[0]
915 for k
in range(i
): item
= self
.GetNextSibling(item
)
918 # True if next item should be inserted after current (vs. appended to it)
919 def NeedInsert(self
, item
):
920 xxx
= self
.GetPyData(item
)
921 if item
== self
.root
: return False # root item
922 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
924 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
927 def OnRightDown(self
, evt
):
928 pullDownMenu
= g
.pullDownMenu
930 pt
= evt
.GetPosition();
931 item
, flags
= self
.HitTest(pt
)
932 if item
.Ok() and flags
& wxTREE_HITTEST_ONITEM
:
933 self
.SelectItem(item
)
938 item
= self
.selection
940 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
941 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
943 # self.ctrl = evt.ControlDown() # save Ctrl state
944 # self.shift = evt.ShiftDown() # and Shift too
945 m
= wxMenu() # create menu
949 needInsert
= self
.NeedInsert(item
)
950 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
951 SetMenu(m
, pullDownMenu
.topLevel
)
953 xxx
= self
.GetPyData(item
).treeObject()
954 # Check parent for possible child nodes if inserting sibling
955 if needInsert
: xxx
= xxx
.parent
956 if xxx
.__class
__ == xxxMenuBar
:
957 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
958 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
959 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
960 SetMenu(m
, pullDownMenu
.toolBarControls
)
961 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
962 SetMenu(m
, pullDownMenu
.menuControls
)
964 SetMenu(m
, pullDownMenu
.controls
)
965 if xxx
.__class
__ == xxxNotebook
:
966 m
.Enable(m
.FindItem('sizer'), False)
967 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
968 m
.Enable(ID_NEW
.SPACER
, False)
969 # Select correct label for create menu
972 menu
.AppendMenu(wxNewId(), 'Insert Child', m
,
973 'Create child object as the first child')
975 menu
.AppendMenu(wxNewId(), 'Append Child', m
,
976 'Create child object as the last child')
979 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
980 'Create sibling before selected object')
982 menu
.AppendMenu(wxNewId(), 'Create Sibling', m
,
983 'Create sibling after selected object')
985 if item
!= self
.root
:
986 xxx
= self
.GetPyData(item
).treeObject()
987 m
= wxMenu() # create replace menu
988 if xxx
.__class
__ == xxxMenuBar
:
989 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
990 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
991 SetMenu2(m
, pullDownMenu
.menuControls
)
992 elif xxx
.__class
__ == xxxToolBar
and \
993 self
.GetItemParent(item
) == self
.root
:
995 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
997 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
998 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
999 (ID_NEW
.FRAME
, 'Frame', 'Create frame')])
1001 SetMenu2(m
, pullDownMenu
.sizers
)
1003 SetMenu2(m
, pullDownMenu
.controls
)
1005 menu
.AppendMenu(id, 'Replace With', m
)
1006 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
1007 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
1008 'Set subclass property')
1009 menu
.AppendSeparator()
1010 # Not using standart IDs because we don't want to show shortcuts
1011 menu
.Append(wxID_CUT
, 'Cut', 'Cut to the clipboard')
1012 menu
.Append(wxID_COPY
, 'Copy', 'Copy to the clipboard')
1013 if self
.ctrl
and item
!= self
.root
:
1014 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
1015 'Paste from the clipboard as a sibling')
1017 menu
.Append(wxID_PASTE
, 'Paste', 'Paste from the clipboard')
1018 menu
.Append(pullDownMenu
.ID_DELETE
,
1019 'Delete', 'Delete object')
1020 if self
.ItemHasChildren(item
):
1021 menu
.AppendSeparator()
1022 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
1023 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
1024 self
.PopupMenu(menu
, evt
.GetPosition())
1028 def Apply(self
, xxx
, item
):
1031 xxx
= xxx
.treeObject()
1032 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
1033 self
.SetItemText(item
, xxx
.treeName())
1034 # Item width may have changed
1035 # !!! Tric to update tree width (wxGTK, ??)
1036 self
.SetIndent(self
.GetIndent())
1037 # Change tree icon for sizers
1038 if isinstance(xxx
, xxxBoxSizer
):
1039 self
.SetItemImage(item
, xxx
.treeImage())
1040 # Set global modified state
1041 g
.frame
.modified
= True