]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/dialogs.py
Patches from Will Sadkin:
[wxWidgets.git] / wxPython / wx / lib / dialogs.py
CommitLineData
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
23import wx
24import layoutf
1fded56b 25
d14a1e28
RD
26#----------------------------------------------------------------------
27
33785d9f 28class 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 50class 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"""
101function wrappers for wxPython system dialogs
102Author: Kevin Altis
103Date: 2003-1-2
104Rev: 3
105
106This is the third refactor of the PythonCard dialog.py module
107for inclusion in the main wxPython distribution. There are a number of
108design decisions and subsequent code refactoring to be done, so I'm
109releasing this just to get some feedback.
110
111rev 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
116rev 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
128class 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
137def 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 153def findDialog(parent=None, searchText='', wholeWordsOnly=0, caseSensitive=0):
b881fc78 154 dlg = wx.Dialog(parent, -1, "Find", wx.DefaultPosition, (370, 120))
d14a1e28 155
b881fc78
RD
156 wx.StaticText(dlg, -1, 'Find what:', (7, 10))
157 wSearchText = wx.TextCtrl(dlg, -1, searchText, (70, 7), (195, -1))
d14a1e28 158 wSearchText.SetValue(searchText)
a5deb2fb
RD
159 wx.Button(dlg, wx.ID_OK, "Find Next", (280, 5), wx.DefaultSize).SetDefault()
160 wx.Button(dlg, wx.ID_CANCEL, "Cancel", (280, 35), wx.DefaultSize)
b881fc78
RD
161 wWholeWord = wx.CheckBox(dlg, -1, 'Match whole word only',
162 (7, 35), wx.DefaultSize, wx.NO_BORDER)
163
d14a1e28
RD
164 if wholeWordsOnly:
165 wWholeWord.SetValue(1)
b881fc78
RD
166
167 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
168
d14a1e28
RD
169 if caseSensitive:
170 wCase.SetValue(1)
b881fc78 171
d14a1e28
RD
172 wSearchText.SetSelection(0, len(wSearchText.GetValue()))
173 wSearchText.SetFocus()
174
175 result = DialogResults(dlg.ShowModal())
176 result.text = wSearchText.GetValue()
177 result.wholeword = wWholeWord.GetValue()
178 result.casesensitive = wCase.GetValue()
179 dlg.Destroy()
180 return result
181
182
183def colorDialog(parent=None, colorData=None, color=None):
184 if colorData:
b881fc78 185 dialog = wx.ColourDialog(parent, colorData)
d14a1e28 186 else:
b881fc78 187 dialog = wx.ColourDialog(parent)
d14a1e28 188 dialog.GetColourData().SetChooseFull(1)
b881fc78 189
d14a1e28
RD
190 if color is not None:
191 dialog.GetColourData().SetColour(color)
b881fc78 192
d14a1e28
RD
193 result = DialogResults(dialog.ShowModal())
194 result.colorData = dialog.GetColourData()
195 result.color = result.colorData.GetColour().Get()
196 dialog.Destroy()
197 return result
198
b881fc78
RD
199
200## it is easier to just duplicate the code than
201## try and replace color with colour in the result
d14a1e28
RD
202def colourDialog(parent=None, colourData=None, colour=None):
203 if colourData:
b881fc78 204 dialog = wx.ColourDialog(parent, colourData)
d14a1e28 205 else:
b881fc78 206 dialog = wx.ColourDialog(parent)
d14a1e28 207 dialog.GetColourData().SetChooseFull(1)
b881fc78 208
d14a1e28
RD
209 if colour is not None:
210 dialog.GetColourData().SetColour(color)
b881fc78 211
d14a1e28
RD
212 result = DialogResults(dialog.ShowModal())
213 result.colourData = dialog.GetColourData()
214 result.colour = result.colourData.GetColour().Get()
215 dialog.Destroy()
216 return result
217
218
219def fontDialog(parent=None, fontData=None, font=None):
220 if fontData is None:
b881fc78 221 fontData = wx.FontData()
a5deb2fb
RD
222 fontData.SetColour(wx.BLACK)
223 fontData.SetInitialFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))
b881fc78 224
d14a1e28
RD
225 if font is not None:
226 aFontData.SetInitialFont(font)
b881fc78
RD
227
228 dialog = wx.FontDialog(parent, fontData)
d14a1e28 229 result = DialogResults(dialog.ShowModal())
b881fc78 230
d14a1e28
RD
231 if result.accepted:
232 fontData = dialog.GetFontData()
233 result.fontData = fontData
234 result.color = fontData.GetColour().Get()
235 result.colour = result.color
236 result.font = fontData.GetChosenFont()
237 else:
238 result.color = None
239 result.colour = None
240 result.font = None
b881fc78 241
d14a1e28
RD
242 dialog.Destroy()
243 return result
244
245
b881fc78
RD
246def textEntryDialog(parent=None, message='', title='', defaultText='',
247 style=wx.OK | wx.CANCEL):
248 dialog = wx.TextEntryDialog(parent, message, title, defaultText, style)
d14a1e28
RD
249 result = DialogResults(dialog.ShowModal())
250 result.text = dialog.GetValue()
251 dialog.Destroy()
252 return result
253
254
255def messageDialog(parent=None, message='', title='Message box',
b881fc78
RD
256 aStyle = wx.OK | wx.CANCEL | wx.CENTRE,
257 pos=wx.DefaultPosition):
258 dialog = wx.MessageDialog(parent, message, title, aStyle, pos)
d14a1e28
RD
259 result = DialogResults(dialog.ShowModal())
260 dialog.Destroy()
261 return result
262
263
b881fc78
RD
264## KEA: alerts are common, so I'm providing a class rather than
265## requiring the user code to set up the right icons and buttons
266## the with messageDialog function
267def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition):
268 return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos)
d14a1e28
RD
269
270
b881fc78
RD
271def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
272 size=(500,300)):
273
33785d9f 274 dialog = ScrolledMessageDialog(parent, message, title, pos, size)
d14a1e28
RD
275 result = DialogResults(dialog.ShowModal())
276 dialog.Destroy()
277 return result
278
279
280def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
b881fc78
RD
281 style=wx.OPEN | wx.MULTIPLE):
282
283 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
d14a1e28
RD
284 result = DialogResults(dialog.ShowModal())
285 if result.accepted:
286 result.paths = dialog.GetPaths()
287 else:
288 result.paths = None
289 dialog.Destroy()
290 return result
291
292
b881fc78
RD
293## openFileDialog and saveFileDialog are convenience functions
294## they represent the most common usages of the fileDialog
295## with the most common style options
d14a1e28 296def openFileDialog(parent=None, title='Open', directory='', filename='',
b881fc78
RD
297 wildcard='All Files (*.*)|*.*',
298 style=wx.OPEN | wx.MULTIPLE):
d14a1e28
RD
299 return fileDialog(parent, title, directory, filename, wildcard, style)
300
301
302def saveFileDialog(parent=None, title='Save', directory='', filename='',
b881fc78
RD
303 wildcard='All Files (*.*)|*.*',
304 style=wx.SAVE | wx.HIDE_READONLY | wx.OVERWRITE_PROMPT):
d14a1e28
RD
305 return fileDialog(parent, title, directory, filename, wildcard, style)
306
307
308def dirDialog(parent=None, message='Choose a directory', path='', style=0,
b881fc78
RD
309 pos=wx.DefaultPosition, size=wx.DefaultSize):
310
311 dialog = wx.DirDialog(parent, message, path, style, pos, size)
d14a1e28
RD
312 result = DialogResults(dialog.ShowModal())
313 if result.accepted:
314 result.path = dialog.GetPath()
315 else:
316 result.path = None
317 dialog.Destroy()
318 return result
319
320directoryDialog = dirDialog
321
322
b881fc78
RD
323def singleChoiceDialog(parent=None, message='', title='', lst=[],
324 style=wx.OK | wx.CANCEL | wx.CENTRE):
325 dialog = wx.SingleChoiceDialog(parent, message, title, lst, style)
d14a1e28
RD
326 result = DialogResults(dialog.ShowModal())
327 result.selection = dialog.GetStringSelection()
328 dialog.Destroy()
329 return result
330
331
b881fc78
RD
332def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
333 size=(200,200)):
334
33785d9f 335 dialog = MultipleChoiceDialog(parent, message, title, lst, pos, size)
d14a1e28
RD
336 result = DialogResults(dialog.ShowModal())
337 result.selection = dialog.GetValueString()
338 dialog.Destroy()
339 return result
340
341
342if __name__ == '__main__':
a5deb2fb
RD
343 #import os
344 #print os.getpid()
345
b881fc78 346 class MyApp(wx.App):
d14a1e28
RD
347
348 def OnInit(self):
a5deb2fb 349 self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 200))
b881fc78 350 panel = wx.Panel(frame, -1)
d14a1e28
RD
351 self.panel = panel
352
d14a1e28
RD
353
354 dialogNames = [
355 'alertDialog',
356 'colorDialog',
357 'directoryDialog',
358 'fileDialog',
359 'findDialog',
360 'fontDialog',
361 'messageDialog',
362 'multipleChoiceDialog',
363 'openFileDialog',
364 'saveFileDialog',
365 'scrolledMessageDialog',
366 'singleChoiceDialog',
367 'textEntryDialog',
368 ]
a5deb2fb
RD
369
370 self.nameList = wx.ListBox(panel, -1,
371 size=(130, 180),
372 choices=dialogNames,
373 style=wx.LB_SINGLE)
374 self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList)
d14a1e28 375
b881fc78 376 tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE
a5deb2fb 377 self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle)
d14a1e28 378
a5deb2fb
RD
379 sizer = wx.BoxSizer(wx.HORIZONTAL)
380 sizer.Add(self.nameList, 0, wx.EXPAND|wx.ALL, 20)
381 sizer.Add(self.text1, 1, wx.EXPAND|wx.ALL, 20)
382
383 panel.SetSizer(sizer)
d14a1e28 384
a5deb2fb
RD
385 self.SetTopWindow(frame)
386 frame.Show(1)
d14a1e28
RD
387 return 1
388
a5deb2fb 389
d14a1e28
RD
390 def OnNameListSelected(self, evt):
391 import pprint
392 sel = evt.GetString()
393 result = None
394 if sel == 'alertDialog':
395 result = alertDialog(message='Danger Will Robinson')
396 elif sel == 'colorDialog':
397 result = colorDialog()
398 elif sel == 'directoryDialog':
399 result = directoryDialog()
400 elif sel == 'fileDialog':
401 wildcard = "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
402 result = fileDialog(None, 'Open', '', '', wildcard)
403 elif sel == 'findDialog':
404 result = findDialog()
405 elif sel == 'fontDialog':
406 result = fontDialog()
407 elif sel == 'messageDialog':
408 result = messageDialog(None, 'Hello from Python and wxPython!',
a5deb2fb
RD
409 'A Message Box', wx.OK | wx.ICON_INFORMATION)
410 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
d14a1e28
RD
411 #result = messageDialog(None, 'message', 'title')
412 elif sel == 'multipleChoiceDialog':
413 result = multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
414 elif sel == 'openFileDialog':
415 result = openFileDialog()
416 elif sel == 'saveFileDialog':
417 result = saveFileDialog()
418 elif sel == 'scrolledMessageDialog':
419 msg = "Can't find the file dialog.py"
420 try:
421 # read this source file and then display it
422 import sys
423 filename = sys.argv[-1]
424 fp = open(filename)
425 message = fp.read()
426 fp.close()
427 except:
428 pass
429 result = scrolledMessageDialog(None, message, filename)
430 elif sel == 'singleChoiceDialog':
431 result = singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
432 elif sel == 'textEntryDialog':
433 result = textEntryDialog(None, "message", "title", "text")
434
435 if result:
436 #self.text1.SetValue(pprint.pformat(result.__dict__))
437 self.text1.SetValue(str(result))
438
b881fc78 439 app = MyApp(True)
d14a1e28 440 app.MainLoop()
b881fc78
RD
441
442