1995-04-05 06:18:35 -03:00
|
|
|
"""Easy to use dialogs.
|
|
|
|
|
|
|
|
Message(msg) -- display a message and an OK button.
|
|
|
|
AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
|
1999-02-14 20:04:05 -04:00
|
|
|
AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
|
1995-04-05 06:18:35 -03:00
|
|
|
AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
|
1995-11-14 06:13:49 -04:00
|
|
|
bar = Progress(label, maxvalue) -- Display a progress bar
|
|
|
|
bar.set(value) -- Set value
|
1997-05-12 12:44:14 -03:00
|
|
|
bar.inc( *amount ) -- increment value by amount (default=1)
|
|
|
|
bar.label( *newlabel ) -- get or set text label.
|
1995-04-05 06:18:35 -03:00
|
|
|
|
|
|
|
More documentation in each function.
|
1999-12-12 18:57:51 -04:00
|
|
|
This module uses DLOG resources 260 and on.
|
1995-04-05 06:18:35 -03:00
|
|
|
Based upon STDWIN dialogs with the same names and functions.
|
|
|
|
"""
|
|
|
|
|
1995-07-17 10:25:15 -03:00
|
|
|
from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
|
1995-11-14 06:13:49 -04:00
|
|
|
import Qd
|
|
|
|
import QuickDraw
|
1999-02-10 18:38:44 -04:00
|
|
|
import Dialogs
|
|
|
|
import Windows
|
1997-05-12 12:44:14 -03:00
|
|
|
import Dlg,Win,Evt,Events # sdm7g
|
1999-12-13 12:07:01 -04:00
|
|
|
import Ctl
|
1998-07-01 12:47:44 -03:00
|
|
|
import MacOS
|
|
|
|
import string
|
1999-12-13 12:07:01 -04:00
|
|
|
from ControlAccessor import * # Also import Controls constants
|
1998-07-01 12:47:44 -03:00
|
|
|
|
|
|
|
def cr2lf(text):
|
|
|
|
if '\r' in text:
|
|
|
|
text = string.join(string.split(text, '\r'), '\n')
|
|
|
|
return text
|
|
|
|
|
|
|
|
def lf2cr(text):
|
|
|
|
if '\n' in text:
|
|
|
|
text = string.join(string.split(text, '\n'), '\r')
|
1998-09-28 07:37:08 -03:00
|
|
|
if len(text) > 253:
|
|
|
|
text = text[:253] + '\311'
|
1998-07-01 12:47:44 -03:00
|
|
|
return text
|
1995-04-05 06:18:35 -03:00
|
|
|
|
1999-12-12 18:57:51 -04:00
|
|
|
def Message(msg, id=260, ok=None):
|
1995-04-05 06:18:35 -03:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
d = GetNewDialog(id, -1)
|
|
|
|
if not d:
|
|
|
|
print "Can't get DLOG resource with id =", id
|
|
|
|
return
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(2)
|
1998-07-01 12:47:44 -03:00
|
|
|
SetDialogItemText(h, lf2cr(msg))
|
1999-02-16 12:06:39 -04:00
|
|
|
if ok != None:
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(1)
|
|
|
|
h.SetControlTitle(ok)
|
1995-11-14 06:13:49 -04:00
|
|
|
d.SetDialogDefaultItem(1)
|
2000-01-18 09:36:02 -04:00
|
|
|
d.AutoSizeDialog()
|
2000-08-25 19:06:19 -03:00
|
|
|
d.GetDialogWindow().ShowWindow()
|
1995-04-05 06:18:35 -03:00
|
|
|
while 1:
|
|
|
|
n = ModalDialog(None)
|
|
|
|
if n == 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
|
1999-12-12 18:57:51 -04:00
|
|
|
def AskString(prompt, default = "", id=261, ok=None, cancel=None):
|
1995-04-05 06:18:35 -03:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
d = GetNewDialog(id, -1)
|
|
|
|
if not d:
|
|
|
|
print "Can't get DLOG resource with id =", id
|
|
|
|
return
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(3)
|
1998-07-01 12:47:44 -03:00
|
|
|
SetDialogItemText(h, lf2cr(prompt))
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(4)
|
1998-07-01 12:47:44 -03:00
|
|
|
SetDialogItemText(h, lf2cr(default))
|
1999-01-22 09:14:06 -04:00
|
|
|
d.SelectDialogItemText(4, 0, 999)
|
1995-07-17 10:25:15 -03:00
|
|
|
# d.SetDialogItem(4, 0, 255)
|
1999-02-16 12:06:39 -04:00
|
|
|
if ok != None:
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(1)
|
|
|
|
h.SetControlTitle(ok)
|
1999-02-16 12:06:39 -04:00
|
|
|
if cancel != None:
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(2)
|
|
|
|
h.SetControlTitle(cancel)
|
1995-11-14 06:13:49 -04:00
|
|
|
d.SetDialogDefaultItem(1)
|
|
|
|
d.SetDialogCancelItem(2)
|
2000-01-18 09:36:02 -04:00
|
|
|
d.AutoSizeDialog()
|
2000-08-25 19:06:19 -03:00
|
|
|
d.GetDialogWindow().ShowWindow()
|
1995-04-05 06:18:35 -03:00
|
|
|
while 1:
|
|
|
|
n = ModalDialog(None)
|
|
|
|
if n == 1:
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(4)
|
1998-07-01 12:47:44 -03:00
|
|
|
return cr2lf(GetDialogItemText(h))
|
1995-04-05 06:18:35 -03:00
|
|
|
if n == 2: return None
|
|
|
|
|
1999-12-13 12:07:01 -04:00
|
|
|
def AskPassword(prompt, default='', id=264, ok=None, cancel=None):
|
1999-02-10 18:38:44 -04:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
d = GetNewDialog(id, -1)
|
|
|
|
if not d:
|
|
|
|
print "Can't get DLOG resource with id =", id
|
|
|
|
return
|
1999-12-13 12:07:01 -04:00
|
|
|
h = d.GetDialogItemAsControl(3)
|
1999-02-10 18:38:44 -04:00
|
|
|
SetDialogItemText(h, lf2cr(prompt))
|
1999-12-13 12:07:01 -04:00
|
|
|
pwd = d.GetDialogItemAsControl(4)
|
1999-02-10 18:38:44 -04:00
|
|
|
bullets = '\245'*len(default)
|
1999-12-13 12:07:01 -04:00
|
|
|
## SetControlData(pwd, kControlEditTextPart, kControlEditTextTextTag, bullets)
|
|
|
|
SetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag, default)
|
|
|
|
d.SelectDialogItemText(4, 0, 999)
|
2000-08-25 19:06:19 -03:00
|
|
|
Ctl.SetKeyboardFocus(d.GetDialogWindow(), pwd, kControlEditTextPart)
|
1999-12-13 12:07:01 -04:00
|
|
|
if ok != None:
|
|
|
|
h = d.GetDialogItemAsControl(1)
|
|
|
|
h.SetControlTitle(ok)
|
|
|
|
if cancel != None:
|
|
|
|
h = d.GetDialogItemAsControl(2)
|
|
|
|
h.SetControlTitle(cancel)
|
1999-02-10 18:38:44 -04:00
|
|
|
d.SetDialogDefaultItem(Dialogs.ok)
|
|
|
|
d.SetDialogCancelItem(Dialogs.cancel)
|
2000-01-18 09:36:02 -04:00
|
|
|
d.AutoSizeDialog()
|
2000-08-25 19:06:19 -03:00
|
|
|
d.GetDialogWindow().ShowWindow()
|
1999-02-10 18:38:44 -04:00
|
|
|
while 1:
|
1999-12-13 12:07:01 -04:00
|
|
|
n = ModalDialog(None)
|
|
|
|
if n == 1:
|
|
|
|
h = d.GetDialogItemAsControl(4)
|
|
|
|
return cr2lf(GetControlData(pwd, kControlEditTextPart, kControlEditTextPasswordTag))
|
|
|
|
if n == 2: return None
|
1995-04-05 06:18:35 -03:00
|
|
|
|
1999-12-12 18:57:51 -04:00
|
|
|
def AskYesNoCancel(question, default = 0, yes=None, no=None, cancel=None, id=262):
|
1999-02-25 18:05:45 -04:00
|
|
|
"""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 strign ca be at most 255 characters.
|
|
|
|
"""
|
1995-04-05 06:18:35 -03:00
|
|
|
|
|
|
|
d = GetNewDialog(id, -1)
|
|
|
|
if not d:
|
|
|
|
print "Can't get DLOG resource with id =", id
|
|
|
|
return
|
|
|
|
# Button assignments:
|
|
|
|
# 1 = default (invisible)
|
|
|
|
# 2 = Yes
|
|
|
|
# 3 = No
|
|
|
|
# 4 = Cancel
|
|
|
|
# The question string is item 5
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(5)
|
1998-07-01 12:47:44 -03:00
|
|
|
SetDialogItemText(h, lf2cr(question))
|
1997-06-20 13:23:37 -03:00
|
|
|
if yes != None:
|
2000-02-10 12:15:53 -04:00
|
|
|
if yes == '':
|
|
|
|
d.HideDialogItem(2)
|
|
|
|
else:
|
|
|
|
h = d.GetDialogItemAsControl(2)
|
|
|
|
h.SetControlTitle(yes)
|
1997-06-20 13:23:37 -03:00
|
|
|
if no != None:
|
2000-02-10 12:15:53 -04:00
|
|
|
if no == '':
|
|
|
|
d.HideDialogItem(3)
|
|
|
|
else:
|
|
|
|
h = d.GetDialogItemAsControl(3)
|
|
|
|
h.SetControlTitle(no)
|
1997-06-20 13:23:37 -03:00
|
|
|
if cancel != None:
|
1999-02-16 12:06:39 -04:00
|
|
|
if cancel == '':
|
|
|
|
d.HideDialogItem(4)
|
|
|
|
else:
|
1999-12-12 18:57:51 -04:00
|
|
|
h = d.GetDialogItemAsControl(4)
|
|
|
|
h.SetControlTitle(cancel)
|
1995-11-14 06:13:49 -04:00
|
|
|
d.SetDialogCancelItem(4)
|
1996-04-10 11:49:41 -03:00
|
|
|
if default == 1:
|
|
|
|
d.SetDialogDefaultItem(2)
|
|
|
|
elif default == 0:
|
|
|
|
d.SetDialogDefaultItem(3)
|
|
|
|
elif default == -1:
|
|
|
|
d.SetDialogDefaultItem(4)
|
2000-01-18 09:36:02 -04:00
|
|
|
d.AutoSizeDialog()
|
2000-08-25 19:06:19 -03:00
|
|
|
d.GetDialogWindow().ShowWindow()
|
1995-04-05 06:18:35 -03:00
|
|
|
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
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
|
1995-11-14 06:13:49 -04:00
|
|
|
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
screenbounds = Qd.qd.screenBits.bounds
|
|
|
|
screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
|
|
|
|
screenbounds[2]-4, screenbounds[3]-4
|
|
|
|
|
|
|
|
|
1995-11-14 06:13:49 -04:00
|
|
|
class ProgressBar:
|
1999-12-12 18:57:51 -04:00
|
|
|
def __init__(self, title="Working...", maxval=100, label="", id=263):
|
1995-11-14 06:13:49 -04:00
|
|
|
self.maxval = maxval
|
|
|
|
self.curval = -1
|
1997-06-20 13:23:37 -03:00
|
|
|
self.d = GetNewDialog(id, -1)
|
1997-05-12 12:44:14 -03:00
|
|
|
self.label(label)
|
1995-11-14 06:13:49 -04:00
|
|
|
self._update(0)
|
2000-01-18 09:36:02 -04:00
|
|
|
self.d.AutoSizeDialog()
|
2000-07-24 11:07:15 -03:00
|
|
|
self.title(title)
|
2000-08-25 19:06:19 -03:00
|
|
|
self.d.GetDialogWindow().ShowWindow()
|
1999-12-12 18:57:51 -04:00
|
|
|
self.d.DrawDialog()
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
def __del__( self ):
|
1997-05-13 12:41:07 -03:00
|
|
|
self.d.BringToFront()
|
1997-05-12 12:44:14 -03:00
|
|
|
self.d.HideWindow()
|
|
|
|
del self.d
|
|
|
|
|
|
|
|
def title(self, newstr=""):
|
|
|
|
"""title(text) - Set title of progress window"""
|
|
|
|
w = self.d.GetDialogWindow()
|
2000-07-24 11:07:15 -03:00
|
|
|
w.BringToFront()
|
1997-05-12 12:44:14 -03:00
|
|
|
w.SetWTitle(newstr)
|
1995-11-14 06:13:49 -04:00
|
|
|
|
1997-05-12 12:44:14 -03:00
|
|
|
def label( self, *newstr ):
|
|
|
|
"""label(text) - Set text in progress box"""
|
2000-07-24 11:07:15 -03:00
|
|
|
self.d.GetDialogWindow().BringToFront()
|
1997-05-12 12:44:14 -03:00
|
|
|
if newstr:
|
1998-07-01 12:47:44 -03:00
|
|
|
self._label = lf2cr(newstr[0])
|
1999-12-12 18:57:51 -04:00
|
|
|
text_h = self.d.GetDialogItemAsControl(2)
|
1997-05-12 12:44:14 -03:00
|
|
|
SetDialogItemText(text_h, self._label)
|
|
|
|
|
1995-11-14 06:13:49 -04:00
|
|
|
def _update(self, value):
|
1999-11-05 11:53:10 -04:00
|
|
|
maxval = self.maxval
|
|
|
|
if maxval == 0:
|
|
|
|
# XXXX Quick fix. Should probably display an unknown duration
|
|
|
|
value = 0
|
|
|
|
maxval = 1
|
1999-12-12 18:57:51 -04:00
|
|
|
if maxval > 32767:
|
|
|
|
value = int(value/(maxval/32767.0))
|
|
|
|
maxval = 32767
|
|
|
|
progbar = self.d.GetDialogItemAsControl(3)
|
|
|
|
progbar.SetControlMaximum(maxval)
|
|
|
|
progbar.SetControlValue(value)
|
1995-11-14 06:13:49 -04:00
|
|
|
# Test for cancel button
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
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:
|
|
|
|
raise KeyboardInterrupt, ev
|
|
|
|
else:
|
|
|
|
if part == 4: # inDrag
|
|
|
|
self.d.DragWindow(where, screenbounds)
|
|
|
|
else:
|
|
|
|
MacOS.HandleEvent(ev)
|
|
|
|
|
1995-11-14 06:13:49 -04:00
|
|
|
|
1999-11-05 11:53:10 -04:00
|
|
|
def set(self, value, max=None):
|
1997-05-12 12:44:14 -03:00
|
|
|
"""set(value) - Set progress bar position"""
|
1999-11-05 11:53:10 -04:00
|
|
|
if max != None:
|
|
|
|
self.maxval = max
|
1995-11-14 06:13:49 -04:00
|
|
|
if value < 0: value = 0
|
|
|
|
if value > self.maxval: value = self.maxval
|
1997-05-12 12:44:14 -03:00
|
|
|
self.curval = value
|
1995-11-14 06:13:49 -04:00
|
|
|
self._update(value)
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
def inc(self, n=1):
|
|
|
|
"""inc(amt) - Increment progress bar position"""
|
|
|
|
self.set(self.curval + n)
|
|
|
|
|
1995-04-05 06:18:35 -03:00
|
|
|
def test():
|
1997-05-12 12:44:14 -03:00
|
|
|
import time
|
|
|
|
|
1995-04-05 06:18:35 -03:00
|
|
|
Message("Testing EasyDialogs.")
|
|
|
|
ok = AskYesNoCancel("Do you want to proceed?")
|
1999-12-13 12:07:01 -04:00
|
|
|
ok = AskYesNoCancel("Do you want to identify?", yes="Identify", no="No")
|
1995-04-05 06:18:35 -03:00
|
|
|
if ok > 0:
|
1999-01-22 09:14:06 -04:00
|
|
|
s = AskString("Enter your first name", "Joe")
|
1999-12-13 12:07:01 -04:00
|
|
|
s2 = AskPassword("Okay %s, tell us your nickname"%s, s, cancel="None")
|
|
|
|
if not s2:
|
|
|
|
Message("%s has no secret nickname"%s)
|
|
|
|
else:
|
|
|
|
Message("Hello everybody!!\nThe secret nickname of %s is %s!!!"%(s, s2))
|
1997-05-12 12:44:14 -03:00
|
|
|
text = ( "Working Hard...", "Hardly Working..." ,
|
|
|
|
"So far, so good!", "Keep on truckin'" )
|
|
|
|
bar = ProgressBar("Progress, progress...", 100)
|
|
|
|
try:
|
1997-06-12 07:51:18 -03:00
|
|
|
appsw = MacOS.SchedParams(1, 0)
|
1997-05-12 12:44:14 -03:00
|
|
|
for i in range(100):
|
|
|
|
bar.set(i)
|
|
|
|
time.sleep(0.1)
|
|
|
|
if i % 10 == 0:
|
|
|
|
bar.label(text[(i/10) % 4])
|
|
|
|
bar.label("Done.")
|
|
|
|
time.sleep(0.3) # give'em a chance to see the done.
|
|
|
|
finally:
|
|
|
|
del bar
|
1997-06-12 07:51:18 -03:00
|
|
|
apply(MacOS.SchedParams, appsw)
|
1995-04-05 06:18:35 -03:00
|
|
|
|
|
|
|
|
1997-05-12 12:44:14 -03:00
|
|
|
|
|
|
|
|
1995-04-05 06:18:35 -03:00
|
|
|
if __name__ == '__main__':
|
1997-05-12 12:44:14 -03:00
|
|
|
try:
|
|
|
|
test()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
Message("Operation Canceled.")
|
|
|
|
|