source: branches/blt4/gui/scripts/icons.tcl @ 1649

Last change on this file since 1649 was 1649, checked in by gah, 15 years ago
File size: 3.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: icons - utility for loading icons from a library
3#
4#  This utility makes it easy to load GIF and XBM files installed
5#  in a library in the final installation.  It is used throughout
6#  the Rappture GUI, whenever an icon is needed.
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# ======================================================================
14
15namespace eval Rappture::icon {
16    variable iconpath [file join $RapptureGUI::library scripts images]
17    variable icons
18}
19
20# ----------------------------------------------------------------------
21# USAGE: Rappture::icon <name>
22#
23# Searches for an icon called <name> on all of the directories in
24# the search path set up by RapptureGUI::iconpath.
25# ----------------------------------------------------------------------
26proc Rappture::icon {name} {
27    variable ::Rappture::icon::iconpath
28    variable ::Rappture::icon::icons
29
30    #
31    # Already loaded? then return it directly
32    #
33    if {[info exists icons($name)]} {
34        return $icons($name)
35    }
36
37    #
38    # Search for the icon along the iconpath search path
39    #
40    set file ""
41    foreach dir $iconpath {
42        set path [file join $dir $name.*]
43        set file [lindex [glob -nocomplain $path] 0]
44        if {"" != $file} {
45            break
46        }
47    }
48
49    set imh ""
50    if {"" != $file} {
51        switch -- [file extension $file] {
52            .gif - .jpg - .png - .xpm - .tif {
53                set imh [image create picture -file $file]
54            }
55            .xbm {
56                set fid [open $file r]
57                set data [read $fid]
58                close $fid
59                set imh bitmap-$name
60                blt::bitmap define $imh $data
61            }
62        }
63    }
64    if {"" != $imh} {
65        set icons($name) $imh
66    }
67    return $imh
68}
69
70# ----------------------------------------------------------------------
71# USAGE: Rappture::icon::data <image> ?<format>?
72#
73# Returns the bytes the represent an <image> in the requested
74# <format>, which can get "gif" or "jpeg".
75# ----------------------------------------------------------------------
76proc Rappture::icon::data {image {format "gif"}} {
77    switch -- $format {
78        "gif" {
79            $image export gif -data bytes
80        }
81        "jpeg" - "jpg" {
82            $image export jpg -data bytes -quality 100
83        }
84        default {
85            return ""
86        }
87    }       
88    return $bytes
89}
90
91# ----------------------------------------------------------------------
92# USAGE: Rappture::icon::gif_animate <delay> ?<image> <image>...?
93#
94# Takes a series if <images> and composited them into a single,
95# animated GIF image, with the <delay> in milliseconds between
96# frames.  Returns binary data in GIF89a format.
97# ----------------------------------------------------------------------
98proc Rappture::icon::gif_animate {delay args} {
99    if {[llength $args] < 1} {
100        error "must have at least one image for animation"
101    }
102    set delay [expr {round($delay*0.01)}] ;# convert to 1/100s of second
103    set img [image create picture]
104    $img copy [lindex $args 0]
105    eval $img append [lrange $args 1 end]
106    $img export gif -animated -data bytes -delay $delay
107    image delete $img
108    return $bytes
109}
110
Note: See TracBrowser for help on using the repository browser.