source: branches/1.6/gui/scripts/pushbutton.tcl @ 6363

Last change on this file since 6363 was 5679, checked in by ldelgass, 9 years ago

Full merge 1.3 branch to uq branch to sync. Fixed partial subdirectory merge
by removing mergeinfo from lang/python/Rappture directory.

File size: 6.0 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
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-2012  HUBzero Foundation, LLC
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    itk_option define -onbackground onBackground OnBackground "white"
29    itk_option define -offbackground offBackground OffBackground "grey85"
30
31    constructor {args} {
32        # defined below
33    }
34    destructor {
35        # defined below
36    }
37
38    public method invoke {}
39    public method deselect {}
40    public method select {}
41    public method toggle {}
42    public method disable {}
43    public method enable {}
44
45    protected method _fixValue {args}
46
47    private variable _state 0
48    private variable _enabled 1
49    public variable command "";         # Command to be invoked
50    private variable _variable "";      # Variable to be set.
51    public variable onimage "";         # Image displayed when selected
52    public variable offimage "";        # Image displayed when deselected.
53    public variable disabledimage "";   # Image displayed when deselected.
54    public variable onvalue "1";        # Value set when selected.
55    public variable offvalue "0";       # Value set when deselected.
56    public variable onbackground "white"
57    public variable offbackground "grey85"
58}
59
60itk::usual PushButton {
61    keep -cursor -font
62    keep -foreground -background
63}
64
65# ----------------------------------------------------------------------
66# CONSTRUCTOR
67# ----------------------------------------------------------------------
68itcl::body Rappture::PushButton::constructor {args} {
69    itk_component add button {
70        label $itk_interior.button -borderwidth 1 -relief sunken
71    } {
72        usual
73        ignore -padx -pady -relief -borderwidth -background
74    }
75    bind $itk_component(button) <ButtonPress> \
76        [itcl::code $this invoke]
77    pack $itk_component(button) -expand yes -fill both -ipadx 1 -ipady 1
78    eval itk_initialize $args
79    deselect
80}
81
82itcl::body Rappture::PushButton::invoke {} {
83    if { !$_enabled } {
84        return
85    }
86    toggle
87    if { $command != "" } {
88        uplevel \#0 $command
89    }
90}
91
92itcl::body Rappture::PushButton::toggle {} {
93    if { !$_enabled } {
94        return
95    }
96    set _state [expr !$_state]
97    if { $_state } {
98        select
99    } else {
100        deselect
101    }
102}
103
104itcl::body Rappture::PushButton::disable {} {
105    if { $_enabled } {
106        set _enabled [expr !$_enabled]
107        $itk_component(button) configure -relief raise \
108            -image $disabledimage -bg grey85
109    }
110}
111
112itcl::body Rappture::PushButton::enable {} {
113    if { !$_enabled } {
114        set _enabled [expr !$_enabled]
115        _fixValue
116    }
117}
118
119# ----------------------------------------------------------------------
120# USAGE: _fixValue ?<name1> <name2> <op>?
121#
122# Invoked automatically whenever the -variable associated with this
123# widget is modified.  Copies the value to the current settings for
124# the widget.
125# ----------------------------------------------------------------------
126itcl::body Rappture::PushButton::_fixValue {args} {
127    if {"" == $itk_option(-variable)} {
128        return
129    }
130    upvar #0 $itk_option(-variable) var
131    if { $var != "" && [string is boolean $var] } {
132        set var [expr "$var == 1"]
133    }
134    if { $var == $onvalue } {
135        set _state 1
136        $itk_component(button) configure -relief sunken \
137            -image $onimage -bg $onbackground
138    } else {
139        set _state 0
140        $itk_component(button) configure -relief raise \
141            -image $offimage -bg $offbackground
142    }
143}
144
145itcl::body Rappture::PushButton::select {} {
146    if { !$_enabled } {
147        return
148    }
149    upvar #0 $_variable state
150    set state $onvalue
151}
152
153itcl::body Rappture::PushButton::deselect {} {
154    if { !$_enabled } {
155        return
156    }
157    upvar #0 $_variable state
158    set state $offvalue
159}
160
161
162# ----------------------------------------------------------------------
163# CONFIGURE: -variable
164# ----------------------------------------------------------------------
165itcl::configbody Rappture::PushButton::variable {
166    if {"" != $_variable} {
167        upvar #0 $_variable var
168        trace remove variable var write [itcl::code $this _fixValue]
169    }
170    set _variable $itk_option(-variable)
171
172    if {"" != $_variable} {
173        upvar #0 $_variable var
174        trace add variable var write [itcl::code $this _fixValue]
175
176        # sync to the current value of this variable
177        if {[info exists var]} {
178            _fixValue
179        }
180    }
181}
182
183# ----------------------------------------------------------------------
184# CONFIGURE: -width
185# ----------------------------------------------------------------------
186itcl::configbody Rappture::PushButton::width {
187    $itk_component(button) configure -width $itk_option(-width)
188}
189
190# ----------------------------------------------------------------------
191# CONFIGURE: -height
192# ----------------------------------------------------------------------
193itcl::configbody Rappture::PushButton::height {
194    set _height $itk_option(-height)
195    $itk_component(button) configure -height $itk_option(-height)
196}
197
Note: See TracBrowser for help on using the repository browser.