~ubuntu-branches/ubuntu/gutsy/amsn/gutsy

« back to all changes in this revision

Viewing changes to utils/BWidget-1.7.0/progressbar.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Karkoulis
  • Date: 2006-01-04 15:26:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104152602-ipe1yg00rl3nlklv
Tags: 0.95-1
New Upstream Release (closes: #345052, #278575).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ----------------------------------------------------------------------------
 
2
#  progressbar.tcl
 
3
#  This file is part of Unifix BWidget Toolkit
 
4
# ----------------------------------------------------------------------------
 
5
#  Index of commands:
 
6
#     - ProgressBar::create
 
7
#     - ProgressBar::configure
 
8
#     - ProgressBar::cget
 
9
#     - ProgressBar::_destroy
 
10
#     - ProgressBar::_modify
 
11
# ----------------------------------------------------------------------------
 
12
 
 
13
namespace eval ProgressBar {
 
14
    Widget::define ProgressBar progressbar
 
15
 
 
16
    Widget::declare ProgressBar {
 
17
        {-type        Enum       normal     0
 
18
                      {normal incremental infinite nonincremental_infinite}}
 
19
        {-maximum     Int        100        0 "%d >= 0"}
 
20
        {-background  TkResource ""         0 frame}
 
21
        {-foreground  TkResource "blue"     0 label}
 
22
        {-borderwidth TkResource 2          0 frame}
 
23
        {-troughcolor TkResource ""         0 scrollbar}
 
24
        {-relief      TkResource sunken     0 label}
 
25
        {-orient      Enum       horizontal 1 {horizontal vertical}}
 
26
        {-variable    String     ""         0}
 
27
        {-idle        Boolean    0          0}
 
28
        {-width       TkResource 100        0 frame}
 
29
        {-height      TkResource 4m         0 frame}
 
30
        {-bg          Synonym    -background}
 
31
        {-fg          Synonym    -foreground}
 
32
        {-bd          Synonym    -borderwidth}
 
33
    }
 
34
 
 
35
    Widget::addmap ProgressBar "" :cmd {-background {} -width {} -height {}}
 
36
    Widget::addmap ProgressBar "" .bar {
 
37
        -troughcolor -background -borderwidth {} -relief {}
 
38
    }
 
39
 
 
40
    variable _widget
 
41
}
 
42
 
 
43
 
 
44
# ----------------------------------------------------------------------------
 
45
#  Command ProgressBar::create
 
46
# ----------------------------------------------------------------------------
 
47
proc ProgressBar::create { path args } {
 
48
    variable _widget
 
49
 
 
50
    array set maps [list ProgressBar {} :cmd {} .bar {}]
 
51
    array set maps [Widget::parseArgs ProgressBar $args]
 
52
    eval frame $path $maps(:cmd) -class ProgressBar -bd 0 \
 
53
            -highlightthickness 0 -relief flat
 
54
    Widget::initFromODB ProgressBar $path $maps(ProgressBar)
 
55
 
 
56
    set c  [eval [list canvas $path.bar] $maps(.bar) -highlightthickness 0]
 
57
    set fg [Widget::cget $path -foreground]
 
58
    if { [string equal [Widget::cget $path -orient] "horizontal"] } {
 
59
        $path.bar create rectangle -1 0 0 0 -fill $fg -outline $fg -tags rect
 
60
    } else {
 
61
        $path.bar create rectangle 0 1 0 0 -fill $fg -outline $fg -tags rect
 
62
    }
 
63
 
 
64
    set _widget($path,val) 0
 
65
    set _widget($path,dir) 1
 
66
    set _widget($path,var) [Widget::cget $path -variable]
 
67
    if {$_widget($path,var) != ""} {
 
68
        GlobalVar::tracevar variable $_widget($path,var) w \
 
69
                [list ProgressBar::_modify $path]
 
70
        set _widget($path,afterid) \
 
71
            [after idle [list ProgressBar::_modify $path]]
 
72
    }
 
73
 
 
74
    bind $path.bar <Destroy>   [list ProgressBar::_destroy $path]
 
75
    bind $path.bar <Configure> [list ProgressBar::_modify $path]
 
76
 
 
77
    return [Widget::create ProgressBar $path]
 
78
}
 
79
 
 
80
 
 
81
# ----------------------------------------------------------------------------
 
82
#  Command ProgressBar::configure
 
83
# ----------------------------------------------------------------------------
 
84
proc ProgressBar::configure { path args } {
 
85
    variable _widget
 
86
 
 
87
    set res [Widget::configure $path $args]
 
88
 
 
89
    if { [Widget::hasChangedX $path -variable] } {
 
90
        set newv [Widget::cget $path -variable]
 
91
        if { $_widget($path,var) != "" } {
 
92
            GlobalVar::tracevar vdelete $_widget($path,var) w \
 
93
                    [list ProgressBar::_modify $path]
 
94
        }
 
95
        if { $newv != "" } {
 
96
            set _widget($path,var) $newv
 
97
            GlobalVar::tracevar variable $newv w \
 
98
                    [list ProgressBar::_modify $path]
 
99
            if {![info exists _widget($path,afterid)]} {
 
100
                set _widget($path,afterid) \
 
101
                    [after idle [list ProgressBar::_modify $path]]
 
102
            }
 
103
        } else {
 
104
            set _widget($path,var) ""
 
105
        }
 
106
    }
 
107
 
 
108
    foreach {cbd cor cma} [Widget::hasChangedX $path -borderwidth \
 
109
            -orient -maximum] break
 
110
 
 
111
    if { $cbd || $cor || $cma } {
 
112
        if {![info exists _widget($path,afterid)]} {
 
113
            set _widget($path,afterid) \
 
114
                [after idle [list ProgressBar::_modify $path]]
 
115
        }
 
116
    }
 
117
    if { [Widget::hasChangedX $path -foreground] } {
 
118
        set fg [Widget::cget $path -foreground]
 
119
        $path.bar itemconfigure rect -fill $fg -outline $fg
 
120
    }
 
121
    return $res
 
122
}
 
123
 
 
124
 
 
125
# ----------------------------------------------------------------------------
 
126
#  Command ProgressBar::cget
 
127
# ----------------------------------------------------------------------------
 
128
proc ProgressBar::cget { path option } {
 
129
    return [Widget::cget $path $option]
 
130
}
 
131
 
 
132
 
 
133
# ----------------------------------------------------------------------------
 
134
#  Command ProgressBar::_modify
 
135
# ----------------------------------------------------------------------------
 
136
proc ProgressBar::_modify { path args } {
 
137
    variable _widget
 
138
 
 
139
    catch {unset _widget($path,afterid)}
 
140
    if { ![GlobalVar::exists $_widget($path,var)] ||
 
141
         [set val [GlobalVar::getvar $_widget($path,var)]] < 0 } {
 
142
        catch {place forget $path.bar}
 
143
    } else {
 
144
        place $path.bar -relx 0 -rely 0 -relwidth 1 -relheight 1
 
145
        set type [Widget::getoption $path -type]
 
146
        if { $val != 0 && $type != "normal" && \
 
147
                $type != "nonincremental_infinite"} {
 
148
            set val [expr {$val+$_widget($path,val)}]
 
149
        }
 
150
        set _widget($path,val) $val
 
151
        set max [Widget::getoption $path -maximum]
 
152
        set bd  [expr {2*[$path.bar cget -bd]}]
 
153
        set w   [winfo width  $path.bar]
 
154
        set h   [winfo height $path.bar]
 
155
        if {$type == "infinite" || $type == "nonincremental_infinite"} {
 
156
            # JDC: New infinite behaviour
 
157
            set tval [expr {$val % $max}]
 
158
            if { $tval < ($max / 2.0) } {
 
159
                set x0 [expr {double($tval) / double($max) * 1.5}]
 
160
            } else {
 
161
                set x0 [expr {(1.0-(double($tval) / double($max))) * 1.5}]
 
162
            }
 
163
            set x1 [expr {$x0 + 0.25}]
 
164
            if {[Widget::getoption $path -orient] == "horizontal"} {
 
165
                $path.bar coords rect [expr {$x0*$w}] 0 [expr {$x1*$w}] $h
 
166
            } else {
 
167
                $path.bar coords rect 0 [expr {$h-$x0*$h}] $w \
 
168
                        [expr {$x1*$h}]
 
169
            }
 
170
        } else {
 
171
            if { $val > $max } {set val $max}
 
172
            if {[Widget::getoption $path -orient] == "horizontal"} {
 
173
                $path.bar coords rect -1 0 [expr {$val*$w/$max}] $h
 
174
            } else {
 
175
                $path.bar coords rect 0 [expr {$h+1}] $w \
 
176
                        [expr {$h*(1.0 - double($val)/$max)}]
 
177
            }
 
178
        }
 
179
    }
 
180
    if {![Widget::cget $path -idle]} {
 
181
        update idletasks
 
182
    }
 
183
}
 
184
 
 
185
 
 
186
# ----------------------------------------------------------------------------
 
187
#  Command ProgressBar::_destroy
 
188
# ----------------------------------------------------------------------------
 
189
proc ProgressBar::_destroy { path } {
 
190
    variable _widget
 
191
 
 
192
    if {[info exists _widget($path,afterid)]} {
 
193
        after cancel $_widget($path,afterid)
 
194
        unset _widget($path,afterid)
 
195
    }
 
196
    if {[info exists _widget($path,var)]} {
 
197
        if {$_widget($path,var) != ""} {
 
198
            GlobalVar::tracevar vdelete $_widget($path,var) w \
 
199
                [list ProgressBar::_modify $path]
 
200
        }
 
201
        unset _widget($path,var)
 
202
    }
 
203
    unset _widget($path,dir)
 
204
    Widget::destroy $path
 
205
}