blob: 657433645d319a6d48056ccb401e1ba7bb8c8b51 [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)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000058 while 1:
59 n = ModalDialog(None)
60 if n == 1:
61 return
62
63
Jack Jansencc386881999-12-12 22:57:51 +000064def AskString(prompt, default = "", id=261, ok=None, cancel=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000065 """Display a PROMPT string and a text entry field with a DEFAULT string.
66
67 Return the contents of the text entry field when the user clicks the
68 OK button or presses Return.
69 Return None when the user clicks the Cancel button.
70
71 If omitted, DEFAULT is empty.
72
73 The PROMPT and DEFAULT strings, as well as the return value,
74 can be at most 255 characters long.
75 """
76
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000077 d = GetNewDialog(id, -1)
78 if not d:
79 print "Can't get DLOG resource with id =", id
80 return
Jack Jansencc386881999-12-12 22:57:51 +000081 h = d.GetDialogItemAsControl(3)
Jack Jansena5a49811998-07-01 15:47:44 +000082 SetDialogItemText(h, lf2cr(prompt))
Jack Jansencc386881999-12-12 22:57:51 +000083 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +000084 SetDialogItemText(h, lf2cr(default))
Jack Jansend61f92b1999-01-22 13:14:06 +000085 d.SelectDialogItemText(4, 0, 999)
Jack Jansene4b40381995-07-17 13:25:15 +000086# d.SetDialogItem(4, 0, 255)
Jack Jansen208c15a1999-02-16 16:06:39 +000087 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000088 h = d.GetDialogItemAsControl(1)
89 h.SetControlTitle(ok)
Jack Jansen208c15a1999-02-16 16:06:39 +000090 if cancel != None:
Jack Jansencc386881999-12-12 22:57:51 +000091 h = d.GetDialogItemAsControl(2)
92 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000093 d.SetDialogDefaultItem(1)
94 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000095 while 1:
96 n = ModalDialog(None)
97 if n == 1:
Jack Jansencc386881999-12-12 22:57:51 +000098 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +000099 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000100 if n == 2: return None
101
Jack Jansen60429e01999-12-13 16:07:01 +0000102def AskPassword(prompt, default='', id=264, ok=None, cancel=None):
Jack Jansenb92268a1999-02-10 22:38:44 +0000103 """Display a PROMPT string and a text entry field with a DEFAULT string.
104 The string is displayed as bullets only.
105
106 Return the contents of the text entry field when the user clicks the
107 OK button or presses Return.
108 Return None when the user clicks the Cancel button.
109
110 If omitted, DEFAULT is empty.
111
112 The PROMPT and DEFAULT strings, as well as the return value,
113 can be at most 255 characters long.
114 """
115 d = GetNewDialog(id, -1)
116 if not d:
117 print "Can't get DLOG resource with id =", id
118 return
Jack Jansen60429e01999-12-13 16:07:01 +0000119 h = d.GetDialogItemAsControl(3)
Jack Jansenb92268a1999-02-10 22:38:44 +0000120 SetDialogItemText(h, lf2cr(prompt))
Jack Jansen60429e01999-12-13 16:07:01 +0000121 pwd = d.GetDialogItemAsControl(4)
Jack Jansenb92268a1999-02-10 22:38:44 +0000122 bullets = '\245'*len(default)
Jack Jansen60429e01999-12-13 16:07:01 +0000123## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
124 SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default)
125 d.SelectDialogItemText(4, 0, 999)
126 Ctl.SetKeyboardFocus(d, pwd, kControlEditTextPart)
127 if ok != None:
128 h = d.GetDialogItemAsControl(1)
129 h.SetControlTitle(ok)
130 if cancel != None:
131 h = d.GetDialogItemAsControl(2)
132 h.SetControlTitle(cancel)
Jack Jansenb92268a1999-02-10 22:38:44 +0000133 d.SetDialogDefaultItem(Dialogs.ok)
134 d.SetDialogCancelItem(Dialogs.cancel)
Jack Jansenb92268a1999-02-10 22:38:44 +0000135 while 1:
Jack Jansen60429e01999-12-13 16:07:01 +0000136 n = ModalDialog(None)
137 if n == 1:
138 h = d.GetDialogItemAsControl(4)
139 return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag))
140 if n == 2: return None
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000141
Jack Jansencc386881999-12-12 22:57:51 +0000142def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262):
Jack Jansencf2efc61999-02-25 22:05:45 +0000143 """Display a QUESTION string which can be answered with Yes or No.
144
145 Return 1 when the user clicks the Yes button.
146 Return 0 when the user clicks the No button.
147 Return -1 when the user clicks the Cancel button.
148
149 When the user presses Return, the DEFAULT value is returned.
150 If omitted, this is 0 (No).
151
152 The QUESTION strign ca be at most 255 characters.
153 """
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000154
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000155 d = GetNewDialog(id, -1)
156 if not d:
157 print "Can't get DLOG resource with id =", id
158 return
159 # Button assignments:
160 # 1 = default (invisible)
161 # 2 = Yes
162 # 3 = No
163 # 4 = Cancel
164 # The question string is item 5
Jack Jansencc386881999-12-12 22:57:51 +0000165 h = d.GetDialogItemAsControl(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000166 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000167 if yes != None:
Jack Jansencc386881999-12-12 22:57:51 +0000168 h = d.GetDialogItemAsControl(2)
169 h.SetControlTitle(yes)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000170 if no != None:
Jack Jansencc386881999-12-12 22:57:51 +0000171 h = d.GetDialogItemAsControl(3)
172 h.SetControlTitle(no)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000173 if cancel != None:
Jack Jansen208c15a1999-02-16 16:06:39 +0000174 if cancel == '':
175 d.HideDialogItem(4)
176 else:
Jack Jansencc386881999-12-12 22:57:51 +0000177 h = d.GetDialogItemAsControl(4)
178 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000179 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000180 if default == 1:
181 d.SetDialogDefaultItem(2)
182 elif default == 0:
183 d.SetDialogDefaultItem(3)
184 elif default == -1:
185 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000186 while 1:
187 n = ModalDialog(None)
188 if n == 1: return default
189 if n == 2: return 1
190 if n == 3: return 0
191 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000192
193
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000194
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000195
196screenbounds = Qd.qd.screenBits.bounds
197screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
198 screenbounds[2]-4, screenbounds[3]-4
199
200
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000201class ProgressBar:
Jack Jansencc386881999-12-12 22:57:51 +0000202 def __init__(self, title="Working...", maxval=100, label="", id=263):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000203 self.maxval = maxval
204 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000205 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000206 self.title(title)
207 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000208 self._update(0)
Jack Jansencc386881999-12-12 22:57:51 +0000209 self.d.DrawDialog()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000210
211 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000212 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000213 self.d.HideWindow()
214 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000215
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000216 def title(self, newstr=""):
217 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000218 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000219 w = self.d.GetDialogWindow()
220 w.SetWTitle(newstr)
221
222 def label( self, *newstr ):
223 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000224 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000225 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000226 self._label = lf2cr(newstr[0])
Jack Jansencc386881999-12-12 22:57:51 +0000227 text_h = self.d.GetDialogItemAsControl(2)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000228 SetDialogItemText(text_h, self._label)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000229
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000230 def _update(self, value):
Jack Jansen58fa8181999-11-05 15:53:10 +0000231 maxval = self.maxval
232 if maxval == 0:
233 # XXXX Quick fix. Should probably display an unknown duration
234 value = 0
235 maxval = 1
Jack Jansencc386881999-12-12 22:57:51 +0000236 if maxval > 32767:
237 value = int(value/(maxval/32767.0))
238 maxval = 32767
239 progbar = self.d.GetDialogItemAsControl(3)
240 progbar.SetControlMaximum(maxval)
241 progbar.SetControlValue(value)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000242 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000243
244 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
245 if ready :
246 what,msg,when,where,mod = ev
247 part = Win.FindWindow(where)[0]
248 if Dlg.IsDialogEvent(ev):
249 ds = Dlg.DialogSelect(ev)
250 if ds[0] and ds[1] == self.d and ds[-1] == 1:
251 raise KeyboardInterrupt, ev
252 else:
253 if part == 4: # inDrag
254 self.d.DragWindow(where, screenbounds)
255 else:
256 MacOS.HandleEvent(ev)
257
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000258
Jack Jansen58fa8181999-11-05 15:53:10 +0000259 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000260 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000261 if max != None:
262 self.maxval = max
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000263 if value < 0: value = 0
264 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000265 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000266 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000267
268 def inc(self, n=1):
269 """inc(amt) - Increment progress bar position"""
270 self.set(self.curval + n)
271
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000272def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000273 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000274
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000275 Message("Testing EasyDialogs.")
276 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000277 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000278 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000279 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000280 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
281 if not s2:
282 Message("%s has no secret nickname"%s)
283 else:
284 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000285 text = ( "Working Hard...", "Hardly Working..." ,
286 "So far, so good!", "Keep on truckin'" )
287 bar = ProgressBar("Progress, progress...", 100)
288 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000289 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000290 for i in range(100):
291 bar.set(i)
292 time.sleep(0.1)
293 if i % 10 == 0:
294 bar.label(text[(i/10) % 4])
295 bar.label("Done.")
296 time.sleep(0.3) # give'em a chance to see the done.
297 finally:
298 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000299 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000300
301
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000302
303
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000304if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000305 try:
306 test()
307 except KeyboardInterrupt:
308 Message("Operation Canceled.")
309