forked from wxGlade/wxGlade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogs.py
147 lines (128 loc) · 5.92 KB
/
dialogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: UTF-8 -*-
"""Dialogs to ask users during widget initialisation triggered by an graphical
user interaction.
@copyright: 2014-2016 Carsten Grohmann
@copyright: 2019-2020 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY"""
import compat
import wx
# import the raw dialogs as generated by wxGlade
from _dialogs import *
class WidgetStyleSelectionDialog(wx.Dialog):
"User dialog to select a style during widget creation"
def __init__(self, dlg_title, box_label, choices, options=None, defaults=None):
"""Initialise the dialog and draw the content
dlg_title: Dialog title
box_label: Label of the draw around the listed choices
choices: Choices to select one (string list)
options: see uses of the dialog"""
pos = wx.GetMousePosition()
wx.Dialog.__init__(self, None, -1, dlg_title, pos)
szr = wx.BoxSizer(wx.VERTICAL)
if box_label:
self.box = wx.RadioBox( self, wx.ID_ANY, box_label, wx.DefaultPosition, wx.DefaultSize,choices.split('|'),
1, style=wx.RA_SPECIFY_COLS )
self.box.SetSelection(0)
self.Bind(wx.EVT_RADIOBOX, self.on_choice)
szr.Add(self.box, 5, wx.ALL | wx.EXPAND, 10)
if options:
self._check_option_dependencies(options)
self.option_controls = []
self.option_values = []
for o, option in enumerate(self.options):
# calculate border flags
#flags = wx.LEFT | wx.RIGHT
#if not self.dependencies[o]: flags |= wx.TOP
#if not self.dependencies[o+1]: flags |= wx.BOTTOM
flags = wx.ALL
# create and add the option controls
if option[0]=="Text":
label = option[1]
hsizer = wx.BoxSizer(wx.HORIZONTAL)
hsizer.Add(wx.StaticText(self, -1, label), 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
ctrl = wx.TextCtrl(self, -1, option[1])
value = defaults and defaults[o] or ""
ctrl.SetValue(value)
hsizer.Add(ctrl, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
szr.Add(hsizer, 0, flags|wx.EXPAND, 4)
ctrl.Bind(wx.EVT_TEXT, self.callback)
elif option[0]=="Checkbox":
ctrl = wx.CheckBox(self, -1, option[1])
value = defaults and defaults[o]
ctrl.SetValue(value)
szr.Add(ctrl, 0, flags, 8)
ctrl.Bind(wx.EVT_CHECKBOX, self.callback)
elif option[0]=="Spin":
label, range_ = option[1:]
hsizer = wx.BoxSizer(wx.HORIZONTAL)
hsizer.Add(wx.StaticText(self, -1, label), 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
mi_, ma_ = range_
value = defaults and defaults[o] or 1
ctrl = wx.SpinCtrl(self, -1, str(value), min=mi_, max=ma_, initial=value)
hsizer.Add(ctrl, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
szr.Add(hsizer, 0, flags|wx.EXPAND, 4)
ctrl.Bind(wx.EVT_SPINCTRL, self.callback)
self.option_controls.append(ctrl)
self.option_values.append( value )
self.active = [True]*len(self.options)
self._activate(1)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
szr.Add(line, 0, wx.EXPAND|wx.TOP|wx.LEFT, 5)
# buttons
btnbox = wx.StdDialogButtonSizer()
btnOK = wx.Button(self, wx.ID_OK)
btnOK.SetDefault()
btnCANCEL = wx.Button(self, wx.ID_CANCEL)
btnbox.AddButton(btnOK)
btnbox.AddButton(btnCANCEL)
btnbox.Realize()
szr.Add(btnbox, 0, wx.ALL|wx.ALIGN_CENTER, 8)
self.SetAutoLayout(True)
self.SetSizer(szr)
szr.Fit(self)
def on_choice(self, event):
# this can be overridden
event.Skip()
def _check_option_dependencies(self, options):
# if an option string starts with ">", it depends on the previous option
self.dependencies = [False]*len(options)
self.options = []
for o, option in enumerate(options):
if isinstance(option, compat.basestring):
# string -> checkbox
if option.startswith(">"):
self.dependencies[o] = True
option = option[1:]
if option[0]=="?":
self.options.append( ("Text", option[1:]) )
else:
self.options.append( ("Checkbox", option) )
elif len(option)==2:
# (string, range) -> spin control
label = option[0]
if label.startswith(">"):
self.dependencies[o] = True
label = label[1:]
self.options.append( ("Spin", label, option[1]) )
self.dependencies.append(None) # dummy
def _activate(self, i):
# de-/activate controls based on the preceding options
for i in range(i, len(self.options)):
if self.dependencies[i] and (not self.active[i-1] or not self.option_values[i-1]):
activate = False
else:
activate = True
if activate!=self.active[i]:
self.option_controls[i].Enable(activate)
self.active[i] = activate
def callback(self, event):
# update values and enable / disable dependent options
ctrl = event.GetEventObject()
i = self.option_controls.index(ctrl)
self.option_values[i] = ctrl.GetValue()
self._activate(i+1)
def get_selection(self):
"Return the selected choice."
return self.box.GetStringSelection()
def get_options(self):
return self.option_values