]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/xxx.py
Log the exception when writing to file fails.
[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
11 # Base class for interface parameter classes
12 class xxxNode:
13 def __init__(self, node):
14 self.node = node
15 def remove(self):
16 self.node.parentNode.removeChild(self.node)
17 self.node.unlink()
18
19 # Generic (text) parameter class
20 class xxxParam(xxxNode):
21 # Standard use: for text nodes
22 def __init__(self, node):
23 xxxNode.__init__(self, node)
24 if not node.hasChildNodes():
25 # If does not have child nodes, create empty text node
26 text = g.tree.dom.createTextNode('')
27 node.appendChild(text)
28 else:
29 text = node.childNodes[0] # first child must be text node
30 assert text.nodeType == minidom.Node.TEXT_NODE
31 # Append other text nodes if present and delete them
32 extraText = ''
33 for n in node.childNodes[1:]:
34 if n.nodeType == minidom.Node.TEXT_NODE:
35 extraText += n.data
36 node.removeChild(n)
37 n.unlink()
38 else: break
39 if extraText: text.data = text.data + extraText
40 # Use convertion from unicode to current encoding
41 self.textNode = text
42 # Value returns string
43 if wxUSE_UNICODE: # no conversion is needed
44 def value(self):
45 return self.textNode.data
46 def update(self, value):
47 self.textNode.data = value
48 else:
49 def value(self):
50 try:
51 return self.textNode.data.encode(g.currentEncoding)
52 except LookupError:
53 return self.textNode.data.encode()
54 def update(self, value):
55 try: # handle exception if encoding is wrong
56 self.textNode.data = unicode(value, g.currentEncoding)
57 except UnicodeDecodeError:
58 self.textNode.data = unicode(value)
59 #wxLogMessage("Unicode error: set encoding in file\nglobals.py to something appropriate")
60
61 # Integer parameter
62 class xxxParamInt(xxxParam):
63 # Standard use: for text nodes
64 def __init__(self, node):
65 xxxParam.__init__(self, node)
66 # Value returns string
67 def value(self):
68 try:
69 return int(self.textNode.data)
70 except ValueError:
71 return -1 # invalid value
72 def update(self, value):
73 self.textNode.data = str(value)
74
75 # Content parameter
76 class xxxParamContent(xxxNode):
77 def __init__(self, node):
78 xxxNode.__init__(self, node)
79 data, l = [], [] # data is needed to quicker value retrieval
80 nodes = node.childNodes[:] # make a copy of the child list
81 for n in nodes:
82 if n.nodeType == minidom.Node.ELEMENT_NODE:
83 assert n.tagName == 'item', 'bad content content'
84 if not n.hasChildNodes():
85 # If does not have child nodes, create empty text node
86 text = g.tree.dom.createTextNode('')
87 node.appendChild(text)
88 else:
89 # !!! normalize?
90 text = n.childNodes[0] # first child must be text node
91 assert text.nodeType == minidom.Node.TEXT_NODE
92 l.append(text)
93 data.append(str(text.data))
94 else: # remove other
95 node.removeChild(n)
96 n.unlink()
97 self.l, self.data = l, data
98 def value(self):
99 return self.data
100 def update(self, value):
101 # If number if items is not the same, recreate children
102 if len(value) != len(self.l): # remove first if number of items has changed
103 childNodes = self.node.childNodes[:]
104 for n in childNodes:
105 self.node.removeChild(n)
106 l = []
107 for str in value:
108 itemElem = g.tree.dom.createElement('item')
109 itemText = g.tree.dom.createTextNode(str)
110 itemElem.appendChild(itemText)
111 self.node.appendChild(itemElem)
112 l.append(itemText)
113 self.l = l
114 else:
115 for i in range(len(value)):
116 self.l[i].data = value[i]
117 self.data = value
118
119 # Content parameter for checklist
120 class xxxParamContentCheckList(xxxNode):
121 def __init__(self, node):
122 xxxNode.__init__(self, node)
123 data, l = [], [] # data is needed to quicker value retrieval
124 nodes = node.childNodes[:] # make a copy of the child list
125 for n in nodes:
126 if n.nodeType == minidom.Node.ELEMENT_NODE:
127 assert n.tagName == 'item', 'bad content content'
128 checked = n.getAttribute('checked')
129 if not checked: checked = 0
130 if not n.hasChildNodes():
131 # If does not have child nodes, create empty text node
132 text = g.tree.dom.createTextNode('')
133 node.appendChild(text)
134 else:
135 # !!! normalize?
136 text = n.childNodes[0] # first child must be text node
137 assert text.nodeType == minidom.Node.TEXT_NODE
138 l.append((text, n))
139 data.append((str(text.data), int(checked)))
140 else: # remove other
141 node.removeChild(n)
142 n.unlink()
143 self.l, self.data = l, data
144 def value(self):
145 return self.data
146 def update(self, value):
147 # If number if items is not the same, recreate children
148 if len(value) != len(self.l): # remove first if number of items has changed
149 childNodes = self.node.childNodes[:]
150 for n in childNodes:
151 self.node.removeChild(n)
152 l = []
153 for s,ch in value:
154 itemElem = g.tree.dom.createElement('item')
155 # Add checked only if True
156 if ch: itemElem.setAttribute('checked', '1')
157 itemText = g.tree.dom.createTextNode(s)
158 itemElem.appendChild(itemText)
159 self.node.appendChild(itemElem)
160 l.append((itemText, itemElem))
161 self.l = l
162 else:
163 for i in range(len(value)):
164 self.l[i][0].data = value[i][0]
165 self.l[i][1].setAttribute('checked', str(value[i][1]))
166 self.data = value
167
168 # Bitmap parameter
169 class xxxParamBitmap(xxxParam):
170 def __init__(self, node):
171 xxxParam.__init__(self, node)
172 self.stock_id = node.getAttribute('stock_id')
173 def value(self):
174 return [self.stock_id, xxxParam.value(self)]
175 def update(self, value):
176 self.stock_id = value[0]
177 if self.stock_id:
178 self.node.setAttribute('stock_id', self.stock_id)
179 elif self.node.hasAttribute('stock_id'):
180 self.node.removeAttribute('stock_id')
181 xxxParam.update(self, value[1])
182
183 ################################################################################
184
185 # Classes to interface DOM objects
186 class xxxObject:
187 # Default behavior
188 hasChildren = False # has children elements?
189 hasStyle = True # almost everyone
190 hasName = True # has name attribute?
191 isSizer = hasChild = False
192 allParams = None # Some nodes have no parameters
193 # Style parameters (all optional)
194 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'tooltip']
195 # Special parameters
196 specials = []
197 # Bitmap tags
198 bitmapTags = ['bitmap', 'bitmap2', 'icon']
199 # Required paremeters: none by default
200 required = []
201 # Default parameters with default values
202 default = {}
203 # Parameter types
204 paramDict = {}
205 # Window styles and extended styles
206 winStyles = []
207 # Tree icon index
208 #image = -1
209 # Construct a new xxx object from DOM element
210 # parent is parent xxx object (or None if none), element is DOM element object
211 def __init__(self, parent, element):
212 self.parent = parent
213 self.element = element
214 self.undo = None
215 # Get attributes
216 self.className = element.getAttribute('class')
217 self.subclass = element.getAttribute('subclass')
218 if self.hasName: self.name = element.getAttribute('name')
219 # Set parameters (text element children)
220 self.params = {}
221 nodes = element.childNodes[:]
222 for node in nodes:
223 if node.nodeType == minidom.Node.ELEMENT_NODE:
224 tag = node.tagName
225 if tag == 'object':
226 continue # do nothing for object children here
227 if tag not in self.allParams and tag not in self.styles:
228 print 'WARNING: unknown parameter for %s: %s' % \
229 (self.className, tag)
230 elif tag in self.specials:
231 self.special(tag, node)
232 elif tag == 'content':
233 if self.className == 'wxCheckListBox':
234 self.params[tag] = xxxParamContentCheckList(node)
235 else:
236 self.params[tag] = xxxParamContent(node)
237 elif tag == 'font': # has children
238 self.params[tag] = xxxParamFont(element, node)
239 elif tag in self.bitmapTags:
240 # Can have attributes
241 self.params[tag] = xxxParamBitmap(node)
242 else: # simple parameter
243 self.params[tag] = xxxParam(node)
244 else:
245 pass
246 # Remove all other nodes
247 # element.removeChild(node)
248 # node.unlink()
249
250 # Check that all required params are set
251 for param in self.required:
252 if not self.params.has_key(param):
253 # If default is specified, set it
254 if self.default.has_key(param):
255 elem = g.tree.dom.createElement(param)
256 if param == 'content':
257 if self.className == 'wxCheckListBox':
258 self.params[param] = xxxParamContentCheckList(elem)
259 else:
260 self.params[param] = xxxParamContent(elem)
261 else:
262 self.params[param] = xxxParam(elem)
263 # Find place to put new element: first present element after param
264 found = False
265 paramStyles = self.allParams + self.styles
266 for p in paramStyles[paramStyles.index(param) + 1:]:
267 # Content params don't have same type
268 if self.params.has_key(p) and p != 'content':
269 found = True
270 break
271 if found:
272 nextTextElem = self.params[p].node
273 self.element.insertBefore(elem, nextTextElem)
274 else:
275 self.element.appendChild(elem)
276 else:
277 wxLogWarning('Required parameter %s of %s missing' %
278 (param, self.className))
279 # Returns real tree object
280 def treeObject(self):
281 if self.hasChild: return self.child
282 return self
283 # Returns tree image index
284 def treeImage(self):
285 if self.hasChild: return self.child.treeImage()
286 return self.image
287 # Class name plus wx name
288 def treeName(self):
289 if self.hasChild: return self.child.treeName()
290 if self.subclass: className = self.subclass
291 else: className = self.className
292 if self.hasName and self.name: return className + ' "' + self.name + '"'
293 return className
294 # Class name or subclass
295 def panelName(self):
296 if self.subclass: return self.subclass + '(' + self.className + ')'
297 else: return self.className
298
299 ################################################################################
300
301 # This is a little special: it is both xxxObject and xxxNode
302 class xxxParamFont(xxxObject, xxxNode):
303 allParams = ['size', 'family', 'style', 'weight', 'underlined',
304 'face', 'encoding']
305 def __init__(self, parent, element):
306 xxxObject.__init__(self, parent, element)
307 xxxNode.__init__(self, element)
308 self.parentNode = parent # required to behave similar to DOM node
309 v = []
310 for p in self.allParams:
311 try:
312 v.append(str(self.params[p].value()))
313 except KeyError:
314 v.append('')
315 self.data = v
316 def update(self, value):
317 # `value' is a list of strings corresponding to all parameters
318 elem = self.element
319 # Remove old elements first
320 childNodes = elem.childNodes[:]
321 for node in childNodes: elem.removeChild(node)
322 i = 0
323 self.params.clear()
324 v = []
325 for param in self.allParams:
326 if value[i]:
327 fontElem = g.tree.dom.createElement(param)
328 textNode = g.tree.dom.createTextNode(value[i])
329 self.params[param] = textNode
330 fontElem.appendChild(textNode)
331 elem.appendChild(fontElem)
332 v.append(value[i])
333 i += 1
334 self.data = v
335 def value(self):
336 return self.data
337
338 ################################################################################
339
340 class xxxContainer(xxxObject):
341 hasChildren = True
342 exStyles = []
343
344 # Simulate normal parameter for encoding
345 class xxxEncoding:
346 def value(self):
347 return g.currentEncoding
348 def update(self, val):
349 g.currentEncoding = val
350
351 # Special class for root node
352 class xxxMainNode(xxxContainer):
353 allParams = ['encoding']
354 hasStyle = hasName = False
355 def __init__(self, dom):
356 xxxContainer.__init__(self, None, dom.documentElement)
357 self.className = 'XML tree'
358 # Reset required parameters after processing XML, because encoding is
359 # a little special
360 self.required = ['encoding']
361 self.params['encoding'] = xxxEncoding()
362
363 ################################################################################
364 # Top-level windwows
365
366 class xxxPanel(xxxContainer):
367 allParams = ['pos', 'size', 'style']
368 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
369 'tooltip']
370
371 class xxxDialog(xxxContainer):
372 allParams = ['title', 'centered', 'pos', 'size', 'style']
373 paramDict = {'centered': ParamBool}
374 required = ['title']
375 default = {'title': ''}
376 winStyles = ['wxDEFAULT_DIALOG_STYLE',
377 'wxCAPTION', 'wxMINIMIZE_BOX', 'wxMAXIMIZE_BOX', 'wxCLOSE_BOX',
378 'wxSTAY_ON_TOP',
379 'wxTHICK_FRAME',
380 'wxNO_3D', 'wxDIALOG_NO_PARENT']
381 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
382 'tooltip']
383
384 class xxxFrame(xxxContainer):
385 allParams = ['title', 'centered', 'pos', 'size', 'style']
386 paramDict = {'centered': ParamBool}
387 required = ['title']
388 default = {'title': ''}
389 winStyles = ['wxDEFAULT_FRAME_STYLE',
390 'wxCAPTION', 'wxMINIMIZE_BOX', 'wxMAXIMIZE_BOX', 'wxCLOSE_BOX',
391 'wxSTAY_ON_TOP',
392 'wxSYSTEM_MENU', 'wxRESIZE_BORDER',
393 'wxFRAME_TOOL_WINDOW', 'wxFRAME_NO_TASKBAR',
394 'wxFRAME_FLOAT_ON_PARENT', 'wxFRAME_SHAPED'
395 ]
396 styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
397 'tooltip']
398
399 class xxxTool(xxxObject):
400 allParams = ['bitmap', 'bitmap2', 'toggle', 'tooltip', 'longhelp', 'label']
401 required = ['bitmap']
402 paramDict = {'bitmap2': ParamBitmap, 'toggle': ParamBool}
403 hasStyle = False
404
405 class xxxToolBar(xxxContainer):
406 allParams = ['bitmapsize', 'margins', 'packing', 'separation',
407 'pos', 'size', 'style']
408 hasStyle = False
409 paramDict = {'bitmapsize': ParamPosSize, 'margins': ParamPosSize,
410 'packing': ParamInt, 'separation': ParamInt,
411 'style': ParamNonGenericStyle}
412 winStyles = ['wxTB_FLAT', 'wxTB_DOCKABLE', 'wxTB_VERTICAL', 'wxTB_HORIZONTAL', 'wxTB_TEXT']
413
414 class xxxWizard(xxxContainer):
415 allParams = ['title', 'bitmap', 'pos']
416 required = ['title']
417 default = {'title': ''}
418 winStyles = []
419 exStyles = ['wxWIZARD_EX_HELPBUTTON']
420
421 class xxxWizardPage(xxxContainer):
422 allParams = ['bitmap']
423 winStyles = []
424 exStyles = []
425
426 class xxxWizardPageSimple(xxxContainer):
427 allParams = ['bitmap']
428 winStyles = []
429 exStyles = []
430
431 ################################################################################
432 # Bitmap, Icon
433
434 class xxxBitmap(xxxObject):
435 allParams = ['bitmap']
436 required = ['bitmap']
437
438 # Just like bitmap
439 class xxxIcon(xxxObject):
440 allParams = []
441
442 ################################################################################
443 # Controls
444
445 class xxxStaticText(xxxObject):
446 allParams = ['label', 'pos', 'size', 'style']
447 required = ['label']
448 default = {'label': ''}
449 winStyles = ['wxALIGN_LEFT', 'wxALIGN_RIGHT', 'wxALIGN_CENTRE', 'wxST_NO_AUTORESIZE']
450
451 class xxxStaticLine(xxxObject):
452 allParams = ['pos', 'size', 'style']
453 winStyles = ['wxLI_HORIZONTAL', 'wxLI_VERTICAL']
454
455 class xxxStaticBitmap(xxxObject):
456 allParams = ['bitmap', 'pos', 'size', 'style']
457 required = ['bitmap']
458
459 class xxxTextCtrl(xxxObject):
460 allParams = ['value', 'pos', 'size', 'style']
461 winStyles = ['wxTE_PROCESS_ENTER', 'wxTE_PROCESS_TAB', 'wxTE_MULTILINE',
462 'wxTE_PASSWORD', 'wxTE_READONLY', 'wxHSCROLL']
463 paramDict = {'value': ParamMultilineText}
464
465 class xxxChoice(xxxObject):
466 allParams = ['content', 'selection', 'pos', 'size', 'style']
467 required = ['content']
468 default = {'content': '[]'}
469 winStyles = ['wxCB_SORT']
470
471 class xxxSlider(xxxObject):
472 allParams = ['value', 'min', 'max', 'pos', 'size', 'style',
473 'tickfreq', 'pagesize', 'linesize', 'thumb', 'tick',
474 'selmin', 'selmax']
475 paramDict = {'value': ParamInt, 'tickfreq': ParamInt, 'pagesize': ParamInt,
476 'linesize': ParamInt, 'thumb': ParamInt, 'thumb': ParamInt,
477 'tick': ParamInt, 'selmin': ParamInt, 'selmax': ParamInt}
478 required = ['value', 'min', 'max']
479 winStyles = ['wxSL_HORIZONTAL', 'wxSL_VERTICAL', 'wxSL_AUTOTICKS', 'wxSL_LABELS',
480 'wxSL_LEFT', 'wxSL_RIGHT', 'wxSL_TOP', 'wxSL_BOTTOM',
481 'wxSL_BOTH', 'wxSL_SELRANGE', 'wxSL_INVERSE']
482
483 class xxxGauge(xxxObject):
484 allParams = ['range', 'pos', 'size', 'style', 'value', 'shadow', 'bezel']
485 paramDict = {'range': ParamInt, 'value': ParamInt,
486 'shadow': ParamInt, 'bezel': ParamInt}
487 winStyles = ['wxGA_HORIZONTAL', 'wxGA_VERTICAL', 'wxGA_PROGRESSBAR', 'wxGA_SMOOTH']
488
489 class xxxScrollBar(xxxObject):
490 allParams = ['pos', 'size', 'style', 'value', 'thumbsize', 'range', 'pagesize']
491 paramDict = {'value': ParamInt, 'range': ParamInt, 'thumbsize': ParamInt,
492 'pagesize': ParamInt}
493 winStyles = ['wxSB_HORIZONTAL', 'wxSB_VERTICAL']
494
495 class xxxListCtrl(xxxObject):
496 allParams = ['pos', 'size', 'style']
497 winStyles = ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON',
498 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE',
499 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER',
500 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING']
501
502 class xxxTreeCtrl(xxxObject):
503 allParams = ['pos', 'size', 'style']
504 winStyles = ['wxTR_HAS_BUTTONS', 'wxTR_NO_LINES', 'wxTR_LINES_AT_ROOT',
505 'wxTR_EDIT_LABELS', 'wxTR_MULTIPLE']
506
507 class xxxHtmlWindow(xxxObject):
508 allParams = ['pos', 'size', 'style', 'borders', 'url', 'htmlcode']
509 paramDict = {'borders': ParamInt, 'htmlcode':ParamMultilineText}
510 winStyles = ['wxHW_SCROLLBAR_NEVER', 'wxHW_SCROLLBAR_AUTO']
511
512 class xxxCalendarCtrl(xxxObject):
513 allParams = ['pos', 'size', 'style']
514
515 class xxxNotebook(xxxContainer):
516 allParams = ['usenotebooksizer', 'pos', 'size', 'style']
517 paramDict = {'usenotebooksizer': ParamBool}
518 winStyles = ['wxNB_FIXEDWIDTH', 'wxNB_LEFT', 'wxNB_RIGHT', 'wxNB_BOTTOM']
519
520 class xxxSplitterWindow(xxxContainer):
521 allParams = ['orientation', 'sashpos', 'minsize', 'pos', 'size', 'style']
522 paramDict = {'orientation': ParamOrientation, 'sashpos': ParamUnit, 'minsize': ParamUnit }
523 winStyles = ['wxSP_3D', 'wxSP_3DSASH', 'wxSP_3DBORDER', 'wxSP_BORDER',
524 'wxSP_NOBORDER', 'wxSP_PERMIT_UNSPLIT', 'wxSP_LIVE_UPDATE',
525 'wxSP_NO_XP_THEME' ]
526
527 class xxxGenericDirCtrl(xxxObject):
528 allParams = ['defaultfolder', 'filter', 'defaultfilter', 'pos', 'size', 'style']
529 paramDict = {'defaultfilter': ParamInt}
530 winStyles = ['wxDIRCTRL_DIR_ONLY', 'wxDIRCTRL_3D_INTERNAL', 'wxDIRCTRL_SELECT_FIRST',
531 'wxDIRCTRL_SHOW_FILTERS', 'wxDIRCTRL_EDIT_LABELS']
532
533 class xxxScrolledWindow(xxxContainer):
534 allParams = ['pos', 'size', 'style']
535 winStyles = ['wxHSCROLL', 'wxVSCROLL']
536
537 ################################################################################
538 # Buttons
539
540 class xxxButton(xxxObject):
541 allParams = ['label', 'default', 'pos', 'size', 'style']
542 paramDict = {'default': ParamBool}
543 required = ['label']
544 winStyles = ['wxBU_LEFT', 'wxBU_TOP', 'wxBU_RIGHT', 'wxBU_BOTTOM']
545
546 class xxxBitmapButton(xxxObject):
547 allParams = ['bitmap', 'selected', 'focus', 'disabled', 'default',
548 'pos', 'size', 'style']
549 required = ['bitmap']
550 winStyles = ['wxBU_AUTODRAW', 'wxBU_LEFT', 'wxBU_TOP',
551 'wxBU_RIGHT', 'wxBU_BOTTOM']
552
553 class xxxRadioButton(xxxObject):
554 allParams = ['label', 'value', 'pos', 'size', 'style']
555 paramDict = {'value': ParamBool}
556 required = ['label']
557 winStyles = ['wxRB_GROUP']
558
559 class xxxSpinButton(xxxObject):
560 allParams = ['value', 'min', 'max', 'pos', 'size', 'style']
561 paramDict = {'value': ParamInt}
562 winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
563
564 class xxxSpinCtrl(xxxObject):
565 allParams = ['value', 'min', 'max', 'pos', 'size', 'style']
566 paramDict = {'value': ParamInt}
567 winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
568
569 class xxxToggleButton(xxxObject):
570 allParams = ['label', 'checked', 'pos', 'size', 'style']
571 paramDict = {'checked': ParamBool}
572 required = ['label']
573
574 ################################################################################
575 # Boxes
576
577 class xxxStaticBox(xxxObject):
578 allParams = ['label', 'pos', 'size', 'style']
579 required = ['label']
580
581 class xxxRadioBox(xxxObject):
582 allParams = ['label', 'content', 'selection', 'dimension', 'pos', 'size', 'style']
583 paramDict = {'dimension': ParamInt}
584 required = ['label', 'content']
585 default = {'content': '[]'}
586 winStyles = ['wxRA_SPECIFY_ROWS', 'wxRA_SPECIFY_COLS']
587
588 class xxxCheckBox(xxxObject):
589 allParams = ['label', 'checked', 'pos', 'size', 'style']
590 paramDict = {'checked': ParamBool}
591 winStyles = ['wxCHK_2STATE', 'wxCHK_3STATE', 'wxCHK_ALLOW_3RD_STATE_FOR_USER',
592 'wxALIGN_RIGHT']
593 required = ['label']
594
595 class xxxComboBox(xxxObject):
596 allParams = ['content', 'selection', 'value', 'pos', 'size', 'style']
597 required = ['content']
598 default = {'content': '[]'}
599 winStyles = ['wxCB_SIMPLE', 'wxCB_SORT', 'wxCB_READONLY', 'wxCB_DROPDOWN']
600
601 class xxxListBox(xxxObject):
602 allParams = ['content', 'selection', 'pos', 'size', 'style']
603 required = ['content']
604 default = {'content': '[]'}
605 winStyles = ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL',
606 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT']
607
608 class xxxCheckList(xxxObject):
609 allParams = ['content', 'pos', 'size', 'style']
610 required = ['content']
611 default = {'content': '[]'}
612 winStyles = ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON',
613 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE',
614 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER',
615 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING']
616 paramDict = {'content': ParamContentCheckList}
617
618 ################################################################################
619 # Sizers
620
621 class xxxSizer(xxxContainer):
622 hasName = hasStyle = False
623 paramDict = {'orient': ParamOrient}
624 isSizer = True
625
626 class xxxBoxSizer(xxxSizer):
627 allParams = ['orient']
628 required = ['orient']
629 default = {'orient': 'wxVERTICAL'}
630 # Tree icon depends on orientation
631 def treeImage(self):
632 if self.params['orient'].value() == 'wxHORIZONTAL': return self.imageH
633 else: return self.imageV
634
635 class xxxStaticBoxSizer(xxxBoxSizer):
636 allParams = ['label', 'orient']
637 required = ['label', 'orient']
638
639 class xxxGridSizer(xxxSizer):
640 allParams = ['cols', 'rows', 'vgap', 'hgap']
641 required = ['cols']
642 default = {'cols': '2', 'rows': '2'}
643
644 class xxxStdDialogButtonSizer(xxxSizer):
645 allParams = []
646
647 # For repeated parameters
648 class xxxParamMulti:
649 def __init__(self, node):
650 self.node = node
651 self.l, self.data = [], []
652 def append(self, param):
653 self.l.append(param)
654 self.data.append(param.value())
655 def value(self):
656 return self.data
657 def remove(self):
658 for param in self.l:
659 param.remove()
660 self.l, self.data = [], []
661
662 class xxxFlexGridSizer(xxxGridSizer):
663 specials = ['growablecols', 'growablerows']
664 allParams = ['cols', 'rows', 'vgap', 'hgap'] + specials
665 paramDict = {'growablecols':ParamIntList, 'growablerows':ParamIntList}
666 # Special processing for growable* parameters
667 # (they are represented by several nodes)
668 def special(self, tag, node):
669 if not self.params.has_key(tag):
670 # Create new multi-group
671 self.params[tag] = xxxParamMulti(node)
672 self.params[tag].append(xxxParamInt(node))
673 def setSpecial(self, param, value):
674 # Straightforward implementation: remove, add again
675 self.params[param].remove()
676 del self.params[param]
677 for i in value:
678 node = g.tree.dom.createElement(param)
679 text = g.tree.dom.createTextNode(str(i))
680 node.appendChild(text)
681 self.element.appendChild(node)
682 self.special(param, node)
683
684 class xxxGridBagSizer(xxxSizer):
685 specials = ['growablecols', 'growablerows']
686 allParams = ['vgap', 'hgap'] + specials
687 paramDict = {'growablecols':ParamIntList, 'growablerows':ParamIntList}
688 # Special processing for growable* parameters
689 # (they are represented by several nodes)
690 def special(self, tag, node):
691 if not self.params.has_key(tag):
692 # Create new multi-group
693 self.params[tag] = xxxParamMulti(node)
694 self.params[tag].append(xxxParamInt(node))
695 def setSpecial(self, param, value):
696 # Straightforward implementation: remove, add again
697 self.params[param].remove()
698 del self.params[param]
699 for i in value:
700 node = g.tree.dom.createElement(param)
701 text = g.tree.dom.createTextNode(str(i))
702 node.appendChild(text)
703 self.element.appendChild(node)
704 self.special(param, node)
705
706 # Container with only one child.
707 # Not shown in tree.
708 class xxxChildContainer(xxxObject):
709 hasName = hasStyle = False
710 hasChild = True
711 def __init__(self, parent, element):
712 xxxObject.__init__(self, parent, element)
713 # Must have one child with 'object' tag, but we don't check it
714 nodes = element.childNodes[:] # create copy
715 for node in nodes:
716 if node.nodeType == minidom.Node.ELEMENT_NODE:
717 if node.tagName == 'object':
718 # Create new xxx object for child node
719 self.child = MakeXXXFromDOM(self, node)
720 self.child.parent = parent
721 # Copy hasChildren and isSizer attributes
722 self.hasChildren = self.child.hasChildren
723 self.isSizer = self.child.isSizer
724 return # success
725 else:
726 element.removeChild(node)
727 node.unlink()
728 assert 0, 'no child found'
729
730 class xxxSizerItem(xxxChildContainer):
731 allParams = ['option', 'flag', 'border', 'minsize', 'ratio']
732 paramDict = {'option': ParamInt, 'minsize': ParamPosSize, 'ratio': ParamPosSize}
733 #default = {'cellspan': '1,1'}
734 def __init__(self, parent, element):
735 # For GridBag sizer items, extra parameters added
736 if isinstance(parent, xxxGridBagSizer):
737 self.allParams = self.allParams + ['cellpos', 'cellspan']
738 xxxChildContainer.__init__(self, parent, element)
739 # Remove pos parameter - not needed for sizeritems
740 if 'pos' in self.child.allParams:
741 self.child.allParams = self.child.allParams[:]
742 self.child.allParams.remove('pos')
743
744 class xxxNotebookPage(xxxChildContainer):
745 allParams = ['label', 'selected']
746 paramDict = {'selected': ParamBool}
747 required = ['label']
748 def __init__(self, parent, element):
749 xxxChildContainer.__init__(self, parent, element)
750 # pos and size dont matter for notebookpages
751 if 'pos' in self.child.allParams:
752 self.child.allParams = self.child.allParams[:]
753 self.child.allParams.remove('pos')
754 if 'size' in self.child.allParams:
755 self.child.allParams = self.child.allParams[:]
756 self.child.allParams.remove('size')
757
758 class xxxSpacer(xxxObject):
759 hasName = hasStyle = False
760 allParams = ['size', 'option', 'flag', 'border']
761 paramDict = {'option': ParamInt}
762 default = {'size': '0,0'}
763
764 class xxxMenuBar(xxxContainer):
765 allParams = ['style']
766 paramDict = {'style': ParamNonGenericStyle} # no generic styles
767 winStyles = ['wxMB_DOCKABLE']
768
769 class xxxMenu(xxxContainer):
770 allParams = ['label', 'help', 'style']
771 default = {'label': ''}
772 paramDict = {'style': ParamNonGenericStyle} # no generic styles
773 winStyles = ['wxMENU_TEAROFF']
774
775 class xxxMenuItem(xxxObject):
776 allParams = ['label', 'bitmap', 'accel', 'help',
777 'checkable', 'radio', 'enabled', 'checked']
778 default = {'label': ''}
779 hasStyle = False
780
781 class xxxSeparator(xxxObject):
782 hasName = hasStyle = False
783
784 ################################################################################
785 # Unknown control
786
787 class xxxUnknown(xxxObject):
788 allParams = ['pos', 'size', 'style']
789 paramDict = {'style': ParamNonGenericStyle} # no generic styles
790
791 ################################################################################
792
793 xxxDict = {
794 'wxPanel': xxxPanel,
795 'wxDialog': xxxDialog,
796 'wxFrame': xxxFrame,
797 'tool': xxxTool,
798 'wxToolBar': xxxToolBar,
799 'wxWizard': xxxWizard,
800 'wxWizardPage': xxxWizardPage,
801 'wxWizardPageSimple': xxxWizardPageSimple,
802
803 'wxBitmap': xxxBitmap,
804 'wxIcon': xxxIcon,
805
806 'wxButton': xxxButton,
807 'wxBitmapButton': xxxBitmapButton,
808 'wxRadioButton': xxxRadioButton,
809 'wxSpinButton': xxxSpinButton,
810 'wxToggleButton' : xxxToggleButton,
811
812 'wxStaticBox': xxxStaticBox,
813 'wxStaticBitmap': xxxStaticBitmap,
814 'wxRadioBox': xxxRadioBox,
815 'wxComboBox': xxxComboBox,
816 'wxCheckBox': xxxCheckBox,
817 'wxListBox': xxxListBox,
818
819 'wxStaticText': xxxStaticText,
820 'wxStaticLine': xxxStaticLine,
821 'wxTextCtrl': xxxTextCtrl,
822 'wxChoice': xxxChoice,
823 'wxSlider': xxxSlider,
824 'wxGauge': xxxGauge,
825 'wxScrollBar': xxxScrollBar,
826 'wxTreeCtrl': xxxTreeCtrl,
827 'wxListCtrl': xxxListCtrl,
828 'wxCheckListBox': xxxCheckList,
829 'wxNotebook': xxxNotebook,
830 'wxSplitterWindow': xxxSplitterWindow,
831 'notebookpage': xxxNotebookPage,
832 'wxHtmlWindow': xxxHtmlWindow,
833 'wxCalendarCtrl': xxxCalendarCtrl,
834 'wxGenericDirCtrl': xxxGenericDirCtrl,
835 'wxSpinCtrl': xxxSpinCtrl,
836 'wxScrolledWindow': xxxScrolledWindow,
837
838 'wxBoxSizer': xxxBoxSizer,
839 'wxStaticBoxSizer': xxxStaticBoxSizer,
840 'wxGridSizer': xxxGridSizer,
841 'wxFlexGridSizer': xxxFlexGridSizer,
842 'wxGridBagSizer': xxxGridBagSizer,
843 'wxStdDialogButtonSizer': xxxStdDialogButtonSizer,
844 'sizeritem': xxxSizerItem,
845 'spacer': xxxSpacer,
846
847 'wxMenuBar': xxxMenuBar,
848 'wxMenu': xxxMenu,
849 'wxMenuItem': xxxMenuItem,
850 'separator': xxxSeparator,
851
852 'unknown': xxxUnknown,
853 }
854
855 # Create IDs for all parameters of all classes
856 paramIDs = {'fg': wxNewId(), 'bg': wxNewId(), 'exstyle': wxNewId(), 'font': wxNewId(),
857 'enabled': wxNewId(), 'focused': wxNewId(), 'hidden': wxNewId(),
858 'tooltip': wxNewId(), 'encoding': wxNewId(),
859 'cellpos': wxNewId(), 'cellspan': wxNewId()
860 }
861 for cl in xxxDict.values():
862 if cl.allParams:
863 for param in cl.allParams + cl.paramDict.keys():
864 if not paramIDs.has_key(param):
865 paramIDs[param] = wxNewId()
866
867 ################################################################################
868 # Helper functions
869
870 # Test for object elements
871 def IsObject(node):
872 return node.nodeType == minidom.Node.ELEMENT_NODE and node.tagName == 'object'
873
874 # Make XXX object from some DOM object, selecting correct class
875 def MakeXXXFromDOM(parent, element):
876 try:
877 klass = xxxDict[element.getAttribute('class')]
878 except KeyError:
879 # If we encounter a weird class, use unknown template
880 print 'WARNING: unsupported class:', element.getAttribute('class')
881 klass = xxxUnknown
882 return klass(parent, element)
883
884 # Make empty DOM element
885 def MakeEmptyDOM(className):
886 elem = g.tree.dom.createElement('object')
887 elem.setAttribute('class', className)
888 # Set required and default parameters
889 xxxClass = xxxDict[className]
890 defaultNotRequired = filter(lambda x, l=xxxClass.required: x not in l,
891 xxxClass.default.keys())
892 for param in xxxClass.required + defaultNotRequired:
893 textElem = g.tree.dom.createElement(param)
894 try:
895 textNode = g.tree.dom.createTextNode(xxxClass.default[param])
896 except KeyError:
897 textNode = g.tree.dom.createTextNode('')
898 textElem.appendChild(textNode)
899 elem.appendChild(textElem)
900 return elem
901
902 # Make empty XXX object
903 def MakeEmptyXXX(parent, className):
904 # Make corresponding DOM object first
905 elem = MakeEmptyDOM(className)
906 # If parent is a sizer, we should create sizeritem object, except for spacers
907 if parent:
908 if parent.isSizer and className != 'spacer':
909 sizerItemElem = MakeEmptyDOM('sizeritem')
910 sizerItemElem.appendChild(elem)
911 elem = sizerItemElem
912 elif isinstance(parent, xxxNotebook):
913 pageElem = MakeEmptyDOM('notebookpage')
914 pageElem.appendChild(elem)
915 elem = pageElem
916 # Now just make object
917 return MakeXXXFromDOM(parent, elem)
918