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. |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 6 | bar = Progress(label, maxvalue) -- Display a progress bar |
| 7 | bar.set(value) -- Set value |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 8 | |
| 9 | More documentation in each function. |
| 10 | This module uses DLOG resources 256, 257 and 258. |
| 11 | Based upon STDWIN dialogs with the same names and functions. |
| 12 | """ |
| 13 | |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 14 | from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 15 | import Qd |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 16 | import QuickDraw |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def Message(msg): |
| 20 | """Display a MESSAGE string. |
| 21 | |
| 22 | Return when the user clicks the OK button or presses Return. |
| 23 | |
| 24 | The MESSAGE string can be at most 255 characters long. |
| 25 | """ |
| 26 | |
| 27 | id = 256 |
| 28 | d = GetNewDialog(id, -1) |
| 29 | if not d: |
| 30 | print "Can't get DLOG resource with id =", id |
| 31 | return |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 32 | tp, h, rect = d.GetDialogItem(2) |
| 33 | SetDialogItemText(h, msg) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 34 | d.SetDialogDefaultItem(1) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 35 | while 1: |
| 36 | n = ModalDialog(None) |
| 37 | if n == 1: |
| 38 | return |
| 39 | |
| 40 | |
| 41 | def AskString(prompt, default = ""): |
| 42 | """Display a PROMPT string and a text entry field with a DEFAULT string. |
| 43 | |
| 44 | Return the contents of the text entry field when the user clicks the |
| 45 | OK button or presses Return. |
| 46 | Return None when the user clicks the Cancel button. |
| 47 | |
| 48 | If omitted, DEFAULT is empty. |
| 49 | |
| 50 | The PROMPT and DEFAULT strings, as well as the return value, |
| 51 | can be at most 255 characters long. |
| 52 | """ |
| 53 | |
| 54 | id = 257 |
| 55 | d = GetNewDialog(id, -1) |
| 56 | if not d: |
| 57 | print "Can't get DLOG resource with id =", id |
| 58 | return |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 59 | tp, h, rect = d.GetDialogItem(3) |
| 60 | SetDialogItemText(h, prompt) |
| 61 | tp, h, rect = d.GetDialogItem(4) |
| 62 | SetDialogItemText(h, default) |
| 63 | # d.SetDialogItem(4, 0, 255) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 64 | d.SetDialogDefaultItem(1) |
| 65 | d.SetDialogCancelItem(2) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 66 | while 1: |
| 67 | n = ModalDialog(None) |
| 68 | if n == 1: |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 69 | tp, h, rect = d.GetDialogItem(4) |
| 70 | return GetDialogItemText(h) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 71 | if n == 2: return None |
| 72 | |
| 73 | |
| 74 | def AskYesNoCancel(question, default = 0): |
| 75 | ## """Display a QUESTION string which can be answered with Yes or No. |
| 76 | ## |
| 77 | ## Return 1 when the user clicks the Yes button. |
| 78 | ## Return 0 when the user clicks the No button. |
| 79 | ## Return -1 when the user clicks the Cancel button. |
| 80 | ## |
| 81 | ## When the user presses Return, the DEFAULT value is returned. |
| 82 | ## If omitted, this is 0 (No). |
| 83 | ## |
| 84 | ## The QUESTION strign ca be at most 255 characters. |
| 85 | ## """ |
| 86 | |
| 87 | id = 258 |
| 88 | d = GetNewDialog(id, -1) |
| 89 | if not d: |
| 90 | print "Can't get DLOG resource with id =", id |
| 91 | return |
| 92 | # Button assignments: |
| 93 | # 1 = default (invisible) |
| 94 | # 2 = Yes |
| 95 | # 3 = No |
| 96 | # 4 = Cancel |
| 97 | # The question string is item 5 |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 98 | tp, h, rect = d.GetDialogItem(5) |
| 99 | SetDialogItemText(h, question) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 100 | d.SetDialogCancelItem(4) |
Jack Jansen | 0b690db | 1996-04-10 14:49:41 +0000 | [diff] [blame] | 101 | if default == 1: |
| 102 | d.SetDialogDefaultItem(2) |
| 103 | elif default == 0: |
| 104 | d.SetDialogDefaultItem(3) |
| 105 | elif default == -1: |
| 106 | d.SetDialogDefaultItem(4) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 107 | while 1: |
| 108 | n = ModalDialog(None) |
| 109 | if n == 1: return default |
| 110 | if n == 2: return 1 |
| 111 | if n == 3: return 0 |
| 112 | if n == 4: return -1 |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 113 | |
| 114 | class ProgressBar: |
| 115 | def __init__(self, label="Working...", maxval=100): |
| 116 | self.label = label |
| 117 | self.maxval = maxval |
| 118 | self.curval = -1 |
| 119 | self.d = GetNewDialog(259, -1) |
| 120 | tp, text_h, rect = self.d.GetDialogItem(2) |
| 121 | SetDialogItemText(text_h, "Progress...") |
| 122 | self._update(0) |
| 123 | |
| 124 | def _update(self, value): |
| 125 | tp, h, bar_rect = self.d.GetDialogItem(3) |
| 126 | Qd.SetPort(self.d) |
| 127 | |
| 128 | Qd.FrameRect(bar_rect) # Draw outline |
| 129 | |
| 130 | inner_rect = Qd.InsetRect(bar_rect, 1, 1) |
| 131 | Qd.ForeColor(QuickDraw.whiteColor) |
| 132 | Qd.BackColor(QuickDraw.whiteColor) |
| 133 | Qd.PaintRect(inner_rect) # Clear internal |
| 134 | |
| 135 | l, t, r, b = inner_rect |
| 136 | r = int(l + (r-l)*value/self.maxval) |
| 137 | inner_rect = l, t, r, b |
| 138 | Qd.ForeColor(QuickDraw.blackColor) |
| 139 | Qd.BackColor(QuickDraw.blackColor) |
| 140 | Qd.PaintRect(inner_rect) # Draw bar |
| 141 | |
| 142 | # Restore settings |
| 143 | Qd.ForeColor(QuickDraw.blackColor) |
| 144 | Qd.BackColor(QuickDraw.whiteColor) |
| 145 | |
| 146 | # Test for cancel button |
| 147 | if ModalDialog(self._filterfunc) == 1: |
| 148 | raise KeyboardInterrupt |
| 149 | |
| 150 | def _filterfunc(self, d, e, *more): |
| 151 | return 2 # XXXX For now, this disables the cancel button |
| 152 | |
| 153 | def set(self, value): |
| 154 | if value < 0: value = 0 |
| 155 | if value > self.maxval: value = self.maxval |
| 156 | self._update(value) |
| 157 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | def test(): |
| 161 | Message("Testing EasyDialogs.") |
| 162 | ok = AskYesNoCancel("Do you want to proceed?") |
| 163 | if ok > 0: |
| 164 | s = AskString("Enter your first name") |
| 165 | Message("Thank you,\015%s" % `s`) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 166 | bar = ProgressBar("Counting...", 100) |
| 167 | for i in range(100): |
| 168 | bar.set(i) |
| 169 | del bar |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | if __name__ == '__main__': |
| 173 | test() |