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. |
Just van Rossum | cdcc0f0 | 1999-02-15 00:04:05 +0000 | [diff] [blame] | 5 | AskPassword(prompt, default) -- like AskString(), but shows text as bullets. |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 6 | AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons. |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 7 | bar = Progress(label, maxvalue) -- Display a progress bar |
| 8 | bar.set(value) -- Set value |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 9 | bar.inc( *amount ) -- increment value by amount (default=1) |
| 10 | bar.label( *newlabel ) -- get or set text label. |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 11 | |
| 12 | More documentation in each function. |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 13 | This module uses DLOG resources 260 and on. |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 14 | Based upon STDWIN dialogs with the same names and functions. |
| 15 | """ |
| 16 | |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 17 | from Carbon.Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog |
| 18 | from Carbon import Qd |
| 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 |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 26 | import Nav |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 27 | import MacOS |
| 28 | import string |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 29 | from Carbon.ControlAccessor import * # Also import Controls constants |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 30 | import Carbon.File |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 31 | import macfs |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 32 | import macresource |
Jack Jansen | e58962a | 2003-01-17 23:13:03 +0000 | [diff] [blame^] | 33 | import os |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 34 | |
| 35 | _initialized = 0 |
| 36 | |
| 37 | def _initialize(): |
| 38 | global _initialized |
| 39 | if _initialized: return |
| 40 | macresource.need("DLOG", 260, "dialogs.rsrc", __name__) |
| 41 | |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 42 | |
| 43 | def cr2lf(text): |
| 44 | if '\r' in text: |
| 45 | text = string.join(string.split(text, '\r'), '\n') |
| 46 | return text |
| 47 | |
| 48 | def lf2cr(text): |
| 49 | if '\n' in text: |
| 50 | text = string.join(string.split(text, '\n'), '\r') |
Jack Jansen | d5af7bd | 1998-09-28 10:37:08 +0000 | [diff] [blame] | 51 | if len(text) > 253: |
| 52 | text = text[:253] + '\311' |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 53 | return text |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 54 | |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 55 | def Message(msg, id=260, ok=None): |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 56 | """Display a MESSAGE string. |
| 57 | |
| 58 | Return when the user clicks the OK button or presses Return. |
| 59 | |
| 60 | The MESSAGE string can be at most 255 characters long. |
| 61 | """ |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 62 | _initialize() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 63 | d = GetNewDialog(id, -1) |
| 64 | if not d: |
Jack Jansen | d05e181 | 2002-08-02 11:03:19 +0000 | [diff] [blame] | 65 | print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 66 | return |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 67 | h = d.GetDialogItemAsControl(2) |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 68 | SetDialogItemText(h, lf2cr(msg)) |
Jack Jansen | 208c15a | 1999-02-16 16:06:39 +0000 | [diff] [blame] | 69 | if ok != None: |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 70 | h = d.GetDialogItemAsControl(1) |
| 71 | h.SetControlTitle(ok) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 72 | d.SetDialogDefaultItem(1) |
Jack Jansen | fca049d | 2000-01-18 13:36:02 +0000 | [diff] [blame] | 73 | d.AutoSizeDialog() |
Jack Jansen | 0c1836f | 2000-08-25 22:06:19 +0000 | [diff] [blame] | 74 | d.GetDialogWindow().ShowWindow() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 75 | while 1: |
| 76 | n = ModalDialog(None) |
| 77 | if n == 1: |
| 78 | return |
| 79 | |
| 80 | |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 81 | def AskString(prompt, default = "", id=261, ok=None, cancel=None): |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 82 | """Display a PROMPT string and a text entry field with a DEFAULT string. |
| 83 | |
| 84 | Return the contents of the text entry field when the user clicks the |
| 85 | OK button or presses Return. |
| 86 | Return None when the user clicks the Cancel button. |
| 87 | |
| 88 | If omitted, DEFAULT is empty. |
| 89 | |
| 90 | The PROMPT and DEFAULT strings, as well as the return value, |
| 91 | can be at most 255 characters long. |
| 92 | """ |
| 93 | |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 94 | _initialize() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 95 | d = GetNewDialog(id, -1) |
| 96 | if not d: |
Jack Jansen | d05e181 | 2002-08-02 11:03:19 +0000 | [diff] [blame] | 97 | print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 98 | return |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 99 | h = d.GetDialogItemAsControl(3) |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 100 | SetDialogItemText(h, lf2cr(prompt)) |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 101 | h = d.GetDialogItemAsControl(4) |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 102 | SetDialogItemText(h, lf2cr(default)) |
Jack Jansen | d61f92b | 1999-01-22 13:14:06 +0000 | [diff] [blame] | 103 | d.SelectDialogItemText(4, 0, 999) |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 104 | # d.SetDialogItem(4, 0, 255) |
Jack Jansen | 208c15a | 1999-02-16 16:06:39 +0000 | [diff] [blame] | 105 | if ok != None: |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 106 | h = d.GetDialogItemAsControl(1) |
| 107 | h.SetControlTitle(ok) |
Jack Jansen | 208c15a | 1999-02-16 16:06:39 +0000 | [diff] [blame] | 108 | if cancel != None: |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 109 | h = d.GetDialogItemAsControl(2) |
| 110 | h.SetControlTitle(cancel) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 111 | d.SetDialogDefaultItem(1) |
| 112 | d.SetDialogCancelItem(2) |
Jack Jansen | fca049d | 2000-01-18 13:36:02 +0000 | [diff] [blame] | 113 | d.AutoSizeDialog() |
Jack Jansen | 0c1836f | 2000-08-25 22:06:19 +0000 | [diff] [blame] | 114 | d.GetDialogWindow().ShowWindow() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 115 | while 1: |
| 116 | n = ModalDialog(None) |
| 117 | if n == 1: |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 118 | h = d.GetDialogItemAsControl(4) |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 119 | return cr2lf(GetDialogItemText(h)) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 120 | if n == 2: return None |
| 121 | |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 122 | def AskPassword(prompt, default='', id=264, ok=None, cancel=None): |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 123 | """Display a PROMPT string and a text entry field with a DEFAULT string. |
| 124 | The string is displayed as bullets only. |
| 125 | |
| 126 | Return the contents of the text entry field when the user clicks the |
| 127 | OK button or presses Return. |
| 128 | Return None when the user clicks the Cancel button. |
| 129 | |
| 130 | If omitted, DEFAULT is empty. |
| 131 | |
| 132 | The PROMPT and DEFAULT strings, as well as the return value, |
| 133 | can be at most 255 characters long. |
| 134 | """ |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 135 | _initialize() |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 136 | d = GetNewDialog(id, -1) |
| 137 | if not d: |
Jack Jansen | d05e181 | 2002-08-02 11:03:19 +0000 | [diff] [blame] | 138 | print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 139 | return |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 140 | h = d.GetDialogItemAsControl(3) |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 141 | SetDialogItemText(h, lf2cr(prompt)) |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 142 | pwd = d.GetDialogItemAsControl(4) |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 143 | bullets = '\245'*len(default) |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 144 | ## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets) |
| 145 | SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default) |
| 146 | d.SelectDialogItemText(4, 0, 999) |
Jack Jansen | 0c1836f | 2000-08-25 22:06:19 +0000 | [diff] [blame] | 147 | Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart) |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 148 | if ok != None: |
| 149 | h = d.GetDialogItemAsControl(1) |
| 150 | h.SetControlTitle(ok) |
| 151 | if cancel != None: |
| 152 | h = d.GetDialogItemAsControl(2) |
| 153 | h.SetControlTitle(cancel) |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 154 | d.SetDialogDefaultItem(Dialogs.ok) |
| 155 | d.SetDialogCancelItem(Dialogs.cancel) |
Jack Jansen | fca049d | 2000-01-18 13:36:02 +0000 | [diff] [blame] | 156 | d.AutoSizeDialog() |
Jack Jansen | 0c1836f | 2000-08-25 22:06:19 +0000 | [diff] [blame] | 157 | d.GetDialogWindow().ShowWindow() |
Jack Jansen | b92268a | 1999-02-10 22:38:44 +0000 | [diff] [blame] | 158 | while 1: |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 159 | n = ModalDialog(None) |
| 160 | if n == 1: |
| 161 | h = d.GetDialogItemAsControl(4) |
| 162 | return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag)) |
| 163 | if n == 2: return None |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 164 | |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 165 | def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262): |
Jack Jansen | cf2efc6 | 1999-02-25 22:05:45 +0000 | [diff] [blame] | 166 | """Display a QUESTION string which can be answered with Yes or No. |
| 167 | |
| 168 | Return 1 when the user clicks the Yes button. |
| 169 | Return 0 when the user clicks the No button. |
| 170 | Return -1 when the user clicks the Cancel button. |
| 171 | |
| 172 | When the user presses Return, the DEFAULT value is returned. |
| 173 | If omitted, this is 0 (No). |
| 174 | |
Just van Rossum | 639a740 | 2001-06-26 06:57:12 +0000 | [diff] [blame] | 175 | The QUESTION string can be at most 255 characters. |
Jack Jansen | cf2efc6 | 1999-02-25 22:05:45 +0000 | [diff] [blame] | 176 | """ |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 177 | |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 178 | _initialize() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 179 | d = GetNewDialog(id, -1) |
| 180 | if not d: |
Jack Jansen | d05e181 | 2002-08-02 11:03:19 +0000 | [diff] [blame] | 181 | print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 182 | return |
| 183 | # Button assignments: |
| 184 | # 1 = default (invisible) |
| 185 | # 2 = Yes |
| 186 | # 3 = No |
| 187 | # 4 = Cancel |
| 188 | # The question string is item 5 |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 189 | h = d.GetDialogItemAsControl(5) |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 190 | SetDialogItemText(h, lf2cr(question)) |
Jack Jansen | 3f5aef7 | 1997-06-20 16:23:37 +0000 | [diff] [blame] | 191 | if yes != None: |
Jack Jansen | 8574378 | 2000-02-10 16:15:53 +0000 | [diff] [blame] | 192 | if yes == '': |
| 193 | d.HideDialogItem(2) |
| 194 | else: |
| 195 | h = d.GetDialogItemAsControl(2) |
| 196 | h.SetControlTitle(yes) |
Jack Jansen | 3f5aef7 | 1997-06-20 16:23:37 +0000 | [diff] [blame] | 197 | if no != None: |
Jack Jansen | 8574378 | 2000-02-10 16:15:53 +0000 | [diff] [blame] | 198 | if no == '': |
| 199 | d.HideDialogItem(3) |
| 200 | else: |
| 201 | h = d.GetDialogItemAsControl(3) |
| 202 | h.SetControlTitle(no) |
Jack Jansen | 3f5aef7 | 1997-06-20 16:23:37 +0000 | [diff] [blame] | 203 | if cancel != None: |
Jack Jansen | 208c15a | 1999-02-16 16:06:39 +0000 | [diff] [blame] | 204 | if cancel == '': |
| 205 | d.HideDialogItem(4) |
| 206 | else: |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 207 | h = d.GetDialogItemAsControl(4) |
| 208 | h.SetControlTitle(cancel) |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 209 | d.SetDialogCancelItem(4) |
Jack Jansen | 0b690db | 1996-04-10 14:49:41 +0000 | [diff] [blame] | 210 | if default == 1: |
| 211 | d.SetDialogDefaultItem(2) |
| 212 | elif default == 0: |
| 213 | d.SetDialogDefaultItem(3) |
| 214 | elif default == -1: |
| 215 | d.SetDialogDefaultItem(4) |
Jack Jansen | fca049d | 2000-01-18 13:36:02 +0000 | [diff] [blame] | 216 | d.AutoSizeDialog() |
Jack Jansen | 0c1836f | 2000-08-25 22:06:19 +0000 | [diff] [blame] | 217 | d.GetDialogWindow().ShowWindow() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 218 | while 1: |
| 219 | n = ModalDialog(None) |
| 220 | if n == 1: return default |
| 221 | if n == 2: return 1 |
| 222 | if n == 3: return 0 |
| 223 | if n == 4: return -1 |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 224 | |
| 225 | |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 226 | |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 227 | |
Jack Jansen | 362c7cd0 | 2002-11-30 00:01:29 +0000 | [diff] [blame] | 228 | screenbounds = Qd.GetQDGlobalsScreenBits().bounds |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 229 | screenbounds = screenbounds[0]+4, screenbounds[1]+4, \ |
| 230 | screenbounds[2]-4, screenbounds[3]-4 |
| 231 | |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 232 | kControlProgressBarIndeterminateTag = 'inde' # from Controls.py |
| 233 | |
| 234 | |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 235 | class ProgressBar: |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 236 | def __init__(self, title="Working...", maxval=0, label="", id=263): |
Jack Jansen | 5dd7362 | 2001-02-23 22:18:27 +0000 | [diff] [blame] | 237 | self.w = None |
| 238 | self.d = None |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 239 | _initialize() |
Jack Jansen | 3f5aef7 | 1997-06-20 16:23:37 +0000 | [diff] [blame] | 240 | self.d = GetNewDialog(id, -1) |
Jack Jansen | 784c611 | 2001-02-09 15:57:01 +0000 | [diff] [blame] | 241 | self.w = self.d.GetDialogWindow() |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 242 | self.label(label) |
Jack Jansen | ab48e90 | 2000-07-24 14:07:15 +0000 | [diff] [blame] | 243 | self.title(title) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 244 | self.set(0, maxval) |
| 245 | self.d.AutoSizeDialog() |
Jack Jansen | 784c611 | 2001-02-09 15:57:01 +0000 | [diff] [blame] | 246 | self.w.ShowWindow() |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 247 | self.d.DrawDialog() |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 248 | |
| 249 | def __del__( self ): |
Jack Jansen | 5dd7362 | 2001-02-23 22:18:27 +0000 | [diff] [blame] | 250 | if self.w: |
| 251 | self.w.BringToFront() |
| 252 | self.w.HideWindow() |
Jack Jansen | 784c611 | 2001-02-09 15:57:01 +0000 | [diff] [blame] | 253 | del self.w |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 254 | del self.d |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 255 | |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 256 | def title(self, newstr=""): |
| 257 | """title(text) - Set title of progress window""" |
Jack Jansen | 784c611 | 2001-02-09 15:57:01 +0000 | [diff] [blame] | 258 | self.w.BringToFront() |
| 259 | self.w.SetWTitle(newstr) |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 260 | |
| 261 | def label( self, *newstr ): |
| 262 | """label(text) - Set text in progress box""" |
Jack Jansen | 784c611 | 2001-02-09 15:57:01 +0000 | [diff] [blame] | 263 | self.w.BringToFront() |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 264 | if newstr: |
Jack Jansen | a5a4981 | 1998-07-01 15:47:44 +0000 | [diff] [blame] | 265 | self._label = lf2cr(newstr[0]) |
Jack Jansen | cc38688 | 1999-12-12 22:57:51 +0000 | [diff] [blame] | 266 | text_h = self.d.GetDialogItemAsControl(2) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 267 | SetDialogItemText(text_h, self._label) |
| 268 | |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 269 | def _update(self, value): |
Jack Jansen | 58fa818 | 1999-11-05 15:53:10 +0000 | [diff] [blame] | 270 | maxval = self.maxval |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 271 | if maxval == 0: # an indeterminate bar |
| 272 | Ctl.IdleControls(self.w) # spin the barber pole |
| 273 | else: # a determinate bar |
| 274 | if maxval > 32767: |
| 275 | value = int(value/(maxval/32767.0)) |
| 276 | maxval = 32767 |
| 277 | progbar = self.d.GetDialogItemAsControl(3) |
| 278 | progbar.SetControlMaximum(maxval) |
| 279 | progbar.SetControlValue(value) # set the bar length |
| 280 | |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 281 | # Test for cancel button |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 282 | ready, ev = Evt.WaitNextEvent( Events.mDownMask, 1 ) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 283 | if ready : |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 284 | what,msg,when,where,mod = ev |
| 285 | part = Win.FindWindow(where)[0] |
| 286 | if Dlg.IsDialogEvent(ev): |
| 287 | ds = Dlg.DialogSelect(ev) |
| 288 | if ds[0] and ds[1] == self.d and ds[-1] == 1: |
Jack Jansen | 5dd7362 | 2001-02-23 22:18:27 +0000 | [diff] [blame] | 289 | self.w.HideWindow() |
| 290 | self.w = None |
| 291 | self.d = None |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 292 | raise KeyboardInterrupt, ev |
| 293 | else: |
| 294 | if part == 4: # inDrag |
Jack Jansen | 8d2f3d6 | 2001-07-27 09:21:28 +0000 | [diff] [blame] | 295 | self.w.DragWindow(where, screenbounds) |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 296 | else: |
| 297 | MacOS.HandleEvent(ev) |
| 298 | |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 299 | |
Jack Jansen | 58fa818 | 1999-11-05 15:53:10 +0000 | [diff] [blame] | 300 | def set(self, value, max=None): |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 301 | """set(value) - Set progress bar position""" |
Jack Jansen | 58fa818 | 1999-11-05 15:53:10 +0000 | [diff] [blame] | 302 | if max != None: |
| 303 | self.maxval = max |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 304 | bar = self.d.GetDialogItemAsControl(3) |
| 305 | if max <= 0: # indeterminate bar |
| 306 | bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x01') |
| 307 | else: # determinate bar |
| 308 | bar.SetControlData(0,kControlProgressBarIndeterminateTag,'\x00') |
| 309 | if value < 0: |
| 310 | value = 0 |
| 311 | elif value > self.maxval: |
| 312 | value = self.maxval |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 313 | self.curval = value |
Jack Jansen | 3a87f5b | 1995-11-14 10:13:49 +0000 | [diff] [blame] | 314 | self._update(value) |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 315 | |
| 316 | def inc(self, n=1): |
| 317 | """inc(amt) - Increment progress bar position""" |
| 318 | self.set(self.curval + n) |
| 319 | |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 320 | ARGV_ID=265 |
| 321 | ARGV_ITEM_OK=1 |
| 322 | ARGV_ITEM_CANCEL=2 |
| 323 | ARGV_OPTION_GROUP=3 |
| 324 | ARGV_OPTION_EXPLAIN=4 |
| 325 | ARGV_OPTION_VALUE=5 |
| 326 | ARGV_OPTION_ADD=6 |
| 327 | ARGV_COMMAND_GROUP=7 |
| 328 | ARGV_COMMAND_EXPLAIN=8 |
| 329 | ARGV_COMMAND_ADD=9 |
| 330 | ARGV_ADD_OLDFILE=10 |
| 331 | ARGV_ADD_NEWFILE=11 |
| 332 | ARGV_ADD_FOLDER=12 |
| 333 | ARGV_CMDLINE_GROUP=13 |
| 334 | ARGV_CMDLINE_DATA=14 |
| 335 | |
| 336 | ##def _myModalDialog(d): |
| 337 | ## while 1: |
| 338 | ## ready, ev = Evt.WaitNextEvent(0xffff, -1) |
| 339 | ## print 'DBG: WNE', ready, ev |
| 340 | ## if ready : |
| 341 | ## what,msg,when,where,mod = ev |
| 342 | ## part, window = Win.FindWindow(where) |
| 343 | ## if Dlg.IsDialogEvent(ev): |
| 344 | ## didit, dlgdone, itemdone = Dlg.DialogSelect(ev) |
| 345 | ## print 'DBG: DialogSelect', didit, dlgdone, itemdone, d |
| 346 | ## if didit and dlgdone == d: |
| 347 | ## return itemdone |
| 348 | ## elif window == d.GetDialogWindow(): |
| 349 | ## d.GetDialogWindow().SelectWindow() |
| 350 | ## if part == 4: # inDrag |
| 351 | ## d.DragWindow(where, screenbounds) |
| 352 | ## else: |
| 353 | ## MacOS.HandleEvent(ev) |
| 354 | ## else: |
| 355 | ## MacOS.HandleEvent(ev) |
| 356 | ## |
| 357 | def _setmenu(control, items): |
Jack Jansen | e7bfc91 | 2001-01-09 22:22:58 +0000 | [diff] [blame] | 358 | mhandle = control.GetControlData_Handle(Controls.kControlMenuPart, |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 359 | Controls.kControlPopupButtonMenuHandleTag) |
| 360 | menu = Menu.as_Menu(mhandle) |
| 361 | for item in items: |
| 362 | if type(item) == type(()): |
| 363 | label = item[0] |
| 364 | else: |
| 365 | label = item |
| 366 | if label[-1] == '=' or label[-1] == ':': |
| 367 | label = label[:-1] |
| 368 | menu.AppendMenu(label) |
| 369 | ## mhandle, mid = menu.getpopupinfo() |
Jack Jansen | e7bfc91 | 2001-01-09 22:22:58 +0000 | [diff] [blame] | 370 | ## control.SetControlData_Handle(Controls.kControlMenuPart, |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 371 | ## Controls.kControlPopupButtonMenuHandleTag, mhandle) |
| 372 | control.SetControlMinimum(1) |
| 373 | control.SetControlMaximum(len(items)+1) |
| 374 | |
| 375 | def _selectoption(d, optionlist, idx): |
| 376 | if idx < 0 or idx >= len(optionlist): |
| 377 | MacOS.SysBeep() |
| 378 | return |
| 379 | option = optionlist[idx] |
Jack Jansen | 09c7343 | 2002-06-26 15:14:48 +0000 | [diff] [blame] | 380 | if type(option) == type(()): |
| 381 | if len(option) == 4: |
| 382 | help = option[2] |
| 383 | elif len(option) > 1: |
| 384 | help = option[-1] |
| 385 | else: |
| 386 | help = '' |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 387 | else: |
| 388 | help = '' |
| 389 | h = d.GetDialogItemAsControl(ARGV_OPTION_EXPLAIN) |
Jack Jansen | 09c7343 | 2002-06-26 15:14:48 +0000 | [diff] [blame] | 390 | if help and len(help) > 250: |
| 391 | help = help[:250] + '...' |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 392 | Dlg.SetDialogItemText(h, help) |
| 393 | hasvalue = 0 |
| 394 | if type(option) == type(()): |
| 395 | label = option[0] |
| 396 | else: |
| 397 | label = option |
| 398 | if label[-1] == '=' or label[-1] == ':': |
| 399 | hasvalue = 1 |
| 400 | h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE) |
| 401 | Dlg.SetDialogItemText(h, '') |
| 402 | if hasvalue: |
| 403 | d.ShowDialogItem(ARGV_OPTION_VALUE) |
| 404 | d.SelectDialogItemText(ARGV_OPTION_VALUE, 0, 0) |
| 405 | else: |
| 406 | d.HideDialogItem(ARGV_OPTION_VALUE) |
| 407 | |
| 408 | |
| 409 | def GetArgv(optionlist=None, commandlist=None, addoldfile=1, addnewfile=1, addfolder=1, id=ARGV_ID): |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 410 | _initialize() |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 411 | d = GetNewDialog(id, -1) |
| 412 | if not d: |
Jack Jansen | d05e181 | 2002-08-02 11:03:19 +0000 | [diff] [blame] | 413 | print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 414 | return |
| 415 | # h = d.GetDialogItemAsControl(3) |
| 416 | # SetDialogItemText(h, lf2cr(prompt)) |
| 417 | # h = d.GetDialogItemAsControl(4) |
| 418 | # SetDialogItemText(h, lf2cr(default)) |
| 419 | # d.SelectDialogItemText(4, 0, 999) |
| 420 | # d.SetDialogItem(4, 0, 255) |
| 421 | if optionlist: |
| 422 | _setmenu(d.GetDialogItemAsControl(ARGV_OPTION_GROUP), optionlist) |
| 423 | _selectoption(d, optionlist, 0) |
| 424 | else: |
| 425 | d.GetDialogItemAsControl(ARGV_OPTION_GROUP).DeactivateControl() |
| 426 | if commandlist: |
| 427 | _setmenu(d.GetDialogItemAsControl(ARGV_COMMAND_GROUP), commandlist) |
Jack Jansen | 0bb0a90 | 2000-09-21 22:01:08 +0000 | [diff] [blame] | 428 | if type(commandlist[0]) == type(()) and len(commandlist[0]) > 1: |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 429 | help = commandlist[0][-1] |
| 430 | h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN) |
| 431 | Dlg.SetDialogItemText(h, help) |
| 432 | else: |
| 433 | d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).DeactivateControl() |
| 434 | if not addoldfile: |
| 435 | d.GetDialogItemAsControl(ARGV_ADD_OLDFILE).DeactivateControl() |
| 436 | if not addnewfile: |
| 437 | d.GetDialogItemAsControl(ARGV_ADD_NEWFILE).DeactivateControl() |
| 438 | if not addfolder: |
| 439 | d.GetDialogItemAsControl(ARGV_ADD_FOLDER).DeactivateControl() |
| 440 | d.SetDialogDefaultItem(ARGV_ITEM_OK) |
| 441 | d.SetDialogCancelItem(ARGV_ITEM_CANCEL) |
| 442 | d.GetDialogWindow().ShowWindow() |
| 443 | d.DrawDialog() |
Jack Jansen | eb30843 | 2001-09-09 00:36:01 +0000 | [diff] [blame] | 444 | if hasattr(MacOS, 'SchedParams'): |
| 445 | appsw = MacOS.SchedParams(1, 0) |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 446 | try: |
| 447 | while 1: |
| 448 | stringstoadd = [] |
| 449 | n = ModalDialog(None) |
| 450 | if n == ARGV_ITEM_OK: |
| 451 | break |
| 452 | elif n == ARGV_ITEM_CANCEL: |
| 453 | raise SystemExit |
| 454 | elif n == ARGV_OPTION_GROUP: |
| 455 | idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1 |
| 456 | _selectoption(d, optionlist, idx) |
| 457 | elif n == ARGV_OPTION_VALUE: |
| 458 | pass |
| 459 | elif n == ARGV_OPTION_ADD: |
| 460 | idx = d.GetDialogItemAsControl(ARGV_OPTION_GROUP).GetControlValue()-1 |
| 461 | if 0 <= idx < len(optionlist): |
Jack Jansen | 0bb0a90 | 2000-09-21 22:01:08 +0000 | [diff] [blame] | 462 | option = optionlist[idx] |
| 463 | if type(option) == type(()): |
| 464 | option = option[0] |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 465 | if option[-1] == '=' or option[-1] == ':': |
| 466 | option = option[:-1] |
| 467 | h = d.GetDialogItemAsControl(ARGV_OPTION_VALUE) |
| 468 | value = Dlg.GetDialogItemText(h) |
| 469 | else: |
| 470 | value = '' |
| 471 | if len(option) == 1: |
| 472 | stringtoadd = '-' + option |
| 473 | else: |
| 474 | stringtoadd = '--' + option |
| 475 | stringstoadd = [stringtoadd] |
| 476 | if value: |
| 477 | stringstoadd.append(value) |
| 478 | else: |
| 479 | MacOS.SysBeep() |
| 480 | elif n == ARGV_COMMAND_GROUP: |
| 481 | idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1 |
Jack Jansen | 0bb0a90 | 2000-09-21 22:01:08 +0000 | [diff] [blame] | 482 | if 0 <= idx < len(commandlist) and type(commandlist[idx]) == type(()) and \ |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 483 | len(commandlist[idx]) > 1: |
| 484 | help = commandlist[idx][-1] |
| 485 | h = d.GetDialogItemAsControl(ARGV_COMMAND_EXPLAIN) |
| 486 | Dlg.SetDialogItemText(h, help) |
| 487 | elif n == ARGV_COMMAND_ADD: |
| 488 | idx = d.GetDialogItemAsControl(ARGV_COMMAND_GROUP).GetControlValue()-1 |
| 489 | if 0 <= idx < len(commandlist): |
Jack Jansen | 0bb0a90 | 2000-09-21 22:01:08 +0000 | [diff] [blame] | 490 | command = commandlist[idx] |
| 491 | if type(command) == type(()): |
| 492 | command = command[0] |
| 493 | stringstoadd = [command] |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 494 | else: |
| 495 | MacOS.SysBeep() |
| 496 | elif n == ARGV_ADD_OLDFILE: |
| 497 | fss, ok = macfs.StandardGetFile() |
| 498 | if ok: |
| 499 | stringstoadd = [fss.as_pathname()] |
| 500 | elif n == ARGV_ADD_NEWFILE: |
| 501 | fss, ok = macfs.StandardPutFile('') |
| 502 | if ok: |
| 503 | stringstoadd = [fss.as_pathname()] |
| 504 | elif n == ARGV_ADD_FOLDER: |
| 505 | fss, ok = macfs.GetDirectory() |
| 506 | if ok: |
| 507 | stringstoadd = [fss.as_pathname()] |
| 508 | elif n == ARGV_CMDLINE_DATA: |
| 509 | pass # Nothing to do |
| 510 | else: |
| 511 | raise RuntimeError, "Unknown dialog item %d"%n |
| 512 | |
| 513 | for stringtoadd in stringstoadd: |
| 514 | if '"' in stringtoadd or "'" in stringtoadd or " " in stringtoadd: |
| 515 | stringtoadd = `stringtoadd` |
| 516 | h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA) |
| 517 | oldstr = GetDialogItemText(h) |
| 518 | if oldstr and oldstr[-1] != ' ': |
| 519 | oldstr = oldstr + ' ' |
| 520 | oldstr = oldstr + stringtoadd |
| 521 | if oldstr[-1] != ' ': |
| 522 | oldstr = oldstr + ' ' |
| 523 | SetDialogItemText(h, oldstr) |
| 524 | d.SelectDialogItemText(ARGV_CMDLINE_DATA, 0x7fff, 0x7fff) |
| 525 | h = d.GetDialogItemAsControl(ARGV_CMDLINE_DATA) |
| 526 | oldstr = GetDialogItemText(h) |
| 527 | tmplist = string.split(oldstr) |
| 528 | newlist = [] |
| 529 | while tmplist: |
| 530 | item = tmplist[0] |
| 531 | del tmplist[0] |
| 532 | if item[0] == '"': |
| 533 | while item[-1] != '"': |
| 534 | if not tmplist: |
| 535 | raise RuntimeError, "Unterminated quoted argument" |
| 536 | item = item + ' ' + tmplist[0] |
| 537 | del tmplist[0] |
| 538 | item = item[1:-1] |
| 539 | if item[0] == "'": |
| 540 | while item[-1] != "'": |
| 541 | if not tmplist: |
| 542 | raise RuntimeError, "Unterminated quoted argument" |
| 543 | item = item + ' ' + tmplist[0] |
| 544 | del tmplist[0] |
| 545 | item = item[1:-1] |
| 546 | newlist.append(item) |
| 547 | return newlist |
| 548 | finally: |
Jack Jansen | eb30843 | 2001-09-09 00:36:01 +0000 | [diff] [blame] | 549 | if hasattr(MacOS, 'SchedParams'): |
| 550 | apply(MacOS.SchedParams, appsw) |
Jack Jansen | 0bb0a90 | 2000-09-21 22:01:08 +0000 | [diff] [blame] | 551 | del d |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 552 | |
| 553 | def _mktypelist(typelist): |
| 554 | # Workaround for OSX typeless files: |
| 555 | if 'TEXT' in typelist and not '\0\0\0\0' in typelist: |
| 556 | typelist = typelist + ('\0\0\0\0',) |
| 557 | if not typelist: |
| 558 | return None |
| 559 | data = 'Pyth' + struct.pack("hh", 0, len(typelist)) |
| 560 | for type in typelist: |
| 561 | data = data+type |
| 562 | return Carbon.Res.Handle(data) |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 563 | |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 564 | _ALLOWED_KEYS = { |
| 565 | 'version':1, |
| 566 | 'defaultLocation':1, |
| 567 | 'dialogOptionFlags':1, |
| 568 | 'location':1, |
| 569 | 'clientName':1, |
| 570 | 'windowTitle':1, |
| 571 | 'actionButtonLabel':1, |
| 572 | 'cancelButtonLabel':1, |
| 573 | 'savedFileName':1, |
| 574 | 'message':1, |
| 575 | 'preferenceKey':1, |
| 576 | 'popupExtension':1, |
| 577 | 'eventProc':1, |
| 578 | 'previewProc':1, |
| 579 | 'filterProc':1, |
| 580 | 'typeList':1, |
| 581 | 'fileType':1, |
| 582 | 'fileCreator':1, |
| 583 | # Our extension: |
| 584 | 'wanted':1, |
| 585 | 'multiple':1, |
| 586 | } |
| 587 | |
| 588 | def _process_Nav_args(argsargs, allowed, dftflags): |
| 589 | import aepack |
| 590 | import Carbon.AE |
| 591 | import Carbon.File |
| 592 | args = argsargs.copy() |
| 593 | for k in args.keys(): |
| 594 | if not allowed.has_key(k): |
| 595 | raise TypeError, "Unknown keyword argument: %s" % repr(k) |
| 596 | # Set some defaults, and modify some arguments |
| 597 | if not args.has_key('dialogOptionFlags'): |
| 598 | args['dialogOptionFlags'] = dftflags |
| 599 | if args.has_key('defaultLocation') and \ |
| 600 | not isinstance(args['defaultLocation'], Carbon.AE.AEDesc): |
| 601 | defaultLocation = args['defaultLocation'] |
| 602 | if isinstance(defaultLocation, (Carbon.File.FSSpec, Carbon.File.FSRef)): |
| 603 | args['defaultLocation'] = aepack.pack(defaultLocation) |
| 604 | else: |
| 605 | defaultLocation = Carbon.File.FSRef(defaultLocation) |
| 606 | args['defaultLocation'] = aepack.pack(defaultLocation) |
| 607 | if args.has_key('typeList') and not isinstance(args['typeList'], Carbon.Res.ResourceType): |
| 608 | typeList = args['typeList'].copy() |
| 609 | # Workaround for OSX typeless files: |
| 610 | if 'TEXT' in typeList and not '\0\0\0\0' in typeList: |
| 611 | typeList = typeList + ('\0\0\0\0',) |
| 612 | data = 'Pyth' + struct.pack("hh", 0, len(typeList)) |
| 613 | for type in typeList: |
| 614 | data = data+type |
| 615 | args['typeList'] = Carbon.Res.Handle(data) |
| 616 | tpwanted = str |
| 617 | if args.has_key('wanted'): |
| 618 | tpwanted = args['wanted'] |
| 619 | del args['wanted'] |
| 620 | return args, tpwanted |
| 621 | |
| 622 | def AskFileForOpen(**args): |
| 623 | default_flags = 0x56 # Or 0xe4? |
| 624 | args, tpwanted = _process_Nav_args(args, _ALLOWED_KEYS, default_flags) |
| 625 | try: |
| 626 | rr = Nav.NavChooseFile(args) |
| 627 | good = 1 |
| 628 | except Nav.error, arg: |
| 629 | if arg[0] != -128: # userCancelledErr |
| 630 | raise Nav.error, arg |
| 631 | return None |
| 632 | if not rr.validRecord or not rr.selection: |
| 633 | return None |
| 634 | if issubclass(tpwanted, Carbon.File.FSRef): |
| 635 | return tpwanted(rr.selection_fsr[0]) |
| 636 | if issubclass(tpwanted, Carbon.File.FSSpec): |
| 637 | return tpwanted(rr.selection[0]) |
| 638 | if issubclass(tpwanted, str): |
| 639 | return tpwanted(rr.selection_fsr[0].FSRefMakePath()) |
| 640 | if issubclass(tpwanted, unicode): |
| 641 | return tpwanted(rr.selection_fsr[0].FSRefMakePath(), 'utf8') |
| 642 | raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted) |
| 643 | |
| 644 | def AskFileForSave(**args): |
| 645 | default_flags = 0x07 |
| 646 | args, tpwanted = _process_Nav_args(args, _ALLOWED_KEYS, default_flags) |
| 647 | try: |
| 648 | rr = Nav.NavPutFile(args) |
| 649 | good = 1 |
| 650 | except Nav.error, arg: |
| 651 | if arg[0] != -128: # userCancelledErr |
| 652 | raise Nav.error, arg |
| 653 | return None |
| 654 | if not rr.validRecord or not rr.selection: |
| 655 | return None |
| 656 | if issubclass(tpwanted, Carbon.File.FSRef): |
| 657 | raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave" |
| 658 | if issubclass(tpwanted, Carbon.File.FSSpec): |
| 659 | return tpwanted(rr.selection[0]) |
| 660 | if issubclass(tpwanted, (str, unicode)): |
| 661 | # This is gross, and probably incorrect too |
| 662 | vrefnum, dirid, name = rr.selection[0].as_tuple() |
| 663 | pardir_fss = Carbon.File.FSSpec((vrefnum, dirid, '')) |
Jack Jansen | e58962a | 2003-01-17 23:13:03 +0000 | [diff] [blame^] | 664 | pardir_fsr = Carbon.File.FSRef(pardir_fss) |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 665 | pardir_path = pardir_fsr.FSRefMakePath() # This is utf-8 |
| 666 | name_utf8 = unicode(name, 'macroman').encode('utf8') |
| 667 | fullpath = os.path.join(pardir_path, name_utf8) |
| 668 | if issubclass(tpwanted, unicode): |
| 669 | return unicode(fullpath, 'utf8') |
| 670 | return tpwanted(fullpath) |
| 671 | raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted) |
| 672 | |
| 673 | def AskFolder(**args): |
| 674 | default_flags = 0x17 |
| 675 | args, tpwanted = _process_Nav_args(args, _ALLOWED_KEYS, default_flags) |
| 676 | try: |
| 677 | rr = Nav.NavChooseFolder(args) |
| 678 | good = 1 |
| 679 | except Nav.error, arg: |
| 680 | if arg[0] != -128: # userCancelledErr |
| 681 | raise Nav.error, arg |
| 682 | return None |
| 683 | if not rr.validRecord or not rr.selection: |
| 684 | return None |
| 685 | if issubclass(tpwanted, Carbon.File.FSRef): |
| 686 | return tpwanted(rr.selection_fsr[0]) |
| 687 | if issubclass(tpwanted, Carbon.File.FSSpec): |
| 688 | return tpwanted(rr.selection[0]) |
| 689 | if issubclass(tpwanted, str): |
| 690 | return tpwanted(rr.selection_fsr[0].FSRefMakePath()) |
| 691 | if issubclass(tpwanted, unicode): |
| 692 | return tpwanted(rr.selection_fsr[0].FSRefMakePath(), 'utf8') |
| 693 | raise TypeError, "Unknown value for argument 'wanted': %s" % repr(tpwanted) |
| 694 | |
| 695 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 696 | def test(): |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 697 | import time, sys |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 699 | Message("Testing EasyDialogs.") |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 700 | optionlist = (('v', 'Verbose'), ('verbose', 'Verbose as long option'), |
| 701 | ('flags=', 'Valued option'), ('f:', 'Short valued option')) |
| 702 | commandlist = (('start', 'Start something'), ('stop', 'Stop something')) |
| 703 | argv = GetArgv(optionlist=optionlist, commandlist=commandlist, addoldfile=0) |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 704 | Message("Command line: %s"%' '.join(argv)) |
Jack Jansen | f86eda5 | 2000-09-19 22:42:38 +0000 | [diff] [blame] | 705 | for i in range(len(argv)): |
| 706 | print 'arg[%d] = %s'%(i, `argv[i]`) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 707 | ok = AskYesNoCancel("Do you want to proceed?") |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 708 | ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No") |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 709 | if ok > 0: |
Jack Jansen | d61f92b | 1999-01-22 13:14:06 +0000 | [diff] [blame] | 710 | s = AskString("Enter your first name", "Joe") |
Jack Jansen | 60429e0 | 1999-12-13 16:07:01 +0000 | [diff] [blame] | 711 | s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None") |
| 712 | if not s2: |
| 713 | Message("%s has no secret nickname"%s) |
| 714 | else: |
| 715 | Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2)) |
Jack Jansen | f0d12da | 2003-01-17 16:04:39 +0000 | [diff] [blame] | 716 | else: |
| 717 | s = 'Anonymous' |
| 718 | rv = AskFileForOpen(message="Gimme a file, %s"%s, wanted=macfs.FSSpec) |
| 719 | Message("rv: %s"%rv) |
| 720 | rv = AskFileForSave(wanted=macfs.FSSpec, savedFileName="%s.txt"%s) |
| 721 | Message("rv.as_pathname: %s"%rv.as_pathname()) |
| 722 | rv = AskFolder() |
| 723 | Message("Folder name: %s"%rv) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 724 | text = ( "Working Hard...", "Hardly Working..." , |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 725 | "So far, so good!", "Keep on truckin'" ) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 726 | bar = ProgressBar("Progress, progress...", 0, label="Ramping up...") |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 727 | try: |
Jack Jansen | eb30843 | 2001-09-09 00:36:01 +0000 | [diff] [blame] | 728 | if hasattr(MacOS, 'SchedParams'): |
| 729 | appsw = MacOS.SchedParams(1, 0) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 730 | for i in xrange(20): |
| 731 | bar.inc() |
| 732 | time.sleep(0.05) |
| 733 | bar.set(0,100) |
| 734 | for i in xrange(100): |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 735 | bar.set(i) |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 736 | time.sleep(0.05) |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 737 | if i % 10 == 0: |
| 738 | bar.label(text[(i/10) % 4]) |
| 739 | bar.label("Done.") |
Jack Jansen | 911e87d | 2001-08-27 15:24:07 +0000 | [diff] [blame] | 740 | time.sleep(1.0) # give'em a chance to see "Done." |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 741 | finally: |
| 742 | del bar |
Jack Jansen | eb30843 | 2001-09-09 00:36:01 +0000 | [diff] [blame] | 743 | if hasattr(MacOS, 'SchedParams'): |
| 744 | apply(MacOS.SchedParams, appsw) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 746 | if __name__ == '__main__': |
Jack Jansen | 1d63d8c | 1997-05-12 15:44:14 +0000 | [diff] [blame] | 747 | try: |
| 748 | test() |
| 749 | except KeyboardInterrupt: |
| 750 | Message("Operation Canceled.") |
| 751 | |