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