ProgressIndicator.st
changeset 183 c63a4f284a6d
parent 86 4d7dbb5f1719
child 184 13a2f3677c68
--- a/ProgressIndicator.st	Thu May 16 12:24:38 1996 +0200
+++ b/ProgressIndicator.st	Sat May 18 13:09:56 1996 +0200
@@ -11,11 +11,10 @@
 "
 
 View subclass:#ProgressIndicator
-	 instanceVariableNames:'percentage showPercentage fgColor
-				connectedTop connectedLabel'
-	 classVariableNames:''
-	 poolDictionaries:''
-	 category:'Views-Misc'
+	instanceVariableNames:'percentage showPercentage fgColor connectedTop connectedLabel'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Views-Misc'
 !
 
 !ProgressIndicator class methodsFor:'documentation'!
@@ -34,14 +33,16 @@
 "
 !
 
-version
-    ^ '$Header: /cvs/stx/stx/libwidg2/ProgressIndicator.st,v 1.4 1995-11-11 16:30:31 cg Exp $'
-!
-
 documentation
 "
     a view showing a rectangle filled according the percentage value.
     Can be used as a progress indicator a la MSwindows.
+
+    [author:]
+        Claus Gittinger
+
+    [see also:]
+        ActionWaitBox AnimatedLabel
 "
 !
 
@@ -50,6 +51,10 @@
     basic (internal) interface
     (if progress indicator is to be used in a complex box ...):
 
+    Before you get frustrated - see the convenient-interface examples
+    at the end ;-)
+
+                                                                        [exBegin]
       |top p h|
 
       top := ModalBox new.
