source: trunk/gui/scripts/progress.tcl @ 154

Last change on this file since 154 was 115, checked in by mmc, 19 years ago

Updated all copyright notices.

File size: 5.7 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: progress - progress meter for long-running apps
3#
4#  This widget acts as a progress meter for long-running applications.
5#  It accepts progress reports as a combination of a percentage and
6#  a message, and then displays the percentage on a bar 0-100%.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005  Purdue Research Foundation
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15
16option add *Progress.borderWidth 2 widgetDefault
17option add *Progress.relief sunken widgetDefault
18option add *Progress.length 2i widgetDefault
19option add *Progress.barBackground white widgetDefault
20option add *Progress.barColor blue widgetDefault
21option add *Progress.font -*-helvetica-medium-r-normal-*-*-120-* widgetDefault
22
23itcl::class Rappture::Progress {
24    inherit itk::Widget
25
26    itk_option define -barcolor barColor BarColor ""
27    itk_option define -barbackground barBackground BarBackground ""
28    itk_option define -length length Length 0
29
30    constructor {args} { # defined below }
31    public method settings {args}
32
33    protected method _redraw {}
34
35    private variable _message ""  ;# status message
36    private variable _percent 0   ;# status percentage
37}
38
39itk::usual Progress {
40    keep -cursor -font -foreground -background
41}
42
43# ----------------------------------------------------------------------
44# CONSTRUCTOR
45# ----------------------------------------------------------------------
46itcl::body Rappture::Progress::constructor {args} {
47    itk_component add bar {
48        canvas $itk_interior.bar -highlightthickness 0
49    } {
50        usual
51        keep -borderwidth -relief
52        ignore -highlightthickness -highlightbackground -highlightcolor
53    }
54    pack $itk_component(bar) -expand yes -fill both
55    bind $itk_component(bar) <Configure> [itcl::code $this _redraw]
56
57    itk_component add message {
58        label $itk_interior.mesg -anchor w
59    }
60
61    eval itk_initialize $args
62    component hull configure -borderwidth 0
63
64    set h [font metrics $itk_option(-font) -linespace]
65    $itk_component(bar) configure -height [expr {$h+4}]
66}
67
68# ----------------------------------------------------------------------
69# USAGE: settings ?-percent <val>? ?-message <string>?
70#
71# Clients use this to query/set the settings shown in the progress
72# meter.  With no args, it returns a list of the form "-percent v
73# -message str".  Otherwise, it interprets the args and updates
74# the values.
75# ----------------------------------------------------------------------
76itcl::body Rappture::Progress::settings {args} {
77    if {[llength $args] == 0} {
78        return [list -percent $_percent -message $_message]
79    }
80
81    Rappture::getopts args params {
82        value -percent ""
83        value -message "__ignore__"
84    }
85
86    set changed 0
87    if {$params(-percent) != ""} {
88        if {![string is double $params(-percent)]
89              || $params(-percent) < 0 || $params(-percent) > 100} {
90            error "bad value \"$params(-percent)\": should be 0-100"
91        }
92        set _percent $params(-percent)
93        set changed 1
94    }
95    if {$params(-message) != "__ignore__"} {
96        set _message $params(-message)
97        set changed 1
98    }
99
100    if {$changed} {
101        _redraw
102        update idletasks
103    }
104}
105
106# ----------------------------------------------------------------------
107# USAGE: _redraw
108#
109# Used internally to redraw the progress meter on the internal canvas.
110# ----------------------------------------------------------------------
111itcl::body Rappture::Progress::_redraw {} {
112    set w [winfo width $itk_component(bar)]
113    set h [winfo height $itk_component(bar)]
114
115    if {[string length $_message] > 0} {
116        $itk_component(message) configure -text $_message
117        pack $itk_component(message) -anchor w
118    } else {
119        pack forget $itk_component(message)
120    }
121
122    if {[$itk_component(bar) find all] == ""} {
123        $itk_component(bar) create rectangle 0 0 1 1 \
124            -outline "" -fill $itk_option(-barbackground) -tags barbg
125        $itk_component(bar) create rectangle 0 0 1 1 \
126            -outline "" -fill $itk_option(-barcolor) -tags bar
127        $itk_component(bar) create text 0 0 \
128            -anchor center -text "" -font $itk_option(-font) -tags number
129    }
130
131    set msg [format "%3.0f%%" $_percent]
132    $itk_component(bar) itemconfigure number -text $msg
133
134    set barw [expr {0.01*$_percent*$w}]
135    $itk_component(bar) coords number [expr {$w/2}] [expr {$h/2}]
136    $itk_component(bar) coords bar 0 0 $barw $h
137    $itk_component(bar) coords barbg 0 0 $w $h
138}
139
140# ----------------------------------------------------------------------
141# CONFIGURATION OPTION: -barcolor
142# ----------------------------------------------------------------------
143itcl::configbody Rappture::Progress::barcolor {
144    $itk_component(bar) itemconfigure bar -fill $itk_option(-barcolor)
145}
146
147# ----------------------------------------------------------------------
148# CONFIGURATION OPTION: -barbackground
149# ----------------------------------------------------------------------
150itcl::configbody Rappture::Progress::barbackground {
151    $itk_component(bar) itemconfigure barbg -fill $itk_option(-barbackground)
152}
153
154# ----------------------------------------------------------------------
155# CONFIGURATION OPTION: -length
156# ----------------------------------------------------------------------
157itcl::configbody Rappture::Progress::length {
158    set w [winfo pixels $itk_component(hull) $itk_option(-length)]
159    component hull configure -width $w
160}
Note: See TracBrowser for help on using the repository browser.