entry.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. }
  128. # Tk-on-Cocoa generates characters for these two keys. [Bug 2971663]
  129. bind TEntry <<PrevLine>> {# nothing}
  130. bind TEntry <<NextLine>> {# nothing}
  131. ## Additional emacs-like bindings:
  132. #
  133. bind TEntry <Control-d> { ttk::entry::Delete %W }
  134. bind TEntry <Control-h> { ttk::entry::Backspace %W }
  135. bind TEntry <Control-k> { %W delete insert end }
  136. # Bindings for IME text input.
  137. bind TEntry <<TkStartIMEMarkedText>> {
  138. dict set ::tk::Priv(IMETextMark) "%W" [%W index insert]
  139. }
  140. bind TEntry <<TkEndIMEMarkedText>> {
  141. if { [catch {dict get $::tk::Priv(IMETextMark) "%W"} mark] } {
  142. bell
  143. } else {
  144. %W selection range $mark insert
  145. }
  146. }
  147. bind TEntry <<TkClearIMEMarkedText>> {
  148. %W delete [dict get $::tk::Priv(IMETextMark) "%W"] [%W index insert]
  149. }
  150. bind TEntry <<TkAccentBackspace>> {
  151. ttk::entry::Backspace %W
  152. }
  153. ### Clipboard procedures.
  154. #
  155. ## EntrySelection -- Return the selected text of the entry.
  156. # Raises an error if there is no selection.
  157. #
  158. proc ttk::entry::EntrySelection {w} {
  159. set entryString [string range [$w get] [$w index sel.first] \
  160. [expr {[$w index sel.last] - 1}]]
  161. if {[$w cget -show] ne ""} {
  162. return [string repeat [string index [$w cget -show] 0] \
  163. [string length $entryString]]
  164. }
  165. return $entryString
  166. }
  167. ## Paste -- Insert clipboard contents at current insert point.
  168. #
  169. proc ttk::entry::Paste {w} {
  170. catch {
  171. set clipboard [::tk::GetSelection $w CLIPBOARD]
  172. PendingDelete $w
  173. $w insert insert $clipboard
  174. See $w insert
  175. }
  176. }
  177. ## Copy -- Copy selection to clipboard.
  178. #
  179. proc ttk::entry::Copy {w} {
  180. if {![catch {EntrySelection $w} selection]} {
  181. clipboard clear -displayof $w
  182. clipboard append -displayof $w $selection
  183. }
  184. }
  185. ## Clear -- Delete the selection.
  186. #
  187. proc ttk::entry::Clear {w} {
  188. catch { $w delete sel.first sel.last }
  189. }
  190. ## Cut -- Copy selection to clipboard then delete it.
  191. #
  192. proc ttk::entry::Cut {w} {
  193. Copy $w; Clear $w
  194. }
  195. ### Navigation procedures.
  196. #
  197. ## ClosestGap -- Find closest boundary between characters.
  198. # Returns the index of the character just after the boundary.
  199. #
  200. proc ttk::entry::ClosestGap {w x} {
  201. set pos [$w index @$x]
  202. set bbox [$w bbox $pos]
  203. if {$x - [lindex $bbox 0] > [lindex $bbox 2]/2} {
  204. incr pos
  205. }
  206. return $pos
  207. }
  208. ## See $index -- Make sure that the character at $index is visible.
  209. #
  210. proc ttk::entry::See {w {index insert}} {
  211. set c [$w index $index]
  212. # @@@ OR: check [$w index left] / [$w index right]
  213. if {$c < [$w index @0] || $c >= [$w index @[winfo width $w]]} {
  214. $w xview $c
  215. }
  216. }
  217. ## NextWord -- Find the next word position.
  218. # Note: The "next word position" follows platform conventions:
  219. # either the next end-of-word position, or the start-of-word
  220. # position following the next end-of-word position.
  221. #
  222. set ::ttk::entry::State(startNext) \
  223. [string equal [tk windowingsystem] "win32"]
  224. proc ttk::entry::NextWord {w start} {
  225. variable State
  226. set pos [tcl_endOfWord [$w get] [$w index $start]]
  227. if {$pos >= 0 && $State(startNext)} {
  228. set pos [tcl_startOfNextWord [$w get] $pos]
  229. }
  230. if {$pos < 0} {
  231. return end
  232. }
  233. return $pos
  234. }
  235. ## PrevWord -- Find the previous word position.
  236. #
  237. proc ttk::entry::PrevWord {w start} {
  238. set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
  239. if {$pos < 0} {
  240. return 0
  241. }
  242. return $pos
  243. }
  244. ## RelIndex -- Compute character/word/line-relative index.
  245. #
  246. proc ttk::entry::RelIndex {w where {index insert}} {
  247. switch -- $where {
  248. prevchar { expr {[$w index $index] - 1} }
  249. nextchar { expr {[$w index $index] + 1} }
  250. prevword { PrevWord $w $index }
  251. nextword { NextWord $w $index }
  252. home { return 0 }
  253. end { $w index end }
  254. default { error "Bad relative index $index" }
  255. }
  256. }
  257. ## Move -- Move insert cursor to relative location.
  258. # Also clears the selection, if any, and makes sure
  259. # that the insert cursor is visible.
  260. #
  261. proc ttk::entry::Move {w where} {
  262. $w icursor [RelIndex $w $where]
  263. $w selection clear
  264. See $w insert
  265. }
  266. ### Selection procedures.
  267. #
  268. ## ExtendTo -- Extend the selection to the specified index.
  269. #
  270. # The other end of the selection (the anchor) is determined as follows:
  271. #
  272. # (1) if there is no selection, the anchor is the insert cursor;
  273. # (2) if the index is outside the selection, grow the selection;
  274. # (3) if the insert cursor is at one end of the selection, anchor the other end
  275. # (4) otherwise anchor the start of the selection
  276. #
  277. # The insert cursor is placed at the new end of the selection.
  278. #
  279. # Returns: selection anchor.
  280. #
  281. proc ttk::entry::ExtendTo {w index} {
  282. set index [$w index $index]
  283. set insert [$w index insert]
  284. # Figure out selection anchor:
  285. if {![$w selection present]} {
  286. set anchor $insert
  287. } else {
  288. set selfirst [$w index sel.first]
  289. set sellast [$w index sel.last]
  290. if { ($index < $selfirst)
  291. || ($insert == $selfirst && $index <= $sellast)
  292. } {
  293. set anchor $sellast
  294. } else {
  295. set anchor $selfirst
  296. }
  297. }
  298. # Extend selection:
  299. if {$anchor < $index} {
  300. $w selection range $anchor $index
  301. } else {
  302. $w selection range $index $anchor
  303. }
  304. $w icursor $index
  305. return $anchor
  306. }
  307. ## Extend -- Extend the selection to a relative position, show insert cursor
  308. #
  309. proc ttk::entry::Extend {w where} {
  310. ExtendTo $w [RelIndex $w $where]
  311. See $w
  312. }
  313. ### Button 1 binding procedures.
  314. #
  315. # Double-clicking followed by a drag enters "word-select" mode.
  316. # Triple-clicking enters "line-select" mode.
  317. #
  318. ## Press -- Button-1 binding.
  319. # Set the insertion cursor, claim the input focus, set up for
  320. # future drag operations.
  321. #
  322. proc ttk::entry::Press {w x} {
  323. variable State
  324. $w icursor [ClosestGap $w $x]
  325. $w selection clear
  326. $w instate !disabled { focus $w }
  327. # Set up for future drag, double-click, or triple-click.
  328. set State(x) $x
  329. set State(selectMode) char
  330. set State(anchor) [$w index insert]
  331. }
  332. ## Shift-Press -- Shift-Button-1 binding.
  333. # Extends the selection, sets anchor for future drag operations.
  334. #
  335. proc ttk::entry::Shift-Press {w x} {
  336. variable State
  337. focus $w
  338. set anchor [ExtendTo $w @$x]
  339. set State(x) $x
  340. set State(selectMode) char
  341. set State(anchor) $anchor
  342. }
  343. ## Select $w $x $mode -- Binding for double- and triple- clicks.
  344. # Selects a word or line (according to mode),
  345. # and sets the selection mode for subsequent drag operations.
  346. #
  347. proc ttk::entry::Select {w x mode} {
  348. variable State
  349. set cur [ClosestGap $w $x]
  350. switch -- $mode {
  351. word { WordSelect $w $cur $cur }
  352. line { LineSelect $w $cur $cur }
  353. char { # no-op }
  354. }
  355. set State(anchor) $cur
  356. set State(selectMode) $mode
  357. }
  358. ## Drag -- Button1 motion binding.
  359. #
  360. proc ttk::entry::Drag {w x} {
  361. variable State
  362. set State(x) $x
  363. DragTo $w $x
  364. }
  365. ## DragTo $w $x -- Extend selection to $x based on current selection mode.
  366. #
  367. proc ttk::entry::DragTo {w x} {
  368. variable State
  369. set cur [ClosestGap $w $x]
  370. switch $State(selectMode) {
  371. char { CharSelect $w $State(anchor) $cur }
  372. word { WordSelect $w $State(anchor) $cur }
  373. line { LineSelect $w $State(anchor) $cur }
  374. none { # no-op }
  375. }
  376. }
  377. ## <B1-Leave> binding:
  378. # Begin autoscroll.
  379. #
  380. proc ttk::entry::DragOut {w mode} {
  381. variable State
  382. if {$State(selectMode) ne "none" && $mode eq "NotifyNormal"} {
  383. ttk::Repeatedly ttk::entry::AutoScroll $w
  384. }
  385. }
  386. ## <B1-Enter> binding
  387. # Suspend autoscroll.
  388. #
  389. proc ttk::entry::DragIn {w} {
  390. ttk::CancelRepeat
  391. }
  392. ## <ButtonRelease-1> binding
  393. #
  394. proc ttk::entry::Release {w} {
  395. variable State
  396. set State(selectMode) none
  397. ttk::CancelRepeat ;# suspend autoscroll
  398. }
  399. ## AutoScroll
  400. # Called repeatedly when the mouse is outside an entry window
  401. # with Button 1 down. Scroll the window left or right,
  402. # depending on where the mouse left the window, and extend
  403. # the selection according to the current selection mode.
  404. #
  405. # TODO: AutoScroll should repeat faster (50ms) than normal autorepeat.
  406. # TODO: Need a way for Repeat scripts to cancel themselves.
  407. #
  408. proc ttk::entry::AutoScroll {w} {
  409. variable State
  410. if {![winfo exists $w]} return
  411. set x $State(x)
  412. if {$x > [winfo width $w]} {
  413. $w xview scroll 2 units
  414. DragTo $w $x
  415. } elseif {$x < 0} {
  416. $w xview scroll -2 units
  417. DragTo $w $x
  418. }
  419. }
  420. ## CharSelect -- select characters between index $from and $to
  421. #
  422. proc ttk::entry::CharSelect {w from to} {
  423. if {$to <= $from} {
  424. $w selection range $to $from
  425. } else {
  426. $w selection range $from $to
  427. }
  428. $w icursor $to
  429. }
  430. ## WordSelect -- Select whole words between index $from and $to
  431. #
  432. proc ttk::entry::WordSelect {w from to} {
  433. if {$to < $from} {
  434. set first [WordBack [$w get] $to]
  435. set last [WordForward [$w get] $from]
  436. $w icursor $first
  437. } else {
  438. set first [WordBack [$w get] $from]
  439. set last [WordForward [$w get] $to]
  440. $w icursor $last
  441. }
  442. $w selection range $first $last
  443. }
  444. ## WordBack, WordForward -- helper routines for WordSelect.
  445. #
  446. proc ttk::entry::WordBack {text index} {
  447. if {[set pos [tcl_wordBreakBefore $text $index]] < 0} { return 0 }
  448. return $pos
  449. }
  450. proc ttk::entry::WordForward {text index} {
  451. if {[set pos [tcl_wordBreakAfter $text $index]] < 0} { return end }
  452. return $pos
  453. }
  454. ## LineSelect -- Select the entire line.
  455. #
  456. proc ttk::entry::LineSelect {w _ _} {
  457. variable State
  458. $w selection range 0 end
  459. $w icursor end
  460. }
  461. ### Button 2 binding procedures.
  462. #
  463. ## ScanMark -- Button-2 binding.
  464. # Marks the start of a scan or primary transfer operation.
  465. #
  466. proc ttk::entry::ScanMark {w x} {
  467. variable State
  468. set State(scanX) $x
  469. set State(scanIndex) [$w index @0]
  470. set State(scanMoved) 0
  471. }
  472. ## ScanDrag -- Button2 motion binding.
  473. #
  474. proc ttk::entry::ScanDrag {w x} {
  475. variable State
  476. set dx [expr {$State(scanX) - $x}]
  477. if {abs($dx) > $State(deadband)} {
  478. set State(scanMoved) 1
  479. }
  480. set left [expr {$State(scanIndex) + ($dx*$State(scanNum))/$State(scanDen)}]
  481. $w xview $left
  482. if {$left != [set newLeft [$w index @0]]} {
  483. # We've scanned past one end of the entry;
  484. # reset the mark so that the text will start dragging again
  485. # as soon as the mouse reverses direction.
  486. #
  487. set State(scanX) $x
  488. set State(scanIndex) $newLeft
  489. }
  490. }
  491. ## ScanRelease -- Button2 release binding.
  492. # Do a primary transfer if the mouse has not moved since the button press.
  493. #
  494. proc ttk::entry::ScanRelease {w x} {
  495. variable State
  496. if {!$State(scanMoved)} {
  497. $w instate {!disabled !readonly} {
  498. $w icursor [ClosestGap $w $x]
  499. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  500. }
  501. }
  502. }
  503. ### Insertion and deletion procedures.
  504. #
  505. ## PendingDelete -- Delete selection prior to insert.
  506. # If the entry currently has a selection, delete it and
  507. # set the insert position to where the selection was.
  508. # Returns: 1 if pending delete occurred, 0 if nothing was selected.
  509. #
  510. proc ttk::entry::PendingDelete {w} {
  511. if {[$w selection present]} {
  512. $w icursor sel.first
  513. $w delete sel.first sel.last
  514. return 1
  515. }
  516. return 0
  517. }
  518. ## Insert -- Insert text into the entry widget.
  519. # If a selection is present, the new text replaces it.
  520. # Otherwise, the new text is inserted at the insert cursor.
  521. #
  522. proc ttk::entry::Insert {w s} {
  523. if {$s eq ""} { return }
  524. PendingDelete $w
  525. $w insert insert $s
  526. See $w insert
  527. }
  528. ## Backspace -- Backspace over the character just before the insert cursor.
  529. # If there is a selection, delete that instead.
  530. # If the new insert position is offscreen to the left,
  531. # scroll to place the cursor at about the middle of the window.
  532. #
  533. proc ttk::entry::Backspace {w} {
  534. if {[PendingDelete $w]} {
  535. See $w
  536. return
  537. }
  538. set x [expr {[$w index insert] - 1}]
  539. if {$x < 0} { return }
  540. $w delete $x
  541. if {[$w index @0] >= [$w index insert]} {
  542. set range [$w xview]
  543. set left [lindex $range 0]
  544. set right [lindex $range 1]
  545. $w xview moveto [expr {$left - ($right - $left)/2.0}]
  546. }
  547. }
  548. ## Delete -- Delete the character after the insert cursor.
  549. # If there is a selection, delete that instead.
  550. #
  551. proc ttk::entry::Delete {w} {
  552. if {![PendingDelete $w]} {
  553. $w delete insert
  554. }
  555. }
  556. #*EOF*