]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
layout tweak
[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 lc = layoutf.Layoutf('t=t5#1;b=t5#2;l=l5#1;r=r5#1', (self,ok))
42 text.SetConstraints(lc)
43
44 lc = layoutf.Layoutf('b=b5#1;x%w50#1;w!80;h!25', (self,))
45 ok.SetConstraints(lc)
46 self.SetAutoLayout(1)
47 self.Layout()
48
49
50 class MultipleChoiceDialog(wx.Dialog):
51 def __init__(self, parent, msg, title, lst, pos = wx.DefaultPosition,
52 size = (200,200), style = wx.DEFAULT_DIALOG_STYLE):
53 wx.Dialog.__init__(self, parent, -1, title, pos, size, style)
54
55 x, y = pos
56 if x == -1 and y == -1:
57 self.CenterOnScreen(wx.BOTH)
58
59 dc = wx.ClientDC(self)
60 height = 0
61 for line in msg.splitlines():
62 height = height + dc.GetTextExtent(line)[1] + 2
63
64 stat = wx.StaticText(self, -1, msg)
65 self.lbox = wx.ListBox(self, 100, wx.DefaultPosition, wx.DefaultSize,
66 lst, wx.LB_MULTIPLE)
67
68 ok = wx.Button(self, wx.ID_OK, "OK")
69 cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
70 lc = layoutf.Layoutf('t=t10#1;l=l5#1;r=r5#1;h!%d' % (height,), (self,))
71 stat.SetConstraints(lc)
72
73 lc = layoutf.Layoutf('t=b10#2;l=l5#1;r=r5#1;b=t5#3', (self, stat, ok))
74 self.lbox.SetConstraints(lc)
75
76 lc = layoutf.Layoutf('b=b5#1;x%w25#1;w!80;h!25', (self,))
77 ok.SetConstraints(lc)
78
79 lc = layoutf.Layoutf('b=b5#1;x%w75#1;w!80;h!25', (self,))
80 cancel.SetConstraints(lc)
81
82 self.SetAutoLayout(1)
83 self.lst = lst
84 self.Layout()
85
86 def GetValue(self):
87 return self.lbox.GetSelections()
88
89 def GetValueString(self):
90 sel = self.lbox.GetSelections()
91 val = []
92
93 for i in sel:
94 val.append(self.lst[i])
95
96 return tuple(val)
97
98
99 #----------------------------------------------------------------------
100 """
101 function wrappers for wxPython system dialogs
102 Author: Kevin Altis
103 Date: 2003-1-2
104 Rev: 3
105
106 This is the third refactor of the PythonCard dialog.py module
107 for inclusion in the main wxPython distribution. There are a number of
108 design decisions and subsequent code refactoring to be done, so I'm
109 releasing this just to get some feedback.
110
111 rev 3:
112 - result dictionary replaced by DialogResults class instance
113 - should message arg be replaced with msg? most wxWindows dialogs
114 seem to use the abbreviation?
115
116 rev 2:
117 - All dialog classes have been replaced by function wrappers
118 - Changed arg lists to more closely match wxWindows docs and wxPython.lib.dialogs
119 - changed 'returned' value to the actual button id the user clicked on
120 - added a returnedString value for the string version of the return value
121 - reworked colorDialog and fontDialog so you can pass in just a color or font
122 for the most common usage case
123 - probably need to use colour instead of color to match the English English
124 spelling in wxWindows (sigh)
125 - I still think we could lose the parent arg and just always use None
126 """
127
128 class DialogResults:
129 def __init__(self, returned):
130 self.returned = returned
131 self.accepted = returned in (wx.ID_OK, wx.ID_YES)
132 self.returnedString = returnedString(returned)
133
134 def __repr__(self):
135 return str(self.__dict__)
136
137 def returnedString(ret):
138 if ret == wx.ID_OK:
139 return "Ok"
140 elif ret == wx.ID_CANCEL:
141 return "Cancel"
142 elif ret == wx.ID_YES:
143 return "Yes"
144 elif ret == wx.ID_NO:
145 return "No"
146
147
148 ## findDialog was created before wxPython got a Find/Replace dialog
149 ## but it may be instructive as to how a function wrapper can
150 ## be added for your own custom dialogs
151 ## this dialog is always modal, while wxFindReplaceDialog is
152 ## modeless and so doesn't lend itself to a function wrapper
153 def findDialog(parent=None, searchText='', wholeWordsOnly=0, caseSensitive=0):
154 dlg = wx.Dialog(parent, -1, "Find", wx.DefaultPosition, (380, 120))
155
156 wx.StaticText(dlg, -1, 'Find what:', (7, 10))
157 wSearchText = wx.TextCtrl(dlg, -1, searchText, (80, 7), (195, -1))
158 wSearchText.SetValue(searchText)
159 wx.Button(dlg, wx.ID_OK, "Find Next", (285, 5), wx.DefaultSize).SetDefault()
160 wx.Button(dlg, wx.ID_CANCEL, "Cancel", (285, 35), wx.DefaultSize)
161
162 wWholeWord = wx.CheckBox(dlg, -1, 'Match whole word only',
163 (7, 35), wx.DefaultSize, wx.NO_BORDER)
164
165 if wholeWordsOnly:
166 wWholeWord.SetValue(1)
167
168 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
169
170 if caseSensitive:
171 wCase.SetValue(1)
172
173 wSearchText.SetSelection(0, len(wSearchText.GetValue()))
174 wSearchText.SetFocus()
175
176 result = DialogResults(dlg.ShowModal())
177 result.text = wSearchText.GetValue()
178 result.wholeword = wWholeWord.GetValue()
179 result.casesensitive = wCase.GetValue()
180 dlg.Destroy()
181 return result
182
183
184 def colorDialog(parent=None, colorData=None, color=None):
185 if colorData:
186 dialog = wx.ColourDialog(parent, colorData)
187 else:
188 dialog = wx.ColourDialog(parent)
189 dialog.GetColourData().SetChooseFull(1)
190
191 if color is not None:
192 dialog.GetColourData().SetColour(color)
193
194 result = DialogResults(dialog.ShowModal())
195 result.colorData = dialog.GetColourData()
196 result.color = result.colorData.GetColour().Get()
197 dialog.Destroy()
198 return result
199
200
201 ## it is easier to just duplicate the code than
202 ## try and replace color with colour in the result
203 def colourDialog(parent=None, colourData=None, colour=None):
204 if colourData:
205 dialog = wx.ColourDialog(parent, colourData)
206 else:
207 dialog = wx.ColourDialog(parent)
208 dialog.GetColourData().SetChooseFull(1)
209
210 if colour is not None:
211 dialog.GetColourData().SetColour(color)
212
213 result = DialogResults(dialog.ShowModal())
214 result.colourData = dialog.GetColourData()
215 result.colour = result.colourData.GetColour().Get()
216 dialog.Destroy()
217 return result
218
219
220 def fontDialog(parent=None, fontData=None, font=None):
221 if fontData is None:
222 fontData = wx.FontData()
223 fontData.SetColour(wx.BLACK)
224 fontData.SetInitialFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))
225
226 if font is not None:
227 aFontData.SetInitialFont(font)
228
229 dialog = wx.FontDialog(parent, fontData)
230 result = DialogResults(dialog.ShowModal())
231
232 if result.accepted:
233 fontData = dialog.GetFontData()
234 result.fontData = fontData
235 result.color = fontData.GetColour().Get()
236 result.colour = result.color
237 result.font = fontData.GetChosenFont()
238 else:
239 result.color = None
240 result.colour = None
241 result.font = None
242
243 dialog.Destroy()
244 return result
245
246
247 def textEntryDialog(parent=None, message='', title='', defaultText='',
248 style=wx.OK | wx.CANCEL):
249 dialog = wx.TextEntryDialog(parent, message, title, defaultText, style)
250 result = DialogResults(dialog.ShowModal())
251 result.text = dialog.GetValue()
252 dialog.Destroy()
253 return result
254
255
256 def messageDialog(parent=None, message='', title='Message box',
257 aStyle = wx.OK | wx.CANCEL | wx.CENTRE,
258 pos=wx.DefaultPosition):
259 dialog = wx.MessageDialog(parent, message, title, aStyle, pos)
260 result = DialogResults(dialog.ShowModal())
261 dialog.Destroy()
262 return result
263
264
265 ## KEA: alerts are common, so I'm providing a class rather than
266 ## requiring the user code to set up the right icons and buttons
267 ## the with messageDialog function
268 def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition):
269 return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos)
270
271
272 def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
273 size=(500,300)):
274
275 dialog = ScrolledMessageDialog(parent, message, title, pos, size)
276 result = DialogResults(dialog.ShowModal())
277 dialog.Destroy()
278 return result
279
280
281 def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
282 style=wx.OPEN | wx.MULTIPLE):
283
284 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
285 result = DialogResults(dialog.ShowModal())
286 if result.accepted:
287 result.paths = dialog.GetPaths()
288 else:
289 result.paths = None
290 dialog.Destroy()
291 return result
292
293
294 ## openFileDialog and saveFileDialog are convenience functions
295 ## they represent the most common usages of the fileDialog
296 ## with the most common style options
297 def openFileDialog(parent=None, title='Open', directory='', filename='',
298 wildcard='All Files (*.*)|*.*',
299 style=wx.OPEN | wx.MULTIPLE):
300 return fileDialog(parent, title, directory, filename, wildcard, style)
301
302
303 def saveFileDialog(parent=None, title='Save', directory='', filename='',
304 wildcard='All Files (*.*)|*.*',
305 style=wx.SAVE | wx.HIDE_READONLY | wx.OVERWRITE_PROMPT):
306 return fileDialog(parent, title, directory, filename, wildcard, style)
307
308
309 def dirDialog(parent=None, message='Choose a directory', path='', style=0,
310 pos=wx.DefaultPosition, size=wx.DefaultSize):
311
312 dialog = wx.DirDialog(parent, message, path, style, pos, size)
313 result = DialogResults(dialog.ShowModal())
314 if result.accepted:
315 result.path = dialog.GetPath()
316 else:
317 result.path = None
318 dialog.Destroy()
319 return result
320
321 directoryDialog = dirDialog
322
323
324 def singleChoiceDialog(parent=None, message='', title='', lst=[],
325 style=wx.OK | wx.CANCEL | wx.CENTRE):
326 dialog = wx.SingleChoiceDialog(parent, message, title, lst, style)
327 result = DialogResults(dialog.ShowModal())
328 result.selection = dialog.GetStringSelection()
329 dialog.Destroy()
330 return result
331
332
333 def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
334 size=(200,200)):
335
336 dialog = MultipleChoiceDialog(parent, message, title, lst, pos, size)
337 result = DialogResults(dialog.ShowModal())
338 result.selection = dialog.GetValueString()
339 dialog.Destroy()
340 return result
341
342
343 if __name__ == '__main__':
344 #import os
345 #print os.getpid()
346
347 class MyApp(wx.App):
348
349 def OnInit(self):
350 self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 200))
351 panel = wx.Panel(frame, -1)
352 self.panel = panel
353
354
355 dialogNames = [
356 'alertDialog',
357 'colorDialog',
358 'directoryDialog',
359 'fileDialog',
360 'findDialog',
361 'fontDialog',
362 'messageDialog',
363 'multipleChoiceDialog',
364 'openFileDialog',
365 'saveFileDialog',
366 'scrolledMessageDialog',
367 'singleChoiceDialog',
368 'textEntryDialog',
369 ]
370
371 self.nameList = wx.ListBox(panel, -1,
372 size=(130, 180),
373 choices=dialogNames,
374 style=wx.LB_SINGLE)
375 self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList)
376
377 tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE
378 self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle)
379
380 sizer = wx.BoxSizer(wx.HORIZONTAL)
381 sizer.Add(self.nameList, 0, wx.EXPAND|wx.ALL, 20)
382 sizer.Add(self.text1, 1, wx.EXPAND|wx.ALL, 20)
383
384 panel.SetSizer(sizer)
385
386 self.SetTopWindow(frame)
387 frame.Show(1)
388 return 1
389
390
391 def OnNameListSelected(self, evt):
392 import pprint
393 sel = evt.GetString()
394 result = None
395 if sel == 'alertDialog':
396 result = alertDialog(message='Danger Will Robinson')
397 elif sel == 'colorDialog':
398 result = colorDialog()
399 elif sel == 'directoryDialog':
400 result = directoryDialog()
401 elif sel == 'fileDialog':
402 wildcard = "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
403 result = fileDialog(None, 'Open', '', '', wildcard)
404 elif sel == 'findDialog':
405 result = findDialog()
406 elif sel == 'fontDialog':
407 result = fontDialog()
408 elif sel == 'messageDialog':
409 result = messageDialog(None, 'Hello from Python and wxPython!',
410 'A Message Box', wx.OK | wx.ICON_INFORMATION)
411 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
412 #result = messageDialog(None, 'message', 'title')
413 elif sel == 'multipleChoiceDialog':
414 result = multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
415 elif sel == 'openFileDialog':
416 result = openFileDialog()
417 elif sel == 'saveFileDialog':
418 result = saveFileDialog()
419 elif sel == 'scrolledMessageDialog':
420 msg = "Can't find the file dialog.py"
421 try:
422 # read this source file and then display it
423 import sys
424 filename = sys.argv[-1]
425 fp = open(filename)
426 message = fp.read()
427 fp.close()
428 except:
429 pass
430 result = scrolledMessageDialog(None, message, filename)
431 elif sel == 'singleChoiceDialog':
432 result = singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
433 elif sel == 'textEntryDialog':
434 result = textEntryDialog(None, "message", "title", "text")
435
436 if result:
437 #self.text1.SetValue(pprint.pformat(result.__dict__))
438 self.text1.SetValue(str(result))
439
440 app = MyApp(True)
441 app.MainLoop()
442
443