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