|
16 | 16 | from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog |
17 | 17 | import Qd |
18 | 18 | import QuickDraw |
| 19 | +import Dialogs |
| 20 | +import Windows |
19 | 21 | import Dlg,Win,Evt,Events # sdm7g |
20 | 22 | import MacOS |
21 | 23 | import string |
@@ -86,6 +88,75 @@ def AskString(prompt, default = "", id=257): |
86 | 88 | return cr2lf(GetDialogItemText(h)) |
87 | 89 | if n == 2: return None |
88 | 90 |
|
| 91 | +def AskPassword(prompt, default='', id=257): |
| 92 | + """Display a PROMPT string and a text entry field with a DEFAULT string. |
| 93 | + The string is displayed as bullets only. |
| 94 | + |
| 95 | + Return the contents of the text entry field when the user clicks the |
| 96 | + OK button or presses Return. |
| 97 | + Return None when the user clicks the Cancel button. |
| 98 | + |
| 99 | + If omitted, DEFAULT is empty. |
| 100 | + |
| 101 | + The PROMPT and DEFAULT strings, as well as the return value, |
| 102 | + can be at most 255 characters long. |
| 103 | + """ |
| 104 | + d = GetNewDialog(id, -1) |
| 105 | + if not d: |
| 106 | + print "Can't get DLOG resource with id =", id |
| 107 | + return |
| 108 | + tp, h, rect = d.GetDialogItem(3) # STATIC TEXT ITEM <= prompt |
| 109 | + SetDialogItemText(h, lf2cr(prompt)) |
| 110 | + tp, h, rect = d.GetDialogItem(4) # EDIT TEXT ITEM |
| 111 | + bullets = '\245'*len(default) |
| 112 | + SetDialogItemText(h, bullets ) |
| 113 | + d.SelectDialogItemText(4, 999, 999) |
| 114 | + d.SetDialogDefaultItem(Dialogs.ok) |
| 115 | + d.SetDialogCancelItem(Dialogs.cancel) |
| 116 | + string = default |
| 117 | + oldschedparams = MacOS.SchedParams(0,0) |
| 118 | + while 1: |
| 119 | + ready,ev = Evt.WaitNextEvent( -1, 6 ) |
| 120 | + if not ready: continue |
| 121 | + what,msg,when,where,mod = ev |
| 122 | + if what == 0 : Dlg.DialogSelect(ev) # for blinking caret |
| 123 | + elif Dlg.IsDialogEvent(ev): |
| 124 | + if what == Events.keyDown: |
| 125 | + charcode = msg & Events.charCodeMask |
| 126 | + if ( mod & Events.cmdKey ): |
| 127 | + MacOS.SysBeep() |
| 128 | + continue # don't do cut & paste commands |
| 129 | + else: |
| 130 | + if charcode == Events.kReturnCharCode: |
| 131 | + break |
| 132 | + elif charcode == Events.kEscapeCharCode: |
| 133 | + string = None |
| 134 | + break |
| 135 | + elif charcode in (Events.kLeftArrowCharCode, |
| 136 | + Events.kBackspaceCharCode): |
| 137 | + string = string[:-1] |
| 138 | + else: |
| 139 | + string = string + chr(charcode) |
| 140 | + msg = 0245 # Octal code for bullet |
| 141 | + ev = (what,msg,when,where,mod) |
| 142 | + rs, win, item = Dlg.DialogSelect(ev) |
| 143 | + if item == Dialogs.ok : |
| 144 | + break |
| 145 | + elif item == Dialogs.cancel : |
| 146 | + string = None |
| 147 | + break |
| 148 | + elif what == Events.mouseDown: |
| 149 | + part, win = Win.FindWindow(where) |
| 150 | + if part == Windows.inDrag and win: |
| 151 | + win.DragWindow(where, screenbounds) |
| 152 | + elif part == Windows.inMenuBar: |
| 153 | + MacOS.HandleEvent(ev) |
| 154 | + else: |
| 155 | + MacOS.SysBeep() # Cannot handle selections, unfortunately |
| 156 | + |
| 157 | + elif what == Events.updateEvt: MacOS.HandleEvent(ev) |
| 158 | + apply(MacOS.SchedParams, oldschedparams) |
| 159 | + return string |
89 | 160 |
|
90 | 161 | def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=258): |
91 | 162 | ## """Display a QUESTION string which can be answered with Yes or No. |
|
0 commit comments