2 # ttk::spinbox bindings
5 namespace eval ttk
::spinbox { }
9 # Duplicate the Entry bindings, override if needed:
12 ttk
::copyBindings TEntry TSpinbox
14 bind TSpinbox
<Motion
> { ttk
::spinbox::Motion %W
%x
%y
}
15 bind TSpinbox
<ButtonPress-1
> { ttk
::spinbox::Press %W
%x
%y
}
16 bind TSpinbox
<ButtonRelease-1
> { ttk
::spinbox::Release %W
}
17 bind TSpinbox
<Double-Button-1
> { ttk
::spinbox::DoubleClick %W
%x
%y
}
18 bind TSpinbox
<Triple-Button-1
> {} ;# disable TEntry triple-click
20 bind TSpinbox
<KeyPress-Up
> { event generate
%W
<<Increment
>> }
21 bind TSpinbox
<KeyPress-Down
> { event generate
%W
<<Decrement
>> }
23 bind TSpinbox
<<Increment
>> { ttk
::spinbox::Spin %W
+1 }
24 bind TSpinbox
<<Decrement
>> { ttk
::spinbox::Spin %W
-1 }
26 ttk
::bindMouseWheel TSpinbox
[list ttk
::spinbox::MouseWheel %W
]
31 proc ttk
::spinbox::Motion {w x y
} {
32 if { [$w identify
$x $y] eq
"textarea"
33 && [$w instate
{!readonly
!disabled
}]
35 ttk
::setCursor $w text
43 proc ttk
::spinbox::Press {w x y
} {
44 if {[$w instate disabled
]} { return }
46 switch -glob -- [$w identify
$x $y] {
47 *textarea
{ ttk
::entry::Press $w $x }
49 *uparrow
{ ttk
::Repeatedly event generate
$w <<Increment
>> }
51 *downarrow
{ ttk
::Repeatedly event generate
$w <<Decrement
>> }
53 if {$y * 2 >= [winfo height
$w]} {
54 set event <<Decrement
>>
56 set event <<Increment
>>
58 ttk
::Repeatedly event generate
$w $event
64 # Select all if over the text area; otherwise same as Press.
66 proc ttk
::spinbox::DoubleClick {w x y
} {
67 if {[$w instate disabled
]} { return }
69 switch -glob -- [$w identify
$x $y] {
70 *textarea
{ SelectAll
$w }
75 proc ttk
::spinbox::Release {w
} {
80 # Mousewheel callback. Turn these into <<Increment>> (-1, up)
81 # or <<Decrement> (+1, down) events.
83 proc ttk
::spinbox::MouseWheel {w dir
} {
85 event generate
$w <<Increment
>>
87 event generate
$w <<Decrement
>>
92 # Select widget contents.
94 proc ttk
::spinbox::SelectAll {w
} {
95 $w selection range
0 end
100 # Limit $v to lie between $min and $max
102 proc ttk
::spinbox::Limit {v min max
} {
103 if {$v < $min} { return $min }
104 if {$v > $max} { return $max }
109 # Adjust $v to lie between $min and $max, wrapping if out of bounds.
111 proc ttk
::spinbox::Wrap {v min max
} {
112 if {$v < $min} { return $max }
113 if {$v > $max} { return $min }
118 # Limit or wrap spinbox value depending on -wrap.
120 proc ttk
::spinbox::Adjust {w v min max
} {
121 if {[$w cget
-wrap]} {
122 return [Wrap
$v $min $max]
124 return [Limit
$v $min $max]
129 # Handle <<Increment>> and <<Decrement>> events.
130 # If -values is specified, cycle through the list.
131 # Otherwise cycle through numeric range based on
132 # -from, -to, and -increment.
134 proc ttk
::spinbox::Spin {w dir
} {
135 set nvalues
[llength [set values
[$w cget
-values]]]
138 set current
[lsearch -exact $values $value]
139 set index
[Adjust
$w [expr {$current + $dir}] 0 [expr {$nvalues - 1}]]
140 $w set [lindex $values $index]
143 set v
[expr {[scan [$w get
] %f
] + $dir * [$w cget
-increment]}]
145 set v
[$w cget
-from]
147 $w set [FormatValue
$w [Adjust
$w $v [$w cget
-from] [$w cget
-to]]]
150 uplevel #0 [$w cget -command]
154 # Reformat numeric value based on -format.
156 proc ttk
::spinbox::FormatValue {w val
} {
157 set fmt
[$w cget
-format]
159 # Try to guess a suitable -format based on -increment.
160 set delta
[expr {abs
([$w cget
-increment])}]
161 if {0 < $delta && $delta < 1} {
162 # NB: This guesses wrong if -increment has more than 1
163 # significant digit itself, e.g., -increment 0.25
164 set nsd
[expr {int
(ceil
(-log10($delta)))}]
170 return [format $fmt $val]