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