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