blob: 1b3f5ba4342db9b81c9f1a516a37f5842ce8da21 [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')
31 return text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000032
Jack Jansen3f5aef71997-06-20 16:23:37 +000033def Message(msg, id=256):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000034 """Display a MESSAGE string.
35
36 Return when the user clicks the OK button or presses Return.
37
38 The MESSAGE string can be at most 255 characters long.
39 """
40
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000041 d = GetNewDialog(id, -1)
42 if not d:
43 print "Can't get DLOG resource with id =", id
44 return
Jack Jansene4b40381995-07-17 13:25:15 +000045 tp, h, rect = d.GetDialogItem(2)
Jack Jansena5a49811998-07-01 15:47:44 +000046 SetDialogItemText(h, lf2cr(msg))
Jack Jansen3a87f5b1995-11-14 10:13:49 +000047 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000048 while 1:
49 n = ModalDialog(None)
50 if n == 1:
51 return
52
53
Jack Jansen3f5aef71997-06-20 16:23:37 +000054def AskString(prompt, default = "", id=257):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000055 """Display a PROMPT string and a text entry field with a DEFAULT string.
56
57 Return the contents of the text entry field when the user clicks the
58 OK button or presses Return.
59 Return None when the user clicks the Cancel button.
60
61 If omitted, DEFAULT is empty.
62
63 The PROMPT and DEFAULT strings, as well as the return value,
64 can be at most 255 characters long.
65 """
66
67 id = 257
68 d = GetNewDialog(id, -1)
69 if not d:
70 print "Can't get DLOG resource with id =", id
71 return
Jack Jansene4b40381995-07-17 13:25:15 +000072 tp, h, rect = d.GetDialogItem(3)
Jack Jansena5a49811998-07-01 15:47:44 +000073 SetDialogItemText(h, lf2cr(prompt))
Jack Jansene4b40381995-07-17 13:25:15 +000074 tp, h, rect = d.GetDialogItem(4)
Jack Jansena5a49811998-07-01 15:47:44 +000075 SetDialogItemText(h, lf2cr(default))
Jack Jansene4b40381995-07-17 13:25:15 +000076# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000077 d.SetDialogDefaultItem(1)
78 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000079 while 1:
80 n = ModalDialog(None)
81 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000082 tp, h, rect = d.GetDialogItem(4)
Jack Jansena5a49811998-07-01 15:47:44 +000083 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000084 if n == 2: return None
85
86
Jack Jansen3f5aef71997-06-20 16:23:37 +000087def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=258):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000088## """Display a QUESTION string which can be answered with Yes or No.
89##
90## Return 1 when the user clicks the Yes button.
91## Return 0 when the user clicks the No button.
92## Return -1 when the user clicks the Cancel button.
93##
94## When the user presses Return, the DEFAULT value is returned.
95## If omitted, this is 0 (No).
96##
97## The QUESTION strign ca be at most 255 characters.
98## """
99
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000100 d = GetNewDialog(id, -1)
101 if not d:
102 print "Can't get DLOG resource with id =", id
103 return
104 # Button assignments:
105 # 1 = default (invisible)
106 # 2 = Yes
107 # 3 = No
108 # 4 = Cancel
109 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +0000110 tp, h, rect = d.GetDialogItem(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000111 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000112 if yes != None:
113 tp, h, rect = d.GetDialogItem(2)
114 h.as_Control().SetControlTitle(yes)
115 if no != None:
116 tp, h, rect = d.GetDialogItem(3)
117 h.as_Control().SetControlTitle(no)
118 if cancel != None:
119 tp, h, rect = d.GetDialogItem(4)
120 h.as_Control().SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000121 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000122 if default == 1:
123 d.SetDialogDefaultItem(2)
124 elif default == 0:
125 d.SetDialogDefaultItem(3)
126 elif default == -1:
127 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000128 while 1:
129 n = ModalDialog(None)
130 if n == 1: return default
131 if n == 2: return 1
132 if n == 3: return 0
133 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000134
135
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000136
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000137
138screenbounds = Qd.qd.screenBits.bounds
139screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
140 screenbounds[2]-4, screenbounds[3]-4
141
142
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000143class ProgressBar:
Jack Jansen3f5aef71997-06-20 16:23:37 +0000144 def __init__(self, title="Working...", maxval=100, label="", id=259):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000145 self.maxval = maxval
146 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000147 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000148 self.title(title)
149 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000150 self._update(0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000151
152 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000153 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000154 self.d.HideWindow()
155 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000156
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000157 def title(self, newstr=""):
158 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000159 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000160 w = self.d.GetDialogWindow()
161 w.SetWTitle(newstr)
162
163 def label( self, *newstr ):
164 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000165 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000166 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000167 self._label = lf2cr(newstr[0])
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000168 tp, text_h, rect = self.d.GetDialogItem(2)
169 SetDialogItemText(text_h, self._label)
170
171
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000172 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000173 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000174 tp, h, bar_rect = self.d.GetDialogItem(3)
175 Qd.SetPort(self.d)
176
177 Qd.FrameRect(bar_rect) # Draw outline
178
179 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000180 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000181
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000182 Qd.ForeColor(QuickDraw.blackColor)
183 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000184 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
185
186 Qd.ForeColor(QuickDraw.whiteColor)
187 Qd.BackColor(QuickDraw.whiteColor)
188 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
189
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000190 # Restore settings
191 Qd.ForeColor(QuickDraw.blackColor)
192 Qd.BackColor(QuickDraw.whiteColor)
193
194 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000195
196 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
197 if ready :
198 what,msg,when,where,mod = ev
199 part = Win.FindWindow(where)[0]
200 if Dlg.IsDialogEvent(ev):
201 ds = Dlg.DialogSelect(ev)
202 if ds[0] and ds[1] == self.d and ds[-1] == 1:
203 raise KeyboardInterrupt, ev
204 else:
205 if part == 4: # inDrag
206 self.d.DragWindow(where, screenbounds)
207 else:
208 MacOS.HandleEvent(ev)
209
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000210
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000211 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000212 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000213 if value < 0: value = 0
214 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000215 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000216 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000217
218 def inc(self, n=1):
219 """inc(amt) - Increment progress bar position"""
220 self.set(self.curval + n)
221
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000222def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000223 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000224
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000225 Message("Testing EasyDialogs.")
226 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen3f5aef71997-06-20 16:23:37 +0000227 ok = AskYesNoCancel("Do you want to identify?", yes="Indentify", no="Don't identify")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000228 if ok > 0:
229 s = AskString("Enter your first name")
Jack Jansena5a49811998-07-01 15:47:44 +0000230 Message("Thank you,\n%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000231 text = ( "Working Hard...", "Hardly Working..." ,
232 "So far, so good!", "Keep on truckin'" )
233 bar = ProgressBar("Progress, progress...", 100)
234 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000235 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000236 for i in range(100):
237 bar.set(i)
238 time.sleep(0.1)
239 if i % 10 == 0:
240 bar.label(text[(i/10) % 4])
241 bar.label("Done.")
242 time.sleep(0.3) # give'em a chance to see the done.
243 finally:
244 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000245 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000246
247
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000248
249
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000250if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000251 try:
252 test()
253 except KeyboardInterrupt:
254 Message("Operation Canceled.")
255