def Message(msg, id=260, ok=None): """Display a MESSAGE string. Return when the user clicks the OK button or presses Return. The MESSAGE string can be at most 255 characters long. """ _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(2) SetDialogItemText(h, lf2cr(msg)) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) d.SetDialogDefaultItem(1) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: return return
def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262): """Display a QUESTION string which can be answered with Yes or No. Return 1 when the user clicks the Yes button. Return 0 when the user clicks the No button. Return -1 when the user clicks the Cancel button. When the user presses Return, the DEFAULT value is returned. If omitted, this is 0 (No). The QUESTION string can be at most 255 characters. """ _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, " (missing resource file?)" return # Button assignments: # 1 = default (invisible) # 2 = Yes # 3 = No # 4 = Cancel # The question string is item 5 h = d.GetDialogItemAsControl(5) SetDialogItemText(h, lf2cr(question)) if yes is not None: if yes == '': d.HideDialogItem(2) else: h = d.GetDialogItemAsControl(2) h.SetControlTitle(yes) if no is not None: if no == '': d.HideDialogItem(3) else: h = d.GetDialogItemAsControl(3) h.SetControlTitle(no) if cancel is not None: if cancel == '': d.HideDialogItem(4) else: h = d.GetDialogItemAsControl(4) h.SetControlTitle(cancel) d.SetDialogCancelItem(4) if default == 1: d.SetDialogDefaultItem(2) elif default == 0: d.SetDialogDefaultItem(3) elif default == -1: d.SetDialogDefaultItem(4) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: return default if n == 2: return 1 if n == 3: return 0 if n == 4: return -1
def AskString(prompt, default='', id=261, ok=None, cancel=None): _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(3) SetDialogItemText(h, lf2cr(prompt)) h = d.GetDialogItemAsControl(4) SetDialogItemText(h, lf2cr(default)) d.SelectDialogItemText(4, 0, 999) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) if cancel is not None: h = d.GetDialogItemAsControl(2) h.SetControlTitle(cancel) d.SetDialogDefaultItem(1) d.SetDialogCancelItem(2) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: h = d.GetDialogItemAsControl(4) return cr2lf(GetDialogItemText(h)) if n == 2: return return
def AskYesNoCancel(question, default=0, yes=None, no=None, cancel=None, id=262): _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(5) SetDialogItemText(h, lf2cr(question)) if yes is not None: if yes == '': d.HideDialogItem(2) else: h = d.GetDialogItemAsControl(2) h.SetControlTitle(yes) if no is not None: if no == '': d.HideDialogItem(3) else: h = d.GetDialogItemAsControl(3) h.SetControlTitle(no) if cancel is not None: if cancel == '': d.HideDialogItem(4) else: h = d.GetDialogItemAsControl(4) h.SetControlTitle(cancel) d.SetDialogCancelItem(4) if default == 1: d.SetDialogDefaultItem(2) elif default == 0: d.SetDialogDefaultItem(3) elif default == -1: d.SetDialogDefaultItem(4) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: return default if n == 2: return 1 if n == 3: return 0 if n == 4: return -1 return
def AskPassword(prompt, default='', id=264, ok=None, cancel=None): """Display a PROMPT string and a text entry field with a DEFAULT string. The string is displayed as bullets only. Return the contents of the text entry field when the user clicks the OK button or presses Return. Return None when the user clicks the Cancel button. If omitted, DEFAULT is empty. The PROMPT and DEFAULT strings, as well as the return value, can be at most 255 characters long. """ _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(3) SetDialogItemText(h, lf2cr(prompt)) pwd = d.GetDialogItemAsControl(4) bullets = '\xa5' * len(default) SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default) d.SelectDialogItemText(4, 0, 999) Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) if cancel is not None: h = d.GetDialogItemAsControl(2) h.SetControlTitle(cancel) d.SetDialogDefaultItem(Dialogs.ok) d.SetDialogCancelItem(Dialogs.cancel) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: h = d.GetDialogItemAsControl(4) return cr2lf( GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag)) if n == 2: return return
def AskString(prompt, default='', id=261, ok=None, cancel=None): """Display a PROMPT string and a text entry field with a DEFAULT string. Return the contents of the text entry field when the user clicks the OK button or presses Return. Return None when the user clicks the Cancel button. If omitted, DEFAULT is empty. The PROMPT and DEFAULT strings, as well as the return value, can be at most 255 characters long. """ _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(3) SetDialogItemText(h, lf2cr(prompt)) h = d.GetDialogItemAsControl(4) SetDialogItemText(h, lf2cr(default)) d.SelectDialogItemText(4, 0, 999) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) if cancel is not None: h = d.GetDialogItemAsControl(2) h.SetControlTitle(cancel) d.SetDialogDefaultItem(1) d.SetDialogCancelItem(2) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: h = d.GetDialogItemAsControl(4) return cr2lf(GetDialogItemText(h)) if n == 2: return return
def Message(msg, id=260, ok=None): _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(2) SetDialogItemText(h, lf2cr(msg)) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) d.SetDialogDefaultItem(1) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: return return
def AskPassword(prompt, default='', id=264, ok=None, cancel=None): _initialize() _interact() d = GetNewDialog(id, -1) if not d: print "EasyDialogs: Can't get DLOG resource with id =", id, ' (missing resource file?)' return else: h = d.GetDialogItemAsControl(3) SetDialogItemText(h, lf2cr(prompt)) pwd = d.GetDialogItemAsControl(4) bullets = '\xa5' * len(default) SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default) d.SelectDialogItemText(4, 0, 999) Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart) if ok is not None: h = d.GetDialogItemAsControl(1) h.SetControlTitle(ok) if cancel is not None: h = d.GetDialogItemAsControl(2) h.SetControlTitle(cancel) d.SetDialogDefaultItem(Dialogs.ok) d.SetDialogCancelItem(Dialogs.cancel) d.AutoSizeDialog() d.GetDialogWindow().ShowWindow() while 1: n = ModalDialog(None) if n == 1: h = d.GetDialogItemAsControl(4) return cr2lf( GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag)) if n == 2: return return
class ProgressBar: def __init__(self, title='Working...', maxval=0, label='', id=263): self.w = None self.d = None _initialize() self.d = GetNewDialog(id, -1) self.w = self.d.GetDialogWindow() self.label(label) self.title(title) self.set(0, maxval) self.d.AutoSizeDialog() self.w.ShowWindow() self.d.DrawDialog() return def __del__(self): if self.w: self.w.BringToFront() self.w.HideWindow() del self.w del self.d def title(self, newstr=''): """title(text) - Set title of progress window""" self.w.BringToFront() self.w.SetWTitle(newstr) def label(self, *newstr): """label(text) - Set text in progress box""" self.w.BringToFront() if newstr: self._label = lf2cr(newstr[0]) text_h = self.d.GetDialogItemAsControl(2) SetDialogItemText(text_h, self._label) def _update(self, value): maxval = self.maxval if maxval == 0: Ctl.IdleControls(self.w) else: if maxval > 32767: value = int(value / (maxval / 32767.0)) maxval = 32767 maxval = int(maxval) value = int(value) progbar = self.d.GetDialogItemAsControl(3) progbar.SetControlMaximum(maxval) progbar.SetControlValue(value) ready, ev = Evt.WaitNextEvent(Events.mDownMask, 1) if ready: what, msg, when, where, mod = ev part = Win.FindWindow(where)[0] if Dlg.IsDialogEvent(ev): ds = Dlg.DialogSelect(ev) if ds[0] and ds[1] == self.d and ds[-1] == 1: self.w.HideWindow() self.w = None self.d = None raise KeyboardInterrupt, ev elif part == 4: self.w.DragWindow(where, screenbounds) else: MacOS.HandleEvent(ev) return def set(self, value, max=None): """set(value) - Set progress bar position""" if max is not None: self.maxval = max bar = self.d.GetDialogItemAsControl(3) if max <= 0: bar.SetControlData(0, kControlProgressBarIndeterminateTag, '\x01') else: bar.SetControlData(0, kControlProgressBarIndeterminateTag, '\x00') if value < 0: value = 0 elif value > self.maxval: value = self.maxval self.curval = value self._update(value) return def inc(self, n=1): """inc(amt) - Increment progress bar position""" self.set(self.curval + n)
"""Easy to use dialogs.