source: branches/blt4/gui/scripts/videospeed.tcl @ 2297

Last change on this file since 2297 was 2174, checked in by gah, 14 years ago
File size: 6.0 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: spinint - spinner for integer values
4#
5#  This widget is a spinner with up/down arrows for managing integer
6#  values.
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
15package require BLT
16
17option add *Videospeed.width 5 widgetDefault
18option add *Videospeed.textBackground white widgetDefault
19
20blt::bitmap define Videospeed-up {
21#define up_width 8
22#define up_height 4
23static unsigned char up_bits[] = {
24   0x10, 0x38, 0x7c, 0xfe};
25}
26
27blt::bitmap define Videospeed-down {
28#define arrow_width 8
29#define arrow_height 4
30static unsigned char arrow_bits[] = {
31   0xfe, 0x7c, 0x38, 0x10};
32}
33
34itcl::class Rappture::Videospeed {
35    inherit itk::Widget
36
37    itk_option define -min min Min ""
38    itk_option define -max max Max ""
39    itk_option define -factor factor Factor 1
40
41    constructor {args} { # defined below }
42
43    public method value {args}
44    public method bump {{factor up}}
45    protected method _validate {char}
46    protected variable _value ""
47}
48
49itk::usual Videospeed {
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::Videospeed::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) <KeyPress-Return> \
74        "$this value \[$itk_component(entry) get\]"
75    bind $itk_component(entry) <KeyPress-Tab> \
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 Videospeed-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 Videospeed-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::Videospeed::value {args} {
115    if {[llength $args] == 1} {
116        set string [lindex $args 0]
117        # allow for floating point values
118        if { [regexp {^ *([0-9]+\.?[0-9]*|\.[0-9]+)x *$} $string match newval] } {
119        } elseif { [regexp {^ *([0-9]+\.?[0-9]*|\.[0-9]+) *$} $string match newval] } {
120        } else {
121            bell
122            return
123        }
124        if {"" != $newval} {
125            if {"" != $itk_option(-min) && $newval < $itk_option(-min)} {
126                set newval $itk_option(-min)
127            }
128            if {"" != $itk_option(-max) && $newval > $itk_option(-max)} {
129                set newval $itk_option(-max)
130            }
131        }
132        set _value $newval
133        $itk_component(entry) delete 0 end
134        $itk_component(entry) insert 0 ${newval}x
135        after 10 \
136            [list catch [list event generate $itk_component(hull) <<Value>>]]
137    } elseif {[llength $args] != 0} {
138        error "wrong # args: should be \"value ?newval?\""
139    }
140    return $_value
141}
142
143# ----------------------------------------------------------------------
144# USAGE: bump ?<factor>?
145#
146# Used internally when you click on the up/down arrows.  Clients
147# can also use it directly to bump values up/down.  The optional
148# <factor> can be an integer value or the keyword "up" or "down".
149# ----------------------------------------------------------------------
150itcl::body Rappture::Videospeed::bump {{factor up}} {
151    if {"up" == $factor} {
152        set factor $itk_option(-factor)
153    } elseif {"down" == $factor} {
154        set factor [expr {1.0/$itk_option(-factor)}]
155    } elseif {![string is integer $factor]} {
156        error "bad factor \"$factor\": should be up, down, or integer"
157    }
158
159    set val [$itk_component(entry) get]
160    if {$val == ""} {
161        set val 0
162    }
163    value [expr {$_value*$factor}]
164}
165
166# ----------------------------------------------------------------------
167# USAGE: _validate <char>
168#
169# Validates each character as it is typed into the spinner.
170# If the <char> is not a digit, then this procedure beeps and
171# prevents the character from being inserted.
172# ----------------------------------------------------------------------
173itcl::body Rappture::Videospeed::_validate {char} {
174    if {[string match "\[ -~\]" $char]} {
175        if {![string match "\[0-9\]" $char]} {
176            bell
177            return -code break
178        }
179    }
180}
Note: See TracBrowser for help on using the repository browser.