blob: 12dbf973ac68e98c283a24dcc7ec3ee351067fcc [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 Jansend61f92b1999-01-22 13:14:06 +000078 d.SelectDialogItemText(4, 0, 999)
Jack Jansene4b40381995-07-17 13:25:15 +000079# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000080 d.SetDialogDefaultItem(1)
81 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000082 while 1:
83 n = ModalDialog(None)
84 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000085 tp, h, rect = d.GetDialogItem(4)
Jack Jansena5a49811998-07-01 15:47:44 +000086 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000087 if n == 2: return None
88
89
Jack Jansen3f5aef71997-06-20 16:23:37 +000090def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=258):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000091## """Display a QUESTION string which can be answered with Yes or No.
92##
93## Return 1 when the user clicks the Yes button.
94## Return 0 when the user clicks the No button.
95## Return -1 when the user clicks the Cancel button.
96##
97## When the user presses Return, the DEFAULT value is returned.
98## If omitted, this is 0 (No).
99##
100## The QUESTION strign ca be at most 255 characters.
101## """
102
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000103 d = GetNewDialog(id, -1)
104 if not d:
105 print "Can't get DLOG resource with id =", id
106 return
107 # Button assignments:
108 # 1 = default (invisible)
109 # 2 = Yes
110 # 3 = No
111 # 4 = Cancel
112 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +0000113 tp, h, rect = d.GetDialogItem(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000114 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000115 if yes != None:
116 tp, h, rect = d.GetDialogItem(2)
117 h.as_Control().SetControlTitle(yes)
118 if no != None:
119 tp, h, rect = d.GetDialogItem(3)
120 h.as_Control().SetControlTitle(no)
121 if cancel != None:
122 tp, h, rect = d.GetDialogItem(4)
123 h.as_Control().SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000124 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000125 if default == 1:
126 d.SetDialogDefaultItem(2)
127 elif default == 0:
128 d.SetDialogDefaultItem(3)
129 elif default == -1:
130 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000131 while 1:
132 n = ModalDialog(None)
133 if n == 1: return default
134 if n == 2: return 1
135 if n == 3: return 0
136 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000137
138
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000139
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000140
141screenbounds = Qd.qd.screenBits.bounds
142screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
143 screenbounds[2]-4, screenbounds[3]-4
144
145
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000146class ProgressBar:
Jack Jansen3f5aef71997-06-20 16:23:37 +0000147 def __init__(self, title="Working...", maxval=100, label="", id=259):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000148 self.maxval = maxval
149 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000150 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000151 self.title(title)
152 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000153 self._update(0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000154
155 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000156 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000157 self.d.HideWindow()
158 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000159
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000160 def title(self, newstr=""):
161 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000162 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000163 w = self.d.GetDialogWindow()
164 w.SetWTitle(newstr)
165
166 def label( self, *newstr ):
167 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000168 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000169 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000170 self._label = lf2cr(newstr[0])
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000171 tp, text_h, rect = self.d.GetDialogItem(2)
172 SetDialogItemText(text_h, self._label)
173
174
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000175 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000176 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000177 tp, h, bar_rect = self.d.GetDialogItem(3)
178 Qd.SetPort(self.d)
179
180 Qd.FrameRect(bar_rect) # Draw outline
181
182 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000183 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000184
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000185 Qd.ForeColor(QuickDraw.blackColor)
186 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000187 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
188
189 Qd.ForeColor(QuickDraw.whiteColor)
190 Qd.BackColor(QuickDraw.whiteColor)
191 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
192
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000193 # Restore settings
194 Qd.ForeColor(QuickDraw.blackColor)
195 Qd.BackColor(QuickDraw.whiteColor)
196
197 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000198
199 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
200 if ready :
201 what,msg,when,where,mod = ev
202 part = Win.FindWindow(where)[0]
203 if Dlg.IsDialogEvent(ev):
204 ds = Dlg.DialogSelect(ev)
205 if ds[0] and ds[1] == self.d and ds[-1] == 1:
206 raise KeyboardInterrupt, ev
207 else:
208 if part == 4: # inDrag
209 self.d.DragWindow(where, screenbounds)
210 else:
211 MacOS.HandleEvent(ev)
212
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000213
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000214 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000215 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000216 if value < 0: value = 0
217 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000218 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000219 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000220
221 def inc(self, n=1):
222 """inc(amt) - Increment progress bar position"""
223 self.set(self.curval + n)
224
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000225def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000226 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000227
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000228 Message("Testing EasyDialogs.")
229 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen3f5aef71997-06-20 16:23:37 +0000230 ok = AskYesNoCancel("Do you want to identify?", yes="Indentify", no="Don't identify")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000231 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000232 s = AskString("Enter your first name", "Joe")
Jack Jansena5a49811998-07-01 15:47:44 +0000233 Message("Thank you,\n%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000234 text = ( "Working Hard...", "Hardly Working..." ,
235 "So far, so good!", "Keep on truckin'" )
236 bar = ProgressBar("Progress, progress...", 100)
237 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000238 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000239 for i in range(100):
240 bar.set(i)
241 time.sleep(0.1)
242 if i % 10 == 0:
243 bar.label(text[(i/10) % 4])
244 bar.label("Done.")
245 time.sleep(0.3) # give'em a chance to see the done.
246 finally:
247 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000248 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000249
250
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000251
252
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000253if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000254 try:
255 test()
256 except KeyboardInterrupt:
257 Message("Operation Canceled.")
258