spinbox.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. bind Spinbox <2> {
  267. if {!$tk_strictMotif} {
  268. ::tk::EntryScanMark %W %x
  269. }
  270. }
  271. bind Spinbox <B2-Motion> {
  272. if {!$tk_strictMotif} {
  273. ::tk::EntryScanDrag %W %x
  274. }
  275. }
  276. # ::tk::spinbox::Invoke --
  277. # Invoke an element of the spinbox
  278. #
  279. # Arguments:
  280. # w - The spinbox window.
  281. # elem - Element to invoke
  282. proc ::tk::spinbox::Invoke {w elem} {
  283. variable ::tk::Priv
  284. if {![winfo exists $w]} {
  285. return
  286. }
  287. if {![info exists Priv(outsideElement)]} {
  288. $w invoke $elem
  289. incr Priv(repeated)
  290. }
  291. set delay [$w cget -repeatinterval]
  292. if {$delay > 0} {
  293. set Priv(afterId) [after $delay \
  294. [list ::tk::spinbox::Invoke $w $elem]]
  295. }
  296. }
  297. # ::tk::spinbox::ClosestGap --
  298. # Given x and y coordinates, this procedure finds the closest boundary
  299. # between characters to the given coordinates and returns the index
  300. # of the character just after the boundary.
  301. #
  302. # Arguments:
  303. # w - The spinbox window.
  304. # x - X-coordinate within the window.
  305. proc ::tk::spinbox::ClosestGap {w x} {
  306. set pos [$w index @$x]
  307. set bbox [$w bbox $pos]
  308. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  309. return $pos
  310. }
  311. incr pos
  312. }
  313. # ::tk::spinbox::ArrowPress --
  314. # This procedure is invoked to handle button-1 presses in buttonup
  315. # or buttondown elements of spinbox widgets.
  316. #
  317. # Arguments:
  318. # w - The spinbox window in which the button was pressed.
  319. # x - The x-coordinate of the button press.
  320. # y - The y-coordinate of the button press.
  321. proc ::tk::spinbox::ArrowPress {w x y} {
  322. variable ::tk::Priv
  323. if {[$w cget -state] ne "disabled" && \
  324. [string match "button*" $Priv(element)]} {
  325. $w selection element $Priv(element)
  326. set Priv(repeated) 0
  327. set Priv(relief) [$w cget -$Priv(element)relief]
  328. catch {after cancel $Priv(afterId)}
  329. set delay [$w cget -repeatdelay]
  330. if {$delay > 0} {
  331. set Priv(afterId) [after $delay \
  332. [list ::tk::spinbox::Invoke $w $Priv(element)]]
  333. }
  334. if {[info exists Priv(outsideElement)]} {
  335. unset Priv(outsideElement)
  336. }
  337. }
  338. }
  339. # ::tk::spinbox::ButtonDown --
  340. # This procedure is invoked to handle button-1 presses in spinbox
  341. # widgets. It moves the insertion cursor, sets the selection anchor,
  342. # and claims the input focus.
  343. #
  344. # Arguments:
  345. # w - The spinbox window in which the button was pressed.
  346. # x - The x-coordinate of the button press.
  347. # y - The y-coordinate of the button press.
  348. proc ::tk::spinbox::ButtonDown {w x y} {
  349. variable ::tk::Priv
  350. # Get the element that was clicked in. If we are not directly over
  351. # the spinbox, default to entry. This is necessary for spinbox grabs.
  352. #
  353. set Priv(element) [$w identify $x $y]
  354. if {$Priv(element) eq ""} {
  355. set Priv(element) "entry"
  356. }
  357. switch -exact $Priv(element) {
  358. "buttonup" - "buttondown" {
  359. ::tk::spinbox::ArrowPress $w $x $y
  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 -errorcode {TK SPINBOX UNKNOWN_ELEMENT} \
  372. "unknown spinbox element \"$Priv(element)\""
  373. }
  374. }
  375. }
  376. # ::tk::spinbox::ButtonUp --
  377. # This procedure is invoked to handle button-1 releases in spinbox
  378. # widgets.
  379. #
  380. # Arguments:
  381. # w - The spinbox window in which the button was pressed.
  382. # x - The x-coordinate of the button press.
  383. # y - The y-coordinate of the button press.
  384. proc ::tk::spinbox::ButtonUp {w x y} {
  385. variable ::tk::Priv
  386. ::tk::CancelRepeat
  387. # Priv(relief) may not exist if the ButtonUp is not paired with
  388. # a preceding ButtonDown
  389. if {[info exists Priv(element)] && [info exists Priv(relief)] && \
  390. [string match "button*" $Priv(element)]} {
  391. if {[info exists Priv(repeated)] && !$Priv(repeated)} {
  392. $w invoke $Priv(element)
  393. }
  394. $w configure -$Priv(element)relief $Priv(relief)
  395. $w selection element none
  396. }
  397. }
  398. # ::tk::spinbox::MouseSelect --
  399. # This procedure is invoked when dragging out a selection with
  400. # the mouse. Depending on the selection mode (character, word,
  401. # line) it selects in different-sized units. This procedure
  402. # ignores mouse motions initially until the mouse has moved from
  403. # one character to another or until there have been multiple clicks.
  404. #
  405. # Arguments:
  406. # w - The spinbox window in which the button was pressed.
  407. # x - The x-coordinate of the mouse.
  408. # cursor - optional place to set cursor.
  409. proc ::tk::spinbox::MouseSelect {w x {cursor {}}} {
  410. variable ::tk::Priv
  411. if {$Priv(element) ne "entry"} {
  412. # The ButtonUp command triggered by ButtonRelease-1 handles
  413. # invoking one of the spinbuttons.
  414. return
  415. }
  416. set cur [::tk::spinbox::ClosestGap $w $x]
  417. set anchor [$w index anchor]
  418. if {($cur ne $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
  419. set Priv(mouseMoved) 1
  420. }
  421. switch $Priv(selectMode) {
  422. char {
  423. if {$Priv(mouseMoved)} {
  424. if {$cur < $anchor} {
  425. $w selection range $cur $anchor
  426. } elseif {$cur > $anchor} {
  427. $w selection range $anchor $cur
  428. } else {
  429. $w selection clear
  430. }
  431. }
  432. }
  433. word {
  434. if {$cur < [$w index anchor]} {
  435. set before [tcl_wordBreakBefore [$w get] $cur]
  436. set after [tcl_wordBreakAfter [$w get] [expr {$anchor-1}]]
  437. } else {
  438. set before [tcl_wordBreakBefore [$w get] $anchor]
  439. set after [tcl_wordBreakAfter [$w get] [expr {$cur - 1}]]
  440. }
  441. if {$before < 0} {
  442. set before 0
  443. }
  444. if {$after < 0} {
  445. set after end
  446. }
  447. $w selection range $before $after
  448. }
  449. line {
  450. $w selection range 0 end
  451. }
  452. }
  453. if {$cursor ne {} && $cursor ne "ignore"} {
  454. catch {$w icursor $cursor}
  455. }
  456. update idletasks
  457. }
  458. # ::tk::spinbox::Paste --
  459. # This procedure sets the insertion cursor to the current mouse position,
  460. # pastes the selection there, and sets the focus to the window.
  461. #
  462. # Arguments:
  463. # w - The spinbox window.
  464. # x - X position of the mouse.
  465. proc ::tk::spinbox::Paste {w x} {
  466. $w icursor [::tk::spinbox::ClosestGap $w $x]
  467. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  468. if {"disabled" eq [$w cget -state]} {
  469. focus $w
  470. }
  471. }
  472. # ::tk::spinbox::Motion --
  473. # This procedure is invoked when the mouse moves in a spinbox window
  474. # with button 1 down.
  475. #
  476. # Arguments:
  477. # w - The spinbox window.
  478. # x - The x-coordinate of the mouse.
  479. # y - The y-coordinate of the mouse.
  480. proc ::tk::spinbox::Motion {w x y} {
  481. variable ::tk::Priv
  482. if {![info exists Priv(element)]} {
  483. set Priv(element) [$w identify $x $y]
  484. }
  485. set Priv(x) $x
  486. if {"entry" eq $Priv(element)} {
  487. ::tk::spinbox::MouseSelect $w $x ignore
  488. } elseif {[$w identify $x $y] ne $Priv(element)} {
  489. if {![info exists Priv(outsideElement)]} {
  490. # We've wandered out of the spin button
  491. # setting outside element will cause ::tk::spinbox::Invoke to
  492. # loop without doing anything
  493. set Priv(outsideElement) ""
  494. $w selection element none
  495. }
  496. } elseif {[info exists Priv(outsideElement)]} {
  497. unset Priv(outsideElement)
  498. $w selection element $Priv(element)
  499. }
  500. }
  501. # ::tk::spinbox::AutoScan --
  502. # This procedure is invoked when the mouse leaves an spinbox window
  503. # with button 1 down. It scrolls the window left or right,
  504. # depending on where the mouse is, and reschedules itself as an
  505. # "after" command so that the window continues to scroll until the
  506. # mouse moves back into the window or the mouse button is released.
  507. #
  508. # Arguments:
  509. # w - The spinbox window.
  510. proc ::tk::spinbox::AutoScan {w} {
  511. variable ::tk::Priv
  512. set x $Priv(x)
  513. if {$x >= [winfo width $w]} {
  514. $w xview scroll 2 units
  515. ::tk::spinbox::MouseSelect $w $x ignore
  516. } elseif {$x < 0} {
  517. $w xview scroll -2 units
  518. ::tk::spinbox::MouseSelect $w $x ignore
  519. }
  520. set Priv(afterId) [after 50 [list ::tk::spinbox::AutoScan $w]]
  521. }
  522. # ::tk::spinbox::GetSelection --
  523. #
  524. # Returns the selected text of the spinbox. Differs from entry in that
  525. # a spinbox has no -show option to obscure contents.
  526. #
  527. # Arguments:
  528. # w - The spinbox window from which the text to get
  529. proc ::tk::spinbox::GetSelection {w} {
  530. return [string range [$w get] [$w index sel.first] \
  531. [expr {[$w index sel.last] - 1}]]
  532. }