]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.dialogs
3 # Purpose: wxScrolledMessageDialog, wxMultipleChoiceDialog 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.
21 #----------------------------------------------------------------------
23 class wxScrolledMessageDialog(wx
.Dialog
):
24 def __init__(self
, parent
, msg
, caption
, pos
= wx
.DefaultPosition
,
26 wx
.Dialog
.__init
__(self
, parent
, -1, caption
, pos
, size
)
28 if x
== -1 and y
== -1:
29 self
.CenterOnScreen(wx
.BOTH
)
31 text
= wx
.TextCtrl(self
, -1, msg
, wx
.DefaultPosition
, wx
.DefaultSize
,
32 wx
.TE_MULTILINE | wx
.TE_READONLY
)
34 ok
= wx
.Button(self
, wx
.ID_OK
, "OK")
35 lc
= layoutf
.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self
,ok
))
36 text
.SetConstraints(lc
)
38 lc
= layoutf
.Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self
,))
44 class wxMultipleChoiceDialog(wx
.Dialog
):
45 def __init__(self
, parent
, msg
, title
, lst
, pos
= wx
.DefaultPosition
,
46 size
= (200,200), style
= wx
.DEFAULT_DIALOG_STYLE
):
47 wx
.Dialog
.__init
__(self
, parent
, -1, title
, pos
, size
, style
)
50 if x
== -1 and y
== -1:
51 self
.CenterOnScreen(wx
.BOTH
)
53 dc
= wx
.ClientDC(self
)
55 for line
in msg
.splitlines():
56 height
= height
+ dc
.GetTextExtent(line
)[1] + 2
58 stat
= wx
.StaticText(self
, -1, msg
)
59 self
.lbox
= wx
.ListBox(self
, 100, wx
.DefaultPosition
, wx
.DefaultSize
,
62 ok
= wx
.Button(self
, wx
.ID_OK
, "OK")
63 cancel
= wx
.Button(self
, wx
.ID_CANCEL
, "Cancel")
64 lc
= layoutf
.Layoutf('t=t10#1;l=l5#1;r=r5#1;h!%d' % (height
,), (self
,))
65 stat
.SetConstraints(lc
)
67 lc
= layoutf
.Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3', (self
, stat
, ok
))
68 self
.lbox
.SetConstraints(lc
)
70 lc
= layoutf
.Layoutf('b=b5#1;x%w25#1;w!80;h!25', (self
,))
73 lc
= layoutf
.Layoutf('b=b5#1;x%w75#1;w!80;h!25', (self
,))
74 cancel
.SetConstraints(lc
)
81 return self
.lbox
.GetSelections()
83 def GetValueString(self
):
84 sel
= self
.lbox
.GetSelections()
88 val
.append(self
.lst
[i
])
93 #----------------------------------------------------------------------
95 function wrappers for wxPython system dialogs
100 This is the third refactor of the PythonCard dialog.py module
101 for inclusion in the main wxPython distribution. There are a number of
102 design decisions and subsequent code refactoring to be done, so I'm
103 releasing this just to get some feedback.
106 - result dictionary replaced by DialogResults class instance
107 - should message arg be replaced with msg? most wxWindows dialogs
108 seem to use the abbreviation?
111 - All dialog classes have been replaced by function wrappers
112 - Changed arg lists to more closely match wxWindows docs and wxPython.lib.dialogs
113 - changed 'returned' value to the actual button id the user clicked on
114 - added a returnedString value for the string version of the return value
115 - reworked colorDialog and fontDialog so you can pass in just a color or font
116 for the most common usage case
117 - probably need to use colour instead of color to match the English English
118 spelling in wxWindows (sigh)
119 - I still think we could lose the parent arg and just always use None
123 def __init__(self
, returned
):
124 self
.returned
= returned
125 self
.accepted
= returned
in (wx
.ID_OK
, wx
.ID_YES
)
126 self
.returnedString
= returnedString(returned
)
129 return str(self
.__dict
__)
131 def returnedString(ret
):
134 elif ret
== wx
.ID_CANCEL
:
136 elif ret
== wx
.ID_YES
:
138 elif ret
== wx
.ID_NO
:
142 ## findDialog was created before wxPython got a Find/Replace dialog
143 ## but it may be instructive as to how a function wrapper can
144 ## be added for your own custom dialogs
145 ## this dialog is always modal, while wxFindReplaceDialog is
146 ## modeless and so doesn't lend itself to a function wrapper
147 def findDialog(parent
=None, searchText
='', wholeWordsOnly
=0, caseSensitive
=0):
148 dlg
= wx
.Dialog(parent
, -1, "Find", wx
.DefaultPosition
, (370, 120))
150 wx
.StaticText(dlg
, -1, 'Find what:', (7, 10))
151 wSearchText
= wx
.TextCtrl(dlg
, -1, searchText
, (70, 7), (195, -1))
152 wSearchText
.SetValue(searchText
)
153 wx
.wxButton(dlg
, wx
.ID_OK
, "Find Next", (280, 5), wx
.DefaultSize
).SetDefault()
154 wx
.wxButton(dlg
, wx
.ID_CANCEL
, "Cancel", (280, 35), wx
.DefaultSize
)
155 wWholeWord
= wx
.CheckBox(dlg
, -1, 'Match whole word only',
156 (7, 35), wx
.DefaultSize
, wx
.NO_BORDER
)
159 wWholeWord
.SetValue(1)
161 wCase
= wx
.CheckBox(dlg
, -1, 'Match case', (7, 55), wx
.DefaultSize
, wx
.NO_BORDER
)
166 wSearchText
.SetSelection(0, len(wSearchText
.GetValue()))
167 wSearchText
.SetFocus()
169 result
= DialogResults(dlg
.ShowModal())
170 result
.text
= wSearchText
.GetValue()
171 result
.wholeword
= wWholeWord
.GetValue()
172 result
.casesensitive
= wCase
.GetValue()
177 def colorDialog(parent
=None, colorData
=None, color
=None):
179 dialog
= wx
.ColourDialog(parent
, colorData
)
181 dialog
= wx
.ColourDialog(parent
)
182 dialog
.GetColourData().SetChooseFull(1)
184 if color
is not None:
185 dialog
.GetColourData().SetColour(color
)
187 result
= DialogResults(dialog
.ShowModal())
188 result
.colorData
= dialog
.GetColourData()
189 result
.color
= result
.colorData
.GetColour().Get()
194 ## it is easier to just duplicate the code than
195 ## try and replace color with colour in the result
196 def colourDialog(parent
=None, colourData
=None, colour
=None):
198 dialog
= wx
.ColourDialog(parent
, colourData
)
200 dialog
= wx
.ColourDialog(parent
)
201 dialog
.GetColourData().SetChooseFull(1)
203 if colour
is not None:
204 dialog
.GetColourData().SetColour(color
)
206 result
= DialogResults(dialog
.ShowModal())
207 result
.colourData
= dialog
.GetColourData()
208 result
.colour
= result
.colourData
.GetColour().Get()
213 def fontDialog(parent
=None, fontData
=None, font
=None):
215 fontData
= wx
.FontData()
218 aFontData
.SetInitialFont(font
)
220 dialog
= wx
.FontDialog(parent
, fontData
)
221 result
= DialogResults(dialog
.ShowModal())
224 fontData
= dialog
.GetFontData()
225 result
.fontData
= fontData
226 result
.color
= fontData
.GetColour().Get()
227 result
.colour
= result
.color
228 result
.font
= fontData
.GetChosenFont()
238 def textEntryDialog(parent
=None, message
='', title
='', defaultText
='',
239 style
=wx
.OK | wx
.CANCEL
):
240 dialog
= wx
.TextEntryDialog(parent
, message
, title
, defaultText
, style
)
241 result
= DialogResults(dialog
.ShowModal())
242 result
.text
= dialog
.GetValue()
247 def messageDialog(parent
=None, message
='', title
='Message box',
248 aStyle
= wx
.OK | wx
.CANCEL | wx
.CENTRE
,
249 pos
=wx
.DefaultPosition
):
250 dialog
= wx
.MessageDialog(parent
, message
, title
, aStyle
, pos
)
251 result
= DialogResults(dialog
.ShowModal())
256 ## KEA: alerts are common, so I'm providing a class rather than
257 ## requiring the user code to set up the right icons and buttons
258 ## the with messageDialog function
259 def alertDialog(parent
=None, message
='', title
='Alert', pos
=wx
.DefaultPosition
):
260 return messageDialog(parent
, message
, title
, wx
.ICON_EXCLAMATION | wx
.OK
, pos
)
263 def scrolledMessageDialog(parent
=None, message
='', title
='', pos
=wx
.DefaultPosition
,
266 dialog
= wxScrolledMessageDialog(parent
, message
, title
, pos
, size
)
267 result
= DialogResults(dialog
.ShowModal())
272 def fileDialog(parent
=None, title
='Open', directory
='', filename
='', wildcard
='*.*',
273 style
=wx
.OPEN | wx
.MULTIPLE
):
275 dialog
= wx
.FileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
276 result
= DialogResults(dialog
.ShowModal())
278 result
.paths
= dialog
.GetPaths()
285 ## openFileDialog and saveFileDialog are convenience functions
286 ## they represent the most common usages of the fileDialog
287 ## with the most common style options
288 def openFileDialog(parent
=None, title
='Open', directory
='', filename
='',
289 wildcard
='All Files (*.*)|*.*',
290 style
=wx
.OPEN | wx
.MULTIPLE
):
291 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
294 def saveFileDialog(parent
=None, title
='Save', directory
='', filename
='',
295 wildcard
='All Files (*.*)|*.*',
296 style
=wx
.SAVE | wx
.HIDE_READONLY | wx
.OVERWRITE_PROMPT
):
297 return fileDialog(parent
, title
, directory
, filename
, wildcard
, style
)
300 def dirDialog(parent
=None, message
='Choose a directory', path
='', style
=0,
301 pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
303 dialog
= wx
.DirDialog(parent
, message
, path
, style
, pos
, size
)
304 result
= DialogResults(dialog
.ShowModal())
306 result
.path
= dialog
.GetPath()
312 directoryDialog
= dirDialog
315 def singleChoiceDialog(parent
=None, message
='', title
='', lst
=[],
316 style
=wx
.OK | wx
.CANCEL | wx
.CENTRE
):
317 dialog
= wx
.SingleChoiceDialog(parent
, message
, title
, lst
, style
)
318 result
= DialogResults(dialog
.ShowModal())
319 result
.selection
= dialog
.GetStringSelection()
324 def multipleChoiceDialog(parent
=None, message
='', title
='', lst
=[], pos
=wx
.DefaultPosition
,
327 dialog
= wxMultipleChoiceDialog(parent
, message
, title
, lst
, pos
, size
)
328 result
= DialogResults(dialog
.ShowModal())
329 result
.selection
= dialog
.GetValueString()
334 if __name__
== '__main__':
338 frame
= wx
.Frame(None, -1, "Dialogs", size
=(400, 200))
339 panel
= wx
.Panel(frame
, -1)
352 'multipleChoiceDialog',
355 'scrolledMessageDialog',
356 'singleChoiceDialog',
359 self
.nameList
= wx
.ListBox(panel
, -1, (0, 0), (130, 180), dialogNames
, style
=wx
.LB_SINGLE
)
360 self
.Bind(wx
.EVT_LISTBOX
, self
.OnNameListSelected
, id=self
.nameList
.GetId())
362 tstyle
= wx
.TE_RICH2 | wx
.TE_PROCESS_TAB | wx
.TE_MULTILINE
363 self
.text1
= wx
.TextCtrl(panel
, -1, pos
=(150, 0), size
=(200, 180), style
=tstyle
)
365 self
.SetTopWindow(frame
)
369 def OnNameListSelected(self
, evt
):
371 sel
= evt
.GetString()
373 if sel
== 'alertDialog':
374 result
= alertDialog(message
='Danger Will Robinson')
375 elif sel
== 'colorDialog':
376 result
= colorDialog()
377 elif sel
== 'directoryDialog':
378 result
= directoryDialog()
379 elif sel
== 'fileDialog':
380 wildcard
= "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
381 result
= fileDialog(None, 'Open', '', '', wildcard
)
382 elif sel
== 'findDialog':
383 result
= findDialog()
384 elif sel
== 'fontDialog':
385 result
= fontDialog()
386 elif sel
== 'messageDialog':
387 result
= messageDialog(None, 'Hello from Python and wxPython!',
388 'A Message Box', wx
.wxOK | wx
.wxICON_INFORMATION
)
389 #wx.wxYES_NO | wx.wxNO_DEFAULT | wx.wxCANCEL | wx.wxICON_INFORMATION)
390 #result = messageDialog(None, 'message', 'title')
391 elif sel
== 'multipleChoiceDialog':
392 result
= multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
393 elif sel
== 'openFileDialog':
394 result
= openFileDialog()
395 elif sel
== 'saveFileDialog':
396 result
= saveFileDialog()
397 elif sel
== 'scrolledMessageDialog':
398 msg
= "Can't find the file dialog.py"
400 # read this source file and then display it
402 filename
= sys
.argv
[-1]
408 result
= scrolledMessageDialog(None, message
, filename
)
409 elif sel
== 'singleChoiceDialog':
410 result
= singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
411 elif sel
== 'textEntryDialog':
412 result
= textEntryDialog(None, "message", "title", "text")
415 #self.text1.SetValue(pprint.pformat(result.__dict__))
416 self
.text1
.SetValue(str(result
))