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
36 if _initialized
: return
37 macresource
.need("DLOG", 260, "dialogs.rsrc", __name__
)
42 text
= string
.join(string
.split(text
, '\r'), '\n')
47 text
= string
.join(string
.split(text
, '\n'), '\r')
49 text
= text
[:253] + '\311'
52 def Message(msg
, id=260, ok
=None):
53 """Display a MESSAGE string.
55 Return when the user clicks the OK button or presses Return.
57 The MESSAGE string can be at most 255 characters long.
60 d
= GetNewDialog(id, -1)
62 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
64 h
= d
.GetDialogItemAsControl(2)
65 SetDialogItemText(h
, lf2cr(msg
))
67 h
= d
.GetDialogItemAsControl(1)
69 d
.SetDialogDefaultItem(1)
71 d
.GetDialogWindow().ShowWindow()
78 def AskString(prompt
, default
= "", id=261, ok
=None, cancel
=None):
79 """Display a PROMPT string and a text entry field with a DEFAULT string.
81 Return the contents of the text entry field when the user clicks the
82 OK button or presses Return.
83 Return None when the user clicks the Cancel button.
85 If omitted, DEFAULT is empty.
87 The PROMPT and DEFAULT strings, as well as the return value,
88 can be at most 255 characters long.
92 d
= GetNewDialog(id, -1)
94 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
96 h
= d
.GetDialogItemAsControl(3)
97 SetDialogItemText(h
, lf2cr(prompt
))
98 h
= d
.GetDialogItemAsControl(4)
99 SetDialogItemText(h
, lf2cr(default
))
100 d
.SelectDialogItemText(4, 0, 999)
101 # d.SetDialogItem(4, 0, 255)
103 h
= d
.GetDialogItemAsControl(1)
104 h
.SetControlTitle(ok
)
106 h
= d
.GetDialogItemAsControl(2)
107 h
.SetControlTitle(cancel
)
108 d
.SetDialogDefaultItem(1)
109 d
.SetDialogCancelItem(2)
111 d
.GetDialogWindow().ShowWindow()
113 n
= ModalDialog(None)
115 h
= d
.GetDialogItemAsControl(4)
116 return cr2lf(GetDialogItemText(h
))
117 if n
== 2: return None
119 def AskPassword(prompt
, default
='', id=264, ok
=None, cancel
=None):
120 """Display a PROMPT string and a text entry field with a DEFAULT string.
121 The string is displayed as bullets only.
123 Return the contents of the text entry field when the user clicks the
124 OK button or presses Return.
125 Return None when the user clicks the Cancel button.
127 If omitted, DEFAULT is empty.
129 The PROMPT and DEFAULT strings, as well as the return value,
130 can be at most 255 characters long.
133 d
= GetNewDialog(id, -1)
135 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
137 h
= d
.GetDialogItemAsControl(3)
138 SetDialogItemText(h
, lf2cr(prompt
))
139 pwd
= d
.GetDialogItemAsControl(4)
140 bullets
= '\245'*len(default
)
141 ## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
142 SetControlData(pwd
, kControlEditTextPart
, kControlEditTextPasswordTag
, default
)
143 d
.SelectDialogItemText(4, 0, 999)
144 Ctl
.SetKeyboardFocus(d
.GetDialogWindow(), pwd
, kControlEditTextPart
)
146 h
= d
.GetDialogItemAsControl(1)
147 h
.SetControlTitle(ok
)
149 h
= d
.GetDialogItemAsControl(2)
150 h
.SetControlTitle(cancel
)
151 d
.SetDialogDefaultItem(Dialogs
.ok
)
152 d
.SetDialogCancelItem(Dialogs
.cancel
)
154 d
.GetDialogWindow().ShowWindow()
156 n
= ModalDialog(None)
158 h
= d
.GetDialogItemAsControl(4)
159 return cr2lf(GetControlData(pwd
, kControlEditTextPart
, kControlEditTextPasswordTag
))
160 if n
== 2: return None
162 def AskYesNoCancel(question
, default
= 0, yes
=None, no
=None, cancel
=None, id=262):
163 """Display a QUESTION string which can be answered with Yes or No.
165 Return 1 when the user clicks the Yes button.
166 Return 0 when the user clicks the No button.
167 Return -1 when the user clicks the Cancel button.
169 When the user presses Return, the DEFAULT value is returned.
170 If omitted, this is 0 (No).
172 The QUESTION string can be at most 255 characters.
176 d
= GetNewDialog(id, -1)
178 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
180 # Button assignments:
181 # 1 = default (invisible)
185 # The question string is item 5
186 h
= d
.GetDialogItemAsControl(5)
187 SetDialogItemText(h
, lf2cr(question
))
192 h
= d
.GetDialogItemAsControl(2)
193 h
.SetControlTitle(yes
)
198 h
= d
.GetDialogItemAsControl(3)
199 h
.SetControlTitle(no
)
204 h
= d
.GetDialogItemAsControl(4)
205 h
.SetControlTitle(cancel
)
206 d
.SetDialogCancelItem(4)
208 d
.SetDialogDefaultItem(2)
210 d
.SetDialogDefaultItem(3)
212 d
.SetDialogDefaultItem(4)
214 d
.GetDialogWindow().ShowWindow()
216 n
= ModalDialog(None)
217 if n
== 1: return default
225 screenbounds
= Qd
.GetQDGlobalsScreenBits().bounds
226 screenbounds
= screenbounds
[0]+4, screenbounds
[1]+4, \
227 screenbounds
[2]-4, screenbounds
[3]-4
229 kControlProgressBarIndeterminateTag
= 'inde' # from Controls.py
233 def __init__(self
, title
="Working...", maxval
=0, label
="", id=263):
237 self
.d
= GetNewDialog(id, -1)
238 self
.w
= self
.d
.GetDialogWindow()
242 self
.d
.AutoSizeDialog()
248 self
.w
.BringToFront()
253 def title(self
, newstr
=""):
254 """title(text) - Set title of progress window"""
255 self
.w
.BringToFront()
256 self
.w
.SetWTitle(newstr
)
258 def label( self
, *newstr
):
259 """label(text) - Set text in progress box"""
260 self
.w
.BringToFront()
262 self
._label
= lf2cr(newstr
[0])
263 text_h
= self
.d
.GetDialogItemAsControl(2)
264 SetDialogItemText(text_h
, self
._label
)
266 def _update(self
, value
):
268 if maxval
== 0: # an indeterminate bar
269 Ctl
.IdleControls(self
.w
) # spin the barber pole
270 else: # a determinate bar
272 value
= int(value
/(maxval
/32767.0))
274 progbar
= self
.d
.GetDialogItemAsControl(3)
275 progbar
.SetControlMaximum(maxval
)
276 progbar
.SetControlValue(value
) # set the bar length
278 # Test for cancel button
279 ready
, ev
= Evt
.WaitNextEvent( Events
.mDownMask
, 1 )
281 what
,msg
,when
,where
,mod
= ev
282 part
= Win
.FindWindow(where
)[0]
283 if Dlg
.IsDialogEvent(ev
):
284 ds
= Dlg
.DialogSelect(ev
)
285 if ds
[0] and ds
[1] == self
.d
and ds
[-1] == 1:
289 raise KeyboardInterrupt, ev
291 if part
== 4: # inDrag
292 self
.w
.DragWindow(where
, screenbounds
)
294 MacOS
.HandleEvent(ev
)
297 def set(self
, value
, max=None):
298 """set(value) - Set progress bar position"""
301 bar
= self
.d
.GetDialogItemAsControl(3)
302 if max <= 0: # indeterminate bar
303 bar
.SetControlData(0,kControlProgressBarIndeterminateTag
,'\x01')
304 else: # determinate bar
305 bar
.SetControlData(0,kControlProgressBarIndeterminateTag
,'\x00')
308 elif value
> self
.maxval
:
314 """inc(amt) - Increment progress bar position"""
315 self
.set(self
.curval
+ n
)
321 ARGV_OPTION_EXPLAIN
=4
325 ARGV_COMMAND_EXPLAIN
=8
330 ARGV_CMDLINE_GROUP
=13
333 ##def _myModalDialog(d):
335 ## ready, ev = Evt.WaitNextEvent(0xffff, -1)
336 ## print 'DBG: WNE', ready, ev
338 ## what,msg,when,where,mod = ev
339 ## part, window = Win.FindWindow(where)
340 ## if Dlg.IsDialogEvent(ev):
341 ## didit, dlgdone, itemdone = Dlg.DialogSelect(ev)
342 ## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d
343 ## if didit and dlgdone == d:
345 ## elif window == d.GetDialogWindow():
346 ## d.GetDialogWindow().SelectWindow()
347 ## if part == 4: # inDrag
348 ## d.DragWindow(where, screenbounds)
350 ## MacOS.HandleEvent(ev)
352 ## MacOS.HandleEvent(ev)
354 def _setmenu(control
, items
):
355 mhandle
= control
.GetControlData_Handle(Controls
.kControlMenuPart
,
356 Controls
.kControlPopupButtonMenuHandleTag
)
357 menu
= Menu
.as_Menu(mhandle
)
359 if type(item
) == type(()):
363 if label
[-1] == '=' or label
[-1] == ':':
365 menu
.AppendMenu(label
)
366 ## mhandle, mid = menu.getpopupinfo()
367 ## control.SetControlData_Handle(Controls.kControlMenuPart,
368 ## Controls.kControlPopupButtonMenuHandleTag, mhandle)
369 control
.SetControlMinimum(1)
370 control
.SetControlMaximum(len(items
)+1)
372 def _selectoption(d
, optionlist
, idx
):
373 if idx
< 0 or idx
>= len(optionlist
):
376 option
= optionlist
[idx
]
377 if type(option
) == type(()):
380 elif len(option
) > 1:
386 h
= d
.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN
)
387 if help and len(help) > 250:
388 help = help[:250] + '...'
389 Dlg
.SetDialogItemText(h
, help)
391 if type(option
) == type(()):
395 if label
[-1] == '=' or label
[-1] == ':':
397 h
= d
.GetDialogItemAsControl(ARGV_OPTION_VALUE
)
398 Dlg
.SetDialogItemText(h
, '')
400 d
.ShowDialogItem(ARGV_OPTION_VALUE
)
401 d
.SelectDialogItemText(ARGV_OPTION_VALUE
, 0, 0)
403 d
.HideDialogItem(ARGV_OPTION_VALUE
)
406 def GetArgv(optionlist
=None, commandlist
=None, addoldfile
=1, addnewfile
=1, addfolder
=1, id=ARGV_ID
):
408 d
= GetNewDialog(id, -1)
410 print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)"
412 # h = d.GetDialogItemAsControl(3)
413 # SetDialogItemText(h, lf2cr(prompt))
414 # h = d.GetDialogItemAsControl(4)
415 # SetDialogItemText(h, lf2cr(default))
416 # d.SelectDialogItemText(4, 0, 999)
417 # d.SetDialogItem(4, 0, 255)
419 _setmenu(d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
), optionlist
)
420 _selectoption(d
, optionlist
, 0)
422 d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
).DeactivateControl()
424 _setmenu(d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
), commandlist
)
425 if type(commandlist
[0]) == type(()) and len(commandlist
[0]) > 1:
426 help = commandlist
[0][-1]
427 h
= d
.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN
)
428 Dlg
.SetDialogItemText(h
, help)
430 d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
).DeactivateControl()
432 d
.GetDialogItemAsControl(ARGV_ADD_OLDFILE
).DeactivateControl()
434 d
.GetDialogItemAsControl(ARGV_ADD_NEWFILE
).DeactivateControl()
436 d
.GetDialogItemAsControl(ARGV_ADD_FOLDER
).DeactivateControl()
437 d
.SetDialogDefaultItem(ARGV_ITEM_OK
)
438 d
.SetDialogCancelItem(ARGV_ITEM_CANCEL
)
439 d
.GetDialogWindow().ShowWindow()
441 if hasattr(MacOS
, 'SchedParams'):
442 appsw
= MacOS
.SchedParams(1, 0)
446 n
= ModalDialog(None)
447 if n
== ARGV_ITEM_OK
:
449 elif n
== ARGV_ITEM_CANCEL
:
451 elif n
== ARGV_OPTION_GROUP
:
452 idx
= d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
).GetControlValue()-1
453 _selectoption(d
, optionlist
, idx
)
454 elif n
== ARGV_OPTION_VALUE
:
456 elif n
== ARGV_OPTION_ADD
:
457 idx
= d
.GetDialogItemAsControl(ARGV_OPTION_GROUP
).GetControlValue()-1
458 if 0 <= idx
< len(optionlist
):
459 option
= optionlist
[idx
]
460 if type(option
) == type(()):
462 if option
[-1] == '=' or option
[-1] == ':':
464 h
= d
.GetDialogItemAsControl(ARGV_OPTION_VALUE
)
465 value
= Dlg
.GetDialogItemText(h
)
469 stringtoadd
= '-' + option
471 stringtoadd
= '--' + option
472 stringstoadd
= [stringtoadd
]
474 stringstoadd
.append(value
)
477 elif n
== ARGV_COMMAND_GROUP
:
478 idx
= d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
).GetControlValue()-1
479 if 0 <= idx
< len(commandlist
) and type(commandlist
[idx
]) == type(()) and \
480 len(commandlist
[idx
]) > 1:
481 help = commandlist
[idx
][-1]
482 h
= d
.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN
)
483 Dlg
.SetDialogItemText(h
, help)
484 elif n
== ARGV_COMMAND_ADD
:
485 idx
= d
.GetDialogItemAsControl(ARGV_COMMAND_GROUP
).GetControlValue()-1
486 if 0 <= idx
< len(commandlist
):
487 command
= commandlist
[idx
]
488 if type(command
) == type(()):
490 stringstoadd
= [command
]
493 elif n
== ARGV_ADD_OLDFILE
:
494 fss
, ok
= macfs
.StandardGetFile()
496 stringstoadd
= [fss
.as_pathname()]
497 elif n
== ARGV_ADD_NEWFILE
:
498 fss
, ok
= macfs
.StandardPutFile('')
500 stringstoadd
= [fss
.as_pathname()]
501 elif n
== ARGV_ADD_FOLDER
:
502 fss
, ok
= macfs
.GetDirectory()
504 stringstoadd
= [fss
.as_pathname()]
505 elif n
== ARGV_CMDLINE_DATA
:
508 raise RuntimeError, "Unknown dialog item %d"%n
510 for stringtoadd
in stringstoadd
:
511 if '"' in stringtoadd
or "'" in stringtoadd
or " " in stringtoadd
:
512 stringtoadd
= `stringtoadd`
513 h
= d
.GetDialogItemAsControl(ARGV_CMDLINE_DATA
)
514 oldstr
= GetDialogItemText(h
)
515 if oldstr
and oldstr
[-1] != ' ':
516 oldstr
= oldstr
+ ' '
517 oldstr
= oldstr
+ stringtoadd
518 if oldstr
[-1] != ' ':
519 oldstr
= oldstr
+ ' '
520 SetDialogItemText(h
, oldstr
)
521 d
.SelectDialogItemText(ARGV_CMDLINE_DATA
, 0x7fff, 0x7fff)
522 h
= d
.GetDialogItemAsControl(ARGV_CMDLINE_DATA
)
523 oldstr
= GetDialogItemText(h
)
524 tmplist
= string
.split(oldstr
)
530 while item
[-1] != '"':
532 raise RuntimeError, "Unterminated quoted argument"
533 item
= item
+ ' ' + tmplist
[0]
537 while item
[-1] != "'":
539 raise RuntimeError, "Unterminated quoted argument"
540 item
= item
+ ' ' + tmplist
[0]
546 if hasattr(MacOS
, 'SchedParams'):
547 apply(MacOS
.SchedParams
, appsw
)
553 Message("Testing EasyDialogs.")
554 optionlist
= (('v', 'Verbose'), ('verbose', 'Verbose as long option'),
555 ('flags=', 'Valued option'), ('f:', 'Short valued option'))
556 commandlist
= (('start', 'Start something'), ('stop', 'Stop something'))
557 argv
= GetArgv(optionlist
=optionlist
, commandlist
=commandlist
, addoldfile
=0)
558 for i
in range(len(argv
)):
559 print 'arg[%d] = %s'%(i
, `argv
[i
]`
)
560 print 'Type return to continue - ',
562 ok
= AskYesNoCancel("Do you want to proceed?")
563 ok
= AskYesNoCancel("Do you want to identify?", yes
="Identify", no
="No")
565 s
= AskString("Enter your first name", "Joe")
566 s2
= AskPassword("Okay %s, tell us your nickname"%s, s
, cancel
="None")
568 Message("%s has no secret nickname"%s)
570 Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s
, s2
))
571 text
= ( "Working Hard...", "Hardly Working..." ,
572 "So far, so good!", "Keep on truckin'" )
573 bar
= ProgressBar("Progress, progress...", 0, label
="Ramping up...")
575 if hasattr(MacOS
, 'SchedParams'):
576 appsw
= MacOS
.SchedParams(1, 0)
581 for i
in xrange(100):
585 bar
.label(text
[(i
/10) % 4])
587 time
.sleep(1.0) # give'em a chance to see "Done."
590 if hasattr(MacOS
, 'SchedParams'):
591 apply(MacOS
.SchedParams
, appsw
)
593 if __name__
== '__main__':
596 except KeyboardInterrupt:
597 Message("Operation Canceled.")