gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

634 lines
17KB

  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. variable State
  227. set pos [tcl_endOfWord [$w get] [$w index $start]]
  228. if {$pos >= 0 && $State(startNext)} {
  229. set pos [tcl_startOfNextWord [$w get] $pos]
  230. }
  231. if {$pos < 0} {
  232. return end
  233. }
  234. return $pos
  235. }
  236. ## PrevWord -- Find the previous word position.
  237. #
  238. proc ttk::entry::PrevWord {w start} {
  239. set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
  240. if {$pos < 0} {
  241. return 0
  242. }
  243. return $pos
  244. }
  245. ## RelIndex -- Compute character/word/line-relative index.
  246. #
  247. proc ttk::entry::RelIndex {w where {index insert}} {
  248. switch -- $where {
  249. prevchar { expr {[$w index $index] - 1} }
  250. nextchar { expr {[$w index $index] + 1} }
  251. prevword { PrevWord $w $index }
  252. nextword { NextWord $w $index }
  253. home { return 0 }
  254. end { $w index end }
  255. default { error "Bad relative index $index" }
  256. }
  257. }
  258. ## Move -- Move insert cursor to relative location.
  259. # Also clears the selection, if any, and makes sure
  260. # that the insert cursor is visible.
  261. #
  262. proc ttk::entry::Move {w where} {
  263. $w icursor [RelIndex $w $where]
  264. $w selection clear
  265. See $w insert
  266. }
  267. ### Selection procedures.
  268. #
  269. ## ExtendTo -- Extend the selection to the specified index.
  270. #
  271. # The other end of the selection (the anchor) is determined as follows:
  272. #
  273. # (1) if there is no selection, the anchor is the insert cursor;
  274. # (2) if the index is outside the selection, grow the selection;
  275. # (3) if the insert cursor is at one end of the selection, anchor the other end
  276. # (4) otherwise anchor the start of the selection
  277. #
  278. # The insert cursor is placed at the new end of the selection.
  279. #
  280. # Returns: selection anchor.
  281. #
  282. proc ttk::entry::ExtendTo {w index} {
  283. set index [$w index $index]
  284. set insert [$w index insert]
  285. # Figure out selection anchor:
  286. if {![$w selection present]} {
  287. set anchor $insert
  288. } else {
  289. set selfirst [$w index sel.first]
  290. set sellast [$w index sel.last]
  291. if { ($index < $selfirst)
  292. || ($insert == $selfirst && $index <= $sellast)
  293. } {
  294. set anchor $sellast
  295. } else {
  296. set anchor $selfirst
  297. }
  298. }
  299. # Extend selection:
  300. if {$anchor < $index} {
  301. $w selection range $anchor $index
  302. } else {
  303. $w selection range $index $anchor
  304. }
  305. $w icursor $index
  306. return $anchor
  307. }
  308. ## Extend -- Extend the selection to a relative position, show insert cursor
  309. #
  310. proc ttk::entry::Extend {w where} {
  311. ExtendTo $w [RelIndex $w $where]
  312. See $w
  313. }
  314. ### Button 1 binding procedures.
  315. #
  316. # Double-clicking followed by a drag enters "word-select" mode.
  317. # Triple-clicking enters "line-select" mode.
  318. #
  319. ## Press -- Button-1 binding.
  320. # Set the insertion cursor, claim the input focus, set up for
  321. # future drag operations.
  322. #
  323. proc ttk::entry::Press {w x} {
  324. variable State
  325. $w icursor [ClosestGap $w $x]
  326. $w selection clear
  327. $w instate !disabled { focus $w }
  328. # Set up for future drag, double-click, or triple-click.
  329. set State(x) $x
  330. set State(selectMode) char
  331. set State(anchor) [$w index insert]
  332. }
  333. ## Shift-Press -- Shift-Button-1 binding.
  334. # Extends the selection, sets anchor for future drag operations.
  335. #
  336. proc ttk::entry::Shift-Press {w x} {
  337. variable State
  338. focus $w
  339. set anchor [ExtendTo $w @$x]
  340. set State(x) $x
  341. set State(selectMode) char
  342. set State(anchor) $anchor
  343. }
  344. ## Select $w $x $mode -- Binding for double- and triple- clicks.
  345. # Selects a word or line (according to mode),
  346. # and sets the selection mode for subsequent drag operations.
  347. #
  348. proc ttk::entry::Select {w x mode} {
  349. variable State
  350. set cur [ClosestGap $w $x]
  351. switch -- $mode {
  352. word { WordSelect $w $cur $cur }
  353. line { LineSelect $w $cur $cur }
  354. char { # no-op }
  355. }
  356. set State(anchor) $cur
  357. set State(selectMode) $mode
  358. }
  359. ## Drag -- Button1 motion binding.
  360. #
  361. proc ttk::entry::Drag {w x} {
  362. variable State
  363. set State(x) $x
  364. DragTo $w $x
  365. }
  366. ## DragTo $w $x -- Extend selection to $x based on current selection mode.
  367. #
  368. proc ttk::entry::DragTo {w x} {
  369. variable State
  370. set cur [ClosestGap $w $x]
  371. switch $State(selectMode) {
  372. char { CharSelect $w $State(anchor) $cur }
  373. word { WordSelect $w $State(anchor) $cur }
  374. line { LineSelect $w $State(anchor) $cur }
  375. none { # no-op }
  376. }
  377. }
  378. ## <B1-Leave> binding:
  379. # Begin autoscroll.
  380. #
  381. proc ttk::entry::DragOut {w mode} {
  382. variable State
  383. if {$State(selectMode) ne "none" && $mode eq "NotifyNormal"} {
  384. ttk::Repeatedly ttk::entry::AutoScroll $w
  385. }
  386. }
  387. ## <B1-Enter> binding
  388. # Suspend autoscroll.
  389. #
  390. proc ttk::entry::DragIn {w} {
  391. ttk::CancelRepeat
  392. }
  393. ## <ButtonRelease-1> binding
  394. #
  395. proc ttk::entry::Release {w} {
  396. variable State
  397. set State(selectMode) none
  398. ttk::CancelRepeat ;# suspend autoscroll
  399. }
  400. ## AutoScroll
  401. # Called repeatedly when the mouse is outside an entry window
  402. # with Button 1 down. Scroll the window left or right,
  403. # depending on where the mouse left the window, and extend
  404. # the selection according to the current selection mode.
  405. #
  406. # TODO: AutoScroll should repeat faster (50ms) than normal autorepeat.
  407. # TODO: Need a way for Repeat scripts to cancel themselves.
  408. #
  409. proc ttk::entry::AutoScroll {w} {
  410. variable State
  411. if {![winfo exists $w]} return
  412. set x $State(x)
  413. if {$x > [winfo width $w]} {
  414. $w xview scroll 2 units
  415. DragTo $w $x
  416. } elseif {$x < 0} {
  417. $w xview scroll -2 units
  418. DragTo $w $x
  419. }
  420. }
  421. ## CharSelect -- select characters between index $from and $to
  422. #
  423. proc ttk::entry::CharSelect {w from to} {
  424. if {$to <= $from} {
  425. $w selection range $to $from
  426. } else {
  427. $w selection range $from $to
  428. }
  429. $w icursor $to
  430. }
  431. ## WordSelect -- Select whole words between index $from and $to
  432. #
  433. proc ttk::entry::WordSelect {w from to} {
  434. if {$to < $from} {
  435. set first [WordBack [$w get] $to]
  436. set last [WordForward [$w get] $from]
  437. $w icursor $first
  438. } else {
  439. set first [WordBack [$w get] $from]
  440. set last [WordForward [$w get] $to]
  441. $w icursor $last
  442. }
  443. $w selection range $first $last
  444. }
  445. ## WordBack, WordForward -- helper routines for WordSelect.
  446. #
  447. proc ttk::entry::WordBack {text index} {
  448. if {[set pos [tcl_wordBreakBefore $text $index]] < 0} { return 0 }
  449. return $pos
  450. }
  451. proc ttk::entry::WordForward {text index} {
  452. if {[set pos [tcl_wordBreakAfter $text $index]] < 0} { return end }
  453. return $pos
  454. }
  455. ## LineSelect -- Select the entire line.
  456. #
  457. proc ttk::entry::LineSelect {w _ _} {
  458. variable State
  459. $w selection range 0 end
  460. $w icursor end
  461. }
  462. ### Button 2 binding procedures.
  463. #
  464. ## ScanMark -- Button-2 binding.
  465. # Marks the start of a scan or primary transfer operation.
  466. #
  467. proc ttk::entry::ScanMark {w x} {
  468. variable State
  469. set State(scanX) $x
  470. set State(scanIndex) [$w index @0]
  471. set State(scanMoved) 0
  472. }
  473. ## ScanDrag -- Button2 motion binding.
  474. #
  475. proc ttk::entry::ScanDrag {w x} {
  476. variable State
  477. set dx [expr {$State(scanX) - $x}]
  478. if {abs($dx) > $State(deadband)} {
  479. set State(scanMoved) 1
  480. }
  481. set left [expr {$State(scanIndex) + ($dx*$State(scanNum))/$State(scanDen)}]
  482. $w xview $left
  483. if {$left != [set newLeft [$w index @0]]} {
  484. # We've scanned past one end of the entry;
  485. # reset the mark so that the text will start dragging again
  486. # as soon as the mouse reverses direction.
  487. #
  488. set State(scanX) $x
  489. set State(scanIndex) $newLeft
  490. }
  491. }
  492. ## ScanRelease -- Button2 release binding.
  493. # Do a primary transfer if the mouse has not moved since the button press.
  494. #
  495. proc ttk::entry::ScanRelease {w x} {
  496. variable State
  497. if {!$State(scanMoved)} {
  498. $w instate {!disabled !readonly} {
  499. $w icursor [ClosestGap $w $x]
  500. catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
  501. }
  502. }
  503. }
  504. ### Insertion and deletion procedures.
  505. #
  506. ## PendingDelete -- Delete selection prior to insert.
  507. # If the entry currently has a selection, delete it and
  508. # set the insert position to where the selection was.
  509. # Returns: 1 if pending delete occurred, 0 if nothing was selected.
  510. #
  511. proc ttk::entry::PendingDelete {w} {
  512. if {[$w selection present]} {
  513. $w icursor sel.first
  514. $w delete sel.first sel.last
  515. return 1
  516. }
  517. return 0
  518. }
  519. ## Insert -- Insert text into the entry widget.
  520. # If a selection is present, the new text replaces it.
  521. # Otherwise, the new text is inserted at the insert cursor.
  522. #
  523. proc ttk::entry::Insert {w s} {
  524. if {$s eq ""} { return }
  525. PendingDelete $w
  526. $w insert insert $s
  527. See $w insert
  528. }
  529. ## Backspace -- Backspace over the character just before the insert cursor.
  530. # If there is a selection, delete that instead.
  531. # If the new insert position is offscreen to the left,
  532. # scroll to place the cursor at about the middle of the window.
  533. #
  534. proc ttk::entry::Backspace {w} {
  535. if {[PendingDelete $w]} {
  536. See $w
  537. return
  538. }
  539. set x [expr {[$w index insert] - 1}]
  540. if {$x < 0} { return }
  541. $w delete $x
  542. if {[$w index @0] >= [$w index insert]} {
  543. set range [$w xview]
  544. set left [lindex $range 0]
  545. set right [lindex $range 1]
  546. $w xview moveto [expr {$left - ($right - $left)/2.0}]
  547. }
  548. }
  549. ## Delete -- Delete the character after the insert cursor.
  550. # If there is a selection, delete that instead.
  551. #
  552. proc ttk::entry::Delete {w} {
  553. if {![PendingDelete $w]} {
  554. $w delete insert
  555. }
  556. }
  557. #*EOF*
上海开阖软件有限公司 沪ICP备12045867号-1