]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | #---------------------------------------------------------------------- |
a5deb2fb RD |
2 | # Name: wx.lib.dialogs |
3 | # Purpose: ScrolledMessageDialog, MultipleChoiceDialog and | |
d14a1e28 RD |
4 | # function wrappers for the common dialogs by Kevin Altis. |
5 | # | |
6 | # Author: Various | |
7 | # | |
8 | # Created: 3-January-2002 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 2002 by Total Control Software | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------- | |
b881fc78 RD |
13 | # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
14 | # | |
15 | # o Updated for 2.5 compatability. | |
16 | # | |
33785d9f RD |
17 | # 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
18 | # | |
19 | # o wxScrolledMessageDialog -> ScrolledMessageDialog | |
20 | # o wxMultipleChoiceDialog -> MultipleChoiceDialog | |
21 | # | |
1fded56b | 22 | |
b881fc78 RD |
23 | import wx |
24 | import layoutf | |
1fded56b | 25 | |
d14a1e28 RD |
26 | #---------------------------------------------------------------------- |
27 | ||
33785d9f | 28 | class ScrolledMessageDialog(wx.Dialog): |
f50544d8 RD |
29 | def __init__(self, parent, msg, caption, |
30 | pos=wx.DefaultPosition, size=(500,300), | |
31 | style=wx.DEFAULT_DIALOG_STYLE): | |
32 | wx.Dialog.__init__(self, parent, -1, caption, pos, size, style) | |
d14a1e28 RD |
33 | x, y = pos |
34 | if x == -1 and y == -1: | |
b881fc78 RD |
35 | self.CenterOnScreen(wx.BOTH) |
36 | ||
f50544d8 RD |
37 | text = wx.TextCtrl(self, -1, msg, |
38 | style=wx.TE_MULTILINE | wx.TE_READONLY) | |
b881fc78 RD |
39 | |
40 | ok = wx.Button(self, wx.ID_OK, "OK") | |
678666f2 | 41 | ok.SetDefault() |
b881fc78 RD |
42 | lc = layoutf.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok)) |
43 | text.SetConstraints(lc) | |
44 | ||
678666f2 | 45 | lc = layoutf.Layoutf('b=b5#1;x%w50#1;w!80;h*', (self,)) |
b881fc78 | 46 | ok.SetConstraints(lc) |
d14a1e28 RD |
47 | self.SetAutoLayout(1) |
48 | self.Layout() | |
49 | ||
50 | ||
33785d9f | 51 | class MultipleChoiceDialog(wx.Dialog): |
b881fc78 RD |
52 | def __init__(self, parent, msg, title, lst, pos = wx.DefaultPosition, |
53 | size = (200,200), style = wx.DEFAULT_DIALOG_STYLE): | |
54 | wx.Dialog.__init__(self, parent, -1, title, pos, size, style) | |
55 | ||
d14a1e28 RD |
56 | x, y = pos |
57 | if x == -1 and y == -1: | |
b881fc78 RD |
58 | self.CenterOnScreen(wx.BOTH) |
59 | ||
b881fc78 RD |
60 | stat = wx.StaticText(self, -1, msg) |
61 | self.lbox = wx.ListBox(self, 100, wx.DefaultPosition, wx.DefaultSize, | |
62 | lst, wx.LB_MULTIPLE) | |
63 | ||
64 | ok = wx.Button(self, wx.ID_OK, "OK") | |
678666f2 | 65 | ok.SetDefault() |
b881fc78 | 66 | cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
0f9da074 RD |
67 | |
68 | dlgsizer = wx.BoxSizer(wx.VERTICAL) | |
69 | dlgsizer.Add(stat, 0, wx.ALL, 4) | |
70 | dlgsizer.Add(self.lbox, 1, wx.EXPAND | wx.ALL, 4) | |
71 | ||
72 | btnsizer = wx.StdDialogButtonSizer() | |
73 | btnsizer.AddButton(ok) | |
74 | btnsizer.AddButton(cancel) | |
718903fe | 75 | btnsizer.Realize() |
0f9da074 RD |
76 | |
77 | dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 4) | |
78 | ||
79 | self.SetSizer(dlgsizer) | |
b881fc78 | 80 | |
d14a1e28 RD |
81 | self.lst = lst |
82 | self.Layout() | |
83 | ||
84 | def GetValue(self): | |
85 | return self.lbox.GetSelections() | |
86 | ||
87 | def GetValueString(self): | |
88 | sel = self.lbox.GetSelections() | |
6def2b52 | 89 | val = [ self.lst[i] for i in sel ] |
b881fc78 | 90 | return tuple(val) |
d14a1e28 RD |
91 | |
92 | ||
93 | #---------------------------------------------------------------------- | |
94 | """ | |
95 | function wrappers for wxPython system dialogs | |
96 | Author: Kevin Altis | |
97 | Date: 2003-1-2 | |
98 | Rev: 3 | |
99 | ||
100 | This is the third refactor of the PythonCard dialog.py module | |
101 | for inclusion in the main wxPython distribution. There are a number of | |
102 | design decisions and subsequent code refactoring to be done, so I'm | |
103 | releasing this just to get some feedback. | |
104 | ||
105 | rev 3: | |
106 | - result dictionary replaced by DialogResults class instance | |
107 | - should message arg be replaced with msg? most wxWindows dialogs | |
108 | seem to use the abbreviation? | |
109 | ||
110 | rev 2: | |
111 | - All dialog classes have been replaced by function wrappers | |
112 | - Changed arg lists to more closely match wxWindows docs and wxPython.lib.dialogs | |
113 | - changed 'returned' value to the actual button id the user clicked on | |
114 | - added a returnedString value for the string version of the return value | |
115 | - reworked colorDialog and fontDialog so you can pass in just a color or font | |
116 | for the most common usage case | |
117 | - probably need to use colour instead of color to match the English English | |
118 | spelling in wxWindows (sigh) | |
119 | - I still think we could lose the parent arg and just always use None | |
120 | """ | |
121 | ||
122 | class DialogResults: | |
123 | def __init__(self, returned): | |
124 | self.returned = returned | |
b881fc78 | 125 | self.accepted = returned in (wx.ID_OK, wx.ID_YES) |
d14a1e28 RD |
126 | self.returnedString = returnedString(returned) |
127 | ||
128 | def __repr__(self): | |
129 | return str(self.__dict__) | |
130 | ||
131 | def returnedString(ret): | |
b881fc78 | 132 | if ret == wx.ID_OK: |
d14a1e28 | 133 | return "Ok" |
b881fc78 | 134 | elif ret == wx.ID_CANCEL: |
d14a1e28 | 135 | return "Cancel" |
b881fc78 | 136 | elif ret == wx.ID_YES: |
d14a1e28 | 137 | return "Yes" |
b881fc78 | 138 | elif ret == wx.ID_NO: |
d14a1e28 RD |
139 | return "No" |
140 | ||
141 | ||
b881fc78 RD |
142 | ## findDialog was created before wxPython got a Find/Replace dialog |
143 | ## but it may be instructive as to how a function wrapper can | |
144 | ## be added for your own custom dialogs | |
145 | ## this dialog is always modal, while wxFindReplaceDialog is | |
146 | ## modeless and so doesn't lend itself to a function wrapper | |
d14a1e28 | 147 | def findDialog(parent=None, searchText='', wholeWordsOnly=0, caseSensitive=0): |
9e26b2b2 | 148 | dlg = wx.Dialog(parent, -1, "Find", wx.DefaultPosition, (380, 120)) |
d14a1e28 | 149 | |
b881fc78 | 150 | wx.StaticText(dlg, -1, 'Find what:', (7, 10)) |
9e26b2b2 | 151 | wSearchText = wx.TextCtrl(dlg, -1, searchText, (80, 7), (195, -1)) |
d14a1e28 | 152 | wSearchText.SetValue(searchText) |
9e26b2b2 RD |
153 | wx.Button(dlg, wx.ID_OK, "Find Next", (285, 5), wx.DefaultSize).SetDefault() |
154 | wx.Button(dlg, wx.ID_CANCEL, "Cancel", (285, 35), wx.DefaultSize) | |
155 | ||
b881fc78 RD |
156 | wWholeWord = wx.CheckBox(dlg, -1, 'Match whole word only', |
157 | (7, 35), wx.DefaultSize, wx.NO_BORDER) | |
158 | ||
d14a1e28 RD |
159 | if wholeWordsOnly: |
160 | wWholeWord.SetValue(1) | |
b881fc78 RD |
161 | |
162 | wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER) | |
163 | ||
d14a1e28 RD |
164 | if caseSensitive: |
165 | wCase.SetValue(1) | |
b881fc78 | 166 | |
d14a1e28 RD |
167 | wSearchText.SetSelection(0, len(wSearchText.GetValue())) |
168 | wSearchText.SetFocus() | |
169 | ||
170 | result = DialogResults(dlg.ShowModal()) | |
19cc8ed9 RD |
171 | result.searchText = wSearchText.GetValue() |
172 | result.wholeWordsOnly = wWholeWord.GetValue() | |
173 | result.caseSensitive = wCase.GetValue() | |
d14a1e28 RD |
174 | dlg.Destroy() |
175 | return result | |
176 | ||
177 | ||
178 | def colorDialog(parent=None, colorData=None, color=None): | |
179 | if colorData: | |
b881fc78 | 180 | dialog = wx.ColourDialog(parent, colorData) |
d14a1e28 | 181 | else: |
b881fc78 | 182 | dialog = wx.ColourDialog(parent) |
d14a1e28 | 183 | dialog.GetColourData().SetChooseFull(1) |
b881fc78 | 184 | |
d14a1e28 RD |
185 | if color is not None: |
186 | dialog.GetColourData().SetColour(color) | |
b881fc78 | 187 | |
d14a1e28 RD |
188 | result = DialogResults(dialog.ShowModal()) |
189 | result.colorData = dialog.GetColourData() | |
190 | result.color = result.colorData.GetColour().Get() | |
191 | dialog.Destroy() | |
192 | return result | |
193 | ||
b881fc78 RD |
194 | |
195 | ## it is easier to just duplicate the code than | |
196 | ## try and replace color with colour in the result | |
d14a1e28 RD |
197 | def colourDialog(parent=None, colourData=None, colour=None): |
198 | if colourData: | |
b881fc78 | 199 | dialog = wx.ColourDialog(parent, colourData) |
d14a1e28 | 200 | else: |
b881fc78 | 201 | dialog = wx.ColourDialog(parent) |
d14a1e28 | 202 | dialog.GetColourData().SetChooseFull(1) |
b881fc78 | 203 | |
d14a1e28 RD |
204 | if colour is not None: |
205 | dialog.GetColourData().SetColour(color) | |
b881fc78 | 206 | |
d14a1e28 RD |
207 | result = DialogResults(dialog.ShowModal()) |
208 | result.colourData = dialog.GetColourData() | |
209 | result.colour = result.colourData.GetColour().Get() | |
210 | dialog.Destroy() | |
211 | return result | |
212 | ||
213 | ||
214 | def fontDialog(parent=None, fontData=None, font=None): | |
215 | if fontData is None: | |
b881fc78 | 216 | fontData = wx.FontData() |
a5deb2fb RD |
217 | fontData.SetColour(wx.BLACK) |
218 | fontData.SetInitialFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)) | |
b881fc78 | 219 | |
d14a1e28 | 220 | if font is not None: |
19cc8ed9 | 221 | fontData.SetInitialFont(font) |
b881fc78 RD |
222 | |
223 | dialog = wx.FontDialog(parent, fontData) | |
d14a1e28 | 224 | result = DialogResults(dialog.ShowModal()) |
b881fc78 | 225 | |
d14a1e28 RD |
226 | if result.accepted: |
227 | fontData = dialog.GetFontData() | |
228 | result.fontData = fontData | |
229 | result.color = fontData.GetColour().Get() | |
230 | result.colour = result.color | |
231 | result.font = fontData.GetChosenFont() | |
232 | else: | |
233 | result.color = None | |
234 | result.colour = None | |
235 | result.font = None | |
b881fc78 | 236 | |
d14a1e28 RD |
237 | dialog.Destroy() |
238 | return result | |
239 | ||
240 | ||
b881fc78 RD |
241 | def textEntryDialog(parent=None, message='', title='', defaultText='', |
242 | style=wx.OK | wx.CANCEL): | |
243 | dialog = wx.TextEntryDialog(parent, message, title, defaultText, style) | |
d14a1e28 RD |
244 | result = DialogResults(dialog.ShowModal()) |
245 | result.text = dialog.GetValue() | |
246 | dialog.Destroy() | |
247 | return result | |
248 | ||
249 | ||
250 | def messageDialog(parent=None, message='', title='Message box', | |
b881fc78 RD |
251 | aStyle = wx.OK | wx.CANCEL | wx.CENTRE, |
252 | pos=wx.DefaultPosition): | |
253 | dialog = wx.MessageDialog(parent, message, title, aStyle, pos) | |
d14a1e28 RD |
254 | result = DialogResults(dialog.ShowModal()) |
255 | dialog.Destroy() | |
256 | return result | |
257 | ||
258 | ||
b881fc78 RD |
259 | ## KEA: alerts are common, so I'm providing a class rather than |
260 | ## requiring the user code to set up the right icons and buttons | |
261 | ## the with messageDialog function | |
262 | def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition): | |
263 | return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos) | |
d14a1e28 RD |
264 | |
265 | ||
b881fc78 RD |
266 | def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition, |
267 | size=(500,300)): | |
268 | ||
33785d9f | 269 | dialog = ScrolledMessageDialog(parent, message, title, pos, size) |
d14a1e28 RD |
270 | result = DialogResults(dialog.ShowModal()) |
271 | dialog.Destroy() | |
272 | return result | |
273 | ||
274 | ||
275 | def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*', | |
b881fc78 RD |
276 | style=wx.OPEN | wx.MULTIPLE): |
277 | ||
278 | dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style) | |
d14a1e28 RD |
279 | result = DialogResults(dialog.ShowModal()) |
280 | if result.accepted: | |
281 | result.paths = dialog.GetPaths() | |
282 | else: | |
283 | result.paths = None | |
284 | dialog.Destroy() | |
285 | return result | |
286 | ||
287 | ||
b881fc78 RD |
288 | ## openFileDialog and saveFileDialog are convenience functions |
289 | ## they represent the most common usages of the fileDialog | |
290 | ## with the most common style options | |
d14a1e28 | 291 | def openFileDialog(parent=None, title='Open', directory='', filename='', |
b881fc78 RD |
292 | wildcard='All Files (*.*)|*.*', |
293 | style=wx.OPEN | wx.MULTIPLE): | |
d14a1e28 RD |
294 | return fileDialog(parent, title, directory, filename, wildcard, style) |
295 | ||
296 | ||
297 | def saveFileDialog(parent=None, title='Save', directory='', filename='', | |
b881fc78 | 298 | wildcard='All Files (*.*)|*.*', |
a43dbe72 | 299 | style=wx.SAVE | wx.OVERWRITE_PROMPT): |
d14a1e28 RD |
300 | return fileDialog(parent, title, directory, filename, wildcard, style) |
301 | ||
302 | ||
303 | def dirDialog(parent=None, message='Choose a directory', path='', style=0, | |
b881fc78 RD |
304 | pos=wx.DefaultPosition, size=wx.DefaultSize): |
305 | ||
306 | dialog = wx.DirDialog(parent, message, path, style, pos, size) | |
d14a1e28 RD |
307 | result = DialogResults(dialog.ShowModal()) |
308 | if result.accepted: | |
309 | result.path = dialog.GetPath() | |
310 | else: | |
311 | result.path = None | |
312 | dialog.Destroy() | |
313 | return result | |
314 | ||
315 | directoryDialog = dirDialog | |
316 | ||
317 | ||
b881fc78 RD |
318 | def singleChoiceDialog(parent=None, message='', title='', lst=[], |
319 | style=wx.OK | wx.CANCEL | wx.CENTRE): | |
19cc8ed9 | 320 | dialog = wx.SingleChoiceDialog(parent, message, title, list(lst), style | wx.DEFAULT_DIALOG_STYLE) |
d14a1e28 RD |
321 | result = DialogResults(dialog.ShowModal()) |
322 | result.selection = dialog.GetStringSelection() | |
323 | dialog.Destroy() | |
324 | return result | |
325 | ||
326 | ||
6def2b52 RD |
327 | def multipleChoiceDialog(parent=None, message='', title='', lst=[], |
328 | pos=wx.DefaultPosition, size=wx.DefaultSize): | |
b881fc78 | 329 | |
6def2b52 RD |
330 | dialog = wx.MultiChoiceDialog(parent, message, title, lst, |
331 | wx.CHOICEDLG_STYLE, pos) | |
d14a1e28 | 332 | result = DialogResults(dialog.ShowModal()) |
6def2b52 | 333 | result.selection = tuple([lst[i] for i in dialog.GetSelections()]) |
d14a1e28 RD |
334 | dialog.Destroy() |
335 | return result | |
336 | ||
337 | ||
338 | if __name__ == '__main__': | |
a5deb2fb RD |
339 | #import os |
340 | #print os.getpid() | |
341 | ||
b881fc78 | 342 | class MyApp(wx.App): |
d14a1e28 RD |
343 | |
344 | def OnInit(self): | |
19cc8ed9 | 345 | self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 240)) |
b881fc78 | 346 | panel = wx.Panel(frame, -1) |
d14a1e28 RD |
347 | self.panel = panel |
348 | ||
d14a1e28 RD |
349 | |
350 | dialogNames = [ | |
351 | 'alertDialog', | |
352 | 'colorDialog', | |
353 | 'directoryDialog', | |
354 | 'fileDialog', | |
355 | 'findDialog', | |
356 | 'fontDialog', | |
357 | 'messageDialog', | |
358 | 'multipleChoiceDialog', | |
359 | 'openFileDialog', | |
360 | 'saveFileDialog', | |
361 | 'scrolledMessageDialog', | |
362 | 'singleChoiceDialog', | |
363 | 'textEntryDialog', | |
364 | ] | |
a5deb2fb RD |
365 | |
366 | self.nameList = wx.ListBox(panel, -1, | |
367 | size=(130, 180), | |
368 | choices=dialogNames, | |
369 | style=wx.LB_SINGLE) | |
370 | self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList) | |
d14a1e28 | 371 | |
b881fc78 | 372 | tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE |
a5deb2fb | 373 | self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle) |
d14a1e28 | 374 | |
a5deb2fb RD |
375 | sizer = wx.BoxSizer(wx.HORIZONTAL) |
376 | sizer.Add(self.nameList, 0, wx.EXPAND|wx.ALL, 20) | |
377 | sizer.Add(self.text1, 1, wx.EXPAND|wx.ALL, 20) | |
378 | ||
379 | panel.SetSizer(sizer) | |
d14a1e28 | 380 | |
a5deb2fb RD |
381 | self.SetTopWindow(frame) |
382 | frame.Show(1) | |
d14a1e28 RD |
383 | return 1 |
384 | ||
a5deb2fb | 385 | |
d14a1e28 RD |
386 | def OnNameListSelected(self, evt): |
387 | import pprint | |
388 | sel = evt.GetString() | |
389 | result = None | |
390 | if sel == 'alertDialog': | |
391 | result = alertDialog(message='Danger Will Robinson') | |
392 | elif sel == 'colorDialog': | |
393 | result = colorDialog() | |
394 | elif sel == 'directoryDialog': | |
395 | result = directoryDialog() | |
396 | elif sel == 'fileDialog': | |
397 | wildcard = "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*" | |
398 | result = fileDialog(None, 'Open', '', '', wildcard) | |
399 | elif sel == 'findDialog': | |
400 | result = findDialog() | |
401 | elif sel == 'fontDialog': | |
402 | result = fontDialog() | |
403 | elif sel == 'messageDialog': | |
404 | result = messageDialog(None, 'Hello from Python and wxPython!', | |
a5deb2fb RD |
405 | 'A Message Box', wx.OK | wx.ICON_INFORMATION) |
406 | #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION) | |
d14a1e28 RD |
407 | #result = messageDialog(None, 'message', 'title') |
408 | elif sel == 'multipleChoiceDialog': | |
409 | result = multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three']) | |
410 | elif sel == 'openFileDialog': | |
411 | result = openFileDialog() | |
412 | elif sel == 'saveFileDialog': | |
413 | result = saveFileDialog() | |
414 | elif sel == 'scrolledMessageDialog': | |
415 | msg = "Can't find the file dialog.py" | |
416 | try: | |
417 | # read this source file and then display it | |
418 | import sys | |
419 | filename = sys.argv[-1] | |
420 | fp = open(filename) | |
421 | message = fp.read() | |
422 | fp.close() | |
423 | except: | |
424 | pass | |
425 | result = scrolledMessageDialog(None, message, filename) | |
426 | elif sel == 'singleChoiceDialog': | |
427 | result = singleChoiceDialog(None, "message", "title", ['one', 'two', 'three']) | |
428 | elif sel == 'textEntryDialog': | |
429 | result = textEntryDialog(None, "message", "title", "text") | |
430 | ||
431 | if result: | |
432 | #self.text1.SetValue(pprint.pformat(result.__dict__)) | |
433 | self.text1.SetValue(str(result)) | |
434 | ||
b881fc78 | 435 | app = MyApp(True) |
d14a1e28 | 436 | app.MainLoop() |
b881fc78 RD |
437 | |
438 |