gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1239 lines
33KB

  1. # text.tcl --
  2. #
  3. # This file defines the default bindings for Tk text widgets and provides
  4. # procedures that help in implementing the bindings.
  5. #
  6. # Copyright (c) 1992-1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  8. # Copyright (c) 1998 by Scriptics Corporation.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. #-------------------------------------------------------------------------
  14. # Elements of ::tk::Priv that are used in this file:
  15. #
  16. # afterId - If non-null, it means that auto-scanning is underway
  17. # and it gives the "after" id for the next auto-scan
  18. # command to be executed.
  19. # char - Character position on the line; kept in order
  20. # to allow moving up or down past short lines while
  21. # still remembering the desired position.
  22. # mouseMoved - Non-zero means the mouse has moved a significant
  23. # amount since the button went down (so, for example,
  24. # start dragging out a selection).
  25. # prevPos - Used when moving up or down lines via the keyboard.
  26. # Keeps track of the previous insert position, so
  27. # we can distinguish a series of ups and downs, all
  28. # in a row, from a new up or down.
  29. # selectMode - The style of selection currently underway:
  30. # char, word, or line.
  31. # x, y - Last known mouse coordinates for scanning
  32. # and auto-scanning.
  33. #
  34. #-------------------------------------------------------------------------
  35. #-------------------------------------------------------------------------
  36. # The code below creates the default class bindings for text widgets.
  37. #-------------------------------------------------------------------------
  38. # Standard Motif bindings:
  39. bind Text <1> {
  40. tk::TextButton1 %W %x %y
  41. %W tag remove sel 0.0 end
  42. }
  43. bind Text <B1-Motion> {
  44. set tk::Priv(x) %x
  45. set tk::Priv(y) %y
  46. tk::TextSelectTo %W %x %y
  47. }
  48. bind Text <Double-1> {
  49. set tk::Priv(selectMode) word
  50. tk::TextSelectTo %W %x %y
  51. catch {%W mark set insert sel.first}
  52. }
  53. bind Text <Triple-1> {
  54. set tk::Priv(selectMode) line
  55. tk::TextSelectTo %W %x %y
  56. catch {%W mark set insert sel.first}
  57. }
  58. bind Text <Shift-1> {
  59. tk::TextResetAnchor %W @%x,%y
  60. set tk::Priv(selectMode) char
  61. tk::TextSelectTo %W %x %y
  62. }
  63. bind Text <Double-Shift-1> {
  64. set tk::Priv(selectMode) word
  65. tk::TextSelectTo %W %x %y 1
  66. }
  67. bind Text <Triple-Shift-1> {
  68. set tk::Priv(selectMode) line
  69. tk::TextSelectTo %W %x %y
  70. }
  71. bind Text <B1-Leave> {
  72. set tk::Priv(x) %x
  73. set tk::Priv(y) %y
  74. tk::TextAutoScan %W
  75. }
  76. bind Text <B1-Enter> {
  77. tk::CancelRepeat
  78. }
  79. bind Text <ButtonRelease-1> {
  80. tk::CancelRepeat
  81. }
  82. bind Text <Control-1> {
  83. %W mark set insert @%x,%y
  84. # An operation that moves the insert mark without making it
  85. # one end of the selection must insert an autoseparator
  86. if {[%W cget -autoseparators]} {
  87. %W edit separator
  88. }
  89. }
  90. # stop an accidental double click triggering <Double-Button-1>
  91. bind Text <Double-Control-1> { # nothing }
  92. # stop an accidental movement triggering <B1-Motion>
  93. bind Text <Control-B1-Motion> { # nothing }
  94. bind Text <<PrevChar>> {
  95. tk::TextSetCursor %W insert-1displayindices
  96. }
  97. bind Text <<NextChar>> {
  98. tk::TextSetCursor %W insert+1displayindices
  99. }
  100. bind Text <<PrevLine>> {
  101. tk::TextSetCursor %W [tk::TextUpDownLine %W -1]
  102. }
  103. bind Text <<NextLine>> {
  104. tk::TextSetCursor %W [tk::TextUpDownLine %W 1]
  105. }
  106. bind Text <<SelectPrevChar>> {
  107. tk::TextKeySelect %W [%W index {insert - 1displayindices}]
  108. }
  109. bind Text <<SelectNextChar>> {
  110. tk::TextKeySelect %W [%W index {insert + 1displayindices}]
  111. }
  112. bind Text <<SelectPrevLine>> {
  113. tk::TextKeySelect %W [tk::TextUpDownLine %W -1]
  114. }
  115. bind Text <<SelectNextLine>> {
  116. tk::TextKeySelect %W [tk::TextUpDownLine %W 1]
  117. }
  118. bind Text <<PrevWord>> {
  119. tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  120. }
  121. bind Text <<NextWord>> {
  122. tk::TextSetCursor %W [tk::TextNextWord %W insert]
  123. }
  124. bind Text <<PrevPara>> {
  125. tk::TextSetCursor %W [tk::TextPrevPara %W insert]
  126. }
  127. bind Text <<NextPara>> {
  128. tk::TextSetCursor %W [tk::TextNextPara %W insert]
  129. }
  130. bind Text <<SelectPrevWord>> {
  131. tk::TextKeySelect %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  132. }
  133. bind Text <<SelectNextWord>> {
  134. tk::TextKeySelect %W [tk::TextNextWord %W insert]
  135. }
  136. bind Text <<SelectPrevPara>> {
  137. tk::TextKeySelect %W [tk::TextPrevPara %W insert]
  138. }
  139. bind Text <<SelectNextPara>> {
  140. tk::TextKeySelect %W [tk::TextNextPara %W insert]
  141. }
  142. bind Text <Prior> {
  143. tk::TextSetCursor %W [tk::TextScrollPages %W -1]
  144. }
  145. bind Text <Shift-Prior> {
  146. tk::TextKeySelect %W [tk::TextScrollPages %W -1]
  147. }
  148. bind Text <Next> {
  149. tk::TextSetCursor %W [tk::TextScrollPages %W 1]
  150. }
  151. bind Text <Shift-Next> {
  152. tk::TextKeySelect %W [tk::TextScrollPages %W 1]
  153. }
  154. bind Text <Control-Prior> {
  155. %W xview scroll -1 page
  156. }
  157. bind Text <Control-Next> {
  158. %W xview scroll 1 page
  159. }
  160. bind Text <<LineStart>> {
  161. tk::TextSetCursor %W {insert display linestart}
  162. }
  163. bind Text <<SelectLineStart>> {
  164. tk::TextKeySelect %W {insert display linestart}
  165. }
  166. bind Text <<LineEnd>> {
  167. tk::TextSetCursor %W {insert display lineend}
  168. }
  169. bind Text <<SelectLineEnd>> {
  170. tk::TextKeySelect %W {insert display lineend}
  171. }
  172. bind Text <Control-Home> {
  173. tk::TextSetCursor %W 1.0
  174. }
  175. bind Text <Control-Shift-Home> {
  176. tk::TextKeySelect %W 1.0
  177. }
  178. bind Text <Control-End> {
  179. tk::TextSetCursor %W {end - 1 indices}
  180. }
  181. bind Text <Control-Shift-End> {
  182. tk::TextKeySelect %W {end - 1 indices}
  183. }
  184. bind Text <Tab> {
  185. if {[%W cget -state] eq "normal"} {
  186. tk::TextInsert %W \t
  187. focus %W
  188. break
  189. }
  190. }
  191. bind Text <Shift-Tab> {
  192. # Needed only to keep <Tab> binding from triggering; doesn't
  193. # have to actually do anything.
  194. break
  195. }
  196. bind Text <Control-Tab> {
  197. focus [tk_focusNext %W]
  198. }
  199. bind Text <Control-Shift-Tab> {
  200. focus [tk_focusPrev %W]
  201. }
  202. bind Text <Control-i> {
  203. tk::TextInsert %W \t
  204. }
  205. bind Text <Return> {
  206. tk::TextInsert %W \n
  207. if {[%W cget -autoseparators]} {
  208. %W edit separator
  209. }
  210. }
  211. bind Text <Delete> {
  212. if {[tk::TextCursorInSelection %W]} {
  213. %W delete sel.first sel.last
  214. } else {
  215. if {[%W compare end != insert+1c]} {
  216. %W delete insert
  217. }
  218. %W see insert
  219. }
  220. }
  221. bind Text <BackSpace> {
  222. if {[tk::TextCursorInSelection %W]} {
  223. %W delete sel.first sel.last
  224. } else {
  225. if {[%W compare insert != 1.0]} {
  226. %W delete insert-1c
  227. }
  228. %W see insert
  229. }
  230. }
  231. bind Text <Control-space> {
  232. %W mark set [tk::TextAnchor %W] insert
  233. }
  234. bind Text <Select> {
  235. %W mark set [tk::TextAnchor %W] insert
  236. }
  237. bind Text <Control-Shift-space> {
  238. set tk::Priv(selectMode) char
  239. tk::TextKeyExtend %W insert
  240. }
  241. bind Text <Shift-Select> {
  242. set tk::Priv(selectMode) char
  243. tk::TextKeyExtend %W insert
  244. }
  245. bind Text <<SelectAll>> {
  246. %W tag add sel 1.0 end
  247. }
  248. bind Text <<SelectNone>> {
  249. %W tag remove sel 1.0 end
  250. # An operation that clears the selection must insert an autoseparator,
  251. # because the selection operation may have moved the insert mark
  252. if {[%W cget -autoseparators]} {
  253. %W edit separator
  254. }
  255. }
  256. bind Text <<Cut>> {
  257. tk_textCut %W
  258. }
  259. bind Text <<Copy>> {
  260. tk_textCopy %W
  261. }
  262. bind Text <<Paste>> {
  263. tk_textPaste %W
  264. }
  265. bind Text <<Clear>> {
  266. # Make <<Clear>> an atomic operation on the Undo stack,
  267. # i.e. separate it from other delete operations on either side
  268. if {[%W cget -autoseparators]} {
  269. %W edit separator
  270. }
  271. catch {%W delete sel.first sel.last}
  272. if {[%W cget -autoseparators]} {
  273. %W edit separator
  274. }
  275. }
  276. bind Text <<PasteSelection>> {
  277. if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
  278. || !$tk::Priv(mouseMoved)} {
  279. tk::TextPasteSelection %W %x %y
  280. }
  281. }
  282. bind Text <Insert> {
  283. catch {tk::TextInsert %W [::tk::GetSelection %W PRIMARY]}
  284. }
  285. bind Text <Key> {
  286. tk::TextInsert %W %A
  287. }
  288. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  289. # Otherwise, if a widget binding for one of these is defined, the
  290. # <Key> class binding will also fire and insert the character,
  291. # which is wrong. Ditto for <Escape>.
  292. bind Text <Alt-Key> {# nothing }
  293. bind Text <Meta-Key> {# nothing}
  294. bind Text <Control-Key> {# nothing}
  295. bind Text <Escape> {# nothing}
  296. bind Text <KP_Enter> {# nothing}
  297. if {[tk windowingsystem] eq "aqua"} {
  298. bind Text <Command-Key> {# nothing}
  299. bind Text <Mod4-Key> {# nothing}
  300. }
  301. # Additional emacs-like bindings:
  302. bind Text <Control-d> {
  303. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  304. %W delete insert
  305. }
  306. }
  307. bind Text <Control-k> {
  308. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  309. if {[%W compare insert == {insert lineend}]} {
  310. %W delete insert
  311. } else {
  312. %W delete insert {insert lineend}
  313. }
  314. }
  315. }
  316. bind Text <Control-o> {
  317. if {!$tk_strictMotif} {
  318. %W insert insert \n
  319. %W mark set insert insert-1c
  320. }
  321. }
  322. bind Text <Control-t> {
  323. if {!$tk_strictMotif} {
  324. tk::TextTranspose %W
  325. }
  326. }
  327. bind Text <<Undo>> {
  328. # An Undo operation may remove the separator at the top of the Undo stack.
  329. # Then the item at the top of the stack gets merged with the subsequent changes.
  330. # Place separators before and after Undo to prevent this.
  331. if {[%W cget -autoseparators]} {
  332. %W edit separator
  333. }
  334. catch { %W edit undo }
  335. if {[%W cget -autoseparators]} {
  336. %W edit separator
  337. }
  338. }
  339. bind Text <<Redo>> {
  340. catch { %W edit redo }
  341. }
  342. bind Text <Meta-b> {
  343. if {!$tk_strictMotif} {
  344. tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  345. }
  346. }
  347. bind Text <Meta-d> {
  348. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  349. %W delete insert [tk::TextNextWord %W insert]
  350. }
  351. }
  352. bind Text <Meta-f> {
  353. if {!$tk_strictMotif} {
  354. tk::TextSetCursor %W [tk::TextNextWord %W insert]
  355. }
  356. }
  357. bind Text <Meta-less> {
  358. if {!$tk_strictMotif} {
  359. tk::TextSetCursor %W 1.0
  360. }
  361. }
  362. bind Text <Meta-greater> {
  363. if {!$tk_strictMotif} {
  364. tk::TextSetCursor %W end-1c
  365. }
  366. }
  367. bind Text <Meta-BackSpace> {
  368. if {!$tk_strictMotif} {
  369. %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
  370. }
  371. }
  372. bind Text <Meta-Delete> {
  373. if {!$tk_strictMotif} {
  374. %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
  375. }
  376. }
  377. # Bindings for IME text input.
  378. bind Text <<TkStartIMEMarkedText>> {
  379. dict set ::tk::Priv(IMETextMark) "%W" [%W index insert]
  380. }
  381. bind Text <<TkEndIMEMarkedText>> {
  382. if { [catch {dict get $::tk::Priv(IMETextMark) "%W"} mark] } {
  383. bell
  384. } else {
  385. %W tag add IMEmarkedtext $mark insert
  386. %W tag configure IMEmarkedtext -underline on
  387. }
  388. }
  389. bind Text <<TkClearIMEMarkedText>> {
  390. %W delete IMEmarkedtext.first IMEmarkedtext.last
  391. }
  392. bind Text <<TkAccentBackspace>> {
  393. %W delete insert-1c
  394. }
  395. # Macintosh only bindings:
  396. if {[tk windowingsystem] eq "aqua"} {
  397. bind Text <Control-v> {
  398. tk::TextScrollPages %W 1
  399. }
  400. # End of Mac only bindings
  401. }
  402. # A few additional bindings of my own.
  403. bind Text <Control-h> {
  404. if {!$tk_strictMotif && [%W compare insert != 1.0]} {
  405. %W delete insert-1c
  406. %W see insert
  407. }
  408. }
  409. if {[tk windowingsystem] ne "aqua"} {
  410. bind Text <2> {
  411. if {!$tk_strictMotif} {
  412. tk::TextScanMark %W %x %y
  413. }
  414. }
  415. bind Text <B2-Motion> {
  416. if {!$tk_strictMotif} {
  417. tk::TextScanDrag %W %x %y
  418. }
  419. }
  420. } else {
  421. bind Text <3> {
  422. if {!$tk_strictMotif} {
  423. tk::TextScanMark %W %x %y
  424. }
  425. }
  426. bind Text <B3-Motion> {
  427. if {!$tk_strictMotif} {
  428. tk::TextScanDrag %W %x %y
  429. }
  430. }
  431. }
  432. set ::tk::Priv(prevPos) {}
  433. # The MouseWheel will typically only fire on Windows and MacOS X.
  434. # However, someone could use the "event generate" command to produce one
  435. # on other platforms. We must be careful not to round -ve values of %D
  436. # down to zero.
  437. if {[tk windowingsystem] eq "aqua"} {
  438. bind Text <MouseWheel> {
  439. %W yview scroll [expr {-15 * (%D)}] pixels
  440. }
  441. bind Text <Option-MouseWheel> {
  442. %W yview scroll [expr {-150 * (%D)}] pixels
  443. }
  444. bind Text <Shift-MouseWheel> {
  445. %W xview scroll [expr {-15 * (%D)}] pixels
  446. }
  447. bind Text <Shift-Option-MouseWheel> {
  448. %W xview scroll [expr {-150 * (%D)}] pixels
  449. }
  450. } else {
  451. # We must make sure that positive and negative movements are rounded
  452. # equally to integers, avoiding the problem that
  453. # (int)1/3 = 0,
  454. # but
  455. # (int)-1/3 = -1
  456. # The following code ensure equal +/- behaviour.
  457. bind Text <MouseWheel> {
  458. if {%D >= 0} {
  459. %W yview scroll [expr {-%D/3}] pixels
  460. } else {
  461. %W yview scroll [expr {(2-%D)/3}] pixels
  462. }
  463. }
  464. bind Text <Shift-MouseWheel> {
  465. if {%D >= 0} {
  466. %W xview scroll [expr {-%D/3}] pixels
  467. } else {
  468. %W xview scroll [expr {(2-%D)/3}] pixels
  469. }
  470. }
  471. }
  472. if {[tk windowingsystem] eq "x11"} {
  473. # Support for mousewheels on Linux/Unix commonly comes through mapping
  474. # the wheel to the extended buttons. If you have a mousewheel, find
  475. # Linux configuration info at:
  476. # https://linuxreviews.org/HOWTO_change_the_mouse_speed_in_X
  477. bind Text <4> {
  478. if {!$tk_strictMotif} {
  479. %W yview scroll -50 pixels
  480. }
  481. }
  482. bind Text <5> {
  483. if {!$tk_strictMotif} {
  484. %W yview scroll 50 pixels
  485. }
  486. }
  487. bind Text <Shift-4> {
  488. if {!$tk_strictMotif} {
  489. %W xview scroll -50 pixels
  490. }
  491. }
  492. bind Text <Shift-5> {
  493. if {!$tk_strictMotif} {
  494. %W xview scroll 50 pixels
  495. }
  496. }
  497. }
  498. # ::tk::TextClosestGap --
  499. # Given x and y coordinates, this procedure finds the closest boundary
  500. # between characters to the given coordinates and returns the index
  501. # of the character just after the boundary.
  502. #
  503. # Arguments:
  504. # w - The text window.
  505. # x - X-coordinate within the window.
  506. # y - Y-coordinate within the window.
  507. proc ::tk::TextClosestGap {w x y} {
  508. set pos [$w index @$x,$y]
  509. set bbox [$w bbox $pos]
  510. if {$bbox eq ""} {
  511. return $pos
  512. }
  513. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  514. return $pos
  515. }
  516. $w index "$pos + 1 char"
  517. }
  518. # ::tk::TextButton1 --
  519. # This procedure is invoked to handle button-1 presses in text
  520. # widgets. It moves the insertion cursor, sets the selection anchor,
  521. # and claims the input focus.
  522. #
  523. # Arguments:
  524. # w - The text window in which the button was pressed.
  525. # x - The x-coordinate of the button press.
  526. # y - The x-coordinate of the button press.
  527. proc ::tk::TextButton1 {w x y} {
  528. variable ::tk::Priv
  529. set Priv(selectMode) char
  530. set Priv(mouseMoved) 0
  531. set Priv(pressX) $x
  532. set anchorname [tk::TextAnchor $w]
  533. $w mark set insert [TextClosestGap $w $x $y]
  534. $w mark set $anchorname insert
  535. # Set the anchor mark's gravity depending on the click position
  536. # relative to the gap
  537. set bbox [$w bbox [$w index $anchorname]]
  538. if {$x > [lindex $bbox 0]} {
  539. $w mark gravity $anchorname right
  540. } else {
  541. $w mark gravity $anchorname left
  542. }
  543. focus $w
  544. if {[$w cget -autoseparators]} {
  545. $w edit separator
  546. }
  547. }
  548. # ::tk::TextSelectTo --
  549. # This procedure is invoked to extend the selection, typically when
  550. # dragging it with the mouse. Depending on the selection mode (character,
  551. # word, line) it selects in different-sized units. This procedure
  552. # ignores mouse motions initially until the mouse has moved from
  553. # one character to another or until there have been multiple clicks.
  554. #
  555. # Note that the 'anchor' is implemented programmatically using
  556. # a text widget mark, and uses a name that will be unique for each
  557. # text widget (even when there are multiple peers). Currently the
  558. # anchor is considered private to Tk, hence the name 'tk::anchor$w'.
  559. #
  560. # Arguments:
  561. # w - The text window in which the button was pressed.
  562. # x - Mouse x position.
  563. # y - Mouse y position.
  564. set ::tk::Priv(textanchoruid) 0
  565. proc ::tk::TextAnchor {w} {
  566. variable Priv
  567. if {![info exists Priv(textanchor,$w)]} {
  568. set Priv(textanchor,$w) tk::anchor[incr Priv(textanchoruid)]
  569. }
  570. return $Priv(textanchor,$w)
  571. }
  572. proc ::tk::TextSelectTo {w x y {extend 0}} {
  573. variable ::tk::Priv
  574. set anchorname [tk::TextAnchor $w]
  575. set cur [TextClosestGap $w $x $y]
  576. if {[catch {$w index $anchorname}]} {
  577. $w mark set $anchorname $cur
  578. }
  579. set anchor [$w index $anchorname]
  580. if {[$w compare $cur != $anchor] || (abs($Priv(pressX) - $x) >= 3)} {
  581. set Priv(mouseMoved) 1
  582. }
  583. switch -- $Priv(selectMode) {
  584. char {
  585. if {[$w compare $cur < $anchorname]} {
  586. set first $cur
  587. set last $anchorname
  588. } else {
  589. set first $anchorname
  590. set last $cur
  591. }
  592. }
  593. word {
  594. # Set initial range based only on the anchor (1 char min width)
  595. if {[$w mark gravity $anchorname] eq "right"} {
  596. set first $anchorname
  597. set last "$anchorname + 1c"
  598. } else {
  599. set first "$anchorname - 1c"
  600. set last $anchorname
  601. }
  602. # Extend range (if necessary) based on the current point
  603. if {[$w compare $cur < $first]} {
  604. set first $cur
  605. } elseif {[$w compare $cur > $last]} {
  606. set last $cur
  607. }
  608. # Now find word boundaries
  609. set first [TextPrevPos $w "$first + 1c" tcl_wordBreakBefore]
  610. set last [TextNextPos $w "$last - 1c" tcl_wordBreakAfter]
  611. }
  612. line {
  613. # Set initial range based only on the anchor
  614. set first "$anchorname linestart"
  615. set last "$anchorname lineend"
  616. # Extend range (if necessary) based on the current point
  617. if {[$w compare $cur < $first]} {
  618. set first "$cur linestart"
  619. } elseif {[$w compare $cur > $last]} {
  620. set last "$cur lineend"
  621. }
  622. set first [$w index $first]
  623. set last [$w index "$last + 1c"]
  624. }
  625. }
  626. if {$Priv(mouseMoved) || ($Priv(selectMode) ne "char")} {
  627. $w tag remove sel 0.0 end
  628. $w mark set insert $cur
  629. $w tag add sel $first $last
  630. $w tag remove sel $last end
  631. update idletasks
  632. }
  633. }
  634. # ::tk::TextKeyExtend --
  635. # This procedure handles extending the selection from the keyboard,
  636. # where the point to extend to is really the boundary between two
  637. # characters rather than a particular character.
  638. #
  639. # Arguments:
  640. # w - The text window.
  641. # index - The point to which the selection is to be extended.
  642. proc ::tk::TextKeyExtend {w index} {
  643. set anchorname [tk::TextAnchor $w]
  644. set cur [$w index $index]
  645. if {[catch {$w index $anchorname}]} {
  646. $w mark set $anchorname $cur
  647. }
  648. set anchor [$w index $anchorname]
  649. if {[$w compare $cur < $anchorname]} {
  650. set first $cur
  651. set last $anchorname
  652. } else {
  653. set first $anchorname
  654. set last $cur
  655. }
  656. $w tag remove sel 0.0 $first
  657. $w tag add sel $first $last
  658. $w tag remove sel $last end
  659. }
  660. # ::tk::TextPasteSelection --
  661. # This procedure sets the insertion cursor to the mouse position,
  662. # inserts the selection, and sets the focus to the window.
  663. #
  664. # Arguments:
  665. # w - The text window.
  666. # x, y - Position of the mouse.
  667. proc ::tk::TextPasteSelection {w x y} {
  668. $w mark set insert [TextClosestGap $w $x $y]
  669. if {![catch {::tk::GetSelection $w PRIMARY} sel]} {
  670. set oldSeparator [$w cget -autoseparators]
  671. if {$oldSeparator} {
  672. $w configure -autoseparators 0
  673. $w edit separator
  674. }
  675. $w insert insert $sel
  676. if {$oldSeparator} {
  677. $w edit separator
  678. $w configure -autoseparators 1
  679. }
  680. }
  681. if {[$w cget -state] eq "normal"} {
  682. focus $w
  683. }
  684. }
  685. # ::tk::TextAutoScan --
  686. # This procedure is invoked when the mouse leaves a text window
  687. # with button 1 down. It scrolls the window up, down, left, or right,
  688. # depending on where the mouse is (this information was saved in
  689. # ::tk::Priv(x) and ::tk::Priv(y)), and reschedules itself as an "after"
  690. # command so that the window continues to scroll until the mouse
  691. # moves back into the window or the mouse button is released.
  692. #
  693. # Arguments:
  694. # w - The text window.
  695. proc ::tk::TextAutoScan {w} {
  696. variable ::tk::Priv
  697. if {![winfo exists $w]} {
  698. return
  699. }
  700. if {$Priv(y) >= [winfo height $w]} {
  701. $w yview scroll [expr {1 + $Priv(y) - [winfo height $w]}] pixels
  702. } elseif {$Priv(y) < 0} {
  703. $w yview scroll [expr {-1 + $Priv(y)}] pixels
  704. } elseif {$Priv(x) >= [winfo width $w]} {
  705. $w xview scroll 2 units
  706. } elseif {$Priv(x) < 0} {
  707. $w xview scroll -2 units
  708. } else {
  709. return
  710. }
  711. TextSelectTo $w $Priv(x) $Priv(y)
  712. set Priv(afterId) [after 50 [list tk::TextAutoScan $w]]
  713. }
  714. # ::tk::TextSetCursor
  715. # Move the insertion cursor to a given position in a text. Also
  716. # clears the selection, if there is one in the text, and makes sure
  717. # that the insertion cursor is visible. Also, don't let the insertion
  718. # cursor appear on the dummy last line of the text.
  719. #
  720. # Arguments:
  721. # w - The text window.
  722. # pos - The desired new position for the cursor in the window.
  723. proc ::tk::TextSetCursor {w pos} {
  724. if {[$w compare $pos == end]} {
  725. set pos {end - 1 chars}
  726. }
  727. $w mark set insert $pos
  728. $w tag remove sel 1.0 end
  729. $w see insert
  730. if {[$w cget -autoseparators]} {
  731. $w edit separator
  732. }
  733. }
  734. # ::tk::TextKeySelect
  735. # This procedure is invoked when stroking out selections using the
  736. # keyboard. It moves the cursor to a new position, then extends
  737. # the selection to that position.
  738. #
  739. # Arguments:
  740. # w - The text window.
  741. # new - A new position for the insertion cursor (the cursor hasn't
  742. # actually been moved to this position yet).
  743. proc ::tk::TextKeySelect {w new} {
  744. set anchorname [tk::TextAnchor $w]
  745. if {[$w tag nextrange sel 1.0 end] eq ""} {
  746. if {[$w compare $new < insert]} {
  747. $w tag add sel $new insert
  748. } else {
  749. $w tag add sel insert $new
  750. }
  751. $w mark set $anchorname insert
  752. } else {
  753. if {[catch {$w index $anchorname}]} {
  754. $w mark set $anchorname insert
  755. }
  756. if {[$w compare $new < $anchorname]} {
  757. set first $new
  758. set last $anchorname
  759. } else {
  760. set first $anchorname
  761. set last $new
  762. }
  763. $w tag remove sel 1.0 $first
  764. $w tag add sel $first $last
  765. $w tag remove sel $last end
  766. }
  767. $w mark set insert $new
  768. $w see insert
  769. update idletasks
  770. }
  771. # ::tk::TextResetAnchor --
  772. # Set the selection anchor to whichever end is farthest from the
  773. # index argument. One special trick: if the selection has two or
  774. # fewer characters, just leave the anchor where it is. In this
  775. # case it doesn't matter which point gets chosen for the anchor,
  776. # and for the things like Shift-Left and Shift-Right this produces
  777. # better behavior when the cursor moves back and forth across the
  778. # anchor.
  779. #
  780. # Arguments:
  781. # w - The text widget.
  782. # index - Position at which mouse button was pressed, which determines
  783. # which end of selection should be used as anchor point.
  784. proc ::tk::TextResetAnchor {w index} {
  785. if {[$w tag ranges sel] eq ""} {
  786. # Don't move the anchor if there is no selection now; this
  787. # makes the widget behave "correctly" when the user clicks
  788. # once, then shift-clicks somewhere -- ie, the area between
  789. # the two clicks will be selected. [Bug: 5929].
  790. return
  791. }
  792. set anchorname [tk::TextAnchor $w]
  793. set a [$w index $index]
  794. set b [$w index sel.first]
  795. set c [$w index sel.last]
  796. if {[$w compare $a < $b]} {
  797. $w mark set $anchorname sel.last
  798. return
  799. }
  800. if {[$w compare $a > $c]} {
  801. $w mark set $anchorname sel.first
  802. return
  803. }
  804. scan $a "%d.%d" lineA chA
  805. scan $b "%d.%d" lineB chB
  806. scan $c "%d.%d" lineC chC
  807. if {$lineB < $lineC+2} {
  808. set total [string length [$w get $b $c]]
  809. if {$total <= 2} {
  810. return
  811. }
  812. if {[string length [$w get $b $a]] < ($total/2)} {
  813. $w mark set $anchorname sel.last
  814. } else {
  815. $w mark set $anchorname sel.first
  816. }
  817. return
  818. }
  819. if {($lineA-$lineB) < ($lineC-$lineA)} {
  820. $w mark set $anchorname sel.last
  821. } else {
  822. $w mark set $anchorname sel.first
  823. }
  824. }
  825. # ::tk::TextCursorInSelection --
  826. # Check whether the selection exists and contains the insertion cursor. Note
  827. # that it assumes that the selection is contiguous.
  828. #
  829. # Arguments:
  830. # w - The text widget whose selection is to be checked
  831. proc ::tk::TextCursorInSelection {w} {
  832. expr {
  833. [llength [$w tag ranges sel]]
  834. && [$w compare sel.first <= insert]
  835. && [$w compare sel.last >= insert]
  836. }
  837. }
  838. # ::tk::TextInsert --
  839. # Insert a string into a text at the point of the insertion cursor.
  840. # If there is a selection in the text, and it covers the point of the
  841. # insertion cursor, then delete the selection before inserting.
  842. #
  843. # Arguments:
  844. # w - The text window in which to insert the string
  845. # s - The string to insert (usually just a single character)
  846. proc ::tk::TextInsert {w s} {
  847. if {$s eq "" || [$w cget -state] eq "disabled"} {
  848. return
  849. }
  850. set compound 0
  851. if {[TextCursorInSelection $w]} {
  852. set oldSeparator [$w cget -autoseparators]
  853. if {$oldSeparator} {
  854. $w configure -autoseparators 0
  855. $w edit separator
  856. set compound 1
  857. }
  858. $w delete sel.first sel.last
  859. }
  860. $w insert insert $s
  861. $w see insert
  862. if {$compound && $oldSeparator} {
  863. $w edit separator
  864. $w configure -autoseparators 1
  865. }
  866. }
  867. # ::tk::TextUpDownLine --
  868. # Returns the index of the character one display line above or below the
  869. # insertion cursor. There is a tricky thing here: we want to maintain the
  870. # original x position across repeated operations, even though some lines
  871. # that will get passed through don't have enough characters to cover the
  872. # original column.
  873. #
  874. # Arguments:
  875. # w - The text window in which the cursor is to move.
  876. # n - The number of display lines to move: -1 for up one line,
  877. # +1 for down one line.
  878. proc ::tk::TextUpDownLine {w n} {
  879. variable ::tk::Priv
  880. set i [$w index insert]
  881. if {$Priv(prevPos) ne $i} {
  882. set Priv(textPosOrig) $i
  883. }
  884. set lines [$w count -displaylines $Priv(textPosOrig) $i]
  885. set new [$w index \
  886. "$Priv(textPosOrig) + [expr {$lines + $n}] displaylines"]
  887. set Priv(prevPos) $new
  888. if {[$w compare $new == "end display lineend"] \
  889. || [$w compare $new == "insert display linestart"]} {
  890. set Priv(textPosOrig) $new
  891. }
  892. return $new
  893. }
  894. # ::tk::TextPrevPara --
  895. # Returns the index of the beginning of the paragraph just before a given
  896. # position in the text (the beginning of a paragraph is the first non-blank
  897. # character after a blank line).
  898. #
  899. # Arguments:
  900. # w - The text window in which the cursor is to move.
  901. # pos - Position at which to start search.
  902. proc ::tk::TextPrevPara {w pos} {
  903. set pos [$w index "$pos linestart"]
  904. while {1} {
  905. if {([$w get "$pos - 1 line"] eq "\n" && ([$w get $pos] ne "\n")) \
  906. || $pos eq "1.0"} {
  907. if {[regexp -indices -- {^[ \t]+(.)} \
  908. [$w get $pos "$pos lineend"] -> index]} {
  909. set pos [$w index "$pos + [lindex $index 0] chars"]
  910. }
  911. if {[$w compare $pos != insert] || [lindex [split $pos .] 0]==1} {
  912. return $pos
  913. }
  914. }
  915. set pos [$w index "$pos - 1 line"]
  916. }
  917. }
  918. # ::tk::TextNextPara --
  919. # Returns the index of the beginning of the paragraph just after a given
  920. # position in the text (the beginning of a paragraph is the first non-blank
  921. # character after a blank line).
  922. #
  923. # Arguments:
  924. # w - The text window in which the cursor is to move.
  925. # start - Position at which to start search.
  926. proc ::tk::TextNextPara {w start} {
  927. set pos [$w index "$start linestart + 1 line"]
  928. while {[$w get $pos] ne "\n"} {
  929. if {[$w compare $pos == end]} {
  930. return [$w index "end - 1c"]
  931. }
  932. set pos [$w index "$pos + 1 line"]
  933. }
  934. while {[$w get $pos] eq "\n"} {
  935. set pos [$w index "$pos + 1 line"]
  936. if {[$w compare $pos == end]} {
  937. return [$w index "end - 1c"]
  938. }
  939. }
  940. if {[regexp -indices -- {^[ \t]+(.)} \
  941. [$w get $pos "$pos lineend"] -> index]} {
  942. return [$w index "$pos + [lindex $index 0] chars"]
  943. }
  944. return $pos
  945. }
  946. # ::tk::TextScrollPages --
  947. # This is a utility procedure used in bindings for moving up and down
  948. # pages and possibly extending the selection along the way. It scrolls
  949. # the view in the widget by the number of pages, and it returns the
  950. # index of the character that is at the same position in the new view
  951. # as the insertion cursor used to be in the old view.
  952. #
  953. # Arguments:
  954. # w - The text window in which the cursor is to move.
  955. # count - Number of pages forward to scroll; may be negative
  956. # to scroll backwards.
  957. proc ::tk::TextScrollPages {w count} {
  958. set bbox [$w bbox insert]
  959. $w yview scroll $count pages
  960. if {$bbox eq ""} {
  961. return [$w index @[expr {[winfo height $w]/2}],0]
  962. }
  963. return [$w index @[lindex $bbox 0],[lindex $bbox 1]]
  964. }
  965. # ::tk::TextTranspose --
  966. # This procedure implements the "transpose" function for text widgets.
  967. # It tranposes the characters on either side of the insertion cursor,
  968. # unless the cursor is at the end of the line. In this case it
  969. # transposes the two characters to the left of the cursor. In either
  970. # case, the cursor ends up to the right of the transposed characters.
  971. #
  972. # Arguments:
  973. # w - Text window in which to transpose.
  974. proc ::tk::TextTranspose w {
  975. set pos insert
  976. if {[$w compare $pos != "$pos lineend"]} {
  977. set pos [$w index "$pos + 1 char"]
  978. }
  979. set new [$w get "$pos - 1 char"][$w get "$pos - 2 char"]
  980. if {[$w compare "$pos - 1 char" == 1.0]} {
  981. return
  982. }
  983. # ensure this is seen as an atomic op to undo
  984. set autosep [$w cget -autoseparators]
  985. if {$autosep} {
  986. $w configure -autoseparators 0
  987. $w edit separator
  988. }
  989. $w delete "$pos - 2 char" $pos
  990. $w insert insert $new
  991. $w see insert
  992. if {$autosep} {
  993. $w edit separator
  994. $w configure -autoseparators $autosep
  995. }
  996. }
  997. # ::tk_textCopy --
  998. # This procedure copies the selection from a text widget into the
  999. # clipboard.
  1000. #
  1001. # Arguments:
  1002. # w - Name of a text widget.
  1003. proc ::tk_textCopy w {
  1004. if {![catch {set data [$w get sel.first sel.last]}]} {
  1005. clipboard clear -displayof $w
  1006. clipboard append -displayof $w $data
  1007. }
  1008. }
  1009. # ::tk_textCut --
  1010. # This procedure copies the selection from a text widget into the
  1011. # clipboard, then deletes the selection (if it exists in the given
  1012. # widget).
  1013. #
  1014. # Arguments:
  1015. # w - Name of a text widget.
  1016. proc ::tk_textCut w {
  1017. if {![catch {set data [$w get sel.first sel.last]}]} {
  1018. # make <<Cut>> an atomic operation on the Undo stack,
  1019. # i.e. separate it from other delete operations on either side
  1020. set oldSeparator [$w cget -autoseparators]
  1021. if {([$w cget -state] eq "normal") && $oldSeparator} {
  1022. $w edit separator
  1023. }
  1024. clipboard clear -displayof $w
  1025. clipboard append -displayof $w $data
  1026. $w delete sel.first sel.last
  1027. if {([$w cget -state] eq "normal") && $oldSeparator} {
  1028. $w edit separator
  1029. }
  1030. }
  1031. }
  1032. # ::tk_textPaste --
  1033. # This procedure pastes the contents of the clipboard to the insertion
  1034. # point in a text widget.
  1035. #
  1036. # Arguments:
  1037. # w - Name of a text widget.
  1038. proc ::tk_textPaste w {
  1039. if {![catch {::tk::GetSelection $w CLIPBOARD} sel]} {
  1040. set oldSeparator [$w cget -autoseparators]
  1041. if {$oldSeparator} {
  1042. $w configure -autoseparators 0
  1043. $w edit separator
  1044. }
  1045. if {[tk windowingsystem] ne "x11"} {
  1046. catch { $w delete sel.first sel.last }
  1047. }
  1048. $w insert insert $sel
  1049. if {$oldSeparator} {
  1050. $w edit separator
  1051. $w configure -autoseparators 1
  1052. }
  1053. }
  1054. }
  1055. # ::tk::TextNextWord --
  1056. # Returns the index of the next word position after a given position in the
  1057. # text. The next word is platform dependent and may be either the next
  1058. # end-of-word position or the next start-of-word position after the next
  1059. # end-of-word position.
  1060. #
  1061. # Arguments:
  1062. # w - The text window in which the cursor is to move.
  1063. # start - Position at which to start search.
  1064. if {[tk windowingsystem] eq "win32"} {
  1065. proc ::tk::TextNextWord {w start} {
  1066. TextNextPos $w [TextNextPos $w $start tcl_endOfWord] \
  1067. tcl_startOfNextWord
  1068. }
  1069. } else {
  1070. proc ::tk::TextNextWord {w start} {
  1071. TextNextPos $w $start tcl_endOfWord
  1072. }
  1073. }
  1074. # ::tk::TextNextPos --
  1075. # Returns the index of the next position after the given starting
  1076. # position in the text as computed by a specified function.
  1077. #
  1078. # Arguments:
  1079. # w - The text window in which the cursor is to move.
  1080. # start - Position at which to start search.
  1081. # op - Function to use to find next position.
  1082. proc ::tk::TextNextPos {w start op} {
  1083. set text ""
  1084. set cur $start
  1085. while {[$w compare $cur < end]} {
  1086. set text $text[$w get -displaychars $cur "$cur lineend + 1c"]
  1087. set pos [$op $text 0]
  1088. if {$pos >= 0} {
  1089. return [$w index "$start + $pos display chars"]
  1090. }
  1091. set cur [$w index "$cur lineend +1c"]
  1092. }
  1093. return end
  1094. }
  1095. # ::tk::TextPrevPos --
  1096. # Returns the index of the previous position before the given starting
  1097. # position in the text as computed by a specified function.
  1098. #
  1099. # Arguments:
  1100. # w - The text window in which the cursor is to move.
  1101. # start - Position at which to start search.
  1102. # op - Function to use to find next position.
  1103. proc ::tk::TextPrevPos {w start op} {
  1104. set text ""
  1105. set cur $start
  1106. while {[$w compare $cur > 0.0]} {
  1107. set text [$w get -displaychars "$cur linestart - 1c" $cur]$text
  1108. set pos [$op $text end]
  1109. if {$pos >= 0} {
  1110. return [$w index "$cur linestart - 1c + $pos display chars"]
  1111. }
  1112. set cur [$w index "$cur linestart - 1c"]
  1113. }
  1114. return 0.0
  1115. }
  1116. # ::tk::TextScanMark --
  1117. #
  1118. # Marks the start of a possible scan drag operation
  1119. #
  1120. # Arguments:
  1121. # w - The text window from which the text to get
  1122. # x - x location on screen
  1123. # y - y location on screen
  1124. proc ::tk::TextScanMark {w x y} {
  1125. variable ::tk::Priv
  1126. $w scan mark $x $y
  1127. set Priv(x) $x
  1128. set Priv(y) $y
  1129. set Priv(mouseMoved) 0
  1130. }
  1131. # ::tk::TextScanDrag --
  1132. #
  1133. # Marks the start of a possible scan drag operation
  1134. #
  1135. # Arguments:
  1136. # w - The text window from which the text to get
  1137. # x - x location on screen
  1138. # y - y location on screen
  1139. proc ::tk::TextScanDrag {w x y} {
  1140. variable ::tk::Priv
  1141. # Make sure these exist, as some weird situations can trigger the
  1142. # motion binding without the initial press. [Bug #220269]
  1143. if {![info exists Priv(x)]} {
  1144. set Priv(x) $x
  1145. }
  1146. if {![info exists Priv(y)]} {
  1147. set Priv(y) $y
  1148. }
  1149. if {($x != $Priv(x)) || ($y != $Priv(y))} {
  1150. set Priv(mouseMoved) 1
  1151. }
  1152. if {[info exists Priv(mouseMoved)] && $Priv(mouseMoved)} {
  1153. $w scan dragto $x $y
  1154. }
  1155. }
上海开阖软件有限公司 沪ICP备12045867号-1