]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Added a ProgressBar() class (which shows a progress bar). Needs new
authorJack Jansen <jack.jansen@cwi.nl>
Tue, 14 Nov 1995 10:13:49 +0000 (10:13 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Tue, 14 Nov 1995 10:13:49 +0000 (10:13 +0000)
resource.

Mac/Lib/EasyDialogs.py

index dc668b04971469c3591c817e31ce9352048c7572..3dfe9f3d62a91c1f4561fee2f65375e5446211cc 100644 (file)
@@ -3,6 +3,8 @@
 Message(msg) -- display a message and an OK button.
 AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
 AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
+bar = Progress(label, maxvalue) -- Display a progress bar
+bar.set(value) -- Set value
 
 More documentation in each function.
 This module uses DLOG resources 256, 257 and 258.
@@ -10,6 +12,12 @@ Based upon STDWIN dialogs with the same names and functions.
 """
 
 from Dlg import GetNewDialog, SetDialogItemText, GetDialogItemText, ModalDialog
+import Qd
+import addpack
+addpack.addpack('Tools')
+addpack.addpack('bgen')
+addpack.addpack('qd')
+import QuickDraw
 
 
 def Message(msg):
@@ -27,6 +35,7 @@ def Message(msg):
                return
        tp, h, rect = d.GetDialogItem(2)
        SetDialogItemText(h, msg)
+       d.SetDialogDefaultItem(1)
        while 1:
                n = ModalDialog(None)
                if n == 1:
@@ -56,6 +65,8 @@ def AskString(prompt, default = ""):
        tp, h, rect = d.GetDialogItem(4)
        SetDialogItemText(h, default)
 #      d.SetDialogItem(4, 0, 255)
+       d.SetDialogDefaultItem(1)
+       d.SetDialogCancelItem(2)
        while 1:
                n = ModalDialog(None)
                if n == 1:
@@ -90,12 +101,60 @@ def AskYesNoCancel(question, default = 0):
        # The question string is item 5
        tp, h, rect = d.GetDialogItem(5)
        SetDialogItemText(h, question)
+       d.SetDialogCancelItem(4)
+       if default in (2, 3, 4):
+               d.SetDialogDefaultItem(default)
        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
+               
+class ProgressBar:
+       def __init__(self, label="Working...", maxval=100):
+               self.label = label
+               self.maxval = maxval
+               self.curval = -1
+               self.d = GetNewDialog(259, -1)
+               tp, text_h, rect = self.d.GetDialogItem(2)
+               SetDialogItemText(text_h, "Progress...")
+               self._update(0)
+               
+       def _update(self, value):
+               tp, h, bar_rect = self.d.GetDialogItem(3)
+               Qd.SetPort(self.d)
+               
+               Qd.FrameRect(bar_rect)  # Draw outline
+               
+               inner_rect = Qd.InsetRect(bar_rect, 1, 1)
+               Qd.ForeColor(QuickDraw.whiteColor)
+               Qd.BackColor(QuickDraw.whiteColor)
+               Qd.PaintRect(inner_rect)        # Clear internal
+               
+               l, t, r, b = inner_rect
+               r = int(l + (r-l)*value/self.maxval)
+               inner_rect = l, t, r, b
+               Qd.ForeColor(QuickDraw.blackColor)
+               Qd.BackColor(QuickDraw.blackColor)
+               Qd.PaintRect(inner_rect)        # Draw bar
+               
+               # Restore settings
+               Qd.ForeColor(QuickDraw.blackColor)
+               Qd.BackColor(QuickDraw.whiteColor)
+               
+               # Test for cancel button
+               if ModalDialog(self._filterfunc) == 1:
+                       raise KeyboardInterrupt
+                       
+       def _filterfunc(self, d, e, *more):
+               return 2 # XXXX For now, this disables the cancel button
+                               
+       def set(self, value):
+               if value < 0: value = 0
+               if value > self.maxval: value = self.maxval
+               self._update(value)
+               
 
 
 def test():
@@ -104,6 +163,10 @@ def test():
        if ok > 0:
                s = AskString("Enter your first name")
                Message("Thank you,\015%s" % `s`)
+       bar = ProgressBar("Counting...", 100)
+       for i in range(100):
+               bar.set(i)
+       del bar
 
 
 if __name__ == '__main__':