]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/xxx.py
Add wx.SizerItem.Assign* methods, deprecate coresponding wx.SizerItem.Set* methods
[wxWidgets.git] / wxPython / wx / tools / XRCed / xxx.py
1 # Name: xxx.py ('xxx' is easy to distinguish from 'wx' :) )
2 # Purpose: XML interface classes
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 22.08.2001
5 # RCS-ID: $Id$
6
7 from xml.dom import minidom
8 from globals import *
9 from params import *
10 import traceback, types
11
12 # Base class for interface parameter classes
13 class xxxNode:
14 def __init__(self, node):
15 self.node = node
16 def remove(self):
17 self.node.parentNode.removeChild(self.node)
18 self.node.unlink()
19
20 # Generic (text) parameter class
21 class xxxParam(xxxNode):
22 # Standard use: for text nodes
23 def __init__(self, node):
24 xxxNode.__init__(self, node)
25 if not node.hasChildNodes():
26 # If does not have child nodes, create empty text node
27 text = g.tree.dom.createTextNode('')
28 node.appendChild(text)
29 else:
30 text = node.childNodes[0] # first child must be text node
31 assert text.nodeType == minidom.Node.TEXT_NODE
32 # Append other text nodes if present and delete them
33 extraText = ''
34 for n in node.childNodes[1:]:
35 if n.nodeType == minidom.Node.TEXT_NODE:
36 extraText += n.data
37 node.removeChild(n)
38 n.unlink()
39 else: break
40 if extraText: text.data = text.data + extraText
41 # Use convertion from unicode to current encoding
42 self.textNode = text
43 # Value returns string
44 if wx.USE_UNICODE: # no conversion is needed
45 def value(self):
46 return self.textNode.data
47 def update(self, value):
48 self.textNode.data = value
49 else:
50 def value(self):
51 try:
52 return self.textNode.data.encode(g.currentEncoding)
53 except LookupError:
54 return self.textNode.data.encode()
55 def update(self, value):
56 try: # handle exception if encoding is wrong
57 self.textNode.data = unicode(value, g.currentEncoding)
58 except UnicodeDecodeError:
59 self.textNode.data = unicode(value)
60 #wx.LogMessage("Unicode error: set encoding in file\nglobals.py to something appropriate")
61
62 # Integer parameter
63 class xxxParamInt(xxxParam):
64 # Standard use: for text nodes
65 def __init__(self, node):
66 xxxParam.__init__(self, node)
67 # Value returns string
68 def value(self):
69 try:
70 return int(self.textNode.data)
71 except ValueError:
72 return -1 # invalid value
73 def update(self, value):
74 self.textNode.data = str(value)
75
76 # Content parameter
77 class xxxParamContent(xxxNode):
78 def __init__(self, node):
79 xxxNode.__init__(self, node)
80 data, l = [], [] # data is needed to quicker value retrieval
81 nodes = node.childNodes[:] # make a copy of the child list
82 for n in nodes:
83 if n.nodeType == minidom.Node.ELEMENT_NODE:
84 assert n.tagName == 'item', 'bad content content'
85 if not n.hasChildNodes():
86 # If does not have child nodes, create empty text node
87 text = g.tree.dom.createTextNode('')
88 node.appendChild(text)
89 else:
90 # !!! normalize?
91 text = n.childNodes[0] # first child must be text node
92 assert text.nodeType == minidom.Node.TEXT_NODE
93 l.append(text)
94 data.append(text.data)
95 else: # remove other
96 node.removeChild(n)
97 n.unlink()
98 self.l, self.data = l, data
99 def value(self):
100 return self.data
101 def update(self, value):
102 # If number if items is not the same, recreate children
103 if len(value) != len(self.l): # remove first if number of items has changed
104 childNodes = self.node.childNodes[:]
105 for n in childNodes:
106 self.node.removeChild(n)
107 l = []
108 for str in value:
109 itemElem = g.tree.dom.createElement('item')
110 itemText = g.tree.dom.createTextNode(str)
111 itemElem.appendChild(itemText)
112 self.node.appendChild(itemElem)
113 l.append(itemText)
114 self.l = l
115 else:
116 for i in range(len(value)):
117 self.l[i].data = value[i]
118 self.data = value
119
120 # Content parameter for checklist
121 class xxxParamContentCheckList(xxxNode):
122 def __init__(self, node):
123 xxxNode.__init__(self, node)
124 data, l = [], [] # data is needed to quicker value retrieval
125 nodes = node.childNodes[:] # make a copy of the child list
126 for n in nodes:
127 if n.nodeType == minidom.Node.ELEMENT_NODE:
128 assert n.tagName == 'item', 'bad content content'
129 checked = n.getAttribute('checked')
130 if not checked: checked = 0
131 if not n.hasChildNodes():
132 # If does not have child nodes, create empty text node
133 text = g.tree.dom.createTextNode('')
134 node.appendChild(text)
135 else:
136 # !!! normalize?
137 text = n.childNodes[0] # first child must be text node
138 assert text.nodeType == minidom.Node.TEXT_NODE
139 l.append((text, n))
140 data.append((str(text.data), int(checked)))
141 else: # remove other
142 node.removeChild(n)
143 n.unlink()
144 self.l, self.data = l, data
145 def value(self):
146 return self.data
147 def update(self, value):
148 # If number if items is not the same, recreate children
149 if len(value) != len(self.l): # remove first if number of items has changed
150 childNodes = self.node.childNodes[:]
151 for n in childNodes:
152 self.node.removeChild(n)
153 l = []
154 for s,ch in value:
155 itemElem = g.tree.dom.createElement('item')
156 # Add checked only if True
157 if ch: itemElem.setAttribute('checked', '1')
158 itemText = g.tree.dom.createTextNode(s)
159 itemElem.appendChild(itemText)
160 self.node.appendChild(itemElem)
161 l.append((itemText, itemElem))
162 self.l = l
163 else:
164 for i in range(len(value)):
165 self.l[i][0].data = value[i][0]
166 self.l[i][1].setAttribute('checked', str(value[i][1]))
167 self.data = value
168
169 # Bitmap parameter
170 class xxxParamBitmap(xxxParam):
171 def __init__(self, node):
172 xxxParam.__init__(self, node)
173 self.stock_id = node.getAttribute('stock_id')
174 def value(self):
175 return [self.stock_id, xxxParam.value(self)]
176 def update(self, value):
177 self.stock_id = value[0]
178 if self.stock_id:
179 self.node.setAttribute('stock_id', self.stock_id)
180 elif self.node.hasAttribute('stock_id'):
181 self.node.removeAttribute('stock_id')
182 xxxParam.update(self, value[1])
183
184 ################################################################################
185
186 # Classes to interface DOM objects
187 class xxxObject:
188 # Default behavior
189 hasChildren = False # has children elements?
190 hasStyle = True # almost everyone
191 hasName = True # has name attribute?
192 isSizer = hasChild = False
193 isElement = True
194 allParams = None # Some nodes have no parameters
195 # Style parameters (all optional)
196 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'tooltip']
197 # Special parameters
198 specials = []
199 # Bitmap tags
200 bitmapTags = ['bitmap', 'bitmap2', 'icon']
201 # Required paremeters: none by default
202 required = []
203 # Default parameters with default values
204 default = {}
205 # Parameter types
206 paramDict = {}
207 # Window styles and extended styles
208 winStyles = []
209 # Tree icon index
210 #image = -1
211 # Construct a new xxx object from DOM element
212 # parent is parent xxx object (or None if none), element is DOM element object
213 def __init__(self, parent, element, refElem=None):
214 self.parent = parent
215 self.node = element
216 self.refElem = refElem
217 self.undo = None
218 # Reference are dereferenced
219 if element.tagName == 'object_ref':
220 # Find original object
221 self.ref = element.getAttribute('ref')
222 if refElem:
223 self.className = self.refElem.getAttribute('class')
224 else:
225 self.className = 'xxxUnknown'
226 self.required = []
227 else:
228 # Get attributes
229 self.ref = None
230 self.className = element.getAttribute('class')
231 self.subclass = element.getAttribute('subclass')
232 if self.hasName: self.name = element.getAttribute('name')
233 # Set parameters (text element children)
234 self.params = {}
235 for n in element.childNodes[:]:
236 if n.nodeType == minidom.Node.ELEMENT_NODE:
237 tag = n.tagName
238 if tag in ['object', 'object_ref']:
239 continue # do nothing for object children here
240 elif tag not in self.allParams and tag not in self.styles:
241 print 'WARNING: unknown parameter for %s: %s' % \
242 (self.className, tag)
243 elif tag in self.specials:
244 self.special(tag, n)
245 elif tag == 'content':
246 if self.className == 'wxCheckListBox':
247 self.params[tag] = xxxParamContentCheckList(n)
248 else:
249 self.params[tag] = xxxParamContent(n)
250 elif tag == 'font': # has children
251 self.params[tag] = xxxParamFont(element, n)
252 elif tag in self.bitmapTags:
253 # Can have attributes
254 self.params[tag] = xxxParamBitmap(n)
255 else: # simple parameter
256 self.params[tag] = xxxParam(n)
257 elif n.nodeType == minidom.Node.TEXT_NODE and n.data.isspace():
258 # Remove empty text nodes
259 element.removeChild(n)
260 n.unlink()
261
262 # Check that all required params are set
263 for param in self.required:
264 if not self.params.has_key(param):
265 # If default is specified, set it
266 if self.default.has_key(param):
267 elem = g.tree.dom.createElement(param)
268 if param == 'content':
269 if self.className == 'wxCheckListBox':
270 self.params[param] = xxxParamContentCheckList(elem)
271 else:
272 self.params[param] = xxxParamContent(elem)
273 else:
274 self.params[param] = xxxParam(elem)
275 # Find place to put new element: first present element after param
276 found = False
277 paramStyles = self.allParams + self.styles
278 for p in paramStyles[paramStyles.index(param) + 1:]:
279 # Content params don't have same type
280 if self.params.has_key(p) and p != 'content':
281 found = True
282 break
283 if found:
284 nextTextElem = self.params[p].node
285 self.node.insertBefore(elem, nextTextElem)
286 else:
287 self.node.appendChild(elem)
288 else:
289 wx.LogWarning('Required parameter %s of %s missing' %
290 (param, self.className))
291 # Returns real tree object
292 def treeObject(self):
293 if self.hasChild: return self.child
294 return self
295 # Returns tree image index
296 def treeImage(self):
297 if self.hasChild: return self.child.treeImage()
298 return self.image
299 # Class name plus wx name
300 def treeName(self):
301 if self.hasChild: return self.child.treeName()
302 if self.subclass: className = self.subclass
303 else: className = self.className
304 if self.hasName and self.name: return className + ' "' + self.name + '"'
305 return className
306 # Class name or subclass
307 def panelName(self):
308 if self.subclass: name = self.subclass + '(' + self.className + ')'
309 name = self.className
310 if self.ref: name = 'ref: ' + self.ref + ', ' + name
311 return name
312 # Sets name of tree object
313 def setTreeName(self, name):
314 if self.hasChild: obj = self.child
315 else: obj = self
316 obj.name = name
317 obj.node.setAttribute('name', name)
318 # Set normal (text) params
319 def set(self, param, value):
320 try:
321 self.params[param].update(value)
322 except KeyError:
323 elem = g.tree.dom.createElement(param)
324 p = xxxParam(elem)
325 p.update(value)
326 self.params[param] = p
327 self.node.appendChild(elem)
328 # Special processing for growablecols-like parameters
329 # represented by several nodes
330 def special(self, tag, node):
331 if not self.params.has_key(tag):
332 # Create new multi-group
333 self.params[tag] = xxxParamMulti(node)
334 self.params[tag].append(xxxParamInt(node))
335 def setSpecial(self, param, value):
336 # Straightforward implementation: remove, add again
337 self.params[param].remove()
338 del self.params[param]
339 for i in value:
340 node = g.tree.dom.createElement(param)
341 text = g.tree.dom.createTextNode(str(i))
342 node.appendChild(text)
343 self.node.appendChild(node)
344 self.special(param, node)
345
346 # Imitation of FindResource/DoFindResource from xmlres.cpp
347 def DoFindResource(parent, name, classname, recursive):
348 for n in parent.childNodes:
349 if n.nodeType == minidom.Node.ELEMENT_NODE and \
350 n.tagName in ['object', 'object_ref'] and \
351 n.getAttribute('name') == name:
352 cls = n.getAttribute('class')
353 if not classname or cls == classname: return n
354 if not cls or n.tagName == 'object_ref':
355 refName = n.getAttribute('ref')
356 if not refName: continue
357 refNode = FindResource(refName)
358 if refName and refNode.getAttribute('class') == classname:
359 return n
360 if recursive:
361 for n in parent.childNodes:
362 if n.nodeType == minidom.Node.ELEMENT_NODE and \
363 n.tagName in ['object', 'object_ref']:
364 found = DoFindResource(n, name, classname, True)
365 if found: return found
366 def FindResource(name, classname='', recursive=True):
367 found = DoFindResource(g.tree.mainNode, name, classname, recursive)
368 if found: return found
369 wx.LogError('XRC resource "%s" not found!' % name)
370
371
372 ################################################################################
373
374 # This is a little special: it is both xxxObject and xxxNode
375 class xxxParamFont(xxxObject, xxxNode):
376 allParams = ['size', 'family', 'style', 'weight', 'underlined',
377 'face', 'encoding']
378 def __init__(self, parent, element):
379 xxxObject.__init__(self, parent, element)
380 xxxNode.__init__(self, element)
381 self.parentNode = parent # required to behave similar to DOM node
382 v = []
383 for p in self.allParams:
384 try:
385 v.append(str(self.params[p].value()))
386 except KeyError:
387 v.append('')
388 self.data = v
389 def update(self, value):
390 # `value' is a list of strings corresponding to all parameters
391 elem = self.node
392 # Remove old elements first
393 childNodes = elem.childNodes[:]
394 for node in childNodes: elem.removeChild(node)
395 i = 0
396 self.params.clear()
397 v = []
398 for param in self.allParams:
399 if value[i]:
400 fontElem = g.tree.dom.createElement(param)
401 textNode = g.tree.dom.createTextNode(value[i])
402 self.params[param] = textNode
403 fontElem.appendChild(textNode)
404 elem.appendChild(fontElem)
405 v.append(value[i])
406 i += 1
407 self.data = v
408 def value(self):
409 return self.data
410
411 ################################################################################
412
413 class xxxContainer(xxxObject):
414 hasChildren = True
415 exStyles = []
416
417 # Simulate normal parameter for encoding
418 class xxxEncoding:
419 def value(self):
420 return g.currentEncoding
421 def update(self, val):
422 g.currentEncoding = val
423
424 # Special class for root node
425 class xxxMainNode(xxxContainer):
426 allParams = ['encoding']
427 hasStyle = hasName = False
428 def __init__(self, dom):
429 xxxContainer.__init__(self, None, dom.documentElement)
430 self.className = 'XML tree'
431 # Reset required parameters after processing XML, because encoding is
432 # a little special
433 self.required = ['encoding']
434 self.params['encoding'] = xxxEncoding()
435
436 ################################################################################
437 # Top-level windwows
438
439 class xxxPanel(xxxContainer):
440 allParams = ['pos', 'size', 'style']
441 winStyles = ['wxNO_3D', 'wxTAB_TRAVERSAL']
442 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
443 'tooltip']
444
445 class xxxDialog(xxxContainer):
446 allParams = ['title', 'centered', 'pos', 'size', 'style']
447 paramDict = {'centered': ParamBool}
448 required = ['title']
449 default = {'title': ''}
450 winStyles = ['wxDEFAULT_DIALOG_STYLE', 'wxCAPTION',
451 'wxSTAY_ON_TOP', 'wxSYSTEM_MENU', 'wxTHICK_FRAME',
452 'wxRESIZE_BORDER', 'wxRESIZE_BOX', 'wxCLOSE_BOX',
453 'wxMAXIMIZE_BOX', 'wxMINIMIZE_BOX',
454 'wxDIALOG_MODAL', 'wxDIALOG_MODELESS', 'wxDIALOG_NO_PARENT'
455 'wxNO_3D', 'wxTAB_TRAVERSAL']
456 exStyles = ['wxWS_EX_VALIDATE_RECURSIVELY', 'wxDIALOG_EX_METAL']
457 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
458 'tooltip']
459
460 class xxxFrame(xxxContainer):
461 allParams = ['title', 'centered', 'pos', 'size', 'style']
462 paramDict = {'centered': ParamBool}
463 required = ['title']
464 default = {'title': ''}
465 winStyles = ['wxDEFAULT_FRAME_STYLE', 'wxDEFAULT_DIALOG_STYLE', 'wxCAPTION',
466 'wxSTAY_ON_TOP', 'wxSYSTEM_MENU', 'wxTHICK_FRAME',
467 'wxRESIZE_BORDER', 'wxRESIZE_BOX', 'wxCLOSE_BOX',
468 'wxMAXIMIZE_BOX', 'wxMINIMIZE_BOX',
469 'wxFRAME_NO_TASKBAR', 'wxFRAME_SHAPED', 'wxFRAME_TOOL_WINDOW',
470 'wxFRAME_FLOAT_ON_PARENT',
471 'wxNO_3D', 'wxTAB_TRAVERSAL']
472 exStyles = ['wxWS_EX_VALIDATE_RECURSIVELY', 'wxFRAME_EX_METAL']
473 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
474 'tooltip']
475
476 class xxxTool(xxxObject):
477 allParams = ['bitmap', 'bitmap2', 'radio', 'toggle', 'tooltip', 'longhelp', 'label']
478 required = ['bitmap']
479 paramDict = {'bitmap2': ParamBitmap, 'radio': ParamBool, 'toggle': ParamBool}
480 hasStyle = False
481
482 class xxxToolBar(xxxContainer):
483 allParams = ['bitmapsize', 'margins', 'packing', 'separation', 'dontattachtoframe',
484 'pos', 'size', 'style']
485 hasStyle = False
486 paramDict = {'bitmapsize': ParamPosSize, 'margins': ParamPosSize,
487 'packing': ParamUnit, 'separation': ParamUnit,
488 'dontattachtoframe': ParamBool, 'style': ParamNonGenericStyle}
489 winStyles = ['wxTB_FLAT', 'wxTB_DOCKABLE', 'wxTB_VERTICAL', 'wxTB_HORIZONTAL',
490 'wxTB_3DBUTTONS','wxTB_TEXT', 'wxTB_NOICONS', 'wxTB_NODIVIDER',
491 'wxTB_NOALIGN', 'wxTB_HORZ_LAYOUT', 'wxTB_HORZ_TEXT']
492
493 class xxxStatusBar(xxxObject):
494 hasStyle = False
495 allParams = ['fields', 'widths', 'styles', 'style']
496 paramDict = {'fields': ParamIntNN, 'widths': ParamText, 'styles': ParamText,
497 'style': ParamNonGenericStyle}
498 winStyles = ['wxST_SIZEGRIP']
499
500 class xxxWizard(xxxContainer):
501 allParams = ['title', 'bitmap', 'pos']
502 required = ['title']
503 default = {'title': ''}
504 winStyles = []
505 exStyles = ['wxWIZARD_EX_HELPBUTTON']
506 styles = ['fg', 'bg', 'font', 'exstyle']
507
508 class xxxWizardPage(xxxContainer):
509 allParams = ['bitmap']
510 winStyles = []
511 exStyles = []
512
513 class xxxWizardPageSimple(xxxContainer):
514 allParams = ['bitmap']
515 winStyles = []
516 exStyles = []
517
518 ################################################################################
519 # Bitmap, Icon
520
521 class xxxBitmap(xxxObject):
522 allParams = ['bitmap']
523 required = ['bitmap']
524
525 # Just like bitmap
526 class xxxIcon(xxxObject):
527 allParams = []
528
529 ################################################################################
530 # Controls
531
532 class xxxStaticText(xxxObject):
533 allParams = ['label', 'pos', 'size', 'style']
534 required = ['label']
535 default = {'label': ''}
536 winStyles = ['wxALIGN_LEFT', 'wxALIGN_RIGHT', 'wxALIGN_CENTRE', 'wxST_NO_AUTORESIZE']
537
538 class xxxStaticLine(xxxObject):
539 allParams = ['pos', 'size', 'style']
540 winStyles = ['wxLI_HORIZONTAL', 'wxLI_VERTICAL']
541
542 class xxxStaticBitmap(xxxObject):
543 allParams = ['bitmap', 'pos', 'size', 'style']
544 required = ['bitmap']
545
546 class xxxTextCtrl(xxxObject):
547 allParams = ['value', 'pos', 'size', 'style']
548 winStyles = ['wxTE_NO_VSCROLL',
549 'wxTE_AUTO_SCROLL',
550 'wxTE_PROCESS_ENTER',
551 'wxTE_PROCESS_TAB',
552 'wxTE_MULTILINE',
553 'wxTE_PASSWORD',
554 'wxTE_READONLY',
555 'wxHSCROLL',
556 'wxTE_RICH',
557 'wxTE_RICH2',
558 'wxTE_AUTO_URL',
559 'wxTE_NOHIDESEL',
560 'wxTE_LEFT',
561 'wxTE_CENTRE',
562 'wxTE_RIGHT',
563 'wxTE_DONTWRAP',
564 'wxTE_LINEWRAP',
565 'wxTE_WORDWRAP']
566 paramDict = {'value': ParamMultilineText}
567
568 class xxxChoice(xxxObject):
569 allParams = ['content', 'selection', 'pos', 'size', 'style']
570 required = ['content']
571 default = {'content': '[]'}
572 winStyles = ['wxCB_SORT']
573
574 class xxxSlider(xxxObject):
575 allParams = ['value', 'min', 'max', 'pos', 'size', 'style',
576 'tickfreq', 'pagesize', 'linesize', 'thumb', 'tick',
577 'selmin', 'selmax']
578 paramDict = {'value': ParamInt, 'tickfreq': ParamIntNN, 'pagesize': ParamIntNN,
579 'linesize': ParamIntNN, 'thumb': ParamUnit,
580 'tick': ParamInt, 'selmin': ParamInt, 'selmax': ParamInt}
581 required = ['value', 'min', 'max']
582 winStyles = ['wxSL_HORIZONTAL', 'wxSL_VERTICAL', 'wxSL_AUTOTICKS', 'wxSL_LABELS',
583 'wxSL_LEFT', 'wxSL_RIGHT', 'wxSL_TOP', 'wxSL_BOTTOM',
584 'wxSL_BOTH', 'wxSL_SELRANGE', 'wxSL_INVERSE']
585
586 class xxxGauge(xxxObject):
587 allParams = ['range', 'pos', 'size', 'style', 'value', 'shadow', 'bezel']
588 paramDict = {'range': ParamIntNN, 'value': ParamIntNN,
589 'shadow': ParamIntNN, 'bezel': ParamIntNN}
590 winStyles = ['wxGA_HORIZONTAL', 'wxGA_VERTICAL', 'wxGA_PROGRESSBAR', 'wxGA_SMOOTH']
591
592 class xxxScrollBar(xxxObject):
593 allParams = ['pos', 'size', 'style', 'value', 'thumbsize', 'range', 'pagesize']
594 paramDict = {'value': ParamIntNN, 'range': ParamIntNN, 'thumbsize': ParamIntNN,
595 'pagesize': ParamIntNN}
596 winStyles = ['wxSB_HORIZONTAL', 'wxSB_VERTICAL']
597
598 class xxxListCtrl(xxxObject):
599 allParams = ['pos', 'size', 'style']
600 winStyles = ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON',
601 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE',
602 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER',
603 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING',
604 'wxLC_VIRTUAL', 'wxLC_HRULES', 'wxLC_VRULES', 'wxLC_NO_SORT_HEADER']
605
606 class xxxTreeCtrl(xxxObject):
607 allParams = ['pos', 'size', 'style']
608 winStyles = ['wxTR_EDIT_LABELS',
609 'wxTR_NO_BUTTONS',
610 'wxTR_HAS_BUTTONS',
611 'wxTR_TWIST_BUTTONS',
612 'wxTR_NO_LINES',
613 'wxTR_FULL_ROW_HIGHLIGHT',
614 'wxTR_LINES_AT_ROOT',
615 'wxTR_HIDE_ROOT',
616 'wxTR_ROW_LINES',
617 'wxTR_HAS_VARIABLE_ROW_HEIGHT',
618 'wxTR_SINGLE',
619 'wxTR_MULTIPLE',
620 'wxTR_EXTENDED',
621 'wxTR_DEFAULT_STYLE']
622
623 class xxxHtmlWindow(xxxObject):
624 allParams = ['pos', 'size', 'style', 'borders', 'url', 'htmlcode']
625 paramDict = {'htmlcode':ParamMultilineText}
626 winStyles = ['wxHW_SCROLLBAR_NEVER', 'wxHW_SCROLLBAR_AUTO', 'wxHW_NO_SELECTION']
627
628 class xxxCalendarCtrl(xxxObject):
629 allParams = ['pos', 'size', 'style']
630 winStyles = ['wxCAL_SUNDAY_FIRST', 'wxCAL_MONDAY_FIRST', 'wxCAL_SHOW_HOLIDAYS',
631 'wxCAL_NO_YEAR_CHANGE', 'wxCAL_NO_MONTH_CHANGE',
632 'wxCAL_SEQUENTIAL_MONTH_SELECTION', 'wxCAL_SHOW_SURROUNDING_WEEKS']
633
634 class xxxNotebook(xxxContainer):
635 allParams = ['pos', 'size', 'style']
636 winStyles = ['wxNB_TOP', 'wxNB_LEFT', 'wxNB_RIGHT', 'wxNB_BOTTOM',
637 'wxNB_FIXEDWIDTH', 'wxNB_MULTILINE', 'wxNB_NOPAGETHEME', 'wxNB_FLAT']
638
639 class xxxChoicebook(xxxContainer):
640 allParams = ['pos', 'size', 'style']
641 winStyles = ['wxCHB_DEFAULT', 'wxCHB_LEFT', 'wxCHB_RIGHT', 'wxCHB_TOP', 'wxCHB_BOTTOM']
642
643 class xxxListbook(xxxContainer):
644 allParams = ['pos', 'size', 'style']
645 winStyles = ['wxLB_DEFAULT', 'wxLB_LEFT', 'wxLB_RIGHT', 'wxLB_TOP', 'wxLB_BOTTOM']
646
647 class xxxSplitterWindow(xxxContainer):
648 allParams = ['orientation', 'sashpos', 'minsize', 'pos', 'size', 'style']
649 paramDict = {'orientation': ParamOrientation, 'sashpos': ParamUnit, 'minsize': ParamUnit }
650 winStyles = ['wxSP_3D', 'wxSP_3DSASH', 'wxSP_3DBORDER',
651 'wxSP_FULLSASH', 'wxSP_NOBORDER', 'wxSP_PERMIT_UNSPLIT', 'wxSP_LIVE_UPDATE',
652 'wxSP_NO_XP_THEME' ]
653
654 class xxxGenericDirCtrl(xxxObject):
655 allParams = ['defaultfolder', 'filter', 'defaultfilter', 'pos', 'size', 'style']
656 paramDict = {'defaultfilter': ParamIntNN}
657 winStyles = ['wxDIRCTRL_DIR_ONLY', 'wxDIRCTRL_3D_INTERNAL', 'wxDIRCTRL_SELECT_FIRST',
658 'wxDIRCTRL_SHOW_FILTERS']
659
660 class xxxScrolledWindow(xxxContainer):
661 allParams = ['pos', 'size', 'style']
662 winStyles = ['wxHSCROLL', 'wxVSCROLL', 'wxNO_3D', 'wxTAB_TRAVERSAL']
663
664 class xxxDateCtrl(xxxObject):
665 allParams = ['pos', 'size', 'style', 'borders']
666 winStyles = ['wxDP_DEFAULT', 'wxDP_SPIN', 'wxDP_DROPDOWN',
667 'wxDP_ALLOWNONE', 'wxDP_SHOWCENTURY']
668
669 class xxxGrid(xxxObject):
670 allParams = ['pos', 'size', 'style']
671
672 class xxxFilePickerCtrl(xxxObject):
673 allParams = ['value', 'message', 'wildcard', 'pos', 'size', 'style']
674 winStyles = ['wxFLP_OPEN', 'wxFLP_SAVE', 'wxFLP_OVERWRITE_PROMPT',
675 'wxFLP_FILE_MUST_EXIST', 'wxFLP_CHANGE_DIR',
676 'wxFLP_DEFAULT_STYLE']
677
678
679 ################################################################################
680 # Buttons
681
682 class xxxButton(xxxObject):
683 allParams = ['label', 'default', 'pos', 'size', 'style']
684 paramDict = {'default': ParamBool}
685 required = ['label']
686 winStyles = ['wxBU_LEFT', 'wxBU_TOP', 'wxBU_RIGHT', 'wxBU_BOTTOM', 'wxBU_EXACTFIT',
687 'wxNO_BORDER']
688
689 class xxxBitmapButton(xxxObject):
690 allParams = ['bitmap', 'selected', 'focus', 'disabled', 'default',
691 'pos', 'size', 'style']
692 paramDict = {'selected': ParamBitmap, 'focus': ParamBitmap, 'disabled': ParamBitmap,
693 'default': ParamBool}
694 required = ['bitmap']
695 winStyles = ['wxBU_AUTODRAW', 'wxBU_LEFT', 'wxBU_RIGHT',
696 'wxBU_TOP', 'wxBU_BOTTOM']
697
698 class xxxRadioButton(xxxObject):
699 allParams = ['label', 'value', 'pos', 'size', 'style']
700 paramDict = {'value': ParamBool}
701 required = ['label']
702 winStyles = ['wxRB_GROUP', 'wxRB_SINGLE']
703
704 class xxxSpinButton(xxxObject):
705 allParams = ['value', 'min', 'max', 'pos', 'size', 'style']
706 paramDict = {'value': ParamInt}
707 winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
708
709 class xxxSpinCtrl(xxxObject):
710 allParams = ['value', 'min', 'max', 'pos', 'size', 'style']
711 paramDict = {'value': ParamInt}
712 winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
713
714 class xxxToggleButton(xxxObject):
715 allParams = ['label', 'checked', 'pos', 'size', 'style']
716 paramDict = {'checked': ParamBool}
717 required = ['label']
718
719 ################################################################################
720 # Boxes
721
722 class xxxStaticBox(xxxObject):
723 allParams = ['label', 'pos', 'size', 'style']
724 required = ['label']
725
726 class xxxRadioBox(xxxObject):
727 allParams = ['label', 'content', 'selection', 'dimension', 'pos', 'size', 'style']
728 paramDict = {'dimension': ParamIntNN}
729 required = ['label', 'content']
730 default = {'content': '[]'}
731 winStyles = ['wxRA_SPECIFY_ROWS', 'wxRA_SPECIFY_COLS', 'wxRA_HORIZONTAL',
732 'wxRA_VERTICAL']
733
734 class xxxCheckBox(xxxObject):
735 allParams = ['label', 'checked', 'pos', 'size', 'style']
736 paramDict = {'checked': ParamBool}
737 winStyles = ['wxCHK_2STATE', 'wxCHK_3STATE', 'wxCHK_ALLOW_3RD_STATE_FOR_USER',
738 'wxALIGN_RIGHT']
739 required = ['label']
740
741 class xxxComboBox(xxxObject):
742 allParams = ['content', 'selection', 'value', 'pos', 'size', 'style']
743 required = ['content']
744 default = {'content': '[]'}
745 winStyles = ['wxCB_SIMPLE', 'wxCB_SORT', 'wxCB_READONLY', 'wxCB_DROPDOWN']
746
747 class xxxListBox(xxxObject):
748 allParams = ['content', 'selection', 'pos', 'size', 'style']
749 required = ['content']
750 default = {'content': '[]'}
751 winStyles = ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL',
752 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT']
753
754 class xxxCheckList(xxxObject):
755 allParams = ['content', 'pos', 'size', 'style']
756 required = ['content']
757 default = {'content': '[]'}
758 winStyles = ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL',
759 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT']
760 paramDict = {'content': ParamContentCheckList}
761
762 ################################################################################
763 # Sizers
764
765 class xxxSizer(xxxContainer):
766 hasName = hasStyle = False
767 paramDict = {'orient': ParamOrient}
768 isSizer = True
769 itemTag = 'sizeritem' # different for some sizers
770
771 class xxxBoxSizer(xxxSizer):
772 allParams = ['orient']
773 required = ['orient']
774 default = {'orient': 'wxVERTICAL'}
775 # Tree icon depends on orientation
776 def treeImage(self):
777 if self.params['orient'].value() == 'wxHORIZONTAL': return self.imageH
778 else: return self.imageV
779
780 class xxxStaticBoxSizer(xxxBoxSizer):
781 allParams = ['label', 'orient']
782 required = ['label', 'orient']
783
784 class xxxGridSizer(xxxSizer):
785 allParams = ['cols', 'rows', 'vgap', 'hgap']
786 required = ['cols']
787 default = {'cols': '2', 'rows': '2'}
788
789 class xxxStdDialogButtonSizer(xxxSizer):
790 allParams = []
791 itemTag = 'button'
792
793 # For repeated parameters
794 class xxxParamMulti:
795 def __init__(self, node):
796 self.node = node
797 self.l, self.data = [], []
798 def append(self, param):
799 self.l.append(param)
800 self.data.append(param.value())
801 def value(self):
802 return self.data
803 def remove(self):
804 for param in self.l:
805 param.remove()
806 self.l, self.data = [], []
807
808 class xxxFlexGridSizer(xxxGridSizer):
809 specials = ['growablecols', 'growablerows']
810 allParams = ['cols', 'rows', 'vgap', 'hgap'] + specials
811 paramDict = {'growablecols': ParamIntList, 'growablerows': ParamIntList}
812
813 class xxxGridBagSizer(xxxSizer):
814 specials = ['growablecols', 'growablerows']
815 allParams = ['vgap', 'hgap'] + specials
816 paramDict = {'growablecols': ParamIntList, 'growablerows': ParamIntList}
817
818 # Container with only one child.
819 # Not shown in tree.
820 class xxxChildContainer(xxxObject):
821 hasName = hasStyle = False
822 hasChild = True
823 def __init__(self, parent, element, refElem=None):
824 xxxObject.__init__(self, parent, element, refElem)
825 # Must have one child with 'object' tag, but we don't check it
826 nodes = element.childNodes[:] # create copy
827 for node in nodes:
828 if node.nodeType == minidom.Node.ELEMENT_NODE:
829 if node.tagName in ['object', 'object_ref']:
830 # Create new xxx object for child node
831 self.child = MakeXXXFromDOM(self, node)
832 self.child.parent = parent
833 # Copy hasChildren and isSizer attributes
834 self.hasChildren = self.child.hasChildren
835 self.isSizer = self.child.isSizer
836 return # success
837 else:
838 element.removeChild(node)
839 node.unlink()
840 assert 0, 'no child found'
841 def resetChild(self, xxx):
842 '''Reset child info (for replacing with another class).'''
843 self.child = xxx
844 self.hasChildren = xxx.hasChildren
845 self.isSizer = xxx.isSizer
846
847 class xxxSizerItem(xxxChildContainer):
848 allParams = ['option', 'flag', 'border', 'minsize', 'ratio']
849 paramDict = {'option': ParamInt, 'minsize': ParamPosSize, 'ratio': ParamPosSize}
850 defaults_panel = {}
851 defaults_control = {}
852 def __init__(self, parent, element, refElem=None):
853 # For GridBag sizer items, extra parameters added
854 if isinstance(parent, xxxGridBagSizer):
855 self.allParams = self.allParams + ['cellpos', 'cellspan']
856 xxxChildContainer.__init__(self, parent, element, refElem)
857 # Remove pos parameter - not needed for sizeritems
858 if 'pos' in self.child.allParams:
859 self.child.allParams = self.child.allParams[:]
860 self.child.allParams.remove('pos')
861 def resetChild(self, xxx):
862 xxxChildContainer.resetChild(self, xxx)
863 # Remove pos parameter - not needed for sizeritems
864 if 'pos' in self.child.allParams:
865 self.child.allParams = self.child.allParams[:]
866 self.child.allParams.remove('pos')
867
868 class xxxSizerItemButton(xxxSizerItem):
869 allParams = []
870 paramDict = {}
871 def __init__(self, parent, element, refElem=None):
872 xxxChildContainer.__init__(self, parent, element, refElem=None)
873 # Remove pos parameter - not needed for sizeritems
874 if 'pos' in self.child.allParams:
875 self.child.allParams = self.child.allParams[:]
876 self.child.allParams.remove('pos')
877
878 class xxxPage(xxxChildContainer):
879 allParams = ['label', 'selected']
880 paramDict = {'selected': ParamBool}
881 required = ['label']
882 def __init__(self, parent, element, refElem=None):
883 xxxChildContainer.__init__(self, parent, element, refElem)
884 # pos and size dont matter for notebookpages
885 if 'pos' in self.child.allParams:
886 self.child.allParams = self.child.allParams[:]
887 self.child.allParams.remove('pos')
888 if 'size' in self.child.allParams:
889 self.child.allParams = self.child.allParams[:]
890 self.child.allParams.remove('size')
891
892 class xxxSpacer(xxxObject):
893 hasName = hasStyle = False
894 allParams = ['size', 'option', 'flag', 'border']
895 paramDict = {'option': ParamInt}
896 default = {'size': '0,0'}
897 def __init__(self, parent, element, refElem=None):
898 # For GridBag sizer items, extra parameters added
899 if isinstance(parent, xxxGridBagSizer):
900 self.allParams = self.allParams + ['cellpos', 'cellspan']
901 xxxObject.__init__(self, parent, element, refElem)
902
903 class xxxMenuBar(xxxContainer):
904 allParams = ['style']
905 paramDict = {'style': ParamNonGenericStyle} # no generic styles
906 winStyles = ['wxMB_DOCKABLE']
907
908 class xxxMenu(xxxContainer):
909 allParams = ['label', 'help', 'style']
910 default = {'label': ''}
911 paramDict = {'style': ParamNonGenericStyle} # no generic styles
912 winStyles = ['wxMENU_TEAROFF']
913
914 class xxxMenuItem(xxxObject):
915 allParams = ['label', 'bitmap', 'accel', 'help',
916 'checkable', 'radio', 'enabled', 'checked']
917 default = {'label': ''}
918 hasStyle = False
919
920 class xxxSeparator(xxxObject):
921 hasName = hasStyle = False
922
923 ################################################################################
924 # Unknown control
925
926 class xxxUnknown(xxxObject):
927 allParams = ['pos', 'size', 'style']
928 winStyles = ['wxNO_FULL_REPAINT_ON_RESIZE']
929
930 ################################################################################
931 # Comment
932
933 _handlers = [] # custom handler classes/funcs
934 def getHandlers():
935 return _handlers
936 def setHandlers(handlers):
937 global _handlers
938 _handlers = handlers
939 _CFuncPtr = None # ctypes function type
940
941 def register(hndlr):
942 """Register hndlr function or XmlResourceHandler class."""
943 if _CFuncPtr and isinstance(hndlr, _CFuncPtr):
944 _handlers.append(hndlr)
945 return
946 if not isinstance(hndlr, type):
947 wx.LogError('handler is not a type: %s' % hndlr)
948 elif not issubclass(hndlr, wx.xrc.XmlResourceHandler):
949 wx.LogError('handler is not a XmlResourceHandler: %s' % hndlr)
950 else:
951 _handlers.append(hndlr)
952
953 def load_dl(path, localname=''):
954 """Load shared/dynamic library into xxx namespace.
955
956 If path is not absolute or relative, try to find in the module's directory.
957 """
958 if not localname:
959 localname = os.path.basename(os.path.splitext(path)[0])
960 try:
961 import ctypes
962 global _CFuncPtr
963 _CFuncPtr = ctypes._CFuncPtr # use as a flag of loaded ctypes
964 #if not os.path.dirname(path) and os.path.isfile(path):
965
966 except ImportError:
967 wx.LogError('ctypes module not found')
968 globals()[localname] = None
969 return
970 try:
971 dl = ctypes.CDLL(path)
972 globals()[localname] = dl
973 # Register AddXmlHandlers() if exists
974 try:
975 register(dl.AddXmlHandlers)
976 except:
977 pass
978 except:
979 wx.LogError('error loading dynamic library: %s' % path)
980 print traceback.print_exc()
981
982 # Called when creating test window
983 def addHandlers():
984 for h in _handlers:
985 if _CFuncPtr and isinstance(h, _CFuncPtr):
986 try:
987 apply(h, ())
988 except:
989 wx.LogError('error calling DL func: "%s"' % h)
990 print traceback.print_exc()
991 else:
992 try:
993 xrc.XmlResource.Get().AddHandler(apply(h, ()))
994 except:
995 wx.LogError('error adding XmlHandler: "%s"' % h)
996 print traceback.print_exc()
997
998 def custom(klassName, klass='unknown'):
999 """Define custom control based on xrcClass.
1000
1001 klass: new object name
1002 xrcClass: name of an existing XRC object class or
1003 a class object defining class parameters.
1004 """
1005 if type(klass) is str:
1006 # Copy correct xxx class under new name
1007 kl = xxxDict[klass]
1008 xxxClass = types.ClassType('xxx' + klassName, kl.__bases__, kl.__dict__)
1009 else:
1010 xxxClass = klass
1011 # Register param IDs
1012 for param in klass.allParams + klass.paramDict.keys():
1013 if not paramIDs.has_key(param):
1014 paramIDs[param] = wx.NewId()
1015 # Insert in dictionaty
1016 xxxDict[klassName] = xxxClass
1017 # Add to menu
1018 g.pullDownMenu.addCustom(klassName)
1019
1020 class xxxParamComment(xxxParam):
1021 locals = {} # namespace for comment directives
1022 allow = None # undefined initial state for current file
1023 def __init__(self, node):
1024 xxxNode.__init__(self, node)
1025 self.textNode = node
1026 # Parse "pragma" comments if enabled
1027 if node.data and node.data[0] == '%' and g.conf.allowExec != 'no' and \
1028 xxxParamComment.allow is not False:
1029 # Show warning
1030 if g.conf.allowExec == 'ask' and xxxParamComment.allow is None:
1031 flags = wx.ICON_EXCLAMATION | wx.YES_NO | wx.CENTRE
1032 dlg = wx.MessageDialog(g.frame, '''
1033 This file contains executable %comment directives. Allow to execute?''',
1034 'Warning', flags)
1035 say = dlg.ShowModal()
1036 dlg.Destroy()
1037 if say == wx.ID_YES:
1038 xxxParamComment.allow = True
1039 else:
1040 xxxParamComment.allow = False
1041 try:
1042 code = node.data[1:]
1043 exec code in globals(), self.locals
1044 except:
1045 wx.LogError('exec error: "%s"' % code)
1046 print traceback.print_exc()
1047
1048 class xxxComment(xxxObject):
1049 hasStyle = hasName = False
1050 allParams = required = ['comment']
1051
1052 def __init__(self, parent, node):
1053 self.parent = parent
1054 self.node = node
1055 self.isElement = False
1056 self.undo = None
1057 self.className = 'comment'
1058 self.ref = self.subclass = None
1059 self.params = {'comment': xxxParamComment(node)}
1060
1061 def treeName(self):
1062 # Replace newlines by \n to avoid tree item resizing
1063 return self.params['comment'].value().replace('\n', r'\n')
1064
1065 ################################################################################
1066
1067 # Mapping of XRC names to xxx classes
1068 xxxDict = {
1069 'wxPanel': xxxPanel,
1070 'wxDialog': xxxDialog,
1071 'wxFrame': xxxFrame,
1072 'tool': xxxTool,
1073 'wxToolBar': xxxToolBar,
1074 'wxStatusBar': xxxStatusBar,
1075 'wxWizard': xxxWizard,
1076 'wxWizardPage': xxxWizardPage,
1077 'wxWizardPageSimple': xxxWizardPageSimple,
1078
1079 'wxBitmap': xxxBitmap,
1080 'wxIcon': xxxIcon,
1081
1082 'wxButton': xxxButton,
1083 'wxBitmapButton': xxxBitmapButton,
1084 'wxRadioButton': xxxRadioButton,
1085 'wxSpinButton': xxxSpinButton,
1086 'wxToggleButton' : xxxToggleButton,
1087
1088 'wxStaticBox': xxxStaticBox,
1089 'wxStaticBitmap': xxxStaticBitmap,
1090 'wxRadioBox': xxxRadioBox,
1091 'wxComboBox': xxxComboBox,
1092 'wxCheckBox': xxxCheckBox,
1093 'wxListBox': xxxListBox,
1094
1095 'wxStaticText': xxxStaticText,
1096 'wxStaticLine': xxxStaticLine,
1097 'wxTextCtrl': xxxTextCtrl,
1098 'wxChoice': xxxChoice,
1099 'wxSlider': xxxSlider,
1100 'wxGauge': xxxGauge,
1101 'wxScrollBar': xxxScrollBar,
1102 'wxTreeCtrl': xxxTreeCtrl,
1103 'wxListCtrl': xxxListCtrl,
1104 'wxCheckListBox': xxxCheckList,
1105 'notebookpage': xxxPage,
1106 'choicebookpage': xxxPage,
1107 'listbookpage': xxxPage,
1108 'wxNotebook': xxxNotebook,
1109 'wxChoicebook': xxxChoicebook,
1110 'wxListbook': xxxListbook,
1111 'wxSplitterWindow': xxxSplitterWindow,
1112 'wxHtmlWindow': xxxHtmlWindow,
1113 'wxCalendarCtrl': xxxCalendarCtrl,
1114 'wxGenericDirCtrl': xxxGenericDirCtrl,
1115 'wxSpinCtrl': xxxSpinCtrl,
1116 'wxScrolledWindow': xxxScrolledWindow,
1117 'wxGrid': xxxGrid,
1118 'wxFilePickerCtrl': xxxFilePickerCtrl,
1119 'wxDatePickerCtrl': xxxDateCtrl,
1120
1121 'wxBoxSizer': xxxBoxSizer,
1122 'wxStaticBoxSizer': xxxStaticBoxSizer,
1123 'wxGridSizer': xxxGridSizer,
1124 'wxFlexGridSizer': xxxFlexGridSizer,
1125 'wxGridBagSizer': xxxGridBagSizer,
1126 'wxStdDialogButtonSizer': xxxStdDialogButtonSizer,
1127 'sizeritem': xxxSizerItem, 'button': xxxSizerItemButton,
1128 'spacer': xxxSpacer,
1129
1130 'wxMenuBar': xxxMenuBar,
1131 'wxMenu': xxxMenu,
1132 'wxMenuItem': xxxMenuItem,
1133 'separator': xxxSeparator,
1134
1135 'unknown': xxxUnknown,
1136 'comment': xxxComment,
1137 }
1138
1139 # Create IDs for all parameters of all classes
1140 paramIDs = {'fg': wx.NewId(), 'bg': wx.NewId(), 'exstyle': wx.NewId(), 'font': wx.NewId(),
1141 'enabled': wx.NewId(), 'focused': wx.NewId(), 'hidden': wx.NewId(),
1142 'tooltip': wx.NewId(), 'encoding': wx.NewId(),
1143 'cellpos': wx.NewId(), 'cellspan': wx.NewId(),
1144 'text': wx.NewId()
1145 }
1146 for cl in xxxDict.values():
1147 if cl.allParams:
1148 for param in cl.allParams + cl.paramDict.keys():
1149 if not paramIDs.has_key(param):
1150 paramIDs[param] = wx.NewId()
1151
1152 ################################################################################
1153 # Helper functions
1154
1155 # Test for object elements
1156 def IsObject(node):
1157 return node.nodeType == minidom.Node.ELEMENT_NODE and \
1158 node.tagName in ['object', 'object_ref'] or \
1159 node.nodeType == minidom.Node.COMMENT_NODE
1160
1161 # Make XXX object from some DOM object, selecting correct class
1162 def MakeXXXFromDOM(parent, node):
1163 if node.nodeType == minidom.Node.COMMENT_NODE:
1164 return xxxComment(parent, node)
1165 if node.tagName == 'object_ref':
1166 ref = node.getAttribute('ref')
1167 refElem = FindResource(ref)
1168 if refElem: cls = refElem.getAttribute('class')
1169 else: return xxxUnknown(parent, node)
1170 else:
1171 refElem = None
1172 cls = node.getAttribute('class')
1173 try:
1174 klass = xxxDict[cls]
1175 except KeyError:
1176 # If we encounter a weird class, use unknown template
1177 print 'WARNING: unsupported class:', node.getAttribute('class')
1178 klass = xxxUnknown
1179 return klass(parent, node, refElem)
1180
1181 # Make empty DOM element
1182 def MakeEmptyDOM(className):
1183 elem = g.tree.dom.createElement('object')
1184 elem.setAttribute('class', className)
1185 # Set required and default parameters
1186 xxxClass = xxxDict[className]
1187 defaultNotRequired = filter(lambda x, l=xxxClass.required: x not in l,
1188 xxxClass.default.keys())
1189 for param in xxxClass.required + defaultNotRequired:
1190 textElem = g.tree.dom.createElement(param)
1191 try:
1192 textNode = g.tree.dom.createTextNode(xxxClass.default[param])
1193 except KeyError:
1194 textNode = g.tree.dom.createTextNode('')
1195 textElem.appendChild(textNode)
1196 elem.appendChild(textElem)
1197 return elem
1198
1199 # Make empty XXX object
1200 def MakeEmptyXXX(parent, className):
1201 # Make corresponding DOM object first
1202 elem = MakeEmptyDOM(className)
1203 # Special handling, e.g. if parent is a sizer, we should create
1204 # sizeritem object, except for spacers, etc.
1205 if parent:
1206 if parent.isSizer and className != 'spacer':
1207 sizerItemElem = MakeEmptyDOM(parent.itemTag)
1208 sizerItemElem.appendChild(elem)
1209 elem = sizerItemElem
1210 elif isinstance(parent, xxxNotebook):
1211 pageElem = MakeEmptyDOM('notebookpage')
1212 pageElem.appendChild(elem)
1213 elem = pageElem
1214 elif isinstance(parent, xxxChoicebook):
1215 pageElem = MakeEmptyDOM('choicebookpage')
1216 pageElem.appendChild(elem)
1217 elem = pageElem
1218 elif isinstance(parent, xxxListbook):
1219 pageElem = MakeEmptyDOM('listbookpage')
1220 pageElem.appendChild(elem)
1221 elem = pageElem
1222 # Now just make object
1223 xxx = MakeXXXFromDOM(parent, elem)
1224 # Special defaults for new panels and controls
1225 if isinstance(xxx, xxxSizerItem):
1226 if isinstance(xxx.child, xxxContainer) and not xxx.child.isSizer:
1227 for param,v in xxxSizerItem.defaults_panel.items():
1228 xxx.set(param, v)
1229 elif isinstance(xxx.child, xxxObject):
1230 for param,v in xxxSizerItem.defaults_control.items():
1231 xxx.set(param, v)
1232 return xxx
1233
1234 # Make empty DOM element for reference
1235 def MakeEmptyRefDOM(ref):
1236 elem = g.tree.dom.createElement('object_ref')
1237 elem.setAttribute('ref', ref)
1238 return elem
1239
1240 # Make empty XXX object
1241 def MakeEmptyRefXXX(parent, ref):
1242 # Make corresponding DOM object first
1243 elem = MakeEmptyRefDOM(ref)
1244 # If parent is a sizer, we should create sizeritem object, except for spacers
1245 if parent:
1246 if parent.isSizer:
1247 sizerItemElem = MakeEmptyDOM(parent.itemTag)
1248 sizerItemElem.appendChild(elem)
1249 elem = sizerItemElem
1250 elif isinstance(parent, xxxNotebook):
1251 pageElem = MakeEmptyDOM('notebookpage')
1252 pageElem.appendChild(elem)
1253 elem = pageElem
1254 elif isinstance(parent, xxxChoicebook):
1255 pageElem = MakeEmptyDOM('choicebookpage')
1256 pageElem.appendChild(elem)
1257 elem = pageElem
1258 elif isinstance(parent, xxxListbook):
1259 pageElem = MakeEmptyDOM('listbookpage')
1260 pageElem.appendChild(elem)
1261 elem = pageElem
1262 # Now just make object
1263 xxx = MakeXXXFromDOM(parent, elem)
1264 # Label is not used for references
1265 xxx.allParams = xxx.allParams[:]
1266 #xxx.allParams.remove('label')
1267 return xxx
1268
1269 # Make empty comment node
1270 def MakeEmptyCommentDOM():
1271 node = g.tree.dom.createComment('')
1272 return node
1273
1274 # Make empty xxxComment
1275 def MakeEmptyCommentXXX(parent):
1276 node = MakeEmptyCommentDOM()
1277 # Now just make object
1278 xxx = MakeXXXFromDOM(parent, node)
1279 return xxx
1280