10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# tk common message boxes
20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# this module provides an interface to the native message boxes
40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# available in Tk 4.2 and newer.
50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# written by Fredrik Lundh, May 1997
70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# options (all have default values):
110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - default: which button to make default (one of the reply codes)
130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - icon: which icon to display (see below)
150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - message: the message to display
170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - parent: which window to place the dialog on top of
190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - title: dialog title
210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# - type: dialog type; that is, which buttons to display (see below)
230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom tkCommonDialog import Dialog
260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# constants
290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# icons
310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiERROR = "error"
320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiINFO = "info"
330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiQUESTION = "question"
340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiWARNING = "warning"
350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# types
370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiABORTRETRYIGNORE = "abortretryignore"
380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiOK = "ok"
390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiOKCANCEL = "okcancel"
400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiRETRYCANCEL = "retrycancel"
410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiYESNO = "yesno"
420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiYESNOCANCEL = "yesnocancel"
430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# replies
450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiABORT = "abort"
460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiRETRY = "retry"
470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiIGNORE = "ignore"
480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiOK = "ok"
490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiCANCEL = "cancel"
500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiYES = "yes"
510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiNO = "no"
520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# message dialog class
560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass Message(Dialog):
580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "A message box"
590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    command  = "tk_messageBox"
610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi#
640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# convenience stuff
650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Rename _icon and _type options to allow overriding them in options
670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef _show(title=None, message=None, _icon=None, _type=None, **options):
680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if _icon and "icon" not in options:    options["icon"] = _icon
690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if _type and "type" not in options:    options["type"] = _type
700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if title:   options["title"] = title
710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if message: options["message"] = message
720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    res = Message(**options).show()
730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # In some Tcl installations, yes/no is converted into a boolean.
740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if isinstance(res, bool):
750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if res:
760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            return YES
770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return NO
780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # In others we get a Tcl_Obj.
790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return str(res)
800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef showinfo(title=None, message=None, **options):
820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Show an info message"
830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return _show(title, message, INFO, OK, **options)
840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef showwarning(title=None, message=None, **options):
860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Show a warning message"
870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return _show(title, message, WARNING, OK, **options)
880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef showerror(title=None, message=None, **options):
900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Show an error message"
910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return _show(title, message, ERROR, OK, **options)
920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef askquestion(title=None, message=None, **options):
940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Ask a question"
950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return _show(title, message, QUESTION, YESNO, **options)
960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef askokcancel(title=None, message=None, **options):
980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Ask if operation should proceed; return true if the answer is ok"
990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    s = _show(title, message, QUESTION, OKCANCEL, **options)
1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return s == OK
1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef askyesno(title=None, message=None, **options):
1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Ask a question; return true if the answer is yes"
1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    s = _show(title, message, QUESTION, YESNO, **options)
1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return s == YES
1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef askyesnocancel(title=None, message=None, **options):
1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Ask a question; return true if the answer is yes, None if cancelled."
1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    s = _show(title, message, QUESTION, YESNOCANCEL, **options)
1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # s might be a Tcl index object, so convert it to a string
1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    s = str(s)
1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if s == CANCEL:
1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return None
1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return s == YES
1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef askretrycancel(title=None, message=None, **options):
1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    "Ask if operation should be retried; return true if the answer is yes"
1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    s = _show(title, message, WARNING, RETRYCANCEL, **options)
1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    return s == RETRY
1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# --------------------------------------------------------------------
1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# test stuff
1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__":
1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "info", showinfo("Spam", "Egg Information")
1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "warning", showwarning("Spam", "Egg Warning")
1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "error", showerror("Spam", "Egg Alert")
1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "question", askquestion("Spam", "Question?")
1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "proceed", askokcancel("Spam", "Proceed?")
1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "yes/no", askyesno("Spam", "Got it?")
1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "yes/no/cancel", askyesnocancel("Spam", "Want it?")
1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    print "try again", askretrycancel("Spam", "Try again?")
135