1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: animicon - an animated icon |
---|
4 | # |
---|
5 | # This widget displays an animated icon. It acts like a label, but |
---|
6 | # it allows you to start/stop the animation. |
---|
7 | # ====================================================================== |
---|
8 | # AUTHOR: Michael McLennan, Purdue University |
---|
9 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
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 | # ====================================================================== |
---|
14 | package require Itk |
---|
15 | |
---|
16 | option add *Animicon.delay 100 widgetDefault |
---|
17 | option add *Animicon.textBackground white widgetDefault |
---|
18 | |
---|
19 | itcl::class Rappture::Animicon { |
---|
20 | inherit itk::Widget |
---|
21 | |
---|
22 | itk_option define -delay delay Delay 100 |
---|
23 | itk_option define -images images Images "" |
---|
24 | |
---|
25 | constructor {args} { # defined below } |
---|
26 | |
---|
27 | public method start {} |
---|
28 | public method stop {} |
---|
29 | public method isrunning {} |
---|
30 | |
---|
31 | protected method _next {} |
---|
32 | protected method _loadFrames {} |
---|
33 | |
---|
34 | private variable _frames ;# array of frames indexed from 0 |
---|
35 | private variable _pos 0 ;# current position in anim sequence |
---|
36 | private variable _afterid "" ;# after ID for next animation step |
---|
37 | } |
---|
38 | |
---|
39 | itk::usual Animicon { |
---|
40 | keep -cursor -background |
---|
41 | } |
---|
42 | |
---|
43 | # ---------------------------------------------------------------------- |
---|
44 | # CONSTRUCTOR |
---|
45 | # ---------------------------------------------------------------------- |
---|
46 | itcl::body Rappture::Animicon::constructor {args} { |
---|
47 | itk_component add icon { |
---|
48 | label $itk_interior.icon |
---|
49 | } |
---|
50 | pack $itk_component(icon) -expand yes -fill both |
---|
51 | |
---|
52 | eval itk_initialize $args |
---|
53 | } |
---|
54 | |
---|
55 | # ---------------------------------------------------------------------- |
---|
56 | # USAGE: start |
---|
57 | # |
---|
58 | # Clients use this to start the animation. If the animation is |
---|
59 | # already running, it does nothing. Otherwise, it starts the |
---|
60 | # animation. |
---|
61 | # ---------------------------------------------------------------------- |
---|
62 | itcl::body Rappture::Animicon::start {} { |
---|
63 | if {![isrunning]} { |
---|
64 | _next |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | # ---------------------------------------------------------------------- |
---|
69 | # USAGE: stop |
---|
70 | # |
---|
71 | # Clients use this to stop the animation. If the animation is |
---|
72 | # not running, it does nothing. Otherwise, it stops the |
---|
73 | # animation on the current frame. |
---|
74 | # ---------------------------------------------------------------------- |
---|
75 | itcl::body Rappture::Animicon::stop {} { |
---|
76 | if {[isrunning]} { |
---|
77 | after cancel $_afterid |
---|
78 | set _afterid "" |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | # ---------------------------------------------------------------------- |
---|
83 | # USAGE: isrunning |
---|
84 | # |
---|
85 | # Returns true if the animation is currently running, and false |
---|
86 | # otherwise. |
---|
87 | # ---------------------------------------------------------------------- |
---|
88 | itcl::body Rappture::Animicon::isrunning {} { |
---|
89 | return [expr {"" != $_afterid}] |
---|
90 | } |
---|
91 | |
---|
92 | # ---------------------------------------------------------------------- |
---|
93 | # USAGE: _next |
---|
94 | # |
---|
95 | # Used internally to load the next animation frame. |
---|
96 | # ---------------------------------------------------------------------- |
---|
97 | itcl::body Rappture::Animicon::_next {} { |
---|
98 | $itk_component(icon) configure -image $_frames($_pos) |
---|
99 | if {[incr _pos] >= [array size _frames]} { |
---|
100 | set _pos 0 |
---|
101 | } |
---|
102 | set _afterid [after $itk_option(-delay) [itcl::code $this _next]] |
---|
103 | } |
---|
104 | |
---|
105 | # ---------------------------------------------------------------------- |
---|
106 | # OPTION: -images |
---|
107 | # |
---|
108 | # Sets a sequence of icon names that will be loaded for an animation. |
---|
109 | # ---------------------------------------------------------------------- |
---|
110 | itcl::configbody Rappture::Animicon::images { |
---|
111 | set restart [isrunning] |
---|
112 | stop |
---|
113 | |
---|
114 | array unset _frames |
---|
115 | if {[llength $itk_option(-images)] >= 1} { |
---|
116 | set w 0 |
---|
117 | set h 0 |
---|
118 | set i 0 |
---|
119 | foreach name $itk_option(-images) { |
---|
120 | set imh [Rappture::icon $name] |
---|
121 | if {"" == $imh} { |
---|
122 | error "image not found: $name" |
---|
123 | } |
---|
124 | set _frames($i) $imh |
---|
125 | if {[image width $imh] > $w} { set w [image width $imh] } |
---|
126 | if {[image height $imh] > $h} { set h [image height $imh] } |
---|
127 | incr i |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | $itk_component(icon) configure -width $w -height $h -image $_frames(0) |
---|
132 | set _pos 0 |
---|
133 | |
---|
134 | if {$restart} { |
---|
135 | start |
---|
136 | } |
---|
137 | } |
---|