source: trunk/gui/scripts/animicon.tcl @ 2136

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