]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
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$ | |
1fded56b | 6 | |
d14a1e28 RD |
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): | |
80389ff7 RR |
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") | |
d14a1e28 RD |
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') | |
2481bf3c | 213 | self.subclass = element.getAttribute('subclass') |
d14a1e28 RD |
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': | |
edfeb1b8 | 229 | if self.className == 'wxCheckListBox': |
d14a1e28 RD |
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': | |
edfeb1b8 | 251 | if self.className == 'wxCheckListBox': |
d14a1e28 RD |
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() | |
2481bf3c RR |
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 | |
d14a1e28 RD |
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: | |
d14a1e28 | 339 | def value(self): |
7353d818 | 340 | return g.currentEncoding |
d14a1e28 | 341 | def update(self, val): |
7353d818 | 342 | g.currentEncoding = val |
d14a1e28 RD |
343 | |
344 | # Special class for root node | |
345 | class xxxMainNode(xxxContainer): | |
346 | allParams = ['encoding'] | |
347 | hasStyle = hasName = False | |
348 | def __init__(self, dom): | |
349 | xxxContainer.__init__(self, None, dom.documentElement) | |
350 | self.className = 'XML tree' | |
351 | # Reset required parameters after processing XML, because encoding is | |
352 | # a little special | |
353 | self.required = ['encoding'] | |
7353d818 | 354 | self.params['encoding'] = xxxEncoding() |
d14a1e28 RD |
355 | |
356 | ################################################################################ | |
357 | # Top-level windwows | |
358 | ||
359 | class xxxPanel(xxxContainer): | |
360 | allParams = ['pos', 'size', 'style'] | |
361 | styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle', | |
362 | 'tooltip'] | |
363 | winStyles = ['wxNO_3D', 'wxTAB_TRAVERSAL', 'wxCLIP_CHILDREN'] | |
364 | exStyles = ['wxWS_EX_VALIDATE_RECURSIVELY'] | |
365 | ||
366 | class xxxDialog(xxxContainer): | |
367 | allParams = ['title', 'centered', 'pos', 'size', 'style'] | |
368 | paramDict = {'centered': ParamBool} | |
369 | required = ['title'] | |
370 | default = {'title': ''} | |
371 | winStyles = ['wxDEFAULT_DIALOG_STYLE', 'wxSTAY_ON_TOP', | |
9abdd2cb | 372 | ## 'wxDIALOG_MODAL', 'wxDIALOG_MODELESS', |
d14a1e28 RD |
373 | 'wxCAPTION', 'wxSYSTEM_MENU', 'wxRESIZE_BORDER', 'wxRESIZE_BOX', |
374 | 'wxTHICK_FRAME', | |
375 | 'wxNO_3D', 'wxTAB_TRAVERSAL', 'wxCLIP_CHILDREN'] | |
376 | styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle', | |
377 | 'tooltip'] | |
378 | exStyles = ['wxWS_EX_VALIDATE_RECURSIVELY'] | |
379 | ||
380 | class xxxFrame(xxxContainer): | |
381 | allParams = ['title', 'centered', 'pos', 'size', 'style'] | |
382 | paramDict = {'centered': ParamBool} | |
383 | required = ['title'] | |
384 | default = {'title': ''} | |
385 | winStyles = ['wxDEFAULT_FRAME_STYLE', 'wxDEFAULT_DIALOG_STYLE', | |
386 | 'wxSTAY_ON_TOP', | |
387 | 'wxCAPTION', 'wxSYSTEM_MENU', 'wxRESIZE_BORDER', | |
388 | 'wxRESIZE_BOX', 'wxMINIMIZE_BOX', 'wxMAXIMIZE_BOX', | |
389 | 'wxFRAME_FLOAT_ON_PARENT', 'wxFRAME_TOOL_WINDOW', | |
390 | 'wxNO_3D', 'wxTAB_TRAVERSAL', 'wxCLIP_CHILDREN'] | |
391 | styles = ['fg', 'bg', 'font', 'enabled', 'focused', 'hidden', 'exstyle', | |
392 | 'tooltip'] | |
393 | exStyles = ['wxWS_EX_VALIDATE_RECURSIVELY'] | |
394 | ||
395 | class xxxTool(xxxObject): | |
396 | allParams = ['bitmap', 'bitmap2', 'toggle', 'tooltip', 'longhelp'] | |
397 | required = ['bitmap'] | |
398 | paramDict = {'bitmap2': ParamBitmap, 'toggle': ParamBool} | |
399 | hasStyle = False | |
400 | ||
401 | class xxxToolBar(xxxContainer): | |
402 | allParams = ['bitmapsize', 'margins', 'packing', 'separation', | |
403 | 'pos', 'size', 'style'] | |
404 | hasStyle = False | |
405 | paramDict = {'bitmapsize': ParamPosSize, 'margins': ParamPosSize, | |
406 | 'packing': ParamInt, 'separation': ParamInt, | |
407 | 'style': ParamNonGenericStyle} | |
408 | winStyles = ['wxTB_FLAT', 'wxTB_DOCKABLE', 'wxTB_VERTICAL', 'wxTB_HORIZONTAL'] | |
409 | ||
410 | ################################################################################ | |
411 | # Bitmap, Icon | |
412 | ||
413 | class xxxBitmap(xxxObject): | |
414 | allParams = ['bitmap'] | |
415 | required = ['bitmap'] | |
416 | ||
417 | # Just like bitmap | |
418 | class xxxIcon(xxxObject): | |
419 | allParams = ['icon'] | |
420 | required = ['icon'] | |
421 | ||
422 | ################################################################################ | |
423 | # Controls | |
424 | ||
425 | class xxxStaticText(xxxObject): | |
426 | allParams = ['label', 'pos', 'size', 'style'] | |
427 | required = ['label'] | |
428 | default = {'label': ''} | |
429 | winStyles = ['wxALIGN_LEFT', 'wxALIGN_RIGHT', 'wxALIGN_CENTRE', 'wxST_NO_AUTORESIZE'] | |
430 | ||
431 | class xxxStaticLine(xxxObject): | |
432 | allParams = ['pos', 'size', 'style'] | |
433 | winStyles = ['wxLI_HORIZONTAL', 'wxLI_VERTICAL'] | |
434 | ||
435 | class xxxStaticBitmap(xxxObject): | |
436 | allParams = ['bitmap', 'pos', 'size', 'style'] | |
437 | required = ['bitmap'] | |
438 | ||
439 | class xxxTextCtrl(xxxObject): | |
440 | allParams = ['value', 'pos', 'size', 'style'] | |
441 | winStyles = ['wxTE_PROCESS_ENTER', 'wxTE_PROCESS_TAB', 'wxTE_MULTILINE', | |
442 | 'wxTE_PASSWORD', 'wxTE_READONLY', 'wxHSCROLL'] | |
443 | paramDict = {'value': ParamMultilineText} | |
444 | ||
445 | class xxxChoice(xxxObject): | |
446 | allParams = ['content', 'selection', 'pos', 'size', 'style'] | |
447 | required = ['content'] | |
448 | default = {'content': '[]'} | |
449 | winStyles = ['wxCB_SORT'] | |
450 | ||
451 | class xxxSlider(xxxObject): | |
452 | allParams = ['value', 'min', 'max', 'pos', 'size', 'style', | |
453 | 'tickfreq', 'pagesize', 'linesize', 'thumb', 'tick', | |
454 | 'selmin', 'selmax'] | |
455 | paramDict = {'value': ParamInt, 'tickfreq': ParamInt, 'pagesize': ParamInt, | |
456 | 'linesize': ParamInt, 'thumb': ParamInt, 'thumb': ParamInt, | |
457 | 'tick': ParamInt, 'selmin': ParamInt, 'selmax': ParamInt} | |
458 | required = ['value', 'min', 'max'] | |
459 | winStyles = ['wxSL_HORIZONTAL', 'wxSL_VERTICAL', 'wxSL_AUTOTICKS', 'wxSL_LABELS', | |
460 | 'wxSL_LEFT', 'wxSL_RIGHT', 'wxSL_TOP', 'wxSL_BOTTOM', | |
461 | 'wxSL_BOTH', 'wxSL_SELRANGE'] | |
462 | ||
463 | class xxxGauge(xxxObject): | |
464 | allParams = ['range', 'pos', 'size', 'style', 'value', 'shadow', 'bezel'] | |
465 | paramDict = {'range': ParamInt, 'value': ParamInt, | |
466 | 'shadow': ParamInt, 'bezel': ParamInt} | |
467 | winStyles = ['wxGA_HORIZONTAL', 'wxGA_VERTICAL', 'wxGA_PROGRESSBAR', 'wxGA_SMOOTH'] | |
468 | ||
469 | class xxxScrollBar(xxxObject): | |
470 | allParams = ['pos', 'size', 'style', 'value', 'thumbsize', 'range', 'pagesize'] | |
471 | paramDict = {'value': ParamInt, 'range': ParamInt, 'thumbsize': ParamInt, | |
472 | 'pagesize': ParamInt} | |
473 | winStyles = ['wxSB_HORIZONTAL', 'wxSB_VERTICAL'] | |
474 | ||
475 | class xxxListCtrl(xxxObject): | |
476 | allParams = ['pos', 'size', 'style'] | |
477 | winStyles = ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON', | |
478 | 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE', | |
479 | 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER', | |
480 | 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING'] | |
481 | ||
482 | class xxxTreeCtrl(xxxObject): | |
483 | allParams = ['pos', 'size', 'style'] | |
484 | winStyles = ['wxTR_HAS_BUTTONS', 'wxTR_NO_LINES', 'wxTR_LINES_AT_ROOT', | |
485 | 'wxTR_EDIT_LABELS', 'wxTR_MULTIPLE'] | |
486 | ||
487 | class xxxHtmlWindow(xxxObject): | |
488 | allParams = ['pos', 'size', 'style', 'borders', 'url', 'htmlcode'] | |
489 | paramDict = {'borders': ParamInt, 'htmlcode':ParamMultilineText} | |
490 | winStyles = ['wxHW_SCROLLBAR_NEVER', 'wxHW_SCROLLBAR_AUTO'] | |
491 | ||
492 | class xxxCalendarCtrl(xxxObject): | |
493 | allParams = ['pos', 'size', 'style'] | |
494 | ||
495 | class xxxNotebook(xxxContainer): | |
496 | allParams = ['usenotebooksizer', 'pos', 'size', 'style'] | |
497 | paramDict = {'usenotebooksizer': ParamBool} | |
498 | winStyles = ['wxNB_FIXEDWIDTH', 'wxNB_LEFT', 'wxNB_RIGHT', 'wxNB_BOTTOM'] | |
499 | ||
68ae5821 RD |
500 | class xxxSplitterWindow(xxxContainer): |
501 | allParams = ['orientation', 'sashpos', 'minsize', 'pos', 'size', 'style'] | |
502 | paramDict = {'orientation': ParamOrientation, 'sashpos': ParamUnit, 'minsize': ParamUnit } | |
503 | winStyles = ['wxSP_3D', 'wxSP_3DSASH', 'wxSP_3DBORDER', 'wxSP_BORDER', | |
504 | 'wxSP_NOBORDER', 'wxSP_PERMIT_UNSPLIT', 'wxSP_LIVE_UPDATE', | |
505 | 'wxSP_NO_XP_THEME' ] | |
506 | ||
d14a1e28 RD |
507 | class xxxGenericDirCtrl(xxxObject): |
508 | allParams = ['defaultfolder', 'filter', 'defaultfilter', 'pos', 'size', 'style'] | |
509 | paramDict = {'defaultfilter': ParamInt} | |
510 | winStyles = ['wxDIRCTRL_DIR_ONLY', 'wxDIRCTRL_3D_INTERNAL', 'wxDIRCTRL_SELECT_FIRST', | |
511 | 'wxDIRCTRL_SHOW_FILTERS', 'wxDIRCTRL_EDIT_LABELS'] | |
512 | ||
513 | class xxxScrolledWindow(xxxContainer): | |
514 | allParams = ['pos', 'size', 'style'] | |
515 | winStyles = ['wxHSCROLL', 'wxVSCROLL'] | |
516 | ||
517 | ################################################################################ | |
518 | # Buttons | |
519 | ||
520 | class xxxButton(xxxObject): | |
521 | allParams = ['label', 'default', 'pos', 'size', 'style'] | |
522 | paramDict = {'default': ParamBool} | |
523 | required = ['label'] | |
524 | winStyles = ['wxBU_LEFT', 'wxBU_TOP', 'wxBU_RIGHT', 'wxBU_BOTTOM'] | |
525 | ||
526 | class xxxBitmapButton(xxxObject): | |
527 | allParams = ['bitmap', 'selected', 'focus', 'disabled', 'default', | |
528 | 'pos', 'size', 'style'] | |
529 | required = ['bitmap'] | |
530 | winStyles = ['wxBU_AUTODRAW', 'wxBU_LEFT', 'wxBU_TOP', | |
531 | 'wxBU_RIGHT', 'wxBU_BOTTOM'] | |
532 | ||
533 | class xxxRadioButton(xxxObject): | |
534 | allParams = ['label', 'value', 'pos', 'size', 'style'] | |
535 | paramDict = {'value': ParamBool} | |
536 | required = ['label'] | |
537 | winStyles = ['wxRB_GROUP'] | |
538 | ||
539 | class xxxSpinButton(xxxObject): | |
540 | allParams = ['value', 'min', 'max', 'pos', 'size', 'style'] | |
541 | paramDict = {'value': ParamInt} | |
542 | winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP'] | |
543 | ||
544 | class xxxSpinCtrl(xxxObject): | |
545 | allParams = ['value', 'min', 'max', 'pos', 'size', 'style'] | |
546 | paramDict = {'value': ParamInt} | |
547 | winStyles = ['wxSP_HORIZONTAL', 'wxSP_VERTICAL', 'wxSP_ARROW_KEYS', 'wxSP_WRAP'] | |
548 | ||
3d49f2fb RD |
549 | class xxxToggleButton(xxxObject): |
550 | allParams = ['label', 'checked', 'pos', 'size', 'style'] | |
551 | paramDict = {'checked': ParamBool} | |
552 | required = ['label'] | |
553 | ||
d14a1e28 RD |
554 | ################################################################################ |
555 | # Boxes | |
556 | ||
557 | class xxxStaticBox(xxxObject): | |
558 | allParams = ['label', 'pos', 'size', 'style'] | |
559 | required = ['label'] | |
560 | ||
561 | class xxxRadioBox(xxxObject): | |
562 | allParams = ['label', 'content', 'selection', 'dimension', 'pos', 'size', 'style'] | |
563 | paramDict = {'dimension': ParamInt} | |
564 | required = ['label', 'content'] | |
565 | default = {'content': '[]'} | |
566 | winStyles = ['wxRA_SPECIFY_ROWS', 'wxRA_SPECIFY_COLS'] | |
567 | ||
568 | class xxxCheckBox(xxxObject): | |
569 | allParams = ['label', 'checked', 'pos', 'size', 'style'] | |
570 | paramDict = {'checked': ParamBool} | |
3d49f2fb RD |
571 | winStyles = ['wxCHK_2STATE', 'wxCHK_3STATE', 'wxCHK_ALLOW_3RD_STATE_FOR_USER', |
572 | 'wxALIGN_RIGHT'] | |
d14a1e28 RD |
573 | required = ['label'] |
574 | ||
575 | class xxxComboBox(xxxObject): | |
576 | allParams = ['content', 'selection', 'value', 'pos', 'size', 'style'] | |
577 | required = ['content'] | |
578 | default = {'content': '[]'} | |
579 | winStyles = ['wxCB_SIMPLE', 'wxCB_SORT', 'wxCB_READONLY', 'wxCB_DROPDOWN'] | |
580 | ||
581 | class xxxListBox(xxxObject): | |
582 | allParams = ['content', 'selection', 'pos', 'size', 'style'] | |
583 | required = ['content'] | |
584 | default = {'content': '[]'} | |
585 | winStyles = ['wxLB_SINGLE', 'wxLB_MULTIPLE', 'wxLB_EXTENDED', 'wxLB_HSCROLL', | |
586 | 'wxLB_ALWAYS_SB', 'wxLB_NEEDED_SB', 'wxLB_SORT'] | |
587 | ||
588 | class xxxCheckList(xxxObject): | |
589 | allParams = ['content', 'pos', 'size', 'style'] | |
590 | required = ['content'] | |
591 | default = {'content': '[]'} | |
592 | winStyles = ['wxLC_LIST', 'wxLC_REPORT', 'wxLC_ICON', 'wxLC_SMALL_ICON', | |
593 | 'wxLC_ALIGN_TOP', 'wxLC_ALIGN_LEFT', 'wxLC_AUTOARRANGE', | |
594 | 'wxLC_USER_TEXT', 'wxLC_EDIT_LABELS', 'wxLC_NO_HEADER', | |
595 | 'wxLC_SINGLE_SEL', 'wxLC_SORT_ASCENDING', 'wxLC_SORT_DESCENDING'] | |
596 | paramDict = {'content': ParamContentCheckList} | |
597 | ||
598 | ################################################################################ | |
599 | # Sizers | |
600 | ||
601 | class xxxSizer(xxxContainer): | |
602 | hasName = hasStyle = False | |
603 | paramDict = {'orient': ParamOrient} | |
604 | isSizer = True | |
605 | ||
606 | class xxxBoxSizer(xxxSizer): | |
607 | allParams = ['orient'] | |
608 | required = ['orient'] | |
609 | default = {'orient': 'wxVERTICAL'} | |
610 | # Tree icon depends on orientation | |
611 | def treeImage(self): | |
612 | if self.params['orient'].value() == 'wxHORIZONTAL': return self.imageH | |
613 | else: return self.imageV | |
614 | ||
615 | class xxxStaticBoxSizer(xxxBoxSizer): | |
616 | allParams = ['label', 'orient'] | |
617 | required = ['label', 'orient'] | |
618 | ||
619 | class xxxGridSizer(xxxSizer): | |
620 | allParams = ['cols', 'rows', 'vgap', 'hgap'] | |
621 | required = ['cols'] | |
622 | default = {'cols': '2', 'rows': '2'} | |
623 | ||
624 | # For repeated parameters | |
625 | class xxxParamMulti: | |
626 | def __init__(self, node): | |
627 | self.node = node | |
628 | self.l, self.data = [], [] | |
629 | def append(self, param): | |
630 | self.l.append(param) | |
631 | self.data.append(param.value()) | |
632 | def value(self): | |
633 | return self.data | |
634 | def remove(self): | |
635 | for param in self.l: | |
636 | param.remove() | |
637 | self.l, self.data = [], [] | |
638 | ||
639 | class xxxFlexGridSizer(xxxGridSizer): | |
640 | specials = ['growablecols', 'growablerows'] | |
641 | allParams = ['cols', 'rows', 'vgap', 'hgap'] + specials | |
642 | paramDict = {'growablecols':ParamIntList, 'growablerows':ParamIntList} | |
643 | # Special processing for growable* parameters | |
644 | # (they are represented by several nodes) | |
645 | def special(self, tag, node): | |
646 | if not self.params.has_key(tag): | |
647 | # Create new multi-group | |
648 | self.params[tag] = xxxParamMulti(node) | |
649 | self.params[tag].append(xxxParamInt(node)) | |
650 | def setSpecial(self, param, value): | |
651 | # Straightforward implementation: remove, add again | |
652 | self.params[param].remove() | |
653 | del self.params[param] | |
654 | for i in value: | |
655 | node = g.tree.dom.createElement(param) | |
656 | text = g.tree.dom.createTextNode(str(i)) | |
657 | node.appendChild(text) | |
658 | self.element.appendChild(node) | |
659 | self.special(param, node) | |
660 | ||
a4c013b2 RR |
661 | class xxxGridBagSizer(xxxSizer): |
662 | specials = ['growablecols', 'growablerows'] | |
663 | allParams = ['vgap', 'hgap'] + specials | |
664 | paramDict = {'growablecols':ParamIntList, 'growablerows':ParamIntList} | |
665 | # Special processing for growable* parameters | |
666 | # (they are represented by several nodes) | |
667 | def special(self, tag, node): | |
668 | if not self.params.has_key(tag): | |
669 | # Create new multi-group | |
670 | self.params[tag] = xxxParamMulti(node) | |
671 | self.params[tag].append(xxxParamInt(node)) | |
672 | def setSpecial(self, param, value): | |
673 | # Straightforward implementation: remove, add again | |
674 | self.params[param].remove() | |
675 | del self.params[param] | |
676 | for i in value: | |
677 | node = g.tree.dom.createElement(param) | |
678 | text = g.tree.dom.createTextNode(str(i)) | |
679 | node.appendChild(text) | |
680 | self.element.appendChild(node) | |
681 | self.special(param, node) | |
682 | ||
d14a1e28 RD |
683 | # Container with only one child. |
684 | # Not shown in tree. | |
685 | class xxxChildContainer(xxxObject): | |
686 | hasName = hasStyle = False | |
687 | hasChild = True | |
688 | def __init__(self, parent, element): | |
689 | xxxObject.__init__(self, parent, element) | |
690 | # Must have one child with 'object' tag, but we don't check it | |
691 | nodes = element.childNodes[:] # create copy | |
692 | for node in nodes: | |
693 | if node.nodeType == minidom.Node.ELEMENT_NODE: | |
694 | if node.tagName == 'object': | |
695 | # Create new xxx object for child node | |
696 | self.child = MakeXXXFromDOM(self, node) | |
697 | self.child.parent = parent | |
698 | # Copy hasChildren and isSizer attributes | |
699 | self.hasChildren = self.child.hasChildren | |
700 | self.isSizer = self.child.isSizer | |
701 | return # success | |
702 | else: | |
703 | element.removeChild(node) | |
704 | node.unlink() | |
705 | assert 0, 'no child found' | |
706 | ||
707 | class xxxSizerItem(xxxChildContainer): | |
a4c013b2 RR |
708 | allParams = ['option', 'flag', 'border', 'minsize', 'ratio'] |
709 | paramDict = {'option': ParamInt, 'minsize': ParamPosSize, 'ratio': ParamPosSize} | |
710 | #default = {'cellspan': '1,1'} | |
d14a1e28 | 711 | def __init__(self, parent, element): |
a4c013b2 RR |
712 | # For GridBag sizer items, extra parameters added |
713 | if isinstance(parent, xxxGridBagSizer): | |
714 | self.allParams = self.allParams + ['cellpos', 'cellspan'] | |
d14a1e28 RD |
715 | xxxChildContainer.__init__(self, parent, element) |
716 | # Remove pos parameter - not needed for sizeritems | |
717 | if 'pos' in self.child.allParams: | |
718 | self.child.allParams = self.child.allParams[:] | |
719 | self.child.allParams.remove('pos') | |
720 | ||
721 | class xxxNotebookPage(xxxChildContainer): | |
722 | allParams = ['label', 'selected'] | |
723 | paramDict = {'selected': ParamBool} | |
724 | required = ['label'] | |
725 | def __init__(self, parent, element): | |
726 | xxxChildContainer.__init__(self, parent, element) | |
727 | # pos and size dont matter for notebookpages | |
728 | if 'pos' in self.child.allParams: | |
729 | self.child.allParams = self.child.allParams[:] | |
730 | self.child.allParams.remove('pos') | |
731 | if 'size' in self.child.allParams: | |
732 | self.child.allParams = self.child.allParams[:] | |
733 | self.child.allParams.remove('size') | |
734 | ||
735 | class xxxSpacer(xxxObject): | |
736 | hasName = hasStyle = False | |
737 | allParams = ['size', 'option', 'flag', 'border'] | |
738 | paramDict = {'option': ParamInt} | |
739 | default = {'size': '0,0'} | |
740 | ||
741 | class xxxMenuBar(xxxContainer): | |
742 | allParams = ['style'] | |
743 | paramDict = {'style': ParamNonGenericStyle} # no generic styles | |
744 | winStyles = ['wxMB_DOCKABLE'] | |
745 | ||
746 | class xxxMenu(xxxContainer): | |
747 | allParams = ['label', 'help', 'style'] | |
748 | default = {'label': ''} | |
749 | paramDict = {'style': ParamNonGenericStyle} # no generic styles | |
750 | winStyles = ['wxMENU_TEAROFF'] | |
751 | ||
752 | class xxxMenuItem(xxxObject): | |
753 | allParams = ['label', 'bitmap', 'accel', 'help', | |
754 | 'checkable', 'radio', 'enabled', 'checked'] | |
755 | default = {'label': ''} | |
756 | hasStyle = False | |
757 | ||
758 | class xxxSeparator(xxxObject): | |
759 | hasName = hasStyle = False | |
760 | ||
761 | ################################################################################ | |
762 | # Unknown control | |
763 | ||
764 | class xxxUnknown(xxxObject): | |
765 | allParams = ['pos', 'size', 'style'] | |
766 | paramDict = {'style': ParamNonGenericStyle} # no generic styles | |
767 | ||
768 | ################################################################################ | |
769 | ||
770 | xxxDict = { | |
771 | 'wxPanel': xxxPanel, | |
772 | 'wxDialog': xxxDialog, | |
773 | 'wxFrame': xxxFrame, | |
774 | 'tool': xxxTool, | |
775 | 'wxToolBar': xxxToolBar, | |
776 | ||
777 | 'wxBitmap': xxxBitmap, | |
778 | 'wxIcon': xxxIcon, | |
779 | ||
780 | 'wxButton': xxxButton, | |
781 | 'wxBitmapButton': xxxBitmapButton, | |
782 | 'wxRadioButton': xxxRadioButton, | |
783 | 'wxSpinButton': xxxSpinButton, | |
3d49f2fb | 784 | 'wxToggleButton' : xxxToggleButton, |
d14a1e28 RD |
785 | |
786 | 'wxStaticBox': xxxStaticBox, | |
787 | 'wxStaticBitmap': xxxStaticBitmap, | |
788 | 'wxRadioBox': xxxRadioBox, | |
789 | 'wxComboBox': xxxComboBox, | |
790 | 'wxCheckBox': xxxCheckBox, | |
791 | 'wxListBox': xxxListBox, | |
792 | ||
793 | 'wxStaticText': xxxStaticText, | |
794 | 'wxStaticLine': xxxStaticLine, | |
795 | 'wxTextCtrl': xxxTextCtrl, | |
796 | 'wxChoice': xxxChoice, | |
797 | 'wxSlider': xxxSlider, | |
798 | 'wxGauge': xxxGauge, | |
799 | 'wxScrollBar': xxxScrollBar, | |
800 | 'wxTreeCtrl': xxxTreeCtrl, | |
801 | 'wxListCtrl': xxxListCtrl, | |
edfeb1b8 | 802 | 'wxCheckListBox': xxxCheckList, |
d14a1e28 | 803 | 'wxNotebook': xxxNotebook, |
68ae5821 | 804 | 'wxSplitterWindow': xxxSplitterWindow, |
d14a1e28 RD |
805 | 'notebookpage': xxxNotebookPage, |
806 | 'wxHtmlWindow': xxxHtmlWindow, | |
807 | 'wxCalendarCtrl': xxxCalendarCtrl, | |
808 | 'wxGenericDirCtrl': xxxGenericDirCtrl, | |
809 | 'wxSpinCtrl': xxxSpinCtrl, | |
810 | 'wxScrolledWindow': xxxScrolledWindow, | |
811 | ||
812 | 'wxBoxSizer': xxxBoxSizer, | |
813 | 'wxStaticBoxSizer': xxxStaticBoxSizer, | |
814 | 'wxGridSizer': xxxGridSizer, | |
815 | 'wxFlexGridSizer': xxxFlexGridSizer, | |
a4c013b2 | 816 | 'wxGridBagSizer': xxxGridBagSizer, |
d14a1e28 RD |
817 | 'sizeritem': xxxSizerItem, |
818 | 'spacer': xxxSpacer, | |
819 | ||
820 | 'wxMenuBar': xxxMenuBar, | |
821 | 'wxMenu': xxxMenu, | |
822 | 'wxMenuItem': xxxMenuItem, | |
823 | 'separator': xxxSeparator, | |
824 | ||
825 | 'unknown': xxxUnknown, | |
826 | } | |
827 | ||
828 | # Create IDs for all parameters of all classes | |
829 | paramIDs = {'fg': wxNewId(), 'bg': wxNewId(), 'exstyle': wxNewId(), 'font': wxNewId(), | |
830 | 'enabled': wxNewId(), 'focused': wxNewId(), 'hidden': wxNewId(), | |
a4c013b2 RR |
831 | 'tooltip': wxNewId(), 'encoding': wxNewId(), |
832 | 'cellpos': wxNewId(), 'cellspan': wxNewId() | |
d14a1e28 RD |
833 | } |
834 | for cl in xxxDict.values(): | |
835 | if cl.allParams: | |
836 | for param in cl.allParams + cl.paramDict.keys(): | |
837 | if not paramIDs.has_key(param): | |
838 | paramIDs[param] = wxNewId() | |
839 | ||
840 | ################################################################################ | |
841 | # Helper functions | |
842 | ||
843 | # Test for object elements | |
844 | def IsObject(node): | |
845 | return node.nodeType == minidom.Node.ELEMENT_NODE and node.tagName == 'object' | |
846 | ||
847 | # Make XXX object from some DOM object, selecting correct class | |
848 | def MakeXXXFromDOM(parent, element): | |
849 | try: | |
850 | klass = xxxDict[element.getAttribute('class')] | |
851 | except KeyError: | |
852 | # If we encounter a weird class, use unknown template | |
853 | print 'WARNING: unsupported class:', element.getAttribute('class') | |
854 | klass = xxxUnknown | |
855 | return klass(parent, element) | |
856 | ||
857 | # Make empty DOM element | |
858 | def MakeEmptyDOM(className): | |
859 | elem = g.tree.dom.createElement('object') | |
860 | elem.setAttribute('class', className) | |
861 | # Set required and default parameters | |
862 | xxxClass = xxxDict[className] | |
863 | defaultNotRequired = filter(lambda x, l=xxxClass.required: x not in l, | |
864 | xxxClass.default.keys()) | |
865 | for param in xxxClass.required + defaultNotRequired: | |
866 | textElem = g.tree.dom.createElement(param) | |
867 | try: | |
868 | textNode = g.tree.dom.createTextNode(xxxClass.default[param]) | |
869 | except KeyError: | |
870 | textNode = g.tree.dom.createTextNode('') | |
871 | textElem.appendChild(textNode) | |
872 | elem.appendChild(textElem) | |
873 | return elem | |
874 | ||
875 | # Make empty XXX object | |
876 | def MakeEmptyXXX(parent, className): | |
877 | # Make corresponding DOM object first | |
878 | elem = MakeEmptyDOM(className) | |
879 | # If parent is a sizer, we should create sizeritem object, except for spacers | |
880 | if parent: | |
881 | if parent.isSizer and className != 'spacer': | |
882 | sizerItemElem = MakeEmptyDOM('sizeritem') | |
883 | sizerItemElem.appendChild(elem) | |
884 | elem = sizerItemElem | |
885 | elif isinstance(parent, xxxNotebook): | |
886 | pageElem = MakeEmptyDOM('notebookpage') | |
887 | pageElem.appendChild(elem) | |
888 | elem = pageElem | |
889 | # Now just make object | |
890 | return MakeXXXFromDOM(parent, elem) | |
1fded56b | 891 |