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
= wx
.GetDefaultPyEncoding()
27 self
.buffer += data
.encode(encoding
)
28 except UnicodeEncodeError:
29 self
.buffer += data
.encode(encoding
, 'xmlcharrefreplace')
32 wx
.MemoryFSHandler
.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
59 STATUS_BAR
= wx
.NewId()
61 STATIC_TEXT
= wx
.NewId()
62 TEXT_CTRL
= wx
.NewId()
65 BITMAP_BUTTON
= wx
.NewId()
66 RADIO_BUTTON
= wx
.NewId()
67 SPIN_BUTTON
= wx
.NewId()
68 TOGGLE_BUTTON
= wx
.NewId()
70 STATIC_BOX
= wx
.NewId()
71 CHECK_BOX
= wx
.NewId()
72 RADIO_BOX
= wx
.NewId()
73 COMBO_BOX
= wx
.NewId()
76 STATIC_LINE
= wx
.NewId()
77 STATIC_BITMAP
= wx
.NewId()
81 SCROLL_BAR
= wx
.NewId()
82 TREE_CTRL
= wx
.NewId()
83 LIST_CTRL
= wx
.NewId()
84 CHECK_LIST
= wx
.NewId()
86 CHOICEBOOK
= wx
.NewId()
88 SPLITTER_WINDOW
= wx
.NewId()
90 SCROLLED_WINDOW
= wx
.NewId()
91 HTML_WINDOW
= wx
.NewId()
92 CALENDAR_CTRL
= wx
.NewId()
93 DATE_CTRL
= wx
.NewId()
94 FILE_PICKER_CTRL
= wx
.NewId()
95 GENERIC_DIR_CTRL
= wx
.NewId()
96 SPIN_CTRL
= wx
.NewId()
99 WIZARD_PAGE
= wx
.NewId()
100 WIZARD_PAGE_SIMPLE
= wx
.NewId()
103 STATUS_BAR
= wx
.NewId()
105 BOX_SIZER
= wx
.NewId()
106 STATIC_BOX_SIZER
= wx
.NewId()
107 GRID_SIZER
= wx
.NewId()
108 FLEX_GRID_SIZER
= wx
.NewId()
109 GRID_BAG_SIZER
= wx
.NewId()
110 STD_DIALOG_BUTTON_SIZER
= wx
.NewId()
113 TOOL_BAR
= wx
.NewId()
116 MENU_ITEM
= wx
.NewId()
117 SEPARATOR
= wx
.NewId()
119 OK_BUTTON
= wx
.NewId()
120 YES_BUTTON
= wx
.NewId()
121 SAVE_BUTTON
= wx
.NewId()
122 APPLY_BUTTON
= wx
.NewId()
123 NO_BUTTON
= wx
.NewId()
124 CANCEL_BUTTON
= wx
.NewId()
125 HELP_BUTTON
= wx
.NewId()
126 CONTEXT_HELP_BUTTON
= wx
.NewId()
132 for i
in range(99): wx
.NewId() # reserve IDs for custom controls
139 ID_EXPAND
= wx
.NewId()
140 ID_COLLAPSE
= wx
.NewId()
141 ID_PASTE_SIBLING
= wx
.NewId()
142 ID_TOOL_PASTE
= wx
.NewId()
143 ID_SUBCLASS
= wx
.NewId()
145 def __init__(self
, parent
):
146 self
.ID_DELETE
= parent
.ID_DELETE
147 wx
.EVT_MENU_RANGE(parent
, ID_NEW
.PANEL
, ID_NEW
.LAST
, parent
.OnCreate
)
148 wx
.EVT_MENU_RANGE(parent
, 1000 + ID_NEW
.PANEL
, 1000 + ID_NEW
.LAST
, parent
.OnReplace
)
149 wx
.EVT_MENU(parent
, self
.ID_COLLAPSE
, parent
.OnCollapse
)
150 wx
.EVT_MENU(parent
, self
.ID_EXPAND
, parent
.OnExpand
)
151 wx
.EVT_MENU(parent
, self
.ID_PASTE_SIBLING
, parent
.OnPaste
)
152 wx
.EVT_MENU(parent
, self
.ID_SUBCLASS
, parent
.OnSubclass
)
153 # We connect to tree, but process in frame
154 wx
.EVT_MENU_HIGHLIGHT_ALL(g
.tree
, parent
.OnPullDownHighlight
)
156 # Mapping from IDs to element names
158 ID_NEW
.PANEL
: 'wxPanel',
159 ID_NEW
.DIALOG
: 'wxDialog',
160 ID_NEW
.FRAME
: 'wxFrame',
161 ID_NEW
.WIZARD
: 'wxWizard',
162 ID_NEW
.WIZARD_PAGE
: 'wxWizardPage',
163 ID_NEW
.WIZARD_PAGE_SIMPLE
: 'wxWizardPageSimple',
164 ID_NEW
.TOOL_BAR
: 'wxToolBar',
166 ID_NEW
.STATUS_BAR
: 'wxStatusBar',
167 ID_NEW
.MENU_BAR
: 'wxMenuBar',
168 ID_NEW
.MENU
: 'wxMenu',
169 ID_NEW
.MENU_ITEM
: 'wxMenuItem',
170 ID_NEW
.BITMAP
: 'wxBitmap',
171 ID_NEW
.ICON
: 'wxIcon',
172 ID_NEW
.SEPARATOR
: 'separator',
174 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
175 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
177 ID_NEW
.BUTTON
: 'wxButton',
178 ID_NEW
.BITMAP_BUTTON
: 'wxBitmapButton',
179 ID_NEW
.RADIO_BUTTON
: 'wxRadioButton',
180 ID_NEW
.SPIN_BUTTON
: 'wxSpinButton',
181 ID_NEW
.TOGGLE_BUTTON
: 'wxToggleButton',
183 ID_NEW
.STATIC_BOX
: 'wxStaticBox',
184 ID_NEW
.CHECK_BOX
: 'wxCheckBox',
185 ID_NEW
.RADIO_BOX
: 'wxRadioBox',
186 ID_NEW
.COMBO_BOX
: 'wxComboBox',
187 ID_NEW
.LIST_BOX
: 'wxListBox',
188 ID_NEW
.CHECK_LIST
: 'wxCheckListBox',
190 ID_NEW
.STATIC_LINE
: 'wxStaticLine',
191 ID_NEW
.STATIC_BITMAP
: 'wxStaticBitmap',
192 ID_NEW
.CHOICE
: 'wxChoice',
193 ID_NEW
.SLIDER
: 'wxSlider',
194 ID_NEW
.GAUGE
: 'wxGauge',
195 ID_NEW
.SCROLL_BAR
: 'wxScrollBar',
196 ID_NEW
.TREE_CTRL
: 'wxTreeCtrl',
197 ID_NEW
.LIST_CTRL
: 'wxListCtrl',
198 ID_NEW
.NOTEBOOK
: 'wxNotebook',
199 ID_NEW
.CHOICEBOOK
: 'wxChoicebook',
200 ID_NEW
.LISTBOOK
: 'wxListbook',
201 ID_NEW
.SPLITTER_WINDOW
: 'wxSplitterWindow',
202 ID_NEW
.GRID
: 'wxGrid',
203 ID_NEW
.SCROLLED_WINDOW
: 'wxScrolledWindow',
204 ID_NEW
.HTML_WINDOW
: 'wxHtmlWindow',
205 ID_NEW
.CALENDAR_CTRL
: 'wxCalendarCtrl',
206 ID_NEW
.DATE_CTRL
: 'wxDatePickerCtrl',
207 ID_NEW
.FILE_PICKER_CTRL
: 'wxFilePickerCtrl',
208 ID_NEW
.GENERIC_DIR_CTRL
: 'wxGenericDirCtrl',
209 ID_NEW
.SPIN_CTRL
: 'wxSpinCtrl',
211 ID_NEW
.BOX_SIZER
: 'wxBoxSizer',
212 ID_NEW
.STATIC_BOX_SIZER
: 'wxStaticBoxSizer',
213 ID_NEW
.GRID_SIZER
: 'wxGridSizer',
214 ID_NEW
.FLEX_GRID_SIZER
: 'wxFlexGridSizer',
215 ID_NEW
.GRID_BAG_SIZER
: 'wxGridBagSizer',
216 ID_NEW
.STD_DIALOG_BUTTON_SIZER
: 'wxStdDialogButtonSizer',
217 ID_NEW
.SPACER
: 'spacer',
218 ID_NEW
.UNKNOWN
: 'unknown',
220 ID_NEW
.OK_BUTTON
: 'wxButton',
221 ID_NEW
.YES_BUTTON
: 'wxButton',
222 ID_NEW
.SAVE_BUTTON
: 'wxButton',
223 ID_NEW
.APPLY_BUTTON
: 'wxButton',
224 ID_NEW
.NO_BUTTON
: 'wxButton',
225 ID_NEW
.CANCEL_BUTTON
: 'wxButton',
226 ID_NEW
.HELP_BUTTON
: 'wxButton',
227 ID_NEW
.CONTEXT_HELP_BUTTON
: 'wxButton',
230 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
231 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
232 (ID_NEW
.FRAME
, 'Frame', 'Create frame'),
233 (ID_NEW
.WIZARD
, 'Wizard', 'Create wizard'),
235 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
236 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
237 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
239 (ID_NEW
.BITMAP
, 'Bitmap', 'Create bitmap'),
240 (ID_NEW
.ICON
, 'Icon', 'Create icon'),
243 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
244 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
245 (ID_NEW
.CHOICEBOOK
, 'Choicebook', 'Create choicebook control'),
246 (ID_NEW
.LISTBOOK
, 'Listbook', 'Create listbook control'),
247 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
248 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
249 (ID_NEW
.STATUS_BAR
, 'StatusBar', 'Create status bar'),
250 # (ID_NEW.WIZARD_PAGE, 'WizardPage', 'Create wizard page'),
251 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
254 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
255 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
256 'Create static box sizer'),
257 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
258 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
259 'Create flexgrid sizer'),
260 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
261 'Create gridbag sizer'),
262 # (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
263 # 'Create standard button sizer'),
264 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
267 ['control', 'Various controls',
268 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
269 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
270 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
271 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
272 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
273 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
274 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
275 (ID_NEW
.SPIN_CTRL
, 'SpinCtrl', 'Create spin'),
276 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
277 (ID_NEW
.TREE_CTRL
, 'TreeCtrl', 'Create tree'),
278 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list'),
279 # (ID_NEW.GRID, 'Grid', 'Create grid'),
280 (ID_NEW
.SCROLLED_WINDOW
, 'ScrolledWindow', 'Create scrolled window'),
281 (ID_NEW
.HTML_WINDOW
, 'HtmlWindow', 'Create HTML window'),
282 (ID_NEW
.CALENDAR_CTRL
, 'CalendarCtrl', 'Create calendar control'),
283 (ID_NEW
.DATE_CTRL
, 'DatePickerCtrl', 'Create date picker control'),
284 # (ID_NEW.FILE_PICKER_CTRL, 'FilePickerCtrl', 'Create file picker control'),
285 (ID_NEW
.GENERIC_DIR_CTRL
, 'GenericDirCtrl', 'Create generic dir control'),
286 (ID_NEW
.UNKNOWN
, 'Unknown', 'Create custom control placeholder'),
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'),
293 (ID_NEW
.TOGGLE_BUTTON
, 'ToggleButton', 'Create toggle button'),
296 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
297 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
298 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
299 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
300 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
301 (ID_NEW
.CHECK_LIST
, 'CheckListBox', 'Create checklist box'),
303 ['container', 'Containers',
304 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
305 (ID_NEW
.NOTEBOOK
, 'Notebook', 'Create notebook control'),
306 (ID_NEW
.CHOICEBOOK
, 'Choicebook', 'Create choicebook control'),
307 (ID_NEW
.LISTBOOK
, 'Listbook', 'Create listbook control'),
308 (ID_NEW
.SPLITTER_WINDOW
, 'SplitterWindow', 'Create splitter window'),
309 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
310 (ID_NEW
.STATUS_BAR
, 'StatusBar', 'Create status bar'),
311 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
312 # (ID_NEW.WIZARD_PAGE, 'Wizard Page', 'Create wizard page'),
313 (ID_NEW
.WIZARD_PAGE_SIMPLE
, 'WizardPageSimple', 'Create simple wizard page'),
316 (ID_NEW
.BOX_SIZER
, 'BoxSizer', 'Create box sizer'),
317 (ID_NEW
.STATIC_BOX_SIZER
, 'StaticBoxSizer',
318 'Create static box sizer'),
319 (ID_NEW
.GRID_SIZER
, 'GridSizer', 'Create grid sizer'),
320 (ID_NEW
.FLEX_GRID_SIZER
, 'FlexGridSizer',
321 'Create flexgrid sizer'),
322 (ID_NEW
.GRID_BAG_SIZER
, 'GridBagSizer',
323 'Create gridbag sizer'),
324 (ID_NEW
.SPACER
, 'Spacer', 'Create spacer'),
325 (ID_NEW
.STD_DIALOG_BUTTON_SIZER
, 'StdDialogButtonSizer',
326 'Create standard button sizer'),
329 self
.menuControls
= [
330 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
331 (ID_NEW
.MENU_ITEM
, 'MenuItem', 'Create menu item'),
332 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
334 self
.toolBarControls
= [
335 (ID_NEW
.TOOL
, 'Tool', 'Create tool'),
336 (ID_NEW
.SEPARATOR
, 'Separator', 'Create separator'),
337 ['control', 'Various controls',
338 (ID_NEW
.STATIC_TEXT
, 'Label', 'Create label'),
339 (ID_NEW
.STATIC_BITMAP
, 'Bitmap', 'Create bitmap'),
340 (ID_NEW
.STATIC_LINE
, 'Line', 'Create line'),
341 (ID_NEW
.TEXT_CTRL
, 'TextBox', 'Create text box'),
342 (ID_NEW
.CHOICE
, 'Choice', 'Create choice'),
343 (ID_NEW
.SLIDER
, 'Slider', 'Create slider'),
344 (ID_NEW
.GAUGE
, 'Gauge', 'Create gauge'),
345 (ID_NEW
.SCROLL_BAR
, 'ScrollBar', 'Create scroll bar'),
346 (ID_NEW
.LIST_CTRL
, 'ListCtrl', 'Create list control'),
348 ['button', 'Buttons',
349 (ID_NEW
.BUTTON
, 'Button', 'Create button'),
350 (ID_NEW
.BITMAP_BUTTON
, 'BitmapButton', 'Create bitmap button'),
351 (ID_NEW
.RADIO_BUTTON
, 'RadioButton', 'Create radio button'),
352 (ID_NEW
.SPIN_BUTTON
, 'SpinButton', 'Create spin button'),
355 (ID_NEW
.STATIC_BOX
, 'StaticBox', 'Create static box'),
356 (ID_NEW
.CHECK_BOX
, 'CheckBox', 'Create check box'),
357 (ID_NEW
.RADIO_BOX
, 'RadioBox', 'Create radio box'),
358 (ID_NEW
.COMBO_BOX
, 'ComboBox', 'Create combo box'),
359 (ID_NEW
.LIST_BOX
, 'ListBox', 'Create list box'),
360 (ID_NEW
.CHECK_LIST
, 'CheckListBox', 'Create checklist box'),
364 (ID_NEW
.OK_BUTTON
, 'OK Button', 'Create standard button'),
365 (ID_NEW
.YES_BUTTON
, 'YES Button', 'Create standard button'),
366 (ID_NEW
.SAVE_BUTTON
, 'SAVE Button', 'Create standard button'),
367 (ID_NEW
.APPLY_BUTTON
, 'APPLY Button', 'Create standard button'),
368 (ID_NEW
.NO_BUTTON
, 'NO Button', 'Create standard button'),
369 (ID_NEW
.CANCEL_BUTTON
, 'CANCEL Button', 'Create standard button'),
370 (ID_NEW
.HELP_BUTTON
, 'HELP Button', 'Create standard button'),
371 (ID_NEW
.CONTEXT_HELP_BUTTON
, 'CONTEXT HELP Button', 'Create standard button'),
373 self
.stdButtonIDs
= {
374 ID_NEW
.OK_BUTTON
: ('wxID_OK', '&Ok'),
375 ID_NEW
.YES_BUTTON
: ('wxID_YES', '&Yes'),
376 ID_NEW
.SAVE_BUTTON
: ('wxID_SAVE', '&Save'),
377 ID_NEW
.APPLY_BUTTON
: ('wxID_APPLY', '&Apply'),
378 ID_NEW
.NO_BUTTON
: ('wxID_NO', '&No'),
379 ID_NEW
.CANCEL_BUTTON
: ('wxID_CANCEL', '&Cancel'),
380 ID_NEW
.HELP_BUTTON
: ('wxID_HELP', '&Help'),
381 ID_NEW
.CONTEXT_HELP_BUTTON
: ('wxID_CONTEXT_HELP', '&Help'),
383 self
.custom
= ['custom', 'User-defined controls']
386 def addCustom(self
, klass
):
387 n
= len(self
.custom
)-2
388 self
.custom
.append((ID_NEW
.CUSTOM
+ n
, klass
))
389 self
.customMap
[ID_NEW
.CUSTOM
+ n
] = klass
392 ################################################################################
394 # Set menu to list items.
395 # Each menu command is a tuple (id, label, help)
396 # submenus are lists [id, label, help, submenu]
397 # and separators are any other type. Shift is for making
398 # alternative sets of IDs. (+1000).
399 def SetMenu(m
, list, shift
=False):
401 if type(l
) == types
.TupleType
:
403 if shift
: l
= (1000 + l
[0],) + l
[1:]
405 elif type(l
) == types
.ListType
:
407 SetMenu(subMenu
, l
[2:], shift
)
408 m
.AppendMenu(wx
.NewId(), l
[0], subMenu
, l
[1])
412 ################################################################################
415 def __init__(self
, pos
, size
):
416 if size
.width
== -1: size
.width
= 0
417 if size
.height
== -1: size
.height
= 0
419 l1
= wx
.Window(w
, -1, pos
, wx
.Size(size
.width
, 2))
420 l1
.SetBackgroundColour(wx
.RED
)
421 l2
= wx
.Window(w
, -1, pos
, wx
.Size(2, size
.height
))
422 l2
.SetBackgroundColour(wx
.RED
)
423 l3
= wx
.Window(w
, -1, wx
.Point(pos
.x
+ size
.width
- 2, pos
.y
), wx
.Size(2, size
.height
))
424 l3
.SetBackgroundColour(wx
.RED
)
425 l4
= wx
.Window(w
, -1, wx
.Point(pos
.x
, pos
.y
+ size
.height
- 2), wx
.Size(size
.width
, 2))
426 l4
.SetBackgroundColour(wx
.RED
)
427 self
.lines
= [l1
, l2
, l3
, l4
]
429 # Move highlight to a new position
430 def Replace(self
, pos
, size
):
431 if size
.width
== -1: size
.width
= 0
432 if size
.height
== -1: size
.height
= 0
433 self
.lines
[0].SetDimensions(pos
.x
, pos
.y
, size
.width
, 2)
434 self
.lines
[1].SetDimensions(pos
.x
, pos
.y
, 2, size
.height
)
435 self
.lines
[2].SetDimensions(pos
.x
+ size
.width
- 2, pos
.y
, 2, size
.height
)
436 self
.lines
[3].SetDimensions(pos
.x
, pos
.y
+ size
.height
- 2, size
.width
, 2)
440 map(wx
.Window
.Destroy
, self
.lines
)
441 g
.testWin
.highLight
= None
443 map(wx
.Window
.Refresh
, self
.lines
)
445 ################################################################################
447 class XML_Tree(wx
.TreeCtrl
):
448 def __init__(self
, parent
, id):
449 wx
.TreeCtrl
.__init
__(self
, parent
, id,
450 style
= wx
.TR_HAS_BUTTONS | wx
.TR_MULTIPLE | wx
.TR_EDIT_LABELS
)
451 self
.SetBackgroundColour(wx
.Colour(224, 248, 224))
452 self
.fontComment
= wx
.FFont(self
.GetFont().GetPointSize(),
453 self
.GetFont().GetFamily(),
456 wx
.EVT_TREE_SEL_CHANGED(self
, self
.GetId(), self
.OnSelChanged
)
457 # One works on Linux, another on Windows
458 if wx
.Platform
== '__WXGTK__':
459 wx
.EVT_TREE_ITEM_ACTIVATED(self
, self
.GetId(), self
.OnItemActivated
)
461 wx
.EVT_LEFT_DCLICK(self
, self
.OnDClick
)
462 wx
.EVT_RIGHT_DOWN(self
, self
.OnRightDown
)
463 wx
.EVT_TREE_ITEM_EXPANDED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
464 wx
.EVT_TREE_ITEM_COLLAPSED(self
, self
.GetId(), self
.OnItemExpandedCollapsed
)
465 self
.Bind(wx
.EVT_TREE_BEGIN_LABEL_EDIT
, self
.OnBeginLabelEdit
)
466 self
.Bind(wx
.EVT_TREE_END_LABEL_EDIT
, self
.OnEndLabelEdit
)
468 self
.selection
= None
469 self
.selectionChanging
= False
470 self
.needUpdate
= False
471 self
.pendingHighLight
= None
472 self
.ctrl
= self
.shift
= False
475 il
= wx
.ImageList(16, 16, True)
476 self
.rootImage
= il
.Add(images
.getTreeRootImage().Scale(16,16).ConvertToBitmap())
477 xxxComment
.image
= il
.Add(images
.getTreeCommentImage().Scale(16,16).ConvertToBitmap())
478 xxxObject
.image
= il
.Add(images
.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
479 xxxPanel
.image
= il
.Add(images
.getTreePanelImage().Scale(16,16).ConvertToBitmap())
480 xxxDialog
.image
= il
.Add(images
.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
481 xxxFrame
.image
= il
.Add(images
.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
482 xxxMenuBar
.image
= il
.Add(images
.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
483 xxxMenu
.image
= il
.Add(images
.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
484 xxxMenuItem
.image
= il
.Add(images
.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
485 xxxToolBar
.image
= il
.Add(images
.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
486 xxxTool
.image
= il
.Add(images
.getTreeToolImage().Scale(16,16).ConvertToBitmap())
487 xxxSeparator
.image
= il
.Add(images
.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
488 xxxSizer
.imageH
= il
.Add(images
.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
489 xxxSizer
.imageV
= il
.Add(images
.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
490 xxxStaticBoxSizer
.imageH
= il
.Add(images
.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
491 xxxStaticBoxSizer
.imageV
= il
.Add(images
.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
492 xxxGridSizer
.image
= il
.Add(images
.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
493 xxxFlexGridSizer
.image
= il
.Add(images
.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
495 self
.SetImageList(il
)
497 def RegisterKeyEvents(self
):
498 wx
.EVT_KEY_DOWN(self
, g
.tools
.OnKeyDown
)
499 wx
.EVT_KEY_UP(self
, g
.tools
.OnKeyUp
)
500 wx
.EVT_ENTER_WINDOW(self
, g
.tools
.OnMouse
)
501 wx
.EVT_LEAVE_WINDOW(self
, g
.tools
.OnMouse
)
503 def ExpandAll(self
, item
):
504 if self
.ItemHasChildren(item
):
506 i
, cookie
= self
.GetFirstChild(item
)
510 i
, cookie
= self
.GetNextChild(item
, cookie
)
513 def CollapseAll(self
, item
):
514 if self
.ItemHasChildren(item
):
515 i
, cookie
= self
.GetFirstChild(item
)
519 i
, cookie
= self
.GetNextChild(item
, cookie
)
526 self
.selection
= None
528 self
.DeleteAllItems()
529 # Add minimal structure
530 if self
.dom
: self
.dom
.unlink()
531 self
.dom
= MyDocument()
532 self
.dummyNode
= self
.dom
.createComment('dummy node')
534 self
.mainNode
= self
.dom
.createElement('resource')
535 self
.dom
.appendChild(self
.mainNode
)
536 self
.rootObj
= xxxMainNode(self
.dom
)
537 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
538 data
=wx
.TreeItemData(self
.rootObj
))
539 self
.SetItemHasChildren(self
.root
)
540 self
.testElem
= self
.dom
.createElement('dummy')
541 self
.mainNode
.appendChild(self
.testElem
)
542 self
.Expand(self
.root
)
544 # Clear old data and set new
545 def SetData(self
, dom
):
546 self
.selection
= None
548 self
.DeleteAllItems()
549 # Add minimal structure
550 if self
.dom
: self
.dom
.unlink()
552 self
.dummyNode
= self
.dom
.createComment('dummy node')
553 # Find 'resource' child, add it's children
554 self
.mainNode
= dom
.documentElement
555 self
.rootObj
= xxxMainNode(self
.dom
)
556 self
.root
= self
.AddRoot('XML tree', self
.rootImage
,
557 data
=wx
.TreeItemData(self
.rootObj
))
558 self
.SetItemHasChildren(self
.root
)
559 nodes
= self
.mainNode
.childNodes
[:]
562 self
.AddNode(self
.root
, None, node
)
564 self
.mainNode
.removeChild(node
)
566 if self
.mainNode
.firstChild
:
567 self
.testElem
= self
.dom
.createElement('dummy')
568 self
.mainNode
.insertBefore(self
.testElem
, self
.mainNode
.firstChild
)
570 self
.testElem
= self
.dom
.createElement('dummy')
571 self
.mainNode
.appendChild(self
.testElem
)
572 self
.Expand(self
.root
)
574 # Add tree item for given parent item if node is DOM element node with
575 # object/object_ref tag. xxxParent is parent xxx object
576 def AddNode(self
, itemParent
, xxxParent
, node
):
577 # Set item data to current node
579 xxx
= MakeXXXFromDOM(xxxParent
, node
)
581 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
583 treeObj
= xxx
.treeObject()
585 item
= self
.AppendItem(itemParent
, treeObj
.treeName(),
586 image
=treeObj
.treeImage(),
587 data
=wx
.TreeItemData(xxx
))
588 # Different color for comments and references
589 if xxx
.className
== 'comment':
590 self
.SetItemTextColour(item
, 'Blue')
591 self
.SetItemFont(item
, self
.fontComment
)
593 self
.SetItemTextColour(item
, 'DarkGreen')
594 elif treeObj
.hasStyle
and treeObj
.params
.get('hidden', False):
595 self
.SetItemTextColour(item
, 'Grey')
596 # Try to find children objects
597 if treeObj
.hasChildren
:
598 nodes
= treeObj
.node
.childNodes
[:]
601 self
.AddNode(item
, treeObj
, n
)
602 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
603 treeObj
.node
.removeChild(n
)
606 # Insert new item at specific position
607 def InsertNode(self
, itemParent
, parent
, elem
, nextItem
):
608 # Insert in XML tree and wxWin
609 xxx
= MakeXXXFromDOM(parent
, elem
)
610 # If nextItem is None, we append to parent, otherwise insert before it
612 node
= self
.GetPyData(nextItem
).node
613 parent
.node
.insertBefore(elem
, node
)
614 # Inserting before is difficult, se we insert after or first child
615 index
= self
.ItemIndex(nextItem
)
616 newItem
= self
.InsertItemBefore(itemParent
, index
,
617 xxx
.treeName(), image
=xxx
.treeImage())
618 self
.SetPyData(newItem
, xxx
)
620 parent
.node
.appendChild(elem
)
621 newItem
= self
.AppendItem(itemParent
, xxx
.treeName(), image
=xxx
.treeImage(),
622 data
=wx
.TreeItemData(xxx
))
623 treeObj
= xxx
.treeObject()
624 # Different color for references and comments
625 if xxx
.className
== 'comment':
626 self
.SetItemTextColour(newItem
, 'Blue')
627 self
.SetItemFont(newItem
, self
.fontComment
)
629 self
.SetItemTextColour(newItem
, 'DarkGreen')
630 elif treeObj
.hasStyle
and treeObj
.params
.get('hidden', False):
631 self
.SetItemTextColour(newItem
, 'Grey')
634 treeObj
= xxx
.treeObject()
635 for n
in treeObj
.node
.childNodes
:
637 self
.AddNode(newItem
, treeObj
, n
)
640 # Remove leaf of tree, return it's data object
641 def RemoveLeaf(self
, leaf
):
642 xxx
= self
.GetPyData(leaf
)
644 parent
= node
.parentNode
645 parent
.removeChild(node
)
647 # Reset selection object
648 self
.selection
= None
651 # Find position relative to the top-level window
652 def FindNodePos(self
, item
, obj
=None):
654 if item
== g
.testWin
.item
: return wx
.Point(0, 0)
655 itemParent
= self
.GetItemParent(item
)
657 if not obj
: obj
= self
.FindNodeObject(item
)
658 if self
.GetPyData(itemParent
).treeObject().__class
__ in \
659 [xxxNotebook
, xxxChoicebook
, xxxListbook
]:
660 book
= self
.FindNodeObject(itemParent
)
662 for i
in range(book
.GetPageCount()):
663 if book
.GetPage(i
) == obj
:
664 if book
.GetSelection() != i
:
666 # Remove highlight - otherwise highlight window won't be visible
667 if g
.testWin
.highLight
:
668 g
.testWin
.highLight
.Remove()
670 # For sizers and notebooks we must select the first window-like parent
671 winParent
= itemParent
672 while self
.GetPyData(winParent
).isSizer
:
673 winParent
= self
.GetItemParent(winParent
)
674 # Notebook children are layed out in a little strange way
675 # wxGTK places NB panels relative to the NB parent
676 if wx
.Platform
== '__WXGTK__':
677 if self
.GetPyData(itemParent
).treeObject().__class
__ == xxxNotebook
:
678 winParent
= self
.GetItemParent(winParent
)
679 parentPos
= self
.FindNodePos(winParent
)
680 pos
= obj
.GetPosition()
681 # Position (-1,-1) is really (0,0)
682 if pos
== (-1,-1): pos
= (0,0)
683 return parentPos
+ pos
685 # Find window (or sizer) corresponding to a tree item.
686 def FindNodeObject(self
, item
):
688 # If top-level, return testWin (or panel its panel)
689 if item
== testWin
.item
: return testWin
.panel
690 itemParent
= self
.GetItemParent(item
)
691 xxx
= self
.GetPyData(item
).treeObject()
692 parentWin
= self
.FindNodeObject(itemParent
)
693 # Top-level sizer? return window's sizer
694 if xxx
.isSizer
and isinstance(parentWin
, wx
.Window
):
695 return parentWin
.GetSizer()
696 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
, xxxSeparator
]: return None
697 elif xxx
.__class
__ in [xxxToolBar
, xxxMenuBar
]:
698 # If it's the main toolbar or menubar, we can't really select it
699 if xxx
.parent
.__class
__ == xxxFrame
: return None
700 elif isinstance(xxx
.parent
, xxxToolBar
):
701 # Select complete toolbar
703 elif isinstance(xxx
.parent
, xxxStdDialogButtonSizer
):
704 # This sizer returns non-existing children
705 for ch
in parentWin
.GetChildren():
706 if ch
.GetWindow() and ch
.GetWindow().GetName() == xxx
.name
:
707 return ch
.GetWindow()
709 elif xxx
.parent
.__class
__ in [xxxChoicebook
, xxxListbook
]:
710 # First window is controld
711 return parentWin
.GetChildren()[self
.ItemIndex(item
)+1]
712 # Otherwise get parent's object and it's child
713 child
= parentWin
.GetChildren()[self
.WindowIndex(item
)]
714 # Return window or sizer for sizer items
715 if child
.GetClassName() == 'wxSizerItem':
716 if child
.IsWindow(): child
= child
.GetWindow()
717 elif child
.IsSizer():
718 child
= child
.GetSizer()
721 def OnSelChanged(self
, evt
):
722 if self
.selectionChanging
: return
723 self
.selectionChanging
= True
725 self
.SelectItem(evt
.GetItem())
726 self
.selectionChanging
= False
728 def ChangeSelection(self
, item
):
730 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
731 #oldItem = evt.GetOldItem()
733 oldItem
= self
.selection
734 # use GetItemParent as a way to determine if the itemId is still valid
735 if oldItem
and self
.GetItemParent(oldItem
):
736 xxx
= self
.GetPyData(oldItem
)
737 # If some data was modified, apply changes
738 if g
.panel
.IsModified():
739 self
.Apply(xxx
, oldItem
)
741 if g
.testWin
.highLight
:
742 g
.testWin
.highLight
.Remove()
743 self
.needUpdate
= True
744 status
= 'Changes were applied'
745 g
.frame
.SetStatusText(status
)
747 self
.selection
= item
748 if not self
.selection
.IsOk():
749 self
.selection
= None
751 xxx
= self
.GetPyData(self
.selection
)
756 # Highlighting is done in OnIdle
757 self
.pendingHighLight
= self
.selection
759 # Check if item is in testWin subtree
760 def IsHighlatable(self
, item
):
761 if item
== g
.testWin
.item
: return False
762 while item
!= self
.root
:
763 item
= self
.GetItemParent(item
)
764 if item
== g
.testWin
.item
:
768 # Highlight selected item
769 def HighLight(self
, item
):
770 self
.pendingHighLight
= None
771 # Can highlight only with some top-level windows
772 if not g
.testWin
or self
.GetPyData(g
.testWin
.item
).treeObject().__class
__ \
773 not in [xxxDialog
, xxxPanel
, xxxFrame
]:
775 # If a control from another window is selected, remove highlight
776 if not self
.IsHighlatable(item
):
777 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
779 # Get window/sizer object
780 obj
= self
.FindNodeObject(item
)
781 xxx
= self
.GetPyData(item
).treeObject()
782 # Remove existing HL if item not found or is hidden
783 if not obj
or xxx
.hasStyle
and xxx
.params
.get('hidden', False):
784 if g
.testWin
.highLight
: g
.testWin
.highLight
.Remove()
786 pos
= self
.FindNodePos(item
, obj
)
789 # Negative positions are not working quite well
790 hl
= g
.testWin
.highLight
791 # If highlight object has the same size SetDimension does not repaint it
792 # so we must remove the old HL window
793 if hl
and hl
.size
== size
:
797 hl
.Replace(pos
, size
)
799 g
.testWin
.highLight
= hl
= HighLightBox(pos
, size
)
803 def ShowTestWindow(self
, item
):
804 xxx
= self
.GetPyData(item
)
805 if g
.panel
.IsModified():
806 self
.Apply(xxx
, item
) # apply changes
807 availableViews
= ['wxFrame', 'wxPanel', 'wxDialog',
808 'wxMenuBar', 'wxToolBar', 'wxWizard',
809 'wxWizardPageSimple']
811 # Walk up the tree until we find an item that has a view
812 while item
and self
.GetPyData(item
).treeObject().className
not in availableViews
:
813 item
= self
.GetItemParent(item
)
814 if not item
or not item
.IsOk():
815 wx
.LogMessage('No view for this element (yet)')
818 if g
.testWin
: # Reset old
820 self
.SetItemBold(g
.testWin
.item
, False)
823 self
.CreateTestWin(item
)
826 # Maybe an error occurred, so we need to test
828 self
.SetItemBold(g
.testWin
.item
)
829 # Select original item
830 self
.ChangeSelection(originalItem
)
832 # Double-click on Linux
833 def OnItemActivated(self
, evt
):
834 if evt
.GetItem() != self
.root
:
835 self
.ShowTestWindow(evt
.GetItem())
837 # Double-click on Windows
838 def OnDClick(self
, evt
):
839 item
, flags
= self
.HitTest(evt
.GetPosition())
840 if flags
in [wx
.TREE_HITTEST_ONITEMBUTTON
, wx
.TREE_HITTEST_ONITEMLABEL
]:
841 if item
!= self
.root
: self
.ShowTestWindow(item
)
845 def OnItemExpandedCollapsed(self
, evt
):
846 # Update tool palette
850 # (re)create test window
851 def CreateTestWin(self
, item
):
853 # Create a window with this resource
854 xxx
= self
.GetPyData(item
).treeObject()
855 # Close old window, remember where it was
858 pos
= testWin
.GetPosition()
859 if item
== testWin
.item
:
860 # Remember highlight if same top-level window
861 if testWin
.highLight
:
862 highLight
= testWin
.highLight
.item
863 if xxx
.className
== 'wxPanel':
864 if testWin
.highLight
:
865 testWin
.pendingHighLight
= highLight
866 testWin
.highLight
.Remove()
867 testWin
.panel
.Destroy()
871 testWin
= g
.testWin
= None
874 testWin
= g
.testWin
= None
878 memFile
= MemoryFile('xxx.xrc')
879 # Create memory XML file
880 elem
= xxx
.node
.cloneNode(True)
885 elem
.setAttribute('name', STD_NAME
)
886 oldTestNode
= self
.testElem
888 self
.mainNode
.replaceChild(elem
, oldTestNode
)
890 # Replace wizard page class temporarily
891 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
892 oldCl
= elem
.getAttribute('class')
893 elem
.setAttribute('class', 'wxPanel')
894 parent
= elem
.parentNode
895 encd
= self
.rootObj
.params
['encoding'].value()
896 if not encd
: encd
= None
898 self
.dom
.writexml(memFile
, encoding
=encd
)
901 wx
.LogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
902 wx
.LogError('Error writing temporary file')
903 memFile
.close() # write to wxMemoryFS
904 xmlFlags
= xrc
.XRC_NO_SUBCLASSING
905 # Use translations if encoding is not specified
906 if not g
.currentEncoding
:
907 xmlFlags
!= xrc
.XRC_USE_LOCALE
908 res
= xrc
.XmlResource('', xmlFlags
)
909 xrc
.XmlResource
.Set(res
) # set as global
913 res
.Load('memory:xxx.xrc')
915 if xxx
.__class
__ == xxxFrame
:
916 # Frame can't have many children,
917 # but it's first child possibly can...
918 # child = self.GetFirstChild(item)[0]
919 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
920 # # Clean-up before recursive call or error
921 # wx.MemoryFSHandler.RemoveFile('xxx.xrc')
923 # self.CreateTestWin(child)
925 # This currently works under GTK, but not under MSW
926 testWin
= g
.testWin
= wx
.PreFrame()
927 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
929 testWin
.panel
= testWin
930 #testWin.CreateStatusBar()
931 testWin
.SetClientSize(testWin
.GetBestSize())
932 testWin
.SetPosition(pos
)
934 elif xxx
.__class
__ == xxxPanel
:
937 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'Panel: ' + name
,
938 pos
=pos
, name
=STD_NAME
)
939 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
940 testWin
.SetClientSize(testWin
.GetBestSize())
942 elif xxx
.__class
__ == xxxDialog
:
943 testWin
= g
.testWin
= res
.LoadDialog(g
.frame
, STD_NAME
)
944 testWin
.panel
= testWin
946 testWin
.SetPosition(pos
)
948 # Dialog's default code does not produce wx.EVT_CLOSE
949 wx
.EVT_BUTTON(testWin
, wx
.ID_OK
, self
.OnCloseTestWin
)
950 wx
.EVT_BUTTON(testWin
, wx
.ID_CANCEL
, self
.OnCloseTestWin
)
951 elif xxx
.__class
__ == xxxWizard
:
952 wiz
= wx
.wizard
.PreWizard()
953 res
.LoadOnObject(wiz
, g
.frame
, STD_NAME
, 'wxWizard')
954 # Find first page (don't know better way)
956 for w
in wiz
.GetChildren():
957 if isinstance(w
, wx
.wizard
.WizardPage
):
961 wx
.LogError('Wizard is empty')
963 # Wizard should be modal
964 self
.SetItemBold(item
)
966 self
.SetItemBold(item
, False)
968 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
971 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'Wizard page: ' + name
,
972 pos
=pos
, name
=STD_NAME
)
973 testWin
.panel
= wx
.PrePanel()
974 res
.LoadOnObject(testWin
.panel
, testWin
, STD_NAME
, 'wxPanel')
975 testWin
.SetClientSize(testWin
.GetBestSize())
977 elif xxx
.__class
__ == xxxMenuBar
:
978 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'MenuBar: ' + name
,
979 pos
=pos
, name
=STD_NAME
)
981 # Set status bar to display help
982 testWin
.CreateStatusBar()
983 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
984 testWin
.SetMenuBar(testWin
.menuBar
)
986 elif xxx
.__class
__ == xxxToolBar
:
987 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'ToolBar: ' + name
,
988 pos
=pos
, name
=STD_NAME
)
990 # Set status bar to display help
991 testWin
.CreateStatusBar()
992 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
993 testWin
.SetToolBar(testWin
.toolBar
)
995 # Catch some events, set highlight
998 wx
.EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
999 wx
.EVT_SIZE(testWin
, self
.OnSizeTestWin
)
1000 testWin
.highLight
= None
1001 if highLight
and not self
.pendingHighLight
:
1002 self
.HighLight(highLight
)
1005 self
.SetItemBold(item
, False)
1006 g
.testWinPos
= g
.testWin
.GetPosition()
1009 inf
= sys
.exc_info()
1010 wx
.LogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
1011 wx
.LogError('Error loading resource')
1013 res
.Unload('xxx.xrc')
1014 xrc
.XmlResource
.Set(None)
1015 wx
.MemoryFSHandler
.RemoveFile('xxx.xrc')
1017 def CloseTestWindow(self
):
1018 if not g
.testWin
: return
1019 self
.SetItemBold(g
.testWin
.item
, False)
1020 g
.frame
.tb
.ToggleTool(g
.frame
.ID_TOOL_LOCATE
, False)
1021 g
.testWinPos
= g
.testWin
.GetPosition()
1025 def OnCloseTestWin(self
, evt
):
1026 self
.CloseTestWindow()
1028 def OnSizeTestWin(self
, evt
):
1029 if g
.testWin
.highLight
:
1030 self
.HighLight(g
.testWin
.highLight
.item
)
1033 # Return index in parent, for real window children
1034 def WindowIndex(self
, item
):
1035 n
= 0 # index of sibling
1036 prev
= self
.GetPrevSibling(item
)
1038 # MenuBar is not a child
1039 if not isinstance(self
.GetPyData(prev
), xxxMenuBar
):
1041 prev
= self
.GetPrevSibling(prev
)
1044 # Return item index in parent
1045 def ItemIndex(self
, item
):
1046 n
= 0 # index of sibling
1047 prev
= self
.GetPrevSibling(item
)
1049 prev
= self
.GetPrevSibling(prev
)
1053 # Full tree index of an item - list of positions
1054 def ItemFullIndex(self
, item
):
1055 if not item
.IsOk(): return None
1057 while item
!= self
.root
:
1058 l
.insert(0, self
.ItemIndex(item
))
1059 item
= self
.GetItemParent(item
)
1061 # Get item position from full index
1062 def ItemAtFullIndex(self
, index
):
1063 if index
is None: return wx
.TreeItemId()
1066 item
= self
.GetFirstChild(item
)[0]
1067 for k
in range(i
): item
= self
.GetNextSibling(item
)
1070 # True if next item should be inserted after current (vs. appended to it)
1071 def NeedInsert(self
, item
):
1072 xxx
= self
.GetPyData(item
)
1073 if item
== self
.root
: return False # root item
1074 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
1076 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
1078 # Override to use like single-selection tree
1079 def GetSelection(self
):
1080 return self
.selection
1081 def SelectItem(self
, item
):
1083 self
.ChangeSelection(item
)
1084 wx
.TreeCtrl
.SelectItem(self
, item
)
1087 def OnRightDown(self
, evt
):
1088 pullDownMenu
= g
.pullDownMenu
1090 pt
= evt
.GetPosition();
1091 item
, flags
= self
.HitTest(pt
)
1092 if item
.Ok() and flags
& wx
.TREE_HITTEST_ONITEM
:
1093 self
.SelectItem(item
)
1098 item
= self
.selection
1100 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
1101 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
1103 # self.ctrl = evt.ControlDown() # save Ctrl state
1104 # self.shift = evt.ShiftDown() # and Shift too
1105 m
= wx
.Menu() # create menu
1109 needInsert
= self
.NeedInsert(item
)
1110 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
1111 SetMenu(m
, pullDownMenu
.topLevel
)
1113 m
.Append(ID_NEW
.REF
, 'reference...', 'Create object_ref node')
1114 m
.Append(ID_NEW
.COMMENT
, 'comment', 'Create comment node')
1116 xxx
= self
.GetPyData(item
).treeObject()
1117 # Check parent for possible child nodes if inserting sibling
1118 if needInsert
: xxx
= xxx
.parent
1119 if xxx
.__class
__ == xxxMenuBar
:
1120 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
1121 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
1122 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
1123 SetMenu(m
, pullDownMenu
.toolBarControls
)
1124 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1125 SetMenu(m
, pullDownMenu
.menuControls
)
1126 elif xxx
.__class
__ == xxxStdDialogButtonSizer
:
1127 SetMenu(m
, pullDownMenu
.stdButtons
)
1129 SetMenu(m
, pullDownMenu
.controls
)
1130 if xxx
.__class
__ in [xxxNotebook
, xxxChoicebook
, xxxListbook
]:
1131 m
.Enable(m
.FindItem('sizer'), False)
1132 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
1133 m
.Enable(ID_NEW
.SPACER
, False)
1134 if xxx
.__class
__ is not xxxFrame
:
1135 m
.Enable(ID_NEW
.MENU_BAR
, False)
1136 # Add custom controls menu
1137 if len(pullDownMenu
.custom
) > 2:
1138 SetMenu(m
, [pullDownMenu
.custom
])
1140 m
.Append(ID_NEW
.REF
, 'reference...', 'Create object_ref node')
1141 m
.Append(ID_NEW
.COMMENT
, 'comment', 'Create comment node')
1142 # Select correct label for create menu
1145 menu
.AppendMenu(wx
.NewId(), 'Insert Child', m
,
1146 'Create child object as the first child')
1148 menu
.AppendMenu(wx
.NewId(), 'Append Child', m
,
1149 'Create child object as the last child')
1152 menu
.AppendMenu(wx
.NewId(), 'Create Sibling', m
,
1153 'Create sibling before selected object')
1155 menu
.AppendMenu(wx
.NewId(), 'Create Sibling', m
,
1156 'Create sibling after selected object')
1157 # Build replace menu
1158 if item
!= self
.root
:
1159 xxx
= self
.GetPyData(item
).treeObject()
1160 m
= wx
.Menu() # create replace menu
1161 if xxx
.__class
__ == xxxMenuBar
:
1162 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
1163 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1164 SetMenu(m
, pullDownMenu
.menuControls
, shift
=True)
1165 elif xxx
.__class
__ == xxxToolBar
and \
1166 self
.GetItemParent(item
) == self
.root
:
1167 SetMenu(m
, [], shift
=True)
1168 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
1170 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
1171 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
1172 (ID_NEW
.FRAME
, 'Frame', 'Create frame')], shift
=True)
1173 elif xxx
.isSizer
and self
.ItemHasChildren(item
):
1174 SetMenu(m
, pullDownMenu
.sizers
, shift
=True)
1176 SetMenu(m
, pullDownMenu
.controls
, shift
=True)
1179 menu
.AppendMenu(id, 'Replace With', m
)
1180 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
1181 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
1182 'Set "subclass" property')
1183 menu
.AppendSeparator()
1184 # Not using standart IDs because we don't want to show shortcuts
1185 menu
.Append(wx
.ID_CUT
, 'Cut', 'Cut to the clipboard')
1186 menu
.Append(wx
.ID_COPY
, 'Copy', 'Copy to the clipboard')
1187 if self
.ctrl
and item
!= self
.root
:
1188 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
1189 'Paste from the clipboard as a sibling')
1191 menu
.Append(wx
.ID_PASTE
, 'Paste', 'Paste from the clipboard')
1192 menu
.Append(pullDownMenu
.ID_DELETE
,
1193 'Delete', 'Delete object')
1194 if self
.ItemHasChildren(item
):
1195 menu
.AppendSeparator()
1196 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
1197 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
1198 self
.PopupMenu(menu
, evt
.GetPosition())
1201 # Redefine to force the update of font dimentions on wxGTK
1202 if wx
.Platform
== '__WXGTK__':
1203 def SetItemBold(self
, item
, state
=True):
1204 wx
.TreeCtrl
.SetItemBold(self
, item
, state
)
1205 self
.SetIndent(self
.GetIndent())
1208 def Apply(self
, xxx
, item
):
1211 xxx
= xxx
.treeObject()
1212 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
1213 self
.SetItemText(item
, xxx
.treeName())
1214 # Item width may have changed
1215 # !!! Tric to update tree width (wxGTK, ??)
1216 self
.SetIndent(self
.GetIndent())
1217 elif xxx
.className
== 'comment':
1218 self
.SetItemText(item
, xxx
.treeName())
1219 # Change tree icon for sizers
1220 if isinstance(xxx
, xxxBoxSizer
):
1221 self
.SetItemImage(item
, xxx
.treeImage())
1222 # Set global modified state
1223 g
.frame
.SetModified()
1225 def OnBeginLabelEdit(self
, evt
):
1226 xxx
= self
.GetPyData(evt
.GetItem())
1232 def OnEndLabelEdit(self
, evt
):
1233 xxx
= self
.GetPyData(evt
.GetItem())
1235 if not xxx
.isElement
:
1236 node
.data
= evt
.GetLabel()
1237 g
.panel
.SetData(xxx
)