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