entry.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. #
  2. # DERIVED FROM: tk/library/entry.tcl r1.22
  3. #
  4. # Copyright (c) 1992-1994 The Regents of the University of California.
  5. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  6. # Copyright (c) 2004, Joe English
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11. namespace eval ttk {
  12. namespace eval entry {
  13. variable State
  14. set State(x) 0
  15. set State(selectMode) none
  16. set State(anchor) 0
  17. set State(scanX) 0
  18. set State(scanIndex) 0
  19. set State(scanMoved) 0
  20. # Button-2 scan speed is (scanNum/scanDen) characters
  21. # per pixel of mouse movement.
  22. # The standard Tk entry widget uses the equivalent of
  23. # scanNum = 10, scanDen = average character width.
  24. # I don't know why that was chosen.
  25. #
  26. set State(scanNum) 1
  27. set State(scanDen) 1
  28. set State(deadband) 3 ;# #pixels for mouse-moved deadband.
  29. }
  30. }
  31. ### Option database settings.
  32. #
  33. option add *TEntry.cursor [ttk::cursor text] widgetDefault
  34. ### Bindings.
  35. #
  36. # Removed the following standard Tk bindings:
  37. #
  38. # <Control-space>, <Control-Shift-space>,
  39. # <Select>, <Shift-Select>:
  40. # Ttk entry widget doesn't use selection anchor.
  41. # <Insert>:
  42. # Inserts PRIMARY selection (on non-Windows platforms).
  43. # This is inconsistent with typical platform bindings.
  44. # <Double-Shift-Button-1>, <Triple-Shift-Button-1>:
  45. # These don't do the right thing to start with.
  46. # <Meta-b>, <Meta-d>, <Meta-f>,
  47. # <Meta-BackSpace>, <Meta-Delete>:
  48. # Judgment call. If <Meta> happens to be assigned to the Alt key,
  49. # these could conflict with application accelerators.
  50. # (Plus, who has a Meta key these days?)
  51. # <Control-t>:
  52. # Another judgment call. If anyone misses this, let me know
  53. # and I'll put it back.
  54. #
  55. ## Clipboard events:
  56. #
  57. bind TEntry <<Cut>> { ttk::entry::Cut %W }
  58. bind TEntry <<Copy>> { ttk::entry::Copy %W }
  59. bind TEntry <<Paste>> { ttk::entry::Paste %W }
  60. bind TEntry <<Clear>> { ttk::entry::Clear %W }
  61. ## Button1 bindings:
  62. # Used for selection and navigation.
  63. #
  64. bind TEntry <Button-1> { ttk::entry::Press %W %x }
  65. bind TEntry <Shift-Button-1> { ttk::entry::Shift-Press %W %x }
  66. bind TEntry <Double-Button-1> { ttk::entry::Select %W %x word }
  67. bind TEntry <Triple-Button-1> { ttk::entry::Select %W %x line }
  68. bind TEntry <B1-Motion> { ttk::entry::Drag %W %x }
  69. bind TEntry <B1-Leave> { ttk::entry::DragOut %W %m }
  70. bind TEntry <B1-Enter> { ttk::entry::DragIn %W }
  71. bind TEntry <ButtonRelease-1> { ttk::entry::Release %W }
  72. bind TEntry <<ToggleSelection>> {
  73. %W instate {!readonly !disabled} { %W icursor @%x ; focus %W }
  74. }
  75. ## Button2 (Button3 on Aqua) bindings:
  76. # Used for scanning and primary transfer.
  77. # Note: ButtonRelease-2 (ButtonRelease-3 on Aqua)
  78. # is mapped to <<PasteSelection>> in tk.tcl.
  79. #
  80. if {[tk windowingsystem] ne "aqua"} {
  81. bind TEntry <Button-2> { ttk::entry::ScanMark %W %x }
  82. bind TEntry <B2-Motion> { ttk::entry::ScanDrag %W %x }
  83. bind TEntry <ButtonRelease-2> { ttk::entry::ScanRelease %W %x }
  84. } else {
  85. bind TEntry <Button-3> { ttk::entry::ScanMark %W %x }
  86. bind TEntry <B3-Motion> { ttk::entry::ScanDrag %W %x }
  87. bind TEntry <ButtonRelease-3> { ttk::entry::ScanRelease %W %x }
  88. }
  89. bind TEntry <<PasteSelection>> { ttk::entry::ScanRelease %W %x }
  90. ## Keyboard navigation bindings:
  91. #
  92. bind TEntry <<PrevChar>> { ttk::entry::Move %W prevchar }
  93. bind TEntry <<NextChar>> { ttk::entry::Move %W nextchar }
  94. bind TEntry <<PrevWord>> { ttk::entry::Move %W prevword }
  95. bind TEntry <<NextWord>> { ttk::entry::Move %W nextword }
  96. bind TEntry <<LineStart>> { ttk::entry::Move %W home }
  97. bind TEntry <<LineEnd>> { ttk::entry::Move %W end }
  98. bind TEntry <<SelectPrevChar>> { ttk::entry::Extend %W prevchar }
  99. bind TEntry <<SelectNextChar>> { ttk::entry::Extend %W nextchar }
  100. bind TEntry <<SelectPrevWord>> { ttk::entry::Extend %W prevword }
  101. bind TEntry <<SelectNextWord>> { ttk::entry::Extend %W nextword }
  102. bind TEntry <<SelectLineStart>> { ttk::entry::Extend %W home }
  103. bind TEntry <<SelectLineEnd>> { ttk::entry::Extend %W end }
  104. bind TEntry <<SelectAll>> { %W selection range 0 end }
  105. bind TEntry <<SelectNone>> { %W selection clear }
  106. bind TEntry <<TraverseIn>> { %W selection range 0 end; %W icursor end }
  107. ## Edit bindings:
  108. #
  109. bind TEntry <Key> { ttk::entry::Insert %W %A }
  110. bind TEntry <Delete> { ttk::entry::Delete %W }
  111. bind TEntry <BackSpace> { ttk::entry::Backspace %W }
  112. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  113. # Otherwise, the <Key> class binding will fire and insert the character.
  114. # Ditto for Escape, Return, and Tab.
  115. #
  116. bind TEntry <Alt-Key> {# nothing}
  117. bind TEntry <Meta-Key> {# nothing}
  118. bind TEntry <Control-Key> {# nothing}
  119. bind TEntry <Escape> {# nothing}
  120. bind TEntry <Return> {# nothing}
  121. bind TEntry <KP_Enter> {# nothing}
  122. bind TEntry <Tab> {# nothing}
  123. # Argh. Apparently on Windows, the NumLock modifier is interpreted
  124. # as a Command modifier.
  125. if {[tk windowingsystem] eq "aqua"} {
  126. bind TEntry <Command-Key> {# nothing}
  127. bind TEntry <Mod4-Key> {# nothing}
  128. }
  129. # Tk-on-Cocoa generates characters for these two keys. [Bug 2971663]
  130. bind TEntry <<PrevLine>> {# nothing}
  131. bind TEntry <<NextLine>> {# nothing}
  132. ## Additional emacs-like bindings:
  133. #
  134. bind TEntry <Control-d> { ttk::entry::Delete %W }
  135. bind TEntry <Control-h> { ttk::entry::Backspace %W }
  136. bind TEntry <Control-k> { %W delete insert end }
  137. # Bindings for IME text input.
  138. bind TEntry <<TkStartIMEMarkedText>> {
  139. dict set ::tk::Priv(IMETextMark) "%W" [%W index insert]
  140. }
  141. bind TEntry <<TkEndIMEMarkedText>> {
  142. if { [catch {dict get $::tk::Priv(IMETextMark) "%W"} mark] } {
  143. bell
  144. } else {
  145. %W selection range $mark insert
  146. }
  147. }
  148. bind TEntry <<TkClearIMEMarkedText>> {
  149. %W delete [dict get $::tk::Priv(IMETextMark) "%W"] [%W index insert]
  150. }
  151. bind TEntry <<TkAccentBackspace>> {
  152. ttk::entry::Backspace %W
  153. }
  154. ### Clipboard procedures.
  155. #
  156. ## EntrySelection -- Return the selected text of the entry.
  157. # Raises an error if there is no selection.
  158. #
  159. proc ttk::entry::EntrySelection {w} {
  160. set entryString [string range [$w get] [$w index sel.first] \
  161. [expr {[$w index sel.last] - 1}]]
  162. if {[$w cget -show] ne ""} {
  163. return [string repeat [string index [$w cget -show] 0] \
  164. [string length $entryString]]
  165. }
  166. return $entryString
  167. }
  168. ## Paste -- Insert clipboard contents at current insert point.
  169. #
  170. proc ttk::entry::Paste {w} {
  171. catch {
  172. set clipboard [::tk::GetSelection $w CLIPBOARD]
  173. PendingDelete $w
  174. $w insert insert $clipboard
  175. See $w insert
  176. }
  177. }
  178. ## Copy -- Copy selection to clipboard.
  179. #
  180. proc ttk::entry::Copy {w} {
  181. if {![catch {EntrySelection $w} selection]} {
  182. clipboard clear -displayof $w
  183. clipboard append -displayof $w $selection
  184. }
  185. }
  186. ## Clear -- Delete the selection.
  187. #
  188. proc ttk::entry::Clear {w} {
  189. catch { $w delete sel.first sel.last }
  190. }
  191. ## Cut -- Copy selection to clipboard then delete it.
  192. #
  193. proc ttk::entry::Cut {w} {
  194. Copy $w; Clear $w
  195. }
  196. ### Navigation procedures.
  197. #
  198. ## ClosestGap -- Find closest boundary between characters.
  199. # Returns the index of the character just after the boundary.
  200. #
  201. proc ttk::entry::ClosestGap {w x} {
  202. set pos [$w index @$x]
  203. set bbox [$w bbox $pos]
  204. if {$x - [lindex $bbox 0] > [lindex $bbox 2]/2} {
  205. incr pos
  206. }
  207. return $pos
  208. }
  209. ## See $index -- Make sure that the character at $index is visible.
  210. #
  211. proc ttk::entry::See {w {index insert}} {
  212. set c [$w index $index]
  213. # @@@ OR: check [$w index left] / [$w index right]
  214. if {$c < [$w index @0] || $c >= [$w index @[winfo width $w]]} {
  215. $w xview $c
  216. }
  217. }
  218. ## NextWord -- Find the next word position.
  219. # Note: The "next word position" follows platform conventions:
  220. # either the next end-of-word position, or the start-of-word
  221. # position following the next end-of-word position.
  222. #
  223. set ::ttk::entry::State(startNext) \
  224. [string equal [tk windowingsystem] "win32"]
  225. proc ttk::entry::NextWord {w start} {
  226. # the check on [winfo class] is because the spinbox and combobox also use this proc
  227. if {[winfo class $w] eq "TEntry" && [$w cget -show] ne ""} {
  228. return end
  229. }
  230. variable State
  231. set pos [tcl_endOfWord [$w get] [$w index $start]]
  232. if {$pos >= 0 && $State(startNext)} {
  233. set pos [tcl_startOfNextWord [$w get] $pos]
  234. }
  235. if {$pos < 0} {
  236. return end
  237. }
  238. return $pos
  239. }
  240. ## PrevWord -- Find the previous word position.
  241. #
  242. proc ttk::entry::PrevWord {w start} {
  243. # the check on [winfo class] is because the spinbox and combobox also use this proc
  244. if {[winfo class $w] eq "TEntry" && [$w cget -show] ne ""} {
  245. return 0
  246. }
  247. set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
  248. if {$pos < 0} {
  249. return 0
  250. }
  251. return $pos
  252. }
  253. ## RelIndex -- Compute character/word/line-relative index.
  254. #
  255. proc ttk::entry::RelIndex {w where {index insert}} {
  256. switch -- $where {
  257. prevchar { expr {[$w index $index] - 1} }
  258. nextchar { expr {[$w index $index] + 1} }
  259. prevword { PrevWord $w $index }
  260. nextword { NextWord $w $index }
  261. home { return 0 }
  262. end { $w index end }
  263. default { error "Bad relative index $index" }
  264. }
  265. }
  266. ## Move -- Move insert cursor to relative location.
  267. # Also clears the selection, if any, and makes sure
  268. # that the insert cursor is visible.
  269. #
  270. proc ttk::entry::Move {w where} {
  271. $w icursor [RelIndex $w $where]
  272. $w selection clear
  273. See $w insert
  274. }
  275. ### Selection procedures.
  276. #
  277. ## ExtendTo -- Extend the selection to the specified index.
  278. #
  279. # The other end of the selection (the anchor) is determined as follows:
  280. #
  281. # (1) if there is no selection, the anchor is the insert cursor;
  282. # (2) if the index is outside the selection, grow the selection;
  283. # (3) if the insert cursor is at one end of the selection, anchor the other end
  284. # (4) otherwise anchor the start of the selection
  285. #
  286. # The insert cursor is placed at the new end of the selection.
  287. #
  288. # Returns: selection anchor.
  289. #
  290. proc ttk::entry::ExtendTo {w index} {
  291. set index [$w index $index]
  292. set insert [$w index insert]
  293. # Figure out selection anchor:
  294. if {![$w selection present]} {
  295. set anchor $insert
  296. } else {
  297. set selfirst [$w index sel.first]
  298. set sellast [$w index sel.last]
  299. if { ($index < $selfirst)
  300. || ($insert == $selfirst && $index <= $sellast)
  301. } {
  302. set anchor $sellast
  303. } else {
  304. set anchor $selfirst
  305. }
  306. }
  307. # Extend selection:
  308. if {$anchor < $index} {
  309. $w selection range $anchor $index
  310. } else {
  311. $w selection range $index $anchor
  312. }
  313. $w icursor $index
  314. return $anchor
  315. }
  316. ## Extend -- Extend the selection to a relative position, show insert cursor
  317. #
  318. proc ttk::entry::Extend {w where} {
  319. ExtendTo $w [RelIndex $w $where]
  320. See $w
  321. }
  322. ### Button 1 binding procedures.
  323. #
  324. # Double-clicking followed by a drag enters "word-select" mode.
  325. # Triple-clicking enters "line-select" mode.
  326. #
  327. ## Press -- Button-1 binding.
  328. # Set the insertion cursor, claim the input focus, set up for
  329. # future drag operations.
  330. #
  331. proc ttk::entry::Press {w x} {
  332. variable State
  333. $w icursor [ClosestGap $w $x]
  334. $w selection clear
  335. $w instate !disabled { focus $w }
  336. # Set up for future drag, double-click, or triple-click.
  337. set State(x) $x
  338. set State(selectMode) char
  339. set State(anchor) [$w index insert]
  340. }
  341. ## Shift-Press -- Shift-Button-1 binding.
  342. # Extends the selection, sets anchor for future drag operations.
  343. #
  344. proc ttk::entry::Shift-Press {w x} {
  345. variable State
  346. focus $w
  347. set anchor [ExtendTo $w @$x]
  348. set State(x) $x
  349. set State(selectMode) char
  350. set State(anchor) $anchor
  351. }
  352. ## Select $w $x $mode -- Binding for double- and triple- clicks.
  353. # Selects a word or line (according to mode),
  354. # and sets the selection mode for subsequent drag operations.
  355. #
  356. proc ttk::entry::Select {w x mode} {
  357. variable State
  358. set cur [ClosestGap $w $x]
  359. switch -- $mode {
  360. word { WordSelect $w $cur $cur }
  361. line { LineSelect $w $cur $cur }
  362. char { # no-op }
  363. }
  364. set State(anchor) $cur
  365. set State(selectMode) $mode
  366. }
  367. ## Drag -- Button1 motion binding.
  368. #
  369. proc ttk::entry::Drag {w x} {
  370. variable State
  371. set State(x) $x
  372. DragTo $w $x
  373. }
  374. ## DragTo $w $x -- Extend selection to $x based on current selection mode.
  375. #
  376. proc ttk::entry::DragTo {w x} {
  377. variable State
  378. set cur [ClosestGap $w $x]
  379. switch $State(selectMode) {
  380. char { CharSelect $w $State(anchor) $cur }
  381. word { WordSelect $w $State(anchor) $cur }
  382. line { LineSelect $w $State(anchor) $cur }
  383. none { # no-op }
  384. }
  385. }
  386. ## <B1-Leave> binding:
  387. # Begin autoscroll.
  388. #
  389. proc ttk::entry::DragOut {w mode} {
  390. variable State
  391. if {$State(selectMode) ne "none" && $mode eq "NotifyNormal"} {
  392. ttk::Repeatedly ttk::entry::AutoScroll $w
  393. }
  394. }
  395. ## <B1-Enter> binding
  396. # Suspend autoscroll.
  397. #
  398. proc ttk::entry::DragIn {w} {
  399. ttk::CancelRepeat
  400. }
  401. ## <ButtonRelease-1> binding
  402. #
  403. proc ttk::entry::Release {w} {
  404. variable State
  405. set State(selectMode) none
  406. ttk::CancelRepeat ;# suspend autoscroll
  407. }
  408. ## AutoScroll
  409. # Called repeatedly when the mouse is outside an entry window
  410. # with Button 1 down. Scroll the window left or right,
  411. # depending on where the mouse left the window, and extend
  412. # the selection according to the current selection mode.
  413. #
  414. # TODO: AutoScroll should repeat faster (50ms) than normal autorepeat.
  415. # TODO: Need a way for Repeat scripts to cancel themselves.
  416. #
  417. proc ttk::entry::AutoScroll {w} {
  418. variable State
  419. if {![winfo exists $w]} return
  420. set x $State(x)
  421. if {$x > [winfo width $w]} {
  422. $w xview scroll 2 units
  423. DragTo $w $x
  424. } elseif {$x < 0} {
  425. $w xview scroll -2 units
  426. DragTo $w $x
  427. }
  428. }
  429. ## CharSelect -- select characters between index $from and $to
  430. #
  431. proc ttk::entry::CharSelect {w from to} {
  432. if {$to <= $from} {
  433. $w selection range $to $from
  434. } else {
  435. $w selection range $from $to
  436. }
  437. $w icursor $to
  438. }
  439. ## WordSelect -- Select whole words between index $from and $to
  440. #
  441. proc ttk::entry::WordSelect {w from to} {
  442. if {$to < $from} {
  443. set first [WordBack [$w get] $to]
  444. set last [WordForward [$w get] $from]
  445. $w icursor $first
  446. } else {
  447. set first [WordBack [$w get] $from]
  448. set last [WordForward [$w get] $to]
  449. $w icursor $last
  450. }
  451. $w selection range $first $last
  452. }
  453. ## WordBack, WordForward -- helper routines for WordSelect.
  454. #
  455. proc ttk::entry::WordBack {text index} {
  456. if {[set pos [tcl_wordBreakBefore $text $index]] < 0} { return 0 }
  457. return $pos
  458. }
  459. proc ttk::entry::WordForward {text index} {
  460. if {[set pos [tcl_wordBreakAfter $text $index]] < 0} { return end }
  461. return $pos
  462. }
  463. ## LineSelect -- Select the entire line.
  464. #
  465. proc ttk::entry::LineSelect {w _ _} {
  466. variable State
  467. $w selection range 0 end
  468. $w icursor end
  469. }
  470. ### Button 2 binding procedures.
  471. #
  472. ## ScanMark -- Button-2 binding.
  473. # Marks the start of a scan or primary transfer operation.
  474. #
  475. proc ttk::entry::ScanMark {w x} {
  476. variable State
  477. set State(scanX) $x
  478. set State(scanIndex) [$w index @0]
  479. set State(scanMoved) 0
  480. }
  481. ## ScanDrag -- Button2 motion binding.
  482. #
  483. proc ttk::entry::ScanDrag {w x} {
  484. variable State
  485. set dx [expr {$State(scanX) - $x}]
  486. if {abs($dx) > $State(deadband)} {
  487. set State(scanMoved) 1
  488. }
  489. set left [expr {$State(scanIndex) + ($dx*$State(scanNum))/$State(scanDen)}]
  490. $w xview $left
  491. if {$left != [set newLeft [$w index @0]]} {
  492. # We've scanned past one end of the entry;
  493. # reset the mark so that the text will start dragging again
  494. # as soon as the mouse reverses direction.
  495. #
  496. set State(scanX) $x
  497. set State(scanIndex) $newLeft
  498. }
  499. }
  500. ## ScanRelease -- Button2 release binding.
  501. # Do a primary transfer if the mouse has not moved since the button press.
  502. #
  503. proc ttk::entry::ScanRelease {w x} {
  504. variable State
  505. if {!$State(scanMoved)} {
  506. $w instate {!disabled !readonly} {
  507. $w icursor [ClosestGap $w $x]
  508. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  509. }
  510. }
  511. }
  512. ### Insertion and deletion procedures.
  513. #
  514. ## PendingDelete -- Delete selection prior to insert.
  515. # If the entry currently has a selection, delete it and
  516. # set the insert position to where the selection was.
  517. # Returns: 1 if pending delete occurred, 0 if nothing was selected.
  518. #
  519. proc ttk::entry::PendingDelete {w} {
  520. if {[$w selection present]} {
  521. $w icursor sel.first
  522. $w delete sel.first sel.last
  523. return 1
  524. }
  525. return 0
  526. }
  527. ## Insert -- Insert text into the entry widget.
  528. # If a selection is present, the new text replaces it.
  529. # Otherwise, the new text is inserted at the insert cursor.
  530. #
  531. proc ttk::entry::Insert {w s} {
  532. if {$s eq ""} { return }
  533. PendingDelete $w
  534. $w insert insert $s
  535. See $w insert
  536. }
  537. ## Backspace -- Backspace over the character just before the insert cursor.
  538. # If there is a selection, delete that instead.
  539. # If the new insert position is offscreen to the left,
  540. # scroll to place the cursor at about the middle of the window.
  541. #
  542. proc ttk::entry::Backspace {w} {
  543. if {[PendingDelete $w]} {
  544. See $w
  545. return
  546. }
  547. set x [expr {[$w index insert] - 1}]
  548. if {$x < 0} { return }
  549. $w delete $x
  550. if {[$w index @0] >= [$w index insert]} {
  551. set range [$w xview]
  552. set left [lindex $range 0]
  553. set right [lindex $range 1]
  554. $w xview moveto [expr {$left - ($right - $left)/2.0}]
  555. }
  556. }
  557. ## Delete -- Delete the character after the insert cursor.
  558. # If there is a selection, delete that instead.
  559. #
  560. proc ttk::entry::Delete {w} {
  561. if {![PendingDelete $w]} {
  562. $w delete insert
  563. }
  564. }
  565. #*EOF*