blob: c830c558d5a42c0ea64fc25eedc3b18be4c6b309 [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 Jansene4b40381995-07-17 13:25:15 +000017from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
Jack Jansen3a87f5b1995-11-14 10:13:49 +000018import Qd
Jack Jansen3a87f5b1995-11-14 10:13:49 +000019import QuickDraw
Jack Jansenb92268a1999-02-10 22:38:44 +000020import Dialogs
21import Windows
Jack Jansen1d63d8c1997-05-12 15:44:14 +000022import Dlg,Win,Evt,Events # sdm7g
Jack Jansen60429e01999-12-13 16:07:01 +000023import Ctl
Jack Jansenf86eda52000-09-19 22:42:38 +000024import Controls
25import Menu
Jack Jansena5a49811998-07-01 15:47:44 +000026import MacOS
27import string
Jack Jansen60429e01999-12-13 16:07:01 +000028from 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
161 The QUESTION strign ca be at most 255 characters.
162 """
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
217
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000218class ProgressBar:
Jack Jansencc386881999-12-12 22:57:51 +0000219 def __init__(self, title="Working...", maxval=100, label="", id=263):
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000220 self.maxval = maxval
221 self.curval = -1
Jack Jansen3f5aef71997-06-20 16:23:37 +0000222 self.d = GetNewDialog(id, -1)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000223 self.label(label)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000224 self._update(0)
Jack Jansenfca049d2000-01-18 13:36:02 +0000225 self.d.AutoSizeDialog()
Jack Jansenab48e902000-07-24 14:07:15 +0000226 self.title(title)
Jack Jansen0c1836f2000-08-25 22:06:19 +0000227 self.d.GetDialogWindow().ShowWindow()
Jack Jansencc386881999-12-12 22:57:51 +0000228 self.d.DrawDialog()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000229
230 def __del__( self ):
Jack Jansen48c55271997-05-13 15:41:07 +0000231 self.d.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000232 self.d.HideWindow()
233 del self.d
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000234
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000235 def title(self, newstr=""):
236 """title(text) - Set title of progress window"""
237 w = self.d.GetDialogWindow()
Jack Jansenab48e902000-07-24 14:07:15 +0000238 w.BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000239 w.SetWTitle(newstr)
240
241 def label( self, *newstr ):
242 """label(text) - Set text in progress box"""
Jack Jansenab48e902000-07-24 14:07:15 +0000243 self.d.GetDialogWindow().BringToFront()
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000244 if newstr:
Jack Jansena5a49811998-07-01 15:47:44 +0000245 self._label = lf2cr(newstr[0])
Jack Jansencc386881999-12-12 22:57:51 +0000246 text_h = self.d.GetDialogItemAsControl(2)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000247 SetDialogItemText(text_h, self._label)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000248
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000249 def _update(self, value):
Jack Jansen58fa8181999-11-05 15:53:10 +0000250 maxval = self.maxval
251 if maxval == 0:
252 # XXXX Quick fix. Should probably display an unknown duration
253 value = 0
254 maxval = 1
Jack Jansencc386881999-12-12 22:57:51 +0000255 if maxval > 32767:
256 value = int(value/(maxval/32767.0))
257 maxval = 32767
258 progbar = self.d.GetDialogItemAsControl(3)
259 progbar.SetControlMaximum(maxval)
260 progbar.SetControlValue(value)
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000261 # Test for cancel button
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000262
263 ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 )
264 if ready :
265 what,msg,when,where,mod = ev
266 part = Win.FindWindow(where)[0]
267 if Dlg.IsDialogEvent(ev):
268 ds = Dlg.DialogSelect(ev)
269 if ds[0] and ds[1] == self.d and ds[-1] == 1:
270 raise KeyboardInterrupt, ev
271 else:
272 if part == 4: # inDrag
273 self.d.DragWindow(where, screenbounds)
274 else:
275 MacOS.HandleEvent(ev)
276
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000277
Jack Jansen58fa8181999-11-05 15:53:10 +0000278 def set(self, value, max=None):
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000279 """set(value) - Set progress bar position"""
Jack Jansen58fa8181999-11-05 15:53:10 +0000280 if max != None:
281 self.maxval = max
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000282 if value < 0: value = 0
283 if value > self.maxval: value = self.maxval
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000284 self.curval = value
Jack Jansen3a87f5b1995-11-14 10:13:49 +0000285 self._update(value)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000286
287 def inc(self, n=1):
288 """inc(amt) - Increment progress bar position"""
289 self.set(self.curval + n)
290
Jack Jansenf86eda52000-09-19 22:42:38 +0000291ARGV_ID=265
292ARGV_ITEM_OK=1
293ARGV_ITEM_CANCEL=2
294ARGV_OPTION_GROUP=3
295ARGV_OPTION_EXPLAIN=4
296ARGV_OPTION_VALUE=5
297ARGV_OPTION_ADD=6
298ARGV_COMMAND_GROUP=7
299ARGV_COMMAND_EXPLAIN=8
300ARGV_COMMAND_ADD=9
301ARGV_ADD_OLDFILE=10
302ARGV_ADD_NEWFILE=11
303ARGV_ADD_FOLDER=12
304ARGV_CMDLINE_GROUP=13
305ARGV_CMDLINE_DATA=14
306
307##def _myModalDialog(d):
308## while 1:
309## ready, ev = Evt.WaitNextEvent(0xffff, -1)
310## print 'DBG: WNE', ready, ev
311## if ready :
312## what,msg,when,where,mod = ev
313## part, window = Win.FindWindow(where)
314## if Dlg.IsDialogEvent(ev):
315## didit, dlgdone, itemdone = Dlg.DialogSelect(ev)
316## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d
317## if didit and dlgdone == d:
318## return itemdone
319## elif window == d.GetDialogWindow():
320## d.GetDialogWindow().SelectWindow()
321## if part == 4: # inDrag
322## d.DragWindow(where, screenbounds)
323## else:
324## MacOS.HandleEvent(ev)
325## else:
326## MacOS.HandleEvent(ev)
327##
328def _setmenu(control, items):
329 mhandle = control.GetControlDataHandle(Controls.kControlMenuPart,
330 Controls.kControlPopupButtonMenuHandleTag)
331 menu = Menu.as_Menu(mhandle)
332 for item in items:
333 if type(item) == type(()):
334 label = item[0]
335 else:
336 label = item
337 if label[-1] == '=' or label[-1] == ':':
338 label = label[:-1]
339 menu.AppendMenu(label)
340## mhandle, mid = menu.getpopupinfo()
341## control.SetControlDataHandle(Controls.kControlMenuPart,
342## Controls.kControlPopupButtonMenuHandleTag, mhandle)
343 control.SetControlMinimum(1)
344 control.SetControlMaximum(len(items)+1)
345
346def _selectoption(d, optionlist, idx):
347 if idx < 0 or idx >= len(optionlist):
348 MacOS.SysBeep()
349 return
350 option = optionlist[idx]
351 if type(option) == type(()) and \
352 len(option) > 1:
353 help = option[-1]
354 else:
355 help = ''
356 h = d.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN)
357 Dlg.SetDialogItemText(h, help)
358 hasvalue = 0
359 if type(option) == type(()):
360 label = option[0]
361 else:
362 label = option
363 if label[-1] == '=' or label[-1] == ':':
364 hasvalue = 1
365 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
366 Dlg.SetDialogItemText(h, '')
367 if hasvalue:
368 d.ShowDialogItem(ARGV_OPTION_VALUE)
369 d.SelectDialogItemText(ARGV_OPTION_VALUE, 0, 0)
370 else:
371 d.HideDialogItem(ARGV_OPTION_VALUE)
372
373
374def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID):
375 d = GetNewDialog(id, -1)
376 if not d:
377 print "Can't get DLOG resource with id =", id
378 return
379# h = d.GetDialogItemAsControl(3)
380# SetDialogItemText(h, lf2cr(prompt))
381# h = d.GetDialogItemAsControl(4)
382# SetDialogItemText(h, lf2cr(default))
383# d.SelectDialogItemText(4, 0, 999)
384# d.SetDialogItem(4, 0, 255)
385 if optionlist:
386 _setmenu(d.GetDialogItemAsControl(ARGV_OPTION_GROUP), optionlist)
387 _selectoption(d, optionlist, 0)
388 else:
389 d.GetDialogItemAsControl(ARGV_OPTION_GROUP).DeactivateControl()
390 if commandlist:
391 _setmenu(d.GetDialogItemAsControl(ARGV_COMMAND_GROUP), commandlist)
392 if type(commandlist) == type(()) and len(commandlist[0]) > 1:
393 help = commandlist[0][-1]
394 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
395 Dlg.SetDialogItemText(h, help)
396 else:
397 d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).DeactivateControl()
398 if not addoldfile:
399 d.GetDialogItemAsControl(ARGV_ADD_OLDFILE).DeactivateControl()
400 if not addnewfile:
401 d.GetDialogItemAsControl(ARGV_ADD_NEWFILE).DeactivateControl()
402 if not addfolder:
403 d.GetDialogItemAsControl(ARGV_ADD_FOLDER).DeactivateControl()
404 d.SetDialogDefaultItem(ARGV_ITEM_OK)
405 d.SetDialogCancelItem(ARGV_ITEM_CANCEL)
406 d.GetDialogWindow().ShowWindow()
407 d.DrawDialog()
408 appsw = MacOS.SchedParams(1, 0)
409 try:
410 while 1:
411 stringstoadd = []
412 n = ModalDialog(None)
413 if n == ARGV_ITEM_OK:
414 break
415 elif n == ARGV_ITEM_CANCEL:
416 raise SystemExit
417 elif n == ARGV_OPTION_GROUP:
418 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
419 _selectoption(d, optionlist, idx)
420 elif n == ARGV_OPTION_VALUE:
421 pass
422 elif n == ARGV_OPTION_ADD:
423 idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1
424 if 0 <= idx < len(optionlist):
425 if type(optionlist) == type(()):
426 option = optionlist[idx][0]
427 else:
428 option = optionlist[idx]
429 if option[-1] == '=' or option[-1] == ':':
430 option = option[:-1]
431 h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE)
432 value = Dlg.GetDialogItemText(h)
433 else:
434 value = ''
435 if len(option) == 1:
436 stringtoadd = '-' + option
437 else:
438 stringtoadd = '--' + option
439 stringstoadd = [stringtoadd]
440 if value:
441 stringstoadd.append(value)
442 else:
443 MacOS.SysBeep()
444 elif n == ARGV_COMMAND_GROUP:
445 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
446 if 0 <= idx < len(commandlist) and type(commandlist) == type(()) and \
447 len(commandlist[idx]) > 1:
448 help = commandlist[idx][-1]
449 h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN)
450 Dlg.SetDialogItemText(h, help)
451 elif n == ARGV_COMMAND_ADD:
452 idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1
453 if 0 <= idx < len(commandlist):
454 if type(commandlist) == type(()):
455 stringstoadd = [commandlist[idx][0]]
456 else:
457 stringstoadd = [commandlist[idx]]
458 else:
459 MacOS.SysBeep()
460 elif n == ARGV_ADD_OLDFILE:
461 fss, ok = macfs.StandardGetFile()
462 if ok:
463 stringstoadd = [fss.as_pathname()]
464 elif n == ARGV_ADD_NEWFILE:
465 fss, ok = macfs.StandardPutFile('')
466 if ok:
467 stringstoadd = [fss.as_pathname()]
468 elif n == ARGV_ADD_FOLDER:
469 fss, ok = macfs.GetDirectory()
470 if ok:
471 stringstoadd = [fss.as_pathname()]
472 elif n == ARGV_CMDLINE_DATA:
473 pass # Nothing to do
474 else:
475 raise RuntimeError, "Unknown dialog item %d"%n
476
477 for stringtoadd in stringstoadd:
478 if '"' in stringtoadd or "'" in stringtoadd or " " in stringtoadd:
479 stringtoadd = `stringtoadd`
480 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
481 oldstr = GetDialogItemText(h)
482 if oldstr and oldstr[-1] != ' ':
483 oldstr = oldstr + ' '
484 oldstr = oldstr + stringtoadd
485 if oldstr[-1] != ' ':
486 oldstr = oldstr + ' '
487 SetDialogItemText(h, oldstr)
488 d.SelectDialogItemText(ARGV_CMDLINE_DATA, 0x7fff, 0x7fff)
489 h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA)
490 oldstr = GetDialogItemText(h)
491 tmplist = string.split(oldstr)
492 newlist = []
493 while tmplist:
494 item = tmplist[0]
495 del tmplist[0]
496 if item[0] == '"':
497 while item[-1] != '"':
498 if not tmplist:
499 raise RuntimeError, "Unterminated quoted argument"
500 item = item + ' ' + tmplist[0]
501 del tmplist[0]
502 item = item[1:-1]
503 if item[0] == "'":
504 while item[-1] != "'":
505 if not tmplist:
506 raise RuntimeError, "Unterminated quoted argument"
507 item = item + ' ' + tmplist[0]
508 del tmplist[0]
509 item = item[1:-1]
510 newlist.append(item)
511 return newlist
512 finally:
513 apply(MacOS.SchedParams, appsw)
514
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000515def test():
Jack Jansenf86eda52000-09-19 22:42:38 +0000516 import time, sys
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000517
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000518 Message("Testing EasyDialogs.")
Jack Jansenf86eda52000-09-19 22:42:38 +0000519 optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'),
520 ('flags=', 'Valued option'), ('f:', 'Short valued option'))
521 commandlist = (('start', 'Start something'), ('stop', 'Stop something'))
522 argv = GetArgv(optionlist=optionlist, commandlist=commandlist, addoldfile=0)
523 for i in range(len(argv)):
524 print 'arg[%d] = %s'%(i, `argv[i]`)
525 print 'Type return to continue - ',
526 sys.stdin.readline()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000527 ok = AskYesNoCancel("Do you want to proceed?")
Jack Jansen60429e01999-12-13 16:07:01 +0000528 ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000529 if ok > 0:
Jack Jansend61f92b1999-01-22 13:14:06 +0000530 s = AskString("Enter your first name", "Joe")
Jack Jansen60429e01999-12-13 16:07:01 +0000531 s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
532 if not s2:
533 Message("%s has no secret nickname"%s)
534 else:
535 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000536 text = ( "Working Hard...", "Hardly Working..." ,
537 "So far, so good!", "Keep on truckin'" )
538 bar = ProgressBar("Progress, progress...", 100)
539 try:
Jack Jansen3368cb71997-06-12 10:51:18 +0000540 appsw = MacOS.SchedParams(1, 0)
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000541 for i in range(100):
542 bar.set(i)
543 time.sleep(0.1)
544 if i % 10 == 0:
545 bar.label(text[(i/10) % 4])
546 bar.label("Done.")
547 time.sleep(0.3) # give'em a chance to see the done.
548 finally:
549 del bar
Jack Jansen3368cb71997-06-12 10:51:18 +0000550 apply(MacOS.SchedParams, appsw)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000551
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000552if __name__ == '__main__':
Jack Jansen1d63d8c1997-05-12 15:44:14 +0000553 try:
554 test()
555 except KeyboardInterrupt:
556 Message("Operation Canceled.")
557