entry.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. # entry.tcl --
  2. #
  3. # This file defines the default bindings for Tk entry widgets and provides
  4. # procedures that help in implementing those bindings.
  5. #
  6. # Copyright (c) 1992-1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. #-------------------------------------------------------------------------
  13. # Elements of tk::Priv that are used in this file:
  14. #
  15. # afterId - If non-null, it means that auto-scanning is underway
  16. # and it gives the "after" id for the next auto-scan
  17. # command to be executed.
  18. # mouseMoved - Non-zero means the mouse has moved a significant
  19. # amount since the button went down (so, for example,
  20. # start dragging out a selection).
  21. # pressX - X-coordinate at which the mouse button was pressed.
  22. # selectMode - The style of selection currently underway:
  23. # char, word, or line.
  24. # x, y - Last known mouse coordinates for scanning
  25. # and auto-scanning.
  26. # data - Used for Cut and Copy
  27. #-------------------------------------------------------------------------
  28. #-------------------------------------------------------------------------
  29. # The code below creates the default class bindings for entries.
  30. #-------------------------------------------------------------------------
  31. bind Entry <<Cut>> {
  32. if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
  33. clipboard clear -displayof %W
  34. clipboard append -displayof %W $tk::Priv(data)
  35. %W delete sel.first sel.last
  36. unset tk::Priv(data)
  37. }
  38. }
  39. bind Entry <<Copy>> {
  40. if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
  41. clipboard clear -displayof %W
  42. clipboard append -displayof %W $tk::Priv(data)
  43. unset tk::Priv(data)
  44. }
  45. }
  46. bind Entry <<Paste>> {
  47. global tcl_platform
  48. catch {
  49. if {[tk windowingsystem] ne "x11"} {
  50. catch {
  51. %W delete sel.first sel.last
  52. }
  53. }
  54. %W insert insert [::tk::GetSelection %W CLIPBOARD]
  55. tk::EntrySeeInsert %W
  56. }
  57. }
  58. bind Entry <<Clear>> {
  59. # ignore if there is no selection
  60. catch { %W delete sel.first sel.last }
  61. }
  62. bind Entry <<PasteSelection>> {
  63. if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
  64. || !$tk::Priv(mouseMoved)} {
  65. tk::EntryPaste %W %x
  66. }
  67. }
  68. bind Entry <<TraverseIn>> {
  69. %W selection range 0 end
  70. %W icursor end
  71. }
  72. # Standard Motif bindings:
  73. bind Entry <1> {
  74. tk::EntryButton1 %W %x
  75. %W selection clear
  76. }
  77. bind Entry <B1-Motion> {
  78. set tk::Priv(x) %x
  79. tk::EntryMouseSelect %W %x
  80. }
  81. bind Entry <Double-1> {
  82. set tk::Priv(selectMode) word
  83. tk::EntryMouseSelect %W %x
  84. catch {%W icursor sel.last}
  85. }
  86. bind Entry <Triple-1> {
  87. set tk::Priv(selectMode) line
  88. tk::EntryMouseSelect %W %x
  89. catch {%W icursor sel.last}
  90. }
  91. bind Entry <Shift-1> {
  92. set tk::Priv(selectMode) char
  93. %W selection adjust @%x
  94. }
  95. bind Entry <Double-Shift-1> {
  96. set tk::Priv(selectMode) word
  97. tk::EntryMouseSelect %W %x
  98. }
  99. bind Entry <Triple-Shift-1> {
  100. set tk::Priv(selectMode) line
  101. tk::EntryMouseSelect %W %x
  102. }
  103. bind Entry <B1-Leave> {
  104. set tk::Priv(x) %x
  105. tk::EntryAutoScan %W
  106. }
  107. bind Entry <B1-Enter> {
  108. tk::CancelRepeat
  109. }
  110. bind Entry <ButtonRelease-1> {
  111. tk::CancelRepeat
  112. }
  113. bind Entry <Control-1> {
  114. %W icursor @%x
  115. }
  116. bind Entry <Left> {
  117. tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
  118. }
  119. bind Entry <Right> {
  120. tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
  121. }
  122. bind Entry <Shift-Left> {
  123. tk::EntryKeySelect %W [expr {[%W index insert] - 1}]
  124. tk::EntrySeeInsert %W
  125. }
  126. bind Entry <Shift-Right> {
  127. tk::EntryKeySelect %W [expr {[%W index insert] + 1}]
  128. tk::EntrySeeInsert %W
  129. }
  130. bind Entry <Control-Left> {
  131. tk::EntrySetCursor %W [tk::EntryPreviousWord %W insert]
  132. }
  133. bind Entry <Control-Right> {
  134. tk::EntrySetCursor %W [tk::EntryNextWord %W insert]
  135. }
  136. bind Entry <Shift-Control-Left> {
  137. tk::EntryKeySelect %W [tk::EntryPreviousWord %W insert]
  138. tk::EntrySeeInsert %W
  139. }
  140. bind Entry <Shift-Control-Right> {
  141. tk::EntryKeySelect %W [tk::EntryNextWord %W insert]
  142. tk::EntrySeeInsert %W
  143. }
  144. bind Entry <Home> {
  145. tk::EntrySetCursor %W 0
  146. }
  147. bind Entry <Shift-Home> {
  148. tk::EntryKeySelect %W 0
  149. tk::EntrySeeInsert %W
  150. }
  151. bind Entry <End> {
  152. tk::EntrySetCursor %W end
  153. }
  154. bind Entry <Shift-End> {
  155. tk::EntryKeySelect %W end
  156. tk::EntrySeeInsert %W
  157. }
  158. bind Entry <Delete> {
  159. if {[%W selection present]} {
  160. %W delete sel.first sel.last
  161. } else {
  162. %W delete insert
  163. }
  164. }
  165. bind Entry <BackSpace> {
  166. tk::EntryBackspace %W
  167. }
  168. bind Entry <Control-space> {
  169. %W selection from insert
  170. }
  171. bind Entry <Select> {
  172. %W selection from insert
  173. }
  174. bind Entry <Control-Shift-space> {
  175. %W selection adjust insert
  176. }
  177. bind Entry <Shift-Select> {
  178. %W selection adjust insert
  179. }
  180. bind Entry <Control-slash> {
  181. %W selection range 0 end
  182. }
  183. bind Entry <Control-backslash> {
  184. %W selection clear
  185. }
  186. bind Entry <KeyPress> {
  187. tk::CancelRepeat
  188. tk::EntryInsert %W %A
  189. }
  190. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  191. # Otherwise, if a widget binding for one of these is defined, the
  192. # <KeyPress> class binding will also fire and insert the character,
  193. # which is wrong. Ditto for Escape, Return, and Tab.
  194. bind Entry <Alt-KeyPress> {# nothing}
  195. bind Entry <Meta-KeyPress> {# nothing}
  196. bind Entry <Control-KeyPress> {# nothing}
  197. bind Entry <Escape> {# nothing}
  198. bind Entry <Return> {# nothing}
  199. bind Entry <KP_Enter> {# nothing}
  200. bind Entry <Tab> {# nothing}
  201. if {[tk windowingsystem] eq "aqua"} {
  202. bind Entry <Command-KeyPress> {# nothing}
  203. }
  204. # On Windows, paste is done using Shift-Insert. Shift-Insert already
  205. # generates the <<Paste>> event, so we don't need to do anything here.
  206. if {[tk windowingsystem] ne "win32"} {
  207. bind Entry <Insert> {
  208. catch {tk::EntryInsert %W [::tk::GetSelection %W PRIMARY]}
  209. }
  210. }
  211. # Additional emacs-like bindings:
  212. bind Entry <Control-a> {
  213. if {!$tk_strictMotif} {
  214. tk::EntrySetCursor %W 0
  215. }
  216. }
  217. bind Entry <Control-b> {
  218. if {!$tk_strictMotif} {
  219. tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
  220. }
  221. }
  222. bind Entry <Control-d> {
  223. if {!$tk_strictMotif} {
  224. %W delete insert
  225. }
  226. }
  227. bind Entry <Control-e> {
  228. if {!$tk_strictMotif} {
  229. tk::EntrySetCursor %W end
  230. }
  231. }
  232. bind Entry <Control-f> {
  233. if {!$tk_strictMotif} {
  234. tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
  235. }
  236. }
  237. bind Entry <Control-h> {
  238. if {!$tk_strictMotif} {
  239. tk::EntryBackspace %W
  240. }
  241. }
  242. bind Entry <Control-k> {
  243. if {!$tk_strictMotif} {
  244. %W delete insert end
  245. }
  246. }
  247. bind Entry <Control-t> {
  248. if {!$tk_strictMotif} {
  249. tk::EntryTranspose %W
  250. }
  251. }
  252. bind Entry <Meta-b> {
  253. if {!$tk_strictMotif} {
  254. tk::EntrySetCursor %W [tk::EntryPreviousWord %W insert]
  255. }
  256. }
  257. bind Entry <Meta-d> {
  258. if {!$tk_strictMotif} {
  259. %W delete insert [tk::EntryNextWord %W insert]
  260. }
  261. }
  262. bind Entry <Meta-f> {
  263. if {!$tk_strictMotif} {
  264. tk::EntrySetCursor %W [tk::EntryNextWord %W insert]
  265. }
  266. }
  267. bind Entry <Meta-BackSpace> {
  268. if {!$tk_strictMotif} {
  269. %W delete [tk::EntryPreviousWord %W insert] insert
  270. }
  271. }
  272. bind Entry <Meta-Delete> {
  273. if {!$tk_strictMotif} {
  274. %W delete [tk::EntryPreviousWord %W insert] insert
  275. }
  276. }
  277. # A few additional bindings of my own.
  278. bind Entry <2> {
  279. if {!$tk_strictMotif} {
  280. ::tk::EntryScanMark %W %x
  281. }
  282. }
  283. bind Entry <B2-Motion> {
  284. if {!$tk_strictMotif} {
  285. ::tk::EntryScanDrag %W %x
  286. }
  287. }
  288. # ::tk::EntryClosestGap --
  289. # Given x and y coordinates, this procedure finds the closest boundary
  290. # between characters to the given coordinates and returns the index
  291. # of the character just after the boundary.
  292. #
  293. # Arguments:
  294. # w - The entry window.
  295. # x - X-coordinate within the window.
  296. proc ::tk::EntryClosestGap {w x} {
  297. set pos [$w index @$x]
  298. set bbox [$w bbox $pos]
  299. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  300. return $pos
  301. }
  302. incr pos
  303. }
  304. # ::tk::EntryButton1 --
  305. # This procedure is invoked to handle button-1 presses in entry
  306. # widgets. It moves the insertion cursor, sets the selection anchor,
  307. # and claims the input focus.
  308. #
  309. # Arguments:
  310. # w - The entry window in which the button was pressed.
  311. # x - The x-coordinate of the button press.
  312. proc ::tk::EntryButton1 {w x} {
  313. variable ::tk::Priv
  314. set Priv(selectMode) char
  315. set Priv(mouseMoved) 0
  316. set Priv(pressX) $x
  317. $w icursor [EntryClosestGap $w $x]
  318. $w selection from insert
  319. if {"disabled" ne [$w cget -state]} {
  320. focus $w
  321. }
  322. }
  323. # ::tk::EntryMouseSelect --
  324. # This procedure is invoked when dragging out a selection with
  325. # the mouse. Depending on the selection mode (character, word,
  326. # line) it selects in different-sized units. This procedure
  327. # ignores mouse motions initially until the mouse has moved from
  328. # one character to another or until there have been multiple clicks.
  329. #
  330. # Arguments:
  331. # w - The entry window in which the button was pressed.
  332. # x - The x-coordinate of the mouse.
  333. proc ::tk::EntryMouseSelect {w x} {
  334. variable ::tk::Priv
  335. set cur [EntryClosestGap $w $x]
  336. set anchor [$w index anchor]
  337. if {($cur != $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
  338. set Priv(mouseMoved) 1
  339. }
  340. switch $Priv(selectMode) {
  341. char {
  342. if {$Priv(mouseMoved)} {
  343. if {$cur < $anchor} {
  344. $w selection range $cur $anchor
  345. } elseif {$cur > $anchor} {
  346. $w selection range $anchor $cur
  347. } else {
  348. $w selection clear
  349. }
  350. }
  351. }
  352. word {
  353. if {$cur < $anchor} {
  354. set before [tcl_wordBreakBefore [$w get] $cur]
  355. set after [tcl_wordBreakAfter [$w get] [expr {$anchor-1}]]
  356. } elseif {$cur > $anchor} {
  357. set before [tcl_wordBreakBefore [$w get] $anchor]
  358. set after [tcl_wordBreakAfter [$w get] [expr {$cur - 1}]]
  359. } else {
  360. if {[$w index @$Priv(pressX)] < $anchor} {
  361. incr anchor -1
  362. }
  363. set before [tcl_wordBreakBefore [$w get] $anchor]
  364. set after [tcl_wordBreakAfter [$w get] $anchor]
  365. }
  366. if {$before < 0} {
  367. set before 0
  368. }
  369. if {$after < 0} {
  370. set after end
  371. }
  372. $w selection range $before $after
  373. }
  374. line {
  375. $w selection range 0 end
  376. }
  377. }
  378. if {$Priv(mouseMoved)} {
  379. $w icursor $cur
  380. }
  381. update idletasks
  382. }
  383. # ::tk::EntryPaste --
  384. # This procedure sets the insertion cursor to the current mouse position,
  385. # pastes the selection there, and sets the focus to the window.
  386. #
  387. # Arguments:
  388. # w - The entry window.
  389. # x - X position of the mouse.
  390. proc ::tk::EntryPaste {w x} {
  391. $w icursor [EntryClosestGap $w $x]
  392. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  393. if {"disabled" ne [$w cget -state]} {
  394. focus $w
  395. }
  396. }
  397. # ::tk::EntryAutoScan --
  398. # This procedure is invoked when the mouse leaves an entry window
  399. # with button 1 down. It scrolls the window left or right,
  400. # depending on where the mouse is, and reschedules itself as an
  401. # "after" command so that the window continues to scroll until the
  402. # mouse moves back into the window or the mouse button is released.
  403. #
  404. # Arguments:
  405. # w - The entry window.
  406. proc ::tk::EntryAutoScan {w} {
  407. variable ::tk::Priv
  408. set x $Priv(x)
  409. if {![winfo exists $w]} {
  410. return
  411. }
  412. if {$x >= [winfo width $w]} {
  413. $w xview scroll 2 units
  414. EntryMouseSelect $w $x
  415. } elseif {$x < 0} {
  416. $w xview scroll -2 units
  417. EntryMouseSelect $w $x
  418. }
  419. set Priv(afterId) [after 50 [list tk::EntryAutoScan $w]]
  420. }
  421. # ::tk::EntryKeySelect --
  422. # This procedure is invoked when stroking out selections using the
  423. # keyboard. It moves the cursor to a new position, then extends
  424. # the selection to that position.
  425. #
  426. # Arguments:
  427. # w - The entry window.
  428. # new - A new position for the insertion cursor (the cursor hasn't
  429. # actually been moved to this position yet).
  430. proc ::tk::EntryKeySelect {w new} {
  431. if {![$w selection present]} {
  432. $w selection from insert
  433. $w selection to $new
  434. } else {
  435. $w selection adjust $new
  436. }
  437. $w icursor $new
  438. }
  439. # ::tk::EntryInsert --
  440. # Insert a string into an entry at the point of the insertion cursor.
  441. # If there is a selection in the entry, and it covers the point of the
  442. # insertion cursor, then delete the selection before inserting.
  443. #
  444. # Arguments:
  445. # w - The entry window in which to insert the string
  446. # s - The string to insert (usually just a single character)
  447. proc ::tk::EntryInsert {w s} {
  448. if {$s eq ""} {
  449. return
  450. }
  451. catch {
  452. set insert [$w index insert]
  453. if {([$w index sel.first] <= $insert)
  454. && ([$w index sel.last] >= $insert)} {
  455. $w delete sel.first sel.last
  456. }
  457. }
  458. $w insert insert $s
  459. EntrySeeInsert $w
  460. }
  461. # ::tk::EntryBackspace --
  462. # Backspace over the character just before the insertion cursor.
  463. # If backspacing would move the cursor off the left edge of the
  464. # window, reposition the cursor at about the middle of the window.
  465. #
  466. # Arguments:
  467. # w - The entry window in which to backspace.
  468. proc ::tk::EntryBackspace w {
  469. if {[$w selection present]} {
  470. $w delete sel.first sel.last
  471. } else {
  472. set x [expr {[$w index insert] - 1}]
  473. if {$x >= 0} {
  474. $w delete $x
  475. }
  476. if {[$w index @0] >= [$w index insert]} {
  477. set range [$w xview]
  478. set left [lindex $range 0]
  479. set right [lindex $range 1]
  480. $w xview moveto [expr {$left - ($right - $left)/2.0}]
  481. }
  482. }
  483. }
  484. # ::tk::EntrySeeInsert --
  485. # Make sure that the insertion cursor is visible in the entry window.
  486. # If not, adjust the view so that it is.
  487. #
  488. # Arguments:
  489. # w - The entry window.
  490. proc ::tk::EntrySeeInsert w {
  491. set c [$w index insert]
  492. if {($c < [$w index @0]) || ($c > [$w index @[winfo width $w]])} {
  493. $w xview $c
  494. }
  495. }
  496. # ::tk::EntrySetCursor -
  497. # Move the insertion cursor to a given position in an entry. Also
  498. # clears the selection, if there is one in the entry, and makes sure
  499. # that the insertion cursor is visible.
  500. #
  501. # Arguments:
  502. # w - The entry window.
  503. # pos - The desired new position for the cursor in the window.
  504. proc ::tk::EntrySetCursor {w pos} {
  505. $w icursor $pos
  506. $w selection clear
  507. EntrySeeInsert $w
  508. }
  509. # ::tk::EntryTranspose -
  510. # This procedure implements the "transpose" function for entry widgets.
  511. # It tranposes the characters on either side of the insertion cursor,
  512. # unless the cursor is at the end of the line. In this case it
  513. # transposes the two characters to the left of the cursor. In either
  514. # case, the cursor ends up to the right of the transposed characters.
  515. #
  516. # Arguments:
  517. # w - The entry window.
  518. proc ::tk::EntryTranspose w {
  519. set i [$w index insert]
  520. if {$i < [$w index end]} {
  521. incr i
  522. }
  523. set first [expr {$i-2}]
  524. if {$first < 0} {
  525. return
  526. }
  527. set data [$w get]
  528. set new [string index $data [expr {$i-1}]][string index $data $first]
  529. $w delete $first $i
  530. $w insert insert $new
  531. EntrySeeInsert $w
  532. }
  533. # ::tk::EntryNextWord --
  534. # Returns the index of the next word position after a given position in the
  535. # entry. The next word is platform dependent and may be either the next
  536. # end-of-word position or the next start-of-word position after the next
  537. # end-of-word position.
  538. #
  539. # Arguments:
  540. # w - The entry window in which the cursor is to move.
  541. # start - Position at which to start search.
  542. if {[tk windowingsystem] eq "win32"} {
  543. proc ::tk::EntryNextWord {w start} {
  544. set pos [tcl_endOfWord [$w get] [$w index $start]]
  545. if {$pos >= 0} {
  546. set pos [tcl_startOfNextWord [$w get] $pos]
  547. }
  548. if {$pos < 0} {
  549. return end
  550. }
  551. return $pos
  552. }
  553. } else {
  554. proc ::tk::EntryNextWord {w start} {
  555. set pos [tcl_endOfWord [$w get] [$w index $start]]
  556. if {$pos < 0} {
  557. return end
  558. }
  559. return $pos
  560. }
  561. }
  562. # ::tk::EntryPreviousWord --
  563. #
  564. # Returns the index of the previous word position before a given
  565. # position in the entry.
  566. #
  567. # Arguments:
  568. # w - The entry window in which the cursor is to move.
  569. # start - Position at which to start search.
  570. proc ::tk::EntryPreviousWord {w start} {
  571. set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
  572. if {$pos < 0} {
  573. return 0
  574. }
  575. return $pos
  576. }
  577. # ::tk::EntryScanMark --
  578. #
  579. # Marks the start of a possible scan drag operation
  580. #
  581. # Arguments:
  582. # w - The entry window from which the text to get
  583. # x - x location on screen
  584. proc ::tk::EntryScanMark {w x} {
  585. $w scan mark $x
  586. set ::tk::Priv(x) $x
  587. set ::tk::Priv(y) 0 ; # not used
  588. set ::tk::Priv(mouseMoved) 0
  589. }
  590. # ::tk::EntryScanDrag --
  591. #
  592. # Marks the start of a possible scan drag operation
  593. #
  594. # Arguments:
  595. # w - The entry window from which the text to get
  596. # x - x location on screen
  597. proc ::tk::EntryScanDrag {w x} {
  598. # Make sure these exist, as some weird situations can trigger the
  599. # motion binding without the initial press. [Bug #220269]
  600. if {![info exists ::tk::Priv(x)]} { set ::tk::Priv(x) $x }
  601. # allow for a delta
  602. if {abs($x-$::tk::Priv(x)) > 2} {
  603. set ::tk::Priv(mouseMoved) 1
  604. }
  605. $w scan dragto $x
  606. }
  607. # ::tk::EntryGetSelection --
  608. #
  609. # Returns the selected text of the entry with respect to the -show option.
  610. #
  611. # Arguments:
  612. # w - The entry window from which the text to get
  613. proc ::tk::EntryGetSelection {w} {
  614. set entryString [string range [$w get] [$w index sel.first] \
  615. [expr {[$w index sel.last] - 1}]]
  616. if {[$w cget -show] ne ""} {
  617. return [string repeat [string index [$w cget -show] 0] \
  618. [string length $entryString]]
  619. }
  620. return $entryString
  621. }