spinbox.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. global tcl_platform
  53. catch {
  54. if {[tk windowingsystem] ne "x11"} {
  55. catch {
  56. %W delete sel.first sel.last
  57. }
  58. }
  59. %W insert insert [::tk::GetSelection %W CLIPBOARD]
  60. ::tk::EntrySeeInsert %W
  61. }
  62. }
  63. bind Spinbox <<Clear>> {
  64. %W delete sel.first sel.last
  65. }
  66. bind Spinbox <<PasteSelection>> {
  67. if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
  68. || !$tk::Priv(mouseMoved)} {
  69. ::tk::spinbox::Paste %W %x
  70. }
  71. }
  72. bind Spinbox <<TraverseIn>> {
  73. %W selection range 0 end
  74. %W icursor end
  75. }
  76. # Standard Motif bindings:
  77. bind Spinbox <1> {
  78. ::tk::spinbox::ButtonDown %W %x %y
  79. }
  80. bind Spinbox <B1-Motion> {
  81. ::tk::spinbox::Motion %W %x %y
  82. }
  83. bind Spinbox <Double-1> {
  84. set tk::Priv(selectMode) word
  85. ::tk::spinbox::MouseSelect %W %x sel.first
  86. }
  87. bind Spinbox <Triple-1> {
  88. set tk::Priv(selectMode) line
  89. ::tk::spinbox::MouseSelect %W %x 0
  90. }
  91. bind Spinbox <Shift-1> {
  92. set tk::Priv(selectMode) char
  93. %W selection adjust @%x
  94. }
  95. bind Spinbox <Double-Shift-1> {
  96. set tk::Priv(selectMode) word
  97. ::tk::spinbox::MouseSelect %W %x
  98. }
  99. bind Spinbox <Triple-Shift-1> {
  100. set tk::Priv(selectMode) line
  101. ::tk::spinbox::MouseSelect %W %x
  102. }
  103. bind Spinbox <B1-Leave> {
  104. set tk::Priv(x) %x
  105. ::tk::spinbox::AutoScan %W
  106. }
  107. bind Spinbox <B1-Enter> {
  108. tk::CancelRepeat
  109. }
  110. bind Spinbox <ButtonRelease-1> {
  111. ::tk::spinbox::ButtonUp %W %x %y
  112. }
  113. bind Spinbox <Control-1> {
  114. %W icursor @%x
  115. }
  116. bind Spinbox <Up> {
  117. %W invoke buttonup
  118. }
  119. bind Spinbox <Down> {
  120. %W invoke buttondown
  121. }
  122. bind Spinbox <Left> {
  123. ::tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
  124. }
  125. bind Spinbox <Right> {
  126. ::tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
  127. }
  128. bind Spinbox <Shift-Left> {
  129. ::tk::EntryKeySelect %W [expr {[%W index insert] - 1}]
  130. ::tk::EntrySeeInsert %W
  131. }
  132. bind Spinbox <Shift-Right> {
  133. ::tk::EntryKeySelect %W [expr {[%W index insert] + 1}]
  134. ::tk::EntrySeeInsert %W
  135. }
  136. bind Spinbox <Control-Left> {
  137. ::tk::EntrySetCursor %W [::tk::EntryPreviousWord %W insert]
  138. }
  139. bind Spinbox <Control-Right> {
  140. ::tk::EntrySetCursor %W [::tk::EntryNextWord %W insert]
  141. }
  142. bind Spinbox <Shift-Control-Left> {
  143. ::tk::EntryKeySelect %W [::tk::EntryPreviousWord %W insert]
  144. ::tk::EntrySeeInsert %W
  145. }
  146. bind Spinbox <Shift-Control-Right> {
  147. ::tk::EntryKeySelect %W [::tk::EntryNextWord %W insert]
  148. ::tk::EntrySeeInsert %W
  149. }
  150. bind Spinbox <Home> {
  151. ::tk::EntrySetCursor %W 0
  152. }
  153. bind Spinbox <Shift-Home> {
  154. ::tk::EntryKeySelect %W 0
  155. ::tk::EntrySeeInsert %W
  156. }
  157. bind Spinbox <End> {
  158. ::tk::EntrySetCursor %W end
  159. }
  160. bind Spinbox <Shift-End> {
  161. ::tk::EntryKeySelect %W end
  162. ::tk::EntrySeeInsert %W
  163. }
  164. bind Spinbox <Delete> {
  165. if {[%W selection present]} {
  166. %W delete sel.first sel.last
  167. } else {
  168. %W delete insert
  169. }
  170. }
  171. bind Spinbox <BackSpace> {
  172. ::tk::EntryBackspace %W
  173. }
  174. bind Spinbox <Control-space> {
  175. %W selection from insert
  176. }
  177. bind Spinbox <Select> {
  178. %W selection from insert
  179. }
  180. bind Spinbox <Control-Shift-space> {
  181. %W selection adjust insert
  182. }
  183. bind Spinbox <Shift-Select> {
  184. %W selection adjust insert
  185. }
  186. bind Spinbox <Control-slash> {
  187. %W selection range 0 end
  188. }
  189. bind Spinbox <Control-backslash> {
  190. %W selection clear
  191. }
  192. bind Spinbox <KeyPress> {
  193. ::tk::EntryInsert %W %A
  194. }
  195. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  196. # Otherwise, if a widget binding for one of these is defined, the
  197. # <KeyPress> class binding will also fire and insert the character,
  198. # which is wrong. Ditto for Escape, Return, and Tab.
  199. bind Spinbox <Alt-KeyPress> {# nothing}
  200. bind Spinbox <Meta-KeyPress> {# nothing}
  201. bind Spinbox <Control-KeyPress> {# nothing}
  202. bind Spinbox <Escape> {# nothing}
  203. bind Spinbox <Return> {# nothing}
  204. bind Spinbox <KP_Enter> {# nothing}
  205. bind Spinbox <Tab> {# nothing}
  206. if {[tk windowingsystem] eq "aqua"} {
  207. bind Spinbox <Command-KeyPress> {# nothing}
  208. }
  209. # On Windows, paste is done using Shift-Insert. Shift-Insert already
  210. # generates the <<Paste>> event, so we don't need to do anything here.
  211. if {[tk windowingsystem] ne "win32"} {
  212. bind Spinbox <Insert> {
  213. catch {::tk::EntryInsert %W [::tk::GetSelection %W PRIMARY]}
  214. }
  215. }
  216. # Additional emacs-like bindings:
  217. bind Spinbox <Control-a> {
  218. if {!$tk_strictMotif} {
  219. ::tk::EntrySetCursor %W 0
  220. }
  221. }
  222. bind Spinbox <Control-b> {
  223. if {!$tk_strictMotif} {
  224. ::tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
  225. }
  226. }
  227. bind Spinbox <Control-d> {
  228. if {!$tk_strictMotif} {
  229. %W delete insert
  230. }
  231. }
  232. bind Spinbox <Control-e> {
  233. if {!$tk_strictMotif} {
  234. ::tk::EntrySetCursor %W end
  235. }
  236. }
  237. bind Spinbox <Control-f> {
  238. if {!$tk_strictMotif} {
  239. ::tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
  240. }
  241. }
  242. bind Spinbox <Control-h> {
  243. if {!$tk_strictMotif} {
  244. ::tk::EntryBackspace %W
  245. }
  246. }
  247. bind Spinbox <Control-k> {
  248. if {!$tk_strictMotif} {
  249. %W delete insert end
  250. }
  251. }
  252. bind Spinbox <Control-t> {
  253. if {!$tk_strictMotif} {
  254. ::tk::EntryTranspose %W
  255. }
  256. }
  257. bind Spinbox <Meta-b> {
  258. if {!$tk_strictMotif} {
  259. ::tk::EntrySetCursor %W [::tk::EntryPreviousWord %W insert]
  260. }
  261. }
  262. bind Spinbox <Meta-d> {
  263. if {!$tk_strictMotif} {
  264. %W delete insert [::tk::EntryNextWord %W insert]
  265. }
  266. }
  267. bind Spinbox <Meta-f> {
  268. if {!$tk_strictMotif} {
  269. ::tk::EntrySetCursor %W [::tk::EntryNextWord %W insert]
  270. }
  271. }
  272. bind Spinbox <Meta-BackSpace> {
  273. if {!$tk_strictMotif} {
  274. %W delete [::tk::EntryPreviousWord %W insert] insert
  275. }
  276. }
  277. bind Spinbox <Meta-Delete> {
  278. if {!$tk_strictMotif} {
  279. %W delete [::tk::EntryPreviousWord %W insert] insert
  280. }
  281. }
  282. # A few additional bindings of my own.
  283. bind Spinbox <2> {
  284. if {!$tk_strictMotif} {
  285. ::tk::EntryScanMark %W %x
  286. }
  287. }
  288. bind Spinbox <B2-Motion> {
  289. if {!$tk_strictMotif} {
  290. ::tk::EntryScanDrag %W %x
  291. }
  292. }
  293. # ::tk::spinbox::Invoke --
  294. # Invoke an element of the spinbox
  295. #
  296. # Arguments:
  297. # w - The spinbox window.
  298. # elem - Element to invoke
  299. proc ::tk::spinbox::Invoke {w elem} {
  300. variable ::tk::Priv
  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::ButtonDown --
  328. # This procedure is invoked to handle button-1 presses in spinbox
  329. # widgets. It moves the insertion cursor, sets the selection anchor,
  330. # and claims the input focus.
  331. #
  332. # Arguments:
  333. # w - The spinbox window in which the button was pressed.
  334. # x - The x-coordinate of the button press.
  335. proc ::tk::spinbox::ButtonDown {w x y} {
  336. variable ::tk::Priv
  337. # Get the element that was clicked in. If we are not directly over
  338. # the spinbox, default to entry. This is necessary for spinbox grabs.
  339. #
  340. set Priv(element) [$w identify $x $y]
  341. if {$Priv(element) eq ""} {
  342. set Priv(element) "entry"
  343. }
  344. switch -exact $Priv(element) {
  345. "buttonup" - "buttondown" {
  346. if {"disabled" ne [$w cget -state]} {
  347. $w selection element $Priv(element)
  348. set Priv(repeated) 0
  349. set Priv(relief) [$w cget -$Priv(element)relief]
  350. catch {after cancel $Priv(afterId)}
  351. set delay [$w cget -repeatdelay]
  352. if {$delay > 0} {
  353. set Priv(afterId) [after $delay \
  354. [list ::tk::spinbox::Invoke $w $Priv(element)]]
  355. }
  356. if {[info exists Priv(outsideElement)]} {
  357. unset Priv(outsideElement)
  358. }
  359. }
  360. }
  361. "entry" {
  362. set Priv(selectMode) char
  363. set Priv(mouseMoved) 0
  364. set Priv(pressX) $x
  365. $w icursor [::tk::spinbox::ClosestGap $w $x]
  366. $w selection from insert
  367. if {"disabled" ne [$w cget -state]} {focus $w}
  368. $w selection clear
  369. }
  370. default {
  371. return -code error "unknown spinbox element \"$Priv(element)\""
  372. }
  373. }
  374. }
  375. # ::tk::spinbox::ButtonUp --
  376. # This procedure is invoked to handle button-1 releases in spinbox
  377. # widgets.
  378. #
  379. # Arguments:
  380. # w - The spinbox window in which the button was pressed.
  381. # x - The x-coordinate of the button press.
  382. proc ::tk::spinbox::ButtonUp {w x y} {
  383. variable ::tk::Priv
  384. ::tk::CancelRepeat
  385. # Priv(relief) may not exist if the ButtonUp is not paired with
  386. # a preceding ButtonDown
  387. if {[info exists Priv(element)] && [info exists Priv(relief)] && \
  388. [string match "button*" $Priv(element)]} {
  389. if {[info exists Priv(repeated)] && !$Priv(repeated)} {
  390. $w invoke $Priv(element)
  391. }
  392. $w configure -$Priv(element)relief $Priv(relief)
  393. $w selection element none
  394. }
  395. }
  396. # ::tk::spinbox::MouseSelect --
  397. # This procedure is invoked when dragging out a selection with
  398. # the mouse. Depending on the selection mode (character, word,
  399. # line) it selects in different-sized units. This procedure
  400. # ignores mouse motions initially until the mouse has moved from
  401. # one character to another or until there have been multiple clicks.
  402. #
  403. # Arguments:
  404. # w - The spinbox window in which the button was pressed.
  405. # x - The x-coordinate of the mouse.
  406. # cursor - optional place to set cursor.
  407. proc ::tk::spinbox::MouseSelect {w x {cursor {}}} {
  408. variable ::tk::Priv
  409. if {$Priv(element) ne "entry"} {
  410. # The ButtonUp command triggered by ButtonRelease-1 handles
  411. # invoking one of the spinbuttons.
  412. return
  413. }
  414. set cur [::tk::spinbox::ClosestGap $w $x]
  415. set anchor [$w index anchor]
  416. if {($cur ne $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
  417. set Priv(mouseMoved) 1
  418. }
  419. switch $Priv(selectMode) {
  420. char {
  421. if {$Priv(mouseMoved)} {
  422. if {$cur < $anchor} {
  423. $w selection range $cur $anchor
  424. } elseif {$cur > $anchor} {
  425. $w selection range $anchor $cur
  426. } else {
  427. $w selection clear
  428. }
  429. }
  430. }
  431. word {
  432. if {$cur < [$w index anchor]} {
  433. set before [tcl_wordBreakBefore [$w get] $cur]
  434. set after [tcl_wordBreakAfter [$w get] [expr {$anchor-1}]]
  435. } else {
  436. set before [tcl_wordBreakBefore [$w get] $anchor]
  437. set after [tcl_wordBreakAfter [$w get] [expr {$cur - 1}]]
  438. }
  439. if {$before < 0} {
  440. set before 0
  441. }
  442. if {$after < 0} {
  443. set after end
  444. }
  445. $w selection range $before $after
  446. }
  447. line {
  448. $w selection range 0 end
  449. }
  450. }
  451. if {$cursor ne {} && $cursor ne "ignore"} {
  452. catch {$w icursor $cursor}
  453. }
  454. update idletasks
  455. }
  456. # ::tk::spinbox::Paste --
  457. # This procedure sets the insertion cursor to the current mouse position,
  458. # pastes the selection there, and sets the focus to the window.
  459. #
  460. # Arguments:
  461. # w - The spinbox window.
  462. # x - X position of the mouse.
  463. proc ::tk::spinbox::Paste {w x} {
  464. $w icursor [::tk::spinbox::ClosestGap $w $x]
  465. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  466. if {"disabled" eq [$w cget -state]} {
  467. focus $w
  468. }
  469. }
  470. # ::tk::spinbox::Motion --
  471. # This procedure is invoked when the mouse moves in a spinbox window
  472. # with button 1 down.
  473. #
  474. # Arguments:
  475. # w - The spinbox window.
  476. proc ::tk::spinbox::Motion {w x y} {
  477. variable ::tk::Priv
  478. if {![info exists Priv(element)]} {
  479. set Priv(element) [$w identify $x $y]
  480. }
  481. set Priv(x) $x
  482. if {"entry" eq $Priv(element)} {
  483. ::tk::spinbox::MouseSelect $w $x ignore
  484. } elseif {[$w identify $x $y] ne $Priv(element)} {
  485. if {![info exists Priv(outsideElement)]} {
  486. # We've wandered out of the spin button
  487. # setting outside element will cause ::tk::spinbox::Invoke to
  488. # loop without doing anything
  489. set Priv(outsideElement) ""
  490. $w selection element none
  491. }
  492. } elseif {[info exists Priv(outsideElement)]} {
  493. unset Priv(outsideElement)
  494. $w selection element $Priv(element)
  495. }
  496. }
  497. # ::tk::spinbox::AutoScan --
  498. # This procedure is invoked when the mouse leaves an spinbox window
  499. # with button 1 down. It scrolls the window left or right,
  500. # depending on where the mouse is, and reschedules itself as an
  501. # "after" command so that the window continues to scroll until the
  502. # mouse moves back into the window or the mouse button is released.
  503. #
  504. # Arguments:
  505. # w - The spinbox window.
  506. proc ::tk::spinbox::AutoScan {w} {
  507. variable ::tk::Priv
  508. set x $Priv(x)
  509. if {$x >= [winfo width $w]} {
  510. $w xview scroll 2 units
  511. ::tk::spinbox::MouseSelect $w $x ignore
  512. } elseif {$x < 0} {
  513. $w xview scroll -2 units
  514. ::tk::spinbox::MouseSelect $w $x ignore
  515. }
  516. set Priv(afterId) [after 50 [list ::tk::spinbox::AutoScan $w]]
  517. }
  518. # ::tk::spinbox::GetSelection --
  519. #
  520. # Returns the selected text of the spinbox. Differs from entry in that
  521. # a spinbox has no -show option to obscure contents.
  522. #
  523. # Arguments:
  524. # w - The spinbox window from which the text to get
  525. proc ::tk::spinbox::GetSelection {w} {
  526. return [string range [$w get] [$w index sel.first] \
  527. [expr {[$w index sel.last] - 1}]]
  528. }