spinbox.tcl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <KeyPress> {
  194. ::tk::EntryInsert %W %A
  195. }
  196. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  197. # Otherwise, if a widget binding for one of these is defined, the
  198. # <KeyPress> class binding will also fire and insert the character,
  199. # which is wrong. Ditto for Escape, Return, and Tab.
  200. bind Spinbox <Alt-KeyPress> {# nothing}
  201. bind Spinbox <Meta-KeyPress> {# nothing}
  202. bind Spinbox <Control-KeyPress> {# 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-KeyPress> {# nothing}
  211. }
  212. # On Windows, paste is done using Shift-Insert. Shift-Insert already
  213. # generates the <<Paste>> event, so we don't need to do anything here.
  214. if {[tk windowingsystem] ne "win32"} {
  215. bind Spinbox <Insert> {
  216. catch {::tk::EntryInsert %W [::tk::GetSelection %W PRIMARY]}
  217. }
  218. }
  219. # Additional emacs-like bindings:
  220. bind Spinbox <Control-d> {
  221. if {!$tk_strictMotif} {
  222. %W delete insert
  223. }
  224. }
  225. bind Spinbox <Control-h> {
  226. if {!$tk_strictMotif} {
  227. ::tk::EntryBackspace %W
  228. }
  229. }
  230. bind Spinbox <Control-k> {
  231. if {!$tk_strictMotif} {
  232. %W delete insert end
  233. }
  234. }
  235. bind Spinbox <Control-t> {
  236. if {!$tk_strictMotif} {
  237. ::tk::EntryTranspose %W
  238. }
  239. }
  240. bind Spinbox <Meta-b> {
  241. if {!$tk_strictMotif} {
  242. ::tk::EntrySetCursor %W [::tk::EntryPreviousWord %W insert]
  243. }
  244. }
  245. bind Spinbox <Meta-d> {
  246. if {!$tk_strictMotif} {
  247. %W delete insert [::tk::EntryNextWord %W insert]
  248. }
  249. }
  250. bind Spinbox <Meta-f> {
  251. if {!$tk_strictMotif} {
  252. ::tk::EntrySetCursor %W [::tk::EntryNextWord %W insert]
  253. }
  254. }
  255. bind Spinbox <Meta-BackSpace> {
  256. if {!$tk_strictMotif} {
  257. %W delete [::tk::EntryPreviousWord %W insert] insert
  258. }
  259. }
  260. bind Spinbox <Meta-Delete> {
  261. if {!$tk_strictMotif} {
  262. %W delete [::tk::EntryPreviousWord %W insert] insert
  263. }
  264. }
  265. # A few additional bindings of my own.
  266. if {[tk windowingsystem] ne "aqua"} {
  267. bind Spinbox <2> {
  268. if {!$tk_strictMotif} {
  269. ::tk::EntryScanMark %W %x
  270. }
  271. }
  272. bind Spinbox <B2-Motion> {
  273. if {!$tk_strictMotif} {
  274. ::tk::EntryScanDrag %W %x
  275. }
  276. }
  277. } else {
  278. bind Spinbox <3> {
  279. if {!$tk_strictMotif} {
  280. ::tk::EntryScanMark %W %x
  281. }
  282. }
  283. bind Spinbox <B3-Motion> {
  284. if {!$tk_strictMotif} {
  285. ::tk::EntryScanDrag %W %x
  286. }
  287. }
  288. }
  289. # ::tk::spinbox::Invoke --
  290. # Invoke an element of the spinbox
  291. #
  292. # Arguments:
  293. # w - The spinbox window.
  294. # elem - Element to invoke
  295. proc ::tk::spinbox::Invoke {w elem} {
  296. variable ::tk::Priv
  297. if {![winfo exists $w]} {
  298. return
  299. }
  300. if {![info exists Priv(outsideElement)]} {
  301. $w invoke $elem
  302. incr Priv(repeated)
  303. }
  304. set delay [$w cget -repeatinterval]
  305. if {$delay > 0} {
  306. set Priv(afterId) [after $delay \
  307. [list ::tk::spinbox::Invoke $w $elem]]
  308. }
  309. }
  310. # ::tk::spinbox::ClosestGap --
  311. # Given x and y coordinates, this procedure finds the closest boundary
  312. # between characters to the given coordinates and returns the index
  313. # of the character just after the boundary.
  314. #
  315. # Arguments:
  316. # w - The spinbox window.
  317. # x - X-coordinate within the window.
  318. proc ::tk::spinbox::ClosestGap {w x} {
  319. set pos [$w index @$x]
  320. set bbox [$w bbox $pos]
  321. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  322. return $pos
  323. }
  324. incr pos
  325. }
  326. # ::tk::spinbox::ArrowPress --
  327. # This procedure is invoked to handle button-1 presses in buttonup
  328. # or buttondown elements of spinbox widgets.
  329. #
  330. # Arguments:
  331. # w - The spinbox window in which the button was pressed.
  332. # x - The x-coordinate of the button press.
  333. # y - The y-coordinate of the button press.
  334. proc ::tk::spinbox::ArrowPress {w x y} {
  335. variable ::tk::Priv
  336. if {[$w cget -state] ne "disabled" && \
  337. [string match "button*" $Priv(element)]} {
  338. $w selection element $Priv(element)
  339. set Priv(repeated) 0
  340. set Priv(relief) [$w cget -$Priv(element)relief]
  341. catch {after cancel $Priv(afterId)}
  342. set delay [$w cget -repeatdelay]
  343. if {$delay > 0} {
  344. set Priv(afterId) [after $delay \
  345. [list ::tk::spinbox::Invoke $w $Priv(element)]]
  346. }
  347. if {[info exists Priv(outsideElement)]} {
  348. unset Priv(outsideElement)
  349. }
  350. }
  351. }
  352. # ::tk::spinbox::ButtonDown --
  353. # This procedure is invoked to handle button-1 presses in spinbox
  354. # widgets. It moves the insertion cursor, sets the selection anchor,
  355. # and claims the input focus.
  356. #
  357. # Arguments:
  358. # w - The spinbox window in which the button was pressed.
  359. # x - The x-coordinate of the button press.
  360. # y - The y-coordinate of the button press.
  361. proc ::tk::spinbox::ButtonDown {w x y} {
  362. variable ::tk::Priv
  363. # Get the element that was clicked in. If we are not directly over
  364. # the spinbox, default to entry. This is necessary for spinbox grabs.
  365. #
  366. set Priv(element) [$w identify $x $y]
  367. if {$Priv(element) eq ""} {
  368. set Priv(element) "entry"
  369. }
  370. switch -exact $Priv(element) {
  371. "buttonup" - "buttondown" {
  372. ::tk::spinbox::ArrowPress $w $x $y
  373. }
  374. "entry" {
  375. set Priv(selectMode) char
  376. set Priv(mouseMoved) 0
  377. set Priv(pressX) $x
  378. $w icursor [::tk::spinbox::ClosestGap $w $x]
  379. $w selection from insert
  380. if {"disabled" ne [$w cget -state]} {focus $w}
  381. $w selection clear
  382. }
  383. default {
  384. return -code error -errorcode {TK SPINBOX UNKNOWN_ELEMENT} \
  385. "unknown spinbox element \"$Priv(element)\""
  386. }
  387. }
  388. }
  389. # ::tk::spinbox::ButtonUp --
  390. # This procedure is invoked to handle button-1 releases in spinbox
  391. # widgets.
  392. #
  393. # Arguments:
  394. # w - The spinbox window in which the button was pressed.
  395. # x - The x-coordinate of the button press.
  396. # y - The y-coordinate of the button press.
  397. proc ::tk::spinbox::ButtonUp {w x y} {
  398. variable ::tk::Priv
  399. ::tk::CancelRepeat
  400. # Priv(relief) may not exist if the ButtonUp is not paired with
  401. # a preceding ButtonDown
  402. if {[info exists Priv(element)] && [info exists Priv(relief)] && \
  403. [string match "button*" $Priv(element)]} {
  404. if {[info exists Priv(repeated)] && !$Priv(repeated)} {
  405. $w invoke $Priv(element)
  406. }
  407. $w configure -$Priv(element)relief $Priv(relief)
  408. $w selection element none
  409. }
  410. }
  411. # ::tk::spinbox::MouseSelect --
  412. # This procedure is invoked when dragging out a selection with
  413. # the mouse. Depending on the selection mode (character, word,
  414. # line) it selects in different-sized units. This procedure
  415. # ignores mouse motions initially until the mouse has moved from
  416. # one character to another or until there have been multiple clicks.
  417. #
  418. # Arguments:
  419. # w - The spinbox window in which the button was pressed.
  420. # x - The x-coordinate of the mouse.
  421. # cursor - optional place to set cursor.
  422. proc ::tk::spinbox::MouseSelect {w x {cursor {}}} {
  423. variable ::tk::Priv
  424. if {$Priv(element) ne "entry"} {
  425. # The ButtonUp command triggered by ButtonRelease-1 handles
  426. # invoking one of the spinbuttons.
  427. return
  428. }
  429. set cur [::tk::spinbox::ClosestGap $w $x]
  430. set anchor [$w index anchor]
  431. if {($cur ne $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
  432. set Priv(mouseMoved) 1
  433. }
  434. switch $Priv(selectMode) {
  435. char {
  436. if {$Priv(mouseMoved)} {
  437. if {$cur < $anchor} {
  438. $w selection range $cur $anchor
  439. } elseif {$cur > $anchor} {
  440. $w selection range $anchor $cur
  441. } else {
  442. $w selection clear
  443. }
  444. }
  445. }
  446. word {
  447. if {$cur < [$w index anchor]} {
  448. set before [tcl_wordBreakBefore [$w get] $cur]
  449. set after [tcl_wordBreakAfter [$w get] $anchor-1]
  450. } else {
  451. set before [tcl_wordBreakBefore [$w get] $anchor]
  452. set after [tcl_wordBreakAfter [$w get] $cur-1]
  453. }
  454. if {$before < 0} {
  455. set before 0
  456. }
  457. if {$after < 0} {
  458. set after end
  459. }
  460. $w selection range $before $after
  461. }
  462. line {
  463. $w selection range 0 end
  464. }
  465. }
  466. if {$cursor ne {} && $cursor ne "ignore"} {
  467. catch {$w icursor $cursor}
  468. }
  469. update idletasks
  470. }
  471. # ::tk::spinbox::Paste --
  472. # This procedure sets the insertion cursor to the current mouse position,
  473. # pastes the selection there, and sets the focus to the window.
  474. #
  475. # Arguments:
  476. # w - The spinbox window.
  477. # x - X position of the mouse.
  478. proc ::tk::spinbox::Paste {w x} {
  479. $w icursor [::tk::spinbox::ClosestGap $w $x]
  480. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  481. if {"disabled" eq [$w cget -state]} {
  482. focus $w
  483. }
  484. }
  485. # ::tk::spinbox::Motion --
  486. # This procedure is invoked when the mouse moves in a spinbox window
  487. # with button 1 down.
  488. #
  489. # Arguments:
  490. # w - The spinbox window.
  491. # x - The x-coordinate of the mouse.
  492. # y - The y-coordinate of the mouse.
  493. proc ::tk::spinbox::Motion {w x y} {
  494. variable ::tk::Priv
  495. if {![info exists Priv(element)]} {
  496. set Priv(element) [$w identify $x $y]
  497. }
  498. set Priv(x) $x
  499. if {"entry" eq $Priv(element)} {
  500. ::tk::spinbox::MouseSelect $w $x ignore
  501. } elseif {[$w identify $x $y] ne $Priv(element)} {
  502. if {![info exists Priv(outsideElement)]} {
  503. # We've wandered out of the spin button
  504. # setting outside element will cause ::tk::spinbox::Invoke to
  505. # loop without doing anything
  506. set Priv(outsideElement) ""
  507. $w selection element none
  508. }
  509. } elseif {[info exists Priv(outsideElement)]} {
  510. unset Priv(outsideElement)
  511. $w selection element $Priv(element)
  512. }
  513. }
  514. # ::tk::spinbox::AutoScan --
  515. # This procedure is invoked when the mouse leaves an spinbox window
  516. # with button 1 down. It scrolls the window left or right,
  517. # depending on where the mouse is, and reschedules itself as an
  518. # "after" command so that the window continues to scroll until the
  519. # mouse moves back into the window or the mouse button is released.
  520. #
  521. # Arguments:
  522. # w - The spinbox window.
  523. proc ::tk::spinbox::AutoScan {w} {
  524. variable ::tk::Priv
  525. set x $Priv(x)
  526. if {$x >= [winfo width $w]} {
  527. $w xview scroll 2 units
  528. ::tk::spinbox::MouseSelect $w $x ignore
  529. } elseif {$x < 0} {
  530. $w xview scroll -2 units
  531. ::tk::spinbox::MouseSelect $w $x ignore
  532. }
  533. set Priv(afterId) [after 50 [list ::tk::spinbox::AutoScan $w]]
  534. }
  535. # ::tk::spinbox::GetSelection --
  536. #
  537. # Returns the selected text of the spinbox. Differs from entry in that
  538. # a spinbox has no -show option to obscure contents.
  539. #
  540. # Arguments:
  541. # w - The spinbox window from which the text to get
  542. proc ::tk::spinbox::GetSelection {w} {
  543. return [string range [$w get] [$w index sel.first] \
  544. [expr {[$w index sel.last] - 1}]]
  545. }