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
= 1
426 if size
.height
== -1: size
.height
= 1
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
= 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)
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
841 g
.testWin
.highLight
.obj
= obj
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']
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)')
858 if g
.testWin
: # Reset old
860 self
.SetItemBold(g
.testWin
.item
, False)
863 self
.CreateTestWin(item
)
866 # Maybe an error occurred, so we need to test
868 self
.SetItemBold(g
.testWin
.item
)
869 # Select original item
870 self
.ChangeSelection(originalItem
)
872 # Double-click on Linux
873 def OnItemActivated(self
, evt
):
874 if evt
.GetItem() != self
.root
:
875 self
.ShowTestWindow(evt
.GetItem())
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
)
885 def OnItemExpandedCollapsed(self
, evt
):
886 # Update tool palette
890 # (re)create test window
891 def CreateTestWin(self
, item
):
893 # Create a window with this resource
894 xxx
= self
.GetPyData(item
).treeObject()
895 # Close old window, remember where it was
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()
911 testWin
= g
.testWin
= None
914 testWin
= g
.testWin
= None
918 memFile
= MemoryFile('xxx.xrc')
919 # Create memory XML file
920 elem
= xxx
.node
.cloneNode(True)
925 elem
.setAttribute('name', STD_NAME
)
926 oldTestNode
= self
.testElem
928 self
.mainNode
.replaceChild(elem
, oldTestNode
)
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
938 self
.dom
.writexml(memFile
, encoding
=encd
)
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
954 res
.Load('memory:xxx.xrc')
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')
964 # self.CreateTestWin(child)
966 # This currently works under GTK, but not under MSW
967 testWin
= g
.testWin
= wx
.PreFrame()
968 res
.LoadOnFrame(testWin
, g
.frame
, STD_NAME
)
970 testWin
.panel
= testWin
971 #testWin.CreateStatusBar()
972 testWin
.SetClientSize(testWin
.GetBestSize())
973 testWin
.SetPosition(pos
)
975 elif xxx
.__class
__ == xxxPanel
:
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())
984 elif xxx
.__class
__ == xxxDialog
:
985 testWin
= g
.testWin
= res
.LoadDialog(g
.frame
, STD_NAME
)
986 testWin
.panel
= testWin
988 testWin
.SetPosition(pos
)
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)
998 for w
in wiz
.GetChildren():
999 if isinstance(w
, wx
.wizard
.WizardPage
):
1003 wx
.LogError('Wizard is empty')
1005 # Wizard should be modal
1006 self
.SetItemBold(item
)
1008 self
.SetItemBold(item
, False)
1010 elif xxx
.__class
__ in [xxxWizardPage
, xxxWizardPageSimple
]:
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())
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
)
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
)
1037 # Catch some events, set highlight
1040 wx
.EVT_CLOSE(testWin
, self
.OnCloseTestWin
)
1041 wx
.EVT_SIZE(testWin
, self
.OnSizeTestWin
)
1044 testWin
.panel
.SetDropTarget(DropTarget())
1046 testWin
.SetDropTarget(DropTarget())
1048 testWin
.highLight
= testWin
.highLightDT
= None
1049 if highLight
and not self
.pendingHighLight
:
1050 self
.HighLight(highLight
)
1053 self
.SetItemBold(item
, False)
1054 g
.testWinPos
= g
.testWin
.GetPosition()
1057 inf
= sys
.exc_info()
1058 wx
.LogError(traceback
.format_exception(inf
[0], inf
[1], None)[-1])
1059 wx
.LogError('Error loading resource')
1061 res
.Unload('xxx.xrc')
1062 xrc
.XmlResource
.Set(None)
1063 wx
.MemoryFSHandler
.RemoveFile('xxx.xrc')
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()
1073 def OnCloseTestWin(self
, evt
):
1074 self
.CloseTestWindow()
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)
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
)
1088 # MenuBar is not a child
1089 if not isinstance(self
.GetPyData(prev
), xxxMenuBar
):
1091 prev
= self
.GetPrevSibling(prev
)
1094 # Return item index in parent
1095 def ItemIndex(self
, item
):
1096 n
= 0 # index of sibling
1097 prev
= self
.GetPrevSibling(item
)
1099 prev
= self
.GetPrevSibling(prev
)
1103 # Full tree index of an item - list of positions
1104 def ItemFullIndex(self
, item
):
1105 if not item
.IsOk(): return None
1107 while item
!= self
.root
:
1108 l
.insert(0, self
.ItemIndex(item
))
1109 item
= self
.GetItemParent(item
)
1111 # Get item position from full index
1112 def ItemAtFullIndex(self
, index
):
1113 if index
is None: return wx
.TreeItemId()
1116 item
= self
.GetFirstChild(item
)[0]
1117 for k
in range(i
): item
= self
.GetNextSibling(item
)
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):
1126 return not (self
.IsExpanded(item
) and self
.GetChildrenCount(item
, False))
1128 # Override to use like single-selection tree
1129 def GetSelection(self
):
1130 return self
.selection
1132 def SelectItem(self
, item
):
1134 self
.ChangeSelection(item
)
1135 wx
.TreeCtrl
.SelectItem(self
, item
)
1138 def OnRightDown(self
, evt
):
1139 pullDownMenu
= g
.pullDownMenu
1141 pt
= evt
.GetPosition();
1142 item
, flags
= self
.HitTest(pt
)
1143 if item
.Ok() and flags
& wx
.TREE_HITTEST_ONITEM
:
1144 self
.SelectItem(item
)
1149 item
= self
.selection
1151 menu
.Append(g
.pullDownMenu
.ID_EXPAND
, 'Expand', 'Expand tree')
1152 menu
.Append(g
.pullDownMenu
.ID_COLLAPSE
, 'Collapse', 'Collapse tree')
1154 # self.ctrl = evt.ControlDown() # save Ctrl state
1155 # self.shift = evt.ShiftDown() # and Shift too
1156 m
= wx
.Menu() # create menu
1160 needInsert
= self
.NeedInsert(item
)
1161 if item
== self
.root
or needInsert
and self
.GetItemParent(item
) == self
.root
:
1162 SetMenu(m
, pullDownMenu
.topLevel
)
1164 m
.Append(ID_NEW
.REF
, 'reference...', 'Create object_ref node')
1165 m
.Append(ID_NEW
.COMMENT
, 'comment', 'Create comment node')
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
)
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
])
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
1196 menu
.AppendMenu(wx
.NewId(), 'Insert Child', m
,
1197 'Create child object as the first child')
1199 menu
.AppendMenu(wx
.NewId(), 'Append Child', m
,
1200 'Create child object as the last child')
1203 menu
.AppendMenu(wx
.NewId(), 'Create Sibling', m
,
1204 'Create sibling before selected object')
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
]:
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)
1227 SetMenu(m
, pullDownMenu
.controls
, shift
=True)
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')
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())
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())
1259 def Apply(self
, xxx
, item
):
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()
1276 def OnBeginLabelEdit(self
, evt
):
1277 xxx
= self
.GetPyData(evt
.GetItem())
1283 def OnEndLabelEdit(self
, evt
):
1284 xxx
= self
.GetPyData(evt
.GetItem())
1286 if not xxx
.isElement
:
1287 node
.data
= evt
.GetLabel()
1288 g
.panel
.SetData(xxx
)
1291 ################################################################################
1295 class DropTarget(wx
.PyDropTarget
):
1297 self
.do
= MyDataObject()
1298 wx
.DropTarget
.__init
__(self
, self
.do
)
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
)))
1305 return wx
.DragNone
, ()
1306 item
= g
.frame
.FindObject(g
.testWin
.item
, obj
)
1308 return wx
.DragNone
, ()
1309 xxx
= g
.tree
.GetPyData(item
).treeObject()
1311 # Check if window has a XRC sizer, then use it as parent
1313 sizer
= obj
.GetSizer()
1314 sizerItem
= g
.frame
.FindObject(g
.testWin
.item
, sizer
)
1316 parentItem
= sizerItem
1318 item
= wx
.TreeItemId()
1319 # if not sizer but can have children, it is parent with free placement
1320 elif xxx
.hasChildren
:
1322 item
= wx
.TreeItemId()
1323 # Otherwise, try to add to item's parent
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
)
1331 def OnData(self
, x
, y
, d
):
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')
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
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())
1366 g
.frame
.SetStatusText('Inappropriate drop target')
1374 hl
= g
.testWin
.highLightDT
1377 g
.tree
.SetItemTextColour(hl
.item
, g
.tree
.itemColour
)