]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/tree.py
10169a9a6070ebe8dd12c5ec4ef8f837fc458079
[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.clearCustom()
384
385 def clearCustom(self):
386 # Custom controls
387 self.custom = [['custom', 'User-defined controls']]
388 self.customMap = {}
389
390 def addCustom(self, klass):
391 n = len(self.custom[0])-2
392 self.custom[0].append((ID_NEW.CUSTOM + n, klass))
393 self.customMap[ID_NEW.CUSTOM + n] = klass
394
395
396 ################################################################################
397
398 # Set menu to list items.
399 # Each menu command is a tuple (id, label, help)
400 # submenus are lists [id, label, help, submenu]
401 # and separators are any other type. Shift is for making
402 # alternative sets of IDs. (+1000).
403 def SetMenu(m, list, shift=False):
404 for l in list:
405 if type(l) == types.TupleType:
406 # Shift ID
407 if shift: l = (1000 + l[0],) + l[1:]
408 apply(m.Append, l)
409 elif type(l) == types.ListType:
410 subMenu = wx.Menu()
411 SetMenu(subMenu, l[2:], shift)
412 m.AppendMenu(wx.NewId(), l[0], subMenu, l[1])
413 else: # separator
414 m.AppendSeparator()
415
416 ################################################################################
417
418 class HighLightBox:
419 def __init__(self, pos, size):
420 if size.width == -1: size.width = 0
421 if size.height == -1: size.height = 0
422 w = g.testWin.panel
423 l1 = wx.Window(w, -1, pos, wx.Size(size.width, 2))
424 l1.SetBackgroundColour(wx.RED)
425 l2 = wx.Window(w, -1, pos, wx.Size(2, size.height))
426 l2.SetBackgroundColour(wx.RED)
427 l3 = wx.Window(w, -1, wx.Point(pos.x + size.width - 2, pos.y), wx.Size(2, size.height))
428 l3.SetBackgroundColour(wx.RED)
429 l4 = wx.Window(w, -1, wx.Point(pos.x, pos.y + size.height - 2), wx.Size(size.width, 2))
430 l4.SetBackgroundColour(wx.RED)
431 self.lines = [l1, l2, l3, l4]
432 # Move highlight to a new position
433 def Replace(self, pos, size):
434 if size.width == -1: size.width = 0
435 if size.height == -1: size.height = 0
436 self.lines[0].SetDimensions(pos.x, pos.y, size.width, 2)
437 self.lines[1].SetDimensions(pos.x, pos.y, 2, size.height)
438 self.lines[2].SetDimensions(pos.x + size.width - 2, pos.y, 2, size.height)
439 self.lines[3].SetDimensions(pos.x, pos.y + size.height - 2, size.width, 2)
440 # Remove it
441 def Remove(self):
442 map(wx.Window.Destroy, self.lines)
443 g.testWin.highLight = None
444 def Refresh(self):
445 map(wx.Window.Refresh, self.lines)
446
447 ################################################################################
448
449 class XML_Tree(wx.TreeCtrl):
450 def __init__(self, parent, id):
451 wx.TreeCtrl.__init__(self, parent, id,
452 style = wx.TR_HAS_BUTTONS | wx.TR_MULTIPLE | wx.TR_EDIT_LABELS)
453 self.SetBackgroundColour(wx.Colour(224, 248, 224))
454 self.fontComment = wx.FFont(self.GetFont().GetPointSize(),
455 self.GetFont().GetFamily(),
456 wx.FONTFLAG_ITALIC)
457 # Register events
458 wx.EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
459 # One works on Linux, another on Windows
460 if wx.Platform == '__WXGTK__':
461 wx.EVT_TREE_ITEM_ACTIVATED(self, self.GetId(), self.OnItemActivated)
462 else:
463 wx.EVT_LEFT_DCLICK(self, self.OnDClick)
464 wx.EVT_RIGHT_DOWN(self, self.OnRightDown)
465 wx.EVT_TREE_ITEM_EXPANDED(self, self.GetId(), self.OnItemExpandedCollapsed)
466 wx.EVT_TREE_ITEM_COLLAPSED(self, self.GetId(), self.OnItemExpandedCollapsed)
467 self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnBeginLabelEdit)
468 self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnEndLabelEdit)
469
470 self.selection = None
471 self.selectionChanging = False
472 self.needUpdate = False
473 self.pendingHighLight = None
474 self.ctrl = self.shift = False
475 self.dom = None
476 # Create image list
477 il = wx.ImageList(16, 16, True)
478 self.rootImage = il.Add(images.getTreeRootImage().Scale(16,16).ConvertToBitmap())
479 xxxComment.image = il.Add(images.getTreeCommentImage().Scale(16,16).ConvertToBitmap())
480 xxxObject.image = il.Add(images.getTreeDefaultImage().Scale(16,16).ConvertToBitmap())
481 xxxPanel.image = il.Add(images.getTreePanelImage().Scale(16,16).ConvertToBitmap())
482 xxxDialog.image = il.Add(images.getTreeDialogImage().Scale(16,16).ConvertToBitmap())
483 xxxFrame.image = il.Add(images.getTreeFrameImage().Scale(16,16).ConvertToBitmap())
484 xxxMenuBar.image = il.Add(images.getTreeMenuBarImage().Scale(16,16).ConvertToBitmap())
485 xxxMenu.image = il.Add(images.getTreeMenuImage().Scale(16,16).ConvertToBitmap())
486 xxxMenuItem.image = il.Add(images.getTreeMenuItemImage().Scale(16,16).ConvertToBitmap())
487 xxxToolBar.image = il.Add(images.getTreeToolBarImage().Scale(16,16).ConvertToBitmap())
488 xxxTool.image = il.Add(images.getTreeToolImage().Scale(16,16).ConvertToBitmap())
489 xxxSeparator.image = il.Add(images.getTreeSeparatorImage().Scale(16,16).ConvertToBitmap())
490 xxxSizer.imageH = il.Add(images.getTreeSizerHImage().Scale(16,16).ConvertToBitmap())
491 xxxSizer.imageV = il.Add(images.getTreeSizerVImage().Scale(16,16).ConvertToBitmap())
492 xxxStaticBoxSizer.imageH = il.Add(images.getTreeStaticBoxSizerHImage().Scale(16,16).ConvertToBitmap())
493 xxxStaticBoxSizer.imageV = il.Add(images.getTreeStaticBoxSizerVImage().Scale(16,16).ConvertToBitmap())
494 xxxGridSizer.image = il.Add(images.getTreeSizerGridImage().Scale(16,16).ConvertToBitmap())
495 xxxFlexGridSizer.image = il.Add(images.getTreeSizerFlexGridImage().Scale(16,16).ConvertToBitmap())
496 self.il = il
497 self.SetImageList(il)
498
499 def RegisterKeyEvents(self):
500 wx.EVT_KEY_DOWN(self, g.tools.OnKeyDown)
501 wx.EVT_KEY_UP(self, g.tools.OnKeyUp)
502 wx.EVT_ENTER_WINDOW(self, g.tools.OnMouse)
503 wx.EVT_LEAVE_WINDOW(self, g.tools.OnMouse)
504
505 def ExpandAll(self, item):
506 if self.ItemHasChildren(item):
507 self.Expand(item)
508 i, cookie = self.GetFirstChild(item)
509 children = []
510 while i.IsOk():
511 children.append(i)
512 i, cookie = self.GetNextChild(item, cookie)
513 for i in children:
514 self.ExpandAll(i)
515 def CollapseAll(self, item):
516 if self.ItemHasChildren(item):
517 i, cookie = self.GetFirstChild(item)
518 children = []
519 while i.IsOk():
520 children.append(i)
521 i, cookie = self.GetNextChild(item, cookie)
522 for i in children:
523 self.CollapseAll(i)
524 self.Collapse(item)
525
526 # Clear tree
527 def Clear(self):
528 self.selection = None
529 self.UnselectAll()
530 self.DeleteAllItems()
531 # Add minimal structure
532 if self.dom: self.dom.unlink()
533 self.dom = MyDocument()
534 self.dummyNode = self.dom.createComment('dummy node')
535 # Create main node
536 self.mainNode = self.dom.createElement('resource')
537 self.dom.appendChild(self.mainNode)
538 self.rootObj = xxxMainNode(self.dom)
539 self.root = self.AddRoot('XML tree', self.rootImage,
540 data=wx.TreeItemData(self.rootObj))
541 self.SetItemHasChildren(self.root)
542 self.testElem = self.dom.createElement('dummy')
543 self.mainNode.appendChild(self.testElem)
544 self.Expand(self.root)
545
546 # Clear old data and set new
547 def SetData(self, dom):
548 self.selection = None
549 self.UnselectAll()
550 self.DeleteAllItems()
551 # Add minimal structure
552 if self.dom: self.dom.unlink()
553 self.dom = dom
554 self.dummyNode = self.dom.createComment('dummy node')
555 # Find 'resource' child, add it's children
556 self.mainNode = dom.documentElement
557 self.rootObj = xxxMainNode(self.dom)
558 self.root = self.AddRoot('XML tree', self.rootImage,
559 data=wx.TreeItemData(self.rootObj))
560 self.SetItemHasChildren(self.root)
561 nodes = self.mainNode.childNodes[:]
562 for node in nodes:
563 if IsObject(node):
564 self.AddNode(self.root, None, node)
565 else:
566 self.mainNode.removeChild(node)
567 node.unlink()
568 if self.mainNode.firstChild:
569 self.testElem = self.dom.createElement('dummy')
570 self.mainNode.insertBefore(self.testElem, self.mainNode.firstChild)
571 else:
572 self.testElem = self.dom.createElement('dummy')
573 self.mainNode.appendChild(self.testElem)
574 self.Expand(self.root)
575
576 # Add tree item for given parent item if node is DOM element node with
577 # object/object_ref tag. xxxParent is parent xxx object
578 def AddNode(self, itemParent, xxxParent, node):
579 # Set item data to current node
580 try:
581 xxx = MakeXXXFromDOM(xxxParent, node)
582 except:
583 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent, node)
584 raise
585 treeObj = xxx.treeObject()
586 # Append tree item
587 item = self.AppendItem(itemParent, treeObj.treeName(),
588 image=treeObj.treeImage(),
589 data=wx.TreeItemData(xxx))
590 # Different color for comments and references
591 if xxx.className == 'comment':
592 self.SetItemTextColour(item, 'Blue')
593 self.SetItemFont(item, self.fontComment)
594 elif treeObj.ref:
595 self.SetItemTextColour(item, 'DarkGreen')
596 elif treeObj.hasStyle and treeObj.params.get('hidden', False):
597 self.SetItemTextColour(item, 'Grey')
598 # Try to find children objects
599 if treeObj.hasChildren:
600 nodes = treeObj.node.childNodes[:]
601 for n in nodes:
602 if IsObject(n):
603 self.AddNode(item, treeObj, n)
604 elif n.nodeType != minidom.Node.ELEMENT_NODE:
605 treeObj.node.removeChild(n)
606 n.unlink()
607
608 # Insert new item at specific position
609 def InsertNode(self, itemParent, parent, elem, nextItem):
610 # Insert in XML tree and wxWin
611 xxx = MakeXXXFromDOM(parent, elem)
612 # If nextItem is None, we append to parent, otherwise insert before it
613 if nextItem.IsOk():
614 node = self.GetPyData(nextItem).node
615 parent.node.insertBefore(elem, node)
616 # Inserting before is difficult, se we insert after or first child
617 index = self.ItemIndex(nextItem)
618 newItem = self.InsertItemBefore(itemParent, index,
619 xxx.treeName(), image=xxx.treeImage())
620 self.SetPyData(newItem, xxx)
621 else:
622 parent.node.appendChild(elem)
623 newItem = self.AppendItem(itemParent, xxx.treeName(), image=xxx.treeImage(),
624 data=wx.TreeItemData(xxx))
625 treeObj = xxx.treeObject()
626 # Different color for references and comments
627 if xxx.className == 'comment':
628 self.SetItemTextColour(newItem, 'Blue')
629 self.SetItemFont(newItem, self.fontComment)
630 elif treeObj.ref:
631 self.SetItemTextColour(newItem, 'DarkGreen')
632 elif treeObj.hasStyle and treeObj.params.get('hidden', False):
633 self.SetItemTextColour(newItem, 'Grey')
634 # Add children items
635 if xxx.hasChildren:
636 treeObj = xxx.treeObject()
637 for n in treeObj.node.childNodes:
638 if IsObject(n):
639 self.AddNode(newItem, treeObj, n)
640 return newItem
641
642 # Remove leaf of tree, return it's data object
643 def RemoveLeaf(self, leaf):
644 xxx = self.GetPyData(leaf)
645 node = xxx.node
646 parent = node.parentNode
647 parent.removeChild(node)
648 self.Delete(leaf)
649 # Reset selection object
650 self.selection = None
651 return node
652
653 # Find position relative to the top-level window
654 def FindNodePos(self, item, obj=None):
655 # Root at (0,0)
656 if item == g.testWin.item: return wx.Point(0, 0)
657 itemParent = self.GetItemParent(item)
658 # Select book page
659 if not obj: obj = self.FindNodeObject(item)
660 if self.GetPyData(itemParent).treeObject().__class__ in \
661 [xxxNotebook, xxxChoicebook, xxxListbook]:
662 book = self.FindNodeObject(itemParent)
663 # Find position
664 for i in range(book.GetPageCount()):
665 if book.GetPage(i) == obj:
666 if book.GetSelection() != i:
667 book.SetSelection(i)
668 # Remove highlight - otherwise highlight window won't be visible
669 if g.testWin.highLight:
670 g.testWin.highLight.Remove()
671 break
672 # For sizers and notebooks we must select the first window-like parent
673 winParent = itemParent
674 while self.GetPyData(winParent).isSizer:
675 winParent = self.GetItemParent(winParent)
676 # Notebook children are layed out in a little strange way
677 # wxGTK places NB panels relative to the NB parent
678 if wx.Platform == '__WXGTK__':
679 if self.GetPyData(itemParent).treeObject().__class__ == xxxNotebook:
680 winParent = self.GetItemParent(winParent)
681 parentPos = self.FindNodePos(winParent)
682 pos = obj.GetPosition()
683 # Position (-1,-1) is really (0,0)
684 if pos == (-1,-1): pos = (0,0)
685 return parentPos + pos
686
687 # Find window (or sizer) corresponding to a tree item.
688 def FindNodeObject(self, item):
689 testWin = g.testWin
690 # If top-level, return testWin (or panel its panel)
691 if item == testWin.item: return testWin.panel
692 itemParent = self.GetItemParent(item)
693 xxx = self.GetPyData(item).treeObject()
694 parentWin = self.FindNodeObject(itemParent)
695 # Top-level sizer? return window's sizer
696 if xxx.isSizer and isinstance(parentWin, wx.Window):
697 return parentWin.GetSizer()
698 elif xxx.__class__ in [xxxMenu, xxxMenuItem, xxxSeparator]: return None
699 elif xxx.__class__ in [xxxToolBar, xxxMenuBar]:
700 # If it's the main toolbar or menubar, we can't really select it
701 if xxx.parent.__class__ == xxxFrame: return None
702 elif isinstance(xxx.parent, xxxToolBar):
703 # Select complete toolbar
704 return parentWin
705 elif isinstance(xxx.parent, xxxStdDialogButtonSizer):
706 # This sizer returns non-existing children
707 for ch in parentWin.GetChildren():
708 if ch.GetWindow() and ch.GetWindow().GetName() == xxx.name:
709 return ch.GetWindow()
710 return None
711 elif xxx.parent.__class__ in [xxxChoicebook, xxxListbook]:
712 # First window is controld
713 return parentWin.GetChildren()[self.ItemIndex(item)+1]
714 # Otherwise get parent's object and it's child
715 child = parentWin.GetChildren()[self.WindowIndex(item)]
716 # Return window or sizer for sizer items
717 if child.GetClassName() == 'wxSizerItem':
718 if child.IsWindow(): child = child.GetWindow()
719 elif child.IsSizer():
720 child = child.GetSizer()
721 return child
722
723 def OnSelChanged(self, evt):
724 if self.selectionChanging: return
725 self.selectionChanging = True
726 self.UnselectAll()
727 self.SelectItem(evt.GetItem())
728 self.selectionChanging = False
729
730 def ChangeSelection(self, item):
731 # Apply changes
732 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
733 #oldItem = evt.GetOldItem()
734 status = ''
735 oldItem = self.selection
736 # use GetItemParent as a way to determine if the itemId is still valid
737 if oldItem and self.GetItemParent(oldItem):
738 xxx = self.GetPyData(oldItem)
739 # If some data was modified, apply changes
740 if g.panel.IsModified():
741 self.Apply(xxx, oldItem)
742 if g.testWin:
743 if g.testWin.highLight:
744 g.testWin.highLight.Remove()
745 self.needUpdate = True
746 status = 'Changes were applied'
747 g.frame.SetStatusText(status)
748 # Generate view
749 self.selection = item
750 if not self.selection.IsOk():
751 self.selection = None
752 return
753 xxx = self.GetPyData(self.selection)
754 # Update panel
755 g.panel.SetData(xxx)
756 # Update tools
757 g.tools.UpdateUI()
758 # Highlighting is done in OnIdle
759 self.pendingHighLight = self.selection
760
761 # Check if item is in testWin subtree
762 def IsHighlatable(self, item):
763 if item == g.testWin.item: return False
764 while item != self.root:
765 item = self.GetItemParent(item)
766 if item == g.testWin.item:
767 return True
768 return False
769
770 # Highlight selected item
771 def HighLight(self, item):
772 self.pendingHighLight = None
773 # Can highlight only with some top-level windows
774 if not g.testWin or self.GetPyData(g.testWin.item).treeObject().__class__ \
775 not in [xxxDialog, xxxPanel, xxxFrame]:
776 return
777 # If a control from another window is selected, remove highlight
778 if not self.IsHighlatable(item):
779 if g.testWin.highLight: g.testWin.highLight.Remove()
780 return
781 # Get window/sizer object
782 obj = self.FindNodeObject(item)
783 xxx = self.GetPyData(item).treeObject()
784 # Remove existing HL if item not found or is hidden
785 if not obj or xxx.hasStyle and xxx.params.get('hidden', False):
786 if g.testWin.highLight: g.testWin.highLight.Remove()
787 return
788 pos = self.FindNodePos(item, obj)
789 size = obj.GetSize()
790 # Highlight
791 # Negative positions are not working quite well
792 if g.testWin.highLight:
793 g.testWin.highLight.Replace(pos, size)
794 else:
795 g.testWin.highLight = HighLightBox(pos, size)
796 g.testWin.highLight.Refresh()
797 g.testWin.highLight.item = item
798
799 def ShowTestWindow(self, item):
800 xxx = self.GetPyData(item)
801 if g.panel.IsModified():
802 self.Apply(xxx, item) # apply changes
803 availableViews = ['wxFrame', 'wxPanel', 'wxDialog',
804 'wxMenuBar', 'wxToolBar', 'wxWizard',
805 'wxWizardPageSimple']
806 originalItem = item
807 # Walk up the tree until we find an item that has a view
808 while item and self.GetPyData(item).treeObject().className not in availableViews:
809 item = self.GetItemParent(item)
810 if not item or not item.IsOk():
811 wx.LogMessage('No view for this element (yet)')
812 return
813 # Show item in bold
814 if g.testWin: # Reset old
815 self.UnselectAll()
816 self.SetItemBold(g.testWin.item, False)
817 try:
818 wx.BeginBusyCursor()
819 self.CreateTestWin(item)
820 finally:
821 wx.EndBusyCursor()
822 # Maybe an error occurred, so we need to test
823 if g.testWin:
824 self.SetItemBold(g.testWin.item)
825 # Select original item
826 self.ChangeSelection(originalItem)
827
828 # Double-click on Linux
829 def OnItemActivated(self, evt):
830 if evt.GetItem() != self.root:
831 self.ShowTestWindow(evt.GetItem())
832
833 # Double-click on Windows
834 def OnDClick(self, evt):
835 item, flags = self.HitTest(evt.GetPosition())
836 if flags in [wx.TREE_HITTEST_ONITEMBUTTON, wx.TREE_HITTEST_ONITEMLABEL]:
837 if item != self.root: self.ShowTestWindow(item)
838 else:
839 evt.Skip()
840
841 def OnItemExpandedCollapsed(self, evt):
842 # Update tool palette
843 g.tools.UpdateUI()
844 evt.Skip()
845
846 # (re)create test window
847 def CreateTestWin(self, item):
848 testWin = g.testWin
849 # Create a window with this resource
850 xxx = self.GetPyData(item).treeObject()
851 # Close old window, remember where it was
852 highLight = None
853 if testWin:
854 pos = testWin.GetPosition()
855 if item == testWin.item:
856 # Remember highlight if same top-level window
857 if testWin.highLight:
858 highLight = testWin.highLight.item
859 if xxx.className == 'wxPanel':
860 if testWin.highLight:
861 testWin.pendingHighLight = highLight
862 testWin.highLight.Remove()
863 testWin.panel.Destroy()
864 testWin.panel = None
865 else:
866 testWin.Destroy()
867 testWin = g.testWin = None
868 else:
869 testWin.Destroy()
870 testWin = g.testWin = None
871 else:
872 pos = g.testWinPos
873 # Save in memory FS
874 memFile = MemoryFile('xxx.xrc')
875 # Create memory XML file
876 elem = xxx.node.cloneNode(True)
877 if not xxx.name:
878 name = 'noname'
879 else:
880 name = xxx.name
881 elem.setAttribute('name', STD_NAME)
882 oldTestNode = self.testElem
883 self.testElem = elem
884 self.mainNode.replaceChild(elem, oldTestNode)
885 oldTestNode.unlink()
886 # Replace wizard page class temporarily
887 if xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:
888 oldCl = elem.getAttribute('class')
889 elem.setAttribute('class', 'wxPanel')
890 parent = elem.parentNode
891 encd = self.rootObj.params['encoding'].value()
892 if not encd: encd = None
893 try:
894 self.dom.writexml(memFile, encoding=encd)
895 except:
896 inf = sys.exc_info()
897 wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
898 wx.LogError('Error writing temporary file')
899 memFile.close() # write to wxMemoryFS
900 xmlFlags = xrc.XRC_NO_SUBCLASSING
901 # Use translations if encoding is not specified
902 if not g.currentEncoding:
903 xmlFlags != xrc.XRC_USE_LOCALE
904 res = xrc.XmlResource('', xmlFlags)
905 xrc.XmlResource.Set(res) # set as global
906 # Register handlers
907 addHandlers()
908 # Same module list
909 res.Load('memory:xxx.xrc')
910 try:
911 if xxx.__class__ == xxxFrame:
912 # Frame can't have many children,
913 # but it's first child possibly can...
914 # child = self.GetFirstChild(item)[0]
915 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
916 # # Clean-up before recursive call or error
917 # wx.MemoryFSHandler.RemoveFile('xxx.xrc')
918 # wx.EndBusyCursor()
919 # self.CreateTestWin(child)
920 # return
921 # This currently works under GTK, but not under MSW
922 testWin = g.testWin = wx.PreFrame()
923 res.LoadOnFrame(testWin, g.frame, STD_NAME)
924 # Create status bar
925 testWin.panel = testWin
926 #testWin.CreateStatusBar()
927 testWin.SetClientSize(testWin.GetBestSize())
928 testWin.SetPosition(pos)
929 testWin.Show(True)
930 elif xxx.__class__ == xxxPanel:
931 # Create new frame
932 if not testWin:
933 testWin = g.testWin = wx.Frame(g.frame, -1, 'Panel: ' + name,
934 pos=pos, name=STD_NAME)
935 testWin.panel = res.LoadPanel(testWin, STD_NAME)
936 testWin.SetClientSize(testWin.GetBestSize())
937 testWin.Show(True)
938 elif xxx.__class__ == xxxDialog:
939 testWin = g.testWin = res.LoadDialog(g.frame, STD_NAME)
940 testWin.panel = testWin
941 testWin.Layout()
942 testWin.SetPosition(pos)
943 testWin.Show(True)
944 # Dialog's default code does not produce wx.EVT_CLOSE
945 wx.EVT_BUTTON(testWin, wx.ID_OK, self.OnCloseTestWin)
946 wx.EVT_BUTTON(testWin, wx.ID_CANCEL, self.OnCloseTestWin)
947 elif xxx.__class__ == xxxWizard:
948 wiz = wx.wizard.PreWizard()
949 res.LoadOnObject(wiz, g.frame, STD_NAME, 'wxWizard')
950 # Find first page (don't know better way)
951 firstPage = None
952 for w in wiz.GetChildren():
953 if isinstance(w, wx.wizard.WizardPage):
954 firstPage = w
955 break
956 if not firstPage:
957 wx.LogError('Wizard is empty')
958 else:
959 # Wizard should be modal
960 self.SetItemBold(item)
961 wiz.RunWizard(w)
962 self.SetItemBold(item, False)
963 wiz.Destroy()
964 elif xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:
965 # Create new frame
966 if not testWin:
967 testWin = g.testWin = wx.Frame(g.frame, -1, 'Wizard page: ' + name,
968 pos=pos, name=STD_NAME)
969 testWin.panel = wx.PrePanel()
970 res.LoadOnObject(testWin.panel, testWin, STD_NAME, 'wxPanel')
971 testWin.SetClientSize(testWin.GetBestSize())
972 testWin.Show(True)
973 elif xxx.__class__ == xxxMenuBar:
974 testWin = g.testWin = wx.Frame(g.frame, -1, 'MenuBar: ' + name,
975 pos=pos, name=STD_NAME)
976 testWin.panel = None
977 # Set status bar to display help
978 testWin.CreateStatusBar()
979 testWin.menuBar = res.LoadMenuBar(STD_NAME)
980 testWin.SetMenuBar(testWin.menuBar)
981 testWin.Show(True)
982 elif xxx.__class__ == xxxToolBar:
983 testWin = g.testWin = wx.Frame(g.frame, -1, 'ToolBar: ' + name,
984 pos=pos, name=STD_NAME)
985 testWin.panel = None
986 # Set status bar to display help
987 testWin.CreateStatusBar()
988 testWin.toolBar = res.LoadToolBar(testWin, STD_NAME)
989 testWin.SetToolBar(testWin.toolBar)
990 testWin.Show(True)
991 # Catch some events, set highlight
992 if testWin:
993 testWin.item = item
994 wx.EVT_CLOSE(testWin, self.OnCloseTestWin)
995 wx.EVT_SIZE(testWin, self.OnSizeTestWin)
996 testWin.highLight = None
997 if highLight and not self.pendingHighLight:
998 self.HighLight(highLight)
999 except:
1000 if g.testWin:
1001 self.SetItemBold(item, False)
1002 g.testWinPos = g.testWin.GetPosition()
1003 g.testWin.Destroy()
1004 g.testWin = None
1005 inf = sys.exc_info()
1006 wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
1007 wx.LogError('Error loading resource')
1008 # Cleanup
1009 res.Unload('xxx.xrc')
1010 xrc.XmlResource.Set(None)
1011 wx.MemoryFSHandler.RemoveFile('xxx.xrc')
1012
1013 def CloseTestWindow(self):
1014 if not g.testWin: return
1015 self.SetItemBold(g.testWin.item, False)
1016 g.frame.tb.ToggleTool(g.frame.ID_TOOL_LOCATE, False)
1017 g.testWinPos = g.testWin.GetPosition()
1018 g.testWin.Destroy()
1019 g.testWin = None
1020
1021 def OnCloseTestWin(self, evt):
1022 self.CloseTestWindow()
1023
1024 def OnSizeTestWin(self, evt):
1025 if g.testWin.highLight:
1026 self.HighLight(g.testWin.highLight.item)
1027 evt.Skip()
1028
1029 # Return index in parent, for real window children
1030 def WindowIndex(self, item):
1031 n = 0 # index of sibling
1032 prev = self.GetPrevSibling(item)
1033 while prev.IsOk():
1034 # MenuBar is not a child
1035 if not isinstance(self.GetPyData(prev), xxxMenuBar):
1036 n += 1
1037 prev = self.GetPrevSibling(prev)
1038 return n
1039
1040 # Return item index in parent
1041 def ItemIndex(self, item):
1042 n = 0 # index of sibling
1043 prev = self.GetPrevSibling(item)
1044 while prev.IsOk():
1045 prev = self.GetPrevSibling(prev)
1046 n += 1
1047 return n
1048
1049 # Full tree index of an item - list of positions
1050 def ItemFullIndex(self, item):
1051 if not item.IsOk(): return None
1052 l = []
1053 while item != self.root:
1054 l.insert(0, self.ItemIndex(item))
1055 item = self.GetItemParent(item)
1056 return l
1057 # Get item position from full index
1058 def ItemAtFullIndex(self, index):
1059 if index is None: return wx.TreeItemId()
1060 item = self.root
1061 for i in index:
1062 item = self.GetFirstChild(item)[0]
1063 for k in range(i): item = self.GetNextSibling(item)
1064 return item
1065
1066 # True if next item should be inserted after current (vs. appended to it)
1067 def NeedInsert(self, item):
1068 xxx = self.GetPyData(item)
1069 if item == self.root: return False # root item
1070 if xxx.hasChildren and not self.GetChildrenCount(item, False):
1071 return False
1072 return not (self.IsExpanded(item) and self.GetChildrenCount(item, False))
1073
1074 # Override to use like single-selection tree
1075 def GetSelection(self):
1076 return self.selection
1077 def SelectItem(self, item):
1078 self.UnselectAll()
1079 self.ChangeSelection(item)
1080 wx.TreeCtrl.SelectItem(self, item)
1081
1082 # Pull-down
1083 def OnRightDown(self, evt):
1084 pullDownMenu = g.pullDownMenu
1085 # select this item
1086 pt = evt.GetPosition();
1087 item, flags = self.HitTest(pt)
1088 if item.Ok() and flags & wx.TREE_HITTEST_ONITEM:
1089 self.SelectItem(item)
1090
1091 # Setup menu
1092 menu = wx.Menu()
1093
1094 item = self.selection
1095 if not item:
1096 menu.Append(g.pullDownMenu.ID_EXPAND, 'Expand', 'Expand tree')
1097 menu.Append(g.pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse tree')
1098 else:
1099 # self.ctrl = evt.ControlDown() # save Ctrl state
1100 # self.shift = evt.ShiftDown() # and Shift too
1101 m = wx.Menu() # create menu
1102 if self.ctrl:
1103 needInsert = True
1104 else:
1105 needInsert = self.NeedInsert(item)
1106 if item == self.root or needInsert and self.GetItemParent(item) == self.root:
1107 SetMenu(m, pullDownMenu.topLevel)
1108 m.AppendSeparator()
1109 m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
1110 m.Append(ID_NEW.COMMENT, 'comment', 'Create comment node')
1111 else:
1112 xxx = self.GetPyData(item).treeObject()
1113 # Check parent for possible child nodes if inserting sibling
1114 if needInsert: xxx = xxx.parent
1115 if xxx.__class__ == xxxMenuBar:
1116 m.Append(ID_NEW.MENU, 'Menu', 'Create menu')
1117 elif xxx.__class__ in [xxxToolBar, xxxTool] or \
1118 xxx.__class__ == xxxSeparator and xxx.parent.__class__ == xxxToolBar:
1119 SetMenu(m, pullDownMenu.toolBarControls)
1120 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
1121 SetMenu(m, pullDownMenu.menuControls)
1122 elif xxx.__class__ == xxxStdDialogButtonSizer:
1123 SetMenu(m, pullDownMenu.stdButtons)
1124 else:
1125 SetMenu(m, pullDownMenu.controls)
1126 if xxx.__class__ in [xxxNotebook, xxxChoicebook, xxxListbook]:
1127 m.Enable(m.FindItem('sizer'), False)
1128 elif not (xxx.isSizer or xxx.parent and xxx.parent.isSizer):
1129 m.Enable(ID_NEW.SPACER, False)
1130 if xxx.__class__ is not xxxFrame:
1131 m.Enable(ID_NEW.MENU_BAR, False)
1132 # Add custom controls menu
1133 if pullDownMenu.customMap:
1134 SetMenu(m, pullDownMenu.custom)
1135 m.AppendSeparator()
1136 m.Append(ID_NEW.REF, 'reference...', 'Create object_ref node')
1137 m.Append(ID_NEW.COMMENT, 'comment', 'Create comment node')
1138 # Select correct label for create menu
1139 if not needInsert:
1140 if self.shift:
1141 menu.AppendMenu(wx.NewId(), 'Insert Child', m,
1142 'Create child object as the first child')
1143 else:
1144 menu.AppendMenu(wx.NewId(), 'Append Child', m,
1145 'Create child object as the last child')
1146 else:
1147 if self.shift:
1148 menu.AppendMenu(wx.NewId(), 'Create Sibling', m,
1149 'Create sibling before selected object')
1150 else:
1151 menu.AppendMenu(wx.NewId(), 'Create Sibling', m,
1152 'Create sibling after selected object')
1153 # Build replace menu
1154 if item != self.root:
1155 xxx = self.GetPyData(item).treeObject()
1156 m = wx.Menu() # create replace menu
1157 if xxx.__class__ == xxxMenuBar:
1158 m.Append(1000 + ID_NEW.MENU, 'Menu', 'Create menu')
1159 elif xxx.__class__ in [xxxMenu, xxxMenuItem]:
1160 SetMenu(m, pullDownMenu.menuControls, shift=True)
1161 elif xxx.__class__ == xxxToolBar and \
1162 self.GetItemParent(item) == self.root:
1163 SetMenu(m, [], shift=True)
1164 elif xxx.__class__ in [xxxFrame, xxxDialog, xxxPanel]:
1165 SetMenu(m, [
1166 (ID_NEW.PANEL, 'Panel', 'Create panel'),
1167 (ID_NEW.DIALOG, 'Dialog', 'Create dialog'),
1168 (ID_NEW.FRAME, 'Frame', 'Create frame')], shift=True)
1169 elif xxx.isSizer and self.ItemHasChildren(item):
1170 SetMenu(m, pullDownMenu.sizers, shift=True)
1171 else:
1172 SetMenu(m, pullDownMenu.controls, shift=True)
1173 if xxx.isElement:
1174 id = wx.NewId()
1175 menu.AppendMenu(id, 'Replace With', m)
1176 if not m.GetMenuItemCount(): menu.Enable(id, False)
1177 menu.Append(pullDownMenu.ID_SUBCLASS, 'Subclass...',
1178 'Set "subclass" property')
1179 menu.AppendSeparator()
1180 # Not using standart IDs because we don't want to show shortcuts
1181 menu.Append(wx.ID_CUT, 'Cut', 'Cut to the clipboard')
1182 menu.Append(wx.ID_COPY, 'Copy', 'Copy to the clipboard')
1183 if self.ctrl and item != self.root:
1184 menu.Append(pullDownMenu.ID_PASTE_SIBLING, 'Paste Sibling',
1185 'Paste from the clipboard as a sibling')
1186 else:
1187 menu.Append(wx.ID_PASTE, 'Paste', 'Paste from the clipboard')
1188 menu.Append(pullDownMenu.ID_DELETE,
1189 'Delete', 'Delete object')
1190 if self.ItemHasChildren(item):
1191 menu.AppendSeparator()
1192 menu.Append(pullDownMenu.ID_EXPAND, 'Expand', 'Expand subtree')
1193 menu.Append(pullDownMenu.ID_COLLAPSE, 'Collapse', 'Collapse subtree')
1194 self.PopupMenu(menu, evt.GetPosition())
1195 menu.Destroy()
1196
1197 # Redefine to force the update of font dimentions on wxGTK
1198 if wx.Platform == '__WXGTK__':
1199 def SetItemBold(self, item, state=True):
1200 wx.TreeCtrl.SetItemBold(self, item, state)
1201 self.SetIndent(self.GetIndent())
1202
1203 # Apply changes
1204 def Apply(self, xxx, item):
1205 g.panel.Apply()
1206 # Update tree view
1207 xxx = xxx.treeObject()
1208 if xxx.hasName and self.GetItemText(item) != xxx.name:
1209 self.SetItemText(item, xxx.treeName())
1210 # Item width may have changed
1211 # !!! Tric to update tree width (wxGTK, ??)
1212 self.SetIndent(self.GetIndent())
1213 elif xxx.className == 'comment':
1214 self.SetItemText(item, xxx.treeName())
1215 # Change tree icon for sizers
1216 if isinstance(xxx, xxxBoxSizer):
1217 self.SetItemImage(item, xxx.treeImage())
1218 # Set global modified state
1219 g.frame.SetModified()
1220
1221 def OnBeginLabelEdit(self, evt):
1222 xxx = self.GetPyData(evt.GetItem())
1223 if xxx.isElement:
1224 evt.Veto()
1225 else:
1226 evt.Skip()
1227
1228 def OnEndLabelEdit(self, evt):
1229 xxx = self.GetPyData(evt.GetItem())
1230 node = xxx.node
1231 if not xxx.isElement:
1232 node.data = evt.GetLabel()
1233 g.panel.SetData(xxx)
1234 evt.Skip()