]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/dialogs.py
tweak border size
[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, (370, 120))
155
156 wx.StaticText(dlg, -1, 'Find what:', (7, 10))
157 wSearchText = wx.TextCtrl(dlg, -1, searchText, (70, 7), (195, -1))
158 wSearchText.SetValue(searchText)
159 wx.Button(dlg, wx.ID_OK, "Find Next", (280, 5), wx.DefaultSize).SetDefault()
160 wx.Button(dlg, wx.ID_CANCEL, "Cancel", (280, 35), wx.DefaultSize)
161 wWholeWord = wx.CheckBox(dlg, -1, 'Match whole word only',
162 (7, 35), wx.DefaultSize, wx.NO_BORDER)
163
164 if wholeWordsOnly:
165 wWholeWord.SetValue(1)
166
167 wCase = wx.CheckBox(dlg, -1, 'Match case', (7, 55), wx.DefaultSize, wx.NO_BORDER)
168
169 if caseSensitive:
170 wCase.SetValue(1)
171
172 wSearchText.SetSelection(0, len(wSearchText.GetValue()))
173 wSearchText.SetFocus()
174
175 result = DialogResults(dlg.ShowModal())
176 result.text = wSearchText.GetValue()
177 result.wholeword = wWholeWord.GetValue()
178 result.casesensitive = wCase.GetValue()
179 dlg.Destroy()
180 return result
181
182
183 def colorDialog(parent=None, colorData=None, color=None):
184 if colorData:
185 dialog = wx.ColourDialog(parent, colorData)
186 else:
187 dialog = wx.ColourDialog(parent)
188 dialog.GetColourData().SetChooseFull(1)
189
190 if color is not None:
191 dialog.GetColourData().SetColour(color)
192
193 result = DialogResults(dialog.ShowModal())
194 result.colorData = dialog.GetColourData()
195 result.color = result.colorData.GetColour().Get()
196 dialog.Destroy()
197 return result
198
199
200 ## it is easier to just duplicate the code than
201 ## try and replace color with colour in the result
202 def colourDialog(parent=None, colourData=None, colour=None):
203 if colourData:
204 dialog = wx.ColourDialog(parent, colourData)
205 else:
206 dialog = wx.ColourDialog(parent)
207 dialog.GetColourData().SetChooseFull(1)
208
209 if colour is not None:
210 dialog.GetColourData().SetColour(color)
211
212 result = DialogResults(dialog.ShowModal())
213 result.colourData = dialog.GetColourData()
214 result.colour = result.colourData.GetColour().Get()
215 dialog.Destroy()
216 return result
217
218
219 def fontDialog(parent=None, fontData=None, font=None):
220 if fontData is None:
221 fontData = wx.FontData()
222 fontData.SetColour(wx.BLACK)
223 fontData.SetInitialFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))
224
225 if font is not None:
226 aFontData.SetInitialFont(font)
227
228 dialog = wx.FontDialog(parent, fontData)
229 result = DialogResults(dialog.ShowModal())
230
231 if result.accepted:
232 fontData = dialog.GetFontData()
233 result.fontData = fontData
234 result.color = fontData.GetColour().Get()
235 result.colour = result.color
236 result.font = fontData.GetChosenFont()
237 else:
238 result.color = None
239 result.colour = None
240 result.font = None
241
242 dialog.Destroy()
243 return result
244
245
246 def textEntryDialog(parent=None, message='', title='', defaultText='',
247 style=wx.OK | wx.CANCEL):
248 dialog = wx.TextEntryDialog(parent, message, title, defaultText, style)
249 result = DialogResults(dialog.ShowModal())
250 result.text = dialog.GetValue()
251 dialog.Destroy()
252 return result
253
254
255 def messageDialog(parent=None, message='', title='Message box',
256 aStyle = wx.OK | wx.CANCEL | wx.CENTRE,
257 pos=wx.DefaultPosition):
258 dialog = wx.MessageDialog(parent, message, title, aStyle, pos)
259 result = DialogResults(dialog.ShowModal())
260 dialog.Destroy()
261 return result
262
263
264 ## KEA: alerts are common, so I'm providing a class rather than
265 ## requiring the user code to set up the right icons and buttons
266 ## the with messageDialog function
267 def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition):
268 return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos)
269
270
271 def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
272 size=(500,300)):
273
274 dialog = ScrolledMessageDialog(parent, message, title, pos, size)
275 result = DialogResults(dialog.ShowModal())
276 dialog.Destroy()
277 return result
278
279
280 def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
281 style=wx.OPEN | wx.MULTIPLE):
282
283 dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
284 result = DialogResults(dialog.ShowModal())
285 if result.accepted:
286 result.paths = dialog.GetPaths()
287 else:
288 result.paths = None
289 dialog.Destroy()
290 return result
291
292
293 ## openFileDialog and saveFileDialog are convenience functions
294 ## they represent the most common usages of the fileDialog
295 ## with the most common style options
296 def openFileDialog(parent=None, title='Open', directory='', filename='',
297 wildcard='All Files (*.*)|*.*',
298 style=wx.OPEN | wx.MULTIPLE):
299 return fileDialog(parent, title, directory, filename, wildcard, style)
300
301
302 def saveFileDialog(parent=None, title='Save', directory='', filename='',
303 wildcard='All Files (*.*)|*.*',
304 style=wx.SAVE | wx.HIDE_READONLY | wx.OVERWRITE_PROMPT):
305 return fileDialog(parent, title, directory, filename, wildcard, style)
306
307
308 def dirDialog(parent=None, message='Choose a directory', path='', style=0,
309 pos=wx.DefaultPosition, size=wx.DefaultSize):
310
311 dialog = wx.DirDialog(parent, message, path, style, pos, size)
312 result = DialogResults(dialog.ShowModal())
313 if result.accepted:
314 result.path = dialog.GetPath()
315 else:
316 result.path = None
317 dialog.Destroy()
318 return result
319
320 directoryDialog = dirDialog
321
322
323 def singleChoiceDialog(parent=None, message='', title='', lst=[],
324 style=wx.OK | wx.CANCEL | wx.CENTRE):
325 dialog = wx.SingleChoiceDialog(parent, message, title, lst, style)
326 result = DialogResults(dialog.ShowModal())
327 result.selection = dialog.GetStringSelection()
328 dialog.Destroy()
329 return result
330
331
332 def multipleChoiceDialog(parent=None, message='', title='', lst=[], pos=wx.DefaultPosition,
333 size=(200,200)):
334
335 dialog = MultipleChoiceDialog(parent, message, title, lst, pos, size)
336 result = DialogResults(dialog.ShowModal())
337 result.selection = dialog.GetValueString()
338 dialog.Destroy()
339 return result
340
341
342 if __name__ == '__main__':
343 #import os
344 #print os.getpid()
345
346 class MyApp(wx.App):
347
348 def OnInit(self):
349 self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 200))
350 panel = wx.Panel(frame, -1)
351 self.panel = panel
352
353
354 dialogNames = [
355 'alertDialog',
356 'colorDialog',
357 'directoryDialog',
358 'fileDialog',
359 'findDialog',
360 'fontDialog',
361 'messageDialog',
362 'multipleChoiceDialog',
363 'openFileDialog',
364 'saveFileDialog',
365 'scrolledMessageDialog',
366 'singleChoiceDialog',
367 'textEntryDialog',
368 ]
369
370 self.nameList = wx.ListBox(panel, -1,
371 size=(130, 180),
372 choices=dialogNames,
373 style=wx.LB_SINGLE)
374 self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList)
375
376 tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE
377 self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle)
378
379 sizer = wx.BoxSizer(wx.HORIZONTAL)
380 sizer.Add(self.nameList, 0, wx.EXPAND|wx.ALL, 20)
381 sizer.Add(self.text1, 1, wx.EXPAND|wx.ALL, 20)
382
383 panel.SetSizer(sizer)
384
385 self.SetTopWindow(frame)
386 frame.Show(1)
387 return 1
388
389
390 def OnNameListSelected(self, evt):
391 import pprint
392 sel = evt.GetString()
393 result = None
394 if sel == 'alertDialog':
395 result = alertDialog(message='Danger Will Robinson')
396 elif sel == 'colorDialog':
397 result = colorDialog()
398 elif sel == 'directoryDialog':
399 result = directoryDialog()
400 elif sel == 'fileDialog':
401 wildcard = "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
402 result = fileDialog(None, 'Open', '', '', wildcard)
403 elif sel == 'findDialog':
404 result = findDialog()
405 elif sel == 'fontDialog':
406 result = fontDialog()
407 elif sel == 'messageDialog':
408 result = messageDialog(None, 'Hello from Python and wxPython!',
409 'A Message Box', wx.OK | wx.ICON_INFORMATION)
410 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
411 #result = messageDialog(None, 'message', 'title')
412 elif sel == 'multipleChoiceDialog':
413 result = multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
414 elif sel == 'openFileDialog':
415 result = openFileDialog()
416 elif sel == 'saveFileDialog':
417 result = saveFileDialog()
418 elif sel == 'scrolledMessageDialog':
419 msg = "Can't find the file dialog.py"
420 try:
421 # read this source file and then display it
422 import sys
423 filename = sys.argv[-1]
424 fp = open(filename)
425 message = fp.read()
426 fp.close()
427 except:
428 pass
429 result = scrolledMessageDialog(None, message, filename)
430 elif sel == 'singleChoiceDialog':
431 result = singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
432 elif sel == 'textEntryDialog':
433 result = textEntryDialog(None, "message", "title", "text")
434
435 if result:
436 #self.text1.SetValue(pprint.pformat(result.__dict__))
437 self.text1.SetValue(str(result))
438
439 app = MyApp(True)
440 app.MainLoop()
441
442