summaryrefslogtreecommitdiffstats
path: root/library/ttk/spinbox.tcl
blob: 579a22c689f44a018304fbcf9cbdd8040b106878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#
# $Id: spinbox.tcl,v 1.1 2008/11/01 15:34:24 patthoyts Exp $
#
# Tile widget set: spinbox bindings.
#
#

namespace eval ttk::spinbox {
    variable Values	;# Values($cb) is -listvariable of listbox widget

    variable State
    set State(entryPress) 0
}

### Spinbox bindings.
#
# Duplicate the Entry bindings, override if needed:
#

ttk::copyBindings TEntry TSpinbox

bind TSpinbox <Double-Button-1> {ttk::spinbox::Select %W %x %y word}
bind TSpinbox <Triple-Button-1> {ttk::spinbox::Select %W %x %y line}

bind TSpinbox <ButtonPress-1> { ttk::spinbox::Press %W %x %y }
bind TSpinbox <ButtonRelease-1> { ttk::spinbox::Release %W %x %y }
bind TSpinbox <MouseWheel> {ttk::spinbox::Change %W [expr {%D/-120}] line}
bind TSpinbox <Up> {ttk::spinbox::Change %W +[%W cget -increment] line}
bind TSpinbox <Down> {ttk::spinbox::Change %W -[%W cget -increment] line}


proc ttk::spinbox::Press {w x y} {
    if {[$w instate disabled]} { return }
    variable State
    set State(xPress) $x
    set State(yPress) $y
    focus $w
    switch -glob -- [$w identify $x $y] {
        *uparrow {
            ttk::Repeatedly Change $w +[$w cget -increment] line
        }
        *downarrow {
            ttk::Repeatedly Change $w -[$w cget -increment] line
        }
        *textarea {
            set State(entryPress) [$w instate !readonly]
            if {$State(entryPress)} {
                ttk::entry::Press $w $x
            }
        }
    }
}

proc ttk::spinbox::Release {w x y} {
    variable State
    unset -nocomplain State(xPress) State(yPress)
    ttk::CancelRepeat
}

proc ttk::spinbox::Change {w n units} {
    if {[set vlen [llength [$w cget -values]]] != 0} {
        set index [expr {[$w current] + $n}]
        if {[catch {$w current $index}]} {
            if {[$w cget -wrap]} {
                if {$index == -1} {
                    set index [llength [$w cget -values]]
                    incr index -1
                } else {
                    set index 0
                }
                $w current $index
            }
        }
    } else {
        if {![catch {expr {[$w get] + $n}} v]} {
            if {$v < [$w cget -from]} {
                if {[$w cget -wrap]} {
                    set v [$w cget -to]
                } else {
                    set v [$w cget -from]
                }
            } elseif {$v > [$w cget -to]} {
                if {[$w cget -wrap]} {
                    set v [$w cget -from]
                } else {
                    set v [$w cget -to]
                }
            }
            $w set $v
        }
    }
    ::ttk::entry::Select $w 0 $units

    # Run -command callback:
    #
    uplevel #0 [$w cget -command]

}

# Spinbox double-click on the arrows needs interception, otherwise
# pass to the TEntry handler
proc ttk::spinbox::Select {w x y mode} {
    if {[$w instate disabled]} { return }
    variable State
    set State(xPress) $x
    set State(yPress) $y
    switch -glob -- [$w identify $x $y] {
        *uparrow {
            ttk::Repeatedly Change $w +[$w cget -increment] units
        }
        *downarrow {
            ttk::Repeatedly Change $w -[$w cget -increment] units
        }
        *textarea { 
            return [::ttk::entry::Select $w $x $mode]
        }
    }
    return -code continue
}

#*EOF*