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

Last change on this file since 6480 was 5659, checked in by ldelgass, 9 years ago

whitespace

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