]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
1 #----------------------------------------------------------------------
3 # Purpose: ScrolledMessageDialog, MultipleChoiceDialog and
4 # function wrappers for the common dialogs by Kevin Altis.
8 # Created: 3-January-2002
10 # Copyright: (c) 2002 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
13 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 # o Updated for 2.5 compatability.
17 # 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
19 # o wxScrolledMessageDialog -> ScrolledMessageDialog
20 # o wxMultipleChoiceDialog -> MultipleChoiceDialog
26 #----------------------------------------------------------------------
28 class ScrolledMessageDialog(wx
.Dialog
):
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
)
34 if x
== -1 and y
== -1:
35 self
.CenterOnScreen(wx
.BOTH
)
37 text
= wx
.TextCtrl(self
, -1, msg
,
38 style
=wx
.TE_MULTILINE | wx
.TE_READONLY
)
40 ok
= wx
.Button(self
, wx
.ID_OK
, "OK")
42 lc
= layoutf
.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self
,ok
))
43 text
.SetConstraints(lc
)
45 lc
= layoutf
.Layoutf('b=b5#1;x%w50#1;w!80;h*', (self
,))
51 class MultipleChoiceDialog(wx
.Dialog
):
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
)
57 if x
== -1 and y
== -1:
58 self
.CenterOnScreen(wx
.BOTH
)
60 dc
= wx
.ClientDC(self
)
62 for line
in msg
.splitlines():
63 height
= height
+ dc
.GetTextExtent(line
)[1] + 2
65 stat
= wx
.StaticText(self
, -1, msg
)
66 self
.lbox
= wx
.ListBox(self
, 100, wx
.DefaultPosition
, wx
.DefaultSize
,
69 ok
= wx
.Button(self
, wx
.ID_OK
, "OK")
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
)
75 lc
= layoutf
.Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3', (self
, stat
, ok
))
76 self
.lbox
.SetConstraints(lc
)
78 lc
= layoutf
.Layoutf('b=b5#1;x%w25#1;w!80;h*', (self
,))
81 lc
= layoutf
.Layoutf('b=b5#1;x%w75#1;w!80;h*', (self
,))
82 cancel
.SetConstraints(lc
)
89 return self
.lbox
.GetSelections()
91 def GetValueString(self
):
92 sel
= self
.lbox
.GetSelections()
96 val
.append(self
.lst
[i
])
101 #----------------------------------------------------------------------
103 function wrappers for wxPython system dialogs
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.
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?
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
131 def __init__(self
, returned
):
132 self
.returned
= returned
133 self
.accepted
= returned
in (wx
.ID_OK
, wx
.ID_YES
)
134 self
.returnedString
= returnedString(returned
)
137 return str(self
.__dict
__)
139 def returnedString(ret
):
142 elif ret
== wx
.ID_CANCEL
:
144 elif ret
== wx
.ID_YES
:
146 elif ret
== wx
.ID_NO
:
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
155 def findDialog(parent
=None, searchText
='', wholeWordsOnly
=0, caseSensitive
=0):
156 dlg
= wx
.Dialog(parent
, -1, "Find", wx
.DefaultPosition
, (380, 120))
158 wx
.StaticText(dlg
, -1, 'Find what:', (7, 10))
159 wSearchText
= wx
.TextCtrl(dlg
, -1, searchText
, (80, 7), (195, -1))
160 wSearchText
.SetValue(searchText
)
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
)
164 wWholeWord
= wx
.CheckBox(dlg
, -1, 'Match whole word only',
165 (7, 35), wx
.DefaultSize
, wx
.NO_BORDER
)
168 wWholeWord
.SetValue(1)
170 wCase
= wx
.CheckBox(dlg
, -1, 'Match case', (7, 55), wx
.DefaultSize
, wx
.NO_BORDER
)
175 wSearchText
.SetSelection(0, len(wSearchText
.GetValue()))
176 wSearchText
.SetFocus()
178 result
= DialogResults(dlg
.ShowModal())
179 result
.searchText
= wSearchText
.GetValue()
180 result
.wholeWordsOnly
= wWholeWord
.GetValue()
181 result
.caseSensitive
= wCase
.GetValue()
186 def colorDialog(parent
=None, colorData
=None, color
=None):
188 dialog
= wx
.ColourDialog(parent
, colorData
)
190 dialog
= wx
.ColourDialog(parent
)
191 dialog
.GetColourData().SetChooseFull(1)
193 if color
is not None:
194 dialog
.GetColourData().SetColour(color
)
196 result
= DialogResults(dialog
.ShowModal())
197 result
.colorData
= dialog
.GetColourData()
198 result
.color
= result
.colorData
.GetColour().Get()
203 ## it is easier to just duplicate the code than
204 ## try and replace color with colour in the result
205 def colourDialog(parent
=None, colourData
=None, colour
=None):
207 dialog
= wx
.ColourDialog(parent
, colourData
)
209 dialog
= wx
.ColourDialog(parent
)
210 dialog
.GetColourData().SetChooseFull(1)
212 if colour
is not None:
213 dialog
.GetColourData().SetColour(color
)
215 result
= DialogResults(dialog
.ShowModal())
216 result
.colourData
= dialog
.GetColourData()
217 result
.colour
= result
.colourData
.GetColour().Get()
222 def fontDialog(parent
=None, fontData
=None, font
=None):
224 fontData
= wx
.FontData()
225 fontData
.SetColour(wx
.BLACK
)
226 fontData
.SetInitialFont(wx
.SystemSettings
.GetFont(wx
.SYS_DEFAULT_GUI_FONT
))
229 fontData
.SetInitialFont(font
)
231 dialog
= wx
.FontDialog(parent
, fontData
)
232 result
= DialogResults(dialog
.ShowModal())
235 fontData
= dialog
.GetFontData()
236 result
.fontData
= fontData
237 result
.color
= fontData
.GetColour().Get()
238 result
.colour
= result
.color
239 result
.font
= fontData
.GetChosenFont()
249 def textEntryDialog(parent
=None, message
='', title
='', defaultText
='',
250 style
=wx
.OK | wx
.CANCEL
):
251 dialog
= wx
.TextEntryDialog(parent
, message
, title
, defaultText
, style
)
252 result
= DialogResults(dialog
.ShowModal())
253 result
.text
= dialog
.GetValue()
258 def messageDialog(parent
=None, message
='', title
='Message box',
259 aStyle
= wx
.OK | wx
.CANCEL | wx
.CENTRE
,
260 pos
=wx
.DefaultPosition
):
261 dialog
= wx
.MessageDialog(parent
, message
, title
, aStyle
, pos
)
262 result
= DialogResults(dialog
.ShowModal())
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
)
274 def scrolledMessageDialog(parent
=None, message
='', title
='', pos
=wx
.DefaultPosition
,
277 dialog
= ScrolledMessageDialog(parent
, message
, title
, pos
, size
)
278 result
= DialogResults(dialog
.ShowModal())
283 def fileDialog(parent
=None, title
='Open', directory
='', filename
='', wildcard
='*.*',
284 style
=wx
.OPEN | wx
.MULTIPLE
):
286 dialog
= wx
.FileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
287 result
= DialogResults(dialog
.ShowModal())
289 result
.paths
= dialog
.GetPaths()
296 ## openFileDialog and saveFileDialog are convenience functions
297 ## they represent the most common usages of the fileDialog
298 ## with the most common style options
299 def openFileDialog(parent
=None, title
='Open', directory
='', filename
='',
300 wildcard
='All Files (*.*)|*.*',
301 style
=wx
.OPEN | wx
.MULTIPLE
):
302 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
305 def saveFileDialog(parent
=None, title
='Save', directory
='', filename
='',
306 wildcard
='All Files (*.*)|*.*',
307 style
=wx
.SAVE | wx
.HIDE_READONLY | wx
.OVERWRITE_PROMPT
):
308 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
311 def dirDialog(parent
=None, message
='Choose a directory', path
='', style
=0,
312 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
314 dialog
= wx
.DirDialog(parent
, message
, path
, style
, pos
, size
)
315 result
= DialogResults(dialog
.ShowModal())
317 result
.path
= dialog
.GetPath()
323 directoryDialog
= dirDialog
326 def singleChoiceDialog(parent
=None, message
='', title
='', lst
=[],
327 style
=wx
.OK | wx
.CANCEL | wx
.CENTRE
):
328 dialog
= wx
.SingleChoiceDialog(parent
, message
, title
, list(lst
), style | wx
.DEFAULT_DIALOG_STYLE
)
329 result
= DialogResults(dialog
.ShowModal())
330 result
.selection
= dialog
.GetStringSelection()
335 def multipleChoiceDialog(parent
=None, message
='', title
='', lst
=[], pos
=wx
.DefaultPosition
,
338 dialog
= MultipleChoiceDialog(parent
, message
, title
, lst
, pos
, size
)
339 result
= DialogResults(dialog
.ShowModal())
340 result
.selection
= dialog
.GetValueString()
345 if __name__
== '__main__':
352 self
.frame
= frame
= wx
.Frame(None, -1, "Dialogs", size
=(400, 240))
353 panel
= wx
.Panel(frame
, -1)
365 'multipleChoiceDialog',
368 'scrolledMessageDialog',
369 'singleChoiceDialog',
373 self
.nameList
= wx
.ListBox(panel
, -1,
377 self
.Bind(wx
.EVT_LISTBOX
, self
.OnNameListSelected
, self
.nameList
)
379 tstyle
= wx
.TE_RICH2 | wx
.TE_PROCESS_TAB | wx
.TE_MULTILINE
380 self
.text1
= wx
.TextCtrl(panel
, -1, size
=(200, 180), style
=tstyle
)
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)
386 panel
.SetSizer(sizer
)
388 self
.SetTopWindow(frame
)
393 def OnNameListSelected(self
, evt
):
395 sel
= evt
.GetString()
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!',
412 'A Message Box', wx
.OK | wx
.ICON_INFORMATION
)
413 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
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"
424 # read this source file and then display it
426 filename
= sys
.argv
[-1]
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")
439 #self.text1.SetValue(pprint.pformat(result.__dict__))
440 self
.text1
.SetValue(str(result
))