entry.tcl 16 KB

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