entry.tcl 16 KB

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