source: trunk/gui/scripts/flowspeed.tcl @ 5520

Last change on this file since 5520 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: 6.0 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 *Flowspeed.width 5 widgetDefault
19option add *Flowspeed.textBackground white widgetDefault
20
21blt::bitmap define Flowspeed-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 Flowspeed-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::Flowspeed {
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    protected variable _value ""
48}
49
50itk::usual Flowspeed {
51    keep -cursor -font
52    keep -foreground -background
53    keep -textforeground -textbackground
54    keep -selectbackground -selectforeground -selectborderwidth
55}
56
57# ----------------------------------------------------------------------
58# CONSTRUCTOR
59# ----------------------------------------------------------------------
60itcl::body Rappture::Flowspeed::constructor {args} {
61    itk_component add entry {
62        entry $itk_interior.entry
63    } {
64        usual
65        keep -width
66        rename -background -textbackground textBackground Background
67        rename -foreground -textforeground textForeground Foreground
68        rename -highlightbackground -background background Background
69    }
70    pack $itk_component(entry) -side left -expand yes -fill x
71
72    bind $itk_component(entry) <KeyPress> \
73        [itcl::code $this _validate %A]
74    bind $itk_component(entry) <Return> \
75        "$this value \[$itk_component(entry) get\]"
76    bind $itk_component(entry) <KP_Enter> \
77        "$this value \[$itk_component(entry) get\]"
78    bind $itk_component(entry) <KeyPress-Tab> \
79        "$this value \[$itk_component(entry) get\]"
80
81    itk_component add controls {
82        frame $itk_interior.cntls
83    }
84    pack $itk_component(controls) -side right
85
86    itk_component add up {
87        button $itk_component(controls).spinup -bitmap Flowspeed-up \
88            -borderwidth 1 -relief raised -highlightthickness 0 \
89            -command [itcl::code $this bump up]
90    } {
91        usual
92        ignore -borderwidth -highlightthickness
93    }
94    pack $itk_component(up) -side top -expand yes -fill both
95
96    itk_component add down {
97        button $itk_component(controls).spindn -bitmap Flowspeed-down \
98            -borderwidth 1 -relief raised -highlightthickness 0 \
99            -command [itcl::code $this bump down]
100    } {
101        usual
102        ignore -borderwidth -highlightthickness
103    }
104    pack $itk_component(down) -side bottom -expand yes -fill both
105
106    eval itk_initialize $args
107}
108
109# ----------------------------------------------------------------------
110# USAGE: value ?<newval>?
111#
112# Clients use this to query/set the value for this widget.  With
113# no args, it returns the current value for the widget.  If the
114# <newval> is specified, it sets the value of the widget and
115# sends a <<Value>> event.
116# ----------------------------------------------------------------------
117itcl::body Rappture::Flowspeed::value {args} {
118    if {[llength $args] == 1} {
119        set string [lindex $args 0]
120        if { [regexp {^ *([0-9]+)x *$} $string match newval] } {
121        } elseif { [regexp {^ *([0-9]+) *$} $string match newval] } {
122        } else {
123            bell
124            return
125        }
126        if {"" != $newval} {
127            if {"" != $itk_option(-min) && $newval < $itk_option(-min)} {
128                set newval $itk_option(-min)
129            }
130            if {"" != $itk_option(-max) && $newval > $itk_option(-max)} {
131                set newval $itk_option(-max)
132            }
133        }
134        set _value $newval
135        $itk_component(entry) delete 0 end
136        $itk_component(entry) insert 0 ${newval}x
137        after 10 \
138            [list catch [list event generate $itk_component(hull) <<Value>>]]
139    } elseif {[llength $args] != 0} {
140        error "wrong # args: should be \"value ?newval?\""
141    }
142    return $_value
143}
144
145# ----------------------------------------------------------------------
146# USAGE: bump ?<delta>?
147#
148# Used internally when you click on the up/down arrows.  Clients
149# can also use it directly to bump values up/down.  The optional
150# <delta> can be an integer value or the keyword "up" or "down".
151# ----------------------------------------------------------------------
152itcl::body Rappture::Flowspeed::bump {{delta up}} {
153    if {"up" == $delta} {
154        set delta $itk_option(-delta)
155    } elseif {"down" == $delta} {
156        set delta [expr {-$itk_option(-delta)}]
157    } elseif {![string is integer $delta]} {
158        error "bad delta \"$delta\": should be up, down, or integer"
159    }
160
161    set val [$itk_component(entry) get]
162    if {$val == ""} {
163        set val 0
164    }
165    value [expr {$_value+$delta}]
166}
167
168# ----------------------------------------------------------------------
169# USAGE: _validate <char>
170#
171# Validates each character as it is typed into the spinner.
172# If the <char> is not a digit, then this procedure beeps and
173# prevents the character from being inserted.
174# ----------------------------------------------------------------------
175itcl::body Rappture::Flowspeed::_validate {char} {
176    if {[string match "\[ -~\]" $char]} {
177        if {![string match "\[0-9\]" $char]} {
178            bell
179            return -code break
180        }
181    }
182}
Note: See TracBrowser for help on using the repository browser.