blob: 394e9123d36e049d3c588dc7b136f4b44510b3ce [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 Jansen3a87f5b1995-11-14 10:13:49 +00007bar = Progress(label, maxvalue) -- Display a progress bar
8bar.set(value) -- Set value
Jack Jansen1d63d8c1997-05-12 15:44:14 +00009bar.inc( *amount ) -- increment value by amount (default=1)
10bar.label( *newlabel ) -- get or set text label.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000011
12More documentation in each function.
Jack Jansencc386881999-12-12 22:57:51 +000013This module uses DLOG resources 260 and on.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000014Based upon STDWIN dialogs with the same names and functions.
15"""
16
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000017from Carbon.Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
18from Carbon import Qd
19from Carbon import QuickDraw
20from Carbon import Dialogs
21from Carbon import Windows
22from Carbon import Dlg,Win,Evt,Events # sdm7g
23from Carbon import Ctl
24from Carbon import Controls
25from Carbon import Menu
Jack Jansena5a49811998-07-01 15:47:44 +000026import MacOS
27import string
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000028from Carbon.ControlAccessor import * # Also import Controls constants
Jack Jansenf86eda52000-09-19 22:42:38 +000029import macfs
Jack Jansena5a49811998-07-01 15:47:44 +000030
31def cr2lf(text):
32 if '\r' in text:
33 text = string.join(string.split(text, '\r'), '\n')
34 return text
35
36def lf2cr(text):
37 if '\n' in text:
38 text = string.join(string.split(text, '\n'), '\r')
Jack Jansend5af7bd1998-09-28 10:37:08 +000039 if len(text) > 253:
40 text = text[:253] + '\311'
Jack Jansena5a49811998-07-01 15:47:44 +000041 return text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000042
Jack Jansencc386881999-12-12 22:57:51 +000043def Message(msg, id=260, ok=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000044 """Display a MESSAGE string.
45
46 Return when the user clicks the OK button or presses Return.
47
48 The MESSAGE string can be at most 255 characters long.
49 """
50
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000051 d = GetNewDialog(id, -1)
52 if not d:
53 print "Can't get DLOG resource with id =", id
54 return
Jack Jansencc386881999-12-12 22:57:51 +000055 h = d.GetDialogItemAsControl(2)
Jack Jansena5a49811998-07-01 15:47:44 +000056 SetDialogItemText(h, lf2cr(msg))
Jack Jansen208c15a1999-02-16 16:06:39 +000057 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000058 h = d.GetDialogItemAsControl(1)
59 h.SetControlTitle(ok)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000060 d.SetDialogDefaultItem(1)
Jack Jansenfca049d2000-01-18 13:36:02 +000061 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +000062 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000063 while 1:
64 n = ModalDialog(None)
65 if n == 1:
66 return
67
68
Jack Jansencc386881999-12-12 22:57:51 +000069def AskString(prompt, default = "", id=261, ok=None, cancel=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000070 """Display a PROMPT string and a text entry field with a DEFAULT string.
71
72 Return the contents of the text entry field when the user clicks the
73 OK button or presses Return.
74 Return None when the user clicks the Cancel button.
75
76 If omitted, DEFAULT is empty.
77
78 The PROMPT and DEFAULT strings, as well as the return value,
79 can be at most 255 characters long.
80 """
81
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000082 d = GetNewDialog(id, -1)
83 if not d:
84 print "Can't get DLOG resource with id =", id
85 return
Jack Jansencc386881999-12-12 22:57:51 +000086 h = d.GetDialogItemAsControl(3)
Jack Jansena5a49811998-07-01 15:47:44 +000087 SetDialogItemText(h, lf2cr(prompt))
Jack Jansencc386881999-12-12 22:57:51 +000088 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +000089 SetDialogItemText(h, lf2cr(default))
Jack Jansend61f92b1999-01-22 13:14:06 +000090 d.SelectDialogItemText(4, 0, 999)
Jack Jansene4b40381995-07-17 13:25:15 +000091# d.SetDialogItem(4, 0, 255)
Jack Jansen208c15a1999-02-16 16:06:39 +000092 if ok != None:
Jack Jansencc386881999-12-12 22:57:51 +000093 h = d.GetDialogItemAsControl(1)
94 h.SetControlTitle(ok)
Jack Jansen208c15a1999-02-16 16:06:39 +000095 if cancel != None:
Jack Jansencc386881999-12-12 22:57:51 +000096 h = d.GetDialogItemAsControl(2)
97 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +000098 d.SetDialogDefaultItem(1)
99 d.SetDialogCancelItem(2)
Jack Jansenfca049d2000-01-18 13:36:02 +0000100 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000101 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000102 while 1:
103 n = ModalDialog(None)
104 if n == 1:
Jack Jansencc386881999-12-12 22:57:51 +0000105 h = d.GetDialogItemAsControl(4)
Jack Jansena5a49811998-07-01 15:47:44 +0000106 return cr2lf(GetDialogItemText(h))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000107 if n == 2: return None
108
Jack Jansen60429e01999-12-13 16:07:01 +0000109def AskPassword(prompt, default='', id=264, ok=None, cancel=None):
Jack Jansenb92268a1999-02-10 22:38:44 +0000110 """Display a PROMPT string and a text entry field with a DEFAULT string.
111 The string is displayed as bullets only.
112
113 Return the contents of the text entry field when the user clicks the
114 OK button or presses Return.
115 Return None when the user clicks the Cancel button.
116
117 If omitted, DEFAULT is empty.
118
119 The PROMPT and DEFAULT strings, as well as the return value,
120 can be at most 255 characters long.
121 """
122 d = GetNewDialog(id, -1)
123 if not d:
124 print "Can't get DLOG resource with id =", id
125 return
Jack Jansen60429e01999-12-13 16:07:01 +0000126 h = d.GetDialogItemAsControl(3)
Jack Jansenb92268a1999-02-10 22:38:44 +0000127 SetDialogItemText(h, lf2cr(prompt))
Jack Jansen60429e01999-12-13 16:07:01 +0000128 pwd = d.GetDialogItemAsControl(4)
Jack Jansenb92268a1999-02-10 22:38:44 +0000129 bullets = '\245'*len(default)
Jack Jansen60429e01999-12-13 16:07:01 +0000130## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
131 SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default)
132 d.SelectDialogItemText(4, 0, 999)
Jack Jansen0c1836f2000-08-25 22:06:19 +0000133 Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart)
Jack Jansen60429e01999-12-13 16:07:01 +0000134 if ok != None:
135 h = d.GetDialogItemAsControl(1)
136 h.SetControlTitle(ok)
137 if cancel != None:
138 h = d.GetDialogItemAsControl(2)
139 h.SetControlTitle(cancel)
Jack Jansenb92268a1999-02-10 22:38:44 +0000140 d.SetDialogDefaultItem(Dialogs.ok)
141 d.SetDialogCancelItem(Dialogs.cancel)
Jack Jansenfca049d2000-01-18 13:36:02 +0000142 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000143 d.GetDialogWindow().ShowWindow()
Jack Jansenb92268a1999-02-10 22:38:44 +0000144 while 1:
Jack Jansen60429e01999-12-13 16:07:01 +0000145 n = ModalDialog(None)
146 if n == 1:
147 h = d.GetDialogItemAsControl(4)
148 return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag))
149 if n == 2: return None
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000150
Jack Jansencc386881999-12-12 22:57:51 +0000151def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262):
Jack Jansencf2efc61999-02-25 22:05:45 +0000152 """Display a QUESTION string which can be answered with Yes or No.
153
154 Return 1 when the user clicks the Yes button.
155 Return 0 when the user clicks the No button.
156 Return -1 when the user clicks the Cancel button.
157
158 When the user presses Return, the DEFAULT value is returned.
159 If omitted, this is 0 (No).
160
Just van Rossum639a7402001-06-26 06:57:12 +0000161 The QUESTION string can be at most 255 characters.
Jack Jansencf2efc61999-02-25 22:05:45 +0000162 """
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000163
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000164 d = GetNewDialog(id, -1)
165 if not d:
166 print "Can't get DLOG resource with id =", id
167 return
168 # Button assignments:
169 # 1 = default (invisible)
170 # 2 = Yes
171 # 3 = No
172 # 4 = Cancel
173 # The question string is item 5
Jack Jansencc386881999-12-12 22:57:51 +0000174 h = d.GetDialogItemAsControl(5)
Jack Jansena5a49811998-07-01 15:47:44 +0000175 SetDialogItemText(h, lf2cr(question))
Jack Jansen3f5aef71997-06-20 16:23:37 +0000176 if yes != None:
Jack Jansen85743782000-02-10 16:15:53 +0000177 if yes == '':
178 d.HideDialogItem(2)
179 else:
180 h = d.GetDialogItemAsControl(2)
181 h.SetControlTitle(yes)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000182 if no != None:
Jack Jansen85743782000-02-10 16:15:53 +0000183 if no == '':
184 d.HideDialogItem(3)
185 else:
186 h = d.GetDialogItemAsControl(3)
187 h.SetControlTitle(no)
Jack Jansen3f5aef71997-06-20 16:23:37 +0000188 if cancel != None:
Jack Jansen208c15a1999-02-16 16:06:39 +0000189 if cancel == '':
190 d.HideDialogItem(4)
191 else:
Jack Jansencc386881999-12-12 22:57:51 +0000192 h = d.GetDialogItemAsControl(4)
193 h.SetControlTitle(cancel)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000194 d.SetDialogCancelItem(4)
Jack Jansen0b690db1996-04-10 14:49:41 +0000195 if default == 1:
196 d.SetDialogDefaultItem(2)
197 elif default == 0:
198 d.SetDialogDefaultItem(3)
199 elif default == -1:
200 d.SetDialogDefaultItem(4)
Jack Jansenfca049d2000-01-18 13:36:02 +0000201 d.AutoSizeDialog()
Jack Jansen0c1836f2000-08-25 22:06:19 +0000202 d.GetDialogWindow().ShowWindow()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000203 while 1:
204 n = ModalDialog(None)
205 if n == 1: return default
206 if n == 2: return 1
207 if n == 3: return 0
208 if n == 4: return -1
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000209
210
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000211
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000212
213screenbounds = Qd.qd.screenBits.bounds
214screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
215 screenbounds[2]-4, screenbounds[3]-4
216
Jack Jansen911e87d2001-08-27 15:24:07 +0000217kControlProgressBarIndeterminateTag = 'inde' # from Controls.py
218
219
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000220class ProgressBar:
Jack Jansen911e87d2001-08-27 15:24:07 +0000221 def __init__(self, title="Working...", maxval=0, label="", id=263):
Jack Jansen5dd73622001-02-23 22:18:27 +0000222 self.w = None
223 self.d = None
Jack Jansen3f5aef71997-06-20 16:23:37 +0000224 self.d = GetNewDialog(id, -1)
Jack Jansen784c6112001-02-09 15:57:01 +0000225 self.w = self.d.GetDialogWindow()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000226 self.label(label)
Jack Jansenab48e902000-07-24 14:07:15 +0000227 self.title(title)
Jack Jansen911e87d2001-08-27 15:24:07 +0000228 self.set(0, maxval)
229 self.d.AutoSizeDialog()
Jack Jansen784c6112001-02-09 15:57:01 +0000230 self.w.ShowWindow()
Jack Jansencc386881999-12-12 22:57:51 +0000231 self.d.DrawDialog()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000232
233 def __del__( self ):
Jack Jansen5dd73622001-02-23 22:18:27 +0000234 if self.w:
235 self.w.BringToFront()
236 self.w.HideWindow()
Jack Jansen784c6112001-02-09 15:57:01 +0000237 del self.w
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000238 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000239
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000240 def title(self, newstr=""):
241 """title(text) - Set title of progress window"""
Jack Jansen784c6112001-02-09 15:57:01 +0000242 self.w.BringToFront()
243 self.w.SetWTitle(newstr)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000244
245 def label( self, *newstr ):
246 """label(text) - Set text in progress box"""
Jack Jansen784c6112001-02-09 15:57:01 +0000247 self.w.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000248 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000249 self._label = lf2cr(newstr[0])
Jack Jansencc386881999-12-12 22:57:51 +0000250 text_h = self.d.GetDialogItemAsControl(2)
Jack Jansen911e87d2001-08-27 15:24:07 +0000251 SetDialogItemText(text_h, self._label)
252
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000253 def _update(self, value):
Jack Jansen58fa8181999-11-05 15:53:10 +0000254 maxval = self.maxval
Jack Jansen911e87d2001-08-27 15:24:07 +0000255 if maxval == 0: # an indeterminate bar
256 Ctl.IdleControls(self.w) # spin the barber pole
257 else: # a determinate bar
258 if maxval > 32767:
259 value = int(value/(maxval/32767.0))
260 maxval = 32767
261 progbar = self.d.GetDialogItemAsControl(3)
262 progbar.SetControlMaximum(maxval)
263 progbar.SetControlValue(value) # set the bar length
264
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000265 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000266 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
Jack Jansen911e87d2001-08-27 15:24:07 +0000267 if ready :
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000268 what,msg,when,where,mod = ev
269 part = Win.FindWindow(where)[0]
270 if Dlg.IsDialogEvent(ev):
271 ds = Dlg.DialogSelect(ev)
272 if ds[0] and ds[1] == self.d and ds[-1] == 1:
Jack Jansen5dd73622001-02-23 22:18:27 +0000273 self.w.HideWindow()
274 self.w = None
275 self.d = None
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000276 raise KeyboardInterrupt, ev
277 else:
278 if part == 4: # inDrag
Jack Jansen8d2f3d62001-07-27 09:21:28 +0000279 self.w.DragWindow(where, screenbounds)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000280 else:
281 MacOS.HandleEvent(ev)
282
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000283
Jack Jansen58fa8181999-11-05 15:53:10 +0000284 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000285 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000286 if max != None:
287 self.maxval = max
Jack Jansen911e87d2001-08-27 15:24:07 +0000288 bar = self.d.GetDialogItemAsControl(3)
289 if max <= 0: # indeterminate bar
290 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x01')
291 else: # determinate bar
292 bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x00')
293 if value < 0:
294 value = 0
295 elif value > self.maxval:
296 value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000297 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000298 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000299
300 def inc(self, n=1):
301 """inc(amt) - Increment progress bar position"""
302 self.set(self.curval + n)
303
Jack Jansenf86eda52000-09-19 22:42:38 +0000304ARGV_ID=265
305ARGV_ITEM_OK=1
306ARGV_ITEM_CANCEL=2
307ARGV_OPTION_GROUP=3
308ARGV_OPTION_EXPLAIN=4
309ARGV_OPTION_VALUE=5
310ARGV_OPTION_ADD=6
311ARGV_COMMAND_GROUP=7
312ARGV_COMMAND_EXPLAIN=8
313ARGV_COMMAND_ADD=9
314ARGV_ADD_OLDFILE=10
315ARGV_ADD_NEWFILE=11
316ARGV_ADD_FOLDER=12
317ARGV_CMDLINE_GROUP=13
318ARGV_CMDLINE_DATA=14
319
320##def _myModalDialog(d):
321## while 1:
322## ready, ev = Evt.WaitNextEvent(0xffff, -1)
323## print 'DBG: WNE', ready, ev
324## if ready :
325## what,msg,when,where,mod = ev
326## part, window = Win.FindWindow(where)
327## if Dlg.IsDialogEvent(ev):
328## didit, dlgdone, itemdone = Dlg.DialogSelect(ev)
329## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d
330## if didit and dlgdone == d:
331## return itemdone
332## elif window == d.GetDialogWindow():
333## d.GetDialogWindow().SelectWindow()
334## if part == 4: # inDrag
335## d.DragWindow(where, screenbounds)
336## else:
337## MacOS.HandleEvent(ev)
338## else:
339## MacOS.HandleEvent(ev)
340##
341def _setmenu(control, items):
Jack Jansene7bfc912001-01-09 22:22:58 +0000342 mhandle = control.GetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000343 Controls.kControlPopupButtonMenuHandleTag)
344 menu = Menu.as_Menu(mhandle)
345 for item in items:
346 if type(item) == type(()):
347 label = item[0]
348 else:
349 label = item
350 if label[-1] == '=' or label[-1] == ':':
351 label = label[:-1]
352 menu.AppendMenu(label)
353## mhandle, mid = menu.getpopupinfo()
Jack Jansene7bfc912001-01-09 22:22:58 +0000354## control.SetControlData_Handle(Controls.kControlMenuPart,
Jack Jansenf86eda52000-09-19 22:42:38 +0000355## Controls.kControlPopupButtonMenuHandleTag, mhandle)
356 control.SetControlMinimum(1)
357 control.SetControlMaximum(len(items)+1)
358
359def _selectoption(d, optionlist, idx):
360 if idx < 0 or idx >= len(optionlist):
361 MacOS.SysBeep()
362 return
363 option = optionlist[idx]
Jack Jansen09c73432002-06-26 15:14:48 +0000364 if type(option) == type(()):
365 if len(option) == 4:
366 help = option[2]
367 elif len(option) > 1:
368 help = option[-1]
369 else:
370 help = ''
Jack Jansenf86eda52000-09-19 22:42:38 +0000371 else:
372 help = ''
373 h = d.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN)
Jack Jansen09c73432002-06-26 15:14:48 +0000374 if help and len(help) > 250:
375 help = help[:250] + '...'
Jack Jansenf86eda52000-09-19 22:42:38 +0000376 Dlg.SetDialogItemText(h, help)
377 hasvalue = 0
378 if type(option) == type(()):
379 label = option[0]
380 else:
381 label = option
382 if label[-1] == '=' or label[-1] == ':':
383 hasvalue = 1
384 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
385 Dlg.SetDialogItemText(h, '')
386 if hasvalue:
387 d.ShowDialogItem(ARGV_OPTION_VALUE)
388 d.SelectDialogItemText(ARGV_OPTION_VALUE, 0, 0)
389 else:
390 d.HideDialogItem(ARGV_OPTION_VALUE)
391
392
393def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID):
394 d = GetNewDialog(id, -1)
395 if not d:
396 print "Can't get DLOG resource with id =", id
397 return
398# h = d.GetDialogItemAsControl(3)
399# SetDialogItemText(h, lf2cr(prompt))
400# h = d.GetDialogItemAsControl(4)
401# SetDialogItemText(h, lf2cr(default))
402# d.SelectDialogItemText(4, 0, 999)
403# d.SetDialogItem(4, 0, 255)
404 if optionlist:
405 _setmenu(d.GetDialogItemAsControl(ARGV_OPTION_GROUP), optionlist)
406 _selectoption(d, optionlist, 0)
407 else:
408 d.GetDialogItemAsControl(ARGV_OPTION_GROUP).DeactivateControl()
409 if commandlist:
410 _setmenu(d.GetDialogItemAsControl(ARGV_COMMAND_GROUP), commandlist)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000411 if type(commandlist[0]) == type(()) and len(commandlist[0]) > 1:
Jack Jansenf86eda52000-09-19 22:42:38 +0000412 help = commandlist[0][-1]
413 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
414 Dlg.SetDialogItemText(h, help)
415 else:
416 d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).DeactivateControl()
417 if not addoldfile:
418 d.GetDialogItemAsControl(ARGV_ADD_OLDFILE).DeactivateControl()
419 if not addnewfile:
420 d.GetDialogItemAsControl(ARGV_ADD_NEWFILE).DeactivateControl()
421 if not addfolder:
422 d.GetDialogItemAsControl(ARGV_ADD_FOLDER).DeactivateControl()
423 d.SetDialogDefaultItem(ARGV_ITEM_OK)
424 d.SetDialogCancelItem(ARGV_ITEM_CANCEL)
425 d.GetDialogWindow().ShowWindow()
426 d.DrawDialog()
Jack Janseneb308432001-09-09 00:36:01 +0000427 if hasattr(MacOS, 'SchedParams'):
428 appsw = MacOS.SchedParams(1, 0)
Jack Jansenf86eda52000-09-19 22:42:38 +0000429 try:
430 while 1:
431 stringstoadd = []
432 n = ModalDialog(None)
433 if n == ARGV_ITEM_OK:
434 break
435 elif n == ARGV_ITEM_CANCEL:
436 raise SystemExit
437 elif n == ARGV_OPTION_GROUP:
438 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
439 _selectoption(d, optionlist, idx)
440 elif n == ARGV_OPTION_VALUE:
441 pass
442 elif n == ARGV_OPTION_ADD:
443 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
444 if 0 <= idx < len(optionlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000445 option = optionlist[idx]
446 if type(option) == type(()):
447 option = option[0]
Jack Jansenf86eda52000-09-19 22:42:38 +0000448 if option[-1] == '=' or option[-1] == ':':
449 option = option[:-1]
450 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
451 value = Dlg.GetDialogItemText(h)
452 else:
453 value = ''
454 if len(option) == 1:
455 stringtoadd = '-' + option
456 else:
457 stringtoadd = '--' + option
458 stringstoadd = [stringtoadd]
459 if value:
460 stringstoadd.append(value)
461 else:
462 MacOS.SysBeep()
463 elif n == ARGV_COMMAND_GROUP:
464 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
Jack Jansen0bb0a902000-09-21 22:01:08 +0000465 if 0 <= idx < len(commandlist) and type(commandlist[idx]) == type(()) and \
Jack Jansenf86eda52000-09-19 22:42:38 +0000466 len(commandlist[idx]) > 1:
467 help = commandlist[idx][-1]
468 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
469 Dlg.SetDialogItemText(h, help)
470 elif n == ARGV_COMMAND_ADD:
471 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
472 if 0 <= idx < len(commandlist):
Jack Jansen0bb0a902000-09-21 22:01:08 +0000473 command = commandlist[idx]
474 if type(command) == type(()):
475 command = command[0]
476 stringstoadd = [command]
Jack Jansenf86eda52000-09-19 22:42:38 +0000477 else:
478 MacOS.SysBeep()
479 elif n == ARGV_ADD_OLDFILE:
480 fss, ok = macfs.StandardGetFile()
481 if ok:
482 stringstoadd = [fss.as_pathname()]
483 elif n == ARGV_ADD_NEWFILE:
484 fss, ok = macfs.StandardPutFile('')
485 if ok:
486 stringstoadd = [fss.as_pathname()]
487 elif n == ARGV_ADD_FOLDER:
488 fss, ok = macfs.GetDirectory()
489 if ok:
490 stringstoadd = [fss.as_pathname()]
491 elif n == ARGV_CMDLINE_DATA:
492 pass # Nothing to do
493 else:
494 raise RuntimeError, "Unknown dialog item %d"%n
495
496 for stringtoadd in stringstoadd:
497 if '"' in stringtoadd or "'" in stringtoadd or " " in stringtoadd:
498 stringtoadd = `stringtoadd`
499 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
500 oldstr = GetDialogItemText(h)
501 if oldstr and oldstr[-1] != ' ':
502 oldstr = oldstr + ' '
503 oldstr = oldstr + stringtoadd
504 if oldstr[-1] != ' ':
505 oldstr = oldstr + ' '
506 SetDialogItemText(h, oldstr)
507 d.SelectDialogItemText(ARGV_CMDLINE_DATA, 0x7fff, 0x7fff)
508 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
509 oldstr = GetDialogItemText(h)
510 tmplist = string.split(oldstr)
511 newlist = []
512 while tmplist:
513 item = tmplist[0]
514 del tmplist[0]
515 if item[0] == '"':
516 while item[-1] != '"':
517 if not tmplist:
518 raise RuntimeError, "Unterminated quoted argument"
519 item = item + ' ' + tmplist[0]
520 del tmplist[0]
521 item = item[1:-1]
522 if item[0] == "'":
523 while item[-1] != "'":
524 if not tmplist:
525 raise RuntimeError, "Unterminated quoted argument"
526 item = item + ' ' + tmplist[0]
527 del tmplist[0]
528 item = item[1:-1]
529 newlist.append(item)
530 return newlist
531 finally:
Jack Janseneb308432001-09-09 00:36:01 +0000532 if hasattr(MacOS, 'SchedParams'):
533 apply(MacOS.SchedParams, appsw)
Jack Jansen0bb0a902000-09-21 22:01:08 +0000534 del d
Jack Jansenf86eda52000-09-19 22:42:38 +0000535
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000536def test():
Jack Jansenf86eda52000-09-19 22:42:38 +0000537 import time, sys
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000538
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000539 Message("Testing EasyDialogs.")
Jack Jansenf86eda52000-09-19 22:42:38 +0000540 optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'),
541 ('flags=', 'Valued option'), ('f:', 'Short valued option'))
542 commandlist = (('start', 'Start something'), ('stop', 'Stop something'))
543 argv = GetArgv(optionlist=optionlist, commandlist=commandlist, addoldfile=0)
544 for i in range(len(argv)):
545 print 'arg[%d] = %s'%(i, `argv[i]`)
546 print 'Type return to continue - ',
547 sys.stdin.readline()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000548 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000549 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000550 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000551 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000552 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
553 if not s2:
554 Message("%s has no secret nickname"%s)
555 else:
556 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansen911e87d2001-08-27 15:24:07 +0000557 text = ( "Working Hard...", "Hardly Working..." ,
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000558 "So far, so good!", "Keep on truckin'" )
Jack Jansen911e87d2001-08-27 15:24:07 +0000559 bar = ProgressBar("Progress, progress...", 0, label="Ramping up...")
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000560 try:
Jack Janseneb308432001-09-09 00:36:01 +0000561 if hasattr(MacOS, 'SchedParams'):
562 appsw = MacOS.SchedParams(1, 0)
Jack Jansen911e87d2001-08-27 15:24:07 +0000563 for i in xrange(20):
564 bar.inc()
565 time.sleep(0.05)
566 bar.set(0,100)
567 for i in xrange(100):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000568 bar.set(i)
Jack Jansen911e87d2001-08-27 15:24:07 +0000569 time.sleep(0.05)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000570 if i % 10 == 0:
571 bar.label(text[(i/10) % 4])
572 bar.label("Done.")
Jack Jansen911e87d2001-08-27 15:24:07 +0000573 time.sleep(1.0) # give'em a chance to see "Done."
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000574 finally:
575 del bar
Jack Janseneb308432001-09-09 00:36:01 +0000576 if hasattr(MacOS, 'SchedParams'):
577 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000578
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000579if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000580 try:
581 test()
582 except KeyboardInterrupt:
583 Message("Operation Canceled.")
584