source: trunk/gui/scripts/imageentry.tcl @ 428

Last change on this file since 428 was 428, checked in by mmc, 18 years ago
  • Added <sequence> for playing movie outputs and other sequences of related results.
  • Added <resize> option to <image> elements. This can be used to resize input items to a smaller size, so they don't take up so much real estate on the form.
  • Fixed a bug in right/below cases for popup balloons.
  • Reduced the tooltip delay time to 750ms to interact better with Rick's attention span.
  • Fixed the sash between grips to light up when you touch it, so it's easier to see.
File size: 7.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: ImageEntry - widget for displaying images
3#
4#  This widget represents an <image> entry on a control panel.
5#  It displays images which help explain the input.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  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
14package require Img
15
16itcl::class Rappture::ImageEntry {
17    inherit itk::Widget
18
19    constructor {owner path args} { # defined below }
20    destructor { # defined below }
21
22    public method value {args}
23
24    public method label {}
25    public method tooltip {}
26
27    private method _redraw {}
28
29    private variable _owner ""    ;# thing managing this control
30    private variable _path ""     ;# path in XML to this image
31    private variable _imh ""      ;# image handle for current value
32    private variable _resize ""   ;# image for resize operations
33}
34
35itk::usual ImageEntry {
36    keep -cursor -font
37    keep -foreground -background
38    keep -textbackground
39    keep -selectbackground -selectforeground -selectborderwidth
40}
41
42# ----------------------------------------------------------------------
43# CONSTRUCTOR
44# ----------------------------------------------------------------------
45itcl::body Rappture::ImageEntry::constructor {owner path args} {
46    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
47        error "bad object \"$owner\": should be Rappture::ControlOwner"
48    }
49    set _owner $owner
50    set _path $path
51
52    #
53    # Create the widget and configure it properly based on other
54    # hints in the XML.
55    #
56    itk_component add image {
57        ::label $itk_interior.image -borderwidth 0
58    }
59    pack $itk_component(image) -expand yes -fill both
60    bind $itk_component(image) <Configure> [itcl::code $this _redraw]
61
62    set str [$_owner xml get $path.current]
63    if {[string length $str] == 0} {
64        set str [$_owner xml get $path.default]
65    }
66    if {[string length $str] > 0} {
67        value $str
68    }
69
70    eval itk_initialize $args
71}
72
73# ----------------------------------------------------------------------
74# DESTRUCTOR
75# ----------------------------------------------------------------------
76itcl::body Rappture::ImageEntry::destructor {} {
77    if {"" != $_imh} { image delete $_imh }
78    if {"" != $_resize} { image delete $_resize }
79}
80
81# ----------------------------------------------------------------------
82# USAGE: value ?-check? ?<newval>?
83#
84# Clients use this to query/set the value for this widget.  With
85# no args, it returns the current value for the widget.  If the
86# <newval> is specified, it sets the value of the widget and
87# sends a <<Value>> event.  If the -check flag is included, the
88# new value is not actually applied, but just checked for correctness.
89# ----------------------------------------------------------------------
90itcl::body Rappture::ImageEntry::value {args} {
91    set onlycheck 0
92    set i [lsearch -exact $args -check]
93    if {$i >= 0} {
94        set onlycheck 1
95        set args [lreplace $args $i $i]
96    }
97
98    if {[llength $args] == 1} {
99        if {$onlycheck} {
100            # someday we may add validation...
101            return
102        }
103        set newval [lindex $args 0]
104        if {[string length $newval] > 0} {
105            set imh [image create photo -data $newval]
106        } else {
107            set imh ""
108        }
109
110        if {$_imh != ""} {
111            image delete $_imh
112        }
113        set _imh $imh
114        _redraw
115        return $newval
116
117    } elseif {[llength $args] != 0} {
118        error "wrong # args: should be \"value ?-check? ?newval?\""
119    }
120
121    #
122    # Query the value and return.
123    #
124    set data ""
125    if {"" != $_imh} { set data [$_imh cget -data] }
126    return $data
127}
128
129# ----------------------------------------------------------------------
130# USAGE: label
131#
132# Clients use this to query the label associated with this widget.
133# Reaches into the XML and pulls out the appropriate label string.
134# ----------------------------------------------------------------------
135itcl::body Rappture::ImageEntry::label {} {
136    set label [$_owner xml get $_path.about.label]
137    return [string trim $label]
138}
139
140# ----------------------------------------------------------------------
141# USAGE: tooltip
142#
143# Clients use this to query the tooltip associated with this widget.
144# Reaches into the XML and pulls out the appropriate description
145# string.  Returns the string that should be used with the
146# Rappture::Tooltip facility.
147# ----------------------------------------------------------------------
148itcl::body Rappture::ImageEntry::tooltip {} {
149    set str [$_owner xml get $_path.about.description]
150    return [string trim $str]
151}
152
153# ----------------------------------------------------------------------
154# USAGE: _redraw
155#
156# Used internally to update the image displayed in this entry.
157# If the <resize> parameter is set, then the image is resized.
158# The "auto" value resizes to the current area.  The "width=XX" or
159# "height=xx" form resizes to a particular size.  The "none" value
160# leaves the image as-is; this is the default.
161# ----------------------------------------------------------------------
162itcl::body Rappture::ImageEntry::_redraw {} {
163    if {"" == $_imh} {
164        $itk_component(image) configure -image ""
165        return
166    }
167
168    set iw [image width $_imh]
169    set ih [image height $_imh]
170    $itk_component(image) configure -image "" -width $iw -height $ih
171
172    set str [string trim [$_owner xml get $_path.resize]]
173    if {"" == $str} {
174        set str "none"
175    }
176    switch -glob -- $str {
177        auto {
178            if {$_resize == ""} {
179                set _resize [image create photo]
180            }
181            set w [winfo width $itk_component(image)]
182            set h [winfo height $itk_component(image)]
183            if {$w/double($iw) < $h/double($ih)} {
184                set h [expr {round($w/double($iw)*$ih)}]
185            } else {
186                set w [expr {round($h/double($ih)*$iw)}]
187            }
188            $_resize configure -width $w -height $h
189            blt::winop resample $_imh $_resize
190            $itk_component(image) configure -image $_resize
191        }
192        width=* - height=* {
193            if {$_resize == ""} {
194                set _resize [image create photo]
195            }
196            if {[regexp {^width=([0-9]+)$} $str match size]} {
197                set w $size
198                set h [expr {round($w*$ih/double($iw))}]
199                $_resize configure -width $w -height $h
200                blt::winop resample $_imh $_resize
201                $itk_component(image) configure -image $_resize \
202                    -width $w -height $h
203            } elseif {[regexp {^height=([0-9]+)$} $str match size]} {
204                set h $size
205                set w [expr {round($h*$iw/double($ih))}]
206                $_resize configure -width $w -height $h
207                blt::winop resample $_imh $_resize
208                $itk_component(image) configure -image $_resize \
209                    -width $w -height $h
210            } else {
211                $itk_component(image) configure -image $_imh
212            }
213        }
214        default {
215            $itk_component(image) configure -image $_imh
216        }
217    }
218}
Note: See TracBrowser for help on using the repository browser.