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