]>
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")
41 lc
= layoutf
.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self
,ok
))
42 text
.SetConstraints(lc
)
44 lc
= layoutf
.Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self
,))
50 class MultipleChoiceDialog(wx
.Dialog
):
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
)
56 if x
== -1 and y
== -1:
57 self
.CenterOnScreen(wx
.BOTH
)
59 dc
= wx
.ClientDC(self
)
61 for line
in msg
.splitlines():
62 height
= height
+ dc
.GetTextExtent(line
)[1] + 2
64 stat
= wx
.StaticText(self
, -1, msg
)
65 self
.lbox
= wx
.ListBox(self
, 100, wx
.DefaultPosition
, wx
.DefaultSize
,
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
)
73 lc
= layoutf
.Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3', (self
, stat
, ok
))
74 self
.lbox
.SetConstraints(lc
)
76 lc
= layoutf
.Layoutf('b=b5#1;x%w25#1;w!80;h!25', (self
,))
79 lc
= layoutf
.Layoutf('b=b5#1;x%w75#1;w!80;h!25', (self
,))
80 cancel
.SetConstraints(lc
)
87 return self
.lbox
.GetSelections()
89 def GetValueString(self
):
90 sel
= self
.lbox
.GetSelections()
94 val
.append(self
.lst
[i
])
99 #----------------------------------------------------------------------
101 function wrappers for wxPython system dialogs
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.
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?
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
129 def __init__(self
, returned
):
130 self
.returned
= returned
131 self
.accepted
= returned
in (wx
.ID_OK
, wx
.ID_YES
)
132 self
.returnedString
= returnedString(returned
)
135 return str(self
.__dict
__)
137 def returnedString(ret
):
140 elif ret
== wx
.ID_CANCEL
:
142 elif ret
== wx
.ID_YES
:
144 elif ret
== wx
.ID_NO
:
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
153 def findDialog(parent
=None, searchText
='', wholeWordsOnly
=0, caseSensitive
=0):
154 dlg
= wx
.Dialog(parent
, -1, "Find", wx
.DefaultPosition
, (380, 120))
156 wx
.StaticText(dlg
, -1, 'Find what:', (7, 10))
157 wSearchText
= wx
.TextCtrl(dlg
, -1, searchText
, (80, 7), (195, -1))
158 wSearchText
.SetValue(searchText
)
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
)
162 wWholeWord
= wx
.CheckBox(dlg
, -1, 'Match whole word only',
163 (7, 35), wx
.DefaultSize
, wx
.NO_BORDER
)
166 wWholeWord
.SetValue(1)
168 wCase
= wx
.CheckBox(dlg
, -1, 'Match case', (7, 55), wx
.DefaultSize
, wx
.NO_BORDER
)
173 wSearchText
.SetSelection(0, len(wSearchText
.GetValue()))
174 wSearchText
.SetFocus()
176 result
= DialogResults(dlg
.ShowModal())
177 result
.text
= wSearchText
.GetValue()
178 result
.wholeword
= wWholeWord
.GetValue()
179 result
.casesensitive
= wCase
.GetValue()
184 def colorDialog(parent
=None, colorData
=None, color
=None):
186 dialog
= wx
.ColourDialog(parent
, colorData
)
188 dialog
= wx
.ColourDialog(parent
)
189 dialog
.GetColourData().SetChooseFull(1)
191 if color
is not None:
192 dialog
.GetColourData().SetColour(color
)
194 result
= DialogResults(dialog
.ShowModal())
195 result
.colorData
= dialog
.GetColourData()
196 result
.color
= result
.colorData
.GetColour().Get()
201 ## it is easier to just duplicate the code than
202 ## try and replace color with colour in the result
203 def colourDialog(parent
=None, colourData
=None, colour
=None):
205 dialog
= wx
.ColourDialog(parent
, colourData
)
207 dialog
= wx
.ColourDialog(parent
)
208 dialog
.GetColourData().SetChooseFull(1)
210 if colour
is not None:
211 dialog
.GetColourData().SetColour(color
)
213 result
= DialogResults(dialog
.ShowModal())
214 result
.colourData
= dialog
.GetColourData()
215 result
.colour
= result
.colourData
.GetColour().Get()
220 def fontDialog(parent
=None, fontData
=None, font
=None):
222 fontData
= wx
.FontData()
223 fontData
.SetColour(wx
.BLACK
)
224 fontData
.SetInitialFont(wx
.SystemSettings
.GetFont(wx
.SYS_DEFAULT_GUI_FONT
))
227 aFontData
.SetInitialFont(font
)
229 dialog
= wx
.FontDialog(parent
, fontData
)
230 result
= DialogResults(dialog
.ShowModal())
233 fontData
= dialog
.GetFontData()
234 result
.fontData
= fontData
235 result
.color
= fontData
.GetColour().Get()
236 result
.colour
= result
.color
237 result
.font
= fontData
.GetChosenFont()
247 def textEntryDialog(parent
=None, message
='', title
='', defaultText
='',
248 style
=wx
.OK | wx
.CANCEL
):
249 dialog
= wx
.TextEntryDialog(parent
, message
, title
, defaultText
, style
)
250 result
= DialogResults(dialog
.ShowModal())
251 result
.text
= dialog
.GetValue()
256 def messageDialog(parent
=None, message
='', title
='Message box',
257 aStyle
= wx
.OK | wx
.CANCEL | wx
.CENTRE
,
258 pos
=wx
.DefaultPosition
):
259 dialog
= wx
.MessageDialog(parent
, message
, title
, aStyle
, pos
)
260 result
= DialogResults(dialog
.ShowModal())
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
)
272 def scrolledMessageDialog(parent
=None, message
='', title
='', pos
=wx
.DefaultPosition
,
275 dialog
= ScrolledMessageDialog(parent
, message
, title
, pos
, size
)
276 result
= DialogResults(dialog
.ShowModal())
281 def fileDialog(parent
=None, title
='Open', directory
='', filename
='', wildcard
='*.*',
282 style
=wx
.OPEN | wx
.MULTIPLE
):
284 dialog
= wx
.FileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
285 result
= DialogResults(dialog
.ShowModal())
287 result
.paths
= dialog
.GetPaths()
294 ## openFileDialog and saveFileDialog are convenience functions
295 ## they represent the most common usages of the fileDialog
296 ## with the most common style options
297 def openFileDialog(parent
=None, title
='Open', directory
='', filename
='',
298 wildcard
='All Files (*.*)|*.*',
299 style
=wx
.OPEN | wx
.MULTIPLE
):
300 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
303 def saveFileDialog(parent
=None, title
='Save', directory
='', filename
='',
304 wildcard
='All Files (*.*)|*.*',
305 style
=wx
.SAVE | wx
.HIDE_READONLY | wx
.OVERWRITE_PROMPT
):
306 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
309 def dirDialog(parent
=None, message
='Choose a directory', path
='', style
=0,
310 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
312 dialog
= wx
.DirDialog(parent
, message
, path
, style
, pos
, size
)
313 result
= DialogResults(dialog
.ShowModal())
315 result
.path
= dialog
.GetPath()
321 directoryDialog
= dirDialog
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
)
327 result
= DialogResults(dialog
.ShowModal())
328 result
.selection
= dialog
.GetStringSelection()
333 def multipleChoiceDialog(parent
=None, message
='', title
='', lst
=[], pos
=wx
.DefaultPosition
,
336 dialog
= MultipleChoiceDialog(parent
, message
, title
, lst
, pos
, size
)
337 result
= DialogResults(dialog
.ShowModal())
338 result
.selection
= dialog
.GetValueString()
343 if __name__
== '__main__':
350 self
.frame
= frame
= wx
.Frame(None, -1, "Dialogs", size
=(400, 200))
351 panel
= wx
.Panel(frame
, -1)
363 'multipleChoiceDialog',
366 'scrolledMessageDialog',
367 'singleChoiceDialog',
371 self
.nameList
= wx
.ListBox(panel
, -1,
375 self
.Bind(wx
.EVT_LISTBOX
, self
.OnNameListSelected
, self
.nameList
)
377 tstyle
= wx
.TE_RICH2 | wx
.TE_PROCESS_TAB | wx
.TE_MULTILINE
378 self
.text1
= wx
.TextCtrl(panel
, -1, size
=(200, 180), style
=tstyle
)
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)
384 panel
.SetSizer(sizer
)
386 self
.SetTopWindow(frame
)
391 def OnNameListSelected(self
, evt
):
393 sel
= evt
.GetString()
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!',
410 'A Message Box', wx
.OK | wx
.ICON_INFORMATION
)
411 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
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"
422 # read this source file and then display it
424 filename
= sys
.argv
[-1]
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")
437 #self.text1.SetValue(pprint.pformat(result.__dict__))
438 self
.text1
.SetValue(str(result
))