blob: c585d91a61aa4646a248a9956855ac5621bebeb1 [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 ):
134 self.d.HideWindow()
135 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000136
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000137 def title(self, newstr=""):
138 """title(text) - Set title of progress window"""
139 w = self.d.GetDialogWindow()
140 w.SetWTitle(newstr)
141
142 def label( self, *newstr ):
143 """label(text) - Set text in progress box"""
144 if newstr:
145 self._label = newstr[0]
146 tp, text_h, rect = self.d.GetDialogItem(2)
147 SetDialogItemText(text_h, self._label)
148
149
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000150 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000151 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000152 tp, h, bar_rect = self.d.GetDialogItem(3)
153 Qd.SetPort(self.d)
154
155 Qd.FrameRect(bar_rect) # Draw outline
156
157 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000158 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000159
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000160 Qd.ForeColor(QuickDraw.blackColor)
161 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000162 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
163
164 Qd.ForeColor(QuickDraw.whiteColor)
165 Qd.BackColor(QuickDraw.whiteColor)
166 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
167
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000168 # Restore settings
169 Qd.ForeColor(QuickDraw.blackColor)
170 Qd.BackColor(QuickDraw.whiteColor)
171
172 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000173
174 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
175 if ready :
176 what,msg,when,where,mod = ev
177 part = Win.FindWindow(where)[0]
178 if Dlg.IsDialogEvent(ev):
179 ds = Dlg.DialogSelect(ev)
180 if ds[0] and ds[1] == self.d and ds[-1] == 1:
181 raise KeyboardInterrupt, ev
182 else:
183 if part == 4: # inDrag
184 self.d.DragWindow(where, screenbounds)
185 else:
186 MacOS.HandleEvent(ev)
187
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000188
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000189 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000190 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000191 if value < 0: value = 0
192 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000193 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000194 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000195
196 def inc(self, n=1):
197 """inc(amt) - Increment progress bar position"""
198 self.set(self.curval + n)
199
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000200def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000201 import time
202 import MacOS
203
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000204 Message("Testing EasyDialogs.")
205 ok = AskYesNoCancel("Do you want to proceed?")
206 if ok > 0:
207 s = AskString("Enter your first name")
208 Message("Thank you,\015%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000209 text = ( "Working Hard...", "Hardly Working..." ,
210 "So far, so good!", "Keep on truckin'" )
211 bar = ProgressBar("Progress, progress...", 100)
212 try:
213 appsw = MacOS.EnableAppswitch(0)
214 for i in range(100):
215 bar.set(i)
216 time.sleep(0.1)
217 if i % 10 == 0:
218 bar.label(text[(i/10) % 4])
219 bar.label("Done.")
220 time.sleep(0.3) # give'em a chance to see the done.
221 finally:
222 del bar
223 MacOS.EnableAppswitch(appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000224
225
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000226
227
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000228if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000229 try:
230 test()
231 except KeyboardInterrupt:
232 Message("Operation Canceled.")
233