]>
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")
73 dlgsizer
= wx
.BoxSizer(wx
.VERTICAL
)
74 dlgsizer
.Add(stat
, 0, wx
.ALL
, 4)
75 dlgsizer
.Add(self
.lbox
, 1, wx
.EXPAND | wx
.ALL
, 4)
77 btnsizer
= wx
.StdDialogButtonSizer()
78 btnsizer
.AddButton(ok
)
79 btnsizer
.AddButton(cancel
)
82 dlgsizer
.Add(btnsizer
, 0, wx
.ALL | wx
.ALIGN_RIGHT
, 4)
84 self
.SetSizer(dlgsizer
)
91 return self
.lbox
.GetSelections()
93 def GetValueString(self
):
94 sel
= self
.lbox
.GetSelections()
98 val
.append(self
.lst
[i
])
103 #----------------------------------------------------------------------
105 function wrappers for wxPython system dialogs
110 This is the third refactor of the PythonCard dialog.py module
111 for inclusion in the main wxPython distribution. There are a number of
112 design decisions and subsequent code refactoring to be done, so I'm
113 releasing this just to get some feedback.
116 - result dictionary replaced by DialogResults class instance
117 - should message arg be replaced with msg? most wxWindows dialogs
118 seem to use the abbreviation?
121 - All dialog classes have been replaced by function wrappers
122 - Changed arg lists to more closely match wxWindows docs and wxPython.lib.dialogs
123 - changed 'returned' value to the actual button id the user clicked on
124 - added a returnedString value for the string version of the return value
125 - reworked colorDialog and fontDialog so you can pass in just a color or font
126 for the most common usage case
127 - probably need to use colour instead of color to match the English English
128 spelling in wxWindows (sigh)
129 - I still think we could lose the parent arg and just always use None
133 def __init__(self
, returned
):
134 self
.returned
= returned
135 self
.accepted
= returned
in (wx
.ID_OK
, wx
.ID_YES
)
136 self
.returnedString
= returnedString(returned
)
139 return str(self
.__dict
__)
141 def returnedString(ret
):
144 elif ret
== wx
.ID_CANCEL
:
146 elif ret
== wx
.ID_YES
:
148 elif ret
== wx
.ID_NO
:
152 ## findDialog was created before wxPython got a Find/Replace dialog
153 ## but it may be instructive as to how a function wrapper can
154 ## be added for your own custom dialogs
155 ## this dialog is always modal, while wxFindReplaceDialog is
156 ## modeless and so doesn't lend itself to a function wrapper
157 def findDialog(parent
=None, searchText
='', wholeWordsOnly
=0, caseSensitive
=0):
158 dlg
= wx
.Dialog(parent
, -1, "Find", wx
.DefaultPosition
, (380, 120))
160 wx
.StaticText(dlg
, -1, 'Find what:', (7, 10))
161 wSearchText
= wx
.TextCtrl(dlg
, -1, searchText
, (80, 7), (195, -1))
162 wSearchText
.SetValue(searchText
)
163 wx
.Button(dlg
, wx
.ID_OK
, "Find Next", (285, 5), wx
.DefaultSize
).SetDefault()
164 wx
.Button(dlg
, wx
.ID_CANCEL
, "Cancel", (285, 35), wx
.DefaultSize
)
166 wWholeWord
= wx
.CheckBox(dlg
, -1, 'Match whole word only',
167 (7, 35), wx
.DefaultSize
, wx
.NO_BORDER
)
170 wWholeWord
.SetValue(1)
172 wCase
= wx
.CheckBox(dlg
, -1, 'Match case', (7, 55), wx
.DefaultSize
, wx
.NO_BORDER
)
177 wSearchText
.SetSelection(0, len(wSearchText
.GetValue()))
178 wSearchText
.SetFocus()
180 result
= DialogResults(dlg
.ShowModal())
181 result
.searchText
= wSearchText
.GetValue()
182 result
.wholeWordsOnly
= wWholeWord
.GetValue()
183 result
.caseSensitive
= wCase
.GetValue()
188 def colorDialog(parent
=None, colorData
=None, color
=None):
190 dialog
= wx
.ColourDialog(parent
, colorData
)
192 dialog
= wx
.ColourDialog(parent
)
193 dialog
.GetColourData().SetChooseFull(1)
195 if color
is not None:
196 dialog
.GetColourData().SetColour(color
)
198 result
= DialogResults(dialog
.ShowModal())
199 result
.colorData
= dialog
.GetColourData()
200 result
.color
= result
.colorData
.GetColour().Get()
205 ## it is easier to just duplicate the code than
206 ## try and replace color with colour in the result
207 def colourDialog(parent
=None, colourData
=None, colour
=None):
209 dialog
= wx
.ColourDialog(parent
, colourData
)
211 dialog
= wx
.ColourDialog(parent
)
212 dialog
.GetColourData().SetChooseFull(1)
214 if colour
is not None:
215 dialog
.GetColourData().SetColour(color
)
217 result
= DialogResults(dialog
.ShowModal())
218 result
.colourData
= dialog
.GetColourData()
219 result
.colour
= result
.colourData
.GetColour().Get()
224 def fontDialog(parent
=None, fontData
=None, font
=None):
226 fontData
= wx
.FontData()
227 fontData
.SetColour(wx
.BLACK
)
228 fontData
.SetInitialFont(wx
.SystemSettings
.GetFont(wx
.SYS_DEFAULT_GUI_FONT
))
231 fontData
.SetInitialFont(font
)
233 dialog
= wx
.FontDialog(parent
, fontData
)
234 result
= DialogResults(dialog
.ShowModal())
237 fontData
= dialog
.GetFontData()
238 result
.fontData
= fontData
239 result
.color
= fontData
.GetColour().Get()
240 result
.colour
= result
.color
241 result
.font
= fontData
.GetChosenFont()
251 def textEntryDialog(parent
=None, message
='', title
='', defaultText
='',
252 style
=wx
.OK | wx
.CANCEL
):
253 dialog
= wx
.TextEntryDialog(parent
, message
, title
, defaultText
, style
)
254 result
= DialogResults(dialog
.ShowModal())
255 result
.text
= dialog
.GetValue()
260 def messageDialog(parent
=None, message
='', title
='Message box',
261 aStyle
= wx
.OK | wx
.CANCEL | wx
.CENTRE
,
262 pos
=wx
.DefaultPosition
):
263 dialog
= wx
.MessageDialog(parent
, message
, title
, aStyle
, pos
)
264 result
= DialogResults(dialog
.ShowModal())
269 ## KEA: alerts are common, so I'm providing a class rather than
270 ## requiring the user code to set up the right icons and buttons
271 ## the with messageDialog function
272 def alertDialog(parent
=None, message
='', title
='Alert', pos
=wx
.DefaultPosition
):
273 return messageDialog(parent
, message
, title
, wx
.ICON_EXCLAMATION | wx
.OK
, pos
)
276 def scrolledMessageDialog(parent
=None, message
='', title
='', pos
=wx
.DefaultPosition
,
279 dialog
= ScrolledMessageDialog(parent
, message
, title
, pos
, size
)
280 result
= DialogResults(dialog
.ShowModal())
285 def fileDialog(parent
=None, title
='Open', directory
='', filename
='', wildcard
='*.*',
286 style
=wx
.OPEN | wx
.MULTIPLE
):
288 dialog
= wx
.FileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
289 result
= DialogResults(dialog
.ShowModal())
291 result
.paths
= dialog
.GetPaths()
298 ## openFileDialog and saveFileDialog are convenience functions
299 ## they represent the most common usages of the fileDialog
300 ## with the most common style options
301 def openFileDialog(parent
=None, title
='Open', directory
='', filename
='',
302 wildcard
='All Files (*.*)|*.*',
303 style
=wx
.OPEN | wx
.MULTIPLE
):
304 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
307 def saveFileDialog(parent
=None, title
='Save', directory
='', filename
='',
308 wildcard
='All Files (*.*)|*.*',
309 style
=wx
.SAVE | wx
.HIDE_READONLY | wx
.OVERWRITE_PROMPT
):
310 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
313 def dirDialog(parent
=None, message
='Choose a directory', path
='', style
=0,
314 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
316 dialog
= wx
.DirDialog(parent
, message
, path
, style
, pos
, size
)
317 result
= DialogResults(dialog
.ShowModal())
319 result
.path
= dialog
.GetPath()
325 directoryDialog
= dirDialog
328 def singleChoiceDialog(parent
=None, message
='', title
='', lst
=[],
329 style
=wx
.OK | wx
.CANCEL | wx
.CENTRE
):
330 dialog
= wx
.SingleChoiceDialog(parent
, message
, title
, list(lst
), style | wx
.DEFAULT_DIALOG_STYLE
)
331 result
= DialogResults(dialog
.ShowModal())
332 result
.selection
= dialog
.GetStringSelection()
337 def multipleChoiceDialog(parent
=None, message
='', title
='', lst
=[], pos
=wx
.DefaultPosition
,
340 dialog
= MultipleChoiceDialog(parent
, message
, title
, lst
, pos
, size
)
341 result
= DialogResults(dialog
.ShowModal())
342 result
.selection
= dialog
.GetValueString()
347 if __name__
== '__main__':
354 self
.frame
= frame
= wx
.Frame(None, -1, "Dialogs", size
=(400, 240))
355 panel
= wx
.Panel(frame
, -1)
367 'multipleChoiceDialog',
370 'scrolledMessageDialog',
371 'singleChoiceDialog',
375 self
.nameList
= wx
.ListBox(panel
, -1,
379 self
.Bind(wx
.EVT_LISTBOX
, self
.OnNameListSelected
, self
.nameList
)
381 tstyle
= wx
.TE_RICH2 | wx
.TE_PROCESS_TAB | wx
.TE_MULTILINE
382 self
.text1
= wx
.TextCtrl(panel
, -1, size
=(200, 180), style
=tstyle
)
384 sizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
385 sizer
.Add(self
.nameList
, 0, wx
.EXPAND|wx
.ALL
, 20)
386 sizer
.Add(self
.text1
, 1, wx
.EXPAND|wx
.ALL
, 20)
388 panel
.SetSizer(sizer
)
390 self
.SetTopWindow(frame
)
395 def OnNameListSelected(self
, evt
):
397 sel
= evt
.GetString()
399 if sel
== 'alertDialog':
400 result
= alertDialog(message
='Danger Will Robinson')
401 elif sel
== 'colorDialog':
402 result
= colorDialog()
403 elif sel
== 'directoryDialog':
404 result
= directoryDialog()
405 elif sel
== 'fileDialog':
406 wildcard
= "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
407 result
= fileDialog(None, 'Open', '', '', wildcard
)
408 elif sel
== 'findDialog':
409 result
= findDialog()
410 elif sel
== 'fontDialog':
411 result
= fontDialog()
412 elif sel
== 'messageDialog':
413 result
= messageDialog(None, 'Hello from Python and wxPython!',
414 'A Message Box', wx
.OK | wx
.ICON_INFORMATION
)
415 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
416 #result = messageDialog(None, 'message', 'title')
417 elif sel
== 'multipleChoiceDialog':
418 result
= multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
419 elif sel
== 'openFileDialog':
420 result
= openFileDialog()
421 elif sel
== 'saveFileDialog':
422 result
= saveFileDialog()
423 elif sel
== 'scrolledMessageDialog':
424 msg
= "Can't find the file dialog.py"
426 # read this source file and then display it
428 filename
= sys
.argv
[-1]
434 result
= scrolledMessageDialog(None, message
, filename
)
435 elif sel
== 'singleChoiceDialog':
436 result
= singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
437 elif sel
== 'textEntryDialog':
438 result
= textEntryDialog(None, "message", "title", "text")
441 #self.text1.SetValue(pprint.pformat(result.__dict__))
442 self
.text1
.SetValue(str(result
))