source: tags/20110826/gui/scripts/progress.tcl @ 4643

Last change on this file since 4643 was 1929, checked in by gah, 14 years ago
File size: 5.8 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-*-12-* 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 -width 1
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            error "bad value \"$params(-percent)\": should be 0-100"
90        }
91        if {$params(-percent) < 0} {
92            set params(-percent) 0
93        }
94        if {$params(-percent) > 100} {
95            set params(-percent) 100
96        }
97        set _percent $params(-percent)
98        set changed 1
99    }
100    if {$params(-message) != "__ignore__"} {
101        set _message $params(-message)
102        set changed 1
103    }
104
105    if {$changed} {
106        _redraw
107        update idletasks
108    }
109}
110
111# ----------------------------------------------------------------------
112# USAGE: _redraw
113#
114# Used internally to redraw the progress meter on the internal canvas.
115# ----------------------------------------------------------------------
116itcl::body Rappture::Progress::_redraw {} {
117    set w [winfo width $itk_component(bar)]
118    set h [winfo height $itk_component(bar)]
119
120    if {[string length $_message] > 0} {
121        $itk_component(message) configure -text $_message
122        pack $itk_component(message) -fill x
123    } else {
124        pack forget $itk_component(message)
125    }
126
127    if {[$itk_component(bar) find all] == ""} {
128        $itk_component(bar) create rectangle 0 0 1 1 \
129            -outline "" -fill $itk_option(-barbackground) -tags barbg
130        $itk_component(bar) create rectangle 0 0 1 1 \
131            -outline "" -fill $itk_option(-barcolor) -tags bar
132        $itk_component(bar) create text 0 0 \
133            -anchor center -text "" -font $itk_option(-font) -tags number
134    }
135
136    set msg [format "%3.0f%%" $_percent]
137    $itk_component(bar) itemconfigure number -text $msg
138
139    set barw [expr {0.01*$_percent*$w}]
140    $itk_component(bar) coords number [expr {$w/2}] [expr {$h/2}]
141    $itk_component(bar) coords bar 0 0 $barw $h
142    $itk_component(bar) coords barbg 0 0 $w $h
143}
144
145# ----------------------------------------------------------------------
146# CONFIGURATION OPTION: -barcolor
147# ----------------------------------------------------------------------
148itcl::configbody Rappture::Progress::barcolor {
149    $itk_component(bar) itemconfigure bar -fill $itk_option(-barcolor)
150}
151
152# ----------------------------------------------------------------------
153# CONFIGURATION OPTION: -barbackground
154# ----------------------------------------------------------------------
155itcl::configbody Rappture::Progress::barbackground {
156    $itk_component(bar) itemconfigure barbg -fill $itk_option(-barbackground)
157}
158
159# ----------------------------------------------------------------------
160# CONFIGURATION OPTION: -length
161# ----------------------------------------------------------------------
162itcl::configbody Rappture::Progress::length {
163    set w [winfo pixels $itk_component(hull) $itk_option(-length)]
164    $itk_component(bar) configure -width $w
165}
Note: See TracBrowser for help on using the repository browser.