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