Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 1 | """Easy to use dialogs. |
| 2 | |
| 3 | Message(msg) -- display a message and an OK button. |
| 4 | AskString(prompt, default) -- ask for a string, display OK and Cancel buttons. |
| 5 | AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons. |
| 6 | |
| 7 | More documentation in each function. |
| 8 | This module uses DLOG resources 256, 257 and 258. |
| 9 | Based upon STDWIN dialogs with the same names and functions. |
| 10 | """ |
| 11 | |
| 12 | from Dlg import GetNewDialog, SetIText, GetIText, ModalDialog |
| 13 | |
| 14 | |
| 15 | def Message(msg): |
| 16 | """Display a MESSAGE string. |
| 17 | |
| 18 | Return when the user clicks the OK button or presses Return. |
| 19 | |
| 20 | The MESSAGE string can be at most 255 characters long. |
| 21 | """ |
| 22 | |
| 23 | id = 256 |
| 24 | d = GetNewDialog(id, -1) |
| 25 | if not d: |
| 26 | print "Can't get DLOG resource with id =", id |
| 27 | return |
| 28 | tp, h, rect = d.GetDItem(2) |
| 29 | SetIText(h, msg) |
| 30 | while 1: |
| 31 | n = ModalDialog(None) |
| 32 | if n == 1: |
| 33 | return |
| 34 | |
| 35 | |
| 36 | def AskString(prompt, default = ""): |
| 37 | """Display a PROMPT string and a text entry field with a DEFAULT string. |
| 38 | |
| 39 | Return the contents of the text entry field when the user clicks the |
| 40 | OK button or presses Return. |
| 41 | Return None when the user clicks the Cancel button. |
| 42 | |
| 43 | If omitted, DEFAULT is empty. |
| 44 | |
| 45 | The PROMPT and DEFAULT strings, as well as the return value, |
| 46 | can be at most 255 characters long. |
| 47 | """ |
| 48 | |
| 49 | id = 257 |
| 50 | d = GetNewDialog(id, -1) |
| 51 | if not d: |
| 52 | print "Can't get DLOG resource with id =", id |
| 53 | return |
| 54 | tp, h, rect = d.GetDItem(3) |
| 55 | SetIText(h, prompt) |
| 56 | tp, h, rect = d.GetDItem(4) |
| 57 | SetIText(h, default) |
| 58 | d.SelIText(4, 0, 255) |
| 59 | while 1: |
| 60 | n = ModalDialog(None) |
| 61 | if n == 1: |
| 62 | tp, h, rect = d.GetDItem(4) |
| 63 | return GetIText(h) |
| 64 | if n == 2: return None |
| 65 | |
| 66 | |
| 67 | def AskYesNoCancel(question, default = 0): |
| 68 | ## """Display a QUESTION string which can be answered with Yes or No. |
| 69 | ## |
| 70 | ## Return 1 when the user clicks the Yes button. |
| 71 | ## Return 0 when the user clicks the No button. |
| 72 | ## Return -1 when the user clicks the Cancel button. |
| 73 | ## |
| 74 | ## When the user presses Return, the DEFAULT value is returned. |
| 75 | ## If omitted, this is 0 (No). |
| 76 | ## |
| 77 | ## The QUESTION strign ca be at most 255 characters. |
| 78 | ## """ |
| 79 | |
| 80 | id = 258 |
| 81 | d = GetNewDialog(id, -1) |
| 82 | if not d: |
| 83 | print "Can't get DLOG resource with id =", id |
| 84 | return |
| 85 | # Button assignments: |
| 86 | # 1 = default (invisible) |
| 87 | # 2 = Yes |
| 88 | # 3 = No |
| 89 | # 4 = Cancel |
| 90 | # The question string is item 5 |
| 91 | tp, h, rect = d.GetDItem(5) |
| 92 | SetIText(h, question) |
| 93 | while 1: |
| 94 | n = ModalDialog(None) |
| 95 | if n == 1: return default |
| 96 | if n == 2: return 1 |
| 97 | if n == 3: return 0 |
| 98 | if n == 4: return -1 |
| 99 | |
| 100 | |
| 101 | def test(): |
| 102 | Message("Testing EasyDialogs.") |
| 103 | ok = AskYesNoCancel("Do you want to proceed?") |
| 104 | if ok > 0: |
| 105 | s = AskString("Enter your first name") |
| 106 | Message("Thank you,\015%s" % `s`) |
| 107 | |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | test() |