blob: 7e94fee1ff71bce185c8fd9666ccd28b88c5af7e [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.
5AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
Jack Jansen3a87f5b1995-11-14 10:13:49 +00006bar = Progress(label, maxvalue) -- Display a progress bar
7bar.set(value) -- Set value
Jack Jansen1d63d8c1997-05-12 15:44:14 +00008bar.inc( *amount ) -- increment value by amount (default=1)
9bar.label( *newlabel ) -- get or set text label.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000010
11More documentation in each function.
12This module uses DLOG resources 256, 257 and 258.
13Based upon STDWIN dialogs with the same names and functions.
14"""
15
Jack Jansene4b40381995-07-17 13:25:15 +000016from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
Jack Jansen3a87f5b1995-11-14 10:13:49 +000017import Qd
Jack Jansen3a87f5b1995-11-14 10:13:49 +000018import QuickDraw
Jack Jansen1d63d8c1997-05-12 15:44:14 +000019import Dlg,Win,Evt,Events # sdm7g
Jack Jansena5a49811998-07-01 15:47:44 +000020import MacOS
21import string
22
23def cr2lf(text):
24 if '\r' in text:
25 text = string.join(string.split(text, '\r'), '\n')
26 return text
27
28def lf2cr(text):
29 if '\n' in text:
30 text = string.join(string.split(text, '\n'), '\r')
Jack Jansend5af7bd1998-09-28 10:37:08 +000031 if len(text) > 253:
32 text = text[:253] + '\311'
Jack Jansena5a49811998-07-01 15:47:44 +000033 return text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000034
Jack Jansen3f5aef71997-06-20 16:23:37 +000035def Message(msg, id=256):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000036 """Display a MESSAGE string.
37
38 Return when the user clicks the OK button or presses Return.
39
40 The MESSAGE string can be at most 255 characters long.
41 """
42
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000043 d = GetNewDialog(id, -1)
44 if not d:
45 print "Can't get DLOG resource with id =", id
46 return
Jack Jansene4b40381995-07-17 13:25:15 +000047 tp, h, rect = d.GetDialogItem(2)
Jack Jansena5a49811998-07-01 15:47:44 +000048 SetDialogItemText(h, lf2cr(msg))
Jack Jansen3a87f5b1995-11-14 10:13:49 +000049 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000050 while 1:
51 n = ModalDialog(None)
52 if n == 1:
53 return
54
55
Jack Jansen3f5aef71997-06-20 16:23:37 +000056def AskString(prompt, default = "", id=257):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000057 """Display a PROMPT string and a text entry field with a DEFAULT string.
58
59 Return the contents of the text entry field when the user clicks the
60 OK button or presses Return.
61 Return None when the user clicks the Cancel button.
62
63 If omitted, DEFAULT is empty.
64
65 The PROMPT and DEFAULT strings, as well as the return value,
66 can be at most 255 characters long.
67 """
68
69 id = 257
70 d = GetNewDialog(id, -1)
71 if not d:
72 print "Can't get DLOG resource with id =", id
73 return
Jack Jansene4b40381995-07-17 13:25:15 +000074 tp, h, rect = d.GetDialogItem(3)
Jack Jansena5a49811998-07-01 15:47:44 +000075 SetDialogItemText(h, lf2cr(prompt))
Jack Jansene4b40381995-07-17 13:25:15 +000076 tp, h, rect = d.GetDialogItem(4)
Jack Jansena5a49811998-07-01 15:47:44 +000077 SetDialogItemText(h, lf2cr(default))
Jack Jansene4b40381995-07-17 13:25:15 +000078# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000079 d.SetDialogDefaultItem(1)
80 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000081 while 1:
82 n = ModalDialog(None)
83 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000084 tp, h, rect = d.GetDialogItem(4)
Jack Jansena5a49811998-07-01 15:47:44 +000085 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000086 if n == 2: return None
87
88
Jack Jansen3f5aef71997-06-20 16:23:37 +000089def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=258):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000090## """Display a QUESTION string which can be answered with Yes or No.
91##
92## Return 1 when the user clicks the Yes button.
93## Return 0 when the user clicks the No button.
94## Return -1 when the user clicks the Cancel button.
95##
96## When the user presses Return, the DEFAULT value is returned.
97## If omitted, this is 0 (No).
98##
99## The QUESTION strign ca be at most 255 characters.
100## """
101
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000102 d = GetNewDialog(id, -1)
103 if not d:
104 print "Can't get DLOG resource with id =", id
105 return
106 # Button assignments:
107 # 1 = default (invisible)
108 # 2 = Yes
109 # 3 = No
110 # 4 = Cancel
111 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +0000112 tp, h, rect = d.GetDialogItem(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000113 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000114 if yes != None:
115 tp, h, rect = d.GetDialogItem(2)
116 h.as_Control().SetControlTitle(yes)
117 if no != None:
118 tp, h, rect = d.GetDialogItem(3)
119 h.as_Control().SetControlTitle(no)
120 if cancel != None:
121 tp, h, rect = d.GetDialogItem(4)
122 h.as_Control().SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000123 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000124 if default == 1:
125 d.SetDialogDefaultItem(2)
126 elif default == 0:
127 d.SetDialogDefaultItem(3)
128 elif default == -1:
129 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000130 while 1:
131 n = ModalDialog(None)
132 if n == 1: return default
133 if n == 2: return 1
134 if n == 3: return 0
135 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000136
137
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000138
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000139
140screenbounds = Qd.qd.screenBits.bounds
141screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
142 screenbounds[2]-4, screenbounds[3]-4
143
144
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000145class ProgressBar:
Jack Jansen3f5aef71997-06-20 16:23:37 +0000146 def __init__(self, title="Working...", maxval=100, label="", id=259):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000147 self.maxval = maxval
148 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000149 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000150 self.title(title)
151 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000152 self._update(0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000153
154 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000155 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000156 self.d.HideWindow()
157 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000158
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000159 def title(self, newstr=""):
160 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000161 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000162 w = self.d.GetDialogWindow()
163 w.SetWTitle(newstr)
164
165 def label( self, *newstr ):
166 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000167 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000168 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000169 self._label = lf2cr(newstr[0])
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000170 tp, text_h, rect = self.d.GetDialogItem(2)
171 SetDialogItemText(text_h, self._label)
172
173
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000174 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000175 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000176 tp, h, bar_rect = self.d.GetDialogItem(3)
177 Qd.SetPort(self.d)
178
179 Qd.FrameRect(bar_rect) # Draw outline
180
181 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000182 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000183
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000184 Qd.ForeColor(QuickDraw.blackColor)
185 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000186 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
187
188 Qd.ForeColor(QuickDraw.whiteColor)
189 Qd.BackColor(QuickDraw.whiteColor)
190 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
191
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000192 # Restore settings
193 Qd.ForeColor(QuickDraw.blackColor)
194 Qd.BackColor(QuickDraw.whiteColor)
195
196 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000197
198 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
199 if ready :
200 what,msg,when,where,mod = ev
201 part = Win.FindWindow(where)[0]
202 if Dlg.IsDialogEvent(ev):
203 ds = Dlg.DialogSelect(ev)
204 if ds[0] and ds[1] == self.d and ds[-1] == 1:
205 raise KeyboardInterrupt, ev
206 else:
207 if part == 4: # inDrag
208 self.d.DragWindow(where, screenbounds)
209 else:
210 MacOS.HandleEvent(ev)
211
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000212
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000213 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000214 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000215 if value < 0: value = 0
216 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000217 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000218 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000219
220 def inc(self, n=1):
221 """inc(amt) - Increment progress bar position"""
222 self.set(self.curval + n)
223
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000224def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000225 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000226
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000227 Message("Testing EasyDialogs.")
228 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen3f5aef71997-06-20 16:23:37 +0000229 ok = AskYesNoCancel("Do you want to identify?", yes="Indentify", no="Don't identify")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000230 if ok > 0:
231 s = AskString("Enter your first name")
Jack Jansena5a49811998-07-01 15:47:44 +0000232 Message("Thank you,\n%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000233 text = ( "Working Hard...", "Hardly Working..." ,
234 "So far, so good!", "Keep on truckin'" )
235 bar = ProgressBar("Progress, progress...", 100)
236 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000237 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000238 for i in range(100):
239 bar.set(i)
240 time.sleep(0.1)
241 if i % 10 == 0:
242 bar.label(text[(i/10) % 4])
243 bar.label("Done.")
244 time.sleep(0.3) # give'em a chance to see the done.
245 finally:
246 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000247 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000248
249
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000250
251
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000252if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000253 try:
254 test()
255 except KeyboardInterrupt:
256 Message("Operation Canceled.")
257