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