@@ -60,22 +65,22 @@
       p level:-1.
       h := p preferredExtent y.
       p topInset:(h // 2) negated; 
-	bottomInset:(h // 2) negated;
-	leftInset:5;
-	rightInset:5.
+        bottomInset:(h // 2) negated;
+        leftInset:5;
+        rightInset:5.
 
       [
-	  1 to:100 do:[:val |
-	      (Delay forSeconds:0.05) wait.
-	      p percentage:val 
-	  ].
-	  top hide.
+          1 to:100 do:[:val |
+              (Delay forSeconds:0.05) wait.
+              p percentage:val 
+          ].
+          top hide.
       ] fork.
       top open.
-
+                                                                        [exEnd]
 
     changing colors, turning percentage display off:
-
+                                                                        [exBegin]
       |top p h|
 
       top := StandardSystemView new.
@@ -88,19 +93,20 @@
       p foregroundColor:(Color red).
       h := 10.
       p topInset:(h // 2) negated; 
-	bottomInset:(h // 2) negated;
-	leftInset:5;
-	rightInset:5.
+        bottomInset:(h // 2) negated;
+        leftInset:5;
+        rightInset:5.
       top open.
       [
-	  1 to:100 do:[:val |
-	      (Delay forSeconds:0.05) wait.
-	      p percentage:val 
-	  ]
+          1 to:100 do:[:val |
+              (Delay forSeconds:0.05) wait.
+              p percentage:val 
+          ]
       ] fork
+                                                                        [exEnd]
 
     with border (2D look):
-
+                                                                        [exBegin]
       |top p h|
 
       top := StandardSystemView new.
@@ -111,20 +117,21 @@
       p borderWidth:1.
       h := p preferredExtent y.
       p topInset:(h // 2) negated; 
-	bottomInset:(h // 2) negated;
-	leftInset:5;
-	rightInset:5.
+        bottomInset:(h // 2) negated;
+        leftInset:5;
+        rightInset:5.
       top open.
       [
-	  1 to:100 do:[:val |
-	      (Delay forSeconds:0.05) wait.
-	      p percentage:val 
-	  ]
+          1 to:100 do:[:val |
+              (Delay forSeconds:0.05) wait.
+              p percentage:val 
+          ]
       ] fork
+                                                                        [exEnd]
 
 
     getting progress from a model:
-
+                                                                        [exBegin]
       |model top p h|
 
       model := 0 asValue.
@@ -138,23 +145,24 @@
       p level:-1.
       h := p preferredExtent y.
       p topInset:(h // 2) negated; 
-	bottomInset:(h // 2) negated;
-	leftInset:5;
-	rightInset:5.
+        bottomInset:(h // 2) negated;
+        leftInset:5;
+        rightInset:5.
       top open.
 
       [
-	  1 to:100 do:[:val |
-	      (Delay forSeconds:0.05) wait.
-	      model value:val 
-	  ]
+          1 to:100 do:[:val |
+              (Delay forSeconds:0.05) wait.
+              model value:val 
+          ]
       ] fork
+                                                                        [exEnd]
 
 
     concrete example:
       search all files in the source directory for a string
       using grep. Show progress while doing so.
-
+                                                                        [exBegin]
       |top p h names done|
 
       top := StandardSystemView new.
@@ -165,29 +173,33 @@
       p level:-1.
       h := p preferredExtent y.
       p topInset:(h // 2) negated; 
-	bottomInset:(h // 2) negated;
-	leftInset:5;
-	rightInset:5.
+        bottomInset:(h // 2) negated;
+        leftInset:5;
+        rightInset:5.
       top openWithPriority:(Processor activePriority + 1).
 
       names := 'source' asFilename directoryContents.
       done := 0.
       names do:[:aName |
-	|stream line|
+        |fn stream line|
 
-	p percentage:(done / names size * 100).
-	stream := ('source/' , aName) asFilename readStream.
-	[stream atEnd] whileFalse:[
-	    line := stream nextLine.
-	    (line findString:'subclass:') ~~ 0 ifTrue:[
-		Transcript showCr:line
-	    ].
-	].
-	stream close.
-	done := done + 1
+        p percentage:(done / names size * 100).
+        fn := ('source/' , aName) asFilename.
+        fn isDirectory ifFalse:[
+            stream := fn readStream.
+            [stream atEnd] whileFalse:[
+                line := stream nextLine.
+                (line findString:'subclass:') ~~ 0 ifTrue:[
+                    Transcript showCr:line
+                ].
+            ].
+            stream close.
+        ].
+        done := done + 1
       ].
 
       top destroy
+                                                                        [exEnd]
 
 
    using the convenient inBox-interface
@@ -196,136 +208,106 @@
     to indicate ...)
 
     basic interface demonstration:
-
+                                                                        [exBegin]
       |p|
 
       p := ProgressIndicator 
-		inBoxWithLabel:'doing something  ...'
-		abortable:true.
+                inBoxWithLabel:'doing something  ...'
+                abortable:true.
       p showProgressOf:
-	    [:progressValue :currentAction |
+            [:progressValue :currentAction |
 
-	      1 to:100 do:[:val |
-		  (Delay forSeconds:0.05) wait.
-		  val == 25 ifTrue:[
-		      currentAction value:'still going ...'
-		  ].
-		  val == 50 ifTrue:[
-		      currentAction value:'halfway through ...'
-		  ].
-		  val == 75 ifTrue:[
-		      currentAction value:'almost finished ...'
-		  ].
-		  progressValue value:val 
-	      ]
-	    ]
+              1 to:100 do:[:val |
+                  (Delay forSeconds:0.05) wait.
+                  val == 25 ifTrue:[
+                      currentAction value:'still going ...'
+                  ].
+                  val == 50 ifTrue:[
+                      currentAction value:'halfway through ...'
+                  ].
+                  val == 75 ifTrue:[
+                      currentAction value:'almost finished ...'
+                  ].
+                  progressValue value:val 
+              ]
+            ]
+                                                                        [exEnd]
 
 
     above search example using this convenient interface:
-
+                                                                        [exBegin]
       |p|
 
-      p := ProgressIndicator inBoxWithLabel:'searching files ...'.
+      p := ProgressIndicator 
+                inBoxWithLabel:'searching files ...'
+                abortable:false.
       p showProgressOf:
-	    [:progressValue :currentAction |
-		|names nDone|
+            [:progressValue :currentAction |
+                |names nDone|
 
-		names := 'source' asFilename directoryContents.
-		nDone := 0.
-		names do:[:aName |
-		  |stream line|
+                names := 'source' asFilename directoryContents.
+                nDone := 0.
+                names do:[:aName |
+                  |fn stream line|
+
+                  progressValue value:(nDone / names size * 100).
+                  currentAction value:'searching ' , 'source/' , aName , ' ...'.
 
-		  progressValue value:(nDone / names size * 100).
-		  currentAction value:'searching ' , 'source/' , aName , ' ...'.
-
-		  stream := ('source/' , aName) asFilename readStream.
-		  [stream atEnd] whileFalse:[
-		      line := stream nextLine.
-		      (line findString:'subclass:') ~~ 0 ifTrue:[
-			  Transcript showCr:line
-		      ].
-		  ].
-		  stream close.
-		  nDone := nDone + 1
-		].
-	    ].
+                  fn := ('source/' , aName) asFilename.
+                  fn isDirectory ifFalse:[
+                      stream := fn readStream.
+                      [stream atEnd] whileFalse:[
+                          line := stream nextLine.
+                          (line findString:'subclass:') ~~ 0 ifTrue:[
+                              Transcript showCr:line
+                          ].
+                      ].
+                      stream close.
+                  ].
+                  nDone := nDone + 1
+                ].
+            ].
+                                                                        [exEnd]
 
 
     a nice example: copying files a la windows ...
-
+    the following copies all files to /dev/null.
+                                                                        [exBegin]
       |p|
 
       (ProgressIndicator 
-		inBoxWithLabel:'copy files to /dev/null ...'
-		abortable:true)
-	 showProgressOf:
-	    [:progressValue :currentAction |
-		|files nFiles nDone|
-
-		files := '.' asFilename directoryContents.
-		nFiles := files size.
-		nDone := 0.
-		files do:[:aFileName |
-		    |percent|
+                inBoxWithLabel:'copy files to /dev/null ...'
+                abortable:true)
+         showProgressOf:
+            [:progressValue :currentAction |
+                |files nFiles nDone|
 
-		    nDone := nDone + 1.
-		    percent := nDone / nFiles * 100.
-		    progressValue value:percent. 
-		    aFileName asFilename isDirectory ifTrue:[
-			Transcript showCr:('skipping ' , aFileName , ' ...'). 
-			currentAction value:('skipping ' , aFileName , ' ...'). 
-		    ] ifFalse:[
-			Transcript showCr:('copying ' , aFileName , ' ...').
-			currentAction value:('copying ' , aFileName , ' ...').
-			Object errorSignal handle:[:ex |
-			    self warn:'an error occurred while copying ' , aFileName.
-			    ex return
-			] do:[
-			    aFileName asFilename copyTo:'/dev/null'.
-			]
-		    ].
-		].
-	    ].
-"
-! !
-
-!ProgressIndicator methodsFor:'drawing'!
+                files := '.' asFilename directoryContents.
+                nFiles := files size.
+                nDone := 0.
+                files do:[:aFileName |
+                    |percent|
 
-redraw
-    |s rx sx sy sw m w h|
-
-    m := margin + 1.
-    w := width - (m*2).
-    h := height - (m*2).
-
-    s := percentage printString , ' %'.
-    sw := font widthOf:s.
-    sx := (width - sw) // 2.
-    sy := height // 2 + font descent + 2.
-
-    rx := (w * percentage / 100) rounded.
-
-    self paint:Color white.
-    self fillRectangleX:m y:m width:w height:h.
-
-    showPercentage ifTrue:[
-	rx <= (sx+sw) ifTrue:[
-	    self paint:Color black.
-	    self displayString:s x:sx y:sy.
-	]
-    ].
-
-    self paint:fgColor.
-    self fillRectangleX:m y:m width:rx height:h.
-
-    showPercentage ifTrue:[
-	rx >= sx ifTrue:[
-	    self clipRect:(m@m corner:rx+1 @ h).
-	    self paint:Color white.
-	    self displayString:s x:sx y:sy.
-	    self clipRect:nil
-	]
-    ]
+                    nDone := nDone + 1.
+                    percent := nDone / nFiles * 100.
+                    progressValue value:percent. 
+                    aFileName asFilename isDirectory ifTrue:[
+                        Transcript showCr:('skipping ' , aFileName , ' ...'). 
+                        currentAction value:('skipping ' , aFileName , ' ...'). 
+                    ] ifFalse:[
+                        Transcript showCr:('copying ' , aFileName , ' ...').
+                        currentAction value:('copying ' , aFileName , ' ...').
+                        Object errorSignal handle:[:ex |
+                            self warn:'an error occurred while copying ' , aFileName.
+                            ex return
+                        ] do:[
+                            aFileName asFilename copyTo:'/dev/null'.
+                        ]
+                    ].
+                ].
+            ].
+                                                                        [exEnd]
+"
 ! !
 
 !ProgressIndicator class methodsFor:'instance creation'!
@@ -377,6 +359,92 @@
     ^ p
 ! !
 
+!ProgressIndicator methodsFor:'accessing'!
+
+foregroundColor:aColor 
+    fgColor := aColor
+!
+
+percentage:aNumber
+    |newPercentage|
+
+    newPercentage := ((aNumber max:0) min:100) rounded.
+    newPercentage ~~ percentage ifTrue:[
+	percentage := newPercentage.
+	shown ifTrue:[self redraw].
+    ]
+!
+
+showPercentage:aBoolean
+    showPercentage := aBoolean
+! !
+
+!ProgressIndicator methodsFor:'change & update'!
+
+update:aspect with:aParameter from:changedObject
+    (aspect == aspectMsg
+    and:[changedObject == model]) ifTrue:[
+	self percentage:(model perform:aspectMsg).
+	^ self
+    ].
+    ^ super update:aspect with:aParameter from:changedObject
+! !
+
+!ProgressIndicator methodsFor:'drawing'!
+
+redraw
+    |s rx sx sy sw m w h|
+
+    m := margin + 1.
+    w := width - (m*2).
+    h := height - (m*2).
+
+    s := percentage printString , ' %'.
+    sw := font widthOf:s.
+    sx := (width - sw) // 2.
+    sy := height // 2 + font descent + 2.
+
+    rx := (w * percentage / 100) rounded.
+
+    self paint:Color white.
+    self fillRectangleX:m y:m width:w height:h.
+
+    showPercentage ifTrue:[
+	rx <= (sx+sw) ifTrue:[
+	    self paint:Color black.
+	    self displayString:s x:sx y:sy.
+	]
+    ].
+
+    self paint:fgColor.
+    self fillRectangleX:m y:m width:rx height:h.
+
+    showPercentage ifTrue:[
+	rx >= sx ifTrue:[
+	    self clipRect:(m@m corner:rx+1 @ h).
+	    self paint:Color white.
+	    self displayString:s x:sx y:sy.
+	    self clipRect:nil
+	]
+    ]
+! !
+
+!ProgressIndicator methodsFor:'initialization'!
+
+initialize
+    super initialize.
+    viewBackground := styleSheet colorAt:'progressIndicatorViewBackground' default:Color white.
+    fgColor := styleSheet colorAt:'progressIndicatorForegroundColor' default:Color blue.
+    percentage := 0.
+    showPercentage := true.
+! !
+
+!ProgressIndicator methodsFor:'queries'!
+
+preferredExtent
+    ^ 100 @ (font height + font descent + ((margin + 1) * 2))
+! !
+
 !ProgressIndicator methodsFor:'showing progress'!
 
 connectToTop:top label:label
@@ -460,49 +528,8 @@
     "
 ! !
 
-!ProgressIndicator methodsFor:'initialization'!
-
-initialize
-    super initialize.
-    viewBackground := styleSheet colorAt:'progressIndicatorViewBackground' default:Color white.
-    fgColor := styleSheet colorAt:'progressIndicatorForegroundColor' default:Color blue.
-    percentage := 0.
-    showPercentage := true.
-! !
-
-!ProgressIndicator methodsFor:'accessing'!
-
-percentage:aNumber
-    |newPercentage|
-
-    newPercentage := ((aNumber max:0) min:100) rounded.
-    newPercentage ~~ percentage ifTrue:[
-	percentage := newPercentage.
-	shown ifTrue:[self redraw].
-    ]
-!
+!ProgressIndicator class methodsFor:'documentation'!
 
-showPercentage:aBoolean
-    showPercentage := aBoolean
-!
-
-foregroundColor:aColor 
-    fgColor := aColor
+version
+    ^ '$Header: /cvs/stx/stx/libwidg2/ProgressIndicator.st,v 1.5 1996-05-18 11:09:56 cg Exp $'
 ! !
-
-!ProgressIndicator methodsFor:'queries'!
-
-preferredExtent
-    ^ 100 @ (font height + font descent + ((margin + 1) * 2))
-! !
-
-!ProgressIndicator methodsFor:'change & update'!
-
-update:aspect with:aParameter from:changedObject
-    (aspect == aspectMsg
-    and:[changedObject == model]) ifTrue:[
-	self percentage:(model perform:aspectMsg).
-	^ self
-    ].
-    ^ super update:aspect with:aParameter from:changedObject
-! !