1 """Easy to use dialogs.
3 Message(msg) -- display a message and an OK button.
4 AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
5 AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
6 AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
7 bar = Progress(label, maxvalue) -- Display a progress bar
8 bar.set(value) -- Set value
9 bar.inc( *amount ) -- increment value by amount (default=1)
10 bar.label( *newlabel ) -- get or set text label.
12 More documentation in each function.
13 This module uses DLOG resources 260 and on.
14 Based upon STDWIN dialogs with the same names and functions.
17 from Carbon
.Dlg
import GetNewDialog
, SetDialogItemText
, GetDialogItemText
, ModalDialog
19 from Carbon
import QuickDraw
20 from Carbon
import Dialogs
21 from Carbon
import Windows
22 from Carbon
import Dlg
,Win
,Evt
,Events
# sdm7g
23 from Carbon
import Ctl
24 from Carbon
import Controls
25 from Carbon
import Menu
28 from Carbon
.ControlAccessor
import * # Also import Controls constants
33 text
= string
.join(string
.split(text
, '\r'), '\n')
38 text
= string
.join(string
.split(text
, '\n'), '\r')
40 text
= text
[:253] + '\311'
43 def Message(msg
, id=260, ok
=None):
44 """Display a MESSAGE string.
46 Return when the user clicks the OK button or presses Return.
48 The MESSAGE string can be at most 255 characters long.
51 d
= GetNewDialog(id, -1)
53 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
55 h
= d
.GetDialogItemAsControl(2)
56 SetDialogItemText(h
, lf2cr(msg
))
58 h
= d
.GetDialogItemAsControl(1)
60 d
.SetDialogDefaultItem(1)
62 d
.GetDialogWindow().ShowWindow()
69 def AskString(prompt
, default
= "", id=261, ok
=None, cancel
=None):
70 """Display a PROMPT string and a text entry field with a DEFAULT string.
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.
76 If omitted, DEFAULT is empty.
78 The PROMPT and DEFAULT strings, as well as the return value,
79 can be at most 255 characters long.
82 d
= GetNewDialog(id, -1)
84 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
86 h
= d
.GetDialogItemAsControl(3)
87 SetDialogItemText(h
, lf2cr(prompt
))
88 h
= d
.GetDialogItemAsControl(4)
89 SetDialogItemText(h
, lf2cr(default
))
90 d
.SelectDialogItemText(4, 0, 999)
91 # d.SetDialogItem(4, 0, 255)
93 h
= d
.GetDialogItemAsControl(1)
96 h
= d
.GetDialogItemAsControl(2)
97 h
.SetControlTitle(cancel
)
98 d
.SetDialogDefaultItem(1)
99 d
.SetDialogCancelItem(2)
101 d
.GetDialogWindow().ShowWindow()
103 n
= ModalDialog(None)
105 h
= d
.GetDialogItemAsControl(4)
106 return cr2lf(GetDialogItemText(h
))
107 if n
== 2: return None
109 def AskPassword(prompt
, default
='', id=264, ok
=None, cancel
=None):
110 """Display a PROMPT string and a text entry field with a DEFAULT string.
111 The string is displayed as bullets only.
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.
117 If omitted, DEFAULT is empty.
119 The PROMPT and DEFAULT strings, as well as the return value,
120 can be at most 255 characters long.
122 d
= GetNewDialog(id, -1)
124 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
126 h
= d
.GetDialogItemAsControl(3)
127 SetDialogItemText(h
, lf2cr(prompt
))
128 pwd
= d
.GetDialogItemAsControl(4)
129 bullets
= '\245'*len(default
)
130 ## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
131 SetControlData(pwd
, kControlEditTextPart
, kControlEditTextPasswordTag
, default
)
132 d
.SelectDialogItemText(4, 0, 999)
133 Ctl
.SetKeyboardFocus(d
.GetDialogWindow(), pwd
, kControlEditTextPart
)
135 h
= d
.GetDialogItemAsControl(1)
136 h
.SetControlTitle(ok
)
138 h
= d
.GetDialogItemAsControl(2)
139 h
.SetControlTitle(cancel
)
140 d
.SetDialogDefaultItem(Dialogs
.ok
)
141 d
.SetDialogCancelItem(Dialogs
.cancel
)
143 d
.GetDialogWindow().ShowWindow()
145 n
= ModalDialog(None)
147 h
= d
.GetDialogItemAsControl(4)
148 return cr2lf(GetControlData(pwd
, kControlEditTextPart
, kControlEditTextPasswordTag
))
149 if n
== 2: return None
151 def AskYesNoCancel(question
, default
= 0, yes
=None, no
=None, cancel
=None, id=262):
152 """Display a QUESTION string which can be answered with Yes or No.
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.
158 When the user presses Return, the DEFAULT value is returned.
159 If omitted, this is 0 (No).
161 The QUESTION string can be at most 255 characters.
164 d
= GetNewDialog(id, -1)
166 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
168 # Button assignments:
169 # 1 = default (invisible)
173 # The question string is item 5
174 h
= d
.GetDialogItemAsControl(5)
175 SetDialogItemText(h
, lf2cr(question
))
180 h
= d
.GetDialogItemAsControl(2)
181 h
.SetControlTitle(yes
)
186 h
= d
.GetDialogItemAsControl(3)
187 h
.SetControlTitle(no
)
192 h
= d
.GetDialogItemAsControl(4)
193 h
.SetControlTitle(cancel
)
194 d
.SetDialogCancelItem(4)
196 d
.SetDialogDefaultItem(2)
198 d
.SetDialogDefaultItem(3)
200 d
.SetDialogDefaultItem(4)
202 d
.GetDialogWindow().ShowWindow()
204 n
= ModalDialog(None)
205 if n
== 1: return default
213 screenbounds
= Qd
.qd
.screenBits
.bounds
214 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
215 screenbounds
[2]-4, screenbounds
[3]-4
217 kControlProgressBarIndeterminateTag
= 'inde' # from Controls.py
221 def __init__(self
, title
="Working...", maxval
=0, label
="", id=263):
224 self
.d
= GetNewDialog(id, -1)
225 self
.w
= self
.d
.GetDialogWindow()
229 self
.d
.AutoSizeDialog()
235 self
.w
.BringToFront()
240 def title(self
, newstr
=""):
241 """title(text) - Set title of progress window"""
242 self
.w
.BringToFront()
243 self
.w
.SetWTitle(newstr
)
245 def label( self
, *newstr
):
246 """label(text) - Set text in progress box"""
247 self
.w
.BringToFront()
249 self
._label
= lf2cr(newstr
[0])
250 text_h
= self
.d
.GetDialogItemAsControl(2)
251 SetDialogItemText(text_h
, self
._label
)
253 def _update(self
, value
):
255 if maxval
== 0: # an indeterminate bar
256 Ctl
.IdleControls(self
.w
) # spin the barber pole
257 else: # a determinate bar
259 value
= int(value
/(maxval
/32767.0))
261 progbar
= self
.d
.GetDialogItemAsControl(3)
262 progbar
.SetControlMaximum(maxval
)
263 progbar
.SetControlValue(value
) # set the bar length
265 # Test for cancel button
266 ready
, ev
= Evt
.WaitNextEvent( Events
.mDownMask
, 1 )
268 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:
276 raise KeyboardInterrupt, ev
278 if part
== 4: # inDrag
279 self
.w
.DragWindow(where
, screenbounds
)
281 MacOS
.HandleEvent(ev
)
284 def set(self
, value
, max=None):
285 """set(value) - Set progress bar position"""
288 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')
295 elif value
> self
.maxval
:
301 """inc(amt) - Increment progress bar position"""
302 self
.set(self
.curval
+ n
)
308 ARGV_OPTION_EXPLAIN
=4
312 ARGV_COMMAND_EXPLAIN
=8
317 ARGV_CMDLINE_GROUP
=13
320 ##def _myModalDialog(d):
322 ## ready, ev = Evt.WaitNextEvent(0xffff, -1)
323 ## print 'DBG: WNE', ready, ev
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:
332 ## elif window == d.GetDialogWindow():
333 ## d.GetDialogWindow().SelectWindow()
334 ## if part == 4: # inDrag
335 ## d.DragWindow(where, screenbounds)
337 ## MacOS.HandleEvent(ev)
339 ## MacOS.HandleEvent(ev)
341 def _setmenu(control
, items
):
342 mhandle
= control
.GetControlData_Handle(Controls
.kControlMenuPart
,
343 Controls
.kControlPopupButtonMenuHandleTag
)
344 menu
= Menu
.as_Menu(mhandle
)
346 if type(item
) == type(()):
350 if label
[-1] == '=' or label
[-1] == ':':
352 menu
.AppendMenu(label
)
353 ## mhandle, mid = menu.getpopupinfo()
354 ## control.SetControlData_Handle(Controls.kControlMenuPart,
355 ## Controls.kControlPopupButtonMenuHandleTag, mhandle)
356 control
.SetControlMinimum(1)
357 control
.SetControlMaximum(len(items
)+1)
359 def _selectoption(d
, optionlist
, idx
):
360 if idx
< 0 or idx
>= len(optionlist
):
363 option
= optionlist
[idx
]
364 if type(option
) == type(()):
367 elif len(option
) > 1:
373 h
= d
.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN
)
374 if help and len(help) > 250:
375 help = help[:250] + '...'
376 Dlg
.SetDialogItemText(h
, help)
378 if type(option
) == type(()):
382 if label
[-1] == '=' or label
[-1] == ':':
384 h
= d
.GetDialogItemAsControl(ARGV_OPTION_VALUE
)
385 Dlg
.SetDialogItemText(h
, '')
387 d
.ShowDialogItem(ARGV_OPTION_VALUE
)
388 d
.SelectDialogItemText(ARGV_OPTION_VALUE
, 0, 0)
390 d
.HideDialogItem(ARGV_OPTION_VALUE
)
393 def GetArgv(optionlist
=None, commandlist
=None, addoldfile
=1, addnewfile
=1, addfolder
=1, id=ARGV_ID
):
394 d
= GetNewDialog(id, -1)
396 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
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)
405 _setmenu(d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
), optionlist
)
406 _selectoption(d
, optionlist
, 0)
408 d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
).DeactivateControl()
410 _setmenu(d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
), commandlist
)
411 if type(commandlist
[0]) == type(()) and len(commandlist
[0]) > 1:
412 help = commandlist
[0][-1]
413 h
= d
.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN
)
414 Dlg
.SetDialogItemText(h
, help)
416 d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
).DeactivateControl()
418 d
.GetDialogItemAsControl(ARGV_ADD_OLDFILE
).DeactivateControl()
420 d
.GetDialogItemAsControl(ARGV_ADD_NEWFILE
).DeactivateControl()
422 d
.GetDialogItemAsControl(ARGV_ADD_FOLDER
).DeactivateControl()
423 d
.SetDialogDefaultItem(ARGV_ITEM_OK
)
424 d
.SetDialogCancelItem(ARGV_ITEM_CANCEL
)
425 d
.GetDialogWindow().ShowWindow()
427 if hasattr(MacOS
, 'SchedParams'):
428 appsw
= MacOS
.SchedParams(1, 0)
432 n
= ModalDialog(None)
433 if n
== ARGV_ITEM_OK
:
435 elif n
== ARGV_ITEM_CANCEL
:
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
:
442 elif n
== ARGV_OPTION_ADD
:
443 idx
= d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
).GetControlValue()-1
444 if 0 <= idx
< len(optionlist
):
445 option
= optionlist
[idx
]
446 if type(option
) == type(()):
448 if option
[-1] == '=' or option
[-1] == ':':
450 h
= d
.GetDialogItemAsControl(ARGV_OPTION_VALUE
)
451 value
= Dlg
.GetDialogItemText(h
)
455 stringtoadd
= '-' + option
457 stringtoadd
= '--' + option
458 stringstoadd
= [stringtoadd
]
460 stringstoadd
.append(value
)
463 elif n
== ARGV_COMMAND_GROUP
:
464 idx
= d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
).GetControlValue()-1
465 if 0 <= idx
< len(commandlist
) and type(commandlist
[idx
]) == type(()) and \
466 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
):
473 command
= commandlist
[idx
]
474 if type(command
) == type(()):
476 stringstoadd
= [command
]
479 elif n
== ARGV_ADD_OLDFILE
:
480 fss
, ok
= macfs
.StandardGetFile()
482 stringstoadd
= [fss
.as_pathname()]
483 elif n
== ARGV_ADD_NEWFILE
:
484 fss
, ok
= macfs
.StandardPutFile('')
486 stringstoadd
= [fss
.as_pathname()]
487 elif n
== ARGV_ADD_FOLDER
:
488 fss
, ok
= macfs
.GetDirectory()
490 stringstoadd
= [fss
.as_pathname()]
491 elif n
== ARGV_CMDLINE_DATA
:
494 raise RuntimeError, "Unknown dialog item %d"%n
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
)
516 while item
[-1] != '"':
518 raise RuntimeError, "Unterminated quoted argument"
519 item
= item
+ ' ' + tmplist
[0]
523 while item
[-1] != "'":
525 raise RuntimeError, "Unterminated quoted argument"
526 item
= item
+ ' ' + tmplist
[0]
532 if hasattr(MacOS
, 'SchedParams'):
533 apply(MacOS
.SchedParams
, appsw
)
539 Message("Testing EasyDialogs.")
540 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 - ',
548 ok
= AskYesNoCancel("Do you want to proceed?")
549 ok
= AskYesNoCancel("Do you want to identify?", yes
="Identify", no
="No")
551 s
= AskString("Enter your first name", "Joe")
552 s2
= AskPassword("Okay %s, tell us your nickname"%s, s
, cancel
="None")
554 Message("%s has no secret nickname"%s)
556 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s
, s2
))
557 text
= ( "Working Hard...", "Hardly Working..." ,
558 "So far, so good!", "Keep on truckin'" )
559 bar
= ProgressBar("Progress, progress...", 0, label
="Ramping up...")
561 if hasattr(MacOS
, 'SchedParams'):
562 appsw
= MacOS
.SchedParams(1, 0)
567 for i
in xrange(100):
571 bar
.label(text
[(i
/10) % 4])
573 time
.sleep(1.0) # give'em a chance to see "Done."
576 if hasattr(MacOS
, 'SchedParams'):
577 apply(MacOS
.SchedParams
, appsw
)
579 if __name__
== '__main__':
582 except KeyboardInterrupt:
583 Message("Operation Canceled.")