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