blob: 27219a2cbe40fa7b07c7e85476d82240a9479fb2 [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.
Just van Rossumcdcc0f01999-02-15 00:04:05 +00005AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00006AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
Jack Jansen3032fe62003-01-21 14:38:32 +00007GetArgv(optionlist, commandlist) -- fill a sys.argv-like list using a dialog
8AskFileForOpen(...) -- Ask the user for an existing file
9AskFileForSave(...) -- Ask the user for an output file
10AskFolder(...) -- Ask the user to select a folder
Jack Jansen3a87f5b1995-11-14 10:13:49 +000011bar = Progress(label, maxvalue) -- Display a progress bar
12bar.set(value) -- Set value
Jack Jansen1d63d8c1997-05-12 15:44:14 +000013bar.inc( *amount ) -- increment value by amount (default=1)
14bar.label( *newlabel ) -- get or set text label.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000015
16More documentation in each function.
Jack Jansencc386881999-12-12 22:57:51 +000017This module uses DLOG resources 260 and on.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000018Based upon STDWIN dialogs with the same names and functions.
19"""
20
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000021from Carbon.Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
22from Carbon import Qd
23from Carbon import QuickDraw
24from Carbon import Dialogs
25from Carbon import Windows
26from Carbon import Dlg,Win,Evt,Events # sdm7g
27from Carbon import Ctl
28from Carbon import Controls
29from Carbon import Menu
Jack Jansen7451e3b2003-03-03 12:25:02 +000030from Carbon import AE
Jack Jansenf0d12da2003-01-17 16:04:39 +000031import Nav
Jack Jansena5a49811998-07-01 15:47:44 +000032import MacOS
33import string
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000034from Carbon.ControlAccessor import * # Also import Controls constants
Jack Jansenf0d12da2003-01-17 16:04:39 +000035import Carbon.File
Jack Jansendde800e2002-11-07 23:07:05 +000036import macresource
Jack Jansene58962a2003-01-17 23:13:03 +000037import os
Jack Jansen2b3ce3b2003-01-26 20:22:41 +000038import sys
Jack Jansendde800e2002-11-07 23:07:05 +000039
Jack Jansen3032fe62003-01-21 14:38:32 +000040__all__ = ['Message', 'AskString', 'AskPassword', 'AskYesNoCancel',
41 'GetArgv', 'AskFileForOpen', 'AskFileForSave', 'AskFolder',
42 'Progress']
43
Jack Jansendde800e2002-11-07 23:07:05 +000044_initialized = 0
45
46def _initialize():
47 global _initialized
48 if _initialized: return
49 macresource.need("DLOG", 260, "dialogs.rsrc", __name__)
Jack Jansen7451e3b2003-03-03 12:25:02 +000050
51def _interact():
52 """Make sure the application is in the foreground"""
53 AE.AEInteractWithUser(50000000)
Jack Jansena5a49811998-07-01 15:47:44 +000054
55def cr2lf(text):
56 if '\r' in text:
57 text = string.join(string.split(text, '\r'), '\n')
58 return text
59
60def lf2cr(text):
61 if '\n' in text:
62 text = string.join(string.split(text, '\n'), '\r')
Jack Jansend5af7bd1998-09-28 10:37:08 +000063 if len(text) > 253:
64 text = text[:253] + '\311'
Jack Jansena5a49811998-07-01 15:47:44 +000065 return text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000066
Jack Jansencc386881999-12-12 22:57:51 +000067def Message(msg, id=260, ok=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000068 """Display a MESSAGE string.
69
70 Return when the user clicks the OK button or presses Return.
71
72 The MESSAGE string can be at most 255 characters long.
73 """
Jack Jansendde800e2002-11-07 23:07:05 +000074 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +000075 _interact()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000076 d = GetNewDialog(id, -1)
77 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +000078 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000079 return
Jack Jansencc386881999-12-12 22:57:51 +000080 h = d.GetDialogItemAsControl(2)
Jack Jansena5a49811998-07-01 15:47:44 +000081 SetDialogItemText(h, lf2cr(msg))
Jack Jansen208c15a1999-02-16 16:06:39 +000082 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000083 h = d.GetDialogItemAsControl(1)
84 h.SetControlTitle(ok)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000085 d.SetDialogDefaultItem(1)
Jack Jansenfca049d2000-01-18 13:36:02 +000086 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +000087 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000088 while 1:
89 n = ModalDialog(None)
90 if n == 1:
91 return
92
93
Jack Jansencc386881999-12-12 22:57:51 +000094def AskString(prompt, default = "", id=261, ok=None, cancel=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000095 """Display a PROMPT string and a text entry field with a DEFAULT string.
96
97 Return the contents of the text entry field when the user clicks the
98 OK button or presses Return.
99 Return None when the user clicks the Cancel button.
100
101 If omitted, DEFAULT is empty.
102
103 The PROMPT and DEFAULT strings, as well as the return value,
104 can be at most 255 characters long.
105 """
106
Jack Jansendde800e2002-11-07 23:07:05 +0000107 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +0000108 _interact()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000109 d = GetNewDialog(id, -1)
110 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +0000111 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000112 return
Jack Jansencc386881999-12-12 22:57:51 +0000113 h = d.GetDialogItemAsControl(3)
Jack Jansena5a49811998-07-01 15:47:44 +0000114 SetDialogItemText(h, lf2cr(prompt))
Jack Jansencc386881999-12-12 22:57:51 +0000115 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +0000116 SetDialogItemText(h, lf2cr(default))
Jack Jansend61f92b1999-01-22 13:14:06 +0000117 d.SelectDialogItemText(4, 0, 999)
Jack Jansene4b40381995-07-17 13:25:15 +0000118# d.SetDialogItem(4, 0, 255)
Jack Jansen208c15a1999-02-16 16:06:39 +0000119 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +0000120 h = d.GetDialogItemAsControl(1)
121 h.SetControlTitle(ok)
Jack Jansen208c15a1999-02-16 16:06:39 +0000122 if cancel != None:
Jack Jansencc386881999-12-12 22:57:51 +0000123 h = d.GetDialogItemAsControl(2)
124 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000125 d.SetDialogDefaultItem(1)
126 d.SetDialogCancelItem(2)
Jack Jansenfca049d2000-01-18 13:36:02 +0000127 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000128 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000129 while 1:
130 n = ModalDialog(None)
131 if n == 1:
Jack Jansencc386881999-12-12 22:57:51 +0000132 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +0000133 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000134 if n == 2: return None
135
Jack Jansen60429e01999-12-13 16:07:01 +0000136def AskPassword(prompt, default='', id=264, ok=None, cancel=None):
Jack Jansenb92268a1999-02-10 22:38:44 +0000137 """Display a PROMPT string and a text entry field with a DEFAULT string.
138 The string is displayed as bullets only.
139
140 Return the contents of the text entry field when the user clicks the
141 OK button or presses Return.
142 Return None when the user clicks the Cancel button.
143
144 If omitted, DEFAULT is empty.
145
146 The PROMPT and DEFAULT strings, as well as the return value,
147 can be at most 255 characters long.
148 """
Jack Jansendde800e2002-11-07 23:07:05 +0000149 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +0000150 _interact()
Jack Jansenb92268a1999-02-10 22:38:44 +0000151 d = GetNewDialog(id, -1)
152 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +0000153 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Jack Jansenb92268a1999-02-10 22:38:44 +0000154 return
Jack Jansen60429e01999-12-13 16:07:01 +0000155 h = d.GetDialogItemAsControl(3)
Jack Jansenb92268a1999-02-10 22:38:44 +0000156 SetDialogItemText(h, lf2cr(prompt))
Jack Jansen60429e01999-12-13 16:07:01 +0000157 pwd = d.GetDialogItemAsControl(4)
Jack Jansenb92268a1999-02-10 22:38:44 +0000158 bullets = '\245'*len(default)
Jack Jansen60429e01999-12-13 16:07:01 +0000159## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
160 SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default)
161 d.SelectDialogItemText(4, 0, 999)
Jack Jansen0c1836f2000-08-25 22:06:19 +0000162 Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart)
Jack Jansen60429e01999-12-13 16:07:01 +0000163 if ok != None:
164 h = d.GetDialogItemAsControl(1)
165 h.SetControlTitle(ok)
166 if cancel != None:
167 h = d.GetDialogItemAsControl(2)
168 h.SetControlTitle(cancel)
Jack Jansenb92268a1999-02-10 22:38:44 +0000169 d.SetDialogDefaultItem(Dialogs.ok)
170 d.SetDialogCancelItem(Dialogs.cancel)
Jack Jansenfca049d2000-01-18 13:36:02 +0000171 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000172 d.GetDialogWindow().ShowWindow()
Jack Jansenb92268a1999-02-10 22:38:44 +0000173 while 1:
Jack Jansen60429e01999-12-13 16:07:01 +0000174 n = ModalDialog(None)
175 if n == 1:
176 h = d.GetDialogItemAsControl(4)
177 return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag))
178 if n == 2: return None
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000179
Jack Jansencc386881999-12-12 22:57:51 +0000180def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262):
Jack Jansencf2efc61999-02-25 22:05:45 +0000181 """Display a QUESTION string which can be answered with Yes or No.
182
183 Return 1 when the user clicks the Yes button.
184 Return 0 when the user clicks the No button.
185 Return -1 when the user clicks the Cancel button.
186
187 When the user presses Return, the DEFAULT value is returned.
188 If omitted, this is 0 (No).
189
Just van Rossum639a7402001-06-26 06:57:12 +0000190 The QUESTION string can be at most 255 characters.
Jack Jansencf2efc61999-02-25 22:05:45 +0000191 """
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000192
Jack Jansendde800e2002-11-07 23:07:05 +0000193 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +0000194 _interact()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000195 d = GetNewDialog(id, -1)
196 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +0000197 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000198 return
199 # Button assignments:
200 # 1 = default (invisible)
201 # 2 = Yes
202 # 3 = No
203 # 4 = Cancel
204 # The question string is item 5
Jack Jansencc386881999-12-12 22:57:51 +0000205 h = d.GetDialogItemAsControl(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000206 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000207 if yes != None:
Jack Jansen85743782000-02-10 16:15:53 +0000208 if yes == '':
209 d.HideDialogItem(2)
210 else:
211 h = d.GetDialogItemAsControl(2)
212 h.SetControlTitle(yes)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000213 if no != None:
Jack Jansen85743782000-02-10 16:15:53 +0000214 if no == '':
215 d.HideDialogItem(3)
216 else:
217 h = d.GetDialogItemAsControl(3)
218 h.SetControlTitle(no)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000219 if cancel != None:
Jack Jansen208c15a1999-02-16 16:06:39 +0000220 if cancel == '':
221 d.HideDialogItem(4)
222 else:
Jack Jansencc386881999-12-12 22:57:51 +0000223 h = d.GetDialogItemAsControl(4)
224 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000225 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000226 if default == 1:
227 d.SetDialogDefaultItem(2)
228 elif default == 0:
229 d.SetDialogDefaultItem(3)
230 elif default == -1:
231 d.SetDialogDefaultItem(4)
Jack Jansenfca049d2000-01-18 13:36:02 +0000232 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000233 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000234 while 1:
235 n = ModalDialog(None)
236 if n == 1: return default
237 if n == 2: return 1
238 if n == 3: return 0
239 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000240
241
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000242
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000243
Jack Jansen362c7cd2002-11-30 00:01:29 +0000244screenbounds = Qd.GetQDGlobalsScreenBits().bounds
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000245screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
246 screenbounds[2]-4, screenbounds[3]-4
247
Jack Jansen911e87d2001-08-27 15:24:07 +0000248kControlProgressBarIndeterminateTag = 'inde' # from Controls.py
249
250
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000251class ProgressBar:
Jack Jansen911e87d2001-08-27 15:24:07 +0000252 def __init__(self, title="Working...", maxval=0, label="", id=263):
Jack Jansen5dd73622001-02-23 22:18:27 +0000253 self.w = None
254 self.d = None
Jack Jansendde800e2002-11-07 23:07:05 +0000255 _initialize()
Jack Jansen3f5aef71997-06-20 16:23:37 +0000256 self.d = GetNewDialog(id, -1)
Jack Jansen784c6112001-02-09 15:57:01 +0000257 self.w = self.d.GetDialogWindow()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000258 self.label(label)
Jack Jansenab48e902000-07-24 14:07:15 +0000259 self.title(title)
Jack Jansen911e87d2001-08-27 15:24:07 +0000260 self.set(0, maxval)
261 self.d.AutoSizeDialog()
Jack Jansen784c6112001-02-09 15:57:01 +0000262 self.w.ShowWindow()
Jack Jansencc386881999-12-12 22:57:51 +0000263 self.d.DrawDialog()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000264
265 def __del__( self ):
Jack Jansen5dd73622001-02-23 22:18:27 +0000266 if self.w:
267 self.w.BringToFront()
268 self.w.HideWindow()
Jack Jansen784c6112001-02-09 15:57:01 +0000269 del self.w
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000270 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000271
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000272 def title(self, newstr=""):
273 """title(text) - Set title of progress window"""
Jack Jansen784c6112001-02-09 15:57:01 +0000274 self.w.BringToFront()
275 self.w.SetWTitle(newstr)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000276
277 def label( self, *newstr ):
278 """label(text) - Set text in progress box"""
Jack Jansen784c6112001-02-09 15:57:01 +0000279 self.w.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000280 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000281 self._label = lf2cr(newstr[0])
Jack Jansencc386881999-12-12 22:57:51 +0000282 text_h = self.d.GetDialogItemAsControl(2)
Jack Jansen911e87d2001-08-27 15:24:07 +0000283 SetDialogItemText(text_h, self._label)
284
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000285 def _update(self, value):
Jack Jansen58fa8181999-11-05 15:53:10 +0000286 maxval = self.maxval
Jack Jansen911e87d2001-08-27 15:24:07 +0000287 if maxval == 0: # an indeterminate bar
288 Ctl.IdleControls(self.w) # spin the barber pole
289 else: # a determinate bar
290 if maxval > 32767:
291 value = int(value/(maxval/32767.0))
292 maxval = 32767
Jack Jansen52fbe532003-03-24 12:12:24 +0000293 maxval = int(maxval)
294 value = int(value)
Jack Jansen911e87d2001-08-27 15:24:07 +0000295 progbar = self.d.GetDialogItemAsControl(3)
296 progbar.SetControlMaximum(maxval)
297 progbar.SetControlValue(value) # set the bar length
298
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000299 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000300 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
Jack Jansen911e87d2001-08-27 15:24:07 +0000301 if ready :
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000302 what,msg,when,where,mod = ev
303 part = Win.FindWindow(where)[0]
304 if Dlg.IsDialogEvent(ev):
305 ds = Dlg.DialogSelect(ev)
306 if ds[0] and ds[1] == self.d and ds[-1] == 1:
Jack Jansen5dd73622001-02-23 22:18:27 +0000307 self.w.HideWindow()
308 self.w = None
309 self.d = None
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000310 raise KeyboardInterrupt, ev
311 else:
312 if part == 4: # inDrag
Jack Jansen8d2f3d62001-07-27 09:21:28 +0000313 self.w.DragWindow(where, screenbounds)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000314 else:
315 MacOS.HandleEvent(ev)
316
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000317
Jack Jansen58fa8181999-11-05 15:53:10 +0000318 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000319 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000320 if max != None:
321 self.maxval = max
Jack Jansen911e87d2001-08-27 15:24:07 +0000322 bar = self.d.GetDialogItemAsControl(3)
323 if max <= 0: # indeterminate bar
324 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x01')
325 else: # determinate bar
326 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x00')
327 if value < 0:
328 value = 0
329 elif value > self.maxval:
330 value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000331 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000332 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000333
334 def inc(self, n=1):
335 """inc(amt) - Increment progress bar position"""
336 self.set(self.curval + n)
337
Jack Jansenf86eda52000-09-19 22:42:38 +0000338ARGV_ID=265
339ARGV_ITEM_OK=1
340ARGV_ITEM_CANCEL=2
341ARGV_OPTION_GROUP=3
342ARGV_OPTION_EXPLAIN=4
343ARGV_OPTION_VALUE=5
344ARGV_OPTION_ADD=6
345ARGV_COMMAND_GROUP=7
346ARGV_COMMAND_EXPLAIN=8
347ARGV_COMMAND_ADD=9
348ARGV_ADD_OLDFILE=10
349ARGV_ADD_NEWFILE=11
350ARGV_ADD_FOLDER=12
351ARGV_CMDLINE_GROUP=13
352ARGV_CMDLINE_DATA=14
353
354##def _myModalDialog(d):
355## while 1:
356## ready, ev = Evt.WaitNextEvent(0xffff, -1)
357## print 'DBG: WNE', ready, ev
358## if ready :
359## what,msg,when,where,mod = ev
360## part, window = Win.FindWindow(where)
361## if Dlg.IsDialogEvent(ev):
362## didit, dlgdone, itemdone = Dlg.DialogSelect(ev)
363## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d
364## if didit and dlgdone == d:
365## return itemdone
366## elif window == d.GetDialogWindow():
367## d.GetDialogWindow().SelectWindow()
368## if part == 4: # inDrag
369## d.DragWindow(where, screenbounds)
370## else:
371## MacOS.HandleEvent(ev)
372## else:
373## MacOS.HandleEvent(ev)
374##
375def _setmenu(control, items):
Jack Jansene7bfc912001-01-09 22:22:58 +0000376 mhandle = control.GetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000377 Controls.kControlPopupButtonMenuHandleTag)
378 menu = Menu.as_Menu(mhandle)
379 for item in items:
380 if type(item) == type(()):
381 label = item[0]
382 else:
383 label = item
384 if label[-1] == '=' or label[-1] == ':':
385 label = label[:-1]
386 menu.AppendMenu(label)
387## mhandle, mid = menu.getpopupinfo()
Jack Jansene7bfc912001-01-09 22:22:58 +0000388## control.SetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000389## Controls.kControlPopupButtonMenuHandleTag, mhandle)
390 control.SetControlMinimum(1)
391 control.SetControlMaximum(len(items)+1)
392
393def _selectoption(d, optionlist, idx):
394 if idx < 0 or idx >= len(optionlist):
395 MacOS.SysBeep()
396 return
397 option = optionlist[idx]
Jack Jansen09c73432002-06-26 15:14:48 +0000398 if type(option) == type(()):
399 if len(option) == 4:
400 help = option[2]
401 elif len(option) > 1:
402 help = option[-1]
403 else:
404 help = ''
Jack Jansenf86eda52000-09-19 22:42:38 +0000405 else:
406 help = ''
407 h = d.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN)
Jack Jansen09c73432002-06-26 15:14:48 +0000408 if help and len(help) > 250:
409 help = help[:250] + '...'
Jack Jansenf86eda52000-09-19 22:42:38 +0000410 Dlg.SetDialogItemText(h, help)
411 hasvalue = 0
412 if type(option) == type(()):
413 label = option[0]
414 else:
415 label = option
416 if label[-1] == '=' or label[-1] == ':':
417 hasvalue = 1
418 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
419 Dlg.SetDialogItemText(h, '')
420 if hasvalue:
421 d.ShowDialogItem(ARGV_OPTION_VALUE)
422 d.SelectDialogItemText(ARGV_OPTION_VALUE, 0, 0)
423 else:
424 d.HideDialogItem(ARGV_OPTION_VALUE)
425
426
427def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID):
Jack Jansendde800e2002-11-07 23:07:05 +0000428 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +0000429 _interact()
Jack Jansenf86eda52000-09-19 22:42:38 +0000430 d = GetNewDialog(id, -1)
431 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +0000432 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Jack Jansenf86eda52000-09-19 22:42:38 +0000433 return
434# h = d.GetDialogItemAsControl(3)
435# SetDialogItemText(h, lf2cr(prompt))
436# h = d.GetDialogItemAsControl(4)
437# SetDialogItemText(h, lf2cr(default))
438# d.SelectDialogItemText(4, 0, 999)
439# d.SetDialogItem(4, 0, 255)
440 if optionlist:
441 _setmenu(d.GetDialogItemAsControl(ARGV_OPTION_GROUP), optionlist)
442 _selectoption(d, optionlist, 0)
443 else:
444 d.GetDialogItemAsControl(ARGV_OPTION_GROUP).DeactivateControl()
445 if commandlist:
446 _setmenu(d.GetDialogItemAsControl(ARGV_COMMAND_GROUP), commandlist)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000447 if type(commandlist[0]) == type(()) and len(commandlist[0]) > 1:
Jack Jansenf86eda52000-09-19 22:42:38 +0000448 help = commandlist[0][-1]
449 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
450 Dlg.SetDialogItemText(h, help)
451 else:
452 d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).DeactivateControl()
453 if not addoldfile:
454 d.GetDialogItemAsControl(ARGV_ADD_OLDFILE).DeactivateControl()
455 if not addnewfile:
456 d.GetDialogItemAsControl(ARGV_ADD_NEWFILE).DeactivateControl()
457 if not addfolder:
458 d.GetDialogItemAsControl(ARGV_ADD_FOLDER).DeactivateControl()
459 d.SetDialogDefaultItem(ARGV_ITEM_OK)
460 d.SetDialogCancelItem(ARGV_ITEM_CANCEL)
461 d.GetDialogWindow().ShowWindow()
462 d.DrawDialog()
Jack Janseneb308432001-09-09 00:36:01 +0000463 if hasattr(MacOS, 'SchedParams'):
464 appsw = MacOS.SchedParams(1, 0)
Jack Jansenf86eda52000-09-19 22:42:38 +0000465 try:
466 while 1:
467 stringstoadd = []
468 n = ModalDialog(None)
469 if n == ARGV_ITEM_OK:
470 break
471 elif n == ARGV_ITEM_CANCEL:
472 raise SystemExit
473 elif n == ARGV_OPTION_GROUP:
474 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
475 _selectoption(d, optionlist, idx)
476 elif n == ARGV_OPTION_VALUE:
477 pass
478 elif n == ARGV_OPTION_ADD:
479 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
480 if 0 <= idx < len(optionlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000481 option = optionlist[idx]
482 if type(option) == type(()):
483 option = option[0]
Jack Jansenf86eda52000-09-19 22:42:38 +0000484 if option[-1] == '=' or option[-1] == ':':
485 option = option[:-1]
486 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
487 value = Dlg.GetDialogItemText(h)
488 else:
489 value = ''
490 if len(option) == 1:
491 stringtoadd = '-' + option
492 else:
493 stringtoadd = '--' + option
494 stringstoadd = [stringtoadd]
495 if value:
496 stringstoadd.append(value)
497 else:
498 MacOS.SysBeep()
499 elif n == ARGV_COMMAND_GROUP:
500 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
Jack Jansen0bb0a902000-09-21 22:01:08 +0000501 if 0 <= idx < len(commandlist) and type(commandlist[idx]) == type(()) and \
Jack Jansenf86eda52000-09-19 22:42:38 +0000502 len(commandlist[idx]) > 1:
503 help = commandlist[idx][-1]
504 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
505 Dlg.SetDialogItemText(h, help)
506 elif n == ARGV_COMMAND_ADD:
507 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
508 if 0 <= idx < len(commandlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000509 command = commandlist[idx]
510 if type(command) == type(()):
511 command = command[0]
512 stringstoadd = [command]
Jack Jansenf86eda52000-09-19 22:42:38 +0000513 else:
514 MacOS.SysBeep()
515 elif n == ARGV_ADD_OLDFILE:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000516 pathname = AskFileForOpen()
517 if pathname:
518 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000519 elif n == ARGV_ADD_NEWFILE:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000520 pathname = AskFileForSave()
521 if pathname:
522 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000523 elif n == ARGV_ADD_FOLDER:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000524 pathname = AskFolder()
525 if pathname:
526 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000527 elif n == ARGV_CMDLINE_DATA:
528 pass # Nothing to do
529 else:
530 raise RuntimeError, "Unknown dialog item %d"%n
531
532 for stringtoadd in stringstoadd:
533 if '"' in stringtoadd or "'" in stringtoadd or " " in stringtoadd:
534 stringtoadd = `stringtoadd`
535 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
536 oldstr = GetDialogItemText(h)
537 if oldstr and oldstr[-1] != ' ':
538 oldstr = oldstr + ' '
539 oldstr = oldstr + stringtoadd
540 if oldstr[-1] != ' ':
541 oldstr = oldstr + ' '
542 SetDialogItemText(h, oldstr)
543 d.SelectDialogItemText(ARGV_CMDLINE_DATA, 0x7fff, 0x7fff)
544 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
545 oldstr = GetDialogItemText(h)
546 tmplist = string.split(oldstr)
547 newlist = []
548 while tmplist:
549 item = tmplist[0]
550 del tmplist[0]
551 if item[0] == '"':
552 while item[-1] != '"':
553 if not tmplist:
554 raise RuntimeError, "Unterminated quoted argument"
555 item = item + ' ' + tmplist[0]
556 del tmplist[0]
557 item = item[1:-1]
558 if item[0] == "'":
559 while item[-1] != "'":
560 if not tmplist:
561 raise RuntimeError, "Unterminated quoted argument"
562 item = item + ' ' + tmplist[0]
563 del tmplist[0]
564 item = item[1:-1]
565 newlist.append(item)
566 return newlist
567 finally:
Jack Janseneb308432001-09-09 00:36:01 +0000568 if hasattr(MacOS, 'SchedParams'):
569 apply(MacOS.SchedParams, appsw)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000570 del d
Jack Jansenf0d12da2003-01-17 16:04:39 +0000571
Jack Jansen3032fe62003-01-21 14:38:32 +0000572def _process_Nav_args(dftflags, **args):
Jack Jansenf0d12da2003-01-17 16:04:39 +0000573 import aepack
574 import Carbon.AE
575 import Carbon.File
Jack Jansenf0d12da2003-01-17 16:04:39 +0000576 for k in args.keys():
Jack Jansen3032fe62003-01-21 14:38:32 +0000577 if args[k] is None:
578 del args[k]
Jack Jansenf0d12da2003-01-17 16:04:39 +0000579 # Set some defaults, and modify some arguments
580 if not args.has_key('dialogOptionFlags'):
581 args['dialogOptionFlags'] = dftflags
582 if args.has_key('defaultLocation') and \
583 not isinstance(args['defaultLocation'], Carbon.AE.AEDesc):
584 defaultLocation = args['defaultLocation']
585 if isinstance(defaultLocation, (Carbon.File.FSSpec, Carbon.File.FSRef)):
586 args['defaultLocation'] = aepack.pack(defaultLocation)
587 else:
588 defaultLocation = Carbon.File.FSRef(defaultLocation)
589 args['defaultLocation'] = aepack.pack(defaultLocation)
590 if args.has_key('typeList') and not isinstance(args['typeList'], Carbon.Res.ResourceType):
Jack Jansene1c4f0b2003-01-21 22:57:53 +0000591 typeList = args['typeList'][:]
Jack Jansenf0d12da2003-01-17 16:04:39 +0000592 # Workaround for OSX typeless files:
593 if 'TEXT' in typeList and not '\0\0\0\0' in typeList:
594 typeList = typeList + ('\0\0\0\0',)
595 data = 'Pyth' + struct.pack("hh", 0, len(typeList))
596 for type in typeList:
597 data = data+type
598 args['typeList'] = Carbon.Res.Handle(data)
599 tpwanted = str
600 if args.has_key('wanted'):
601 tpwanted = args['wanted']
602 del args['wanted']
603 return args, tpwanted
604
Jack Jansen2731c5c2003-02-07 15:45:40 +0000605def _dummy_Nav_eventproc(msg, data):
606 pass
607
608_default_Nav_eventproc = _dummy_Nav_eventproc
609
610def SetDefaultEventProc(proc):
611 global _default_Nav_eventproc
612 rv = _default_Nav_eventproc
613 if proc is None:
614 proc = _dummy_Nav_eventproc
615 _default_Nav_eventproc = proc
616 return rv
617
Jack Jansen3032fe62003-01-21 14:38:32 +0000618def AskFileForOpen(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000619 message=None,
620 typeList=None,
621 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000622 version=None,
623 defaultLocation=None,
624 dialogOptionFlags=None,
625 location=None,
626 clientName=None,
627 windowTitle=None,
628 actionButtonLabel=None,
629 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000630 preferenceKey=None,
631 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000632 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000633 previewProc=None,
634 filterProc=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000635 wanted=None,
636 multiple=None):
637 """Display a dialog asking the user for a file to open.
638
639 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
640 the other arguments can be looked up in Apple's Navigation Services documentation"""
641
Jack Jansenf0d12da2003-01-17 16:04:39 +0000642 default_flags = 0x56 # Or 0xe4?
Jack Jansen3032fe62003-01-21 14:38:32 +0000643 args, tpwanted = _process_Nav_args(default_flags, version=version,
644 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
645 location=location,clientName=clientName,windowTitle=windowTitle,
646 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
647 message=message,preferenceKey=preferenceKey,
648 popupExtension=popupExtension,eventProc=eventProc,previewProc=previewProc,
649 filterProc=filterProc,typeList=typeList,wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000650 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000651 try:
652 rr = Nav.NavChooseFile(args)
653 good = 1
654 except Nav.error, arg:
655 if arg[0] != -128: # userCancelledErr
656 raise Nav.error, arg
657 return None
658 if not rr.validRecord or not rr.selection:
659 return None
660 if issubclass(tpwanted, Carbon.File.FSRef):
661 return tpwanted(rr.selection_fsr[0])
662 if issubclass(tpwanted, Carbon.File.FSSpec):
663 return tpwanted(rr.selection[0])
664 if issubclass(tpwanted, str):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000665 return tpwanted(rr.selection_fsr[0].as_pathname())
Jack Jansenf0d12da2003-01-17 16:04:39 +0000666 if issubclass(tpwanted, unicode):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000667 return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
Jack Jansenf0d12da2003-01-17 16:04:39 +0000668 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
669
Jack Jansen3032fe62003-01-21 14:38:32 +0000670def AskFileForSave(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000671 message=None,
672 savedFileName=None,
673 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000674 version=None,
675 defaultLocation=None,
676 dialogOptionFlags=None,
677 location=None,
678 clientName=None,
679 windowTitle=None,
680 actionButtonLabel=None,
681 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000682 preferenceKey=None,
683 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000684 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000685 fileType=None,
686 fileCreator=None,
687 wanted=None,
688 multiple=None):
689 """Display a dialog asking the user for a filename to save to.
690
691 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
692 the other arguments can be looked up in Apple's Navigation Services documentation"""
693
694
Jack Jansenf0d12da2003-01-17 16:04:39 +0000695 default_flags = 0x07
Jack Jansen3032fe62003-01-21 14:38:32 +0000696 args, tpwanted = _process_Nav_args(default_flags, version=version,
697 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
698 location=location,clientName=clientName,windowTitle=windowTitle,
699 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
700 savedFileName=savedFileName,message=message,preferenceKey=preferenceKey,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000701 popupExtension=popupExtension,eventProc=eventProc,fileType=fileType,
702 fileCreator=fileCreator,wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000703 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000704 try:
705 rr = Nav.NavPutFile(args)
706 good = 1
707 except Nav.error, arg:
708 if arg[0] != -128: # userCancelledErr
709 raise Nav.error, arg
710 return None
711 if not rr.validRecord or not rr.selection:
712 return None
713 if issubclass(tpwanted, Carbon.File.FSRef):
714 raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave"
715 if issubclass(tpwanted, Carbon.File.FSSpec):
716 return tpwanted(rr.selection[0])
717 if issubclass(tpwanted, (str, unicode)):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000718 if sys.platform == 'mac':
719 fullpath = rr.selection[0].as_pathname()
720 else:
721 # This is gross, and probably incorrect too
722 vrefnum, dirid, name = rr.selection[0].as_tuple()
723 pardir_fss = Carbon.File.FSSpec((vrefnum, dirid, ''))
724 pardir_fsr = Carbon.File.FSRef(pardir_fss)
725 pardir_path = pardir_fsr.FSRefMakePath() # This is utf-8
726 name_utf8 = unicode(name, 'macroman').encode('utf8')
727 fullpath = os.path.join(pardir_path, name_utf8)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000728 if issubclass(tpwanted, unicode):
729 return unicode(fullpath, 'utf8')
730 return tpwanted(fullpath)
731 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
732
Jack Jansen3032fe62003-01-21 14:38:32 +0000733def AskFolder(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000734 message=None,
735 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000736 version=None,
737 defaultLocation=None,
738 dialogOptionFlags=None,
739 location=None,
740 clientName=None,
741 windowTitle=None,
742 actionButtonLabel=None,
743 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000744 preferenceKey=None,
745 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000746 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000747 filterProc=None,
748 wanted=None,
749 multiple=None):
750 """Display a dialog asking the user for select a folder.
751
752 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
753 the other arguments can be looked up in Apple's Navigation Services documentation"""
754
Jack Jansenf0d12da2003-01-17 16:04:39 +0000755 default_flags = 0x17
Jack Jansen3032fe62003-01-21 14:38:32 +0000756 args, tpwanted = _process_Nav_args(default_flags, version=version,
757 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
758 location=location,clientName=clientName,windowTitle=windowTitle,
759 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
760 message=message,preferenceKey=preferenceKey,
761 popupExtension=popupExtension,eventProc=eventProc,filterProc=filterProc,
762 wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000763 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000764 try:
765 rr = Nav.NavChooseFolder(args)
766 good = 1
767 except Nav.error, arg:
768 if arg[0] != -128: # userCancelledErr
769 raise Nav.error, arg
770 return None
771 if not rr.validRecord or not rr.selection:
772 return None
773 if issubclass(tpwanted, Carbon.File.FSRef):
774 return tpwanted(rr.selection_fsr[0])
775 if issubclass(tpwanted, Carbon.File.FSSpec):
776 return tpwanted(rr.selection[0])
777 if issubclass(tpwanted, str):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000778 return tpwanted(rr.selection_fsr[0].as_pathname())
Jack Jansenf0d12da2003-01-17 16:04:39 +0000779 if issubclass(tpwanted, unicode):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000780 return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
Jack Jansenf0d12da2003-01-17 16:04:39 +0000781 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
782
783
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000784def test():
Jack Jansend9bb1a02003-02-21 23:18:48 +0000785 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000786
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000787 Message("Testing EasyDialogs.")
Jack Jansenf86eda52000-09-19 22:42:38 +0000788 optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'),
789 ('flags=', 'Valued option'), ('f:', 'Short valued option'))
790 commandlist = (('start', 'Start something'), ('stop', 'Stop something'))
791 argv = GetArgv(optionlist=optionlist, commandlist=commandlist, addoldfile=0)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000792 Message("Command line: %s"%' '.join(argv))
Jack Jansenf86eda52000-09-19 22:42:38 +0000793 for i in range(len(argv)):
794 print 'arg[%d] = %s'%(i, `argv[i]`)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000795 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000796 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000797 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000798 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000799 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
800 if not s2:
801 Message("%s has no secret nickname"%s)
802 else:
803 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansenf0d12da2003-01-17 16:04:39 +0000804 else:
805 s = 'Anonymous'
Jack Jansend9bb1a02003-02-21 23:18:48 +0000806 rv = AskFileForOpen(message="Gimme a file, %s"%s, wanted=Carbon.File.FSSpec)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000807 Message("rv: %s"%rv)
Jack Jansend9bb1a02003-02-21 23:18:48 +0000808 rv = AskFileForSave(wanted=Carbon.File.FSRef, savedFileName="%s.txt"%s)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000809 Message("rv.as_pathname: %s"%rv.as_pathname())
810 rv = AskFolder()
811 Message("Folder name: %s"%rv)
Jack Jansen911e87d2001-08-27 15:24:07 +0000812 text = ( "Working Hard...", "Hardly Working..." ,
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000813 "So far, so good!", "Keep on truckin'" )
Jack Jansen911e87d2001-08-27 15:24:07 +0000814 bar = ProgressBar("Progress, progress...", 0, label="Ramping up...")
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000815 try:
Jack Janseneb308432001-09-09 00:36:01 +0000816 if hasattr(MacOS, 'SchedParams'):
817 appsw = MacOS.SchedParams(1, 0)
Jack Jansen911e87d2001-08-27 15:24:07 +0000818 for i in xrange(20):
819 bar.inc()
820 time.sleep(0.05)
821 bar.set(0,100)
822 for i in xrange(100):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000823 bar.set(i)
Jack Jansen911e87d2001-08-27 15:24:07 +0000824 time.sleep(0.05)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000825 if i % 10 == 0:
826 bar.label(text[(i/10) % 4])
827 bar.label("Done.")
Jack Jansen911e87d2001-08-27 15:24:07 +0000828 time.sleep(1.0) # give'em a chance to see "Done."
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000829 finally:
830 del bar
Jack Janseneb308432001-09-09 00:36:01 +0000831 if hasattr(MacOS, 'SchedParams'):
832 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000833
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000834if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000835 try:
836 test()
837 except KeyboardInterrupt:
838 Message("Operation Canceled.")
839