2 # Purpose: XRC editor, XML_tree class
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
7 from xxx
import * # xxx imports globals and params
11 # Constant to define standart window name
12 STD_NAME
= '_XRCED_T_W'
14 COLOUR_COMMENT
= 'Blue'
15 COLOUR_REF
= 'DarkGreen'
16 COLOUR_HIDDEN
= 'Grey'
18 COLOUR_DT
= 'DarkGreen'
24 def __init__(self
, name
):
27 def write(self
, data
):
29 encoding
= g
.currentEncoding
31 encoding
= wx
.GetDefaultPyEncoding()
33 self
.buffer += data
.encode(encoding
)
34 except UnicodeEncodeError:
35 self
.buffer += data
.encode(encoding
, 'xmlcharrefreplace')
38 wx
.MemoryFSHandler
.AddFile(self
.name
, self
.buffer)
40 ################################################################################
42 # Redefine writing to include encoding
43 class MyDocument(minidom
.Document
):
45 minidom
.Document
.__init
__(self
)
47 def writexml(self
, writer
, indent
="", addindent
="", newl
="", encoding
=""):
48 if encoding
: encdstr
= 'encoding="%s"' % encoding
50 writer
.write('<?xml version="1.0" %s?>\n' % encdstr
)
51 for node
in self
.childNodes
:
52 node
.writexml(writer
, indent
, addindent
, newl
)
54 ################################################################################
56 # Ids for menu commands
65 STATUS_BAR
= wx
.NewId()
67 STATIC_TEXT
= wx
.NewId()
68 TEXT_CTRL
= wx
.NewId()
71 BITMAP_BUTTON
= wx
.NewId()
72 RADIO_BUTTON
= wx
.NewId()
73 SPIN_BUTTON
= wx
.NewId()
74 TOGGLE_BUTTON
= wx
.NewId()
76 STATIC_BOX
= wx
.NewId()
77 CHECK_BOX
= wx
.NewId()
78 RADIO_BOX
= wx
.NewId()
79 COMBO_BOX
= wx
.NewId()
82 STATIC_LINE
= wx
.NewId()
83 STATIC_BITMAP
= wx
.NewId()
87 SCROLL_BAR
= wx
.NewId()
88 TREE_CTRL
= wx
.NewId()
89 LIST_CTRL
= wx
.NewId()
90 CHECK_LIST
= wx
.NewId()
92 CHOICEBOOK
= wx
.NewId()
94 SPLITTER_WINDOW
= 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()
105 WIZARD_PAGE
= wx
.NewId()
106 WIZARD_PAGE_SIMPLE
= wx
.NewId()
109 STATUS_BAR
= wx
.NewId()
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()
119 TOOL_BAR
= wx
.NewId()
122 MENU_ITEM
= wx
.NewId()
123 SEPARATOR
= wx
.NewId()
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()
138 for i
in range(99): wx
.NewId() # reserve IDs for custom controls
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()
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
)
162 # Mapping from IDs to element names
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',
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',
180 ID_NEW
.STATIC_TEXT
: 'wxStaticText',
181 ID_NEW
.TEXT_CTRL
: 'wxTextCtrl',
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',
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',
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',
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',
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',
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'),
241 (ID_NEW
.TOOL_BAR
, 'ToolBar', 'Create toolbar'),
242 (ID_NEW
.MENU_BAR
, 'MenuBar', 'Create menubar'),
243 (ID_NEW
.MENU
, 'Menu', 'Create menu'),
245 (ID_NEW
.BITMAP
, 'Bitmap', 'Create bitmap'),
246 (ID_NEW
.ICON
, 'Icon', 'Create icon'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
389 self
.custom
= ['custom', 'User-defined controls']
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
398 ################################################################################
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):
407 if type(l
) == types
.TupleType
:
409 if shift
: l
= (1000 + l
[0],) + l
[1:]
411 elif type(l
) == types
.ListType
:
413 SetMenu(subMenu
, l
[2:], shift
)
414 m
.AppendMenu(wx
.NewId(), l
[0], subMenu
, l
[1])
418 ################################################################################
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
= 0
426 if size
.height
== -1: size
.height
= 0
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
]
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
= 0
442 if size
.height
== -1: size
.height
= 0
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)
450 map(wx
.Window
.Destroy
, self
.lines
)
451 g
.testWin
.highLight
= None
453 map(wx
.Window
.Refresh
, self
.lines
)
455 # Same for drop target
456 class HighLightDTBox(HighLightBox
):
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
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
]
477 map(wx
.Window
.Destroy
, self
.lines
)
478 g
.testWin
.highLightDT
= None
480 def updateHL(hl
, hlClass
, pos
, size
):
481 if hl
and hl
.size
== size
:
485 hl
.Replace(pos
, size
)
488 hl
= hlClass(pos
, size
)
491 ################################################################################
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(),
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
)
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
)
514 self
.selection
= None
515 self
.selectionChanging
= False
516 self
.needUpdate
= False
517 self
.pendingHighLight
= None
518 self
.ctrl
= self
.shift
= False
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())
541 self
.SetImageList(il
)
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
)
549 def ExpandAll(self
, item
):
550 if self
.ItemHasChildren(item
):
552 i
, cookie
= self
.GetFirstChild(item
)
556 i
, cookie
= self
.GetNextChild(item
, cookie
)
559 def CollapseAll(self
, item
):
560 if self
.ItemHasChildren(item
):
561 i
, cookie
= self
.GetFirstChild(item
)
565 i
, cookie
= self
.GetNextChild(item
, cookie
)
572 self
.selection
= None
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')
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
)
591 # Clear old data and set new
592 def SetData(self
, dom
):
593 self
.selection
= None
595 self
.DeleteAllItems()
596 # Add minimal structure
597 if self
.dom
: self
.dom
.unlink()
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
[:]
609 self
.AddNode(self
.root
, None, node
)
611 self
.mainNode
.removeChild(node
)
613 if self
.mainNode
.firstChild
:
614 self
.testElem
= self
.dom
.createElement('dummy')
615 self
.mainNode
.insertBefore(self
.testElem
, self
.mainNode
.firstChild
)
617 self
.testElem
= self
.dom
.createElement('dummy')
618 self
.mainNode
.appendChild(self
.testElem
)
619 self
.Expand(self
.root
)
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
626 xxx
= MakeXXXFromDOM(xxxParent
, node
)
628 print 'ERROR: MakeXXXFromDom(%s, %s)' % (xxxParent
, node
)
630 treeObj
= xxx
.treeObject()
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
)
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
[:]
648 self
.AddNode(item
, treeObj
, n
)
649 elif n
.nodeType
!= minidom
.Node
.ELEMENT_NODE
:
650 treeObj
.node
.removeChild(n
)
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
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
)
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
)
676 self
.SetItemTextColour(newItem
, COLOUR_REF
)
677 elif treeObj
.hasStyle
and treeObj
.params
.get('hidden', False):
678 self
.SetItemTextColour(newItem
, COLOUR_HIDDEN
)
681 treeObj
= xxx
.treeObject()
682 for n
in treeObj
.node
.childNodes
:
684 self
.AddNode(newItem
, treeObj
, n
)
687 # Remove leaf of tree, return it's data object
688 def RemoveLeaf(self
, leaf
):
689 xxx
= self
.GetPyData(leaf
)
691 parent
= node
.parentNode
692 parent
.removeChild(node
)
694 # Reset selection object
695 self
.selection
= None
698 # Find position relative to the top-level window
699 def FindNodePos(self
, item
, obj
=None):
701 if item
== g
.testWin
.item
: return wx
.Point(0, 0)
702 itemParent
= self
.GetItemParent(item
)
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
)
709 for i
in range(book
.GetPageCount()):
710 if book
.GetPage(i
) == obj
:
711 if book
.GetSelection() != i
:
713 # Remove highlight - otherwise highlight window won't be visible
714 if g
.testWin
.highLight
:
715 g
.testWin
.highLight
.Remove()
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
732 # Find window (or sizer) corresponding to a tree item.
733 def FindNodeObject(self
, item
):
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
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()
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()
768 def OnSelChanged(self
, evt
):
769 if self
.selectionChanging
: return
770 self
.selectionChanging
= True
772 self
.SelectItem(evt
.GetItem())
773 self
.selectionChanging
= False
774 g
.frame
.SetStatusText('')
776 def ChangeSelection(self
, item
):
778 # !!! problem with wxGTK - GetOldItem is Ok if nothing selected
779 #oldItem = evt.GetOldItem()
781 oldItem
= self
.selection
783 xxx
= self
.GetPyData(oldItem
)
784 # If some data was modified, apply changes
785 if g
.panel
.IsModified():
786 self
.Apply(xxx
, oldItem
)
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
)
794 self
.selection
= item
795 if not self
.selection
.IsOk():
796 self
.selection
= None
798 xxx
= self
.GetPyData(self
.selection
)
803 # Highlighting is done in OnIdle
804 self
.pendingHighLight
= self
.selection
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
:
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
]:
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()
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()
833 pos
= self
.FindNodePos(item
, obj
)
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
842 def ShowTestWindow(self
, item
):
843 xxx
= self
.GetPyData(item
)
844 if g
.panel
.IsModified():
845 self
.Apply(xxx
, item
) # apply changes
846 availableViews
= ['wxFrame', 'wxPanel', 'wxDialog',
847 'wxMenuBar', 'wxToolBar', 'wxWizard',
848 'wxWizardPageSimple']
850 # Walk up the tree until we find an item that has a view
851 while item
and self
.GetPyData(item
).treeObject().className
not in availableViews
:
852 item
= self
.GetItemParent(item
)
853 if not item
or not item
.IsOk():
854 wx
.LogMessage('No view for this element (yet)')
857 if g
.testWin
: # Reset old
859 self
.SetItemBold(g
.testWin
.item
, False)
862 self
.CreateTestWin(item
)
865 # Maybe an error occurred, so we need to test
867 self
.SetItemBold(g
.testWin
.item
)
868 # Select original item
869 self
.ChangeSelection(originalItem
)
871 # Double-click on Linux
872 def OnItemActivated(self
, evt
):
873 if evt
.GetItem() != self
.root
:
874 self
.ShowTestWindow(evt
.GetItem())
876 # Double-click on Windows
877 def OnDClick(self
, evt
):
878 item
, flags
= self
.HitTest(evt
.GetPosition())
879 if flags
in [wx
.TREE_HITTEST_ONITEMBUTTON
, wx
.TREE_HITTEST_ONITEMLABEL
]:
880 if item
!= self
.root
: self
.ShowTestWindow(item
)
884 def OnItemExpandedCollapsed(self
, evt
):
885 # Update tool palette
889 # (re)create test window
890 def CreateTestWin(self
, item
):
892 # Create a window with this resource
893 xxx
= self
.GetPyData(item
).treeObject()
894 # Close old window, remember where it was
897 pos
= testWin
.GetPosition()
898 if item
== testWin
.item
:
899 # Remember highlight if same top-level window
900 if testWin
.highLight
:
901 highLight
= testWin
.highLight
.item
902 if xxx
.className
== 'wxPanel':
903 if testWin
.highLight
:
904 testWin
.pendingHighLight
= highLight
905 testWin
.highLight
.Remove()
906 testWin
.panel
.Destroy()
910 testWin
= g
.testWin
= None
913 testWin
= g
.testWin
= None
917 memFile
= MemoryFile('xxx.xrc')
918 # Create memory XML file
919 elem
= xxx
.node
.cloneNode(True)
924 elem
.setAttribute('name', STD_NAME
)
925 oldTestNode
= self
.testElem
927 self
.mainNode
.replaceChild(elem
, oldTestNode
)
929 # Replace wizard page class temporarily
930 if xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
931 oldCl
= elem
.getAttribute('class')
932 elem
.setAttribute('class', 'wxPanel')
933 parent
= elem
.parentNode
934 encd
= self
.rootObj
.params
['encoding'].value()
935 if not encd
: encd
= None
937 self
.dom
.writexml(memFile
, encoding
=encd
)
940 wx
.LogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
941 wx
.LogError('Error writing temporary file')
942 memFile
.close() # write to wxMemoryFS
943 xmlFlags
= xrc
.XRC_NO_SUBCLASSING
944 # Use translations if encoding is not specified
945 if not g
.currentEncoding
:
946 xmlFlags
!= xrc
.XRC_USE_LOCALE
947 res
= xrc
.XmlResource('', xmlFlags
)
948 res
.InitAllHandlers()
949 xrc
.XmlResource
.Set(res
) # set as global
953 res
.Load('memory:xxx.xrc')
955 if xxx
.__class
__ == xxxFrame
:
956 # Frame can't have many children,
957 # but it's first child possibly can...
958 # child = self.GetFirstChild(item)[0]
959 # if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:
960 # # Clean-up before recursive call or error
961 # wx.MemoryFSHandler.RemoveFile('xxx.xrc')
963 # self.CreateTestWin(child)
965 # This currently works under GTK, but not under MSW
966 testWin
= g
.testWin
= wx
.PreFrame()
967 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
969 testWin
.panel
= testWin
970 #testWin.CreateStatusBar()
971 testWin
.SetClientSize(testWin
.GetBestSize())
972 testWin
.SetPosition(pos
)
974 elif xxx
.__class
__ == xxxPanel
:
977 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'Panel: ' + name
,
978 pos
=pos
, name
=STD_NAME
)
979 testWin
.panel
= res
.LoadPanel(testWin
, STD_NAME
)
980 testWin
.SetClientSize(testWin
.GetBestSize())
982 elif xxx
.__class
__ == xxxDialog
:
983 testWin
= g
.testWin
= res
.LoadDialog(g
.frame
, STD_NAME
)
984 testWin
.panel
= testWin
986 testWin
.SetPosition(pos
)
988 # Dialog's default code does not produce wx.EVT_CLOSE
989 wx
.EVT_BUTTON(testWin
, wx
.ID_OK
, self
.OnCloseTestWin
)
990 wx
.EVT_BUTTON(testWin
, wx
.ID_CANCEL
, self
.OnCloseTestWin
)
991 elif xxx
.__class
__ == xxxWizard
:
992 wiz
= wx
.wizard
.PreWizard()
993 res
.LoadOnObject(wiz
, g
.frame
, STD_NAME
, 'wxWizard')
994 # Find first page (don't know better way)
996 for w
in wiz
.GetChildren():
997 if isinstance(w
, wx
.wizard
.WizardPage
):
1001 wx
.LogError('Wizard is empty')
1003 # Wizard should be modal
1004 self
.SetItemBold(item
)
1006 self
.SetItemBold(item
, False)
1008 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
1011 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'Wizard page: ' + name
,
1012 pos
=pos
, name
=STD_NAME
)
1013 testWin
.panel
= wx
.PrePanel()
1014 res
.LoadOnObject(testWin
.panel
, testWin
, STD_NAME
, 'wxPanel')
1015 testWin
.SetClientSize(testWin
.GetBestSize())
1017 elif xxx
.__class
__ == xxxMenuBar
:
1018 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'MenuBar: ' + name
,
1019 pos
=pos
, name
=STD_NAME
)
1020 testWin
.panel
= None
1021 # Set status bar to display help
1022 testWin
.CreateStatusBar()
1023 testWin
.menuBar
= res
.LoadMenuBar(STD_NAME
)
1024 testWin
.SetMenuBar(testWin
.menuBar
)
1026 elif xxx
.__class
__ == xxxToolBar
:
1027 testWin
= g
.testWin
= wx
.Frame(g
.frame
, -1, 'ToolBar: ' + name
,
1028 pos
=pos
, name
=STD_NAME
)
1029 testWin
.panel
= None
1030 # Set status bar to display help
1031 testWin
.CreateStatusBar()
1032 testWin
.toolBar
= res
.LoadToolBar(testWin
, STD_NAME
)
1033 testWin
.SetToolBar(testWin
.toolBar
)
1035 # Catch some events, set highlight
1038 wx
.EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
1039 wx
.EVT_SIZE(testWin
, self
.OnSizeTestWin
)
1041 testWin
.SetDropTarget(DropTarget())
1043 testWin
.highLight
= testWin
.highLightDT
= None
1044 if highLight
and not self
.pendingHighLight
:
1045 self
.HighLight(highLight
)
1048 self
.SetItemBold(item
, False)
1049 g
.testWinPos
= g
.testWin
.GetPosition()
1052 inf
= sys
.exc_info()
1053 wx
.LogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
1054 wx
.LogError('Error loading resource')
1056 res
.Unload('xxx.xrc')
1057 xrc
.XmlResource
.Set(None)
1058 wx
.MemoryFSHandler
.RemoveFile('xxx.xrc')
1060 def CloseTestWindow(self
):
1061 if not g
.testWin
: return
1062 self
.SetItemBold(g
.testWin
.item
, False)
1063 g
.frame
.tb
.ToggleTool(g
.frame
.ID_TOOL_LOCATE
, False)
1064 g
.testWinPos
= g
.testWin
.GetPosition()
1068 def OnCloseTestWin(self
, evt
):
1069 self
.CloseTestWindow()
1071 def OnSizeTestWin(self
, evt
):
1072 if g
.testWin
.highLight
:
1073 self
.HighLight(g
.testWin
.highLight
.item
)
1076 # Return index in parent, for real window children
1077 def WindowIndex(self
, item
):
1078 n
= 0 # index of sibling
1079 prev
= self
.GetPrevSibling(item
)
1081 # MenuBar is not a child
1082 if not isinstance(self
.GetPyData(prev
), xxxMenuBar
):
1084 prev
= self
.GetPrevSibling(prev
)
1087 # Return item index in parent
1088 def ItemIndex(self
, item
):
1089 n
= 0 # index of sibling
1090 prev
= self
.GetPrevSibling(item
)
1092 prev
= self
.GetPrevSibling(prev
)
1096 # Full tree index of an item - list of positions
1097 def ItemFullIndex(self
, item
):
1098 if not item
.IsOk(): return None
1100 while item
!= self
.root
:
1101 l
.insert(0, self
.ItemIndex(item
))
1102 item
= self
.GetItemParent(item
)
1104 # Get item position from full index
1105 def ItemAtFullIndex(self
, index
):
1106 if index
is None: return wx
.TreeItemId()
1109 item
= self
.GetFirstChild(item
)[0]
1110 for k
in range(i
): item
= self
.GetNextSibling(item
)
1113 # True if next item should be inserted after current (vs. appended to it)
1114 def NeedInsert(self
, item
):
1115 xxx
= self
.GetPyData(item
)
1116 if item
== self
.root
: return False # root item
1117 if xxx
.hasChildren
and not self
.GetChildrenCount(item
, False):
1119 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
1121 # Override to use like single-selection tree
1122 def GetSelection(self
):
1123 return self
.selection
1125 def SelectItem(self
, item
):
1127 self
.ChangeSelection(item
)
1128 wx
.TreeCtrl
.SelectItem(self
, item
)
1131 def OnRightDown(self
, evt
):
1132 pullDownMenu
= g
.pullDownMenu
1134 pt
= evt
.GetPosition();
1135 item
, flags
= self
.HitTest(pt
)
1136 if item
.Ok() and flags
& wx
.TREE_HITTEST_ONITEM
:
1137 self
.SelectItem(item
)
1142 item
= self
.selection
1144 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
1145 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
1147 # self.ctrl = evt.ControlDown() # save Ctrl state
1148 # self.shift = evt.ShiftDown() # and Shift too
1149 m
= wx
.Menu() # create menu
1153 needInsert
= self
.NeedInsert(item
)
1154 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
1155 SetMenu(m
, pullDownMenu
.topLevel
)
1157 m
.Append(ID_NEW
.REF
, 'reference...', 'Create object_ref node')
1158 m
.Append(ID_NEW
.COMMENT
, 'comment', 'Create comment node')
1160 xxx
= self
.GetPyData(item
).treeObject()
1161 # Check parent for possible child nodes if inserting sibling
1162 if needInsert
: xxx
= xxx
.parent
1163 if xxx
.__class
__ == xxxMenuBar
:
1164 m
.Append(ID_NEW
.MENU
, 'Menu', 'Create menu')
1165 elif xxx
.__class
__ in [xxxToolBar
, xxxTool
] or \
1166 xxx
.__class
__ == xxxSeparator
and xxx
.parent
.__class
__ == xxxToolBar
:
1167 SetMenu(m
, pullDownMenu
.toolBarControls
)
1168 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1169 SetMenu(m
, pullDownMenu
.menuControls
)
1170 elif xxx
.__class
__ == xxxStdDialogButtonSizer
:
1171 SetMenu(m
, pullDownMenu
.stdButtons
)
1173 SetMenu(m
, pullDownMenu
.controls
)
1174 if xxx
.__class
__ in [xxxNotebook
, xxxChoicebook
, xxxListbook
]:
1175 m
.Enable(m
.FindItem('sizer'), False)
1176 elif not (xxx
.isSizer
or xxx
.parent
and xxx
.parent
.isSizer
):
1177 m
.Enable(ID_NEW
.SPACER
, False)
1178 if xxx
.__class
__ is not xxxFrame
:
1179 m
.Enable(ID_NEW
.MENU_BAR
, False)
1180 # Add custom controls menu
1181 if len(pullDownMenu
.custom
) > 2:
1182 SetMenu(m
, [pullDownMenu
.custom
])
1184 m
.Append(ID_NEW
.REF
, 'reference...', 'Create object_ref node')
1185 m
.Append(ID_NEW
.COMMENT
, 'comment', 'Create comment node')
1186 # Select correct label for create menu
1189 menu
.AppendMenu(wx
.NewId(), 'Insert Child', m
,
1190 'Create child object as the first child')
1192 menu
.AppendMenu(wx
.NewId(), 'Append Child', m
,
1193 'Create child object as the last child')
1196 menu
.AppendMenu(wx
.NewId(), 'Create Sibling', m
,
1197 'Create sibling before selected object')
1199 menu
.AppendMenu(wx
.NewId(), 'Create Sibling', m
,
1200 'Create sibling after selected object')
1201 # Build replace menu
1202 if item
!= self
.root
:
1203 xxx
= self
.GetPyData(item
).treeObject()
1204 m
= wx
.Menu() # create replace menu
1205 if xxx
.__class
__ == xxxMenuBar
:
1206 m
.Append(1000 + ID_NEW
.MENU
, 'Menu', 'Create menu')
1207 elif xxx
.__class
__ in [xxxMenu
, xxxMenuItem
]:
1208 SetMenu(m
, pullDownMenu
.menuControls
, shift
=True)
1209 elif xxx
.__class
__ == xxxToolBar
and \
1210 self
.GetItemParent(item
) == self
.root
:
1211 SetMenu(m
, [], shift
=True)
1212 elif xxx
.__class
__ in [xxxFrame
, xxxDialog
, xxxPanel
]:
1214 (ID_NEW
.PANEL
, 'Panel', 'Create panel'),
1215 (ID_NEW
.DIALOG
, 'Dialog', 'Create dialog'),
1216 (ID_NEW
.FRAME
, 'Frame', 'Create frame')], shift
=True)
1217 elif xxx
.isSizer
and self
.ItemHasChildren(item
):
1218 SetMenu(m
, pullDownMenu
.sizers
, shift
=True)
1220 SetMenu(m
, pullDownMenu
.controls
, shift
=True)
1223 menu
.AppendMenu(id, 'Replace With', m
)
1224 if not m
.GetMenuItemCount(): menu
.Enable(id, False)
1225 menu
.Append(pullDownMenu
.ID_SUBCLASS
, 'Subclass...',
1226 'Set "subclass" property')
1227 menu
.AppendSeparator()
1228 # Not using standart IDs because we don't want to show shortcuts
1229 menu
.Append(wx
.ID_CUT
, 'Cut', 'Cut to the clipboard')
1230 menu
.Append(wx
.ID_COPY
, 'Copy', 'Copy to the clipboard')
1231 if self
.ctrl
and item
!= self
.root
:
1232 menu
.Append(pullDownMenu
.ID_PASTE_SIBLING
, 'Paste Sibling',
1233 'Paste from the clipboard as a sibling')
1235 menu
.Append(wx
.ID_PASTE
, 'Paste', 'Paste from the clipboard')
1236 menu
.Append(pullDownMenu
.ID_DELETE
,
1237 'Delete', 'Delete object')
1238 if self
.ItemHasChildren(item
):
1239 menu
.AppendSeparator()
1240 menu
.Append(pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand subtree')
1241 menu
.Append(pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse subtree')
1242 self
.PopupMenu(menu
, evt
.GetPosition())
1245 # Redefine to force the update of font dimentions on wxGTK
1246 if wx
.Platform
== '__WXGTK__':
1247 def SetItemBold(self
, item
, state
=True):
1248 wx
.TreeCtrl
.SetItemBold(self
, item
, state
)
1249 self
.SetIndent(self
.GetIndent())
1252 def Apply(self
, xxx
, item
):
1255 xxx
= xxx
.treeObject()
1256 if xxx
.hasName
and self
.GetItemText(item
) != xxx
.name
:
1257 self
.SetItemText(item
, xxx
.treeName())
1258 # Item width may have changed
1259 # !!! Tric to update tree width (wxGTK, ??)
1260 self
.SetIndent(self
.GetIndent())
1261 elif xxx
.className
== 'comment':
1262 self
.SetItemText(item
, xxx
.treeName())
1263 # Change tree icon for sizers
1264 if isinstance(xxx
, xxxBoxSizer
):
1265 self
.SetItemImage(item
, xxx
.treeImage())
1266 # Set global modified state
1267 g
.frame
.SetModified()
1269 def OnBeginLabelEdit(self
, evt
):
1270 xxx
= self
.GetPyData(evt
.GetItem())
1276 def OnEndLabelEdit(self
, evt
):
1277 xxx
= self
.GetPyData(evt
.GetItem())
1279 if not xxx
.isElement
:
1280 node
.data
= evt
.GetLabel()
1281 g
.panel
.SetData(xxx
)
1284 ################################################################################
1288 class DropTarget(wx
.PyDropTarget
):
1290 self
.do
= MyDataObject()
1291 wx
.DropTarget
.__init
__(self
, self
.do
)
1293 # Find best object for dropping
1294 def WhereToDrop(self
, x
, y
, d
):
1295 # Find object by position
1296 obj
= wx
.FindWindowAtPoint(g
.testWin
.ClientToScreen((x
,y
)))
1298 return wx
.DragNone
, ()
1299 item
= g
.frame
.FindObject(g
.testWin
.item
, obj
)
1301 return wx
.DragNone
, ()
1302 xxx
= g
.tree
.GetPyData(item
).treeObject()
1304 # Check if window has a XRC sizer, then use it as parent
1306 sizer
= obj
.GetSizer()
1307 sizerItem
= g
.frame
.FindObject(g
.testWin
.item
, sizer
)
1309 parentItem
= sizerItem
1311 item
= wx
.TreeItemId()
1312 # if not sizer but can have children, it is parent with free placement
1313 elif xxx
.hasChildren
:
1315 item
= wx
.TreeItemId()
1316 # Otherwise, try to add to item's parent
1318 parentItem
= g
.tree
.GetItemParent(item
)
1319 obj
= g
.tree
.FindNodeObject(parentItem
)
1320 parent
= g
.tree
.GetPyData(parentItem
).treeObject()
1321 return d
,(obj
,parent
,parentItem
,item
)
1324 def OnData(self
, x
, y
, d
):
1326 id = int(self
.do
.GetDataHere())
1327 d
,other
= self
.WhereToDrop(x
, y
, d
)
1328 if d
!= wx
.DragNone
:
1329 obj
,parent
,parentItem
,item
= other
1330 g
.tree
.selection
= parentItem
1331 xxx
= g
.frame
.CreateXXX(parent
, parentItem
, item
, id)
1332 # Set coordinates if parent is not sizer
1333 if not parent
.isSizer
:
1334 xxx
.set('pos', '%d,%d' % (x
, y
))
1335 g
.panel
.SetData(xxx
)
1336 g
.frame
.SetStatusText('Object created')
1340 def OnDragOver(self
, x
, y
, d
):
1341 d
,other
= self
.WhereToDrop(x
, y
, d
)
1342 if d
!= wx
.DragNone
:
1343 obj
,parent
,parentItem
,item
= other
1344 pos
, size
= g
.tree
.FindNodePos(parentItem
, obj
), obj
.GetSize()
1345 hl
= g
.testWin
.highLightDT
1346 # Set color of highlighted item back to normal
1348 if hl
.item
!= parentItem
:
1349 g
.tree
.SetItemTextColour(hl
.item
, g
.tree
.itemColour
)
1350 # Highlight future parent
1351 g
.tree
.itemColour
= g
.tree
.GetItemTextColour(parentItem
) # save current
1352 g
.testWin
.highLightDT
= updateHL(hl
, HighLightDTBox
, pos
, size
)
1353 g
.testWin
.highLightDT
.item
= parentItem
1354 g
.tree
.SetItemTextColour(parentItem
, COLOUR_DT
)
1355 g
.tree
.EnsureVisible(parentItem
)
1356 g
.frame
.SetStatusText('Drop target: %s' % parent
.treeName())
1358 g
.frame
.SetStatusText('Inappropriate drop target')
1366 hl
= g
.testWin
.highLightDT
1369 g
.tree
.SetItemTextColour(hl
.item
, g
.tree
.itemColour
)