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