]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tree.py
OnReplace not yet undoable
[wxWidgets.git] / wxPython / wx / tools / XRCed / tree.py
1 # Name: tree.py
2 # Purpose: XRC editor, XML_tree class
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 02.12.2002
5 # RCS-ID: $Id$
6
7 from xxx import * # xxx imports globals and params
8 import types
9 import traceback
10
11 # Constant to define standart window name
12 STD_NAME = '_XRCED_T_W'
13
14 # Icons
15 import images
16
17 class MemoryFile:
18 def __init__(self, name):
19 self.name = name
20 self.buffer = ''
21 def write(self, data):
22 if g.currentEncoding:
23 encoding = g.currentEncoding
24 else:
25 encoding = wxGetDefaultPyEncoding()
26 try:
27 self.buffer += data.encode(encoding)
28 except UnicodeEncodeError:
29 self.buffer += data.encode(encoding, 'xmlcharrefreplace')
30
31 def close(self):
32 wxMemoryFSHandler_AddFile(self.name, self.buffer)
33
34 ################################################################################
35
36 # Redefine writing to include encoding
37 class MyDocument(minidom.Document):
38 def __init__(self):
39 minidom.Document.__init__(self)
40 self.encoding = ''
41 def writexml(self, writer, indent="", addindent="", newl="", encoding=""):
42 if encoding: encdstr = 'encoding="%s"' % encoding
43 else: encdstr = ''
44 writer.write('<?xml version="1.0" %s?>\n' % encdstr)
45 for node in self.childNodes:
46 node.writexml(writer, indent, addindent, newl)
47
48 ################################################################################
49
50 # Ids for menu commands
51 class ID_NEW:
52 PANEL = wxNewId()
53 DIALOG = wxNewId()
54 FRAME = wxNewId()
55 TOOL_BAR = wxNewId()
56 TOOL = wxNewId()
57 MENU_BAR = wxNewId()
58 MENU = wxNewId()
59 STATUS_BAR = wxNewId()
60
61 STATIC_TEXT = wxNewId()
62 TEXT_CTRL = wxNewId()
63
64 BUTTON = wxNewId()
65 BITMAP_BUTTON = wxNewId()
66 RADIO_BUTTON = wxNewId()
67 SPIN_BUTTON = wxNewId()
68 TOGGLE_BUTTON = wxNewId()
69
70 STATIC_BOX = wxNewId()
71 CHECK_BOX = wxNewId()
72 RADIO_BOX = wxNewId()
73 COMBO_BOX = wxNewId()
74 LIST_BOX = wxNewId()
75
76 STATIC_LINE = wxNewId()
77 STATIC_BITMAP = wxNewId()
78 CHOICE = wxNewId()
79 SLIDER = wxNewId()
80 GAUGE = wxNewId()
81 SCROLL_BAR = wxNewId()
82 TREE_CTRL = wxNewId()
83 LIST_CTRL = wxNewId()
84 CHECK_LIST = wxNewId()
85 NOTEBOOK = wxNewId()
86 CHOICEBOOK = wxNewId()
87 LISTBOOK = wxNewId()
88 SPLITTER_WINDOW = wxNewId()
89 SCROLLED_WINDOW = wxNewId()
90 HTML_WINDOW = wxNewId()
91 CALENDAR_CTRL = wxNewId()
92 DATE_CTRL = wxNewId()
93 GENERIC_DIR_CTRL = wxNewId()
94 SPIN_CTRL = wxNewId()
95 UNKNOWN = wxNewId()
96 WIZARD = wxNewId()
97 WIZARD_PAGE = wxNewId()
98 WIZARD_PAGE_SIMPLE = wxNewId()
99 BITMAP = wxNewId()
100 ICON = wxNewId()
101 STATUS_BAR = wxNewId()
102
103 BOX_SIZER = wxNewId()
104 STATIC_BOX_SIZER = wxNewId()
105 GRID_SIZER = wxNewId()
106 FLEX_GRID_SIZER = wxNewId()
107 GRID_BAG_SIZER = wxNewId()
108 STD_DIALOG_BUTTON_SIZER = wxNewId()
109 SPACER = wxNewId()
110
111 TOOL_BAR = wxNewId()
112 TOOL = wxNewId()
113 MENU = wxNewId()
114 MENU_ITEM = wxNewId()
115 SEPARATOR = wxNewId()
116
117 OK_BUTTON = wxNewId()
118 YES_BUTTON = wxNewId()
119 SAVE_BUTTON = wxNewId()
120 APPLY_BUTTON = wxNewId()
121 NO_BUTTON = wxNewId()
122 CANCEL_BUTTON = wxNewId()
123 HELP_BUTTON = wxNewId()
124 CONTEXT_HELP_BUTTON = wxNewId()
125
126 REF = wxNewId()
127
128 LAST = wxNewId()
129
130
131
132 class PullDownMenu:
133 ID_EXPAND = wxNewId()
134 ID_COLLAPSE = wxNewId()
135 ID_PASTE_SIBLING = wxNewId()
136 ID_TOOL_PASTE = wxNewId()
137 ID_SUBCLASS = wxNewId()
138
139 def __init__(self, parent):
140 self.ID_DELETE = parent.ID_DELETE
141 EVT_MENU_RANGE(parent, ID_NEW.PANEL, ID_NEW.LAST, parent.OnCreate)
142 EVT_MENU_RANGE(parent, 1000 + ID_NEW.PANEL, 1000 + ID_NEW.LAST, parent.OnReplace)
143 EVT_MENU(parent, self.ID_COLLAPSE, parent.OnCollapse)
144 EVT_MENU(parent, self.ID_EXPAND, parent.OnExpand)
145 EVT_MENU(parent, self.ID_PASTE_SIBLING, parent.OnPaste)
146 EVT_MENU(parent, self.ID_SUBCLASS, parent.OnSubclass)
147 # We connect to tree, but process in frame
148 EVT_MENU_HIGHLIGHT_ALL(g.tree, parent.OnPullDownHighlight)
149
150 # Mapping from IDs to element names
151 self.createMap = {
152 ID_NEW.PANEL: 'wxPanel',
153 ID_NEW.DIALOG: 'wxDialog',
154 ID_NEW.FRAME: 'wxFrame',
155 ID_NEW.WIZARD: 'wxWizard',
156 ID_NEW.WIZARD_PAGE: 'wxWizardPage',
157 ID_NEW.WIZARD_PAGE_SIMPLE: 'wxWizardPageSimple',
158 ID_NEW.TOOL_BAR: 'wxToolBar',
159 ID_NEW.TOOL: 'tool',
160 ID_NEW.STATUS_BAR: 'wxStatusBar',
161 ID_NEW.MENU_BAR: 'wxMenuBar',
162 ID_NEW.MENU: 'wxMenu',
163 ID_NEW.MENU_ITEM: 'wxMenuItem',
164 ID_NEW.BITMAP: 'wxBitmap',
165 ID_NEW.ICON: 'wxIcon',
166 ID_NEW.SEPARATOR: 'separator',
167
168 ID_NEW.STATIC_TEXT: 'wxStaticText',
169 ID_NEW.TEXT_CTRL: 'wxTextCtrl',
170
171 ID_NEW.BUTTON: 'wxButton',
172 ID_NEW.BITMAP_BUTTON: 'wxBitmapButton',
173 ID_NEW.RADIO_BUTTON: 'wxRadioButton',
174 ID_NEW.SPIN_BUTTON: 'wxSpinButton',
175 ID_NEW.TOGGLE_BUTTON: 'wxToggleButton',
176
177 ID_NEW.STATIC_BOX: 'wxStaticBox',
178 ID_NEW.CHECK_BOX: 'wxCheckBox',
179 ID_NEW.RADIO_BOX: 'wxRadioBox',
180 ID_NEW.COMBO_BOX: 'wxComboBox',
181 ID_NEW.LIST_BOX: 'wxListBox',
182 ID_NEW.CHECK_LIST: 'wxCheckListBox',
183
184 ID_NEW.STATIC_LINE: 'wxStaticLine',
185 ID_NEW.STATIC_BITMAP: 'wxStaticBitmap',
186 ID_NEW.CHOICE: 'wxChoice',
187 ID_NEW.SLIDER: 'wxSlider',
188 ID_NEW.GAUGE: 'wxGauge',
189 ID_NEW.SCROLL_BAR: 'wxScrollBar',
190 ID_NEW.TREE_CTRL: 'wxTreeCtrl',
191 ID_NEW.LIST_CTRL: 'wxListCtrl',
192 ID_NEW.NOTEBOOK: 'wxNotebook',
193 ID_NEW.CHOICEBOOK: 'wxChoicebook',
194 ID_NEW.LISTBOOK: 'wxListbook',
195 ID_NEW.SPLITTER_WINDOW: 'wxSplitterWindow',
196 ID_NEW.SCROLLED_WINDOW: 'wxScrolledWindow',
197 ID_NEW.HTML_WINDOW: 'wxHtmlWindow',
198 ID_NEW.CALENDAR_CTRL: 'wxCalendarCtrl',
199 ID_NEW.DATE_CTRL: 'wxDatePickerCtrl',
200 ID_NEW.GENERIC_DIR_CTRL: 'wxGenericDirCtrl',
201 ID_NEW.SPIN_CTRL: 'wxSpinCtrl',
202
203 ID_NEW.BOX_SIZER: 'wxBoxSizer',
204 ID_NEW.STATIC_BOX_SIZER: 'wxStaticBoxSizer',
205 ID_NEW.GRID_SIZER: 'wxGridSizer',
206 ID_NEW.FLEX_GRID_SIZER: 'wxFlexGridSizer',
207 ID_NEW.GRID_BAG_SIZER: 'wxGridBagSizer',
208 ID_NEW.STD_DIALOG_BUTTON_SIZER: 'wxStdDialogButtonSizer',
209 ID_NEW.SPACER: 'spacer',
210 ID_NEW.UNKNOWN: 'unknown',
211
212 ID_NEW.OK_BUTTON: 'wxButton',
213 ID_NEW.YES_BUTTON: 'wxButton',
214 ID_NEW.SAVE_BUTTON: 'wxButton',
215 ID_NEW.APPLY_BUTTON: 'wxButton',
216 ID_NEW.NO_BUTTON: 'wxButton',
217 ID_NEW.CANCEL_BUTTON: 'wxButton',
218 ID_NEW.HELP_BUTTON: 'wxButton',
219 ID_NEW.CONTEXT_HELP_BUTTON: 'wxButton',
220 }
221 self.topLevel = [
222 (ID_NEW.PANEL, 'Panel', 'Create panel'),
223 (ID_NEW.DIALOG, 'Dialog', 'Create dialog'),
224 (ID_NEW.FRAME, 'Frame', 'Create frame'),
225 (ID_NEW.WIZARD, 'Wizard', 'Create wizard'),
226 None,
227 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
228 (ID_NEW.MENU_BAR, 'MenuBar', 'Create menubar'),
229 (ID_NEW.MENU, 'Menu', 'Create menu'),
230 None,
231 (ID_NEW.BITMAP, 'Bitmap', 'Create bitmap'),
232 (ID_NEW.ICON, 'Icon', 'Create icon'),
233 ]
234 self.containers = [
235 (ID_NEW.PANEL, 'Panel', 'Create panel'),
236 (ID_NEW.NOTEBOOK, 'Notebook', 'Create notebook control'),
237 (ID_NEW.CHOICEBOOK, 'Choicebook', 'Create choicebook control'),
238 (ID_NEW.LISTBOOK, 'Listbook', 'Create listbook control'),
239 (ID_NEW.SPLITTER_WINDOW, 'SplitterWindow', 'Create splitter window'),
240 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
241 (ID_NEW.STATUS_BAR, 'StatusBar', 'Create status bar'),
242 # (ID_NEW.WIZARD_PAGE, 'WizardPage', 'Create wizard page'),
243 (ID_NEW.WIZARD_PAGE_SIMPLE, 'WizardPageSimple', 'Create simple wizard page'),
244 ]
245 self.sizers = [
246 (ID_NEW.BOX_SIZER, 'BoxSizer', 'Create box sizer'),
247 (ID_NEW.STATIC_BOX_SIZER, 'StaticBoxSizer',
248 'Create static box sizer'),
249 (ID_NEW.GRID_SIZER, 'GridSizer', 'Create grid sizer'),
250 (ID_NEW.FLEX_GRID_SIZER, 'FlexGridSizer',
251 'Create flexgrid sizer'),
252 (ID_NEW.GRID_BAG_SIZER, 'GridBagSizer',
253 'Create gridbag sizer'),
254 # (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
255 # 'Create standard button sizer'),
256 (ID_NEW.SPACER, 'Spacer', 'Create spacer'),
257 ]
258 self.controls = [
259 ['control', 'Various controls',
260 (ID_NEW.STATIC_TEXT, 'Label', 'Create label'),
261 (ID_NEW.STATIC_BITMAP, 'Bitmap', 'Create bitmap'),
262 (ID_NEW.STATIC_LINE, 'Line', 'Create line'),
263 (ID_NEW.TEXT_CTRL, 'TextBox', 'Create text box'),
264 (ID_NEW.CHOICE, 'Choice', 'Create choice'),
265 (ID_NEW.SLIDER, 'Slider', 'Create slider'),
266 (ID_NEW.GAUGE, 'Gauge', 'Create gauge'),
267 (ID_NEW.SPIN_CTRL, 'SpinCtrl', 'Create spin'),
268 (ID_NEW.SCROLL_BAR, 'ScrollBar', 'Create scroll bar'),
269 (ID_NEW.TREE_CTRL, 'TreeCtrl', 'Create tree'),
270 (ID_NEW.LIST_CTRL, 'ListCtrl', 'Create list'),
271 (ID_NEW.SCROLLED_WINDOW, 'ScrolledWindow', 'Create scrolled window'),
272 (ID_NEW.HTML_WINDOW, 'HtmlWindow', 'Create HTML window'),
273 (ID_NEW.CALENDAR_CTRL, 'CalendarCtrl', 'Create calendar control'),
274 (ID_NEW.DATE_CTRL, 'DatePickerCtrl', 'Create date picker control'),
275 (ID_NEW.GENERIC_DIR_CTRL, 'GenericDirCtrl', 'Create generic dir control'),
276 (ID_NEW.UNKNOWN, 'Unknown', 'Create custom control placeholder'),
277 ],
278 ['button', 'Buttons',
279 (ID_NEW.BUTTON, 'Button', 'Create button'),
280 (ID_NEW.BITMAP_BUTTON, 'BitmapButton', 'Create bitmap button'),
281 (ID_NEW.RADIO_BUTTON, 'RadioButton', 'Create radio button'),
282 (ID_NEW.SPIN_BUTTON, 'SpinButton', 'Create spin button'),
283 (ID_NEW.TOGGLE_BUTTON, 'ToggleButton', 'Create toggle button'),
284 ],
285 ['box', 'Boxes',
286 (ID_NEW.STATIC_BOX, 'StaticBox', 'Create static box'),
287 (ID_NEW.CHECK_BOX, 'CheckBox', 'Create check box'),
288 (ID_NEW.RADIO_BOX, 'RadioBox', 'Create radio box'),
289 (ID_NEW.COMBO_BOX, 'ComboBox', 'Create combo box'),
290 (ID_NEW.LIST_BOX, 'ListBox', 'Create list box'),
291 (ID_NEW.CHECK_LIST, 'CheckListBox', 'Create checklist box'),
292 ],
293 ['container', 'Containers',
294 (ID_NEW.PANEL, 'Panel', 'Create panel'),
295 (ID_NEW.NOTEBOOK, 'Notebook', 'Create notebook control'),
296 (ID_NEW.CHOICEBOOK, 'Choicebook', 'Create choicebook control'),
297 (ID_NEW.LISTBOOK, 'Listbook', 'Create listbook control'),
298 (ID_NEW.SPLITTER_WINDOW, 'SplitterWindow', 'Create splitter window'),
299 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
300 (ID_NEW.STATUS_BAR, 'StatusBar', 'Create status bar'),
301 # (ID_NEW.WIZARD_PAGE, 'Wizard Page', 'Create wizard page'),
302 (ID_NEW.WIZARD_PAGE_SIMPLE, 'WizardPageSimple', 'Create simple wizard page'),
303 ],
304 ['sizer', 'Sizers',
305 (ID_NEW.BOX_SIZER, 'BoxSizer', 'Create box sizer'),
306 (ID_NEW.STATIC_BOX_SIZER, 'StaticBoxSizer',
307 'Create static box sizer'),
308 (ID_NEW.GRID_SIZER, 'GridSizer', 'Create grid sizer'),
309 (ID_NEW.FLEX_GRID_SIZER, 'FlexGridSizer',
310 'Create flexgrid sizer'),
311 (ID_NEW.GRID_BAG_SIZER, 'GridBagSizer',
312 'Create gridbag sizer'),
313 (ID_NEW.SPACER, 'Spacer', 'Create spacer'),
314 (ID_NEW.STD_DIALOG_BUTTON_SIZER, 'StdDialogButtonSizer',
315 'Create standard button sizer'),
316 ]
317 ]
318 self.menuControls = [
319 (ID_NEW.MENU, 'Menu', 'Create menu'),
320 (ID_NEW.MENU_ITEM, 'MenuItem', 'Create menu item'),
321 (ID_NEW.SEPARATOR, 'Separator', 'Create separator'),
322 ]
323 self.toolBarControls = [
324 (ID_NEW.TOOL, 'Tool', 'Create tool'),
325 (ID_NEW.SEPARATOR, 'Separator', 'Create separator'),
326 ['control', 'Various controls',
327 (ID_NEW.STATIC_TEXT, 'Label', 'Create label'),
328 (ID_NEW.STATIC_BITMAP, 'Bitmap', 'Create bitmap'),
329 (ID_NEW.STATIC_LINE, 'Line', 'Create line'),
330 (ID_NEW.TEXT_CTRL, 'TextBox', 'Create text box'),
331 (ID_NEW.CHOICE, 'Choice', 'Create choice'),
332 (ID_NEW.SLIDER, 'Slider', 'Create slider'),
333 (ID_NEW.GAUGE, 'Gauge', 'Create gauge'),
334 (ID_NEW.SCROLL_BAR, 'ScrollBar', 'Create scroll bar'),
335 (ID_NEW.LIST_CTRL, 'ListCtrl', 'Create list control'),
336 ],
337 ['button', 'Buttons',
338 (ID_NEW.BUTTON, 'Button', 'Create button'),
339 (ID_NEW.BITMAP_BUTTON, 'BitmapButton', 'Create bitmap button'),
340 (ID_NEW.RADIO_BUTTON, 'RadioButton', 'Create radio button'),
341 (ID_NEW.SPIN_BUTTON, 'SpinButton', 'Create spin button'),
342 ],
343 ['box', 'Boxes',
344 (ID_NEW.STATIC_BOX, 'StaticBox', 'Create static box'),
345 (ID_NEW.CHECK_BOX, 'CheckBox', 'Create check box'),
346 (ID_NEW.RADIO_BOX, 'RadioBox', 'Create radio box'),
347 (ID_NEW.COMBO_BOX, 'ComboBox', 'Create combo box'),
348 (ID_NEW.LIST_BOX, 'ListBox', 'Create list box'),
349 (ID_NEW.CHECK_LIST, 'CheckListBox', 'Create checklist box'),
350 ],
351 ]
352 self.stdButtons = [
353 (ID_NEW.OK_BUTTON, 'OK Button', 'Create standard button'),
354 (ID_NEW.YES_BUTTON, 'YES Button', 'Create standard button'),
355 (ID_NEW.SAVE_BUTTON, 'SAVE Button', 'Create standard button'),
356 (ID_NEW.APPLY_BUTTON, 'APPLY Button', 'Create standard button'),
357 (ID_NEW.NO_BUTTON, 'NO Button', 'Create standard button'),
358 (ID_NEW.CANCEL_BUTTON, 'CANCEL Button', 'Create standard button'),
359 (ID_NEW.HELP_BUTTON, 'HELP Button', 'Create standard button'),
360 (ID_NEW.CONTEXT_HELP_BUTTON, 'CONTEXT HELP Button', 'Create standard button'),
361 ]
362 self.stdButtonIDs = {
363 ID_NEW.OK_BUTTON: ('wxID_OK', '&Ok'),
364 ID_NEW.YES_BUTTON: ('wxID_YES', '&Yes'),
365 ID_NEW.SAVE_BUTTON: ('wxID_SAVE', '&Save'),
366 ID_NEW.APPLY_BUTTON: ('wxID_APPLY', '&Apply'),
367 ID_NEW.NO_BUTTON: ('wxID_NO', '&No'),
368 ID_NEW.CANCEL_BUTTON: ('wxID_CANCEL', '&Cancel'),
369 ID_NEW.HELP_BUTTON: ('wxID_HELP', '&Help'),
370 ID_NEW.CONTEXT_HELP_BUTTON: ('wxID_CONTEXT_HELP', '&Help'),
371 }
372
373
374
375 ################################################################################
376
377 # Set menu to list items.
378 # Each menu command is a tuple (id, label, help)
379 # submenus are lists [id, label, help, submenu]
380 # and separators are any other type. Shift is for making
381 # alternative sets of IDs. (+1000).
382 def SetMenu(m, list, shift=False):
383 for l in list:
384 if type(l) == types.TupleType:
385 # Shift ID
386 if shift: l = (1000 + l[0],) + l[1:]
387 apply(m.Append, l)
388 elif type(l) == types.ListType:
389 subMenu = wxMenu()
390 SetMenu(subMenu, l[2:], shift)
391 m.AppendMenu(wxNewId(), l[0], subMenu, l[1])
392 else: # separator
393 m.AppendSeparator()
394
395 ################################################################################
396
397 class HighLightBox:
398 def __init__(self, pos, size):
399 if size.width == -1: size.width = 0
400 if size.height == -1: size.height = 0
401 w = g.testWin.panel
402 l1 = wxWindow(w, -1, pos, wxSize(size.width, 2))
403 l1.SetBackgroundColour(wxRED)
404 l2 = wxWindow(w, -1, pos, wxSize(2, size.height))
405 l2.SetBackgroundColour(wxRED)
406 l3 = wxWindow(w, -1, wxPoint(pos.x + size.width - 2, pos.y), wxSize(2, size.height))
407 l3.SetBackgroundColour(wxRED)
408 l4 = wxWindow(w, -1, wxPoint(pos.x, pos.y + size.height - 2), wxSize(size.width, 2))
409 l4.SetBackgroundColour(wxRED)
410 self.lines = [l1, l2, l3, l4]
411 # Move highlight to a new position
412 def Replace(self, pos, size):
413 if size.width == -1: size.width = 0
414 if size.height == -1: size.height = 0
415 self.lines[0].SetDimensions(pos.x, pos.y, size.width, 2)
416 self.lines[1].SetDimensions(pos.x, pos.y, 2, size.height)
417 self.lines[2].SetDimensions(pos.x + size.width - 2, pos.y, 2, size.height)
418 self.lines[3].SetDimensions(pos.x, pos.y + size.height - 2, size.width, 2)
419 # Remove it
420 def Remove(self):
421 map(wxWindow.Destroy, self.lines)
422 g.testWin.highLight = None
423 def Refresh(self):
424 map(wxWindow.Refresh, self.lines)
425
426 ################################################################################
427
428 class XML_Tree(wxTreeCtrl):
429 def __init__(self, parent, id):
430 wxTreeCtrl.__init__(self, parent, id, style = wxTR_HAS_BUTTONS | wxTR_MULTIPLE)
431 self.SetBackgroundColour(wxColour(224, 248, 224))
432 # Register events
433 EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
434 # One works on Linux, another on Windows
435 if wxPlatform == '__WXGTK__':
436 EVT_TREE_ITEM_ACTIVATED(self, self.GetId(), self.OnItemActivated)
437 else:
438 EVT_LEFT_DCLICK(self, self.OnDClick)
439 EVT_RIGHT_DOWN(self, self.OnRightDown)
440 EVT_TREE_ITEM_EXPANDED(self, self.GetId(), self.OnItemExpandedCollapsed)
441 EVT_TREE_ITEM_COLLAPSED(self, self.GetId(), self.OnItemExpandedCollapsed)
442
443 self.selection = None
444 self.selectionChanging = False
445 self.needUpdate = False
446 self.pendingHighLight = None
447 self.ctrl = self.shift = False
448 self.dom = None
449 # Create image list
450 il = wxImageList(16, 16, True)
451 self.rootImage = il.Add(images.getTreeRootImage().Scale(16,16).ConvertToBitmap())
452 xxxObject.image = il.Add(images.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
453 xxxPanel.image = il.Add(images.getTreePanelImage().Scale(16,16).ConvertToBitmap())
454 xxxDialog.image = il.Add(images.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
455 xxxFrame.image = il.Add(images.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
456 xxxMenuBar.image = il.Add(images.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
457 xxxMenu.image = il.Add(images.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
458 xxxMenuItem.image = il.Add(images.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
459 xxxToolBar.image = il.Add(images.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
460 xxxTool.image = il.Add(images.getTreeToolImage().Scale(16,16).ConvertToBitmap())
461 xxxSeparator.image = il.Add(images.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
462 xxxSizer.imageH = il.Add(images.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
463 xxxSizer.imageV = il.Add(images.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
464 xxxStaticBoxSizer.imageH = il.Add(images.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
465 xxxStaticBoxSizer.imageV = il.Add(images.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
466 xxxGridSizer.image = il.Add(images.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
467 xxxFlexGridSizer.image = il.Add(images.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
468 self.il = il
469 self.SetImageList(il)
470
471 def RegisterKeyEvents(self):
472 EVT_KEY_DOWN(self, g.tools.OnKeyDown)
473 EVT_KEY_UP(self, g.tools.OnKeyUp)
474 EVT_ENTER_WINDOW(self, g.tools.OnMouse)
475 EVT_LEAVE_WINDOW(self, g.tools.OnMouse)
476
477 def ExpandAll(self, item):
478 if self.ItemHasChildren(item):
479 self.Expand(item)
480 i, cookie = self.GetFirstChild(item)
481 children = []
482 while i.IsOk():
483 children.append(i)
484 i, cookie = self.GetNextChild(item, cookie)
485 for i in children:
486 self.ExpandAll(i)
487 def CollapseAll(self, item):
488 if self.ItemHasChildren(item):
489 i, cookie = self.GetFirstChild(item)
490 children = []
491 while i.IsOk():
492 children.append(i)
493 i, cookie = self.GetNextChild(item, cookie)
494 for i in children:
495 self.CollapseAll(i)
496 self.Collapse(item)
497
498 # Clear tree
499 def Clear(self):
500 self.selection = None
501 self.UnselectAll()
502 self.DeleteAllItems()
503 # Add minimal structure
504 if self.dom: self.dom.unlink()
505 self.dom = MyDocument()
506 self.dummyNode = self.dom.createComment('dummy node')
507 # Create main node
508 self.mainNode = self.dom.createElement('resource')
509 self.dom.appendChild(self.mainNode)
510 self.rootObj = xxxMainNode(self.dom)
511 self.root = self.AddRoot('XML tree', self.rootImage,
512 data=wxTreeItemData(self.rootObj))
513 self.SetItemHasChildren(self.root)
514 self.testElem = self.dom.createElement('dummy')
515 self.mainNode.appendChild(self.testElem)
516 self.Expand(self.root)
517
518 # Clear old data and set new
519 def SetData(self, dom):
520 self.selection = None
521 self.UnselectAll()
522 self.DeleteAllItems()
523 # Add minimal structure
524 if self.dom: self.dom.unlink()
525 self.dom = dom
526 self.dummyNode = self.dom.createComment('dummy node')
527 # Find 'resource' child, add it's children
528 self.mainNode = dom.documentElement
529 self.rootObj = xxxMainNode(self.dom)
530 self.root = self.AddRoot('XML tree', self.rootImage,
531 data=wxTreeItemData(self.rootObj))
532 self.SetItemHasChildren(self.root)
533 nodes = self.mainNode.childNodes[:]
534 for node in nodes:
535 if IsObject(node):
536 self.AddNode(self.root, None, node)
537 else:
538 self.mainNode.removeChild(node)
539 node.unlink()
540 if self.mainNode.firstChild:
541 self.testElem = self.dom.createElement('dummy')
542 self.mainNode.insertBefore(self.testElem, self.mainNode.firstChild)
543 else:
544 self.testElem = self.dom.createElement('dummy')
545 self.mainNode.appendChild(self.testElem)
546 self.Expand(self.root)
547
548 # Add tree item for given parent item if node is DOM element node with
549 # object/object_ref tag. xxxParent is parent xxx object
550 def AddNode(self, itemParent, xxxParent, node):
551 # Set item data to current node
552 try:
553 xxx = MakeXXXFromDOM(xxxParent, node)
554 except:
555 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent, node)
556 raise
557 treeObj = xxx.treeObject()
558 # Append tree item
559 item = self.AppendItem(itemParent, treeObj.treeName(),
560 image=treeObj.treeImage(),
561 data=wxTreeItemData(xxx))
562 # Different color for references
563 if treeObj.ref:
564 self.SetItemTextColour(item, 'DarkGreen')
565 # Try to find children objects
566 if treeObj.hasChildren:
567 nodes = treeObj.element.childNodes[:]
568 for n in nodes:
569 if IsObject(n):
570 self.AddNode(item, treeObj, n)
571 elif n.nodeType != minidom.Node.ELEMENT_NODE:
572 treeObj.element.removeChild(n)
573 n.unlink()
574
575 # Insert new item at specific position
576 def InsertNode(self, itemParent, parent, elem, nextItem):
577 # Insert in XML tree and wxWin
578 xxx = MakeXXXFromDOM(parent, elem)
579 # If nextItem is None, we append to parent, otherwise insert before it
580 if nextItem.IsOk():
581 node = self.GetPyData(nextItem).element
582 parent.element.insertBefore(elem, node)
583 # Inserting before is difficult, se we insert after or first child
584 index = self.ItemIndex(nextItem)
585 newItem = self.InsertItemBefore(itemParent, index,
586 xxx.treeName(), image=xxx.treeImage())
587 self.SetPyData(newItem, xxx)
588 else:
589 parent.element.appendChild(elem)
590 newItem = self.AppendItem(itemParent, xxx.treeName(), image=xxx.treeImage(),
591 data=wxTreeItemData(xxx))
592 # Different color for references
593 if xxx.treeObject().ref: self.SetItemTextColour(newItem, 'DarkGreen')
594 # Add children items
595 if xxx.hasChildren:
596 treeObj = xxx.treeObject()
597 for n in treeObj.element.childNodes:
598 if IsObject(n):
599 self.AddNode(newItem, treeObj, n)
600 return newItem
601
602 # Remove leaf of tree, return it's data object
603 def RemoveLeaf(self, leaf):
604 xxx = self.GetPyData(leaf)
605 node = xxx.element
606 parent = node.parentNode
607 parent.removeChild(node)
608 self.Delete(leaf)
609 # Reset selection object
610 self.selection = None
611 return node
612 # Find position relative to the top-level window
613 def FindNodePos(self, item, obj=None):
614 # Root at (0,0)
615 if item == g.testWin.item: return wxPoint(0, 0)
616 itemParent = self.GetItemParent(item)
617 # Select book page
618 if not obj: obj = self.FindNodeObject(item)
619 if self.GetPyData(itemParent).treeObject().__class__ in \
620 [xxxNotebook, xxxChoicebook, xxxListbook]:
621 book = self.FindNodeObject(itemParent)
622 # Find position
623 for i in range(book.GetPageCount()):
624 if book.GetPage(i) == obj:
625 if book.GetSelection() != i:
626 book.SetSelection(i)
627 # Remove highlight - otherwise highlight window won't be visible
628 if g.testWin.highLight:
629 g.testWin.highLight.Remove()
630 break
631 # Find first ancestor which is a wxWindow (not a sizer)
632 winParent = itemParent
633 while self.GetPyData(winParent).isSizer:
634 winParent = self.GetItemParent(winParent)
635 # Notebook children are layed out in a little strange way
636 if self.GetPyData(itemParent).treeObject().__class__ == xxxNotebook:
637 parentPos = wxPoint(0,0)
638 else:
639 parentPos = self.FindNodePos(winParent)
640 # Position (-1,-1) is really (0,0)
641 pos = obj.GetPosition()
642 if pos == (-1,-1): pos = (0,0)
643 return parentPos + pos
644
645 # Find window (or sizer) corresponding to a tree item.
646 def FindNodeObject(self, item):
647 testWin = g.testWin
648 # If top-level, return testWin (or panel its panel)
649 if item == testWin.item: return testWin.panel
650 itemParent = self.GetItemParent(item)
651 xxx = self.GetPyData(item).treeObject()
652 parentWin = self.FindNodeObject(itemParent)
653 # Top-level sizer? return window's sizer
654 if xxx.isSizer and isinstance(parentWin, wxWindow):
655 return parentWin.GetSizer()
656 elif isinstance(xxx, xxxStatusBar): return None
657 elif isinstance(xxx, xxxToolBar):
658 # If it's the main toolbar, we can't really select it
659 if xxx.parent.__class__ == xxxFrame: return None
660 elif isinstance(xxx.parent, xxxToolBar):
661 # Select complete toolbar
662 return parentWin
663 elif isinstance(xxx.parent, xxxStdDialogButtonSizer):
664 # This sizer returns non-existing children
665 for ch in parentWin.GetChildren():
666 if ch.GetWindow() and ch.GetWindow().GetName() == xxx.name:
667 return ch.GetWindow()
668 return None
669 elif xxx.parent.__class__ in [xxxChoicebook, xxxListbook]:
670 # First window is controld
671 return parentWin.GetChildren()[self.ItemIndex(item)+1]
672 # Otherwise get parent's object and it's child
673 child = parentWin.GetChildren()[self.ItemIndex(item)]
674 # Return window or sizer for sizer items
675 if child.GetClassName() == 'wxSizerItem':
676 if child.IsWindow(): child = child.GetWindow()
677 elif child.IsSizer():
678 child = child.GetSizer()
679 # Test for notebook sizers (deprecated)
680 if isinstance(child, wxNotebookSizer):
681 child = child.GetNotebook()
682 return child
683
684 def OnSelChanged(self, evt):
685 if self.selectionChanging: return
686 self.selectionChanging = True
687 self.UnselectAll()
688 self.SelectItem(evt.GetItem())
689 self.selectionChanging = False
690
691 def ChangeSelection(self, item):
692 # Apply changes
693 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
694 #oldItem = evt.GetOldItem()
695 status = ''
696 oldItem = self.selection
697 if oldItem:
698 xxx = self.GetPyData(oldItem)
699 # If some data was modified, apply changes
700 if g.panel.IsModified():
701 self.Apply(xxx, oldItem)
702 #if conf.autoRefresh:
703 if g.testWin:
704 if g.testWin.highLight:
705 g.testWin.highLight.Remove()
706 self.needUpdate = True
707 status = 'Changes were applied'
708 g.frame.SetStatusText(status)
709 # Generate view
710 self.selection = item
711 if not self.selection.IsOk():
712 self.selection = None
713 return
714 xxx = self.GetPyData(self.selection)
715 # Update panel
716 g.panel.SetData(xxx)
717 # Update tools
718 g.tools.UpdateUI()
719 # Highlighting is done in OnIdle
720 self.pendingHighLight = self.selection
721
722 # Check if item is in testWin subtree
723 def IsHighlatable(self, item):
724 if item == g.testWin.item: return False
725 while item != self.root:
726 item = self.GetItemParent(item)
727 if item == g.testWin.item: return True
728 return False
729
730 # Highlight selected item
731 def HighLight(self, item):
732 self.pendingHighLight = None
733 # Can highlight only with some top-level windows
734 if not g.testWin or self.GetPyData(g.testWin.item).treeObject().__class__ \
735 not in [xxxDialog, xxxPanel, xxxFrame]:
736 return
737 # If a control from another window is selected, remove highlight
738 if not self.IsHighlatable(item):
739 if g.testWin.highLight: g.testWin.highLight.Remove()
740 return
741 # Get window/sizer object
742 obj = self.FindNodeObject(item)
743 if not obj: return
744 pos = self.FindNodePos(item, obj)
745 size = obj.GetSize()
746 # Highlight
747 # Negative positions are not working quite well
748 if g.testWin.highLight:
749 g.testWin.highLight.Replace(pos, size)
750 else:
751 g.testWin.highLight = HighLightBox(pos, size)
752 g.testWin.highLight.Refresh()
753 g.testWin.highLight.item = item
754
755 def ShowTestWindow(self, item):
756 xxx = self.GetPyData(item)
757 if g.panel.IsModified():
758 self.Apply(xxx, item) # apply changes
759 availableViews = ['wxFrame', 'wxPanel', 'wxDialog',
760 'wxMenuBar', 'wxToolBar', 'wxWizard',
761 'wxWizardPageSimple']
762 originalItem = item
763 # Walk up the tree until we find an item that has a view
764 while item and self.GetPyData(item).treeObject().className not in availableViews:
765 item = self.GetItemParent(item)
766 if not item or not item.IsOk():
767 wxLogMessage('No view for this element (yet)')
768 return
769 # Show item in bold
770 if g.testWin: # Reset old
771 self.SetItemBold(g.testWin.item, False)
772 try:
773 wxBeginBusyCursor()
774 self.CreateTestWin(item)
775 finally:
776 wxEndBusyCursor()
777 # Maybe an error occurred, so we need to test
778 if g.testWin:
779 self.SetItemBold(g.testWin.item)
780 # Select original item
781 self.ChangeSelection(originalItem)
782
783 # Double-click on Linux
784 def OnItemActivated(self, evt):
785 if evt.GetItem() != self.root:
786 self.ShowTestWindow(evt.GetItem())
787
788 # Double-click on Windows
789 def OnDClick(self, evt):
790 item, flags = self.HitTest(evt.GetPosition())
791 if flags in [wxTREE_HITTEST_ONITEMBUTTON, wxTREE_HITTEST_ONITEMLABEL]:
792 if item != self.root: self.ShowTestWindow(item)
793 else:
794 evt.Skip()
795
796 def OnItemExpandedCollapsed(self, evt):
797 # Update tool palette
798 g.tools.UpdateUI()
799 evt.Skip()
800
801 # (re)create test window
802 def CreateTestWin(self, item):
803 testWin = g.testWin
804 # Create a window with this resource
805 xxx = self.GetPyData(item).treeObject()
806
807 # If frame
808 # if xxx.__class__ == xxxFrame:
809 # Frame can't have many children,
810 # but it's first child possibly can...
811 # child = self.GetFirstChild(item)[0]
812 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
813 # # Clean-up before recursive call or error
814 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
815 # wxEndBusyCursor()
816 # self.CreateTestWin(child)
817 # return
818
819 # Close old window, remember where it was
820 highLight = None
821 if testWin:
822 pos = testWin.GetPosition()
823 if item == testWin.item:
824 # Remember highlight if same top-level window
825 if testWin.highLight:
826 highLight = testWin.highLight.item
827 if xxx.className == 'wxPanel':
828 if testWin.highLight:
829 testWin.pendingHighLight = highLight
830 testWin.highLight.Remove()
831 testWin.panel.Destroy()
832 testWin.panel = None
833 else:
834 testWin.Destroy()
835 testWin = g.testWin = None
836 else:
837 testWin.Destroy()
838 testWin = g.testWin = None
839 else:
840 pos = g.testWinPos
841 # Save in memory FS
842 memFile = MemoryFile('xxx.xrc')
843 # Create memory XML file
844 elem = xxx.element.cloneNode(True)
845 if not xxx.name:
846 name = 'noname'
847 else:
848 name = xxx.name
849 elem.setAttribute('name', STD_NAME)
850 oldTestNode = self.testElem
851 self.testElem = elem
852 self.mainNode.replaceChild(elem, oldTestNode)
853 oldTestNode.unlink()
854 # Replace wizard page class temporarily
855 if xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:
856 oldCl = elem.getAttribute('class')
857 elem.setAttribute('class', 'wxPanel')
858 parent = elem.parentNode
859 encd = self.rootObj.params['encoding'].value()
860 if not encd: encd = None
861 try:
862 self.dom.writexml(memFile, encoding=encd)
863 except:
864 inf = sys.exc_info()
865 wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1])
866 wxLogError('Error writing temporary file')
867 if debug: raise
868 memFile.close() # write to wxMemoryFS
869 xmlFlags = wxXRC_NO_SUBCLASSING
870 # Use translations if encoding is not specified
871 if not g.currentEncoding:
872 xmlFlags != wxXRC_USE_LOCALE
873 res = wxXmlResource('', xmlFlags)
874 res.Load('memory:xxx.xrc')
875 try:
876 if xxx.__class__ == xxxFrame:
877 # Frame can't have many children,
878 # but it's first child possibly can...
879 # child = self.GetFirstChild(item)[0]
880 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
881 # # Clean-up before recursive call or error
882 # wxMemoryFSHandler_RemoveFile('xxx.xrc')
883 # wxEndBusyCursor()
884 # self.CreateTestWin(child)
885 # return
886 # This currently works under GTK, but not under MSW
887 testWin = g.testWin = wxPreFrame()
888 res.LoadOnFrame(testWin, g.frame, STD_NAME)
889 # Create status bar
890 testWin.panel = testWin
891 #testWin.CreateStatusBar()
892 testWin.SetClientSize(testWin.GetBestSize())
893 testWin.SetPosition(pos)
894 testWin.Show(True)
895 elif xxx.__class__ == xxxPanel:
896 # Create new frame
897 if not testWin:
898 testWin = g.testWin = wxFrame(g.frame, -1, 'Panel: ' + name,
899 pos=pos, name=STD_NAME)
900 testWin.panel = res.LoadPanel(testWin, STD_NAME)
901 testWin.SetClientSize(testWin.GetBestSize())
902 testWin.Show(True)
903 elif xxx.__class__ == xxxDialog:
904 testWin = g.testWin = res.LoadDialog(None, STD_NAME)
905 testWin.panel = testWin
906 testWin.Layout()
907 testWin.SetPosition(pos)
908 testWin.Show(True)
909 # Dialog's default code does not produce EVT_CLOSE
910 EVT_BUTTON(testWin, wxID_OK, self.OnCloseTestWin)
911 EVT_BUTTON(testWin, wxID_CANCEL, self.OnCloseTestWin)
912 elif xxx.__class__ == xxxWizard:
913 wiz = wxPreWizard()
914 res.LoadOnObject(wiz, None, STD_NAME, 'wxWizard')
915 # Find first page (don't know better way)
916 firstPage = None
917 for w in wiz.GetChildren():
918 if isinstance(w, wxWizardPage):
919 firstPage = w
920 break
921 if not firstPage:
922 wxLogError('Wizard is empty')
923 else:
924 # Wizard should be modal
925 self.SetItemBold(item)
926 wiz.RunWizard(w)
927 self.SetItemBold(item, False)
928 wiz.Destroy()
929 elif xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:
930 # Create new frame
931 if not testWin:
932 testWin = g.testWin = wxFrame(g.frame, -1, 'Wizard page: ' + name,
933 pos=pos, name=STD_NAME)
934 testWin.panel = wxPrePanel()
935 res.LoadOnObject(testWin.panel, testWin, STD_NAME, 'wxPanel')
936 testWin.SetClientSize(testWin.GetBestSize())
937 testWin.Show(True)
938 elif xxx.__class__ == xxxMenuBar:
939 testWin = g.testWin = wxFrame(g.frame, -1, 'MenuBar: ' + name,
940 pos=pos, name=STD_NAME)
941 testWin.panel = None
942 # Set status bar to display help
943 testWin.CreateStatusBar()
944 testWin.menuBar = res.LoadMenuBar(STD_NAME)
945 testWin.SetMenuBar(testWin.menuBar)
946 testWin.Show(True)
947 elif xxx.__class__ == xxxToolBar:
948 testWin = g.testWin = wxFrame(g.frame, -1, 'ToolBar: ' + name,
949 pos=pos, name=STD_NAME)
950 testWin.panel = None
951 # Set status bar to display help
952 testWin.CreateStatusBar()
953 testWin.toolBar = res.LoadToolBar(testWin, STD_NAME)
954 testWin.SetToolBar(testWin.toolBar)
955 testWin.Show(True)
956 if testWin:
957 testWin.item = item
958 EVT_CLOSE(testWin, self.OnCloseTestWin)
959 testWin.highLight = None
960 if highLight and not self.pendingHighLight:
961 self.HighLight(highLight)
962 except:
963 if g.testWin:
964 self.SetItemBold(item, False)
965 g.testWinPos = g.testWin.GetPosition()
966 g.testWin.Destroy()
967 g.testWin = None
968 inf = sys.exc_info()
969 wxLogError(traceback.format_exception(inf[0], inf[1], None)[-1])
970 wxLogError('Error loading resource')
971 wxMemoryFSHandler_RemoveFile('xxx.xrc')
972
973 def CloseTestWindow(self):
974 if not g.testWin: return
975 self.SetItemBold(g.testWin.item, False)
976 g.frame.tb.ToggleTool(g.frame.ID_TOOL_LOCATE, False)
977 g.testWinPos = g.testWin.GetPosition()
978 g.testWin.Destroy()
979 g.testWin = None
980
981 def OnCloseTestWin(self, evt):
982 self.CloseTestWindow()
983
984 # Return item index in parent
985 def ItemIndex(self, item):
986 n = 0 # index of sibling
987 prev = self.GetPrevSibling(item)
988 while prev.IsOk():
989 prev = self.GetPrevSibling(prev)
990 n += 1
991 return n
992
993 # Full tree index of an item - list of positions
994 def ItemFullIndex(self, item):
995 if not item.IsOk(): return None
996 l = []
997 while item != self.root:
998 l.insert(0, self.ItemIndex(item))
999 item = self.GetItemParent(item)
1000 return l
1001 # Get item position from full index
1002 def ItemAtFullIndex(self, index):
1003 if index is None: return wxTreeItemId()
1004 item = self.root
1005 for i in index:
1006 item = self.GetFirstChild(item)[0]
1007 for k in range(i): item = self.GetNextSibling(item)
1008 return item
1009
1010 # True if next item should be inserted after current (vs. appended to it)
1011 def NeedInsert(self, item):
1012 xxx = self.GetPyData(item)
1013 if item == self.root: return False # root item
1014 if xxx.hasChildren and not self.GetChildrenCount(item, False):
1015 return False
1016 return not (self.IsExpanded(item) and self.GetChildrenCount(item, False))
1017
1018 # Override to use like single-selection tree
1019 def GetSelection(self):
1020 return self.selection
1021 def SelectItem(self, item):
1022 self.UnselectAll()
1023 self.ChangeSelection(item)
1024 wxTreeCtrl.SelectItem(self, item)
1025
1026 # Pull-down
1027 def OnRightDown(self, evt):
1028 pullDownMenu = g.pullDownMenu
1029 # select this item
1030 pt = evt.GetPosition();
1031 item, flags = self.HitTest(pt)
1032 if item.Ok() and flags & wxTREE_HITTEST_ONITEM:
1033 self.SelectItem(item)
1034
1035 # Setup menu
1036 menu = wxMenu()
1037
1038 item = self.selection
1039 if not item:
1040 menu.Append(g.pullDownMenu.ID_EXPAND, 'Expand', 'Expand tree')
1041 menu.Append(g.pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse tree')
1042 else:
1043 # self.ctrl = evt.ControlDown() # save Ctrl state
1044 # self.shift = evt.ShiftDown() # and Shift too
1045 m = wxMenu() # create menu
1046 if self.ctrl:
1047 needInsert = True
1048 else:
1049 needInsert = self.NeedInsert(item)
1050 if item == self.root or needInsert and self.GetItemParent(item) == self.root:
1051 SetMenu(m, pullDownMenu.topLevel)
1052 m.AppendSeparator()
1053 m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
1054 else:
1055 xxx = self.GetPyData(item).treeObject()
1056 # Check parent for possible child nodes if inserting sibling
1057 if needInsert: xxx = xxx.parent
1058 if xxx.__class__ == xxxMenuBar:
1059 m.Append(ID_NEW.MENU, 'Menu', 'Create menu')
1060 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
1061 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
1062 SetMenu(m, pullDownMenu.toolBarControls)
1063 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
1064 SetMenu(m, pullDownMenu.menuControls)
1065 elif xxx.__class__ == xxxStdDialogButtonSizer:
1066 SetMenu(m, pullDownMenu.stdButtons)
1067 else:
1068 SetMenu(m, pullDownMenu.controls)
1069 if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
1070 m.Enable(m.FindItem('sizer'), False)
1071 elif not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
1072 m.Enable(ID_NEW.SPACER, False)
1073 m.AppendSeparator()
1074 m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
1075 # Select correct label for create menu
1076 if not needInsert:
1077 if self.shift:
1078 menu.AppendMenu(wxNewId(), 'Insert Child', m,
1079 'Create child object as the first child')
1080 else:
1081 menu.AppendMenu(wxNewId(), 'Append Child', m,
1082 'Create child object as the last child')
1083 else:
1084 if self.shift:
1085 menu.AppendMenu(wxNewId(), 'Create Sibling', m,
1086 'Create sibling before selected object')
1087 else:
1088 menu.AppendMenu(wxNewId(), 'Create Sibling', m,
1089 'Create sibling after selected object')
1090 # Build replace menu
1091 if item != self.root:
1092 xxx = self.GetPyData(item).treeObject()
1093 m = wxMenu() # create replace menu
1094 if xxx.__class__ == xxxMenuBar:
1095 m.Append(1000 + ID_NEW.MENU, 'Menu', 'Create menu')
1096 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
1097 SetMenu(m, pullDownMenu.menuControls, shift=True)
1098 elif xxx.__class__ == xxxToolBar and \
1099 self.GetItemParent(item) == self.root:
1100 SetMenu(m, [], shift=True)
1101 elif xxx.__class__ in [xxxFrame, xxxDialog, xxxPanel]:
1102 SetMenu(m, [
1103 (ID_NEW.PANEL, 'Panel', 'Create panel'),
1104 (ID_NEW.DIALOG, 'Dialog', 'Create dialog'),
1105 (ID_NEW.FRAME, 'Frame', 'Create frame')], shift=True)
1106 elif xxx.isSizer:
1107 SetMenu(m, pullDownMenu.sizers, shift=True)
1108 else:
1109 SetMenu(m, pullDownMenu.controls, shift=True)
1110 id = wxNewId()
1111 menu.AppendMenu(id, 'Replace With', m)
1112 if not m.GetMenuItemCount(): menu.Enable(id, False)
1113 menu.Append(pullDownMenu.ID_SUBCLASS, 'Subclass...',
1114 'Set "subclass" property')
1115 menu.AppendSeparator()
1116 # Not using standart IDs because we don't want to show shortcuts
1117 menu.Append(wxID_CUT, 'Cut', 'Cut to the clipboard')
1118 menu.Append(wxID_COPY, 'Copy', 'Copy to the clipboard')
1119 if self.ctrl and item != self.root:
1120 menu.Append(pullDownMenu.ID_PASTE_SIBLING, 'Paste Sibling',
1121 'Paste from the clipboard as a sibling')
1122 else:
1123 menu.Append(wxID_PASTE, 'Paste', 'Paste from the clipboard')
1124 menu.Append(pullDownMenu.ID_DELETE,
1125 'Delete', 'Delete object')
1126 if self.ItemHasChildren(item):
1127 menu.AppendSeparator()
1128 menu.Append(pullDownMenu.ID_EXPAND, 'Expand', 'Expand subtree')
1129 menu.Append(pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse subtree')
1130 self.PopupMenu(menu, evt.GetPosition())
1131 menu.Destroy()
1132
1133 # Apply changes
1134 def Apply(self, xxx, item):
1135 g.panel.Apply()
1136 # Update tree view
1137 xxx = xxx.treeObject()
1138 if xxx.hasName and self.GetItemText(item) != xxx.name:
1139 self.SetItemText(item, xxx.treeName())
1140 # Item width may have changed
1141 # !!! Tric to update tree width (wxGTK, ??)
1142 self.SetIndent(self.GetIndent())
1143 # Change tree icon for sizers
1144 if isinstance(xxx, xxxBoxSizer):
1145 self.SetItemImage(item, xxx.treeImage())
1146 # Set global modified state
1147 g.frame.SetModified()
1148