]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tree.py
another fix for undo/redo
[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 = wx.GetDefaultPyEncoding()
26 try:
27 self.buffer += data.encode(encoding)
28 except UnicodeEncodeError:
29 self.buffer += data.encode(encoding, 'xmlcharrefreplace')
30
31 def close(self):
32 wx.MemoryFSHandler.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 = wx.NewId()
53 DIALOG = wx.NewId()
54 FRAME = wx.NewId()
55 TOOL_BAR = wx.NewId()
56 TOOL = wx.NewId()
57 MENU_BAR = wx.NewId()
58 MENU = wx.NewId()
59 STATUS_BAR = wx.NewId()
60
61 STATIC_TEXT = wx.NewId()
62 TEXT_CTRL = wx.NewId()
63
64 BUTTON = wx.NewId()
65 BITMAP_BUTTON = wx.NewId()
66 RADIO_BUTTON = wx.NewId()
67 SPIN_BUTTON = wx.NewId()
68 TOGGLE_BUTTON = wx.NewId()
69
70 STATIC_BOX = wx.NewId()
71 CHECK_BOX = wx.NewId()
72 RADIO_BOX = wx.NewId()
73 COMBO_BOX = wx.NewId()
74 LIST_BOX = wx.NewId()
75
76 STATIC_LINE = wx.NewId()
77 STATIC_BITMAP = wx.NewId()
78 CHOICE = wx.NewId()
79 SLIDER = wx.NewId()
80 GAUGE = wx.NewId()
81 SCROLL_BAR = wx.NewId()
82 TREE_CTRL = wx.NewId()
83 LIST_CTRL = wx.NewId()
84 CHECK_LIST = wx.NewId()
85 NOTEBOOK = wx.NewId()
86 CHOICEBOOK = wx.NewId()
87 LISTBOOK = wx.NewId()
88 SPLITTER_WINDOW = wx.NewId()
89 GRID = 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()
97 UNKNOWN = wx.NewId()
98 WIZARD = wx.NewId()
99 WIZARD_PAGE = wx.NewId()
100 WIZARD_PAGE_SIMPLE = wx.NewId()
101 BITMAP = wx.NewId()
102 ICON = wx.NewId()
103 STATUS_BAR = wx.NewId()
104
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()
111 SPACER = wx.NewId()
112
113 TOOL_BAR = wx.NewId()
114 TOOL = wx.NewId()
115 MENU = wx.NewId()
116 MENU_ITEM = wx.NewId()
117 SEPARATOR = wx.NewId()
118
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()
127
128 REF = wx.NewId()
129 COMMENT = wx.NewId()
130
131 CUSTOM = wx.NewId()
132 for i in range(99): wx.NewId() # reserve IDs for custom controls
133
134 LAST = wx.NewId()
135
136
137
138 class PullDownMenu:
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()
144
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)
155
156 # Mapping from IDs to element names
157 self.createMap = {
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',
165 ID_NEW.TOOL: 'tool',
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',
173
174 ID_NEW.STATIC_TEXT: 'wxStaticText',
175 ID_NEW.TEXT_CTRL: 'wxTextCtrl',
176
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',
182
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',
189
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',
210
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',
219
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',
228 }
229 self.topLevel = [
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'),
234 None,
235 (ID_NEW.TOOL_BAR, 'ToolBar', 'Create toolbar'),
236 (ID_NEW.MENU_BAR, 'MenuBar', 'Create menubar'),
237 (ID_NEW.MENU, 'Menu', 'Create menu'),
238 None,
239 (ID_NEW.BITMAP, 'Bitmap', 'Create bitmap'),
240 (ID_NEW.ICON, 'Icon', 'Create icon'),
241 ]
242 self.containers = [
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'),
252 ]
253 self.sizers = [
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'),
265 ]
266 self.controls = [
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'),
287 ],
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'),
294 ],
295 ['box', 'Boxes',
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'),
302 ],
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'),
314 ],
315 ['sizer', 'Sizers',
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'),
327 ]
328 ]
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'),
333 ]
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'),
347 ],
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'),
353 ],
354 ['box', 'Boxes',
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'),
361 ],
362 ]
363 self.stdButtons = [
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'),
372 ]
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'),
382 }
383 self.custom = ['custom', 'User-defined controls']
384 self.customMap = {}
385
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
390
391
392 ################################################################################
393
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):
400 for l in list:
401 if type(l) == types.TupleType:
402 # Shift ID
403 if shift: l = (1000 + l[0],) + l[1:]
404 apply(m.Append, l)
405 elif type(l) == types.ListType:
406 subMenu = wx.Menu()
407 SetMenu(subMenu, l[2:], shift)
408 m.AppendMenu(wx.NewId(), l[0], subMenu, l[1])
409 else: # separator
410 m.AppendSeparator()
411
412 ################################################################################
413
414 class HighLightBox:
415 def __init__(self, pos, size):
416 if size.width == -1: size.width = 0
417 if size.height == -1: size.height = 0
418 w = g.testWin.panel
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]
428 self.size = size
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)
437 self.size = size
438 # Remove it
439 def Remove(self):
440 map(wx.Window.Destroy, self.lines)
441 g.testWin.highLight = None
442 def Refresh(self):
443 map(wx.Window.Refresh, self.lines)
444
445 ################################################################################
446
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(),
454 wx.FONTFLAG_ITALIC)
455 # Register events
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)
460 else:
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)
467
468 self.selection = None
469 self.selectionChanging = False
470 self.needUpdate = False
471 self.pendingHighLight = None
472 self.ctrl = self.shift = False
473 self.dom = None
474 # Create image list
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())
494 self.il = il
495 self.SetImageList(il)
496
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)
502
503 def ExpandAll(self, item):
504 if self.ItemHasChildren(item):
505 self.Expand(item)
506 i, cookie = self.GetFirstChild(item)
507 children = []
508 while i.IsOk():
509 children.append(i)
510 i, cookie = self.GetNextChild(item, cookie)
511 for i in children:
512 self.ExpandAll(i)
513 def CollapseAll(self, item):
514 if self.ItemHasChildren(item):
515 i, cookie = self.GetFirstChild(item)
516 children = []
517 while i.IsOk():
518 children.append(i)
519 i, cookie = self.GetNextChild(item, cookie)
520 for i in children:
521 self.CollapseAll(i)
522 self.Collapse(item)
523
524 # Clear tree
525 def Clear(self):
526 self.selection = None
527 self.UnselectAll()
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')
533 # Create main 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)
543
544 # Clear old data and set new
545 def SetData(self, dom):
546 self.selection = None
547 self.UnselectAll()
548 self.DeleteAllItems()
549 # Add minimal structure
550 if self.dom: self.dom.unlink()
551 self.dom = dom
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[:]
560 for node in nodes:
561 if IsObject(node):
562 self.AddNode(self.root, None, node)
563 else:
564 self.mainNode.removeChild(node)
565 node.unlink()
566 if self.mainNode.firstChild:
567 self.testElem = self.dom.createElement('dummy')
568 self.mainNode.insertBefore(self.testElem, self.mainNode.firstChild)
569 else:
570 self.testElem = self.dom.createElement('dummy')
571 self.mainNode.appendChild(self.testElem)
572 self.Expand(self.root)
573
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
578 try:
579 xxx = MakeXXXFromDOM(xxxParent, node)
580 except:
581 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent, node)
582 raise
583 treeObj = xxx.treeObject()
584 # Append tree item
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)
592 elif treeObj.ref:
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[:]
599 for n in nodes:
600 if IsObject(n):
601 self.AddNode(item, treeObj, n)
602 elif n.nodeType != minidom.Node.ELEMENT_NODE:
603 treeObj.node.removeChild(n)
604 n.unlink()
605
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
611 if nextItem.IsOk():
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)
619 else:
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)
628 elif treeObj.ref:
629 self.SetItemTextColour(newItem, 'DarkGreen')
630 elif treeObj.hasStyle and treeObj.params.get('hidden', False):
631 self.SetItemTextColour(newItem, 'Grey')
632 # Add children items
633 if xxx.hasChildren:
634 treeObj = xxx.treeObject()
635 for n in treeObj.node.childNodes:
636 if IsObject(n):
637 self.AddNode(newItem, treeObj, n)
638 return newItem
639
640 # Remove leaf of tree, return it's data object
641 def RemoveLeaf(self, leaf):
642 xxx = self.GetPyData(leaf)
643 node = xxx.node
644 parent = node.parentNode
645 parent.removeChild(node)
646 self.Delete(leaf)
647 # Reset selection object
648 self.selection = None
649 return node
650
651 # Find position relative to the top-level window
652 def FindNodePos(self, item, obj=None):
653 # Root at (0,0)
654 if item == g.testWin.item: return wx.Point(0, 0)
655 itemParent = self.GetItemParent(item)
656 # Select book page
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)
661 # Find position
662 for i in range(book.GetPageCount()):
663 if book.GetPage(i) == obj:
664 if book.GetSelection() != i:
665 book.SetSelection(i)
666 # Remove highlight - otherwise highlight window won't be visible
667 if g.testWin.highLight:
668 g.testWin.highLight.Remove()
669 break
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
684
685 # Find window (or sizer) corresponding to a tree item.
686 def FindNodeObject(self, item):
687 testWin = g.testWin
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
702 return parentWin
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()
708 return None
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()
719 return child
720
721 def OnSelChanged(self, evt):
722 if self.selectionChanging: return
723 self.selectionChanging = True
724 self.UnselectAll()
725 self.SelectItem(evt.GetItem())
726 self.selectionChanging = False
727
728 def ChangeSelection(self, item):
729 # Apply changes
730 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
731 #oldItem = evt.GetOldItem()
732 status = ''
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)
740 if g.testWin:
741 if g.testWin.highLight:
742 g.testWin.highLight.Remove()
743 self.needUpdate = True
744 status = 'Changes were applied'
745 g.frame.SetStatusText(status)
746 # Generate view
747 self.selection = item
748 if not self.selection.IsOk():
749 self.selection = None
750 return
751 xxx = self.GetPyData(self.selection)
752 # Update panel
753 g.panel.SetData(xxx)
754 # Update tools
755 g.tools.UpdateUI()
756 # Highlighting is done in OnIdle
757 self.pendingHighLight = self.selection
758
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:
765 return True
766 return False
767
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]:
774 return
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()
778 return
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()
785 return
786 pos = self.FindNodePos(item, obj)
787 size = obj.GetSize()
788 # Highlight
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:
794 hl.Remove()
795 hl = None
796 if hl:
797 hl.Replace(pos, size)
798 else:
799 g.testWin.highLight = hl = HighLightBox(pos, size)
800 hl.Refresh()
801 hl.item = item
802
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']
810 originalItem = item
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)')
816 return
817 # Show item in bold
818 if g.testWin: # Reset old
819 self.UnselectAll()
820 self.SetItemBold(g.testWin.item, False)
821 try:
822 wx.BeginBusyCursor()
823 self.CreateTestWin(item)
824 finally:
825 wx.EndBusyCursor()
826 # Maybe an error occurred, so we need to test
827 if g.testWin:
828 self.SetItemBold(g.testWin.item)
829 # Select original item
830 self.ChangeSelection(originalItem)
831
832 # Double-click on Linux
833 def OnItemActivated(self, evt):
834 if evt.GetItem() != self.root:
835 self.ShowTestWindow(evt.GetItem())
836
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)
842 else:
843 evt.Skip()
844
845 def OnItemExpandedCollapsed(self, evt):
846 # Update tool palette
847 g.tools.UpdateUI()
848 evt.Skip()
849
850 # (re)create test window
851 def CreateTestWin(self, item):
852 testWin = g.testWin
853 # Create a window with this resource
854 xxx = self.GetPyData(item).treeObject()
855 # Close old window, remember where it was
856 highLight = None
857 if testWin:
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()
868 testWin.panel = None
869 else:
870 testWin.Destroy()
871 testWin = g.testWin = None
872 else:
873 testWin.Destroy()
874 testWin = g.testWin = None
875 else:
876 pos = g.testWinPos
877 # Save in memory FS
878 memFile = MemoryFile('xxx.xrc')
879 # Create memory XML file
880 elem = xxx.node.cloneNode(True)
881 if not xxx.name:
882 name = 'noname'
883 else:
884 name = xxx.name
885 elem.setAttribute('name', STD_NAME)
886 oldTestNode = self.testElem
887 self.testElem = elem
888 self.mainNode.replaceChild(elem, oldTestNode)
889 oldTestNode.unlink()
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
897 try:
898 self.dom.writexml(memFile, encoding=encd)
899 except:
900 inf = sys.exc_info()
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
910 # Register handlers
911 addHandlers()
912 # Same module list
913 res.Load('memory:xxx.xrc')
914 try:
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')
922 # wx.EndBusyCursor()
923 # self.CreateTestWin(child)
924 # return
925 # This currently works under GTK, but not under MSW
926 testWin = g.testWin = wx.PreFrame()
927 res.LoadOnFrame(testWin, g.frame, STD_NAME)
928 # Create status bar
929 testWin.panel = testWin
930 #testWin.CreateStatusBar()
931 testWin.SetClientSize(testWin.GetBestSize())
932 testWin.SetPosition(pos)
933 testWin.Show(True)
934 elif xxx.__class__ == xxxPanel:
935 # Create new frame
936 if not testWin:
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())
941 testWin.Show(True)
942 elif xxx.__class__ == xxxDialog:
943 testWin = g.testWin = res.LoadDialog(g.frame, STD_NAME)
944 testWin.panel = testWin
945 testWin.Layout()
946 testWin.SetPosition(pos)
947 testWin.Show(True)
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)
955 firstPage = None
956 for w in wiz.GetChildren():
957 if isinstance(w, wx.wizard.WizardPage):
958 firstPage = w
959 break
960 if not firstPage:
961 wx.LogError('Wizard is empty')
962 else:
963 # Wizard should be modal
964 self.SetItemBold(item)
965 wiz.RunWizard(w)
966 self.SetItemBold(item, False)
967 wiz.Destroy()
968 elif xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:
969 # Create new frame
970 if not testWin:
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())
976 testWin.Show(True)
977 elif xxx.__class__ == xxxMenuBar:
978 testWin = g.testWin = wx.Frame(g.frame, -1, 'MenuBar: ' + name,
979 pos=pos, name=STD_NAME)
980 testWin.panel = None
981 # Set status bar to display help
982 testWin.CreateStatusBar()
983 testWin.menuBar = res.LoadMenuBar(STD_NAME)
984 testWin.SetMenuBar(testWin.menuBar)
985 testWin.Show(True)
986 elif xxx.__class__ == xxxToolBar:
987 testWin = g.testWin = wx.Frame(g.frame, -1, 'ToolBar: ' + name,
988 pos=pos, name=STD_NAME)
989 testWin.panel = None
990 # Set status bar to display help
991 testWin.CreateStatusBar()
992 testWin.toolBar = res.LoadToolBar(testWin, STD_NAME)
993 testWin.SetToolBar(testWin.toolBar)
994 testWin.Show(True)
995 # Catch some events, set highlight
996 if testWin:
997 testWin.item = item
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)
1003 except:
1004 if g.testWin:
1005 self.SetItemBold(item, False)
1006 g.testWinPos = g.testWin.GetPosition()
1007 g.testWin.Destroy()
1008 g.testWin = None
1009 inf = sys.exc_info()
1010 wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
1011 wx.LogError('Error loading resource')
1012 # Cleanup
1013 res.Unload('xxx.xrc')
1014 xrc.XmlResource.Set(None)
1015 wx.MemoryFSHandler.RemoveFile('xxx.xrc')
1016
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()
1022 g.testWin.Destroy()
1023 g.testWin = None
1024
1025 def OnCloseTestWin(self, evt):
1026 self.CloseTestWindow()
1027
1028 def OnSizeTestWin(self, evt):
1029 if g.testWin.highLight:
1030 self.HighLight(g.testWin.highLight.item)
1031 evt.Skip()
1032
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)
1037 while prev.IsOk():
1038 # MenuBar is not a child
1039 if not isinstance(self.GetPyData(prev), xxxMenuBar):
1040 n += 1
1041 prev = self.GetPrevSibling(prev)
1042 return n
1043
1044 # Return item index in parent
1045 def ItemIndex(self, item):
1046 n = 0 # index of sibling
1047 prev = self.GetPrevSibling(item)
1048 while prev.IsOk():
1049 prev = self.GetPrevSibling(prev)
1050 n += 1
1051 return n
1052
1053 # Full tree index of an item - list of positions
1054 def ItemFullIndex(self, item):
1055 if not item.IsOk(): return None
1056 l = []
1057 while item != self.root:
1058 l.insert(0, self.ItemIndex(item))
1059 item = self.GetItemParent(item)
1060 return l
1061 # Get item position from full index
1062 def ItemAtFullIndex(self, index):
1063 if index is None: return wx.TreeItemId()
1064 item = self.root
1065 for i in index:
1066 item = self.GetFirstChild(item)[0]
1067 for k in range(i): item = self.GetNextSibling(item)
1068 return item
1069
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):
1075 return False
1076 return not (self.IsExpanded(item) and self.GetChildrenCount(item, False))
1077
1078 # Override to use like single-selection tree
1079 def GetSelection(self):
1080 return self.selection
1081 def SelectItem(self, item):
1082 self.UnselectAll()
1083 self.ChangeSelection(item)
1084 wx.TreeCtrl.SelectItem(self, item)
1085
1086 # Pull-down
1087 def OnRightDown(self, evt):
1088 pullDownMenu = g.pullDownMenu
1089 # select this item
1090 pt = evt.GetPosition();
1091 item, flags = self.HitTest(pt)
1092 if item.Ok() and flags & wx.TREE_HITTEST_ONITEM:
1093 self.SelectItem(item)
1094
1095 # Setup menu
1096 menu = wx.Menu()
1097
1098 item = self.selection
1099 if not item:
1100 menu.Append(g.pullDownMenu.ID_EXPAND, 'Expand', 'Expand tree')
1101 menu.Append(g.pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse tree')
1102 else:
1103 # self.ctrl = evt.ControlDown() # save Ctrl state
1104 # self.shift = evt.ShiftDown() # and Shift too
1105 m = wx.Menu() # create menu
1106 if self.ctrl:
1107 needInsert = True
1108 else:
1109 needInsert = self.NeedInsert(item)
1110 if item == self.root or needInsert and self.GetItemParent(item) == self.root:
1111 SetMenu(m, pullDownMenu.topLevel)
1112 m.AppendSeparator()
1113 m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
1114 m.Append(ID_NEW.COMMENT, 'comment', 'Create comment node')
1115 else:
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)
1128 else:
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])
1139 m.AppendSeparator()
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
1143 if not needInsert:
1144 if self.shift:
1145 menu.AppendMenu(wx.NewId(), 'Insert Child', m,
1146 'Create child object as the first child')
1147 else:
1148 menu.AppendMenu(wx.NewId(), 'Append Child', m,
1149 'Create child object as the last child')
1150 else:
1151 if self.shift:
1152 menu.AppendMenu(wx.NewId(), 'Create Sibling', m,
1153 'Create sibling before selected object')
1154 else:
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]:
1169 SetMenu(m, [
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)
1175 else:
1176 SetMenu(m, pullDownMenu.controls, shift=True)
1177 if xxx.isElement:
1178 id = wx.NewId()
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')
1190 else:
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())
1199 menu.Destroy()
1200
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())
1206
1207 # Apply changes
1208 def Apply(self, xxx, item):
1209 g.panel.Apply()
1210 # Update tree view
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()
1224
1225 def OnBeginLabelEdit(self, evt):
1226 xxx = self.GetPyData(evt.GetItem())
1227 if xxx.isElement:
1228 evt.Veto()
1229 else:
1230 evt.Skip()
1231
1232 def OnEndLabelEdit(self, evt):
1233 xxx = self.GetPyData(evt.GetItem())
1234 node = xxx.node
1235 if not xxx.isElement:
1236 node.data = evt.GetLabel()
1237 g.panel.SetData(xxx)
1238 evt.Skip()