blob: f4cdbbf55d60daf0f947ceb85cb15b2a675c1afc [file] [log] [blame]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001"""Easy to use dialogs.
2
3Message(msg) -- display a message and an OK button.
4AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
Just van Rossumcdcc0f01999-02-15 00:04:05 +00005AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00006AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
Jack Jansen3a87f5b1995-11-14 10:13:49 +00007bar = Progress(label, maxvalue) -- Display a progress bar
8bar.set(value) -- Set value
Jack Jansen1d63d8c1997-05-12 15:44:14 +00009bar.inc( *amount ) -- increment value by amount (default=1)
10bar.label( *newlabel ) -- get or set text label.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000011
12More documentation in each function.
Jack Jansencc386881999-12-12 22:57:51 +000013This module uses DLOG resources 260 and on.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000014Based upon STDWIN dialogs with the same names and functions.
15"""
16
Jack Jansene4b40381995-07-17 13:25:15 +000017from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
Jack Jansen3a87f5b1995-11-14 10:13:49 +000018import Qd
Jack Jansen3a87f5b1995-11-14 10:13:49 +000019import QuickDraw
Jack Jansenb92268a1999-02-10 22:38:44 +000020import Dialogs
21import Windows
Jack Jansen1d63d8c1997-05-12 15:44:14 +000022import Dlg,Win,Evt,Events # sdm7g
Jack Jansen60429e01999-12-13 16:07:01 +000023import Ctl
Jack Jansena5a49811998-07-01 15:47:44 +000024import MacOS
25import string
Jack Jansen60429e01999-12-13 16:07:01 +000026from ControlAccessor import * # Also import Controls constants
Jack Jansena5a49811998-07-01 15:47:44 +000027
28def cr2lf(text):
29 if '\r' in text:
30 text = string.join(string.split(text, '\r'), '\n')
31 return text
32
33def lf2cr(text):
34 if '\n' in text:
35 text = string.join(string.split(text, '\n'), '\r')
Jack Jansend5af7bd1998-09-28 10:37:08 +000036 if len(text) > 253:
37 text = text[:253] + '\311'
Jack Jansena5a49811998-07-01 15:47:44 +000038 return text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000039
Jack Jansencc386881999-12-12 22:57:51 +000040def Message(msg, id=260, ok=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000041 """Display a MESSAGE string.
42
43 Return when the user clicks the OK button or presses Return.
44
45 The MESSAGE string can be at most 255 characters long.
46 """
47
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000048 d = GetNewDialog(id, -1)
49 if not d:
50 print "Can't get DLOG resource with id =", id
51 return
Jack Jansencc386881999-12-12 22:57:51 +000052 h = d.GetDialogItemAsControl(2)
Jack Jansena5a49811998-07-01 15:47:44 +000053 SetDialogItemText(h, lf2cr(msg))
Jack Jansen208c15a1999-02-16 16:06:39 +000054 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000055 h = d.GetDialogItemAsControl(1)
56 h.SetControlTitle(ok)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000057 d.SetDialogDefaultItem(1)
Jack Jansenfca049d2000-01-18 13:36:02 +000058 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +000059 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000060 while 1:
61 n = ModalDialog(None)
62 if n == 1:
63 return
64
65
Jack Jansencc386881999-12-12 22:57:51 +000066def AskString(prompt, default = "", id=261, ok=None, cancel=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000067 """Display a PROMPT string and a text entry field with a DEFAULT string.
68
69 Return the contents of the text entry field when the user clicks the
70 OK button or presses Return.
71 Return None when the user clicks the Cancel button.
72
73 If omitted, DEFAULT is empty.
74
75 The PROMPT and DEFAULT strings, as well as the return value,
76 can be at most 255 characters long.
77 """
78
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000079 d = GetNewDialog(id, -1)
80 if not d:
81 print "Can't get DLOG resource with id =", id
82 return
Jack Jansencc386881999-12-12 22:57:51 +000083 h = d.GetDialogItemAsControl(3)
Jack Jansena5a49811998-07-01 15:47:44 +000084 SetDialogItemText(h, lf2cr(prompt))
Jack Jansencc386881999-12-12 22:57:51 +000085 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +000086 SetDialogItemText(h, lf2cr(default))
Jack Jansend61f92b1999-01-22 13:14:06 +000087 d.SelectDialogItemText(4, 0, 999)
Jack Jansene4b40381995-07-17 13:25:15 +000088# d.SetDialogItem(4, 0, 255)
Jack Jansen208c15a1999-02-16 16:06:39 +000089 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000090 h = d.GetDialogItemAsControl(1)
91 h.SetControlTitle(ok)
Jack Jansen208c15a1999-02-16 16:06:39 +000092 if cancel != None:
Jack Jansencc386881999-12-12 22:57:51 +000093 h = d.GetDialogItemAsControl(2)
94 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000095 d.SetDialogDefaultItem(1)
96 d.SetDialogCancelItem(2)
Jack Jansenfca049d2000-01-18 13:36:02 +000097 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +000098 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000099 while 1:
100 n = ModalDialog(None)
101 if n == 1:
Jack Jansencc386881999-12-12 22:57:51 +0000102 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +0000103 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000104 if n == 2: return None
105
Jack Jansen60429e01999-12-13 16:07:01 +0000106def AskPassword(prompt, default='', id=264, ok=None, cancel=None):
Jack Jansenb92268a1999-02-10 22:38:44 +0000107 """Display a PROMPT string and a text entry field with a DEFAULT string.
108 The string is displayed as bullets only.
109
110 Return the contents of the text entry field when the user clicks the
111 OK button or presses Return.
112 Return None when the user clicks the Cancel button.
113
114 If omitted, DEFAULT is empty.
115
116 The PROMPT and DEFAULT strings, as well as the return value,
117 can be at most 255 characters long.
118 """
119 d = GetNewDialog(id, -1)
120 if not d:
121 print "Can't get DLOG resource with id =", id
122 return
Jack Jansen60429e01999-12-13 16:07:01 +0000123 h = d.GetDialogItemAsControl(3)
Jack Jansenb92268a1999-02-10 22:38:44 +0000124 SetDialogItemText(h, lf2cr(prompt))
Jack Jansen60429e01999-12-13 16:07:01 +0000125 pwd = d.GetDialogItemAsControl(4)
Jack Jansenb92268a1999-02-10 22:38:44 +0000126 bullets = '\245'*len(default)
Jack Jansen60429e01999-12-13 16:07:01 +0000127## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
128 SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default)
129 d.SelectDialogItemText(4, 0, 999)
Jack Jansen0c1836f2000-08-25 22:06:19 +0000130 Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart)
Jack Jansen60429e01999-12-13 16:07:01 +0000131 if ok != None:
132 h = d.GetDialogItemAsControl(1)
133 h.SetControlTitle(ok)
134 if cancel != None:
135 h = d.GetDialogItemAsControl(2)
136 h.SetControlTitle(cancel)
Jack Jansenb92268a1999-02-10 22:38:44 +0000137 d.SetDialogDefaultItem(Dialogs.ok)
138 d.SetDialogCancelItem(Dialogs.cancel)
Jack Jansenfca049d2000-01-18 13:36:02 +0000139 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000140 d.GetDialogWindow().ShowWindow()
Jack Jansenb92268a1999-02-10 22:38:44 +0000141 while 1:
Jack Jansen60429e01999-12-13 16:07:01 +0000142 n = ModalDialog(None)
143 if n == 1:
144 h = d.GetDialogItemAsControl(4)
145 return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag))
146 if n == 2: return None
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000147
Jack Jansencc386881999-12-12 22:57:51 +0000148def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262):
Jack Jansencf2efc61999-02-25 22:05:45 +0000149 """Display a QUESTION string which can be answered with Yes or No.
150
151 Return 1 when the user clicks the Yes button.
152 Return 0 when the user clicks the No button.
153 Return -1 when the user clicks the Cancel button.
154
155 When the user presses Return, the DEFAULT value is returned.
156 If omitted, this is 0 (No).
157
158 The QUESTION strign ca be at most 255 characters.
159 """
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000160
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000161 d = GetNewDialog(id, -1)
162 if not d:
163 print "Can't get DLOG resource with id =", id
164 return
165 # Button assignments:
166 # 1 = default (invisible)
167 # 2 = Yes
168 # 3 = No
169 # 4 = Cancel
170 # The question string is item 5
Jack Jansencc386881999-12-12 22:57:51 +0000171 h = d.GetDialogItemAsControl(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000172 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000173 if yes != None:
Jack Jansen85743782000-02-10 16:15:53 +0000174 if yes == '':
175 d.HideDialogItem(2)
176 else:
177 h = d.GetDialogItemAsControl(2)
178 h.SetControlTitle(yes)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000179 if no != None:
Jack Jansen85743782000-02-10 16:15:53 +0000180 if no == '':
181 d.HideDialogItem(3)
182 else:
183 h = d.GetDialogItemAsControl(3)
184 h.SetControlTitle(no)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000185 if cancel != None:
Jack Jansen208c15a1999-02-16 16:06:39 +0000186 if cancel == '':
187 d.HideDialogItem(4)
188 else:
Jack Jansencc386881999-12-12 22:57:51 +0000189 h = d.GetDialogItemAsControl(4)
190 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000191 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000192 if default == 1:
193 d.SetDialogDefaultItem(2)
194 elif default == 0:
195 d.SetDialogDefaultItem(3)
196 elif default == -1:
197 d.SetDialogDefaultItem(4)
Jack Jansenfca049d2000-01-18 13:36:02 +0000198 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000199 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000200 while 1:
201 n = ModalDialog(None)
202 if n == 1: return default
203 if n == 2: return 1
204 if n == 3: return 0
205 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000206
207
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000208
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000209
210screenbounds = Qd.qd.screenBits.bounds
211screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
212 screenbounds[2]-4, screenbounds[3]-4
213
214
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000215class ProgressBar:
Jack Jansencc386881999-12-12 22:57:51 +0000216 def __init__(self, title="Working...", maxval=100, label="", id=263):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000217 self.maxval = maxval
218 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000219 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000220 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000221 self._update(0)
Jack Jansenfca049d2000-01-18 13:36:02 +0000222 self.d.AutoSizeDialog()
Jack Jansenab48e902000-07-24 14:07:15 +0000223 self.title(title)
Jack Jansen0c1836f2000-08-25 22:06:19 +0000224 self.d.GetDialogWindow().ShowWindow()
Jack Jansencc386881999-12-12 22:57:51 +0000225 self.d.DrawDialog()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000226
227 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000228 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000229 self.d.HideWindow()
230 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000231
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000232 def title(self, newstr=""):
233 """title(text) - Set title of progress window"""
234 w = self.d.GetDialogWindow()
Jack Jansenab48e902000-07-24 14:07:15 +0000235 w.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000236 w.SetWTitle(newstr)
237
238 def label( self, *newstr ):
239 """label(text) - Set text in progress box"""
Jack Jansenab48e902000-07-24 14:07:15 +0000240 self.d.GetDialogWindow().BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000241 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000242 self._label = lf2cr(newstr[0])
Jack Jansencc386881999-12-12 22:57:51 +0000243 text_h = self.d.GetDialogItemAsControl(2)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000244 SetDialogItemText(text_h, self._label)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000245
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000246 def _update(self, value):
Jack Jansen58fa8181999-11-05 15:53:10 +0000247 maxval = self.maxval
248 if maxval == 0:
249 # XXXX Quick fix. Should probably display an unknown duration
250 value = 0
251 maxval = 1
Jack Jansencc386881999-12-12 22:57:51 +0000252 if maxval > 32767:
253 value = int(value/(maxval/32767.0))
254 maxval = 32767
255 progbar = self.d.GetDialogItemAsControl(3)
256 progbar.SetControlMaximum(maxval)
257 progbar.SetControlValue(value)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000258 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000259
260 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
261 if ready :
262 what,msg,when,where,mod = ev
263 part = Win.FindWindow(where)[0]
264 if Dlg.IsDialogEvent(ev):
265 ds = Dlg.DialogSelect(ev)
266 if ds[0] and ds[1] == self.d and ds[-1] == 1:
267 raise KeyboardInterrupt, ev
268 else:
269 if part == 4: # inDrag
270 self.d.DragWindow(where, screenbounds)
271 else:
272 MacOS.HandleEvent(ev)
273
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000274
Jack Jansen58fa8181999-11-05 15:53:10 +0000275 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000276 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000277 if max != None:
278 self.maxval = max
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000279 if value < 0: value = 0
280 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000281 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000282 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000283
284 def inc(self, n=1):
285 """inc(amt) - Increment progress bar position"""
286 self.set(self.curval + n)
287
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000288def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000289 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000290
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000291 Message("Testing EasyDialogs.")
292 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000293 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000294 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000295 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000296 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
297 if not s2:
298 Message("%s has no secret nickname"%s)
299 else:
300 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000301 text = ( "Working Hard...", "Hardly Working..." ,
302 "So far, so good!", "Keep on truckin'" )
303 bar = ProgressBar("Progress, progress...", 100)
304 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000305 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000306 for i in range(100):
307 bar.set(i)
308 time.sleep(0.1)
309 if i % 10 == 0:
310 bar.label(text[(i/10) % 4])
311 bar.label("Done.")
312 time.sleep(0.3) # give'em a chance to see the done.
313 finally:
314 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000315 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000316
317
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000318
319
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000320if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000321 try:
322 test()
323 except KeyboardInterrupt:
324 Message("Operation Canceled.")
325