blob: 4318777fccb43a96a27b208db68793c74afce2d3 [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
Jack Jansen3f5aef71997-06-20 16:23:37 +000021def Message(msg, id=256):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000022 """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
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000029 d = GetNewDialog(id, -1)
30 if not d:
31 print "Can't get DLOG resource with id =", id
32 return
Jack Jansene4b40381995-07-17 13:25:15 +000033 tp, h, rect = d.GetDialogItem(2)
34 SetDialogItemText(h, msg)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000035 d.SetDialogDefaultItem(1)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000036 while 1:
37 n = ModalDialog(None)
38 if n == 1:
39 return
40
41
Jack Jansen3f5aef71997-06-20 16:23:37 +000042def AskString(prompt, default = "", id=257):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000043 """Display a PROMPT string and a text entry field with a DEFAULT string.
44
45 Return the contents of the text entry field when the user clicks the
46 OK button or presses Return.
47 Return None when the user clicks the Cancel button.
48
49 If omitted, DEFAULT is empty.
50
51 The PROMPT and DEFAULT strings, as well as the return value,
52 can be at most 255 characters long.
53 """
54
55 id = 257
56 d = GetNewDialog(id, -1)
57 if not d:
58 print "Can't get DLOG resource with id =", id
59 return
Jack Jansene4b40381995-07-17 13:25:15 +000060 tp, h, rect = d.GetDialogItem(3)
61 SetDialogItemText(h, prompt)
62 tp, h, rect = d.GetDialogItem(4)
63 SetDialogItemText(h, default)
64# d.SetDialogItem(4, 0, 255)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000065 d.SetDialogDefaultItem(1)
66 d.SetDialogCancelItem(2)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000067 while 1:
68 n = ModalDialog(None)
69 if n == 1:
Jack Jansene4b40381995-07-17 13:25:15 +000070 tp, h, rect = d.GetDialogItem(4)
71 return GetDialogItemText(h)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000072 if n == 2: return None
73
74
Jack Jansen3f5aef71997-06-20 16:23:37 +000075def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=258):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000076## """Display a QUESTION string which can be answered with Yes or No.
77##
78## Return 1 when the user clicks the Yes button.
79## Return 0 when the user clicks the No button.
80## Return -1 when the user clicks the Cancel button.
81##
82## When the user presses Return, the DEFAULT value is returned.
83## If omitted, this is 0 (No).
84##
85## The QUESTION strign ca be at most 255 characters.
86## """
87
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000088 d = GetNewDialog(id, -1)
89 if not d:
90 print "Can't get DLOG resource with id =", id
91 return
92 # Button assignments:
93 # 1 = default (invisible)
94 # 2 = Yes
95 # 3 = No
96 # 4 = Cancel
97 # The question string is item 5
Jack Jansene4b40381995-07-17 13:25:15 +000098 tp, h, rect = d.GetDialogItem(5)
99 SetDialogItemText(h, question)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000100 if yes != None:
101 tp, h, rect = d.GetDialogItem(2)
102 h.as_Control().SetControlTitle(yes)
103 if no != None:
104 tp, h, rect = d.GetDialogItem(3)
105 h.as_Control().SetControlTitle(no)
106 if cancel != None:
107 tp, h, rect = d.GetDialogItem(4)
108 h.as_Control().SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000109 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000110 if default == 1:
111 d.SetDialogDefaultItem(2)
112 elif default == 0:
113 d.SetDialogDefaultItem(3)
114 elif default == -1:
115 d.SetDialogDefaultItem(4)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000116 while 1:
117 n = ModalDialog(None)
118 if n == 1: return default
119 if n == 2: return 1
120 if n == 3: return 0
121 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000122
123
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000124
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000125
126screenbounds = Qd.qd.screenBits.bounds
127screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
128 screenbounds[2]-4, screenbounds[3]-4
129
130
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000131class ProgressBar:
Jack Jansen3f5aef71997-06-20 16:23:37 +0000132 def __init__(self, title="Working...", maxval=100, label="", id=259):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000133 self.maxval = maxval
134 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000135 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000136 self.title(title)
137 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000138 self._update(0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000139
140 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000141 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000142 self.d.HideWindow()
143 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000144
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000145 def title(self, newstr=""):
146 """title(text) - Set title of progress window"""
Jack Jansen48c55271997-05-13 15:41:07 +0000147 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000148 w = self.d.GetDialogWindow()
149 w.SetWTitle(newstr)
150
151 def label( self, *newstr ):
152 """label(text) - Set text in progress box"""
Jack Jansen48c55271997-05-13 15:41:07 +0000153 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000154 if newstr:
155 self._label = newstr[0]
156 tp, text_h, rect = self.d.GetDialogItem(2)
157 SetDialogItemText(text_h, self._label)
158
159
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000160 def _update(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000161 self.d.BringToFront()
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000162 tp, h, bar_rect = self.d.GetDialogItem(3)
163 Qd.SetPort(self.d)
164
165 Qd.FrameRect(bar_rect) # Draw outline
166
167 inner_rect = Qd.InsetRect(bar_rect, 1, 1)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000168 l, t, r, b = inner_rect
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000169
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000170 Qd.ForeColor(QuickDraw.blackColor)
171 Qd.BackColor(QuickDraw.blackColor)
Jack Jansen3a4b3b01996-10-15 16:11:50 +0000172 Qd.PaintRect((l, t, int(l + (r-l)*value/self.maxval), b)) # Draw bar
173
174 Qd.ForeColor(QuickDraw.whiteColor)
175 Qd.BackColor(QuickDraw.whiteColor)
176 Qd.PaintRect((int(l + (r-l)*value/self.maxval), t, r, b)) # Clear rest
177
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000178 # Restore settings
179 Qd.ForeColor(QuickDraw.blackColor)
180 Qd.BackColor(QuickDraw.whiteColor)
181
182 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000183
184 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
185 if ready :
186 what,msg,when,where,mod = ev
187 part = Win.FindWindow(where)[0]
188 if Dlg.IsDialogEvent(ev):
189 ds = Dlg.DialogSelect(ev)
190 if ds[0] and ds[1] == self.d and ds[-1] == 1:
191 raise KeyboardInterrupt, ev
192 else:
193 if part == 4: # inDrag
194 self.d.DragWindow(where, screenbounds)
195 else:
196 MacOS.HandleEvent(ev)
197
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000198
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000199 def set(self, value):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000200 """set(value) - Set progress bar position"""
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000201 if value < 0: value = 0
202 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000203 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000204 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000205
206 def inc(self, n=1):
207 """inc(amt) - Increment progress bar position"""
208 self.set(self.curval + n)
209
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000210def test():
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000211 import time
212 import MacOS
213
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000214 Message("Testing EasyDialogs.")
215 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen3f5aef71997-06-20 16:23:37 +0000216 ok = AskYesNoCancel("Do you want to identify?", yes="Indentify", no="Don't identify")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000217 if ok > 0:
218 s = AskString("Enter your first name")
219 Message("Thank you,\015%s" % `s`)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000220 text = ( "Working Hard...", "Hardly Working..." ,
221 "So far, so good!", "Keep on truckin'" )
222 bar = ProgressBar("Progress, progress...", 100)
223 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000224 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000225 for i in range(100):
226 bar.set(i)
227 time.sleep(0.1)
228 if i % 10 == 0:
229 bar.label(text[(i/10) % 4])
230 bar.label("Done.")
231 time.sleep(0.3) # give'em a chance to see the done.
232 finally:
233 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000234 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000235
236
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000237
238
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000239if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000240 try:
241 test()
242 except KeyboardInterrupt:
243 Message("Operation Canceled.")
244