]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/dialogs.py
reSWIGged
[wxWidgets.git] / wxPython / wx / lib / dialogs.py
CommitLineData
d14a1e28
RD
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#----------------------------------------------------------------------
b881fc78
RD
13# 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14#
15# o Updated for 2.5 compatability.
16#
1fded56b 17
b881fc78
RD
18import wx
19import layoutf
1fded56b 20
d14a1e28
RD
21#----------------------------------------------------------------------
22
b881fc78
RD
23class 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)
d14a1e28
RD
27 x, y = pos
28 if x == -1 and y == -1:
b881fc78
RD
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)
d14a1e28
RD
40 self.SetAutoLayout(1)
41 self.Layout()
42
43
b881fc78
RD
44class 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
d14a1e28
RD
49 x, y = pos
50 if x == -1 and y == -1:
b881fc78
RD
51 self.CenterOnScreen(wx.BOTH)
52
53 dc = wx.ClientDC(self)
d14a1e28
RD
54 height = 0
55 for line in msg.splitlines():
56 height = height + dc.GetTextExtent(line)[1] + 2
b881fc78
RD
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
d14a1e28
RD
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 = []
b881fc78 86
d14a1e28
RD
87 for i in sel:
88 val.append(self.lst[i])
d14a1e28 89
b881fc78 90 return tuple(val)
d14a1e28
RD
91
92
93#----------------------------------------------------------------------
94"""
95function wrappers for wxPython system dialogs
96Author: Kevin Altis
97Date: 2003-1-2
98Rev: 3
99
100This is the third refactor of the PythonCard dialog.py module
101for inclusion in the main wxPython distribution. There are a number of
102design decisions and subsequent code refactoring to be done, so I'm
103releasing this just to get some feedback.
104
105rev 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
110rev 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
122class DialogResults:
123 def __init__(self, returned):
124 self.returned = returned
b881fc78 125 self.accepted = returned in (wx.ID_OK, wx.ID_YES)
d14a1e28
RD
126 self.returnedString = returnedString(returned)
127
128 def __repr__(self):
129 return str(self.__dict__)
130
131def returnedString(ret):
b881fc78 132 if ret == wx.ID_OK:
d14a1e28 133 return "Ok"
b881fc78 134 elif ret == wx.ID_CANCEL:
d14a1e28 135 return "Cancel"
b881fc78 136 elif ret == wx.ID_YES:
d14a1e28 137 return "Yes"
b881fc78 138 elif ret == wx.ID_NO:
d14a1e28
RD
139 return "No"
140
141
b881fc78
RD
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
d14a1e28 147def findDialog(parent=None, searchText='', wholeWordsOnly=0, caseSensitive=0):
b881fc78 148 dlg = wx.Dialog(parent, -1, "Find", wx.DefaultPosition, (370, 120))
d14a1e28 149
b881fc78
RD
150 wx.StaticText(dlg, -1, 'Find what:', (7, 10))
151 wSearchText = wx.TextCtrl(dlg, -1, searchText, (70, 7), (195, -1))
d14a1e28 152 wSearchText.SetValue(searchText)
b881fc78
RD
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
d14a1e28
RD
158 if wholeWordsOnly:
159 wWholeWord.SetValue(1)
b881fc78
RD
160
161 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
162
d14a1e28
RD
163 if caseSensitive:
164 wCase.SetValue(1)
b881fc78 165
d14a1e28
RD
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
177def colorDialog(parent=None, colorData=None, color=None):
178 if colorData:
b881fc78 179 dialog = wx.ColourDialog(parent, colorData)
d14a1e28 180 else:
b881fc78 181 dialog = wx.ColourDialog(parent)
d14a1e28 182 dialog.GetColourData().SetChooseFull(1)
b881fc78 183
d14a1e28
RD
184 if color is not None:
185 dialog.GetColourData().SetColour(color)
b881fc78 186
d14a1e28
RD
187 result = DialogResults(dialog.ShowModal())
188 result.colorData = dialog.GetColourData()
189 result.color = result.colorData.GetColour().Get()
190 dialog.Destroy()
191 return result
192
b881fc78
RD
193
194## it is easier to just duplicate the code than
195## try and replace color with colour in the result
d14a1e28
RD
196def colourDialog(parent=None, colourData=None, colour=None):
197 if colourData:
b881fc78 198 dialog = wx.ColourDialog(parent, colourData)
d14a1e28 199 else:
b881fc78 200 dialog = wx.ColourDialog(parent)
d14a1e28 201 dialog.GetColourData().SetChooseFull(1)
b881fc78 202
d14a1e28
RD
203 if colour is not None:
204 dialog.GetColourData().SetColour(color)
b881fc78 205
d14a1e28
RD
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
213def fontDialog(parent=None, fontData=None, font=None):
214 if fontData is None:
b881fc78
RD
215 fontData = wx.FontData()
216
d14a1e28
RD
217 if font is not None:
218 aFontData.SetInitialFont(font)
b881fc78
RD
219
220 dialog = wx.FontDialog(parent, fontData)
d14a1e28 221 result = DialogResults(dialog.ShowModal())
b881fc78 222
d14a1e28
RD
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
b881fc78 233
d14a1e28
RD
234 dialog.Destroy()
235 return result
236
237
b881fc78
RD
238def textEntryDialog(parent=None, message='', title='', defaultText='',
239 style=wx.OK | wx.CANCEL):
240 dialog = wx.TextEntryDialog(parent, message, title, defaultText, style)
d14a1e28
RD
241 result = DialogResults(dialog.ShowModal())
242 result.text = dialog.GetValue()
243 dialog.Destroy()
244 return result
245
246
247def messageDialog(parent=None, message='', title='Message box',
b881fc78
RD
248 aStyle = wx.OK | wx.CANCEL | wx.CENTRE,
249 pos=wx.DefaultPosition):
250 dialog = wx.MessageDialog(parent, message, title, aStyle, pos)
d14a1e28
RD
251 result = DialogResults(dialog.ShowModal())
252 dialog.Destroy()
253 return result
254
255
b881fc78
RD
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
259def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition):
260 return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos)
d14a1e28
RD
261
262
b881fc78
RD
263def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
264 size=(500,300)):
265
d14a1e28
RD
266 dialog = wxScrolledMessageDialog(parent, message, title, pos, size)
267 result = DialogResults(dialog.ShowModal())
268 dialog.Destroy()
269 return result
270
271
272def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
b881fc78
RD
273 style=wx.OPEN | wx.MULTIPLE):
274
275 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
d14a1e28
RD
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
b881fc78
RD
285## openFileDialog and saveFileDialog are convenience functions
286## they represent the most common usages of the fileDialog
287## with the most common style options
d14a1e28 288def openFileDialog(parent=None, title='Open', directory='', filename='',
b881fc78
RD
289 wildcard='All Files (*.*)|*.*',
290 style=wx.OPEN | wx.MULTIPLE):
d14a1e28
RD
291 return fileDialog(parent, title, directory, filename, wildcard, style)
292
293
294def saveFileDialog(parent=None, title='Save', directory='', filename='',
b881fc78
RD
295 wildcard='All Files (*.*)|*.*',
296 style=wx.SAVE | wx.HIDE_READONLY | wx.OVERWRITE_PROMPT):
d14a1e28
RD
297 return fileDialog(parent, title, directory, filename, wildcard, style)
298
299
300def dirDialog(parent=None, message='Choose a directory', path='', style=0,
b881fc78
RD
301 pos=wx.DefaultPosition, size=wx.DefaultSize):
302
303 dialog = wx.DirDialog(parent, message, path, style, pos, size)
d14a1e28
RD
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
312directoryDialog = dirDialog
313
314
b881fc78
RD
315def singleChoiceDialog(parent=None, message='', title='', lst=[],
316 style=wx.OK | wx.CANCEL | wx.CENTRE):
317 dialog = wx.SingleChoiceDialog(parent, message, title, lst, style)
d14a1e28
RD
318 result = DialogResults(dialog.ShowModal())
319 result.selection = dialog.GetStringSelection()
320 dialog.Destroy()
321 return result
322
323
b881fc78
RD
324def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
325 size=(200,200)):
326
d14a1e28
RD
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
334if __name__ == '__main__':
b881fc78 335 class MyApp(wx.App):
d14a1e28
RD
336
337 def OnInit(self):
b881fc78
RD
338 frame = wx.Frame(None, -1, "Dialogs", size=(400, 200))
339 panel = wx.Panel(frame, -1)
d14a1e28
RD
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 ]
b881fc78
RD
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())
d14a1e28 361
b881fc78
RD
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)
d14a1e28
RD
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
b881fc78 418 app = MyApp(True)
d14a1e28 419 app.MainLoop()
b881fc78
RD
420
421