]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
Fixed a very weird typo
[wxWidgets.git] / 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.
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 #----------------------------------------------------------------------
13 # 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 #
15 # o Updated for 2.5 compatability.
16 #
17
18 import wx
19 import layoutf
20
21 #----------------------------------------------------------------------
22
23 class wxScrolledMessageDialog(wx.Dialog):
24 def __init__(self, parent, msg, caption, pos = wx.DefaultPosition,
25 size = (500,300)):
26 wx.Dialog.__init__(self, parent, -1, caption, pos, size)
27 x, y = pos
28 if x == -1 and y == -1:
29 self.CenterOnScreen(wx.BOTH)
30
31 text = wx.TextCtrl(self, -1, msg, wx.DefaultPosition, wx.DefaultSize,
32 wx.TE_MULTILINE | wx.TE_READONLY)
33
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)
37
38 lc = layoutf.Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,))
39 ok.SetConstraints(lc)
40 self.SetAutoLayout(1)
41 self.Layout()
42
43
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)
48
49 x, y = pos
50 if x == -1 and y == -1:
51 self.CenterOnScreen(wx.BOTH)
52
53 dc = wx.ClientDC(self)
54 height = 0
55 for line in msg.splitlines():
56 height = height + dc.GetTextExtent(line)[1] + 2
57
58 stat = wx.StaticText(self, -1, msg)
59 self.lbox = wx.ListBox(self, 100, wx.DefaultPosition, wx.DefaultSize,
60 lst, wx.LB_MULTIPLE)
61
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)
66
67 lc = layoutf.Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3', (self, stat, ok))
68 self.lbox.SetConstraints(lc)
69
70 lc = layoutf.Layoutf('b=b5#1;x%w25#1;w!80;h!25', (self,))
71 ok.SetConstraints(lc)
72
73 lc = layoutf.Layoutf('b=b5#1;x%w75#1;w!80;h!25', (self,))
74 cancel.SetConstraints(lc)
75
76 self.SetAutoLayout(1)
77 self.lst = lst
78 self.Layout()
79
80 def GetValue(self):
81 return self.lbox.GetSelections()
82
83 def GetValueString(self):
84 sel = self.lbox.GetSelections()
85 val = []
86
87 for i in sel:
88 val.append(self.lst[i])
89
90 return tuple(val)
91
92
93 #----------------------------------------------------------------------
94 """
95 function wrappers for wxPython system dialogs
96 Author: Kevin Altis
97 Date: 2003-1-2
98 Rev: 3
99
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.
104
105 rev 3:
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?
109
110 rev 2:
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
120 """
121
122 class DialogResults:
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)
127
128 def __repr__(self):
129 return str(self.__dict__)
130
131 def returnedString(ret):
132 if ret == wx.ID_OK:
133 return "Ok"
134 elif ret == wx.ID_CANCEL:
135 return "Cancel"
136 elif ret == wx.ID_YES:
137 return "Yes"
138 elif ret == wx.ID_NO:
139 return "No"
140
141
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))
149
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)
157
158 if wholeWordsOnly:
159 wWholeWord.SetValue(1)
160
161 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
162
163 if caseSensitive:
164 wCase.SetValue(1)
165
166 wSearchText.SetSelection(0, len(wSearchText.GetValue()))
167 wSearchText.SetFocus()
168
169 result = DialogResults(dlg.ShowModal())
170 result.text = wSearchText.GetValue()
171 result.wholeword = wWholeWord.GetValue()
172 result.casesensitive = wCase.GetValue()
173 dlg.Destroy()
174 return result
175
176
177 def colorDialog(parent=None, colorData=None, color=None):
178 if colorData:
179 dialog = wx.ColourDialog(parent, colorData)
180 else:
181 dialog = wx.ColourDialog(parent)
182 dialog.GetColourData().SetChooseFull(1)
183
184 if color is not None:
185 dialog.GetColourData().SetColour(color)
186
187 result = DialogResults(dialog.ShowModal())
188 result.colorData = dialog.GetColourData()
189 result.color = result.colorData.GetColour().Get()
190 dialog.Destroy()
191 return result
192
193
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):
197 if colourData:
198 dialog = wx.ColourDialog(parent, colourData)
199 else:
200 dialog = wx.ColourDialog(parent)
201 dialog.GetColourData().SetChooseFull(1)
202
203 if colour is not None:
204 dialog.GetColourData().SetColour(color)
205
206 result = DialogResults(dialog.ShowModal())
207 result.colourData = dialog.GetColourData()
208 result.colour = result.colourData.GetColour().Get()
209 dialog.Destroy()
210 return result
211
212
213 def fontDialog(parent=None, fontData=None, font=None):
214 if fontData is None:
215 fontData = wx.FontData()
216
217 if font is not None:
218 aFontData.SetInitialFont(font)
219
220 dialog = wx.FontDialog(parent, fontData)
221 result = DialogResults(dialog.ShowModal())
222
223 if result.accepted:
224 fontData = dialog.GetFontData()
225 result.fontData = fontData
226 result.color = fontData.GetColour().Get()
227 result.colour = result.color
228 result.font = fontData.GetChosenFont()
229 else:
230 result.color = None
231 result.colour = None
232 result.font = None
233
234 dialog.Destroy()
235 return result
236
237
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()
243 dialog.Destroy()
244 return result
245
246
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())
252 dialog.Destroy()
253 return result
254
255
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)
261
262
263 def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
264 size=(500,300)):
265
266 dialog = wxScrolledMessageDialog(parent, message, title, pos, size)
267 result = DialogResults(dialog.ShowModal())
268 dialog.Destroy()
269 return result
270
271
272 def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
273 style=wx.OPEN | wx.MULTIPLE):
274
275 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
276 result = DialogResults(dialog.ShowModal())
277 if result.accepted:
278 result.paths = dialog.GetPaths()
279 else:
280 result.paths = None
281 dialog.Destroy()
282 return result
283
284
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)
292
293
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)
298
299
300 def dirDialog(parent=None, message='Choose a directory', path='', style=0,
301 pos=wx.DefaultPosition, size=wx.DefaultSize):
302
303 dialog = wx.DirDialog(parent, message, path, style, pos, size)
304 result = DialogResults(dialog.ShowModal())
305 if result.accepted:
306 result.path = dialog.GetPath()
307 else:
308 result.path = None
309 dialog.Destroy()
310 return result
311
312 directoryDialog = dirDialog
313
314
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()
320 dialog.Destroy()
321 return result
322
323
324 def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
325 size=(200,200)):
326
327 dialog = wxMultipleChoiceDialog(parent, message, title, lst, pos, size)
328 result = DialogResults(dialog.ShowModal())
329 result.selection = dialog.GetValueString()
330 dialog.Destroy()
331 return result
332
333
334 if __name__ == '__main__':
335 class MyApp(wx.App):
336
337 def OnInit(self):
338 frame = wx.Frame(None, -1, "Dialogs", size=(400, 200))
339 panel = wx.Panel(frame, -1)
340 self.panel = panel
341
342 frame.Show(1)
343
344 dialogNames = [
345 'alertDialog',
346 'colorDialog',
347 'directoryDialog',
348 'fileDialog',
349 'findDialog',
350 'fontDialog',
351 'messageDialog',
352 'multipleChoiceDialog',
353 'openFileDialog',
354 'saveFileDialog',
355 'scrolledMessageDialog',
356 'singleChoiceDialog',
357 'textEntryDialog',
358 ]
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())
361
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)
364
365 self.SetTopWindow(frame)
366
367 return 1
368
369 def OnNameListSelected(self, evt):
370 import pprint
371 sel = evt.GetString()
372 result = None
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"
399 try:
400 # read this source file and then display it
401 import sys
402 filename = sys.argv[-1]
403 fp = open(filename)
404 message = fp.read()
405 fp.close()
406 except:
407 pass
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")
413
414 if result:
415 #self.text1.SetValue(pprint.pformat(result.__dict__))
416 self.text1.SetValue(str(result))
417
418 app = MyApp(True)
419 app.MainLoop()
420
421