File: drSingleChoiceDialog.py

package info (click to toggle)
drpython 1%3A3.11.4-1.1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 3,868 kB
  • ctags: 2,871
  • sloc: python: 16,653; makefile: 141; sh: 1
file content (288 lines) | stat: -rw-r--r-- 10,515 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#   Programmer: Daniel Pozmanter
#   E-mail:     drpython@bluebottle.com
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2003-2010 Daniel Pozmanter
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   DrPython is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#Single Choice Dialog (Keyboard Navigation, FindCompletion with TextCtrl Echo.)

import wx

#*******************************************************************************************************


class drSingleChoiceDialog(wx.Dialog):
    def __init__(self, parent, title, choices, sort=True, point=wx.DefaultPosition, size=(250, 300), SetSizer=True, header="", editbutton="", sortbutton=""):
        wx.Dialog.__init__(self, parent, -1, title, point, size, wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER)

        self.parent = parent

        self.ID_CHOICES = 101
        self.ID_TXT_STATIC = 102
        self.ID_TXT_CHOICE = 103

        self.ID_OK = 111
        self.ID_CANCEL = 112
        if editbutton:
            self.ID_EDIT = 113
        if sortbutton:
            self.ID_SORT = 114
            self.sortcaption = sortbutton

        #/Constants

        #Components:

        self.listChoices = wx.ListView(self, self.ID_CHOICES, (0, 0), (300, 300), style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_NO_HEADER)

        if header:
            self.txtHeader = wx.StaticText(self, self.ID_TXT_STATIC, header, (0, 0), (300, -1))
        self.txtChoice = wx.TextCtrl (self, self.ID_TXT_CHOICE, "", (0, 0), (250, -1), style=wx.TE_READONLY)

        self.origchoices = choices[:]
        self.choices = choices[:]

        self.listChoices.InsertColumn(0, "Choices")

        self.sortstate = sort
        if sort:
            self.choices.sort()
            #self.choices.sort(lambda x,y: cmp(x.lower(), y.lower())) #case insensitve

        self.setupchoices()

        self.OnSize(None)

        self.btnOk = wx.Button(self, self.ID_OK, "  &Ok  ")

        #self.btnOk.SetDefault()

        self.btnCancel = wx.Button(self, self.ID_CANCEL, "  &Cancel  ")
        if editbutton:
            self.btnEdit = wx.Button(self, self.ID_EDIT, editbutton)
        if sortbutton:
            caption = self.sortcaption
            if sort:
                caption = "  Un&sort  "
            self.btnSort = wx.Button(self, self.ID_SORT, caption)

        #/Components

        #Sizer:

        self.theSizer = wx.BoxSizer(wx.VERTICAL)

        self.textSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.textSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED | wx.ALIGN_RIGHT)
        self.textSizer.Add(self.txtChoice, 1, wx.EXPAND)
        self.textSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)

        if header:
            self.headerSizer = wx.BoxSizer(wx.HORIZONTAL)

            self.headerSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED | wx.ALIGN_RIGHT)
            self.headerSizer.Add(self.txtHeader, 1, wx.EXPAND)
            self.headerSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)


        self.listSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.listSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED | wx.ALIGN_RIGHT)
        self.listSizer.Add(self.listChoices, 1, wx.EXPAND)
        self.listSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)

        self.commandSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.commandSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED | wx.ALIGN_RIGHT)
        self.commandSizer.Add(self.btnCancel, 0, wx.SHAPED | wx.ALIGN_LEFT)
        if editbutton:
            self.commandSizer.Add(wx.StaticText(self, -1, "  "), 1, wx.SHAPED | wx.ALIGN_RIGHT)
            self.commandSizer.Add(self.btnEdit, 0, wx.SHAPED | wx.ALIGN_LEFT)
        if sortbutton:
            self.commandSizer.Add(wx.StaticText(self, -1, "  "), 1, wx.SHAPED | wx.ALIGN_RIGHT)
            self.commandSizer.Add(self.btnSort, 0, wx.SHAPED | wx.ALIGN_LEFT)
        self.commandSizer.Add(wx.StaticText(self, -1, "  "), 1, wx.EXPAND)
        self.commandSizer.Add(self.btnOk, 0, wx.SHAPED | wx.ALIGN_RIGHT)
        self.commandSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED | wx.ALIGN_RIGHT)

        self.theSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)
        if header:
            self.theSizer.Add(self.headerSizer, 0, wx.EXPAND)
            self.theSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)
        self.theSizer.Add(self.textSizer, 0, wx.EXPAND)
        self.theSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)
        self.theSizer.Add(self.listSizer, 9, wx.EXPAND)
        self.theSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)
        self.theSizer.Add(self.commandSizer, 0, wx.EXPAND)
        self.theSizer.Add(wx.StaticText(self, -1, "  "), 0, wx.SHAPED)

        self.SetAutoLayout(True)

        if SetSizer:
            self.SetSizerAndFit(self.theSizer)

        #/Sizer

        #Events:

        self.Bind(wx.EVT_BUTTON, self.OnbtnCancel, id=self.ID_CANCEL)
        self.Bind(wx.EVT_BUTTON, self.OnbtnOk, id=self.ID_OK)
        if editbutton:
            self.Bind(wx.EVT_BUTTON, self.OnbtnEdit, id=self.ID_EDIT)
        if sortbutton:
            self.Bind(wx.EVT_BUTTON, self.OnbtnSort, id=self.ID_SORT)

        self.listChoices.Bind(wx.EVT_LEFT_DCLICK, self.OnbtnOk)
        self.txtChoice.Bind(wx.EVT_CHAR, self.OnChar)

        if wx.Platform == "__WXGTK__":
            self.txtChoice.SetFocus()
        else:
            self.Bind(wx.EVT_CHAR, self.OnChar)
            self.listChoices.Bind(wx.EVT_CHAR, self.OnChar)

        self.listChoices.Bind(wx.EVT_SIZE, self.OnSize)

        #/Events

        if self.listChoices.GetItemCount() > 0:
            self.listChoices.Select(0)
            self.listChoices.Focus(0)

        self.typedchoice = ""

    def GetSelection(self):
        return self.listChoices.GetItemData(self.listChoices.GetFirstSelected())

    def GetStringSelection(self):
        return self.listChoices.GetItemText(self.listChoices.GetFirstSelected())

    def OnbtnEdit(self, event):
        if self.listChoices.GetItemCount() > 0:
            self.EndModal(wx.ID_EDIT)
        else:
            self.EndModal(wx.ID_CANCEL)

    def OnbtnSort(self, event):
        #if self.listChoices.GetItemCount() > 0:
        self.sortstate = not self.sortstate
        self.choices = self.origchoices[:]
        if self.sortstate:
            self.btnSort.SetLabel("  Un&sort  ")
            self.choices.sort()
        else:
            self.btnSort.SetLabel(self.sortcaption)
        self.setupchoices()
        self.listChoices.Select(0)
        self.listChoices.Focus(0)
        self.UpdateTypedChoice()
        self.txtChoice.SetFocus()
        #else:
        #    pass

    def OnbtnCancel(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnbtnOk(self, event):
        if self.listChoices.GetItemCount() > 0:
            self.EndModal(wx.ID_OK)
        else:
            self.EndModal(wx.ID_CANCEL)

    def OnChar(self, event):
        keycode = event.GetKeyCode()

        if keycode >= 32 and keycode <= 127:
            self.typedchoice += chr(keycode).lower()
            self.UpdateTypedChoice()
        elif keycode == wx.WXK_BACK:
            self.typedchoice = self.typedchoice[:-1]
            self.UpdateTypedChoice()

        if keycode in [wx.WXK_DOWN, wx.WXK_UP, wx.WXK_PAGEDOWN, wx.WXK_PAGEUP, wx.WXK_HOME, wx.WXK_END]:
            i = self.listChoices.GetFocusedItem()
            if keycode == wx.WXK_UP:
                i -= 1
            elif keycode == wx.WXK_DOWN:
                i += 1
            elif keycode == wx.WXK_HOME:
                i = 0
            elif keycode == wx.WXK_END:
                i = self.listChoices.GetItemCount() - 1
            elif keycode == wx.WXK_PAGEDOWN:
                i += self.listChoices.CountPerPage
            elif keycode == wx.WXK_PAGEUP:
                i -= self.listChoices.CountPerPage
            #min/max function?
            if i < 0:
                i = 0
            if i >= self.listChoices.GetItemCount():
                i = self.listChoices.GetItemCount() - 1
            #if (i < self.listChoices.GetItemCount()) and (i > -1):
            self.listChoices.Select(i)
            self.listChoices.Focus(i)
            return

        if keycode == wx.WXK_ESCAPE:
            self.OnbtnCancel(None)
        elif keycode == wx.WXK_RETURN:
            self.OnbtnOk(None)
        else:
            event.Skip()

    def OnSize(self, event):
        self.listChoices.SetColumnWidth(0, self.listChoices.GetSizeTuple()[0])
        if event is not None:
            event.Skip()

    def setupchoices(self, findstr=""):
        self.listChoices.DeleteAllItems()
        x = 0
        sofar = 0
        if findstr:
            for c in self.choices:
                a = c.lower()
                if a.find(findstr) > -1:
                    self.listChoices.InsertStringItem(sofar, c)
                    self.listChoices.SetItemData(sofar, x)
                    sofar += 1
                x += 1
        else:
            for c in self.choices:
                self.listChoices.InsertStringItem(x, c)
                self.listChoices.SetItemData(x, x)
                x += 1

    def UpdateTypedChoice(self):
        self.txtChoice.SetValue(self.typedchoice)
        if wx.Platform == "__WXGTK__":
            self.txtChoice.SetSelection(len (self.typedchoice), len(self.typedchoice))
        self.setupchoices(self.typedchoice)
        if self.listChoices.GetItemCount() > 0:
            self.listChoices.Select(0)
            self.listChoices.Focus(0)

if __name__ == "__main__":
    app = wx.App()
    w = drSingleChoiceDialog(None, "Test drSingleChoiceDialog:", ["a","b","c"], wx.CHOICEDLG_STYLE, header="Headertest") #optional headertest
    w.ShowModal()
    w.Destroy()
    app.MainLoop()