home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / System Extensions / Macintosh Drag and Drop 1.1.1 / Demo Applications / DragText Sources / teutilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-13  |  4.1 KB  |  169 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        teutilities.c
  4.  *
  5.  *        Supplemental TextEdit utilities.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Monday, February 3, 1992
  10.  *
  11.  *        Copyright © 1992 Apple Computer, Inc.
  12.  *
  13.  */
  14.  
  15. #include <Script.h>
  16.  
  17.  
  18. /*
  19.  *    TEICut is an intelligent version of TECut. If the selection is a range
  20.  *    of words, TEICut cuts the selection and removes a space from either the
  21.  *    beginning or end of the selection point when the result of cutting leaves
  22.  *    one too many spaces in a sentence. The resulting change in size of the
  23.  *    TextEdit record is returned.
  24.  *
  25.  *    NOTE: This function does not work properly in all situations. It may still
  26.  *        need some work.
  27.  */
  28.  
  29. short TEICut(TEHandle theTE)
  30.  
  31. {    OffsetTable        startOffsets, endOffsets;
  32.     short            selStart, selEnd, characters;
  33.     Handle            theScrap;
  34.  
  35.     if (!(characters = (**theTE).selStart - (**theTE).selEnd))
  36.         return(0);
  37.  
  38.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, true,
  39.                     0L, startOffsets);
  40.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, false,
  41.                     0L, endOffsets);
  42.  
  43.     if ((startOffsets[0].offFirst == (**theTE).selStart) &&
  44.         (endOffsets[0].offSecond  == (**theTE).selEnd)) {
  45.  
  46.         /*
  47.          *    Both the beginning and end of the current selection is on
  48.          *    word boundaries.
  49.          */
  50.  
  51.         selStart = (**theTE).selStart;
  52.         selEnd = (**theTE).selEnd;
  53.         if ((*((**theTE).hText))[selStart - 1] == ' ') {
  54.             TESetSelect(selStart - 1, selEnd, theTE);
  55.             TECut(theTE);
  56.             theScrap = TEScrapHandle();
  57.             BlockMove((char *) (*theScrap) + 1, (*theScrap), TEGetScrapLen() - 1);
  58.             TESetScrapLen(TEGetScrapLen() - 1);
  59.             ZeroScrap();
  60.             TEToScrap();
  61.             characters--;
  62.         } else if ((*((**theTE).hText))[selEnd] == ' ') {
  63.             TESetSelect(selStart, selEnd + 1, theTE);
  64.             TECut(theTE);
  65.             TESetScrapLen(TEGetScrapLen() - 1);
  66.             ZeroScrap();
  67.             TEToScrap();
  68.             characters--;
  69.         } else {
  70.             TECut(theTE);
  71.         }
  72.     } else {
  73.         TECut(theTE);
  74.     }
  75.     return(characters);
  76. }
  77.  
  78.  
  79. /*
  80.  *    TEIPaste is an intelligent version of TEPaste. When pasting into a sentence,
  81.  *    an extra space may be added to either the start or end of the insertion
  82.  *    to maintain readibility of the resulting sentence. Optionally, pointers
  83.  *    may be provided to spaceBefore and/or spaceAfter. These flags return
  84.  *    true if a space was inserted before or after the insertion respectively.
  85.  *
  86.  *    NOTE: This function does not always work properly. It still needs work.
  87.  */
  88.  
  89. short TEIPaste(TEHandle theTE, short *spaceBefore, short *spaceAfter)
  90.  
  91. {    OffsetTable        startOffsets, endOffsets;
  92.     short            addSpaceAfter, characters;
  93.  
  94.     characters = (**theTE).selStart - (**theTE).selEnd;
  95.  
  96.     if (spaceBefore)
  97.         *spaceBefore = false;
  98.     if (spaceAfter)
  99.         *spaceAfter  = false;
  100.  
  101.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, false,
  102.                     0L, startOffsets);
  103.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, true,
  104.                     0L, endOffsets);
  105.  
  106.     addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) &&
  107.                      ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
  108.  
  109.     if ((startOffsets[0].offSecond == (**theTE).selStart) &&
  110.             ((*((**theTE).hText))[(**theTE).selStart - 1] != ' ')) {
  111.         TEKey(' ', theTE);
  112.         characters++;
  113.         if (spaceBefore)
  114.             *spaceBefore = true;
  115.     }
  116.  
  117.     TEPaste(theTE);
  118.     characters += TEGetScrapLen();
  119.  
  120.     if (addSpaceAfter) {
  121.         TEKey(' ', theTE);
  122.         characters++;
  123.         if (spaceAfter)
  124.             *spaceAfter = true;
  125.     }
  126.     return(characters);
  127. }
  128.  
  129.  
  130. /*
  131.  *    TEIsFrontOfLine, given an offset and a TextEdit handle, returns true if
  132.  *    the given offset is at the beginning of a line start.
  133.  */
  134.  
  135. short TEIsFrontOfLine(short offset, TEHandle theTE)
  136.  
  137. {    short        line = 0;
  138.  
  139.     if ((**theTE).teLength == 0)
  140.         return(true);
  141.  
  142.     if (offset >= (**theTE).teLength)
  143.         return( (*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d );
  144.  
  145.     while ((**theTE).lineStarts[line] < offset)
  146.         line++;
  147.  
  148.     return( (**theTE).lineStarts[line] == offset );
  149. }
  150.  
  151.  
  152. /*
  153.  *    TEGetLine, given an offset and a TextEdit handle, returns the line number
  154.  *    of the line that contains the offset.
  155.  */
  156.  
  157. short TEGetLine(short offset, TEHandle theTE)
  158.  
  159. {    short        line = 0;
  160.  
  161.     if (offset > (**theTE).teLength)
  162.         return((**theTE).nLines);
  163.  
  164.     while ((**theTE).lineStarts[line] < offset)
  165.         line++;
  166.     
  167.     return(line);
  168. }
  169.