blob: 742daa42329f9bd229ae9995f748108d6fc294cd [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
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000020
21def Message(msg):
22 """Display a MESSAGE string.
23
24 Return when the user clicks the OK button or presses Return.
25
26 The MESSAGE string can be at most 255 characters long.
27 """
28
29 id = 256
30 d = GetNewDialog(id, -1)
31 if not d:
32 print "Can't get DLOG resource with id =", id
33 return
Jack Jansene4b40381995-07-17 13:25:15 +000034 tp, h, rect = d.GetDialogItem(2)
35 SetDialogItemText(h, msg)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000036 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000037 while 1:
38 n = ModalDialog(None)
39 if n == 1:
40 return
41
42
43def AskString(prompt, default = ""):
44 """Display a PROMPT string and a text entry field with a DEFAULT string.
45
46 Return the contents of the text entry field when the user clicks the
47 OK button or presses Return.
48 Return None when the user clicks the Cancel button.
49
50 If omitted, DEFAULT is empty.
51
52 The PROMPT and DEFAULT strings, as well as the return value,
53 can be at most 255 characters long.
54 """
55
56 id = 257
57 d = GetNewDialog(id, -1)
58 if not d:
59 print "Can't get DLOG resource with id =", id
60 return
Jack Jansene4b40381995-07-17 13:25:15 +000061 tp, h, rect = d.GetDialogItem(3)
62 SetDialogItemText(h, prompt)
63 tp, h, rect = d.GetDialogItem(4)
64 SetDialogItemText(h, default)
65# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000066 d.SetDialogDefaultItem(1)
67 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000068 while 1:
69 n = ModalDialog(None)
70 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000071 tp, h, rect = d.GetDialogItem(4)
72 return GetDialogItemText(h)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000073 if n == 2: return None
74
75
76def AskYesNoCancel(question, default = 0):
77## """Display a QUESTION string which can be answered with Yes or No.
78##
79## Return 1 when the user clicks the Yes button.
80## Return 0 when the user clicks the No button.
81## Return -1 when the user clicks the Cancel button.
82##
83## When the user presses Return, the DEFAULT value is returned.
84## If omitted, this is 0 (No).
85##
86## The QUESTION strign ca be at most 255 characters.
87## """
88
89 id = 258
90 d = GetNewDialog(id, -1)
91 if not d:
92 print "Can't get DLOG resource with id =", id
93 return
94 # Button assignments:
95 # 1 = default (invisible)
96 # 2 = Yes
97 # 3 = No
98 # 4 = Cancel
99 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +0000100 tp, h, rect = d.GetDialogItem(5)
101 SetDialogItemText(h, question)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000102 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000103 if default == 1:
104 d.SetDialogDefaultItem(2)
105 elif default == 0:
106 d.SetDialogDefaultItem(3)
107 elif default == -1:
108 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000109 while 1:
110 n = ModalDialog(None)
111 if n == 1: return default
112 if n == 2: return 1
113 if n == 3: return 0
114 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000115
116
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000117
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000118
119screenbounds = Qd.qd.screenBits.bounds
120screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
121 screenbounds[2]-4, screenbounds[3]-4
122
123
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000124class ProgressBar:
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000125 def __init__(self, title="Working...", maxval=100, label=""):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000126 self.maxval = maxval
127 self.curval = -1
128 self.d = GetNewDialog(259, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000129 self.title(title)
130 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000131 self._update(0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000132
133 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000134 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000135 self.d.HideWindow()
136 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000137
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000138 def title(self, newstr=""):
139 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000140 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000141 w = self.d.GetDialogWindow()
142 w.SetWTitle(newstr)
143
144 def label( self, *newstr ):
145 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000146 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000147 if newstr:
148 self._label = newstr[0]
149 tp, text_h, rect = self.d.GetDialogItem(2)
150 SetDialogItemText(text_h, self._label)
151
152
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000153 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000154 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000155 tp, h, bar_rect = self.d.GetDialogItem(3)
156 Qd.SetPort(self.d)
157
158 Qd.FrameRect(bar_rect) # Draw outline
159
160 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000161 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000162
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000163 Qd.ForeColor(QuickDraw.blackColor)
164 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000165 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
166
167 Qd.ForeColor(QuickDraw.whiteColor)
168 Qd.BackColor(QuickDraw.whiteColor)
169 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
170
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000171 # Restore settings
172 Qd.ForeColor(QuickDraw.blackColor)
173 Qd.BackColor(QuickDraw.whiteColor)
174
175 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000176
177 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
178 if ready :
179 what,msg,when,where,mod = ev
180 part = Win.FindWindow(where)[0]
181 if Dlg.IsDialogEvent(ev):
182 ds = Dlg.DialogSelect(ev)
183 if ds[0] and ds[1] == self.d and ds[-1] == 1:
184 raise KeyboardInterrupt, ev
185 else:
186 if part == 4: # inDrag
187 self.d.DragWindow(where, screenbounds)
188 else:
189 MacOS.HandleEvent(ev)
190
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000191
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000192 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000193 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000194 if value < 0: value = 0
195 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000196 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000197 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000198
199 def inc(self, n=1):
200 """inc(amt) - Increment progress bar position"""
201 self.set(self.curval + n)
202
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000203def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000204 import time
205 import MacOS
206
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000207 Message("Testing EasyDialogs.")
208 ok = AskYesNoCancel("Do you want to proceed?")
209 if ok > 0:
210 s = AskString("Enter your first name")
211 Message("Thank you,\015%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000212 text = ( "Working Hard...", "Hardly Working..." ,
213 "So far, so good!", "Keep on truckin'" )
214 bar = ProgressBar("Progress, progress...", 100)
215 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000216 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000217 for i in range(100):
218 bar.set(i)
219 time.sleep(0.1)
220 if i % 10 == 0:
221 bar.label(text[(i/10) % 4])
222 bar.label("Done.")
223 time.sleep(0.3) # give'em a chance to see the done.
224 finally:
225 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000226 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000227
228
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000229
230
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000231if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000232 try:
233 test()
234 except KeyboardInterrupt:
235 Message("Operation Canceled.")
236