blob: cbbe9bce71cc5313d4ae7e55b0b376049ce8257e [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 Jansen362c7cd02002-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
293 progbar = self.d.GetDialogItemAsControl(3)
294 progbar.SetControlMaximum(maxval)
295 progbar.SetControlValue(value) # set the bar length
296
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000297 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000298 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
Jack Jansen911e87d2001-08-27 15:24:07 +0000299 if ready :
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000300 what,msg,when,where,mod = ev
301 part = Win.FindWindow(where)[0]
302 if Dlg.IsDialogEvent(ev):
303 ds = Dlg.DialogSelect(ev)
304 if ds[0] and ds[1] == self.d and ds[-1] == 1:
Jack Jansen5dd73622001-02-23 22:18:27 +0000305 self.w.HideWindow()
306 self.w = None
307 self.d = None
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000308 raise KeyboardInterrupt, ev
309 else:
310 if part == 4: # inDrag
Jack Jansen8d2f3d62001-07-27 09:21:28 +0000311 self.w.DragWindow(where, screenbounds)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000312 else:
313 MacOS.HandleEvent(ev)
314
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000315
Jack Jansen58fa8181999-11-05 15:53:10 +0000316 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000317 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000318 if max != None:
319 self.maxval = max
Jack Jansen911e87d2001-08-27 15:24:07 +0000320 bar = self.d.GetDialogItemAsControl(3)
321 if max <= 0: # indeterminate bar
322 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x01')
323 else: # determinate bar
324 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x00')
325 if value < 0:
326 value = 0
327 elif value > self.maxval:
328 value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000329 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000330 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000331
332 def inc(self, n=1):
333 """inc(amt) - Increment progress bar position"""
334 self.set(self.curval + n)
335
Jack Jansenf86eda52000-09-19 22:42:38 +0000336ARGV_ID=265
337ARGV_ITEM_OK=1
338ARGV_ITEM_CANCEL=2
339ARGV_OPTION_GROUP=3
340ARGV_OPTION_EXPLAIN=4
341ARGV_OPTION_VALUE=5
342ARGV_OPTION_ADD=6
343ARGV_COMMAND_GROUP=7
344ARGV_COMMAND_EXPLAIN=8
345ARGV_COMMAND_ADD=9
346ARGV_ADD_OLDFILE=10
347ARGV_ADD_NEWFILE=11
348ARGV_ADD_FOLDER=12
349ARGV_CMDLINE_GROUP=13
350ARGV_CMDLINE_DATA=14
351
352##def _myModalDialog(d):
353## while 1:
354## ready, ev = Evt.WaitNextEvent(0xffff, -1)
355## print 'DBG: WNE', ready, ev
356## if ready :
357## what,msg,when,where,mod = ev
358## part, window = Win.FindWindow(where)
359## if Dlg.IsDialogEvent(ev):
360## didit, dlgdone, itemdone = Dlg.DialogSelect(ev)
361## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d
362## if didit and dlgdone == d:
363## return itemdone
364## elif window == d.GetDialogWindow():
365## d.GetDialogWindow().SelectWindow()
366## if part == 4: # inDrag
367## d.DragWindow(where, screenbounds)
368## else:
369## MacOS.HandleEvent(ev)
370## else:
371## MacOS.HandleEvent(ev)
372##
373def _setmenu(control, items):
Jack Jansene7bfc912001-01-09 22:22:58 +0000374 mhandle = control.GetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000375 Controls.kControlPopupButtonMenuHandleTag)
376 menu = Menu.as_Menu(mhandle)
377 for item in items:
378 if type(item) == type(()):
379 label = item[0]
380 else:
381 label = item
382 if label[-1] == '=' or label[-1] == ':':
383 label = label[:-1]
384 menu.AppendMenu(label)
385## mhandle, mid = menu.getpopupinfo()
Jack Jansene7bfc912001-01-09 22:22:58 +0000386## control.SetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000387## Controls.kControlPopupButtonMenuHandleTag, mhandle)
388 control.SetControlMinimum(1)
389 control.SetControlMaximum(len(items)+1)
390
391def _selectoption(d, optionlist, idx):
392 if idx < 0 or idx >= len(optionlist):
393 MacOS.SysBeep()
394 return
395 option = optionlist[idx]
Jack Jansen09c73432002-06-26 15:14:48 +0000396 if type(option) == type(()):
397 if len(option) == 4:
398 help = option[2]
399 elif len(option) > 1:
400 help = option[-1]
401 else:
402 help = ''
Jack Jansenf86eda52000-09-19 22:42:38 +0000403 else:
404 help = ''
405 h = d.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN)
Jack Jansen09c73432002-06-26 15:14:48 +0000406 if help and len(help) > 250:
407 help = help[:250] + '...'
Jack Jansenf86eda52000-09-19 22:42:38 +0000408 Dlg.SetDialogItemText(h, help)
409 hasvalue = 0
410 if type(option) == type(()):
411 label = option[0]
412 else:
413 label = option
414 if label[-1] == '=' or label[-1] == ':':
415 hasvalue = 1
416 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
417 Dlg.SetDialogItemText(h, '')
418 if hasvalue:
419 d.ShowDialogItem(ARGV_OPTION_VALUE)
420 d.SelectDialogItemText(ARGV_OPTION_VALUE, 0, 0)
421 else:
422 d.HideDialogItem(ARGV_OPTION_VALUE)
423
424
425def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID):
Jack Jansendde800e2002-11-07 23:07:05 +0000426 _initialize()
Jack Jansen7451e3b2003-03-03 12:25:02 +0000427 _interact()
Jack Jansenf86eda52000-09-19 22:42:38 +0000428 d = GetNewDialog(id, -1)
429 if not d:
Jack Jansend05e1812002-08-02 11:03:19 +0000430 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
Jack Jansenf86eda52000-09-19 22:42:38 +0000431 return
432# h = d.GetDialogItemAsControl(3)
433# SetDialogItemText(h, lf2cr(prompt))
434# h = d.GetDialogItemAsControl(4)
435# SetDialogItemText(h, lf2cr(default))
436# d.SelectDialogItemText(4, 0, 999)
437# d.SetDialogItem(4, 0, 255)
438 if optionlist:
439 _setmenu(d.GetDialogItemAsControl(ARGV_OPTION_GROUP), optionlist)
440 _selectoption(d, optionlist, 0)
441 else:
442 d.GetDialogItemAsControl(ARGV_OPTION_GROUP).DeactivateControl()
443 if commandlist:
444 _setmenu(d.GetDialogItemAsControl(ARGV_COMMAND_GROUP), commandlist)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000445 if type(commandlist[0]) == type(()) and len(commandlist[0]) > 1:
Jack Jansenf86eda52000-09-19 22:42:38 +0000446 help = commandlist[0][-1]
447 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
448 Dlg.SetDialogItemText(h, help)
449 else:
450 d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).DeactivateControl()
451 if not addoldfile:
452 d.GetDialogItemAsControl(ARGV_ADD_OLDFILE).DeactivateControl()
453 if not addnewfile:
454 d.GetDialogItemAsControl(ARGV_ADD_NEWFILE).DeactivateControl()
455 if not addfolder:
456 d.GetDialogItemAsControl(ARGV_ADD_FOLDER).DeactivateControl()
457 d.SetDialogDefaultItem(ARGV_ITEM_OK)
458 d.SetDialogCancelItem(ARGV_ITEM_CANCEL)
459 d.GetDialogWindow().ShowWindow()
460 d.DrawDialog()
Jack Janseneb308432001-09-09 00:36:01 +0000461 if hasattr(MacOS, 'SchedParams'):
462 appsw = MacOS.SchedParams(1, 0)
Jack Jansenf86eda52000-09-19 22:42:38 +0000463 try:
464 while 1:
465 stringstoadd = []
466 n = ModalDialog(None)
467 if n == ARGV_ITEM_OK:
468 break
469 elif n == ARGV_ITEM_CANCEL:
470 raise SystemExit
471 elif n == ARGV_OPTION_GROUP:
472 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
473 _selectoption(d, optionlist, idx)
474 elif n == ARGV_OPTION_VALUE:
475 pass
476 elif n == ARGV_OPTION_ADD:
477 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
478 if 0 <= idx < len(optionlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000479 option = optionlist[idx]
480 if type(option) == type(()):
481 option = option[0]
Jack Jansenf86eda52000-09-19 22:42:38 +0000482 if option[-1] == '=' or option[-1] == ':':
483 option = option[:-1]
484 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
485 value = Dlg.GetDialogItemText(h)
486 else:
487 value = ''
488 if len(option) == 1:
489 stringtoadd = '-' + option
490 else:
491 stringtoadd = '--' + option
492 stringstoadd = [stringtoadd]
493 if value:
494 stringstoadd.append(value)
495 else:
496 MacOS.SysBeep()
497 elif n == ARGV_COMMAND_GROUP:
498 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
Jack Jansen0bb0a902000-09-21 22:01:08 +0000499 if 0 <= idx < len(commandlist) and type(commandlist[idx]) == type(()) and \
Jack Jansenf86eda52000-09-19 22:42:38 +0000500 len(commandlist[idx]) > 1:
501 help = commandlist[idx][-1]
502 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
503 Dlg.SetDialogItemText(h, help)
504 elif n == ARGV_COMMAND_ADD:
505 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
506 if 0 <= idx < len(commandlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000507 command = commandlist[idx]
508 if type(command) == type(()):
509 command = command[0]
510 stringstoadd = [command]
Jack Jansenf86eda52000-09-19 22:42:38 +0000511 else:
512 MacOS.SysBeep()
513 elif n == ARGV_ADD_OLDFILE:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000514 pathname = AskFileForOpen()
515 if pathname:
516 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000517 elif n == ARGV_ADD_NEWFILE:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000518 pathname = AskFileForSave()
519 if pathname:
520 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000521 elif n == ARGV_ADD_FOLDER:
Jack Jansen08a7a0d2003-01-21 13:56:34 +0000522 pathname = AskFolder()
523 if pathname:
524 stringstoadd = [pathname]
Jack Jansenf86eda52000-09-19 22:42:38 +0000525 elif n == ARGV_CMDLINE_DATA:
526 pass # Nothing to do
527 else:
528 raise RuntimeError, "Unknown dialog item %d"%n
529
530 for stringtoadd in stringstoadd:
531 if '"' in stringtoadd or "'" in stringtoadd or " " in stringtoadd:
532 stringtoadd = `stringtoadd`
533 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
534 oldstr = GetDialogItemText(h)
535 if oldstr and oldstr[-1] != ' ':
536 oldstr = oldstr + ' '
537 oldstr = oldstr + stringtoadd
538 if oldstr[-1] != ' ':
539 oldstr = oldstr + ' '
540 SetDialogItemText(h, oldstr)
541 d.SelectDialogItemText(ARGV_CMDLINE_DATA, 0x7fff, 0x7fff)
542 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
543 oldstr = GetDialogItemText(h)
544 tmplist = string.split(oldstr)
545 newlist = []
546 while tmplist:
547 item = tmplist[0]
548 del tmplist[0]
549 if item[0] == '"':
550 while item[-1] != '"':
551 if not tmplist:
552 raise RuntimeError, "Unterminated quoted argument"
553 item = item + ' ' + tmplist[0]
554 del tmplist[0]
555 item = item[1:-1]
556 if item[0] == "'":
557 while item[-1] != "'":
558 if not tmplist:
559 raise RuntimeError, "Unterminated quoted argument"
560 item = item + ' ' + tmplist[0]
561 del tmplist[0]
562 item = item[1:-1]
563 newlist.append(item)
564 return newlist
565 finally:
Jack Janseneb308432001-09-09 00:36:01 +0000566 if hasattr(MacOS, 'SchedParams'):
567 apply(MacOS.SchedParams, appsw)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000568 del d
Jack Jansenf0d12da2003-01-17 16:04:39 +0000569
Jack Jansen3032fe62003-01-21 14:38:32 +0000570def _process_Nav_args(dftflags, **args):
Jack Jansenf0d12da2003-01-17 16:04:39 +0000571 import aepack
572 import Carbon.AE
573 import Carbon.File
Jack Jansenf0d12da2003-01-17 16:04:39 +0000574 for k in args.keys():
Jack Jansen3032fe62003-01-21 14:38:32 +0000575 if args[k] is None:
576 del args[k]
Jack Jansenf0d12da2003-01-17 16:04:39 +0000577 # Set some defaults, and modify some arguments
578 if not args.has_key('dialogOptionFlags'):
579 args['dialogOptionFlags'] = dftflags
580 if args.has_key('defaultLocation') and \
581 not isinstance(args['defaultLocation'], Carbon.AE.AEDesc):
582 defaultLocation = args['defaultLocation']
583 if isinstance(defaultLocation, (Carbon.File.FSSpec, Carbon.File.FSRef)):
584 args['defaultLocation'] = aepack.pack(defaultLocation)
585 else:
586 defaultLocation = Carbon.File.FSRef(defaultLocation)
587 args['defaultLocation'] = aepack.pack(defaultLocation)
588 if args.has_key('typeList') and not isinstance(args['typeList'], Carbon.Res.ResourceType):
Jack Jansene1c4f0b2003-01-21 22:57:53 +0000589 typeList = args['typeList'][:]
Jack Jansenf0d12da2003-01-17 16:04:39 +0000590 # Workaround for OSX typeless files:
591 if 'TEXT' in typeList and not '\0\0\0\0' in typeList:
592 typeList = typeList + ('\0\0\0\0',)
593 data = 'Pyth' + struct.pack("hh", 0, len(typeList))
594 for type in typeList:
595 data = data+type
596 args['typeList'] = Carbon.Res.Handle(data)
597 tpwanted = str
598 if args.has_key('wanted'):
599 tpwanted = args['wanted']
600 del args['wanted']
601 return args, tpwanted
602
Jack Jansen2731c5c2003-02-07 15:45:40 +0000603def _dummy_Nav_eventproc(msg, data):
604 pass
605
606_default_Nav_eventproc = _dummy_Nav_eventproc
607
608def SetDefaultEventProc(proc):
609 global _default_Nav_eventproc
610 rv = _default_Nav_eventproc
611 if proc is None:
612 proc = _dummy_Nav_eventproc
613 _default_Nav_eventproc = proc
614 return rv
615
Jack Jansen3032fe62003-01-21 14:38:32 +0000616def AskFileForOpen(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000617 message=None,
618 typeList=None,
619 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000620 version=None,
621 defaultLocation=None,
622 dialogOptionFlags=None,
623 location=None,
624 clientName=None,
625 windowTitle=None,
626 actionButtonLabel=None,
627 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000628 preferenceKey=None,
629 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000630 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000631 previewProc=None,
632 filterProc=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000633 wanted=None,
634 multiple=None):
635 """Display a dialog asking the user for a file to open.
636
637 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
638 the other arguments can be looked up in Apple's Navigation Services documentation"""
639
Jack Jansenf0d12da2003-01-17 16:04:39 +0000640 default_flags = 0x56 # Or 0xe4?
Jack Jansen3032fe62003-01-21 14:38:32 +0000641 args, tpwanted = _process_Nav_args(default_flags, version=version,
642 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
643 location=location,clientName=clientName,windowTitle=windowTitle,
644 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
645 message=message,preferenceKey=preferenceKey,
646 popupExtension=popupExtension,eventProc=eventProc,previewProc=previewProc,
647 filterProc=filterProc,typeList=typeList,wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000648 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000649 try:
650 rr = Nav.NavChooseFile(args)
651 good = 1
652 except Nav.error, arg:
653 if arg[0] != -128: # userCancelledErr
654 raise Nav.error, arg
655 return None
656 if not rr.validRecord or not rr.selection:
657 return None
658 if issubclass(tpwanted, Carbon.File.FSRef):
659 return tpwanted(rr.selection_fsr[0])
660 if issubclass(tpwanted, Carbon.File.FSSpec):
661 return tpwanted(rr.selection[0])
662 if issubclass(tpwanted, str):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000663 return tpwanted(rr.selection_fsr[0].as_pathname())
Jack Jansenf0d12da2003-01-17 16:04:39 +0000664 if issubclass(tpwanted, unicode):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000665 return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
Jack Jansenf0d12da2003-01-17 16:04:39 +0000666 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
667
Jack Jansen3032fe62003-01-21 14:38:32 +0000668def AskFileForSave(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000669 message=None,
670 savedFileName=None,
671 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000672 version=None,
673 defaultLocation=None,
674 dialogOptionFlags=None,
675 location=None,
676 clientName=None,
677 windowTitle=None,
678 actionButtonLabel=None,
679 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000680 preferenceKey=None,
681 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000682 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000683 fileType=None,
684 fileCreator=None,
685 wanted=None,
686 multiple=None):
687 """Display a dialog asking the user for a filename to save to.
688
689 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
690 the other arguments can be looked up in Apple's Navigation Services documentation"""
691
692
Jack Jansenf0d12da2003-01-17 16:04:39 +0000693 default_flags = 0x07
Jack Jansen3032fe62003-01-21 14:38:32 +0000694 args, tpwanted = _process_Nav_args(default_flags, version=version,
695 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
696 location=location,clientName=clientName,windowTitle=windowTitle,
697 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
698 savedFileName=savedFileName,message=message,preferenceKey=preferenceKey,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000699 popupExtension=popupExtension,eventProc=eventProc,fileType=fileType,
700 fileCreator=fileCreator,wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000701 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000702 try:
703 rr = Nav.NavPutFile(args)
704 good = 1
705 except Nav.error, arg:
706 if arg[0] != -128: # userCancelledErr
707 raise Nav.error, arg
708 return None
709 if not rr.validRecord or not rr.selection:
710 return None
711 if issubclass(tpwanted, Carbon.File.FSRef):
712 raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave"
713 if issubclass(tpwanted, Carbon.File.FSSpec):
714 return tpwanted(rr.selection[0])
715 if issubclass(tpwanted, (str, unicode)):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000716 if sys.platform == 'mac':
717 fullpath = rr.selection[0].as_pathname()
718 else:
719 # This is gross, and probably incorrect too
720 vrefnum, dirid, name = rr.selection[0].as_tuple()
721 pardir_fss = Carbon.File.FSSpec((vrefnum, dirid, ''))
722 pardir_fsr = Carbon.File.FSRef(pardir_fss)
723 pardir_path = pardir_fsr.FSRefMakePath() # This is utf-8
724 name_utf8 = unicode(name, 'macroman').encode('utf8')
725 fullpath = os.path.join(pardir_path, name_utf8)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000726 if issubclass(tpwanted, unicode):
727 return unicode(fullpath, 'utf8')
728 return tpwanted(fullpath)
729 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
730
Jack Jansen3032fe62003-01-21 14:38:32 +0000731def AskFolder(
Jack Jansen2731c5c2003-02-07 15:45:40 +0000732 message=None,
733 # From here on the order is not documented
Jack Jansen3032fe62003-01-21 14:38:32 +0000734 version=None,
735 defaultLocation=None,
736 dialogOptionFlags=None,
737 location=None,
738 clientName=None,
739 windowTitle=None,
740 actionButtonLabel=None,
741 cancelButtonLabel=None,
Jack Jansen3032fe62003-01-21 14:38:32 +0000742 preferenceKey=None,
743 popupExtension=None,
Jack Jansen2731c5c2003-02-07 15:45:40 +0000744 eventProc=_dummy_Nav_eventproc,
Jack Jansen3032fe62003-01-21 14:38:32 +0000745 filterProc=None,
746 wanted=None,
747 multiple=None):
748 """Display a dialog asking the user for select a folder.
749
750 wanted is the return type wanted: FSSpec, FSRef, unicode or string (default)
751 the other arguments can be looked up in Apple's Navigation Services documentation"""
752
Jack Jansenf0d12da2003-01-17 16:04:39 +0000753 default_flags = 0x17
Jack Jansen3032fe62003-01-21 14:38:32 +0000754 args, tpwanted = _process_Nav_args(default_flags, version=version,
755 defaultLocation=defaultLocation, dialogOptionFlags=dialogOptionFlags,
756 location=location,clientName=clientName,windowTitle=windowTitle,
757 actionButtonLabel=actionButtonLabel,cancelButtonLabel=cancelButtonLabel,
758 message=message,preferenceKey=preferenceKey,
759 popupExtension=popupExtension,eventProc=eventProc,filterProc=filterProc,
760 wanted=wanted,multiple=multiple)
Jack Jansen7451e3b2003-03-03 12:25:02 +0000761 _interact()
Jack Jansenf0d12da2003-01-17 16:04:39 +0000762 try:
763 rr = Nav.NavChooseFolder(args)
764 good = 1
765 except Nav.error, arg:
766 if arg[0] != -128: # userCancelledErr
767 raise Nav.error, arg
768 return None
769 if not rr.validRecord or not rr.selection:
770 return None
771 if issubclass(tpwanted, Carbon.File.FSRef):
772 return tpwanted(rr.selection_fsr[0])
773 if issubclass(tpwanted, Carbon.File.FSSpec):
774 return tpwanted(rr.selection[0])
775 if issubclass(tpwanted, str):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000776 return tpwanted(rr.selection_fsr[0].as_pathname())
Jack Jansenf0d12da2003-01-17 16:04:39 +0000777 if issubclass(tpwanted, unicode):
Jack Jansen2b3ce3b2003-01-26 20:22:41 +0000778 return tpwanted(rr.selection_fsr[0].as_pathname(), 'utf8')
Jack Jansenf0d12da2003-01-17 16:04:39 +0000779 raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted)
780
781
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000782def test():
Jack Jansend9bb1a02003-02-21 23:18:48 +0000783 import time
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000784
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000785 Message("Testing EasyDialogs.")
Jack Jansenf86eda52000-09-19 22:42:38 +0000786 optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'),
787 ('flags=', 'Valued option'), ('f:', 'Short valued option'))
788 commandlist = (('start', 'Start something'), ('stop', 'Stop something'))
789 argv = GetArgv(optionlist=optionlist, commandlist=commandlist, addoldfile=0)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000790 Message("Command line: %s"%' '.join(argv))
Jack Jansenf86eda52000-09-19 22:42:38 +0000791 for i in range(len(argv)):
792 print 'arg[%d] = %s'%(i, `argv[i]`)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000793 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000794 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000795 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000796 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000797 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
798 if not s2:
799 Message("%s has no secret nickname"%s)
800 else:
801 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansenf0d12da2003-01-17 16:04:39 +0000802 else:
803 s = 'Anonymous'
Jack Jansend9bb1a02003-02-21 23:18:48 +0000804 rv = AskFileForOpen(message="Gimme a file, %s"%s, wanted=Carbon.File.FSSpec)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000805 Message("rv: %s"%rv)
Jack Jansend9bb1a02003-02-21 23:18:48 +0000806 rv = AskFileForSave(wanted=Carbon.File.FSRef, savedFileName="%s.txt"%s)
Jack Jansenf0d12da2003-01-17 16:04:39 +0000807 Message("rv.as_pathname: %s"%rv.as_pathname())
808 rv = AskFolder()
809 Message("Folder name: %s"%rv)
Jack Jansen911e87d2001-08-27 15:24:07 +0000810 text = ( "Working Hard...", "Hardly Working..." ,
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000811 "So far, so good!", "Keep on truckin'" )
Jack Jansen911e87d2001-08-27 15:24:07 +0000812 bar = ProgressBar("Progress, progress...", 0, label="Ramping up...")
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000813 try:
Jack Janseneb308432001-09-09 00:36:01 +0000814 if hasattr(MacOS, 'SchedParams'):
815 appsw = MacOS.SchedParams(1, 0)
Jack Jansen911e87d2001-08-27 15:24:07 +0000816 for i in xrange(20):
817 bar.inc()
818 time.sleep(0.05)
819 bar.set(0,100)
820 for i in xrange(100):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000821 bar.set(i)
Jack Jansen911e87d2001-08-27 15:24:07 +0000822 time.sleep(0.05)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000823 if i % 10 == 0:
824 bar.label(text[(i/10) % 4])
825 bar.label("Done.")
Jack Jansen911e87d2001-08-27 15:24:07 +0000826 time.sleep(1.0) # give'em a chance to see "Done."
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000827 finally:
828 del bar
Jack Janseneb308432001-09-09 00:36:01 +0000829 if hasattr(MacOS, 'SchedParams'):
830 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000831
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000832if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000833 try:
834 test()
835 except KeyboardInterrupt:
836 Message("Operation Canceled.")
837