]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/XRCed/params.py
made test window for Dialog and Wizard a child of g.frame
[wxWidgets.git] / wxPython / wx / tools / XRCed / params.py
1 # Name: params.py
2 # Purpose: Classes for parameter introduction
3 # Author: Roman Rolinsky <rolinsky@mema.ucl.ac.be>
4 # Created: 22.08.2001
5 # RCS-ID: $Id$
6
7 import string
8 import os.path
9 from globals import *
10 from types import *
11
12 genericStyles = [
13 'wxSIMPLE_BORDER', 'wxSUNKEN_BORDER', 'wxDOUBLE_BORDER',
14 'wxRAISED_BORDER', 'wxSTATIC_BORDER', 'wxNO_BORDER',
15 'wxCLIP_CHILDREN', 'wxTRANSPARENT_WINDOW', 'wxWANTS_CHARS',
16 'wxNO_FULL_REPAINT_ON_RESIZE', 'wxFULL_REPAINT_ON_RESIZE'
17 ]
18
19 genericExStyles = [
20 'wxWS_EX_VALIDATE_RECURSIVELY',
21 'wxWS_EX_BLOCK_EVENTS',
22 'wxWS_EX_TRANSIENT',
23 'wxFRAME_EX_CONTEXTHELP',
24 'wxWS_EX_PROCESS_IDLE',
25 'wxWS_EX_PROCESS_UI_UPDATES'
26 ]
27
28 buttonSize = (35,-1) # in dialog units, transformed to pixels in panel ctor
29
30 # Class that can properly disable children
31 class PPanel(wx.Panel):
32 def __init__(self, parent, name):
33 wx.Panel.__init__(self, parent, -1, name=name)
34 self.modified = self.freeze = False
35 def Enable(self, value):
36 # Something strange is going on with enable so we make sure...
37 for w in self.GetChildren():
38 w.Enable(value)
39 #wx.Panel.Enable(self, value)
40 def SetModified(self, state=True):
41 self.modified = state
42 if state: g.panel.SetModified(True)
43 # Common method to set modified state
44 def OnChange(self, evt):
45 if self.freeze: return
46 self.SetModified()
47 evt.Skip()
48
49 class ParamBinaryOr(PPanel):
50 def __init__(self, parent, name):
51 PPanel.__init__(self, parent, name)
52 self.ID_TEXT_CTRL = wx.NewId()
53 self.ID_BUTTON_CHOICES = wx.NewId()
54 sizer = wx.BoxSizer()
55 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=wx.Size(200,-1))
56 sizer.Add(self.text, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
57 self.button = wx.Button(self, self.ID_BUTTON_CHOICES, 'Edit...', size=buttonSize)
58 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
59 self.SetAutoLayout(True)
60 self.SetSizerAndFit(sizer)
61 wx.EVT_BUTTON(self, self.ID_BUTTON_CHOICES, self.OnButtonChoices)
62 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
63 def GetValue(self):
64 return self.text.GetValue()
65 def SetValue(self, value):
66 self.freeze = True
67 self.text.SetValue(value)
68 self.freeze = False
69 def OnButtonChoices(self, evt):
70 dlg = g.frame.res.LoadDialog(self, 'DIALOG_CHOICES')
71 if self.GetName() == 'flag': dlg.SetTitle('Sizer item flags')
72 elif self.GetName() == 'style': dlg.SetTitle('Window styles')
73 elif self.GetName() == 'exstyle': dlg.SetTitle('Extended window styles')
74 listBox = xrc.XRCCTRL(dlg, 'CHECKLIST')
75 listBox.InsertItems(self.values, 0)
76 value = map(string.strip, self.text.GetValue().split('|'))
77 if value == ['']: value = []
78 ignored = []
79 for i in value:
80 try:
81 listBox.Check(self.values.index(i))
82 except ValueError:
83 # Try to find equal
84 if self.equal.has_key(i):
85 listBox.Check(self.values.index(self.equal[i]))
86 else:
87 print 'WARNING: unknown flag: %s: ignored.' % i
88 ignored.append(i)
89 if dlg.ShowModal() == wx.ID_OK:
90 value = []
91 for i in range(listBox.GetCount()):
92 if listBox.IsChecked(i):
93 value.append(self.values[i])
94 # Add ignored flags
95 value.extend(ignored)
96 self.SetValue('|'.join(value))
97 self.SetModified()
98 dlg.Destroy()
99
100 class ParamFlag(ParamBinaryOr):
101 values = ['wxTOP', 'wxBOTTOM', 'wxLEFT', 'wxRIGHT', 'wxALL',
102 'wxEXPAND', 'wxGROW', 'wxSHAPED', 'wxSTRETCH_NOT',
103 'wxALIGN_CENTRE', 'wxALIGN_LEFT', 'wxALIGN_RIGHT',
104 'wxALIGN_TOP', 'wxALIGN_BOTTOM',
105 'wxALIGN_CENTRE_VERTICAL', 'wxALIGN_CENTRE_HORIZONTAL',
106 'wxADJUST_MINSIZE', 'wxFIXED_MINSIZE'
107 ]
108 equal = {'wxALIGN_CENTER': 'wxALIGN_CENTRE',
109 'wxALIGN_CENTER_VERTICAL': 'wxALIGN_CENTRE_VERTICAL',
110 'wxALIGN_CENTER_HORIZONTAL': 'wxALIGN_CENTRE_HORIZONTAL',
111 'wxUP': 'wxTOP', 'wxDOWN': 'wxBOTTOM', 'wxNORTH': 'wxTOP',
112 'wxSOUTH': 'wxBOTTOM', 'wxWEST': 'wxLEFT', 'wxEAST': 'wxRIGHT'}
113 def __init__(self, parent, name):
114 ParamBinaryOr.__init__(self, parent, name)
115
116 class ParamNonGenericStyle(ParamBinaryOr):
117 def __init__(self, parent, name):
118 self.values = g.currentXXX.winStyles
119 ParamBinaryOr.__init__(self, parent, name)
120
121 class ParamStyle(ParamBinaryOr):
122 equal = {'wxALIGN_CENTER': 'wxALIGN_CENTRE'}
123 def __init__(self, parent, name):
124 ParamBinaryOr.__init__(self, parent, name)
125 self.valuesSpecific = g.currentXXX.winStyles
126 if self.valuesSpecific: # override if using specific styles
127 # Remove duplicates
128 self.valuesGeneric = [s for s in genericStyles
129 if s not in self.valuesSpecific]
130 wx.EVT_BUTTON(self, self.ID_BUTTON_CHOICES, self.OnButtonChoicesBoth)
131 else:
132 self.values = genericStyles
133 def OnButtonChoicesBoth(self, evt):
134 dlg = g.frame.res.LoadDialog(self, 'DIALOG_STYLES')
135 listBoxSpecific = xrc.XRCCTRL(dlg, 'CHECKLIST_SPECIFIC')
136 listBoxSpecific.InsertItems(self.valuesSpecific, 0)
137 listBoxGeneric = xrc.XRCCTRL(dlg, 'CHECKLIST_GENERIC')
138 listBoxGeneric.InsertItems(self.valuesGeneric, 0)
139 value = map(string.strip, self.text.GetValue().split('|'))
140 if value == ['']: value = []
141 # Set specific styles
142 value2 = [] # collect generic and ignored here
143 for i in value:
144 try:
145 listBoxSpecific.Check(self.valuesSpecific.index(i))
146 except ValueError:
147 # Try to find equal
148 if self.equal.has_key(i):
149 listBoxSpecific.Check(self.valuesSpecific.index(self.equal[i]))
150 else:
151 value2.append(i)
152 ignored = []
153 # Set generic styles, collect non-standart values
154 for i in value2:
155 try:
156 listBoxGeneric.Check(self.valuesGeneric.index(i))
157 except ValueError:
158 # Try to find equal
159 if self.equal.has_key(i):
160 listBoxGeneric.Check(self.valuesGeneric.index(self.equal[i]))
161 else:
162 print 'WARNING: unknown flag: %s: ignored.' % i
163 ignored.append(i)
164 if dlg.ShowModal() == wx.ID_OK:
165 value = [self.valuesSpecific[i]
166 for i in range(listBoxSpecific.GetCount())
167 if listBoxSpecific.IsChecked(i)] + \
168 [self.valuesGeneric[i]
169 for i in range(listBoxGeneric.GetCount())
170 if listBoxGeneric.IsChecked(i)] + ignored
171 self.SetValue('|'.join(value))
172 self.SetModified()
173 dlg.Destroy()
174
175 class ParamExStyle(ParamBinaryOr):
176 def __init__(self, parent, name):
177 if g.currentXXX:
178 self.values = g.currentXXX.exStyles + genericExStyles
179 else:
180 self.values = []
181 ParamBinaryOr.__init__(self, parent, name)
182
183 class ParamColour(PPanel):
184 def __init__(self, parent, name):
185 PPanel.__init__(self, parent, name)
186 self.ID_TEXT_CTRL = wx.NewId()
187 self.ID_BUTTON = wx.NewId()
188 sizer = wx.BoxSizer()
189 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=(80,-1))
190 sizer.Add(self.text, 0, wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM, 2)
191 self.button = wx.Panel(self, self.ID_BUTTON, wx.DefaultPosition, wx.Size(20, 20))
192 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 5)
193 self.SetAutoLayout(True)
194 self.SetSizerAndFit(sizer)
195 self.textModified = False
196 wx.EVT_PAINT(self.button, self.OnPaintButton)
197 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
198 wx.EVT_LEFT_DOWN(self.button, self.OnLeftDown)
199 def GetValue(self):
200 return self.text.GetValue()
201 def SetValue(self, value):
202 self.freeze = True
203 if not value: value = '#FFFFFF'
204 self.text.SetValue(str(value)) # update text ctrl
205 try:
206 colour = wx.Colour(int(value[1:3], 16), int(value[3:5], 16), int(value[5:7], 16))
207 self.button.SetBackgroundColour(colour)
208 except: # ignore errors
209 pass
210 self.button.Refresh()
211 self.freeze = False
212 def OnPaintButton(self, evt):
213 dc = wx.PaintDC(self.button)
214 dc.SetBrush(wx.TRANSPARENT_BRUSH)
215 if self.IsEnabled(): dc.SetPen(wx.BLACK_PEN)
216 else: dc.SetPen(wx.GREY_PEN)
217 size = self.button.GetSize()
218 dc.DrawRectangle(0, 0, size.width, size.height)
219 def OnLeftDown(self, evt):
220 data = wx.ColourData()
221 data.SetColour(self.GetValue())
222 dlg = wx.ColourDialog(self, data)
223 if dlg.ShowModal() == wx.ID_OK:
224 self.SetValue('#%02X%02X%02X' % dlg.GetColourData().GetColour().Get())
225 self.SetModified()
226 dlg.Destroy()
227
228 ################################################################################
229
230 # Mapping from wx constants to XML strings
231 fontFamiliesWx2Xml = {wx.DEFAULT: 'default', wx.DECORATIVE: 'decorative',
232 wx.ROMAN: 'roman', wx.SCRIPT: 'script', wx.SWISS: 'swiss',
233 wx.MODERN: 'modern'}
234 fontStylesWx2Xml = {wx.NORMAL: 'normal', wx.SLANT: 'slant', wx.ITALIC: 'italic'}
235 fontWeightsWx2Xml = {wx.NORMAL: 'normal', wx.LIGHT: 'light', wx.BOLD: 'bold'}
236 def ReverseMap(m):
237 rm = {}
238 for k,v in m.items(): rm[v] = k
239 return rm
240 fontFamiliesXml2wx = ReverseMap(fontFamiliesWx2Xml)
241 fontStylesXml2wx = ReverseMap(fontStylesWx2Xml)
242 fontWeightsXml2wx = ReverseMap(fontWeightsWx2Xml)
243
244 class ParamFont(PPanel):
245 def __init__(self, parent, name):
246 PPanel.__init__(self, parent, name)
247 self.ID_TEXT_CTRL = wx.NewId()
248 self.ID_BUTTON_SELECT = wx.NewId()
249 sizer = wx.BoxSizer()
250 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=(200,-1))
251 sizer.Add(self.text, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
252 self.button = wx.Button(self, self.ID_BUTTON_SELECT, 'Select...', size=buttonSize)
253 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
254 self.SetAutoLayout(True)
255 self.SetSizerAndFit(sizer)
256 self.textModified = False
257 wx.EVT_BUTTON(self, self.ID_BUTTON_SELECT, self.OnButtonSelect)
258 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
259 def OnChange(self, evt):
260 PPanel.OnChange(self, evt)
261 self.textModified = True
262 def _defaultValue(self):
263 return [`g._sysFont.GetPointSize()`, 'default', 'normal', 'normal', '0', '', '']
264 def GetValue(self):
265 if self.textModified: # text has newer value
266 try:
267 return eval(self.text.GetValue())
268 except SyntaxError:
269 wx.LogError('Syntax error in parameter value: ' + self.GetName())
270 return self._defaultValue()
271 return self.value
272 def SetValue(self, value):
273 self.freeze = True # disable other handlers
274 if not value: value = self._defaultValue()
275 self.value = value
276 self.text.SetValue(str(value)) # update text ctrl
277 self.freeze = False
278 def OnButtonSelect(self, evt):
279 if self.textModified: # text has newer value
280 try:
281 self.value = eval(self.text.GetValue())
282 except SyntaxError:
283 wx.LogError('Syntax error in parameter value: ' + self.GetName())
284 self.value = self._defaultValue()
285 # Make initial font
286 # Default values
287 size = g._sysFont.GetPointSize()
288 family = wx.DEFAULT
289 style = weight = wx.NORMAL
290 underlined = 0
291 face = ''
292 enc = wx.FONTENCODING_DEFAULT
293 # Fall back to default if exceptions
294 error = False
295 try:
296 try: size = int(self.value[0])
297 except ValueError: error = True; wx.LogError('Invalid size specification')
298 try: family = fontFamiliesXml2wx[self.value[1]]
299 except KeyError: error = True; wx.LogError('Invalid family specification')
300 try: style = fontStylesXml2wx[self.value[2]]
301 except KeyError: error = True; wx.LogError('Invalid style specification')
302 try: weight = fontWeightsXml2wx[self.value[3]]
303 except KeyError: error = True; wx.LogError('Invalid weight specification')
304 try: underlined = bool(self.value[4])
305 except ValueError: error = True; wx.LogError('Invalid underlined flag specification')
306 face = self.value[5]
307 except IndexError:
308 error = True
309 mapper = wx.FontMapper()
310 if not self.value[6]: enc = mapper.CharsetToEncoding(self.value[6])
311
312 if error: wx.LogError('Invalid font specification')
313 if enc == wx.FONTENCODING_DEFAULT: enc = wx.FONTENCODING_SYSTEM
314 font = wx.Font(size, family, style, weight, underlined, face, enc)
315 data = wx.FontData()
316 data.SetInitialFont(font)
317 dlg = wx.FontDialog(self, data)
318 if dlg.ShowModal() == wx.ID_OK:
319 font = dlg.GetFontData().GetChosenFont()
320 if font.GetEncoding() == wx.FONTENCODING_SYSTEM:
321 encName = ''
322 else:
323 encName = wx.FontMapper.GetEncodingName(font.GetEncoding()).encode()
324 value = [str(font.GetPointSize()),
325 fontFamiliesWx2Xml.get(font.GetFamily(), "default"),
326 fontStylesWx2Xml.get(font.GetStyle(), "normal"),
327 fontWeightsWx2Xml.get(font.GetWeight(), "normal"),
328 str(int(font.GetUnderlined())),
329 font.GetFaceName().encode(),
330 encName
331 ]
332 self.SetValue(value)
333 self.SetModified()
334 self.textModified = False
335 dlg.Destroy()
336
337 ################################################################################
338
339 class ParamInt(PPanel):
340 def __init__(self, parent, name):
341 PPanel.__init__(self, parent, name)
342 self.ID_SPIN_CTRL = wx.NewId()
343 sizer = wx.BoxSizer()
344 self.spin = wx.SpinCtrl(self, self.ID_SPIN_CTRL, size=(60,-1))
345 self.spin.SetRange(-2147483648, 2147483647) # min/max integers
346 sizer.Add(self.spin)
347 self.SetAutoLayout(True)
348 self.SetSizerAndFit(sizer)
349 wx.EVT_SPINCTRL(self, self.ID_SPIN_CTRL, self.OnChange)
350 def GetValue(self):
351 return str(self.spin.GetValue())
352 def SetValue(self, value):
353 self.freeze = True
354 if not value: value = 0
355 self.spin.SetValue(int(value))
356 self.freeze = False
357
358 # Non-negative number
359 class ParamIntNN(PPanel):
360 def __init__(self, parent, name):
361 PPanel.__init__(self, parent, name)
362 self.ID_SPIN_CTRL = wx.NewId()
363 sizer = wx.BoxSizer()
364 self.spin = wx.SpinCtrl(self, self.ID_SPIN_CTRL, size=(60,-1))
365 self.spin.SetRange(0, 10000) # min/max integers
366 sizer.Add(self.spin)
367 self.SetAutoLayout(True)
368 self.SetSizerAndFit(sizer)
369 wx.EVT_SPINCTRL(self, self.ID_SPIN_CTRL, self.OnChange)
370 def GetValue(self):
371 return str(self.spin.GetValue())
372 def SetValue(self, value):
373 self.freeze = True
374 if not value: value = 0
375 self.spin.SetValue(int(value))
376 self.freeze = False
377
378 # Same as int but allows dialog units (XXXd)
379 class ParamUnit(PPanel):
380 def __init__(self, parent, name):
381 PPanel.__init__(self, parent, name)
382 self.ID_TEXT_CTRL = wx.NewId()
383 self.ID_SPIN_BUTTON = wx.NewId()
384 sizer = wx.BoxSizer(wx.HORIZONTAL)
385 self.spin = wx.SpinButton(self, self.ID_SPIN_BUTTON, style = wx.SP_VERTICAL, size=(-1,0))
386 textW = 60 - self.spin.GetSize()[0]
387 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=(textW,-1))
388 self.spin.SetRange(-10000, 10000)
389 sizer.Add(self.text, 0, wx.EXPAND)
390 sizer.Add(self.spin, 0, wx.EXPAND)
391 self.SetAutoLayout(True)
392 self.SetSizerAndFit(sizer)
393 self.spin.Bind(wx.EVT_SPIN_UP, self.OnSpinUp)
394 self.spin.Bind(wx.EVT_SPIN_DOWN, self.OnSpinDown)
395 def GetValue(self):
396 return self.text.GetValue()
397 def SetValue(self, value):
398 if not value: value = '0'
399 self.text.SetValue(value)
400 self.Change(0)
401 def Change(self, x):
402 self.freeze = True
403 # Check if we are working with dialog units
404 value = self.text.GetValue()
405 units = ''
406 if value[-1].upper() == 'D':
407 units = value[-1]
408 value = value[:-1]
409 try:
410 intValue = int(value) + x
411 self.spin.SetValue(intValue)
412 if x: # 0 can be passed to update spin value only
413 self.text.SetValue(str(intValue) + units)
414 self.SetModified()
415 except:
416 # !!! Strange, if I use wx.LogWarning, event is re-generated
417 print 'ERROR: incorrect unit format'
418 self.freeze = False
419 def OnSpinUp(self, evt):
420 self.freeze = True
421 self.Change(1)
422 def OnSpinDown(self, evt):
423 if self.freeze: return
424 self.freeze = True
425 self.Change(-1)
426
427 class ParamMultilineText(PPanel):
428 def __init__(self, parent, name, textWidth=-1):
429 PPanel.__init__(self, parent, name)
430 self.ID_TEXT_CTRL = wx.NewId()
431 self.ID_BUTTON_EDIT = wx.NewId()
432 sizer = wx.BoxSizer()
433 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=wx.Size(200,-1))
434 sizer.Add(self.text, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
435 self.button = wx.Button(self, self.ID_BUTTON_EDIT, 'Edit...', size=buttonSize)
436 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
437 self.SetAutoLayout(True)
438 self.SetSizerAndFit(sizer)
439 wx.EVT_BUTTON(self, self.ID_BUTTON_EDIT, self.OnButtonEdit)
440 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
441 def GetValue(self):
442 return self.text.GetValue()
443 def SetValue(self, value):
444 self.freeze = True # disable other handlers
445 self.text.SetValue(value)
446 self.freeze = False # disable other handlers
447 def OnButtonEdit(self, evt):
448 dlg = g.frame.res.LoadDialog(self, 'DIALOG_TEXT')
449 textCtrl = xrc.XRCCTRL(dlg, 'TEXT')
450 textCtrl.SetValue(self.text.GetValue())
451 if dlg.ShowModal() == wx.ID_OK:
452 self.text.SetValue(textCtrl.GetValue())
453 self.SetModified()
454 dlg.Destroy()
455
456 class ParamText(PPanel):
457 def __init__(self, parent, name, textWidth=-1):
458 PPanel.__init__(self, parent, name)
459 self.ID_TEXT_CTRL = wx.NewId()
460 # We use sizer even here to have the same size of text control
461 sizer = wx.BoxSizer()
462 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=wx.Size(textWidth,-1))
463 if textWidth == -1: option = 1
464 else: option = 0
465 sizer.Add(self.text, option, wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM, 2)
466 self.SetAutoLayout(True)
467 self.SetSizerAndFit(sizer)
468 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
469 def GetValue(self):
470 return self.text.GetValue()
471 def SetValue(self, value):
472 self.freeze = True # disable other handlers
473 self.text.SetValue(value)
474 self.freeze = False # disable other handlers
475
476 class ParamAccel(ParamText):
477 def __init__(self, parent, name):
478 ParamText.__init__(self, parent, name, 100)
479
480 class ParamPosSize(ParamText):
481 def __init__(self, parent, name):
482 ParamText.__init__(self, parent, name, 80)
483
484 class ParamLabel(ParamText):
485 def __init__(self, parent, name):
486 ParamText.__init__(self, parent, name, 200)
487
488 class ParamEncoding(ParamText):
489 def __init__(self, parent, name):
490 ParamText.__init__(self, parent, name, 100)
491
492 class ContentDialog(wx.Dialog):
493 def __init__(self, parent, value):
494 # Load from resource
495 pre = wx.PreDialog()
496 g.frame.res.LoadOnDialog(pre, parent, 'DIALOG_CONTENT')
497 self.PostCreate(pre)
498 self.list = xrc.XRCCTRL(self, 'LIST')
499 # Set list items
500 for v in value:
501 self.list.Append(v)
502 self.SetAutoLayout(True)
503 self.GetSizer().Fit(self)
504 # Callbacks
505 self.ID_BUTTON_APPEND = xrc.XRCID('BUTTON_APPEND')
506 self.ID_BUTTON_REMOVE = xrc.XRCID('BUTTON_REMOVE')
507 self.ID_BUTTON_UP = xrc.XRCID('BUTTON_UP')
508 self.ID_BUTTON_DOWN = xrc.XRCID('BUTTON_DOWN')
509 wx.EVT_BUTTON(self, self.ID_BUTTON_UP, self.OnButtonUp)
510 wx.EVT_BUTTON(self, self.ID_BUTTON_DOWN, self.OnButtonDown)
511 wx.EVT_BUTTON(self, self.ID_BUTTON_APPEND, self.OnButtonAppend)
512 wx.EVT_BUTTON(self, self.ID_BUTTON_REMOVE, self.OnButtonRemove)
513 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_UP, self.OnUpdateUI)
514 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_DOWN, self.OnUpdateUI)
515 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_REMOVE, self.OnUpdateUI)
516 def OnButtonUp(self, evt):
517 i = self.list.GetSelection()
518 str = self.list.GetString(i)
519 self.list.Delete(i)
520 self.list.InsertItems([str], i-1)
521 self.list.SetSelection(i-1)
522 def OnButtonDown(self, evt):
523 i = self.list.GetSelection()
524 str = self.list.GetString(i)
525 self.list.Delete(i)
526 self.list.InsertItems([str], i+1)
527 self.list.SetSelection(i+1)
528 def OnButtonAppend(self, evt):
529 str = wx.GetTextFromUser('Enter new item:', 'Append', '', self)
530 self.list.Append(str)
531 def OnButtonRemove(self, evt):
532 self.list.Delete(self.list.GetSelection())
533 def OnUpdateUI(self, evt):
534 if evt.GetId() == self.ID_BUTTON_REMOVE:
535 evt.Enable(self.list.GetSelection() != -1)
536 elif evt.GetId() == self.ID_BUTTON_UP:
537 evt.Enable(self.list.GetSelection() > 0)
538 elif evt.GetId() == self.ID_BUTTON_DOWN:
539 evt.Enable(self.list.GetSelection() != -1 and \
540 self.list.GetSelection() < self.list.GetCount() - 1)
541
542 class ContentCheckListDialog(wx.Dialog):
543 def __init__(self, parent, value):
544 pre = wx.PreDialog()
545 g.frame.res.LoadOnDialog(pre, parent, 'DIALOG_CONTENT_CHECKLIST')
546 self.PostCreate(pre)
547 self.list = xrc.XRCCTRL(self, 'CHECK_LIST')
548 # Set list items
549 i = 0
550 for v,ch in value:
551 self.list.Append(v)
552 self.list.Check(i, ch)
553 i += 1
554 self.SetAutoLayout(True)
555 self.GetSizer().Fit(self)
556 # Callbacks
557 self.ID_BUTTON_APPEND = xrc.XRCID('BUTTON_APPEND')
558 self.ID_BUTTON_REMOVE = xrc.XRCID('BUTTON_REMOVE')
559 self.ID_BUTTON_UP = xrc.XRCID('BUTTON_UP')
560 self.ID_BUTTON_DOWN = xrc.XRCID('BUTTON_DOWN')
561 wx.EVT_CHECKLISTBOX(self, self.list.GetId(), self.OnCheck)
562 wx.EVT_BUTTON(self, self.ID_BUTTON_UP, self.OnButtonUp)
563 wx.EVT_BUTTON(self, self.ID_BUTTON_DOWN, self.OnButtonDown)
564 wx.EVT_BUTTON(self, self.ID_BUTTON_APPEND, self.OnButtonAppend)
565 wx.EVT_BUTTON(self, self.ID_BUTTON_REMOVE, self.OnButtonRemove)
566 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_UP, self.OnUpdateUI)
567 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_DOWN, self.OnUpdateUI)
568 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_REMOVE, self.OnUpdateUI)
569 def OnCheck(self, evt):
570 # !!! Wrong wxGTK (wxMSW?) behavior: toggling selection if checking
571 self.list.Deselect(evt.GetSelection())
572 def OnButtonUp(self, evt):
573 i = self.list.GetSelection()
574 str, ch = self.list.GetString(i), self.list.IsChecked(i)
575 self.list.Delete(i)
576 self.list.InsertItems([str], i-1)
577 self.list.Check(i-1, ch)
578 self.list.SetSelection(i-1)
579 def OnButtonDown(self, evt):
580 i = self.list.GetSelection()
581 str, ch = self.list.GetString(i), self.list.IsChecked(i)
582 self.list.Delete(i)
583 self.list.InsertItems([str], i+1)
584 self.list.Check(i+1, ch)
585 self.list.SetSelection(i+1)
586 def OnButtonAppend(self, evt):
587 str = wx.GetTextFromUser('Enter new item:', 'Append', '', self)
588 self.list.Append(str)
589 def OnButtonRemove(self, evt):
590 self.list.Delete(self.list.GetSelection())
591 def OnUpdateUI(self, evt):
592 if evt.GetId() == self.ID_BUTTON_REMOVE:
593 evt.Enable(self.list.GetSelection() != -1)
594 elif evt.GetId() == self.ID_BUTTON_UP:
595 evt.Enable(self.list.GetSelection() > 0)
596 elif evt.GetId() == self.ID_BUTTON_DOWN:
597 evt.Enable(self.list.GetSelection() != -1 and \
598 self.list.GetSelection() < self.list.GetCount() - 1)
599
600 class ParamContent(PPanel):
601 def __init__(self, parent, name):
602 PPanel.__init__(self, parent, name)
603 self.ID_TEXT_CTRL = wx.NewId()
604 self.ID_BUTTON_EDIT = wx.NewId()
605 sizer = wx.BoxSizer()
606 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=wx.Size(200,-1))
607 sizer.Add(self.text, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
608 self.button = wx.Button(self, self.ID_BUTTON_EDIT, 'Edit...', size=buttonSize)
609 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
610 self.SetAutoLayout(True)
611 self.SetSizerAndFit(sizer)
612 self.textModified = False
613 wx.EVT_BUTTON(self, self.ID_BUTTON_EDIT, self.OnButtonEdit)
614 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
615 def OnChange(self, evt):
616 PPanel.OnChange(self, evt)
617 self.textModified = True
618 def GetValue(self):
619 if self.textModified: # text has newer value
620 try:
621 return self.text.GetValue().split('|')
622 except ValueError:
623 return []
624 return self.value
625 def SetValue(self, value):
626 self.freeze = True
627 if not value: value = []
628 self.value = value
629 repr_ = '|'.join(map(str, value))
630 self.text.SetValue(repr_) # update text ctrl
631 self.freeze = False
632 def OnButtonEdit(self, evt):
633 if self.textModified: # text has newer value
634 self.value = self.GetValue()
635 dlg = ContentDialog(self, self.value)
636 if dlg.ShowModal() == wx.ID_OK:
637 value = []
638 for i in range(dlg.list.GetCount()):
639 value.append(dlg.list.GetString(i))
640 self.SetValue(value)
641 self.SetModified()
642 self.textModified = False
643 dlg.Destroy()
644 def SetModified(self, state=True):
645 PPanel.SetModified(self, state)
646 self.textModified = False
647
648 # CheckList content
649 class ParamContentCheckList(ParamContent):
650 def __init__(self, parent, name):
651 ParamContent.__init__(self, parent, name)
652 def OnButtonEdit(self, evt):
653 if self.textModified: # text has newer value
654 self.value = self.GetValue()
655 dlg = ContentCheckListDialog(self, self.value)
656 if dlg.ShowModal() == wx.ID_OK:
657 value = []
658 for i in range(dlg.list.GetCount()):
659 value.append((dlg.list.GetString(i), int(dlg.list.IsChecked(i))))
660 self.SetValue(value)
661 self.SetModified()
662 self.textModified = False
663 dlg.Destroy()
664 def SetValue(self, value):
665 self.freeze = True
666 if not value: value = []
667 self.value = value
668 repr_ = '|'.join(map(str,value))
669 self.text.SetValue(repr_) # update text ctrl
670 self.freeze = False
671
672 class IntListDialog(wx.Dialog):
673 def __init__(self, parent, value):
674 pre = wx.PreDialog()
675 g.frame.res.LoadOnDialog(pre, parent, 'DIALOG_INTLIST')
676 self.PostCreate(pre)
677 self.list = xrc.XRCCTRL(self, 'LIST')
678 # Set list items
679 value.sort()
680 for v in value:
681 if type(v) != IntType:
682 wx.LogError('Invalid item type')
683 else:
684 self.list.Append(str(v))
685 self.SetAutoLayout(True)
686 self.GetSizer().Fit(self)
687 # Callbacks
688 self.spinCtrl = xrc.XRCCTRL(self, 'SPIN')
689 wx.EVT_BUTTON(self, xrc.XRCID('BUTTON_ADD'), self.OnButtonAdd)
690 self.ID_BUTTON_REMOVE = xrc.XRCID('BUTTON_REMOVE')
691 wx.EVT_BUTTON(self, self.ID_BUTTON_REMOVE, self.OnButtonRemove)
692 wx.EVT_BUTTON(self, xrc.XRCID('BUTTON_CLEAR'), self.OnButtonClear)
693 wx.EVT_UPDATE_UI(self, self.ID_BUTTON_REMOVE, self.OnUpdateUI)
694 def OnButtonAdd(self, evt):
695 # Check that it's unique
696 try:
697 v = self.spinCtrl.GetValue()
698 s = str(v) # to be sure
699 i = self.list.FindString(s)
700 if i == -1: # ignore non-unique
701 # Find place to insert
702 found = False
703 for i in range(self.list.GetCount()):
704 if int(self.list.GetString(i)) > v:
705 found = True
706 break
707 if found: self.list.InsertItems([s], i)
708 else: self.list.Append(s)
709 except ValueError:
710 wx.LogError('List item is not an int!')
711 def OnButtonRemove(self, evt):
712 self.list.Delete(self.list.GetSelection())
713 def OnButtonClear(self, evt):
714 self.list.Clear()
715 def OnUpdateUI(self, evt):
716 if evt.GetId() == self.ID_BUTTON_REMOVE:
717 evt.Enable(self.list.GetSelection() != -1)
718
719 # For growable list
720 class ParamIntList(ParamContent):
721 def __init__(self, parent, name):
722 ParamContent.__init__(self, parent, name)
723 def OnButtonEdit(self, evt):
724 if self.textModified: # text has newer value
725 try:
726 self.value = map(int, self.text.GetValue().split('|'))
727 except ValueError:
728 self.value = []
729 dlg = IntListDialog(self, self.value)
730 if dlg.ShowModal() == wx.ID_OK:
731 value = []
732 for i in range(dlg.list.GetCount()):
733 value.append(int(dlg.list.GetString(i)))
734 self.SetValue(value)
735 self.SetModified()
736 self.textModified = False
737 dlg.Destroy()
738
739 # Boxless radiobox
740 class RadioBox(PPanel):
741 def __init__(self, parent, id, choices,
742 pos=wx.DefaultPosition, name='radiobox'):
743 PPanel.__init__(self, parent, name)
744 self.choices = choices
745 topSizer = wx.BoxSizer()
746 for i in choices:
747 button = wx.RadioButton(self, -1, i, size=(-1,buttonSize[1]), name=i)
748 topSizer.Add(button, 0, wx.RIGHT, 5)
749 wx.EVT_RADIOBUTTON(self, button.GetId(), self.OnRadioChoice)
750 self.SetAutoLayout(True)
751 self.SetSizerAndFit(topSizer)
752 def SetStringSelection(self, value):
753 self.freeze = True
754 for i in self.choices:
755 self.FindWindowByName(i).SetValue(i == value)
756 self.value = value
757 self.freeze = False
758 def OnRadioChoice(self, evt):
759 if self.freeze: return
760 if evt.GetSelection():
761 self.value = evt.GetEventObject().GetName()
762 self.SetModified()
763 def GetStringSelection(self):
764 return self.value
765
766 class ParamBool(RadioBox):
767 values = {'yes': '1', 'no': '0'}
768 seulav = {'1': 'yes', '0': 'no'}
769 def __init__(self, parent, name):
770 RadioBox.__init__(self, parent, -1, choices=self.values.keys(), name=name)
771 def GetValue(self):
772 return self.values[self.GetStringSelection()]
773 def SetValue(self, value):
774 if not value: value = '1'
775 self.SetStringSelection(self.seulav[value])
776
777 class ParamOrient(RadioBox):
778 values = {'horizontal': 'wxHORIZONTAL', 'vertical': 'wxVERTICAL'}
779 seulav = {'wxHORIZONTAL': 'horizontal', 'wxVERTICAL': 'vertical'}
780 def __init__(self, parent, name):
781 RadioBox.__init__(self, parent, -1, choices=self.values.keys(), name=name)
782 def GetValue(self):
783 return self.values[self.GetStringSelection()]
784 def SetValue(self, value):
785 if not value: value = 'wxHORIZONTAL'
786 self.SetStringSelection(self.seulav[value])
787
788 class ParamOrientation(RadioBox):
789 values = {'horizontal': 'horizontal', 'vertical': 'vertical'}
790 seulav = {'horizontal': 'horizontal', 'vertical': 'vertical'}
791 def __init__(self, parent, name):
792 RadioBox.__init__(self, parent, -1, choices=self.values.keys(), name=name)
793 def GetValue(self):
794 return self.values[self.GetStringSelection()]
795 def SetValue(self, value):
796 if not value: value = 'vertical'
797 self.SetStringSelection(self.seulav[value])
798
799 class ParamFile(PPanel):
800 def __init__(self, parent, name):
801 PPanel.__init__(self, parent, name)
802 self.ID_TEXT_CTRL = wx.NewId()
803 self.ID_BUTTON_BROWSE = wx.NewId()
804 sizer = wx.BoxSizer()
805 self.text = wx.TextCtrl(self, self.ID_TEXT_CTRL, size=wx.Size(200,-1))
806 sizer.Add(self.text, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
807 self.button = wx.Button(self, self.ID_BUTTON_BROWSE, 'Browse...',size=buttonSize)
808 sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
809 self.SetAutoLayout(True)
810 self.SetSizerAndFit(sizer)
811 self.textModified = False
812 wx.EVT_BUTTON(self, self.ID_BUTTON_BROWSE, self.OnButtonBrowse)
813 wx.EVT_TEXT(self, self.ID_TEXT_CTRL, self.OnChange)
814 def OnChange(self, evt):
815 PPanel.OnChange(self, evt)
816 self.textModified = True
817 def GetValue(self):
818 if self.textModified: # text has newer value
819 return self.text.GetValue()
820 return self.value
821 def SetValue(self, value):
822 self.freeze = True
823 self.value = value
824 self.text.SetValue(value) # update text ctrl
825 self.freeze = False
826 def OnButtonBrowse(self, evt):
827 if self.textModified: # text has newer value
828 self.value = self.text.GetValue()
829 dlg = wx.FileDialog(self,
830 defaultDir = os.path.abspath(os.path.dirname(self.value)),
831 defaultFile = os.path.basename(self.value))
832 if dlg.ShowModal() == wx.ID_OK:
833 # Get common part of selected path and current
834 if g.frame.dataFile:
835 curpath = os.path.abspath(g.frame.dataFile)
836 else:
837 curpath = os.path.join(os.getcwd(), '')
838 common = os.path.commonprefix([curpath, dlg.GetPath()])
839 self.SetValue(dlg.GetPath()[len(common):])
840 self.SetModified()
841 self.textModified = False
842 dlg.Destroy()
843
844 class ParamBitmap(PPanel):
845 def __init__(self, parent, name):
846 pre = wx.PrePanel()
847 g.frame.res.LoadOnPanel(pre, parent, 'PANEL_BITMAP')
848 self.PostCreate(pre)
849 self.modified = self.freeze = False
850 self.radio_std = xrc.XRCCTRL(self, 'RADIO_STD')
851 self.radio_file = xrc.XRCCTRL(self, 'RADIO_FILE')
852 self.combo = xrc.XRCCTRL(self, 'COMBO_STD')
853 self.text = xrc.XRCCTRL(self, 'TEXT_FILE')
854 self.button = xrc.XRCCTRL(self, 'BUTTON_BROWSE')
855 self.textModified = False
856 self.SetAutoLayout(True)
857 self.GetSizer().SetMinSize((260, -1))
858 self.GetSizer().Fit(self)
859 wx.EVT_RADIOBUTTON(self, xrc.XRCID('RADIO_STD'), self.OnRadioStd)
860 wx.EVT_RADIOBUTTON(self, xrc.XRCID('RADIO_FILE'), self.OnRadioFile)
861 wx.EVT_BUTTON(self, xrc.XRCID('BUTTON_BROWSE'), self.OnButtonBrowse)
862 wx.EVT_COMBOBOX(self, xrc.XRCID('COMBO_STD'), self.OnCombo)
863 wx.EVT_TEXT(self, xrc.XRCID('COMBO_STD'), self.OnChange)
864 wx.EVT_TEXT(self, xrc.XRCID('TEXT_FILE'), self.OnChange)
865 def OnRadioStd(self, evt):
866 self.SetModified()
867 self.SetValue(['wxART_MISSING_IMAGE',''])
868 def OnRadioFile(self, evt):
869 self.SetModified()
870 self.SetValue(['',''])
871 def updateRadios(self):
872 if self.value[0]:
873 self.radio_std.SetValue(True)
874 self.radio_file.SetValue(False)
875 self.text.Enable(False)
876 self.button.Enable(False)
877 self.combo.Enable(True)
878 else:
879 self.radio_std.SetValue(False)
880 self.radio_file.SetValue(True)
881 self.text.Enable(True)
882 self.button.Enable(True)
883 self.combo.Enable(False)
884 def OnChange(self, evt):
885 PPanel.OnChange(self, evt)
886 self.textModified = True
887 def OnCombo(self, evt):
888 PPanel.OnChange(self, evt)
889 self.value[0] = self.combo.GetValue()
890 def GetValue(self):
891 if self.textModified: # text has newer value
892 return [self.combo.GetValue(), self.text.GetValue()]
893 return self.value
894 def SetValue(self, value):
895 self.freeze = True
896 if not value:
897 self.value = ['', '']
898 else:
899 self.value = value
900 self.combo.SetValue(self.value[0])
901 self.text.SetValue(self.value[1]) # update text ctrl
902 self.updateRadios()
903 self.freeze = False
904 def OnButtonBrowse(self, evt):
905 if self.textModified: # text has newer value
906 self.value[1] = self.text.GetValue()
907 dlg = wx.FileDialog(self,
908 defaultDir = os.path.abspath(os.path.dirname(self.value[1])),
909 defaultFile = os.path.basename(self.value[1]))
910 if dlg.ShowModal() == wx.ID_OK:
911 # Get common part of selected path and current
912 if g.frame.dataFile:
913 curpath = os.path.abspath(g.frame.dataFile)
914 else:
915 curpath = os.path.join(os.getcwd(), '')
916 common = os.path.commonprefix([curpath, dlg.GetPath()])
917 self.SetValue(['', dlg.GetPath()[len(common):]])
918 self.SetModified()
919 self.textModified = False
920 dlg.Destroy()
921
922 paramDict = {
923 'flag': ParamFlag,
924 'style': ParamStyle, 'exstyle': ParamExStyle,
925 'pos': ParamPosSize, 'size': ParamPosSize,
926 'cellpos': ParamPosSize, 'cellspan': ParamPosSize,
927 'border': ParamUnit, 'cols': ParamIntNN, 'rows': ParamIntNN,
928 'vgap': ParamUnit, 'hgap': ParamUnit,
929 'checkable': ParamBool, 'checked': ParamBool, 'radio': ParamBool,
930 'accel': ParamAccel,
931 'label': ParamMultilineText, 'title': ParamText, 'value': ParamText,
932 'content': ParamContent, 'selection': ParamIntNN,
933 'min': ParamInt, 'max': ParamInt,
934 'fg': ParamColour, 'bg': ParamColour, 'font': ParamFont,
935 'enabled': ParamBool, 'focused': ParamBool, 'hidden': ParamBool,
936 'tooltip': ParamText, 'bitmap': ParamBitmap, 'icon': ParamBitmap,
937 'encoding': ParamEncoding, 'borders': ParamUnit
938 }