home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tk3.3b1 / library / text.tcl < prev    next >
Encoding:
Text File  |  1993-07-01  |  3.7 KB  |  124 lines

  1. # text.tcl --
  2. #
  3. # This file contains Tcl procedures used to manage Tk entries.
  4. #
  5. # $Header: /user6/ouster/wish/library/RCS/text.tcl,v 1.3 93/07/01 13:42:07 ouster Exp $ SPRITE (Berkeley)
  6. #
  7. # Copyright (c) 1992-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27.  
  28. # The procedure below is invoked when dragging one end of the selection.
  29. # The arguments are the text window name and the index of the character
  30. # that is to be the new end of the selection.
  31.  
  32. proc tk_textSelectTo {w index} {
  33.     global tk_priv
  34.  
  35.     case $tk_priv(selectMode) {
  36.     char {
  37.         if [$w compare $index < anchor] {
  38.         set first $index
  39.         set last anchor
  40.         } else {
  41.         set first anchor
  42.         set last [$w index $index+1c]
  43.         }
  44.     }
  45.     word {
  46.         if [$w compare $index < anchor] {
  47.         set first [$w index "$index wordstart"]
  48.         set last [$w index "anchor wordend"]
  49.         } else {
  50.         set first [$w index "anchor wordstart"]
  51.         set last [$w index "$index wordend"]
  52.         }
  53.     }
  54.     line {
  55.         if [$w compare $index < anchor] {
  56.         set first [$w index "$index linestart"]
  57.         set last [$w index "anchor lineend + 1c"]
  58.         } else {
  59.         set first [$w index "anchor linestart"]
  60.         set last [$w index "$index lineend + 1c"]
  61.         }
  62.     }
  63.     }
  64.     $w tag remove sel 0.0 $first
  65.     $w tag add sel $first $last
  66.     $w tag remove sel $last end
  67. }
  68.  
  69. # The procedure below is invoked to backspace over one character in
  70. # a text widget.  The name of the widget is passed as argument.
  71.  
  72. proc tk_textBackspace w {
  73.     $w delete insert-1c insert
  74. }
  75.  
  76. # The procedure below compares three indices, a, b, and c.  Index b must
  77. # be less than c.  The procedure returns 1 if a is closer to b than to c,
  78. # and 0 otherwise.  The "w" argument is the name of the text widget in
  79. # which to do the comparison.
  80.  
  81. proc tk_textIndexCloser {w a b c} {
  82.     set a [$w index $a]
  83.     set b [$w index $b]
  84.     set c [$w index $c]
  85.     if [$w compare $a <= $b] {
  86.     return 1
  87.     }
  88.     if [$w compare $a >= $c] {
  89.     return 0
  90.     }
  91.     scan $a "%d.%d" lineA chA
  92.     scan $b "%d.%d" lineB chB
  93.     scan $c "%d.%d" lineC chC
  94.     if {$chC == 0} {
  95.     incr lineC -1
  96.     set chC [string length [$w get $lineC.0 $lineC.end]]
  97.     }
  98.     if {$lineB != $lineC} {
  99.     return [expr {($lineA-$lineB) < ($lineC-$lineA)}]
  100.     }
  101.     return [expr {($chA-$chB) < ($chC-$chA)}]
  102. }
  103.  
  104. # The procedure below is called to reset the selection anchor to
  105. # whichever end is FARTHEST from the index argument.
  106.  
  107. proc tk_textResetAnchor {w index} {
  108.     global tk_priv
  109.     if {[$w tag ranges sel] == ""} {
  110.     set tk_priv(selectMode) char
  111.     $w mark set anchor $index
  112.     return
  113.     }
  114.     if [tk_textIndexCloser $w $index sel.first sel.last] {
  115.     if {$tk_priv(selectMode) == "char"} {
  116.         $w mark set anchor sel.last
  117.     } else {
  118.         $w mark set anchor sel.last-1c
  119.     }
  120.     } else {
  121.     $w mark set anchor sel.first
  122.     }
  123. }
  124.