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