source: branches/blt4/gui/scripts/pushbutton.tcl @ 1923

Last change on this file since 1923 was 1923, checked in by gah, 14 years ago
File size: 5.8 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: PushButton - widget for entering a choice of strings
4#
5#  This widget represents a <choice> entry on a control panel.
6#  It is used to choose one of several mutually-exclusive strings.
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# ======================================================================
14package require Itk
15
16option add *PushButton.width 16 widgetDefault
17option add *PushButton.height 16 widgetDefault
18
19itcl::class Rappture::PushButton {
20    inherit itk::Widget
21
22    itk_option define -variable variable Variable "normal"
23    itk_option define -command command Command "normal"
24    itk_option define -width width Width "normal"
25    itk_option define -height height Height "normal"
26    itk_option define -onvalue onValue OnValue "normal"
27    itk_option define -offvalue offValue OffValue "normal"
28
29    constructor {args} {
30        # defined below
31    }
32    destructor {
33        # defined below
34    }
35
36    public method invoke {}
37    public method deselect {}
38    public method select {}
39    public method toggle {}
40    public method disable {}
41    public method enable {}
42
43    protected method _fixValue {args}
44
45    private variable _state 0
46    private variable _enabled 1
47    public variable command "";         # Command to be invoked
48    private variable _variable "";      # Variable to be set.
49    public variable onimage "";         # Image displayed when selected
50    public variable offimage "";        # Image displayed when deselected.
51    public variable disabledimage "";   # Image displayed when deselected.
52    public variable onvalue "1";        # Value set when selected.
53    public variable offvalue "0";       # Value set when deselected.
54}
55
56itk::usual PushButton {
57    keep -cursor -font
58    keep -foreground -background
59}
60
61# ----------------------------------------------------------------------
62# CONSTRUCTOR
63# ----------------------------------------------------------------------
64itcl::body Rappture::PushButton::constructor {args} {
65    itk_component add button {
66        label $itk_interior.button -borderwidth 1 -relief sunken
67    } {
68        usual
69        ignore -padx -pady -relief -borderwidth -background
70    }
71    bind $itk_component(button) <ButtonPress> \
72        [itcl::code $this invoke]
73    pack $itk_component(button) -expand yes -fill both -ipadx 1 -ipady 1
74    eval itk_initialize $args
75    deselect
76}
77
78itcl::body Rappture::PushButton::invoke {} {
79    if { !$_enabled } {
80        puts "in invoke button not enabled"
81        return
82    }
83    toggle
84    if { $command != "" } {
85        uplevel \#0 $command
86    }
87}
88
89itcl::body Rappture::PushButton::toggle {} {
90    if { !$_enabled } {
91        return
92    }
93    set _state [expr !$_state]
94    if { $_state } {
95        select
96    } else {
97        deselect
98    }
99}
100
101itcl::body Rappture::PushButton::disable {} {
102    if { $_enabled } {
103        set _enabled [expr !$_enabled]
104        $itk_component(button) configure -relief raise \
105            -image $disabledimage -bg grey85
106    }
107}
108
109itcl::body Rappture::PushButton::enable {} {
110    if { !$_enabled } {
111        set _enabled [expr !$_enabled]
112        _fixValue
113    }
114}
115
116# ----------------------------------------------------------------------
117# USAGE: _fixValue ?<name1> <name2> <op>?
118#
119# Invoked automatically whenever the -variable associated with this
120# widget is modified.  Copies the value to the current settings for
121# the widget.
122# ----------------------------------------------------------------------
123itcl::body Rappture::PushButton::_fixValue {args} {
124    if {"" == $itk_option(-variable)} {
125        return
126    }
127    upvar #0 $itk_option(-variable) var
128    if { $var != "" && [string is boolean $var] } {
129        set var [expr "$var == 1"]
130    }
131    if { $var == $onvalue } {
132        set _state 1
133        $itk_component(button) configure -relief sunken \
134            -image $onimage -bg white
135    } else {
136        set _state 0
137        $itk_component(button) configure -relief raise \
138            -image $offimage -bg grey85
139    }
140}
141
142itcl::body Rappture::PushButton::select {} {
143    if { !$_enabled } {
144        return
145    }
146    upvar #0 $_variable state
147    set state $onvalue
148}
149
150itcl::body Rappture::PushButton::deselect {} {
151    if { !$_enabled } {
152        return
153    }
154    upvar #0 $_variable state
155    set state $offvalue
156}
157
158
159# ----------------------------------------------------------------------
160# CONFIGURE: -variable
161# ----------------------------------------------------------------------
162itcl::configbody Rappture::PushButton::variable {
163    if {"" != $_variable} {
164        upvar #0 $_variable var
165        trace remove variable var write [itcl::code $this _fixValue]
166    }
167    set _variable $itk_option(-variable)
168
169    if {"" != $_variable} {
170        upvar #0 $_variable var
171        trace add variable var write [itcl::code $this _fixValue]
172
173        # sync to the current value of this variable
174        if {[info exists var]} {
175            _fixValue
176        }
177    }
178}
179
180# ----------------------------------------------------------------------
181# CONFIGURE: -width
182# ----------------------------------------------------------------------
183itcl::configbody Rappture::PushButton::width {
184    $itk_component(button) configure -width $itk_option(-width)
185}
186
187# ----------------------------------------------------------------------
188# CONFIGURE: -height
189# ----------------------------------------------------------------------
190itcl::configbody Rappture::PushButton::height {
191    set _height $itk_option(-height)
192    $itk_component(button) configure -height $itk_option(-height)
193}
194
Note: See TracBrowser for help on using the repository browser.