spinbox.tcl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. # spinbox.tcl --
  2. #
  3. # This file defines the default bindings for Tk spinbox widgets and provides
  4. # procedures that help in implementing those bindings. The spinbox builds
  5. # off the entry widget, so it can reuse Entry bindings and procedures.
  6. #
  7. # Copyright (c) 1992-1994 The Regents of the University of California.
  8. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  9. # Copyright (c) 1999-2000 Jeffrey Hobbs
  10. # Copyright (c) 2000 Ajuba Solutions
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15. #-------------------------------------------------------------------------
  16. # Elements of tk::Priv that are used in this file:
  17. #
  18. # afterId - If non-null, it means that auto-scanning is underway
  19. # and it gives the "after" id for the next auto-scan
  20. # command to be executed.
  21. # mouseMoved - Non-zero means the mouse has moved a significant
  22. # amount since the button went down (so, for example,
  23. # start dragging out a selection).
  24. # pressX - X-coordinate at which the mouse button was pressed.
  25. # selectMode - The style of selection currently underway:
  26. # char, word, or line.
  27. # x, y - Last known mouse coordinates for scanning
  28. # and auto-scanning.
  29. # data - Used for Cut and Copy
  30. #-------------------------------------------------------------------------
  31. # Initialize namespace
  32. namespace eval ::tk::spinbox {}
  33. #-------------------------------------------------------------------------
  34. # The code below creates the default class bindings for entries.
  35. #-------------------------------------------------------------------------
  36. bind Spinbox <<Cut>> {
  37. if {![catch {::tk::spinbox::GetSelection %W} tk::Priv(data)]} {
  38. clipboard clear -displayof %W
  39. clipboard append -displayof %W $tk::Priv(data)
  40. %W delete sel.first sel.last
  41. unset tk::Priv(data)
  42. }
  43. }
  44. bind Spinbox <<Copy>> {
  45. if {![catch {::tk::spinbox::GetSelection %W} tk::Priv(data)]} {
  46. clipboard clear -displayof %W
  47. clipboard append -displayof %W $tk::Priv(data)
  48. unset tk::Priv(data)
  49. }
  50. }
  51. bind Spinbox <<Paste>> {
  52. catch {
  53. if {[tk windowingsystem] ne "x11"} {
  54. catch {
  55. %W delete sel.first sel.last
  56. }
  57. }
  58. %W insert insert [::tk::GetSelection %W CLIPBOARD]
  59. ::tk::EntrySeeInsert %W
  60. }
  61. }
  62. bind Spinbox <<Clear>> {
  63. %W delete sel.first sel.last
  64. }
  65. bind Spinbox <<PasteSelection>> {
  66. if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
  67. || !$tk::Priv(mouseMoved)} {
  68. ::tk::spinbox::Paste %W %x
  69. }
  70. }
  71. bind Spinbox <<TraverseIn>> {
  72. %W selection range 0 end
  73. %W icursor end
  74. }
  75. # Standard Motif bindings:
  76. bind Spinbox <1> {
  77. ::tk::spinbox::ButtonDown %W %x %y
  78. }
  79. bind Spinbox <B1-Motion> {
  80. ::tk::spinbox::Motion %W %x %y
  81. }
  82. bind Spinbox <Double-1> {
  83. ::tk::spinbox::ArrowPress %W %x %y
  84. set tk::Priv(selectMode) word
  85. ::tk::spinbox::MouseSelect %W %x sel.first
  86. }
  87. bind Spinbox <Triple-1> {
  88. ::tk::spinbox::ArrowPress %W %x %y
  89. set tk::Priv(selectMode) line
  90. ::tk::spinbox::MouseSelect %W %x 0
  91. }
  92. bind Spinbox <Shift-1> {
  93. set tk::Priv(selectMode) char
  94. %W selection adjust @%x
  95. }
  96. bind Spinbox <Double-Shift-1> {
  97. set tk::Priv(selectMode) word
  98. ::tk::spinbox::MouseSelect %W %x
  99. }
  100. bind Spinbox <Triple-Shift-1> {
  101. set tk::Priv(selectMode) line
  102. ::tk::spinbox::MouseSelect %W %x
  103. }
  104. bind Spinbox <B1-Leave> {
  105. set tk::Priv(x) %x
  106. ::tk::spinbox::AutoScan %W
  107. }
  108. bind Spinbox <B1-Enter> {
  109. tk::CancelRepeat
  110. }
  111. bind Spinbox <ButtonRelease-1> {
  112. ::tk::spinbox::ButtonUp %W %x %y
  113. }
  114. bind Spinbox <Control-1> {
  115. %W icursor @%x
  116. }
  117. bind Spinbox <<PrevLine>> {
  118. %W invoke buttonup
  119. }
  120. bind Spinbox <<NextLine>> {
  121. %W invoke buttondown
  122. }
  123. bind Spinbox <<PrevChar>> {
  124. ::tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
  125. }
  126. bind Spinbox <<NextChar>> {
  127. ::tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
  128. }
  129. bind Spinbox <<SelectPrevChar>> {
  130. ::tk::EntryKeySelect %W [expr {[%W index insert] - 1}]
  131. ::tk::EntrySeeInsert %W
  132. }
  133. bind Spinbox <<SelectNextChar>> {
  134. ::tk::EntryKeySelect %W [expr {[%W index insert] + 1}]
  135. ::tk::EntrySeeInsert %W
  136. }
  137. bind Spinbox <<PrevWord>> {
  138. ::tk::EntrySetCursor %W [::tk::EntryPreviousWord %W insert]
  139. }
  140. bind Spinbox <<NextWord>> {
  141. ::tk::EntrySetCursor %W [::tk::EntryNextWord %W insert]
  142. }
  143. bind Spinbox <<SelectPrevWord>> {
  144. ::tk::EntryKeySelect %W [::tk::EntryPreviousWord %W insert]
  145. ::tk::EntrySeeInsert %W
  146. }
  147. bind Spinbox <<SelectNextWord>> {
  148. ::tk::EntryKeySelect %W [::tk::EntryNextWord %W insert]
  149. ::tk::EntrySeeInsert %W
  150. }
  151. bind Spinbox <<LineStart>> {
  152. ::tk::EntrySetCursor %W 0
  153. }
  154. bind Spinbox <<SelectLineStart>> {
  155. ::tk::EntryKeySelect %W 0
  156. ::tk::EntrySeeInsert %W
  157. }
  158. bind Spinbox <<LineEnd>> {
  159. ::tk::EntrySetCursor %W end
  160. }
  161. bind Spinbox <<SelectLineEnd>> {
  162. ::tk::EntryKeySelect %W end
  163. ::tk::EntrySeeInsert %W
  164. }
  165. bind Spinbox <Delete> {
  166. if {[%W selection present]} {
  167. %W delete sel.first sel.last
  168. } else {
  169. %W delete insert
  170. }
  171. }
  172. bind Spinbox <BackSpace> {
  173. ::tk::EntryBackspace %W
  174. }
  175. bind Spinbox <Control-space> {
  176. %W selection from insert
  177. }
  178. bind Spinbox <Select> {
  179. %W selection from insert
  180. }
  181. bind Spinbox <Control-Shift-space> {
  182. %W selection adjust insert
  183. }
  184. bind Spinbox <Shift-Select> {
  185. %W selection adjust insert
  186. }
  187. bind Spinbox <<SelectAll>> {
  188. %W selection range 0 end
  189. }
  190. bind Spinbox <<SelectNone>> {
  191. %W selection clear
  192. }
  193. bind Spinbox <Key> {
  194. ::tk::EntryInsert %W %A
  195. }
  196. # Ignore all Alt, Meta, Control, and Mod4 keypresses unless explicitly bound.
  197. # Otherwise, if a widget binding for one of these is defined, the
  198. # <Key> class binding will also fire and insert the character,
  199. # which is wrong. Ditto for Escape, Return, and Tab.
  200. bind Spinbox <Alt-Key> {# nothing}
  201. bind Spinbox <Meta-Key> {# nothing}
  202. bind Spinbox <Control-Key> {# nothing}
  203. bind Spinbox <Escape> {# nothing}
  204. bind Spinbox <Return> {# nothing}
  205. bind Spinbox <KP_Enter> {# nothing}
  206. bind Spinbox <Tab> {# nothing}
  207. bind Spinbox <Prior> {# nothing}
  208. bind Spinbox <Next> {# nothing}
  209. if {[tk windowingsystem] eq "aqua"} {
  210. bind Spinbox <Command-Key> {# nothing}
  211. bind Spinbox <Mod4-Key> {# nothing}
  212. }
  213. # On Windows, paste is done using Shift-Insert. Shift-Insert already
  214. # generates the <<Paste>> event, so we don't need to do anything here.
  215. if {[tk windowingsystem] ne "win32"} {
  216. bind Spinbox <Insert> {
  217. catch {::tk::EntryInsert %W [::tk::GetSelection %W PRIMARY]}
  218. }
  219. }
  220. # Additional emacs-like bindings:
  221. bind Spinbox <Control-d> {
  222. if {!$tk_strictMotif} {
  223. %W delete insert
  224. }
  225. }
  226. bind Spinbox <Control-h> {
  227. if {!$tk_strictMotif} {
  228. ::tk::EntryBackspace %W
  229. }
  230. }
  231. bind Spinbox <Control-k> {
  232. if {!$tk_strictMotif} {
  233. %W delete insert end
  234. }
  235. }
  236. bind Spinbox <Control-t> {
  237. if {!$tk_strictMotif} {
  238. ::tk::EntryTranspose %W
  239. }
  240. }
  241. bind Spinbox <Meta-b> {
  242. if {!$tk_strictMotif} {
  243. ::tk::EntrySetCursor %W [::tk::EntryPreviousWord %W insert]
  244. }
  245. }
  246. bind Spinbox <Meta-d> {
  247. if {!$tk_strictMotif} {
  248. %W delete insert [::tk::EntryNextWord %W insert]
  249. }
  250. }
  251. bind Spinbox <Meta-f> {
  252. if {!$tk_strictMotif} {
  253. ::tk::EntrySetCursor %W [::tk::EntryNextWord %W insert]
  254. }
  255. }
  256. bind Spinbox <Meta-BackSpace> {
  257. if {!$tk_strictMotif} {
  258. %W delete [::tk::EntryPreviousWord %W insert] insert
  259. }
  260. }
  261. bind Spinbox <Meta-Delete> {
  262. if {!$tk_strictMotif} {
  263. %W delete [::tk::EntryPreviousWord %W insert] insert
  264. }
  265. }
  266. # A few additional bindings of my own.
  267. if {[tk windowingsystem] ne "aqua"} {
  268. bind Spinbox <2> {
  269. if {!$tk_strictMotif} {
  270. ::tk::EntryScanMark %W %x
  271. }
  272. }
  273. bind Spinbox <B2-Motion> {
  274. if {!$tk_strictMotif} {
  275. ::tk::EntryScanDrag %W %x
  276. }
  277. }
  278. } else {
  279. bind Spinbox <3> {
  280. if {!$tk_strictMotif} {
  281. ::tk::EntryScanMark %W %x
  282. }
  283. }
  284. bind Spinbox <B3-Motion> {
  285. if {!$tk_strictMotif} {
  286. ::tk::EntryScanDrag %W %x
  287. }
  288. }
  289. }
  290. # ::tk::spinbox::Invoke --
  291. # Invoke an element of the spinbox
  292. #
  293. # Arguments:
  294. # w - The spinbox window.
  295. # elem - Element to invoke
  296. proc ::tk::spinbox::Invoke {w elem} {
  297. variable ::tk::Priv
  298. if {![winfo exists $w]} {
  299. return
  300. }
  301. if {![info exists Priv(outsideElement)]} {
  302. $w invoke $elem
  303. incr Priv(repeated)
  304. }
  305. set delay [$w cget -repeatinterval]
  306. if {$delay > 0} {
  307. set Priv(afterId) [after $delay \
  308. [list ::tk::spinbox::Invoke $w $elem]]
  309. }
  310. }
  311. # ::tk::spinbox::ClosestGap --
  312. # Given x and y coordinates, this procedure finds the closest boundary
  313. # between characters to the given coordinates and returns the index
  314. # of the character just after the boundary.
  315. #
  316. # Arguments:
  317. # w - The spinbox window.
  318. # x - X-coordinate within the window.
  319. proc ::tk::spinbox::ClosestGap {w x} {
  320. set pos [$w index @$x]
  321. set bbox [$w bbox $pos]
  322. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  323. return $pos
  324. }
  325. incr pos
  326. }
  327. # ::tk::spinbox::ArrowPress --
  328. # This procedure is invoked to handle button-1 presses in buttonup
  329. # or buttondown elements of spinbox widgets.
  330. #
  331. # Arguments:
  332. # w - The spinbox window in which the button was pressed.
  333. # x - The x-coordinate of the button press.
  334. # y - The y-coordinate of the button press.
  335. proc ::tk::spinbox::ArrowPress {w x y} {
  336. variable ::tk::Priv
  337. if {[$w cget -state] ne "disabled" && \
  338. [string match "button*" $Priv(element)]} {
  339. $w selection element $Priv(element)
  340. set Priv(repeated) 0
  341. set Priv(relief) [$w cget -$Priv(element)relief]
  342. catch {after cancel $Priv(afterId)}
  343. set delay [$w cget -repeatdelay]
  344. if {$delay > 0} {
  345. set Priv(afterId) [after $delay \
  346. [list ::tk::spinbox::Invoke $w $Priv(element)]]
  347. }
  348. if {[info exists Priv(outsideElement)]} {
  349. unset Priv(outsideElement)
  350. }
  351. }
  352. }
  353. # ::tk::spinbox::ButtonDown --
  354. # This procedure is invoked to handle button-1 presses in spinbox
  355. # widgets. It moves the insertion cursor, sets the selection anchor,
  356. # and claims the input focus.
  357. #
  358. # Arguments:
  359. # w - The spinbox window in which the button was pressed.
  360. # x - The x-coordinate of the button press.
  361. # y - The y-coordinate of the button press.
  362. proc ::tk::spinbox::ButtonDown {w x y} {
  363. variable ::tk::Priv
  364. # Get the element that was clicked in. If we are not directly over
  365. # the spinbox, default to entry. This is necessary for spinbox grabs.
  366. #
  367. set Priv(element) [$w identify $x $y]
  368. if {$Priv(element) eq ""} {
  369. set Priv(element) "entry"
  370. }
  371. switch -exact $Priv(element) {
  372. "buttonup" - "buttondown" {
  373. ::tk::spinbox::ArrowPress $w $x $y
  374. }
  375. "entry" {
  376. set Priv(selectMode) char
  377. set Priv(mouseMoved) 0
  378. set Priv(pressX) $x
  379. $w icursor [::tk::spinbox::ClosestGap $w $x]
  380. $w selection from insert
  381. if {"disabled" ne [$w cget -state]} {focus $w}
  382. $w selection clear
  383. }
  384. default {
  385. return -code error -errorcode {TK SPINBOX UNKNOWN_ELEMENT} \
  386. "unknown spinbox element \"$Priv(element)\""
  387. }
  388. }
  389. }
  390. # ::tk::spinbox::ButtonUp --
  391. # This procedure is invoked to handle button-1 releases in spinbox
  392. # widgets.
  393. #
  394. # Arguments:
  395. # w - The spinbox window in which the button was pressed.
  396. # x - The x-coordinate of the button press.
  397. # y - The y-coordinate of the button press.
  398. proc ::tk::spinbox::ButtonUp {w x y} {
  399. variable ::tk::Priv
  400. ::tk::CancelRepeat
  401. # Priv(relief) may not exist if the ButtonUp is not paired with
  402. # a preceding ButtonDown
  403. if {[info exists Priv(element)] && [info exists Priv(relief)] && \
  404. [string match "button*" $Priv(element)]} {
  405. if {[info exists Priv(repeated)] && !$Priv(repeated)} {
  406. $w invoke $Priv(element)
  407. }
  408. $w configure -$Priv(element)relief $Priv(relief)
  409. $w selection element none
  410. }
  411. }
  412. # ::tk::spinbox::MouseSelect --
  413. # This procedure is invoked when dragging out a selection with
  414. # the mouse. Depending on the selection mode (character, word,
  415. # line) it selects in different-sized units. This procedure
  416. # ignores mouse motions initially until the mouse has moved from
  417. # one character to another or until there have been multiple clicks.
  418. #
  419. # Arguments:
  420. # w - The spinbox window in which the button was pressed.
  421. # x - The x-coordinate of the mouse.
  422. # cursor - optional place to set cursor.
  423. proc ::tk::spinbox::MouseSelect {w x {cursor {}}} {
  424. variable ::tk::Priv
  425. if {$Priv(element) ne "entry"} {
  426. # The ButtonUp command triggered by ButtonRelease-1 handles
  427. # invoking one of the spinbuttons.
  428. return
  429. }
  430. set cur [::tk::spinbox::ClosestGap $w $x]
  431. set anchor [$w index anchor]
  432. if {($cur ne $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
  433. set Priv(mouseMoved) 1
  434. }
  435. switch $Priv(selectMode) {
  436. char {
  437. if {$Priv(mouseMoved)} {
  438. if {$cur < $anchor} {
  439. $w selection range $cur $anchor
  440. } elseif {$cur > $anchor} {
  441. $w selection range $anchor $cur
  442. } else {
  443. $w selection clear
  444. }
  445. }
  446. }
  447. word {
  448. if {$cur < [$w index anchor]} {
  449. set before [tcl_wordBreakBefore [$w get] $cur]
  450. set after [tcl_wordBreakAfter [$w get] $anchor-1]
  451. } else {
  452. set before [tcl_wordBreakBefore [$w get] $anchor]
  453. set after [tcl_wordBreakAfter [$w get] $cur-1]
  454. }
  455. if {$before < 0} {
  456. set before 0
  457. }
  458. if {$after < 0} {
  459. set after end
  460. }
  461. $w selection range $before $after
  462. }
  463. line {
  464. $w selection range 0 end
  465. }
  466. }
  467. if {$cursor ne {} && $cursor ne "ignore"} {
  468. catch {$w icursor $cursor}
  469. }
  470. update idletasks
  471. }
  472. # ::tk::spinbox::Paste --
  473. # This procedure sets the insertion cursor to the current mouse position,
  474. # pastes the selection there, and sets the focus to the window.
  475. #
  476. # Arguments:
  477. # w - The spinbox window.
  478. # x - X position of the mouse.
  479. proc ::tk::spinbox::Paste {w x} {
  480. $w icursor [::tk::spinbox::ClosestGap $w $x]
  481. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  482. if {"disabled" eq [$w cget -state]} {
  483. focus $w
  484. }
  485. }
  486. # ::tk::spinbox::Motion --
  487. # This procedure is invoked when the mouse moves in a spinbox window
  488. # with button 1 down.
  489. #
  490. # Arguments:
  491. # w - The spinbox window.
  492. # x - The x-coordinate of the mouse.
  493. # y - The y-coordinate of the mouse.
  494. proc ::tk::spinbox::Motion {w x y} {
  495. variable ::tk::Priv
  496. if {![info exists Priv(element)]} {
  497. set Priv(element) [$w identify $x $y]
  498. }
  499. set Priv(x) $x
  500. if {"entry" eq $Priv(element)} {
  501. ::tk::spinbox::MouseSelect $w $x ignore
  502. } elseif {[$w identify $x $y] ne $Priv(element)} {
  503. if {![info exists Priv(outsideElement)]} {
  504. # We've wandered out of the spin button
  505. # setting outside element will cause ::tk::spinbox::Invoke to
  506. # loop without doing anything
  507. set Priv(outsideElement) ""
  508. $w selection element none
  509. }
  510. } elseif {[info exists Priv(outsideElement)]} {
  511. unset Priv(outsideElement)
  512. $w selection element $Priv(element)
  513. }
  514. }
  515. # ::tk::spinbox::AutoScan --
  516. # This procedure is invoked when the mouse leaves an spinbox window
  517. # with button 1 down. It scrolls the window left or right,
  518. # depending on where the mouse is, and reschedules itself as an
  519. # "after" command so that the window continues to scroll until the
  520. # mouse moves back into the window or the mouse button is released.
  521. #
  522. # Arguments:
  523. # w - The spinbox window.
  524. proc ::tk::spinbox::AutoScan {w} {
  525. variable ::tk::Priv
  526. set x $Priv(x)
  527. if {$x >= [winfo width $w]} {
  528. $w xview scroll 2 units
  529. ::tk::spinbox::MouseSelect $w $x ignore
  530. } elseif {$x < 0} {
  531. $w xview scroll -2 units
  532. ::tk::spinbox::MouseSelect $w $x ignore
  533. }
  534. set Priv(afterId) [after 50 [list ::tk::spinbox::AutoScan $w]]
  535. }
  536. # ::tk::spinbox::GetSelection --
  537. #
  538. # Returns the selected text of the spinbox. Differs from entry in that
  539. # a spinbox has no -show option to obscure contents.
  540. #
  541. # Arguments:
  542. # w - The spinbox window from which the text to get
  543. proc ::tk::spinbox::GetSelection {w} {
  544. return [string range [$w get] [$w index sel.first] \
  545. [expr {[$w index sel.last] - 1}]]
  546. }