source: trunk/gui/scripts/spinint.tcl @ 3508

Last change on this file since 3508 was 3454, checked in by gah, 12 years ago

add KP_Enter to Return bindings. Implement OK handler in visviewer base class. Fix setting # of isolines while running a sequence of heightmaps/contours

File size: 5.6 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2
3# ----------------------------------------------------------------------
4#  COMPONENT: spinint - spinner for integer values
5#
6#  This widget is a spinner with up/down arrows for managing integer
7#  values.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16package require BLT
17
18option add *Spinint.width 5 widgetDefault
19option add *Spinint.textBackground white widgetDefault
20
21blt::bitmap define Spinint-up {
22#define up_width 8
23#define up_height 4
24static unsigned char up_bits[] = {
25   0x10, 0x38, 0x7c, 0xfe};
26}
27
28blt::bitmap define Spinint-down {
29#define arrow_width 8
30#define arrow_height 4
31static unsigned char arrow_bits[] = {
32   0xfe, 0x7c, 0x38, 0x10};
33}
34
35itcl::class Rappture::Spinint {
36    inherit itk::Widget
37
38    itk_option define -min min Min ""
39    itk_option define -max max Max ""
40    itk_option define -delta delta Delta 1
41
42    constructor {args} { # defined below }
43
44    public method value {args}
45    public method bump {{delta up}}
46    protected method _validate {char}
47}
48
49itk::usual Spinint {
50    keep -cursor -font
51    keep -foreground -background
52    keep -textforeground -textbackground
53    keep -selectbackground -selectforeground -selectborderwidth
54}
55
56# ----------------------------------------------------------------------
57# CONSTRUCTOR
58# ----------------------------------------------------------------------
59itcl::body Rappture::Spinint::constructor {args} {
60    itk_component add entry {
61        entry $itk_interior.entry
62    } {
63        usual
64        keep -width
65        rename -background -textbackground textBackground Background
66        rename -foreground -textforeground textForeground Foreground
67        rename -highlightbackground -background background Background
68    }
69    pack $itk_component(entry) -side left -expand yes -fill x
70
71    bind $itk_component(entry) <KeyPress> \
72        [itcl::code $this _validate %A]
73    bind $itk_component(entry) <Return> \
74        "$this value \[$itk_component(entry) get\]"
75    bind $itk_component(entry) <KP_Enter> \
76        "$this value \[$itk_component(entry) get\]"
77
78    itk_component add controls {
79        frame $itk_interior.cntls
80    }
81    pack $itk_component(controls) -side right
82
83    itk_component add up {
84        button $itk_component(controls).spinup -bitmap Spinint-up \
85            -borderwidth 1 -relief raised -highlightthickness 0 \
86            -command [itcl::code $this bump up]
87    } {
88        usual
89        ignore -borderwidth -highlightthickness
90    }
91    pack $itk_component(up) -side top -expand yes -fill both
92
93    itk_component add down {
94        button $itk_component(controls).spindn -bitmap Spinint-down \
95            -borderwidth 1 -relief raised -highlightthickness 0 \
96            -command [itcl::code $this bump down]
97    } {
98        usual
99        ignore -borderwidth -highlightthickness
100    }
101    pack $itk_component(down) -side bottom -expand yes -fill both
102
103    eval itk_initialize $args
104}
105
106# ----------------------------------------------------------------------
107# USAGE: value ?<newval>?
108#
109# Clients use this to query/set the value for this widget.  With
110# no args, it returns the current value for the widget.  If the
111# <newval> is specified, it sets the value of the widget and
112# sends a <<Value>> event.
113# ----------------------------------------------------------------------
114itcl::body Rappture::Spinint::value {args} {
115    if {[llength $args] == 1} {
116        set newval [lindex $args 0]
117
118        if {"" != $newval} {
119            if {"" != $itk_option(-min) && $newval < $itk_option(-min)} {
120                set newval $itk_option(-min)
121            }
122            if {"" != $itk_option(-max) && $newval > $itk_option(-max)} {
123                set newval $itk_option(-max)
124            }
125        }
126
127        $itk_component(entry) delete 0 end
128        $itk_component(entry) insert 0 $newval
129        after 10 [list catch [list event generate $itk_component(hull) <<Value>>]]
130    } elseif {[llength $args] != 0} {
131        error "wrong # args: should be \"value ?newval?\""
132    }
133    return [$itk_component(entry) get]
134}
135
136# ----------------------------------------------------------------------
137# USAGE: bump ?<delta>?
138#
139# Used internally when you click on the up/down arrows.  Clients
140# can also use it directly to bump values up/down.  The optional
141# <delta> can be an integer value or the keyword "up" or "down".
142# ----------------------------------------------------------------------
143itcl::body Rappture::Spinint::bump {{delta up}} {
144    if {"up" == $delta} {
145        set delta $itk_option(-delta)
146    } elseif {"down" == $delta} {
147        set delta [expr {-$itk_option(-delta)}]
148    } elseif {![string is integer $delta]} {
149        error "bad delta \"$delta\": should be up, down, or integer"
150    }
151
152    set val [$itk_component(entry) get]
153    if {$val == ""} {
154        set val 0
155    }
156    value [expr {$val+$delta}]
157}
158
159# ----------------------------------------------------------------------
160# USAGE: _validate <char>
161#
162# Validates each character as it is typed into the spinner.
163# If the <char> is not a digit, then this procedure beeps and
164# prevents the character from being inserted.
165# ----------------------------------------------------------------------
166itcl::body Rappture::Spinint::_validate {char} {
167    if {[string match "\[ -~\]" $char]} {
168        if {![string match "\[0-9\]" $char]} {
169            bell
170            return -code break
171        }
172    }
173}
Note: See TracBrowser for help on using the repository browser.