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

Last change on this file since 1695 was 1670, checked in by gah, 14 years ago
File size: 5.2 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
41    protected method _fixValue {args}
42
43    private variable _state 0
44    public variable command "";         # Command to be invoked
45    private variable _variable "";      # Variable to be set.
46    public variable onimage "";         # Image displayed when selected
47    public variable offimage "";        # Image displayed when deselected.
48    public variable onvalue "1";        # Value set when selected.
49    public variable offvalue "0";       # Value set when deselected.
50}
51
52itk::usual PushButton {
53    keep -cursor -font
54    keep -foreground -background
55}
56
57# ----------------------------------------------------------------------
58# CONSTRUCTOR
59# ----------------------------------------------------------------------
60itcl::body Rappture::PushButton::constructor {args} {
61    itk_component add button {
62        label $itk_interior.button -borderwidth 1 -relief sunken
63    } {
64        usual
65        ignore -padx -pady -relief -borderwidth -background
66    }
67    bind $itk_component(button) <ButtonPress> \
68        [itcl::code $this invoke]
69    pack $itk_component(button) -expand yes -fill both -ipadx 1 -ipady 1
70    eval itk_initialize $args
71    deselect
72}
73
74# ----------------------------------------------------------------------
75# CONSTRUCTOR
76# ----------------------------------------------------------------------
77itcl::body Rappture::PushButton::destructor {} {
78   if {"" != $_variable} {
79        upvar #0 $_variable var
80        trace remove variable var write [itcl::code $this _fixValue]
81    }
82}
83
84itcl::body Rappture::PushButton::invoke {} {
85    toggle
86    if { $command != "" } {
87        uplevel \#0 $command
88    }
89}
90
91itcl::body Rappture::PushButton::toggle {} {
92    set _state [expr !$_state]
93    if { $_state } {
94        select
95    } else {
96        deselect
97    }
98}
99
100# ----------------------------------------------------------------------
101# USAGE: _fixValue ?<name1> <name2> <op>?
102#
103# Invoked automatically whenever the -variable associated with this
104# widget is modified.  Copies the value to the current settings for
105# the widget.
106# ----------------------------------------------------------------------
107itcl::body Rappture::PushButton::_fixValue {args} {
108    if {"" == $itk_option(-variable)} {
109        return
110    }
111    upvar #0 $itk_option(-variable) var
112    if { $var != "" && [string is boolean $var] } {
113        set var [expr "$var == 1"]
114    }
115    if { $var == $onvalue } {
116        set _state 1
117        $itk_component(button) configure -relief sunken \
118            -image $onimage -bg white
119    } else {
120        set _state 0
121        $itk_component(button) configure -relief raise \
122            -image $offimage -bg grey85
123    }
124}
125
126itcl::body Rappture::PushButton::select {} {
127    upvar #0 $_variable state
128    set state $onvalue
129}
130
131itcl::body Rappture::PushButton::deselect {} {
132    upvar #0 $_variable state
133    set state $offvalue
134}
135
136
137# ----------------------------------------------------------------------
138# CONFIGURE: -variable
139# ----------------------------------------------------------------------
140itcl::configbody Rappture::PushButton::variable {
141    if {"" != $_variable} {
142        upvar #0 $_variable var
143        trace remove variable var write [itcl::code $this _fixValue]
144    }
145    set _variable $itk_option(-variable)
146
147    if {"" != $_variable} {
148        upvar #0 $_variable var
149        trace add variable var write [itcl::code $this _fixValue]
150
151        # sync to the current value of this variable
152        if {[info exists var]} {
153            _fixValue
154        }
155    }
156}
157
158# ----------------------------------------------------------------------
159# CONFIGURE: -width
160# ----------------------------------------------------------------------
161itcl::configbody Rappture::PushButton::width {
162    $itk_component(button) configure -width $itk_option(-width)
163}
164
165# ----------------------------------------------------------------------
166# CONFIGURE: -height
167# ----------------------------------------------------------------------
168itcl::configbody Rappture::PushButton::height {
169    set _height $itk_option(-height)
170    $itk_component(button) configure -height $itk_option(-height)
171}
172
Note: See TracBrowser for help on using the repository browser.