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