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>
7 from xml
.dom
import minidom
10 import traceback
, types
12 # Base class for interface parameter classes
14 def __init__(self
, node
):
17 self
.node
.parentNode
.removeChild(self
.node
)
20 # Generic (text) parameter class
21 class xxxParam(xxxNode
):
22 # Standard use: for text nodes
23 def __init__(self
, node
):
24 xxxNode
.__init
__(self
, node
)
25 if not node
.hasChildNodes():
26 # If does not have child nodes, create empty text node
27 text
= g
.tree
.dom
.createTextNode('')
28 node
.appendChild(text
)
30 text
= node
.childNodes
[0] # first child must be text node
31 assert text
.nodeType
== minidom
.Node
.TEXT_NODE
32 # Append other text nodes if present and delete them
34 for n
in node
.childNodes
[1:]:
35 if n
.nodeType
== minidom
.Node
.TEXT_NODE
:
40 if extraText
: text
.data
= text
.data
+ extraText
41 # Use convertion from unicode to current encoding
43 # Value returns string
44 if wx
.USE_UNICODE
: # no conversion is needed
46 return self
.textNode
.data
47 def update(self
, value
):
48 self
.textNode
.data
= value
52 return self
.textNode
.data
.encode(g
.currentEncoding
)
54 return self
.textNode
.data
.encode()
55 def update(self
, value
):
56 try: # handle exception if encoding is wrong
57 self
.textNode
.data
= unicode(value
, g
.currentEncoding
)
58 except UnicodeDecodeError:
59 self
.textNode
.data
= unicode(value
)
60 #wx.LogMessage("Unicode error: set encoding in file\nglobals.py to something appropriate")
63 class xxxParamInt(xxxParam
):
64 # Standard use: for text nodes
65 def __init__(self
, node
):
66 xxxParam
.__init
__(self
, node
)
67 # Value returns string
70 return int(self
.textNode
.data
)
72 return -1 # invalid value
73 def update(self
, value
):
74 self
.textNode
.data
= str(value
)
77 class xxxParamContent(xxxNode
):
78 def __init__(self
, node
):
79 xxxNode
.__init
__(self
, node
)
80 data
, l
= [], [] # data is needed to quicker value retrieval
81 nodes
= node
.childNodes
[:] # make a copy of the child list
83 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
84 assert n
.tagName
== 'item', 'bad content content'
85 if not n
.hasChildNodes():
86 # If does not have child nodes, create empty text node
87 text
= g
.tree
.dom
.createTextNode('')
88 node
.appendChild(text
)
91 text
= n
.childNodes
[0] # first child must be text node
92 assert text
.nodeType
== minidom
.Node
.TEXT_NODE
94 data
.append(text
.data
)
98 self
.l
, self
.data
= l
, data
101 def update(self
, value
):
102 # If number if items is not the same, recreate children
103 if len(value
) != len(self
.l
): # remove first if number of items has changed
104 childNodes
= self
.node
.childNodes
[:]
106 self
.node
.removeChild(n
)
109 itemElem
= g
.tree
.dom
.createElement('item')
110 itemText
= g
.tree
.dom
.createTextNode(str)
111 itemElem
.appendChild(itemText
)
112 self
.node
.appendChild(itemElem
)
116 for i
in range(len(value
)):
117 self
.l
[i
].data
= value
[i
]
120 # Content parameter for checklist
121 class xxxParamContentCheckList(xxxNode
):
122 def __init__(self
, node
):
123 xxxNode
.__init
__(self
, node
)
124 data
, l
= [], [] # data is needed to quicker value retrieval
125 nodes
= node
.childNodes
[:] # make a copy of the child list
127 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
128 assert n
.tagName
== 'item', 'bad content content'
129 checked
= n
.getAttribute('checked')
130 if not checked
: checked
= 0
131 if not n
.hasChildNodes():
132 # If does not have child nodes, create empty text node
133 text
= g
.tree
.dom
.createTextNode('')
134 node
.appendChild(text
)
137 text
= n
.childNodes
[0] # first child must be text node
138 assert text
.nodeType
== minidom
.Node
.TEXT_NODE
140 data
.append((str(text
.data
), int(checked
)))
144 self
.l
, self
.data
= l
, data
147 def update(self
, value
):
148 # If number if items is not the same, recreate children
149 if len(value
) != len(self
.l
): # remove first if number of items has changed
150 childNodes
= self
.node
.childNodes
[:]
152 self
.node
.removeChild(n
)
155 itemElem
= g
.tree
.dom
.createElement('item')
156 # Add checked only if True
157 if ch
: itemElem
.setAttribute('checked', '1')
158 itemText
= g
.tree
.dom
.createTextNode(s
)
159 itemElem
.appendChild(itemText
)
160 self
.node
.appendChild(itemElem
)
161 l
.append((itemText
, itemElem
))
164 for i
in range(len(value
)):
165 self
.l
[i
][0].data
= value
[i
][0]
166 self
.l
[i
][1].setAttribute('checked', str(value
[i
][1]))
170 class xxxParamBitmap(xxxParam
):
171 def __init__(self
, node
):
172 xxxParam
.__init
__(self
, node
)
173 self
.stock_id
= node
.getAttribute('stock_id')
175 return [self
.stock_id
, xxxParam
.value(self
)]
176 def update(self
, value
):
177 self
.stock_id
= value
[0]
179 self
.node
.setAttribute('stock_id', self
.stock_id
)
180 elif self
.node
.hasAttribute('stock_id'):
181 self
.node
.removeAttribute('stock_id')
182 xxxParam
.update(self
, value
[1])
184 ################################################################################
186 # Classes to interface DOM objects
189 hasChildren
= False # has children elements?
190 hasStyle
= True # almost everyone
191 hasName
= True # has name attribute?
192 isSizer
= hasChild
= False
194 allParams
= None # Some nodes have no parameters
195 # Style parameters (all optional)
196 styles
= ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'tooltip']
200 bitmapTags
= ['bitmap', 'bitmap2', 'icon']
201 # Required paremeters: none by default
203 # Default parameters with default values
207 # Window styles and extended styles
211 # Construct a new xxx object from DOM element
212 # parent is parent xxx object (or None if none), element is DOM element object
213 def __init__(self
, parent
, element
, refElem
=None):
216 self
.refElem
= refElem
218 # Reference are dereferenced
219 if element
.tagName
== 'object_ref':
220 # Find original object
221 self
.ref
= element
.getAttribute('ref')
223 self
.className
= self
.refElem
.getAttribute('class')
225 self
.className
= 'xxxUnknown'
230 self
.className
= element
.getAttribute('class')
231 self
.subclass
= element
.getAttribute('subclass')
232 if self
.hasName
: self
.name
= element
.getAttribute('name')
233 # Set parameters (text element children)
235 for n
in element
.childNodes
[:]:
236 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
238 if tag
in ['object', 'object_ref']:
239 continue # do nothing for object children here
240 elif tag
not in self
.allParams
and tag
not in self
.styles
:
241 print 'WARNING: unknown parameter for %s: %s' % \
242 (self
.className
, tag
)
243 elif tag
in self
.specials
:
245 elif tag
== 'content':
246 if self
.className
== 'wxCheckListBox':
247 self
.params
[tag
] = xxxParamContentCheckList(n
)
249 self
.params
[tag
] = xxxParamContent(n
)
250 elif tag
== 'font': # has children
251 self
.params
[tag
] = xxxParamFont(element
, n
)
252 elif tag
in self
.bitmapTags
:
253 # Can have attributes
254 self
.params
[tag
] = xxxParamBitmap(n
)
255 else: # simple parameter
256 self
.params
[tag
] = xxxParam(n
)
257 elif n
.nodeType
== minidom
.Node
.TEXT_NODE
and n
.data
.isspace():
258 # Remove empty text nodes
259 element
.removeChild(n
)
262 # Check that all required params are set
263 for param
in self
.required
:
264 if not self
.params
.has_key(param
):
265 # If default is specified, set it
266 if self
.default
.has_key(param
):
267 elem
= g
.tree
.dom
.createElement(param
)
268 if param
== 'content':
269 if self
.className
== 'wxCheckListBox':
270 self
.params
[param
] = xxxParamContentCheckList(elem
)
272 self
.params
[param
] = xxxParamContent(elem
)
274 self
.params
[param
] = xxxParam(elem
)
275 # Find place to put new element: first present element after param
277 paramStyles
= self
.allParams
+ self
.styles
278 for p
in paramStyles
[paramStyles
.index(param
) + 1:]:
279 # Content params don't have same type
280 if self
.params
.has_key(p
) and p
!= 'content':
284 nextTextElem
= self
.params
[p
].node
285 self
.node
.insertBefore(elem
, nextTextElem
)
287 self
.node
.appendChild(elem
)
289 wx
.LogWarning('Required parameter %s of %s missing' %
290 (param
, self
.className
))
291 # Returns real tree object
292 def treeObject(self
):
293 if self
.hasChild
: return self
.child
295 # Returns tree image index
297 if self
.hasChild
: return self
.child
.treeImage()
299 # Class name plus wx name
301 if self
.hasChild
: return self
.child
.treeName()
302 if self
.subclass
: className
= self
.subclass
303 else: className
= self
.className
304 if self
.hasName
and self
.name
: return className
+ ' "' + self
.name
+ '"'
306 # Class name or subclass
308 if self
.subclass
: name
= self
.subclass
+ '(' + self
.className
+ ')'
309 name
= self
.className
310 if self
.ref
: name
= 'ref: ' + self
.ref
+ ', ' + name
312 # Sets name of tree object
313 def setTreeName(self
, name
):
314 if self
.hasChild
: obj
= self
.child
317 obj
.node
.setAttribute('name', name
)
318 # Special processing for growablecols-like parameters
319 # represented by several nodes
320 def special(self
, tag
, node
):
321 if not self
.params
.has_key(tag
):
322 # Create new multi-group
323 self
.params
[tag
] = xxxParamMulti(node
)
324 self
.params
[tag
].append(xxxParamInt(node
))
325 def setSpecial(self
, param
, value
):
326 # Straightforward implementation: remove, add again
327 self
.params
[param
].remove()
328 del self
.params
[param
]
330 node
= g
.tree
.dom
.createElement(param
)
331 text
= g
.tree
.dom
.createTextNode(str(i
))
332 node
.appendChild(text
)
333 self
.node
.appendChild(node
)
334 self
.special(param
, node
)
336 # Imitation of FindResource/DoFindResource from xmlres.cpp
337 def DoFindResource(parent
, name
, classname
, recursive
):
338 for n
in parent
.childNodes
:
339 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
and \
340 n
.tagName
in ['object', 'object_ref'] and \
341 n
.getAttribute('name') == name
:
342 cls
= n
.getAttribute('class')
343 if not classname
or cls
== classname
: return n
344 if not cls
or n
.tagName
== 'object_ref':
345 refName
= n
.getAttribute('ref')
346 if not refName
: continue
347 refNode
= FindResource(refName
)
348 if refName
and refNode
.getAttribute('class') == classname
:
351 for n
in parent
.childNodes
:
352 if n
.nodeType
== minidom
.Node
.ELEMENT_NODE
and \
353 n
.tagName
in ['object', 'object_ref']:
354 found
= DoFindResource(n
, name
, classname
, True)
355 if found
: return found
356 def FindResource(name
, classname
='', recursive
=True):
357 found
= DoFindResource(g
.tree
.mainNode
, name
, classname
, recursive
)
358 if found
: return found
359 wx
.LogError('XRC resource "%s" not found!' % name
)
362 ################################################################################
364 # This is a little special: it is both xxxObject and xxxNode
365 class xxxParamFont(xxxObject
, xxxNode
):
366 allParams
= ['size', 'family', 'style', 'weight', 'underlined',
368 def __init__(self
, parent
, element
):
369 xxxObject
.__init
__(self
, parent
, element
)
370 xxxNode
.__init
__(self
, element
)
371 self
.parentNode
= parent
# required to behave similar to DOM node
373 for p
in self
.allParams
:
375 v
.append(str(self
.params
[p
].value()))
379 def update(self
, value
):
380 # `value' is a list of strings corresponding to all parameters
382 # Remove old elements first
383 childNodes
= elem
.childNodes
[:]
384 for node
in childNodes
: elem
.removeChild(node
)
388 for param
in self
.allParams
:
390 fontElem
= g
.tree
.dom
.createElement(param
)
391 textNode
= g
.tree
.dom
.createTextNode(value
[i
])
392 self
.params
[param
] = textNode
393 fontElem
.appendChild(textNode
)
394 elem
.appendChild(fontElem
)
401 ################################################################################
403 class xxxContainer(xxxObject
):
407 # Simulate normal parameter for encoding
410 return g
.currentEncoding
411 def update(self
, val
):
412 g
.currentEncoding
= val
414 # Special class for root node
415 class xxxMainNode(xxxContainer
):
416 allParams
= ['encoding']
417 hasStyle
= hasName
= False
418 def __init__(self
, dom
):
419 xxxContainer
.__init
__(self
, None, dom
.documentElement
)
420 self
.className
= 'XML tree'
421 # Reset required parameters after processing XML, because encoding is
423 self
.required
= ['encoding']
424 self
.params
['encoding'] = xxxEncoding()
426 ################################################################################
429 class xxxPanel(xxxContainer
):
430 allParams
= ['pos', 'size', 'style']
431 winStyles
= ['wxNO_3D', 'wxTAB_TRAVERSAL']
432 styles
= ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
435 class xxxDialog(xxxContainer
):
436 allParams
= ['title', 'centered', 'pos', 'size', 'style']
437 paramDict
= {'centered': ParamBool}
439 default
= {'title': ''}
440 winStyles
= ['wxDEFAULT_DIALOG_STYLE', 'wxCAPTION',
441 'wxSTAY_ON_TOP', 'wxSYSTEM_MENU', 'wxTHICK_FRAME',
442 'wxRESIZE_BORDER', 'wxRESIZE_BOX', 'wxCLOSE_BOX',
443 'wxMAXIMIZE_BOX', 'wxMINIMIZE_BOX',
444 'wxDIALOG_MODAL', 'wxDIALOG_MODELESS', 'wxDIALOG_NO_PARENT'
445 'wxNO_3D', 'wxTAB_TRAVERSAL']
446 exStyles
= ['wxWS_EX_VALIDATE_RECURSIVELY', 'wxDIALOG_EX_METAL']
447 styles
= ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
450 class xxxFrame(xxxContainer
):
451 allParams
= ['title', 'centered', 'pos', 'size', 'style']
452 paramDict
= {'centered': ParamBool}
454 default
= {'title': ''}
455 winStyles
= ['wxDEFAULT_FRAME_STYLE', 'wxDEFAULT_DIALOG_STYLE', 'wxCAPTION',
456 'wxSTAY_ON_TOP', 'wxSYSTEM_MENU', 'wxTHICK_FRAME',
457 'wxRESIZE_BORDER', 'wxRESIZE_BOX', 'wxCLOSE_BOX',
458 'wxMAXIMIZE_BOX', 'wxMINIMIZE_BOX',
459 'wxFRAME_NO_TASKBAR', 'wxFRAME_SHAPED', 'wxFRAME_TOOL_WINDOW',
460 'wxFRAME_FLOAT_ON_PARENT',
461 'wxNO_3D', 'wxTAB_TRAVERSAL']
462 exStyles
= ['wxWS_EX_VALIDATE_RECURSIVELY', 'wxFRAME_EX_METAL']
463 styles
= ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle',
466 class xxxTool(xxxObject
):
467 allParams
= ['bitmap', 'bitmap2', 'radio', 'toggle', 'tooltip', 'longhelp', 'label']
468 required
= ['bitmap']
469 paramDict
= {'bitmap2': ParamBitmap, 'radio': ParamBool, 'toggle': ParamBool}
472 class xxxToolBar(xxxContainer
):
473 allParams
= ['bitmapsize', 'margins', 'packing', 'separation', 'dontattachtoframe',
474 'pos', 'size', 'style']
476 paramDict
= {'bitmapsize': ParamPosSize
, 'margins': ParamPosSize
,
477 'packing': ParamUnit
, 'separation': ParamUnit
,
478 'dontattachtoframe': ParamBool
, 'style': ParamNonGenericStyle
}
479 winStyles
= ['wxTB_FLAT', 'wxTB_DOCKABLE', 'wxTB_VERTICAL', 'wxTB_HORIZONTAL',
480 'wxTB_3DBUTTONS','wxTB_TEXT', 'wxTB_NOICONS', 'wxTB_NODIVIDER',
481 'wxTB_NOALIGN', 'wxTB_HORZ_LAYOUT', 'wxTB_HORZ_TEXT']
483 class xxxStatusBar(xxxObject
):
485 allParams
= ['fields', 'widths', 'styles', 'style']
486 paramDict
= {'fields': ParamIntNN
, 'widths': ParamText
, 'styles': ParamText
,
487 'style': ParamNonGenericStyle
}
488 winStyles
= ['wxST_SIZEGRIP']
490 class xxxWizard(xxxContainer
):
491 allParams
= ['title', 'bitmap', 'pos']
493 default
= {'title': ''}
495 exStyles
= ['wxWIZARD_EX_HELPBUTTON']
496 styles
= ['fg', 'bg', 'font', 'exstyle']
498 class xxxWizardPage(xxxContainer
):
499 allParams
= ['bitmap']
503 class xxxWizardPageSimple(xxxContainer
):
504 allParams
= ['bitmap']
508 ################################################################################
511 class xxxBitmap(xxxObject
):
512 allParams
= ['bitmap']
513 required
= ['bitmap']
516 class xxxIcon(xxxObject
):
519 ################################################################################
522 class xxxStaticText(xxxObject
):
523 allParams
= ['label', 'pos', 'size', 'style']
525 default
= {'label': ''}
526 winStyles
= ['wxALIGN_LEFT', 'wxALIGN_RIGHT', 'wxALIGN_CENTRE', 'wxST_NO_AUTORESIZE']
528 class xxxStaticLine(xxxObject
):
529 allParams
= ['pos', 'size', 'style']
530 winStyles
= ['wxLI_HORIZONTAL', 'wxLI_VERTICAL']
532 class xxxStaticBitmap(xxxObject
):
533 allParams
= ['bitmap', 'pos', 'size', 'style']
534 required
= ['bitmap']
536 class xxxTextCtrl(xxxObject
):
537 allParams
= ['value', 'pos', 'size', 'style']
538 winStyles
= ['wxTE_NO_VSCROLL',
540 'wxTE_PROCESS_ENTER',
556 paramDict
= {'value': ParamMultilineText}
558 class xxxChoice(xxxObject
):
559 allParams
= ['content', 'selection', 'pos', 'size', 'style']
560 required
= ['content']
561 default
= {'content': '[]'}
562 winStyles
= ['wxCB_SORT']
564 class xxxSlider(xxxObject
):
565 allParams
= ['value', 'min', 'max', 'pos', 'size', 'style',
566 'tickfreq', 'pagesize', 'linesize', 'thumb', 'tick',
568 paramDict
= {'value': ParamInt
, 'tickfreq': ParamIntNN
, 'pagesize': ParamIntNN
,
569 'linesize': ParamIntNN
, 'thumb': ParamUnit
,
570 'tick': ParamInt
, 'selmin': ParamInt
, 'selmax': ParamInt
}
571 required
= ['value', 'min', 'max']
572 winStyles
= ['wxSL_HORIZONTAL', 'wxSL_VERTICAL', 'wxSL_AUTOTICKS', 'wxSL_LABELS',
573 'wxSL_LEFT', 'wxSL_RIGHT', 'wxSL_TOP', 'wxSL_BOTTOM',
574 'wxSL_BOTH', 'wxSL_SELRANGE', 'wxSL_INVERSE']
576 class xxxGauge(xxxObject
):
577 allParams
= ['range', 'pos', 'size', 'style', 'value', 'shadow', 'bezel']
578 paramDict
= {'range': ParamIntNN
, 'value': ParamIntNN
,
579 'shadow': ParamIntNN
, 'bezel': ParamIntNN
}
580 winStyles
= ['wxGA_HORIZONTAL', 'wxGA_VERTICAL', 'wxGA_PROGRESSBAR', 'wxGA_SMOOTH']
582 class xxxScrollBar(xxxObject
):
583 allParams
= ['pos', 'size', 'style', 'value', 'thumbsize', 'range', 'pagesize']
584 paramDict
= {'value': ParamIntNN
, 'range': ParamIntNN
, 'thumbsize': ParamIntNN
,
585 'pagesize': ParamIntNN
}
586 winStyles
= ['wxSB_HORIZONTAL', 'wxSB_VERTICAL']
588 class xxxListCtrl(xxxObject
):
589 allParams
= ['pos', 'size', 'style']
590 winStyles
= ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON',
591 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE',
592 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER',
593 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING',
594 'wxLC_VIRTUAL', 'wxLC_HRULES', 'wxLC_VRULES', 'wxLC_NO_SORT_HEADER']
596 class xxxTreeCtrl(xxxObject
):
597 allParams
= ['pos', 'size', 'style']
598 winStyles
= ['wxTR_EDIT_LABELS',
601 'wxTR_TWIST_BUTTONS',
603 'wxTR_FULL_ROW_HIGHLIGHT',
604 'wxTR_LINES_AT_ROOT',
607 'wxTR_HAS_VARIABLE_ROW_HEIGHT',
611 'wxTR_DEFAULT_STYLE']
613 class xxxHtmlWindow(xxxObject
):
614 allParams
= ['pos', 'size', 'style', 'borders', 'url', 'htmlcode']
615 paramDict
= {'htmlcode':ParamMultilineText}
616 winStyles
= ['wxHW_SCROLLBAR_NEVER', 'wxHW_SCROLLBAR_AUTO', 'wxHW_NO_SELECTION']
618 class xxxCalendarCtrl(xxxObject
):
619 allParams
= ['pos', 'size', 'style']
620 winStyles
= ['wxCAL_SUNDAY_FIRST', 'wxCAL_MONDAY_FIRST', 'wxCAL_SHOW_HOLIDAYS',
621 'wxCAL_NO_YEAR_CHANGE', 'wxCAL_NO_MONTH_CHANGE',
622 'wxCAL_SEQUENTIAL_MONTH_SELECTION', 'wxCAL_SHOW_SURROUNDING_WEEKS']
624 class xxxNotebook(xxxContainer
):
625 allParams
= ['pos', 'size', 'style']
626 winStyles
= ['wxNB_TOP', 'wxNB_LEFT', 'wxNB_RIGHT', 'wxNB_BOTTOM',
627 'wxNB_FIXEDWIDTH', 'wxNB_MULTILINE', 'wxNB_NOPAGETHEME', 'wxNB_FLAT']
629 class xxxChoicebook(xxxContainer
):
630 allParams
= ['pos', 'size', 'style']
631 winStyles
= ['wxCHB_DEFAULT', 'wxCHB_LEFT', 'wxCHB_RIGHT', 'wxCHB_TOP', 'wxCHB_BOTTOM']
633 class xxxListbook(xxxContainer
):
634 allParams
= ['pos', 'size', 'style']
635 winStyles
= ['wxLB_DEFAULT', 'wxLB_LEFT', 'wxLB_RIGHT', 'wxLB_TOP', 'wxLB_BOTTOM']
637 class xxxSplitterWindow(xxxContainer
):
638 allParams
= ['orientation', 'sashpos', 'minsize', 'pos', 'size', 'style']
639 paramDict
= {'orientation': ParamOrientation, 'sashpos': ParamUnit, 'minsize': ParamUnit }
640 winStyles
= ['wxSP_3D', 'wxSP_3DSASH', 'wxSP_3DBORDER',
641 'wxSP_FULLSASH', 'wxSP_NOBORDER', 'wxSP_PERMIT_UNSPLIT', 'wxSP_LIVE_UPDATE',
644 class xxxGenericDirCtrl(xxxObject
):
645 allParams
= ['defaultfolder', 'filter', 'defaultfilter', 'pos', 'size', 'style']
646 paramDict
= {'defaultfilter': ParamIntNN}
647 winStyles
= ['wxDIRCTRL_DIR_ONLY', 'wxDIRCTRL_3D_INTERNAL', 'wxDIRCTRL_SELECT_FIRST',
648 'wxDIRCTRL_SHOW_FILTERS']
650 class xxxScrolledWindow(xxxContainer
):
651 allParams
= ['pos', 'size', 'style']
652 winStyles
= ['wxHSCROLL', 'wxVSCROLL', 'wxNO_3D', 'wxTAB_TRAVERSAL']
654 class xxxDateCtrl(xxxObject
):
655 allParams
= ['pos', 'size', 'style', 'borders']
656 winStyles
= ['wxDP_DEFAULT', 'wxDP_SPIN', 'wxDP_DROPDOWN',
657 'wxDP_ALLOWNONE', 'wxDP_SHOWCENTURY']
659 class xxxGrid(xxxObject
):
660 allParams
= ['pos', 'size', 'style']
662 class xxxFilePickerCtrl(xxxObject
):
663 allParams
= ['value', 'message', 'wildcard', 'pos', 'size', 'style']
664 winStyles
= ['wxFLP_OPEN', 'wxFLP_SAVE', 'wxFLP_OVERWRITE_PROMPT',
665 'wxFLP_FILE_MUST_EXIST', 'wxFLP_CHANGE_DIR',
666 'wxFLP_DEFAULT_STYLE']
669 ################################################################################
672 class xxxButton(xxxObject
):
673 allParams
= ['label', 'default', 'pos', 'size', 'style']
674 paramDict
= {'default': ParamBool}
676 winStyles
= ['wxBU_LEFT', 'wxBU_TOP', 'wxBU_RIGHT', 'wxBU_BOTTOM', 'wxBU_EXACTFIT',
679 class xxxBitmapButton(xxxObject
):
680 allParams
= ['bitmap', 'selected', 'focus', 'disabled', 'default',
681 'pos', 'size', 'style']
682 paramDict
= {'selected': ParamBitmap
, 'focus': ParamBitmap
, 'disabled': ParamBitmap
,
683 'default': ParamBool
}
684 required
= ['bitmap']
685 winStyles
= ['wxBU_AUTODRAW', 'wxBU_LEFT', 'wxBU_RIGHT',
686 'wxBU_TOP', 'wxBU_BOTTOM']
688 class xxxRadioButton(xxxObject
):
689 allParams
= ['label', 'value', 'pos', 'size', 'style']
690 paramDict
= {'value': ParamBool}
692 winStyles
= ['wxRB_GROUP', 'wxRB_SINGLE']
694 class xxxSpinButton(xxxObject
):
695 allParams
= ['value', 'min', 'max', 'pos', 'size', 'style']
696 paramDict
= {'value': ParamInt}
697 winStyles
= ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
699 class xxxSpinCtrl(xxxObject
):
700 allParams
= ['value', 'min', 'max', 'pos', 'size', 'style']
701 paramDict
= {'value': ParamInt}
702 winStyles
= ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP']
704 class xxxToggleButton(xxxObject
):
705 allParams
= ['label', 'checked', 'pos', 'size', 'style']
706 paramDict
= {'checked': ParamBool}
709 ################################################################################
712 class xxxStaticBox(xxxObject
):
713 allParams
= ['label', 'pos', 'size', 'style']
716 class xxxRadioBox(xxxObject
):
717 allParams
= ['label', 'content', 'selection', 'dimension', 'pos', 'size', 'style']
718 paramDict
= {'dimension': ParamIntNN}
719 required
= ['label', 'content']
720 default
= {'content': '[]'}
721 winStyles
= ['wxRA_SPECIFY_ROWS', 'wxRA_SPECIFY_COLS', 'wxRA_HORIZONTAL',
724 class xxxCheckBox(xxxObject
):
725 allParams
= ['label', 'checked', 'pos', 'size', 'style']
726 paramDict
= {'checked': ParamBool}
727 winStyles
= ['wxCHK_2STATE', 'wxCHK_3STATE', 'wxCHK_ALLOW_3RD_STATE_FOR_USER',
731 class xxxComboBox(xxxObject
):
732 allParams
= ['content', 'selection', 'value', 'pos', 'size', 'style']
733 required
= ['content']
734 default
= {'content': '[]'}
735 winStyles
= ['wxCB_SIMPLE', 'wxCB_SORT', 'wxCB_READONLY', 'wxCB_DROPDOWN']
737 class xxxListBox(xxxObject
):
738 allParams
= ['content', 'selection', 'pos', 'size', 'style']
739 required
= ['content']
740 default
= {'content': '[]'}
741 winStyles
= ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL',
742 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT']
744 class xxxCheckList(xxxObject
):
745 allParams
= ['content', 'pos', 'size', 'style']
746 required
= ['content']
747 default
= {'content': '[]'}
748 winStyles
= ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL',
749 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT']
750 paramDict
= {'content': ParamContentCheckList}
752 ################################################################################
755 class xxxSizer(xxxContainer
):
756 hasName
= hasStyle
= False
757 paramDict
= {'orient': ParamOrient}
759 itemTag
= 'sizeritem' # different for some sizers
761 class xxxBoxSizer(xxxSizer
):
762 allParams
= ['orient']
763 required
= ['orient']
764 default
= {'orient': 'wxVERTICAL'}
765 # Tree icon depends on orientation
767 if self
.params
['orient'].value() == 'wxHORIZONTAL': return self
.imageH
768 else: return self
.imageV
770 class xxxStaticBoxSizer(xxxBoxSizer
):
771 allParams
= ['label', 'orient']
772 required
= ['label', 'orient']
774 class xxxGridSizer(xxxSizer
):
775 allParams
= ['cols', 'rows', 'vgap', 'hgap']
777 default
= {'cols': '2', 'rows': '2'}
779 class xxxStdDialogButtonSizer(xxxSizer
):
783 # For repeated parameters
785 def __init__(self
, node
):
787 self
.l
, self
.data
= [], []
788 def append(self
, param
):
790 self
.data
.append(param
.value())
796 self
.l
, self
.data
= [], []
798 class xxxFlexGridSizer(xxxGridSizer
):
799 specials
= ['growablecols', 'growablerows']
800 allParams
= ['cols', 'rows', 'vgap', 'hgap'] + specials
801 paramDict
= {'growablecols': ParamIntList, 'growablerows': ParamIntList}
803 class xxxGridBagSizer(xxxSizer
):
804 specials
= ['growablecols', 'growablerows']
805 allParams
= ['vgap', 'hgap'] + specials
806 paramDict
= {'growablecols': ParamIntList, 'growablerows': ParamIntList}
808 # Container with only one child.
810 class xxxChildContainer(xxxObject
):
811 hasName
= hasStyle
= False
813 def __init__(self
, parent
, element
, refElem
=None):
814 xxxObject
.__init
__(self
, parent
, element
, refElem
)
815 # Must have one child with 'object' tag, but we don't check it
816 nodes
= element
.childNodes
[:] # create copy
818 if node
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
819 if node
.tagName
in ['object', 'object_ref']:
820 # Create new xxx object for child node
821 self
.child
= MakeXXXFromDOM(self
, node
)
822 self
.child
.parent
= parent
823 # Copy hasChildren and isSizer attributes
824 self
.hasChildren
= self
.child
.hasChildren
825 self
.isSizer
= self
.child
.isSizer
828 element
.removeChild(node
)
830 assert 0, 'no child found'
831 def resetChild(self
, xxx
):
832 '''Reset child info (for replacing with another class).'''
834 self
.hasChildren
= xxx
.hasChildren
835 self
.isSizer
= xxx
.isSizer
837 class xxxSizerItem(xxxChildContainer
):
838 allParams
= ['option', 'flag', 'border', 'minsize', 'ratio']
839 paramDict
= {'option': ParamInt, 'minsize': ParamPosSize, 'ratio': ParamPosSize}
840 #default = {'cellspan': '1,1'}
841 def __init__(self
, parent
, element
, refElem
=None):
842 # For GridBag sizer items, extra parameters added
843 if isinstance(parent
, xxxGridBagSizer
):
844 self
.allParams
= self
.allParams
+ ['cellpos', 'cellspan']
845 xxxChildContainer
.__init
__(self
, parent
, element
, refElem
)
846 # Remove pos parameter - not needed for sizeritems
847 if 'pos' in self
.child
.allParams
:
848 self
.child
.allParams
= self
.child
.allParams
[:]
849 self
.child
.allParams
.remove('pos')
850 def resetChild(self
, xxx
):
851 xxxChildContainer
.resetChild(self
, xxx
)
852 # Remove pos parameter - not needed for sizeritems
853 if 'pos' in self
.child
.allParams
:
854 self
.child
.allParams
= self
.child
.allParams
[:]
855 self
.child
.allParams
.remove('pos')
857 class xxxSizerItemButton(xxxSizerItem
):
860 def __init__(self
, parent
, element
, refElem
=None):
861 xxxChildContainer
.__init
__(self
, parent
, element
, refElem
=None)
862 # Remove pos parameter - not needed for sizeritems
863 if 'pos' in self
.child
.allParams
:
864 self
.child
.allParams
= self
.child
.allParams
[:]
865 self
.child
.allParams
.remove('pos')
867 class xxxPage(xxxChildContainer
):
868 allParams
= ['label', 'selected']
869 paramDict
= {'selected': ParamBool}
871 def __init__(self
, parent
, element
, refElem
=None):
872 xxxChildContainer
.__init
__(self
, parent
, element
, refElem
)
873 # pos and size dont matter for notebookpages
874 if 'pos' in self
.child
.allParams
:
875 self
.child
.allParams
= self
.child
.allParams
[:]
876 self
.child
.allParams
.remove('pos')
877 if 'size' in self
.child
.allParams
:
878 self
.child
.allParams
= self
.child
.allParams
[:]
879 self
.child
.allParams
.remove('size')
881 class xxxSpacer(xxxObject
):
882 hasName
= hasStyle
= False
883 allParams
= ['size', 'option', 'flag', 'border']
884 paramDict
= {'option': ParamInt}
885 default
= {'size': '0,0'}
886 def __init__(self
, parent
, element
, refElem
=None):
887 # For GridBag sizer items, extra parameters added
888 if isinstance(parent
, xxxGridBagSizer
):
889 self
.allParams
= self
.allParams
+ ['cellpos', 'cellspan']
890 xxxObject
.__init
__(self
, parent
, element
, refElem
)
892 class xxxMenuBar(xxxContainer
):
893 allParams
= ['style']
894 paramDict
= {'style': ParamNonGenericStyle}
# no generic styles
895 winStyles
= ['wxMB_DOCKABLE']
897 class xxxMenu(xxxContainer
):
898 allParams
= ['label', 'help', 'style']
899 default
= {'label': ''}
900 paramDict
= {'style': ParamNonGenericStyle}
# no generic styles
901 winStyles
= ['wxMENU_TEAROFF']
903 class xxxMenuItem(xxxObject
):
904 allParams
= ['label', 'bitmap', 'accel', 'help',
905 'checkable', 'radio', 'enabled', 'checked']
906 default
= {'label': ''}
909 class xxxSeparator(xxxObject
):
910 hasName
= hasStyle
= False
912 ################################################################################
915 class xxxUnknown(xxxObject
):
916 allParams
= ['pos', 'size', 'style']
917 winStyles
= ['wxNO_FULL_REPAINT_ON_RESIZE']
919 ################################################################################
922 _handlers
= [] # custom handler classes/funcs
923 _CFuncPtr
= None # ctypes function type
926 """Register hndlr function or XmlResourceHandler class."""
927 if _CFuncPtr
and isinstance(hndlr
, _CFuncPtr
):
928 _handlers
.append(hndlr
)
930 if not isinstance(hndlr
, type):
931 wx
.LogError('handler is not a type: %s' % hndlr
)
932 elif not issubclass(hndlr
, wx
.xrc
.XmlResourceHandler
):
933 wx
.LogError('handler is not a XmlResourceHandler: %s' % hndlr
)
935 _handlers
.append(hndlr
)
937 def load_dl(path
, localname
=''):
938 """Load shared/dynamic library into xxx namespace."""
940 localname
= os
.path
.basename(os
.path
.splitext(path
)[0])
944 _CFuncPtr
= ctypes
._CFuncPtr
946 wx
.LogError('ctypes module not found')
947 globals()[localname
] = None
950 dl
= ctypes
.CDLL(path
)
951 globals()[localname
] = dl
952 # Register AddXmlHandlers() if exists
954 register(dl
.AddXmlHandlers
)
958 wx
.LogError('error loading dynamic library: %s' % path
)
959 print traceback
.print_exc()
961 # Called when creating test window
964 if _CFuncPtr
and isinstance(h
, _CFuncPtr
):
968 wx
.LogError('error calling DL func: "%s"' % h
)
969 print traceback
.print_exc()
972 xrc
.XmlResource
.Get().AddHandler(apply(h
, ()))
974 wx
.LogError('error adding XmlHandler: "%s"' % h
)
975 print traceback
.print_exc()
981 def custom(klassName
, klass
='unknown'):
982 """Define custom control based on xrcClass.
984 klass: new object name
985 xrcClass: name of an existing XRC object class or
986 a class object defining class parameters.
988 if type(klass
) is str:
989 # Copy correct xxx class under new name
991 xxxClass
= types
.ClassType('xxx' + klassName
, kl
.__bases
__, kl
.__dict
__)
995 for param
in klass
.allParams
+ klass
.paramDict
.keys():
996 if not paramIDs
.has_key(param
):
997 paramIDs
[param
] = wx
.NewId()
998 # Insert in dictionaty
999 xxxDict
[klassName
] = xxxClass
1001 g
.pullDownMenu
.addCustom(klassName
)
1003 class xxxParamComment(xxxParam
):
1004 def __init__(self
, node
):
1005 xxxNode
.__init
__(self
, node
)
1006 self
.textNode
= node
1007 # Parse "pragma" comments
1008 if node
.data
and node
.data
[0] == '%':
1010 code
= node
.data
[1:]
1011 exec code
in globals()
1013 wx
.LogError('exec error: "%s"' % code
)
1014 print traceback
.print_exc()
1016 class xxxComment(xxxObject
):
1017 hasStyle
= hasName
= False
1018 allParams
= required
= ['comment']
1020 def __init__(self
, parent
, node
):
1021 self
.parent
= parent
1023 self
.isElement
= False
1025 self
.className
= 'comment'
1026 self
.ref
= self
.subclass
= None
1027 self
.params
= {'comment': xxxParamComment(node)}
1030 # Replace newlines by \n to avoid tree item resizing
1031 return self
.params
['comment'].value().replace('\n', r
'\n')
1033 ################################################################################
1035 # Mapping of XRC names to xxx classes
1037 'wxPanel': xxxPanel
,
1038 'wxDialog': xxxDialog
,
1039 'wxFrame': xxxFrame
,
1041 'wxToolBar': xxxToolBar
,
1042 'wxStatusBar': xxxStatusBar
,
1043 'wxWizard': xxxWizard
,
1044 'wxWizardPage': xxxWizardPage
,
1045 'wxWizardPageSimple': xxxWizardPageSimple
,
1047 'wxBitmap': xxxBitmap
,
1050 'wxButton': xxxButton
,
1051 'wxBitmapButton': xxxBitmapButton
,
1052 'wxRadioButton': xxxRadioButton
,
1053 'wxSpinButton': xxxSpinButton
,
1054 'wxToggleButton' : xxxToggleButton
,
1056 'wxStaticBox': xxxStaticBox
,
1057 'wxStaticBitmap': xxxStaticBitmap
,
1058 'wxRadioBox': xxxRadioBox
,
1059 'wxComboBox': xxxComboBox
,
1060 'wxCheckBox': xxxCheckBox
,
1061 'wxListBox': xxxListBox
,
1063 'wxStaticText': xxxStaticText
,
1064 'wxStaticLine': xxxStaticLine
,
1065 'wxTextCtrl': xxxTextCtrl
,
1066 'wxChoice': xxxChoice
,
1067 'wxSlider': xxxSlider
,
1068 'wxGauge': xxxGauge
,
1069 'wxScrollBar': xxxScrollBar
,
1070 'wxTreeCtrl': xxxTreeCtrl
,
1071 'wxListCtrl': xxxListCtrl
,
1072 'wxCheckListBox': xxxCheckList
,
1073 'notebookpage': xxxPage
,
1074 'choicebookpage': xxxPage
,
1075 'listbookpage': xxxPage
,
1076 'wxNotebook': xxxNotebook
,
1077 'wxChoicebook': xxxChoicebook
,
1078 'wxListbook': xxxListbook
,
1079 'wxSplitterWindow': xxxSplitterWindow
,
1080 'wxHtmlWindow': xxxHtmlWindow
,
1081 'wxCalendarCtrl': xxxCalendarCtrl
,
1082 'wxGenericDirCtrl': xxxGenericDirCtrl
,
1083 'wxSpinCtrl': xxxSpinCtrl
,
1084 'wxScrolledWindow': xxxScrolledWindow
,
1086 'wxFilePickerCtrl': xxxFilePickerCtrl
,
1087 'wxDatePickerCtrl': xxxDateCtrl
,
1089 'wxBoxSizer': xxxBoxSizer
,
1090 'wxStaticBoxSizer': xxxStaticBoxSizer
,
1091 'wxGridSizer': xxxGridSizer
,
1092 'wxFlexGridSizer': xxxFlexGridSizer
,
1093 'wxGridBagSizer': xxxGridBagSizer
,
1094 'wxStdDialogButtonSizer': xxxStdDialogButtonSizer
,
1095 'sizeritem': xxxSizerItem
, 'button': xxxSizerItemButton
,
1096 'spacer': xxxSpacer
,
1098 'wxMenuBar': xxxMenuBar
,
1100 'wxMenuItem': xxxMenuItem
,
1101 'separator': xxxSeparator
,
1103 'unknown': xxxUnknown
,
1104 'comment': xxxComment
,
1107 # Create IDs for all parameters of all classes
1108 paramIDs
= {'fg': wx
.NewId(), 'bg': wx
.NewId(), 'exstyle': wx
.NewId(), 'font': wx
.NewId(),
1109 'enabled': wx
.NewId(), 'focused': wx
.NewId(), 'hidden': wx
.NewId(),
1110 'tooltip': wx
.NewId(), 'encoding': wx
.NewId(),
1111 'cellpos': wx
.NewId(), 'cellspan': wx
.NewId(),
1114 for cl
in xxxDict
.values():
1116 for param
in cl
.allParams
+ cl
.paramDict
.keys():
1117 if not paramIDs
.has_key(param
):
1118 paramIDs
[param
] = wx
.NewId()
1120 ################################################################################
1123 # Test for object elements
1125 return node
.nodeType
== minidom
.Node
.ELEMENT_NODE
and \
1126 node
.tagName
in ['object', 'object_ref'] or \
1127 node
.nodeType
== minidom
.Node
.COMMENT_NODE
1129 # Make XXX object from some DOM object, selecting correct class
1130 def MakeXXXFromDOM(parent
, node
):
1131 if node
.nodeType
== minidom
.Node
.COMMENT_NODE
:
1132 return xxxComment(parent
, node
)
1133 if node
.tagName
== 'object_ref':
1134 ref
= node
.getAttribute('ref')
1135 refElem
= FindResource(ref
)
1136 if refElem
: cls
= refElem
.getAttribute('class')
1137 else: return xxxUnknown(parent
, node
)
1140 cls
= node
.getAttribute('class')
1142 klass
= xxxDict
[cls
]
1144 # If we encounter a weird class, use unknown template
1145 print 'WARNING: unsupported class:', node
.getAttribute('class')
1147 return klass(parent
, node
, refElem
)
1149 # Make empty DOM element
1150 def MakeEmptyDOM(className
):
1151 elem
= g
.tree
.dom
.createElement('object')
1152 elem
.setAttribute('class', className
)
1153 # Set required and default parameters
1154 xxxClass
= xxxDict
[className
]
1155 defaultNotRequired
= filter(lambda x
, l
=xxxClass
.required
: x
not in l
,
1156 xxxClass
.default
.keys())
1157 for param
in xxxClass
.required
+ defaultNotRequired
:
1158 textElem
= g
.tree
.dom
.createElement(param
)
1160 textNode
= g
.tree
.dom
.createTextNode(xxxClass
.default
[param
])
1162 textNode
= g
.tree
.dom
.createTextNode('')
1163 textElem
.appendChild(textNode
)
1164 elem
.appendChild(textElem
)
1167 # Make empty XXX object
1168 def MakeEmptyXXX(parent
, className
):
1169 # Make corresponding DOM object first
1170 elem
= MakeEmptyDOM(className
)
1171 # If parent is a sizer, we should create sizeritem object, except for spacers
1173 if parent
.isSizer
and className
!= 'spacer':
1174 sizerItemElem
= MakeEmptyDOM(parent
.itemTag
)
1175 sizerItemElem
.appendChild(elem
)
1176 elem
= sizerItemElem
1177 elif isinstance(parent
, xxxNotebook
):
1178 pageElem
= MakeEmptyDOM('notebookpage')
1179 pageElem
.appendChild(elem
)
1181 elif isinstance(parent
, xxxChoicebook
):
1182 pageElem
= MakeEmptyDOM('choicebookpage')
1183 pageElem
.appendChild(elem
)
1185 elif isinstance(parent
, xxxListbook
):
1186 pageElem
= MakeEmptyDOM('listbookpage')
1187 pageElem
.appendChild(elem
)
1189 # Now just make object
1190 return MakeXXXFromDOM(parent
, elem
)
1192 # Make empty DOM element for reference
1193 def MakeEmptyRefDOM(ref
):
1194 elem
= g
.tree
.dom
.createElement('object_ref')
1195 elem
.setAttribute('ref', ref
)
1198 # Make empty XXX object
1199 def MakeEmptyRefXXX(parent
, ref
):
1200 # Make corresponding DOM object first
1201 elem
= MakeEmptyRefDOM(ref
)
1202 # If parent is a sizer, we should create sizeritem object, except for spacers
1205 sizerItemElem
= MakeEmptyDOM(parent
.itemTag
)
1206 sizerItemElem
.appendChild(elem
)
1207 elem
= sizerItemElem
1208 elif isinstance(parent
, xxxNotebook
):
1209 pageElem
= MakeEmptyDOM('notebookpage')
1210 pageElem
.appendChild(elem
)
1212 elif isinstance(parent
, xxxChoicebook
):
1213 pageElem
= MakeEmptyDOM('choicebookpage')
1214 pageElem
.appendChild(elem
)
1216 elif isinstance(parent
, xxxListbook
):
1217 pageElem
= MakeEmptyDOM('listbookpage')
1218 pageElem
.appendChild(elem
)
1220 # Now just make object
1221 xxx
= MakeXXXFromDOM(parent
, elem
)
1222 # Label is not used for references
1223 xxx
.allParams
= xxx
.allParams
[:]
1224 #xxx.allParams.remove('label')
1227 # Make empty comment node
1228 def MakeEmptyCommentDOM():
1229 node
= g
.tree
.dom
.createComment('')
1232 # Make empty xxxComment
1233 def MakeEmptyCommentXXX(parent
):
1234 node
= MakeEmptyCommentDOM()
1235 # Now just make object
1236 xxx
= MakeXXXFromDOM(parent
, node
)