]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
Added wx.lib.gestures module from Daniel Pozmanter which supports
[wxWidgets.git] / wxPython / wx / lib / dialogs.py
1 #----------------------------------------------------------------------
2 # Name: wx.lib.dialogs
3 # Purpose: ScrolledMessageDialog, MultipleChoiceDialog 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 # 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
18 #
19 # o wxScrolledMessageDialog -> ScrolledMessageDialog
20 # o wxMultipleChoiceDialog -> MultipleChoiceDialog
21 #
22
23 import wx
24 import layoutf
25
26 #----------------------------------------------------------------------
27
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)
33 x, y = pos
34 if x == -1 and y == -1:
35 self.CenterOnScreen(wx.BOTH)
36
37 text = wx.TextCtrl(self, -1, msg,
38 style=wx.TE_MULTILINE | wx.TE_READONLY)
39
40 ok = wx.Button(self, wx.ID_OK, "OK")
41 ok.SetDefault()
42 lc = layoutf.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok))
43 text.SetConstraints(lc)
44
45 lc = layoutf.Layoutf('b=b5#1;x%w50#1;w!80;h*', (self,))
46 ok.SetConstraints(lc)
47 self.SetAutoLayout(1)
48 self.Layout()
49
50
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)
55
56 x, y = pos
57 if x == -1 and y == -1:
58 self.CenterOnScreen(wx.BOTH)
59
60 dc = wx.ClientDC(self)
61 height = 0
62 for line in msg.splitlines():
63 height = height + dc.GetTextExtent(line)[1] + 2
64
65 stat = wx.StaticText(self, -1, msg)
66 self.lbox = wx.ListBox(self, 100, wx.DefaultPosition, wx.DefaultSize,
67 lst, wx.LB_MULTIPLE)
68
69 ok = wx.Button(self, wx.ID_OK, "OK")
70 ok.SetDefault()
71 cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
72
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)
76
77 btnsizer = wx.StdDialogButtonSizer()
78 btnsizer.AddButton(ok)
79 btnsizer.AddButton(cancel)
80 btnsizer.Realize()
81
82 dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 4)
83
84 self.SetSizer(dlgsizer)
85
86 self.SetAutoLayout(1)
87 self.lst = lst
88 self.Layout()
89
90 def GetValue(self):
91 return self.lbox.GetSelections()
92
93 def GetValueString(self):
94 sel = self.lbox.GetSelections()
95 val = []
96
97 for i in sel:
98 val.append(self.lst[i])
99
100 return tuple(val)
101
102
103 #----------------------------------------------------------------------
104 """
105 function wrappers for wxPython system dialogs
106 Author: Kevin Altis
107 Date: 2003-1-2
108 Rev: 3
109
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.
114
115 rev 3:
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?
119
120 rev 2:
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
130 """
131
132 class DialogResults:
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)
137
138 def __repr__(self):
139 return str(self.__dict__)
140
141 def returnedString(ret):
142 if ret == wx.ID_OK:
143 return "Ok"
144 elif ret == wx.ID_CANCEL:
145 return "Cancel"
146 elif ret == wx.ID_YES:
147 return "Yes"
148 elif ret == wx.ID_NO:
149 return "No"
150
151
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))
159
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)
165
166 wWholeWord = wx.CheckBox(dlg, -1, 'Match whole word only',
167 (7, 35), wx.DefaultSize, wx.NO_BORDER)
168
169 if wholeWordsOnly:
170 wWholeWord.SetValue(1)
171
172 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
173
174 if caseSensitive:
175 wCase.SetValue(1)
176
177 wSearchText.SetSelection(0, len(wSearchText.GetValue()))
178 wSearchText.SetFocus()
179
180 result = DialogResults(dlg.ShowModal())
181 result.searchText = wSearchText.GetValue()
182 result.wholeWordsOnly = wWholeWord.GetValue()
183 result.caseSensitive = wCase.GetValue()
184 dlg.Destroy()
185 return result
186
187
188 def colorDialog(parent=None, colorData=None, color=None):
189 if colorData:
190 dialog = wx.ColourDialog(parent, colorData)
191 else:
192 dialog = wx.ColourDialog(parent)
193 dialog.GetColourData().SetChooseFull(1)
194
195 if color is not None:
196 dialog.GetColourData().SetColour(color)
197
198 result = DialogResults(dialog.ShowModal())
199 result.colorData = dialog.GetColourData()
200 result.color = result.colorData.GetColour().Get()
201 dialog.Destroy()
202 return result
203
204
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):
208 if colourData:
209 dialog = wx.ColourDialog(parent, colourData)
210 else:
211 dialog = wx.ColourDialog(parent)
212 dialog.GetColourData().SetChooseFull(1)
213
214 if colour is not None:
215 dialog.GetColourData().SetColour(color)
216
217 result = DialogResults(dialog.ShowModal())
218 result.colourData = dialog.GetColourData()
219 result.colour = result.colourData.GetColour().Get()
220 dialog.Destroy()
221 return result
222
223
224 def fontDialog(parent=None, fontData=None, font=None):
225 if fontData is None:
226 fontData = wx.FontData()
227 fontData.SetColour(wx.BLACK)
228 fontData.SetInitialFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))
229
230 if font is not None:
231 fontData.SetInitialFont(font)
232
233 dialog = wx.FontDialog(parent, fontData)
234 result = DialogResults(dialog.ShowModal())
235
236 if result.accepted:
237 fontData = dialog.GetFontData()
238 result.fontData = fontData
239 result.color = fontData.GetColour().Get()
240 result.colour = result.color
241 result.font = fontData.GetChosenFont()
242 else:
243 result.color = None
244 result.colour = None
245 result.font = None
246
247 dialog.Destroy()
248 return result
249
250
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()
256 dialog.Destroy()
257 return result
258
259
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())
265 dialog.Destroy()
266 return result
267
268
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)
274
275
276 def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
277 size=(500,300)):
278
279 dialog = ScrolledMessageDialog(parent, message, title, pos, size)
280 result = DialogResults(dialog.ShowModal())
281 dialog.Destroy()
282 return result
283
284
285 def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
286 style=wx.OPEN | wx.MULTIPLE):
287
288 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
289 result = DialogResults(dialog.ShowModal())
290 if result.accepted:
291 result.paths = dialog.GetPaths()
292 else:
293 result.paths = None
294 dialog.Destroy()
295 return result
296
297
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)
305
306
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)
311
312
313 def dirDialog(parent=None, message='Choose a directory', path='', style=0,
314 pos=wx.DefaultPosition, size=wx.DefaultSize):
315
316 dialog = wx.DirDialog(parent, message, path, style, pos, size)
317 result = DialogResults(dialog.ShowModal())
318 if result.accepted:
319 result.path = dialog.GetPath()
320 else:
321 result.path = None
322 dialog.Destroy()
323 return result
324
325 directoryDialog = dirDialog
326
327
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()
333 dialog.Destroy()
334 return result
335
336
337 def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
338 size=(200,200)):
339
340 dialog = MultipleChoiceDialog(parent, message, title, lst, pos, size)
341 result = DialogResults(dialog.ShowModal())
342 result.selection = dialog.GetValueString()
343 dialog.Destroy()
344 return result
345
346
347 if __name__ == '__main__':
348 #import os
349 #print os.getpid()
350
351 class MyApp(wx.App):
352
353 def OnInit(self):
354 self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 240))
355 panel = wx.Panel(frame, -1)
356 self.panel = panel
357
358
359 dialogNames = [
360 'alertDialog',
361 'colorDialog',
362 'directoryDialog',
363 'fileDialog',
364 'findDialog',
365 'fontDialog',
366 'messageDialog',
367 'multipleChoiceDialog',
368 'openFileDialog',
369 'saveFileDialog',
370 'scrolledMessageDialog',
371 'singleChoiceDialog',
372 'textEntryDialog',
373 ]
374
375 self.nameList = wx.ListBox(panel, -1,
376 size=(130, 180),
377 choices=dialogNames,
378 style=wx.LB_SINGLE)
379 self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList)
380
381 tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE
382 self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle)
383
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)
387
388 panel.SetSizer(sizer)
389
390 self.SetTopWindow(frame)
391 frame.Show(1)
392 return 1
393
394
395 def OnNameListSelected(self, evt):
396 import pprint
397 sel = evt.GetString()
398 result = None
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"
425 try:
426 # read this source file and then display it
427 import sys
428 filename = sys.argv[-1]
429 fp = open(filename)
430 message = fp.read()
431 fp.close()
432 except:
433 pass
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")
439
440 if result:
441 #self.text1.SetValue(pprint.pformat(result.__dict__))
442 self.text1.SetValue(str(result))
443
444 app = MyApp(True)
445 app.MainLoop()
446
447