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