home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / MacSource / TE32K / TE32K.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-09  |  86.6 KB  |  3,072 lines  |  [TEXT/KAHL]

  1. /**************************************************************************************
  2. *
  3. *        FILE:    TE32K.c
  4. *                Replacement for Text Edit routines
  5. *        
  6. *        EDIT HISTORY:
  7. *
  8. *            v1.2    32 December 1992    Roy Wood <rrwood@canrem.com>
  9. *            v1.2a    11 January 1994        Michael J. Lowe <michaell@gsb.usyd.edu.au>
  10. *
  11. *                                            - implemented tripple click support
  12. *                                            - implemented TE32KFeatureFlag
  13. *                                                - te32KFOutlineHilite
  14. *                                                - te32KFShowCarridgeReturns
  15. *                                            - implemented TE32KGetHiliteRgn for 'Drag and Drop'
  16. *                                            - numerous bug fixes
  17. *                                                        
  18. *                                            - Changes for THINK C 6.0x/7.0x
  19. *                                            - Changes for MPW
  20. *                                            - Changes for Universal Headers
  21. *                                            - Changes for Mac on RISC SDK
  22. *            v1.2b    4/25/1996            Eduard Schwan (71513.2161@compuserve.com)
  23. *                                            - Added #ifndef around header for precompiled headers
  24. *
  25. ***************************************************************************************/
  26.  
  27. #pragma segment TE32K
  28.  
  29. #include <LowMem.h>
  30. #include <Errors.h>
  31. #include <Events.h>
  32. #include <Memory.h>
  33. #include <Scrap.h>
  34.  
  35. #include "TE32K.h"
  36.  
  37. #define    EXTRALINESTARTS        32L
  38. #define    EXTRATEXTBUFF        256L
  39.  
  40. #define    LEFTARROW            28
  41. #define    RIGHTARROW            29
  42. #define UPARROW                30
  43. #define DOWNARROW            31
  44. #define    TAB                    '\t'
  45. #define DELETE                0x08
  46. #define    RETURN                0x0D
  47. #define    ENTER                0x03
  48.  
  49.  
  50.  
  51. Handle            TE32KScrpHandle = nil;
  52. long            TE32KScrpLength = 0L;
  53. TE32KHandle        clickedTE32KH = nil;
  54.  
  55.  
  56.  
  57. static long        LineEndIndex(long, TE32KHandle);
  58. static void        CalParagraph(long, TE32KHandle, long *, long *);
  59. static long     paraLines(long, TE32KHandle);
  60. static void     updateLine(long, TE32KHandle, short, LongRect *);
  61. static void        invertSelRange(long, long, TE32KHandle);
  62. static void        xorCaret(TE32KHandle);
  63. static long        indexToLine(long, TE32KHandle);
  64. static short    shiftKeyDown(void);
  65. static void        getSelRangeRgn(RgnHandle, long,long,TE32KHandle);
  66. static void     MyClickLoop(void);
  67.  
  68.  
  69.  
  70.  
  71. void TE32KSetFontStuff(short txFont, short txFace, short txMode, short txSize, TE32KHandle theTE32KHandle)
  72. {
  73.     register short        i;
  74.     short                oldFont,oldFace,oldSize,oldMode;
  75.     GrafPtr                oldPort;
  76.     FontInfo            theFontInfo;
  77.     GrafPtr             currentPort;
  78.     
  79.     currentPort = (**theTE32KHandle).inPort;
  80.     
  81.     GetPort(&oldPort);
  82.     SetPort(currentPort);
  83.     oldFont = currentPort->txFont;
  84.     oldFace = currentPort->txFace;
  85.     oldSize = currentPort->txSize;
  86.     oldMode = currentPort->txMode;
  87.     
  88.     TextFont((**theTE32KHandle).txFont = txFont);
  89.     TextFace((**theTE32KHandle).txFace = txFace);
  90.     TextSize((**theTE32KHandle).txSize = txSize);
  91.     TextMode((**theTE32KHandle).txMode = txMode);
  92.     
  93.     for (i=0;i<256;i++)
  94.         (**theTE32KHandle).theCharWidths[i] = CharWidth((unsigned char) i);
  95.     
  96.     GetFontInfo(&theFontInfo);
  97.     (**theTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  98.     (**theTE32KHandle).fontAscent = theFontInfo.ascent;
  99.     
  100.     if ((**theTE32KHandle).tabChars)
  101.         (**theTE32KHandle).tabWidth = (**theTE32KHandle).tabChars * (**theTE32KHandle).theCharWidths[' '];
  102.     
  103.     TextFont(oldFont);
  104.     TextFace(oldFace);
  105.     TextSize(oldSize);
  106.     TextMode(oldMode);
  107.     
  108.     SetPort(oldPort);
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. void SetLongRect(LongRect *theLongRect, long left, long top, long right, long bottom)
  116. {
  117.     theLongRect->left = left;
  118.     theLongRect->top = top;
  119.     theLongRect->right = right;
  120.     theLongRect->bottom = bottom;
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. void RectToLongRect(Rect *theRect, LongRect *theLongRect)
  128. {
  129.     theLongRect->left = (long) theRect->left;
  130.     theLongRect->top = (long) theRect->top;
  131.     theLongRect->right = (long) theRect->right;
  132.     theLongRect->bottom = (long) theRect->bottom;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. void LongRectToRect(LongRect *theLongRect, Rect *theRect)
  139. {
  140.     if (theLongRect->left < -32768L)
  141.         theRect->left = (short) -32768;
  142.     else if (theLongRect->left > 32767L)
  143.         theRect->left = (short) 32767;
  144.     else
  145.         theRect->left = (short) theLongRect->left;
  146.     
  147.     if (theLongRect->top < -32768L)
  148.         theRect->top = (short) -32768;
  149.     else if (theLongRect->top > 32767L)
  150.         theRect->top = (short) 32767;
  151.     else
  152.         theRect->top = (short) theLongRect->top;
  153.     
  154.     if (theLongRect->right < -32768L)
  155.         theRect->right = (short) -32768;
  156.     else if (theLongRect->right > 32767L)
  157.         theRect->right = (short) 32767;
  158.     else
  159.         theRect->right = (short) theLongRect->right;
  160.     
  161.     if (theLongRect->bottom < -32768L)
  162.         theRect->bottom = (short) -32768;
  163.     else if (theLongRect->bottom > 32767L)
  164.         theRect->bottom = (short) 32767;
  165.     else
  166.         theRect->bottom = (short) theLongRect->bottom;
  167. }
  168.  
  169.  
  170.  
  171. void OffsetLongRect(LongRect *theLongRect, long x, long y)
  172. {
  173.     theLongRect->left += x;
  174.     theLongRect->top += y;
  175.     theLongRect->right += x;
  176.     theLongRect->bottom += y;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. static long    indexToLine(long selIndex, TE32KHandle theTE32KHandle)
  183. {
  184.     register long    i, delta;
  185.  
  186.     if (theTE32KHandle) {
  187.         if (selIndex<=0L || (**theTE32KHandle).nLines<=1L || (**theTE32KHandle).teLength<1L)
  188.             return(0L);
  189.         else if (selIndex >= (**theTE32KHandle).teLength)
  190.             return((long) ((**theTE32KHandle).nLines - 1L));
  191.         else {
  192.             i = ((**theTE32KHandle).nLines) >> 1;
  193.             
  194.             delta = ((**theTE32KHandle).nLines) >> 1;
  195.             if (delta < 1L) delta = 1L;
  196.             
  197.             while (delta > 0L) {
  198.                 if (selIndex == (**theTE32KHandle).lineStarts[i])
  199.                     delta = 0L;
  200.                 else if (selIndex > (**theTE32KHandle).lineStarts[i]) {
  201.                     if (selIndex < (**theTE32KHandle).lineStarts[i+1])
  202.                         delta = 0L;
  203.                     else
  204.                         i += delta;
  205.                 } else i -= delta;
  206.                 
  207.                 if (delta) {
  208.                     delta >>= 1;
  209.                     if (delta < 1L) delta = 1L;
  210.                 }
  211.             }
  212.         }
  213.         
  214.         if (i < 0L)
  215.             i = 0L;
  216.         else if (i >= (**theTE32KHandle).nLines)
  217.             i = (**theTE32KHandle).nLines - 1L;
  218.         
  219.         return((long) i);
  220.     } else return((long) 0L);
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. static void    xorCaret(TE32KHandle theTE32KHandle)
  230. {
  231.     GrafPtr            oldPort;
  232.     PenState        oldPenState;
  233.     Point            selPt;
  234.     RgnHandle        oldClipRgn;
  235.     Rect            theClipRect;
  236.  
  237.     if (theTE32KHandle && (**theTE32KHandle).active && (**theTE32KHandle).selStart==(**theTE32KHandle).selEnd) {
  238.         if (!(**theTE32KHandle).caretState && ((**theTE32KHandle).selStart < 0 || (**theTE32KHandle).selEnd > (**theTE32KHandle).teLength))
  239.             return;
  240.         
  241.         GetPort(&oldPort);
  242.         SetPort((**theTE32KHandle).inPort);
  243.         
  244.         GetPenState(&oldPenState);
  245.         GetClip(oldClipRgn = NewRgn());
  246.         
  247.         theClipRect.left = (short) (**theTE32KHandle).viewRect.left;
  248.         theClipRect.top = (short) (**theTE32KHandle).viewRect.top;
  249.         theClipRect.right = (short) (**theTE32KHandle).viewRect.right;
  250.         theClipRect.bottom = (short) (**theTE32KHandle).viewRect.bottom;
  251.         
  252.         ClipRect(&theClipRect);
  253.         
  254.         PenNormal();
  255.         PenMode(patXor);
  256.         
  257.         if ((**theTE32KHandle).selPoint.h < -32768L)
  258.             selPt.h = (short) -32768;
  259.         else if ((**theTE32KHandle).selPoint.h > 32767L)
  260.             selPt.h = (short) 32767;
  261.         else
  262.             selPt.h = (short) (**theTE32KHandle).selPoint.h;
  263.         
  264.         if ((**theTE32KHandle).selPoint.v < -32768L)
  265.             selPt.v = (short) -32768;
  266.         else if ((**theTE32KHandle).selPoint.v > 32767L)
  267.             selPt.v = (short) 32767;
  268.         else
  269.             selPt.v = (short) (**theTE32KHandle).selPoint.v;
  270.         
  271.         MoveTo(selPt.h - 1, selPt.v);
  272.         Line(0, -(**theTE32KHandle).fontAscent);
  273.         
  274.         (**theTE32KHandle).caretTime = TickCount() + GetCaretTime();
  275.         (**theTE32KHandle).caretState = !(**theTE32KHandle).caretState;
  276.         
  277.         SetClip(oldClipRgn);
  278.         DisposeRgn(oldClipRgn);
  279.         
  280.         SetPenState(&oldPenState);
  281.         SetPort(oldPort);
  282.     }
  283. }
  284.  
  285.  
  286.  
  287.  
  288. void TE32KInit()
  289. {
  290.     TE32KScrpHandle = NewHandle(0L);
  291.     TE32KScrpLength = 0L;
  292. }
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299. TE32KHandle    TE32KNew(LongRect *destRect, LongRect *viewRect)
  300. {
  301.     TE32KHandle        newTE32KHandle;
  302.     Handle            hText;
  303.     GrafPtr            activePort;
  304.     FontInfo        theFontInfo;
  305.     LongPoint        selPt;
  306. //    OSErr            err;    // [esp]
  307.  
  308.     newTE32KHandle = (TE32KHandle) NewHandle(sizeof(TE32KRec) + (long)sizeof(long)*EXTRALINESTARTS);
  309.     if (MemError()) return((TE32KHandle) nil);
  310.     
  311.     hText = NewHandle(EXTRATEXTBUFF);
  312.     if (MemError()) {
  313.         DisposeHandle((Handle) newTE32KHandle);
  314.         return((TE32KHandle) nil);
  315.     }
  316.     
  317.     (**newTE32KHandle).destRect = *destRect;
  318.     (**newTE32KHandle).viewRect = *viewRect;
  319.     
  320.     GetPort(&activePort);
  321.     GetFontInfo(&theFontInfo);
  322.     
  323.     (**newTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  324.     (**newTE32KHandle).fontAscent = theFontInfo.ascent;
  325.     
  326.     (**newTE32KHandle).selStart =
  327.     (**newTE32KHandle).selEnd =
  328.     
  329.     (**newTE32KHandle).teLength = 0L;
  330.     (**newTE32KHandle).hText = hText;
  331.     
  332.     (**newTE32KHandle).txFont = activePort->txFont;
  333.     (**newTE32KHandle).txFace = activePort->txFace;
  334.     (**newTE32KHandle).txMode = activePort->txMode;
  335.     (**newTE32KHandle).txSize = activePort->txSize;
  336.     
  337.     (**newTE32KHandle).inPort = activePort;
  338.     
  339.     (**newTE32KHandle).tabWidth = 25;
  340.     (**newTE32KHandle).tabChars = 0;
  341.     
  342.     (**newTE32KHandle).maxLineWidth = 32767;
  343.     
  344.     (**newTE32KHandle).clikStuff = false;
  345.     
  346.     (**newTE32KHandle).crOnly = 0x00;
  347.     
  348.     (**newTE32KHandle).featureFlags = 0;
  349.     
  350.     (**newTE32KHandle).nLines = 1L;
  351.     (**newTE32KHandle).lineStarts[0] =
  352.     (**newTE32KHandle).lineStarts[1] = 0L;
  353.     
  354.     (**newTE32KHandle).active = true;
  355.     (**newTE32KHandle).caretState = false;
  356.     (**newTE32KHandle).caretTime = TickCount();
  357.     
  358.     (**newTE32KHandle).dblClick = false;
  359.     (**newTE32KHandle).clickTime = TickCount();
  360.     (**newTE32KHandle).clickLoc = -1L;
  361.     
  362.     TE32KGetPoint((**newTE32KHandle).selStart, &selPt, newTE32KHandle);
  363.     
  364.     (**newTE32KHandle).selPoint = selPt;
  365.     
  366.     (**newTE32KHandle).clikLoop = nil;
  367.     
  368.     TE32KSetFontStuff((**newTE32KHandle).txFont, (**newTE32KHandle).txFace, (**newTE32KHandle).txMode, (**newTE32KHandle).txSize, newTE32KHandle);
  369.     
  370.     return(newTE32KHandle);
  371. }
  372.  
  373.  
  374.  
  375.  
  376.  
  377. void TE32KDispose(TE32KHandle theTE32KHandle)
  378. {
  379.     if (theTE32KHandle) {
  380.         if ((**theTE32KHandle).hText)
  381.             DisposeHandle((**theTE32KHandle).hText);
  382.         
  383.         DisposeHandle((Handle) theTE32KHandle);
  384.     }
  385. }
  386.  
  387.  
  388.  
  389.  
  390. void TE32KCalText(TE32KHandle theTE32KHandle)
  391. {
  392.     register unsigned char    *charPtr;
  393.     register long            charCount;
  394.     register short            *theCharWidths,lineLength,crOnly,maxLineWidth;
  395.     register unsigned char    ch;
  396.     long                    nLines,maxLineStarts,sizeTE32KHandle;
  397.     unsigned char            *charBase;
  398.     Point                    cursorPt;
  399.     short                    rightSide,destLeftSide,tabWidth,maxRewind;
  400.     unsigned char            *oldCharPtr;
  401.     long                    oldCharCount,tempOffset;
  402.  
  403.     if (theTE32KHandle) {
  404.         (**theTE32KHandle).lineStarts[0] =            /* assume the worst can happen and prepare for it */
  405.         (**theTE32KHandle).lineStarts[1] = 0L;
  406.         (**theTE32KHandle).nLines = 1L;
  407.         
  408.         sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  409.         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  410.         
  411.         crOnly = (**theTE32KHandle).crOnly;
  412.         maxLineWidth = (**theTE32KHandle).maxLineWidth;
  413.         
  414.         lineLength = nLines = 0L;
  415.         
  416.         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  417.         charPtr = charBase;
  418.         charCount = (**theTE32KHandle).teLength;
  419.         
  420.         
  421.         if (charCount > 0L) {
  422.             rightSide = (short) ((**theTE32KHandle).destRect.right);
  423.             destLeftSide = (short) ((**theTE32KHandle).destRect.left + 1L);
  424.             cursorPt.h = destLeftSide;
  425.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  426.             
  427.             theCharWidths = (**theTE32KHandle).theCharWidths;
  428.             
  429.             while (charCount--) {
  430.                 ch = *charPtr++;
  431.                 lineLength++;
  432.                 
  433.                 if (!crOnly) {
  434.                     if (ch == TAB)
  435.                         cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  436.                     else if (ch != '\r' && ch != '\n')
  437.                         cursorPt.h += theCharWidths[ch];
  438.                 }
  439.                 
  440.                 if ((ch == '\r' || ch == '\n') || (ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth)) {
  441.                     if ((ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth)) {
  442.                         /* I should probably add a hook for custom word-breaking */
  443.                         
  444.                         maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[nLines];
  445.                         oldCharPtr = charPtr;
  446.                         oldCharCount = charCount;
  447.                         
  448.                         charPtr--;
  449.                         charCount++;
  450.                         maxRewind--;
  451.                         
  452.                         while (*charPtr != ' ' && maxRewind > 0) {
  453.                             charPtr--;
  454.                             charCount++;
  455.                             maxRewind--;
  456.                         }
  457.                         
  458.                         if (maxRewind <= 0) {
  459.                             charPtr = oldCharPtr;
  460.                             charCount = oldCharCount;
  461.                         } else {
  462.                             charPtr++;
  463.                             charCount--;
  464.                         }
  465.                     }
  466.                     
  467.                     if (nLines >= maxLineStarts) {
  468.                         tempOffset = charPtr - charBase;
  469.                                                 
  470.                         sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  471.                         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  472.                         
  473.                         SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  474.                         if (MemError()) return;
  475.                         
  476.                         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  477.                         charPtr = charBase + tempOffset;
  478.                         theCharWidths = (**theTE32KHandle).theCharWidths;
  479.                     }
  480.                     
  481.                     (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  482.                     
  483.                     cursorPt.h = destLeftSide;
  484.                     lineLength = 0L;
  485.                 }
  486.             }
  487.             
  488.             if (nLines >= maxLineStarts) {
  489.                 sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  490.                 
  491.                 SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  492.                 if (MemError()) return;
  493.             }
  494.             
  495.             (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;    
  496.             (**theTE32KHandle).nLines = nLines;
  497.         }
  498.     }
  499. }
  500.  
  501.  
  502.  
  503.  
  504.  
  505. void TE32KUseTextHandle(Handle hText, long textLength, TE32KHandle theTE32KHandle)
  506. {
  507.     LongPoint    selPt;
  508.  
  509.     if (theTE32KHandle) {
  510.         if ((**theTE32KHandle).hText) DisposeHandle((**theTE32KHandle).hText);
  511.         
  512.         SetHandleSize(hText, textLength + EXTRATEXTBUFF);
  513.         
  514.         (**theTE32KHandle).hText = hText;
  515.         (**theTE32KHandle).teLength =
  516.         
  517.         (**theTE32KHandle).selStart =
  518.         (**theTE32KHandle).selEnd = textLength;
  519.         
  520.         TE32KGetPoint((**theTE32KHandle).selStart, &selPt, theTE32KHandle);
  521.         (**theTE32KHandle).selPoint = selPt;
  522.         
  523.         TE32KCalText(theTE32KHandle);
  524.     }
  525. }
  526.  
  527.  
  528.  
  529.  
  530.  
  531. void TE32KSetText(Ptr textPtr,long textLength, TE32KHandle theTE32KHandle)
  532. {
  533.     Handle        hText;
  534.     LongPoint    selPt;
  535. //    OSErr        err;    // [esp]
  536.  
  537.     if (theTE32KHandle) {
  538.         hText = NewHandle(textLength + EXTRATEXTBUFF);
  539.         if (MemError()) return;
  540.         
  541.         if ((**theTE32KHandle).hText) DisposeHandle((**theTE32KHandle).hText);
  542.         
  543.         HLock(hText);
  544.         BlockMoveData(textPtr,*hText,textLength);
  545.         HUnlock(hText);
  546.         
  547.         (**theTE32KHandle).hText = hText;
  548.         (**theTE32KHandle).teLength =
  549.         
  550.         (**theTE32KHandle).selStart =
  551.         (**theTE32KHandle).selEnd = textLength;
  552.         
  553.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  554.         (**theTE32KHandle).selPoint = selPt;
  555.         
  556.         TE32KCalText(theTE32KHandle);
  557.     }
  558. }
  559.  
  560.  
  561.  
  562. Handle    TE32KGetText(TE32KHandle theTE32KHandle)
  563. {
  564.     if (theTE32KHandle){
  565.         return (**theTE32KHandle).hText;
  566.     } else return nil;
  567. }
  568.  
  569.  
  570.  
  571.  
  572. void    TE32KUpdate(LongRect *updateLongRect,TE32KHandle theTE32KHandle)
  573. {
  574.     LongRect                    tempLongRect;
  575.     Rect                        theClipRect,viewRect,updateRect;
  576.     GrafPtr                        oldPort,currentPort;
  577.     RgnHandle                    oldClipRgn;
  578.     register unsigned char        *textPtr;
  579.     register long                firstLine,lastLine,i,thisStart,nextStart,tabWidth;
  580.     Point                        cursorPt;
  581.     short                        oldFont,oldFace,oldSize,oldMode;
  582.     short                        rightSide,destLeftSide;
  583.     LongPoint                    selPt;
  584.     unsigned char                oldCaretState;
  585.     RGBColor                    fgSaveColor, returnGray = {0xD000, 0xD000, 0xD000};
  586.     Boolean                        showReturns, isCurrentPortColor;
  587.  
  588.     if (theTE32KHandle && (**theTE32KHandle).inPort) {
  589.         showReturns = TE32KFeatureFlag(te32KFShowCarridgeReturns, TE32KBitTest, theTE32KHandle);
  590.         tempLongRect = (**theTE32KHandle).viewRect;
  591.         LongRectToRect(&tempLongRect,&viewRect);
  592.         
  593.         tempLongRect = *updateLongRect;
  594.         tempLongRect.top = (**theTE32KHandle).destRect.top + ((updateLongRect->top - (**theTE32KHandle).destRect.top)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  595.         tempLongRect.bottom = (**theTE32KHandle).destRect.top + ((updateLongRect->bottom - (**theTE32KHandle).destRect.top + (**theTE32KHandle).lineHeight - 1L)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  596.         LongRectToRect(&tempLongRect,&updateRect);
  597.         
  598.         if (SectRect(&viewRect,&updateRect,&theClipRect)) {
  599.             GetPort(&oldPort);
  600.             currentPort = (**theTE32KHandle).inPort;
  601.             SetPort(currentPort);
  602.             
  603.             isCurrentPortColor =  (((CGrafPtr)currentPort)->portVersion & 0x8000) != 0;
  604.             
  605.             GetClip(oldClipRgn = NewRgn());
  606.             ClipRect(&theClipRect);
  607.             
  608.             oldCaretState = (**theTE32KHandle).caretState;
  609.             if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && oldCaretState)
  610.                 xorCaret(theTE32KHandle);
  611.             
  612.             firstLine = ((long) theClipRect.top - (**theTE32KHandle).destRect.top)/(long) (**theTE32KHandle).lineHeight;
  613.             lastLine = ((long) theClipRect.bottom - (**theTE32KHandle).destRect.top - 1L)/(long) (**theTE32KHandle).lineHeight;
  614.             
  615.             if (firstLine < 0) firstLine = 0;
  616.             if (lastLine >= (**theTE32KHandle).nLines) lastLine = (**theTE32KHandle).nLines - 1L;
  617.             if (firstLine > lastLine) lastLine = firstLine;
  618.             
  619.             EraseRect(&theClipRect);
  620.             
  621.             if (firstLine < (**theTE32KHandle).nLines && (**theTE32KHandle).teLength > 0L) {
  622.                 rightSide = theClipRect.right;
  623.                 cursorPt.h = destLeftSide = (short) (**theTE32KHandle).destRect.left + 1L;
  624.                 cursorPt.v = (short) ((**theTE32KHandle).destRect.top + firstLine * (long) (**theTE32KHandle).lineHeight + (long) (**theTE32KHandle).fontAscent);
  625.                 
  626.                 oldFont = currentPort->txFont;
  627.                 oldFace = currentPort->txFace;
  628.                 oldSize = currentPort->txSize;
  629.                 oldMode = currentPort->txMode;
  630.                 
  631.                 TextFont((**theTE32KHandle).txFont);
  632.                 TextFace((**theTE32KHandle).txFace);
  633.                 TextSize((**theTE32KHandle).txSize);
  634.                 TextMode((**theTE32KHandle).txMode);
  635.                 
  636.                 HLock((**theTE32KHandle).hText);
  637.                 
  638.                 textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  639.                 tabWidth = (long) (**theTE32KHandle).tabWidth;
  640.                 
  641.                 while (firstLine <= lastLine) {
  642.                     i = thisStart = (**theTE32KHandle).lineStarts[firstLine];
  643.                     nextStart = (**theTE32KHandle).lineStarts[firstLine+1];
  644.                     
  645.                     if (nextStart > thisStart && (textPtr[nextStart-1] == '\r' || textPtr[nextStart-1] == '\n'))
  646.                         nextStart--;
  647.                     
  648.                     MoveTo(cursorPt.h,cursorPt.v);
  649.                     while (thisStart < nextStart) {
  650.                         while (i<nextStart && textPtr[i]!=TAB)
  651.                             i++;
  652.                         
  653.                         if (i > thisStart)
  654.                             DrawText(&(textPtr[thisStart]), 0, (short) (i - thisStart));
  655.                         
  656.                         if (i<nextStart && textPtr[i]==TAB) {
  657.                             MoveTo(destLeftSide + ((currentPort->pnLoc.h - destLeftSide + tabWidth)/tabWidth)*tabWidth,currentPort->pnLoc.v);
  658.                             i++;
  659.                         }
  660.                         
  661.                         thisStart = i;
  662.                         if (currentPort->pnLoc.h > theClipRect.right)
  663.                             thisStart = nextStart;
  664.                     }
  665.                     
  666.                     if (showReturns && thisStart < (**theTE32KHandle).teLength && textPtr[thisStart]) {
  667.                         if (isCurrentPortColor) {
  668.                             GetForeColor(&fgSaveColor);
  669.                             RGBForeColor(&returnGray);
  670.                         }
  671.                         DrawChar('¶');
  672.                         if (isCurrentPortColor)
  673.                             RGBForeColor(&fgSaveColor);
  674.                     }
  675.                     
  676.                     firstLine++;
  677.                     cursorPt.v += (**theTE32KHandle).lineHeight;
  678.                 }
  679.                     
  680.                 HUnlock((**theTE32KHandle).hText);
  681.                 
  682.                 TextFont(oldFont);
  683.                 TextFace(oldFace);
  684.                 TextSize(oldSize);
  685.                 TextMode(oldMode);
  686.             }
  687.             
  688.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  689.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  690.             else {
  691.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  692.                 (**theTE32KHandle).selPoint = selPt;
  693.                 
  694.                 if (oldCaretState) xorCaret(theTE32KHandle);
  695.             }
  696.             
  697.             SetClip(oldClipRgn);
  698.             DisposeRgn(oldClipRgn);
  699.             
  700.             SetPort(oldPort);
  701.         }
  702.     }
  703. }
  704.  
  705.  
  706.  
  707.  
  708.  
  709. void    TE32KScroll(long horiz,long vert,TE32KHandle theTE32KHandle)
  710. {
  711.     LongRect    updateLongRect;
  712.     Rect        scrollRect;
  713.     RgnHandle    updateRgn;
  714.     GrafPtr        oldPort;
  715.     LongPoint    selPt;
  716.  
  717.     if (theTE32KHandle && (**theTE32KHandle).inPort && (horiz || vert)) {
  718.         GetPort(&oldPort);
  719.         SetPort((**theTE32KHandle).inPort);
  720.         
  721.         (**theTE32KHandle).destRect.left += horiz;
  722.         (**theTE32KHandle).destRect.top += vert;
  723.         (**theTE32KHandle).destRect.right += horiz;
  724.         (**theTE32KHandle).destRect.bottom += vert;
  725.         
  726.         (**theTE32KHandle).selPoint.h += horiz;
  727.         (**theTE32KHandle).selPoint.v += vert;
  728.         selPt = (**theTE32KHandle).selPoint;
  729.         
  730.         scrollRect.left = (short) ((**theTE32KHandle).viewRect.left);
  731.         scrollRect.top = (short) ((**theTE32KHandle).viewRect.top);
  732.         scrollRect.right = (short) ((**theTE32KHandle).viewRect.right);
  733.         scrollRect.bottom = (short) ((**theTE32KHandle).viewRect.bottom);
  734.         
  735.         if (horiz < ((**theTE32KHandle).viewRect.right-(**theTE32KHandle).viewRect.left) ||
  736.             vert < ((**theTE32KHandle).viewRect.bottom-(**theTE32KHandle).viewRect.top)) {            
  737.             ScrollRect(&scrollRect,(short) horiz,(short) vert,updateRgn = NewRgn());
  738.             
  739.             updateLongRect.left = (**updateRgn).rgnBBox.left;
  740.             updateLongRect.top = (**updateRgn).rgnBBox.top;
  741.             updateLongRect.right = (**updateRgn).rgnBBox.right;
  742.             updateLongRect.bottom = (**updateRgn).rgnBBox.bottom;
  743.  
  744.             DisposeRgn(updateRgn);
  745.             
  746.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  747.             
  748.             if ((**theTE32KHandle).caretState) xorCaret(theTE32KHandle);
  749.             (**theTE32KHandle).selPoint = selPt;
  750.             xorCaret(theTE32KHandle);
  751.         } else {
  752.             updateLongRect = (**theTE32KHandle).viewRect;
  753.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  754.             
  755.             if ((**theTE32KHandle).caretState) xorCaret(theTE32KHandle);
  756.             (**theTE32KHandle).selPoint = selPt;
  757.             xorCaret(theTE32KHandle);
  758.         }
  759.         
  760.         SetPort(oldPort);
  761.     }
  762. }
  763.  
  764.  
  765.  
  766.  
  767.  
  768. void    TE32KActivate(TE32KHandle theTE32KHandle)
  769. {
  770.     if (theTE32KHandle && !((**theTE32KHandle).active)) {
  771.         (**theTE32KHandle).caretState = false;
  772.             
  773.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  774.         (**theTE32KHandle).active = true;
  775.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  776.     }
  777. }
  778.  
  779.  
  780.  
  781.  
  782.  
  783. void    TE32KIdle(TE32KHandle theTE32KHandle)
  784. {
  785.     if (theTE32KHandle && (**theTE32KHandle).active && TickCount() >= (**theTE32KHandle).caretTime) {
  786.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  787.             xorCaret(theTE32KHandle);
  788.     }
  789. }
  790.  
  791.  
  792.  
  793.  
  794.  
  795. void    TE32KDeactivate(TE32KHandle theTE32KHandle)
  796. {
  797.     if (theTE32KHandle && (**theTE32KHandle).active) {
  798.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd) {
  799.             if ((**theTE32KHandle).caretState)
  800.                 xorCaret(theTE32KHandle);
  801.             (**theTE32KHandle).active = false;
  802.         } else {
  803.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  804.             (**theTE32KHandle).active = false;
  805.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  806.         }
  807.     }
  808. }
  809.  
  810.  
  811.  
  812. void    TE32KGetPoint(long selIndex,LongPoint *selPt,TE32KHandle theTE32KHandle)
  813. {
  814.     register unsigned char    *textPtr;
  815.     register short            *theCharWidths;
  816.     register long            i,thisStart,tabWidth;
  817.     long                    x,y,lineIndex,destLeftSide;
  818.     unsigned char            ch;
  819.     LongPoint                origPt;
  820.     short                    clikStuff;
  821.  
  822.     if (theTE32KHandle) {
  823.         if (selIndex<=0L || (**theTE32KHandle).teLength<1L) {
  824.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  825.             selPt->v = (**theTE32KHandle).destRect.top + (**theTE32KHandle).fontAscent;
  826.             (**theTE32KHandle).clikStuff = false;
  827.             
  828.             return;
  829.         }
  830.         
  831.         clikStuff = (**theTE32KHandle).clikStuff;
  832.         (**theTE32KHandle).clikStuff = false;
  833.         
  834.         origPt = *selPt;
  835.         
  836.         i = indexToLine(selIndex,theTE32KHandle);
  837.         
  838.         y = (**theTE32KHandle).destRect.top + ((**theTE32KHandle).lineHeight * i) + (**theTE32KHandle).fontAscent;
  839.         
  840.         selPt->v = y;
  841.         
  842.         if (!(**theTE32KHandle).crOnly && clikStuff && i > 0 && selIndex == (**theTE32KHandle).lineStarts[i]) {
  843.             i--;
  844.             selPt->v -= (**theTE32KHandle).lineHeight;
  845.         } else if (selIndex < (**theTE32KHandle).lineStarts[i] || (selIndex == (**theTE32KHandle).lineStarts[i] && i < 1)) {
  846.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  847.             return;
  848.         }
  849.         
  850.         
  851.         HLock((**theTE32KHandle).hText);
  852.         
  853.         lineIndex  = i;
  854.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  855.         
  856.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  857.         x = destLeftSide;
  858.         
  859.         thisStart = (**theTE32KHandle).lineStarts[lineIndex];
  860.         
  861.         theCharWidths = (**theTE32KHandle).theCharWidths;
  862.         
  863.         if (textPtr[selIndex-1] != '\r' && textPtr[selIndex-1] != '\n') {
  864.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  865.             
  866.             while (thisStart < selIndex) {
  867.                 ch = textPtr[thisStart++];
  868.                 
  869.                 if (ch == TAB)
  870.                     x = destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  871.                 else
  872.                     x += theCharWidths[ch];
  873.             }
  874.         }
  875.         
  876.         HUnlock((**theTE32KHandle).hText);
  877.         
  878.         selPt->h = x;
  879.     }
  880. }
  881.  
  882.  
  883.  
  884.  
  885. long    TE32KGetOffset(LongPoint *selPt,TE32KHandle theTE32KHandle)
  886. {
  887.     register unsigned char    *textPtr;
  888.     register short            *theCharWidths;
  889.     register long            i,delta,firstChar,lastChar,tabWidth;
  890.     unsigned char            done;
  891.     long                    x,y,selIndex,horiz,destLeftSide;
  892.  
  893.     if (theTE32KHandle) {
  894.         if ((**theTE32KHandle).teLength < 1L)
  895.             return(0L);
  896.         
  897.         horiz = selPt->h;
  898.         
  899.         y = selPt->v - (**theTE32KHandle).destRect.top;
  900.         
  901.         i = y / (long) (**theTE32KHandle).lineHeight;
  902.         
  903.         if (i < 0L)
  904.             return(0L);
  905.         
  906.         if (i >= (**theTE32KHandle).nLines)
  907.             return((**theTE32KHandle).teLength);
  908.         
  909.         theCharWidths = (**theTE32KHandle).theCharWidths;
  910.         
  911.         HLock((**theTE32KHandle).hText);
  912.         
  913.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  914.         
  915.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  916.         x = destLeftSide;
  917.         delta = 0L;
  918.         
  919.         firstChar = (**theTE32KHandle).lineStarts[i];
  920.         lastChar = (**theTE32KHandle).lineStarts[i+1L];
  921.         
  922.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  923.         
  924.         if (firstChar<lastChar && x+delta<horiz) {
  925.             done = false;
  926.         
  927.             while (!done) {
  928.                 if (textPtr[firstChar] != TAB)
  929.                     delta = (long) theCharWidths[textPtr[firstChar]];
  930.                 else
  931.                     delta = (destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth) - x;
  932.                 
  933.                 firstChar++;
  934.                 
  935.                 if (firstChar >= lastChar) {    
  936.                     if (textPtr[lastChar - 1L] == '\r' || textPtr[lastChar - 1L] == '\n')
  937.                         selIndex = lastChar - 1L;
  938.                     else
  939.                         selIndex = lastChar;
  940.                     
  941.                     done = true;
  942.                 } else if (x+delta >= horiz) {
  943.                     if (horiz >= x + (delta >> 1))
  944.                         selIndex = firstChar;
  945.                     else
  946.                         selIndex = --firstChar;
  947.                     
  948.                     done = true;
  949.                 } else
  950.                     x += delta;
  951.             }
  952.         } else selIndex = firstChar;
  953.  
  954.         HUnlock((**theTE32KHandle).hText);
  955.         
  956.         return (selIndex);
  957.     }
  958.     return 0;    // [esp]
  959. }
  960.  
  961.  
  962.  
  963. static void    getSelRangeRgn(RgnHandle selectedRgn, long selStart,long selEnd,TE32KHandle theTE32KHandle)
  964. {
  965.     Rect        viewRect, tempRect1, tempRect2, tempRect3, theRect;
  966.     long        firstLine, lastLine;
  967.     LongPoint    selPt;
  968.     RgnHandle    tempRgn;
  969.     
  970.     viewRect.left = (short) ((**theTE32KHandle).viewRect.left);
  971.     viewRect.top = (short) ((**theTE32KHandle).viewRect.top);
  972.     viewRect.right = (short) ((**theTE32KHandle).viewRect.right);
  973.     viewRect.bottom = (short) ((**theTE32KHandle).viewRect.bottom);
  974.     
  975.     if (selStart > selEnd) {
  976.         firstLine = selStart;
  977.         selStart = selEnd;
  978.         selEnd = firstLine;
  979.     }
  980.     
  981.     firstLine = indexToLine(selStart, theTE32KHandle);
  982.     lastLine = indexToLine(selEnd, theTE32KHandle);
  983.     
  984.     TE32KGetPoint(selStart, &selPt, theTE32KHandle);
  985.     
  986.     selPt.v -= (**theTE32KHandle).fontAscent;
  987.     if (selStart <= (**theTE32KHandle).lineStarts[firstLine])
  988.         selPt.h--;
  989.         
  990.     if (selPt.h < -32768L)
  991.         tempRect1.left = (short) -32768;
  992.     else if (selPt.h > 32767L)
  993.         tempRect1.left = (short) 32767;
  994.     else
  995.         tempRect1.left = (short) selPt.h;
  996.     
  997.     if (selPt.v < -32768L)
  998.         tempRect1.top = (short) -32768;
  999.     else if (selPt.v > 32767L)
  1000.         tempRect1.top = (short) 32767;
  1001.     else
  1002.         tempRect1.top = (short) selPt.v;
  1003.     
  1004.     if (firstLine != lastLine) {
  1005.         tempRect1.right = viewRect.right;
  1006.         tempRect1.bottom = tempRect1.top + (**theTE32KHandle).lineHeight;
  1007.     } else {
  1008.         TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1009.         
  1010.         selPt.v -= (**theTE32KHandle).fontAscent;
  1011.         selPt.v += (**theTE32KHandle).lineHeight;
  1012.         
  1013.         if (selPt.h < -32768L)
  1014.             tempRect1.right = (short) -32768;
  1015.         else if (selPt.h > 32767L)
  1016.             tempRect1.right = (short) 32767;
  1017.         else
  1018.             tempRect1.right = (short) selPt.h;
  1019.         
  1020.         if (selPt.v < -32768L)
  1021.             tempRect1.bottom = (short) -32768;
  1022.         else if (selPt.v > 32767L)
  1023.             tempRect1.bottom = (short) 32767;
  1024.         else
  1025.             tempRect1.bottom = (short) selPt.v;
  1026.     }
  1027.     
  1028.     if (SectRect(&viewRect, &tempRect1, &theRect))
  1029.         RectRgn(selectedRgn, &theRect);
  1030.     
  1031.     if (lastLine > firstLine + 1L) {
  1032.         TE32KGetPoint(selEnd, &selPt, theTE32KHandle);
  1033.         
  1034.         tempRect2.left = viewRect.left;
  1035.         tempRect2.top = tempRect1.bottom;
  1036.         tempRect2.right = viewRect.right;
  1037.         
  1038.         selPt.v -= (**theTE32KHandle).fontAscent;
  1039.         
  1040.         if (selPt.v < -32768L)
  1041.             tempRect2.bottom = (short) -32768;
  1042.         else if (selPt.v > 32767L)
  1043.             tempRect2.bottom = (short) 32767;
  1044.         else
  1045.             tempRect2.bottom = (short) selPt.v;
  1046.         
  1047.         selPt.v += (**theTE32KHandle).fontAscent;
  1048.         
  1049.         if (SectRect(&viewRect, &tempRect2, &theRect)) {
  1050.             RectRgn(tempRgn=NewRgn(), &theRect);
  1051.             UnionRgn(tempRgn, selectedRgn, selectedRgn);
  1052.             DisposeRgn(tempRgn);
  1053.         }
  1054.     }
  1055.     
  1056.     if (lastLine > firstLine && selEnd > (**theTE32KHandle).lineStarts[lastLine]) {
  1057.         if (lastLine == firstLine + 1L)
  1058.             TE32KGetPoint(selEnd, &selPt, theTE32KHandle);
  1059.         
  1060.         selPt.v -= (**theTE32KHandle).fontAscent;
  1061.         
  1062.         if (selPt.v < -32768L)
  1063.             tempRect3.top = (short) -32768;
  1064.         else if (selPt.v > 32767L)
  1065.             tempRect3.top = (short) 32767;
  1066.         else
  1067.             tempRect3.top = (short) selPt.v;
  1068.         
  1069.         selPt.v += (**theTE32KHandle).lineHeight;
  1070.         
  1071.         if (selPt.v < -32768L)
  1072.             tempRect3.bottom = (short) -32768;
  1073.         else if (selPt.v > 32767L)
  1074.             tempRect3.bottom = (short) 32767;
  1075.         else
  1076.             tempRect3.bottom = (short) selPt.v;
  1077.         
  1078.         
  1079.         tempRect3.left = viewRect.left;
  1080.         
  1081.         if (selPt.h < -32768L)
  1082.             tempRect3.right = (short) -32768;
  1083.         else if (selPt.h > 32767L)
  1084.             tempRect3.right = (short) 32767;
  1085.         else
  1086.             tempRect3.right = (short) selPt.h;
  1087.         
  1088.         if (SectRect(&viewRect, &tempRect3, &theRect)) {
  1089.             RectRgn(tempRgn=NewRgn(), &theRect);
  1090.             UnionRgn(tempRgn, selectedRgn, selectedRgn);
  1091.             DisposeRgn(tempRgn);
  1092.         }
  1093.     }
  1094. }
  1095.  
  1096.  
  1097.  
  1098. void TE32KGetHiliteRgn(RgnHandle selectedRgn, TE32KHandle theTE32KHandle)
  1099. {
  1100.     getSelRangeRgn(selectedRgn, (**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,
  1101.                     theTE32KHandle);
  1102. }
  1103.  
  1104.  
  1105.  
  1106. static void    invertSelRange(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1107. {
  1108.     GrafPtr            oldPort;
  1109.     RgnHandle        selectedRgn;
  1110.     PenState        penState;
  1111.     LongPoint        selPt;
  1112.     unsigned char    oldMode;
  1113.  
  1114.     if (theTE32KHandle) {
  1115.         // Clear caret
  1116.         if ((**theTE32KHandle).caretState)
  1117.             xorCaret(theTE32KHandle);
  1118.         
  1119.         // Invert caret
  1120.         if (selStart == selEnd) {
  1121.             TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1122.             (**theTE32KHandle).selPoint = selPt;
  1123.             (**theTE32KHandle).selStart = selStart;
  1124.             (**theTE32KHandle).selEnd = selStart;
  1125.             
  1126.             xorCaret(theTE32KHandle);
  1127.             return;
  1128.         }
  1129.         
  1130.         // else Invert selection range
  1131.         GetPort(&oldPort);
  1132.         SetPort((**theTE32KHandle).inPort);
  1133.         
  1134.         selectedRgn = NewRgn();
  1135.         getSelRangeRgn(selectedRgn, selStart, selEnd, theTE32KHandle);
  1136.         if (!EmptyRgn(selectedRgn)) {
  1137.             oldMode = LMGetHiliteMode();
  1138.             LMSetHiliteMode(1);
  1139.             if ((**theTE32KHandle).active)
  1140.                 InvertRgn(selectedRgn);
  1141.             else if (TE32KFeatureFlag(te32KFOutlineHilite, TE32KBitTest, theTE32KHandle)) {
  1142.                 GetPenState(&penState);
  1143.                 PenMode(srcXor);
  1144.                 FrameRgn(selectedRgn);
  1145.                 SetPenState(&penState);
  1146.             }
  1147.             LMSetHiliteMode(oldMode);
  1148.         }
  1149.         DisposeRgn(selectedRgn);
  1150.         
  1151.         SetPort(oldPort);
  1152.     }
  1153. }
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159. void    TE32KClick(Point startPoint,unsigned char extend,TE32KHandle theTE32KHandle)
  1160. {
  1161.     register long                selIndex,selAnchor,selLast,teLength, selLine;
  1162.     register unsigned char        *textPtr;
  1163.     LongPoint                    selPt,tempPt;
  1164.     Point                        mousePt;
  1165.     GrafPtr                        oldPort;
  1166.     unsigned char                ch;
  1167.     long                        oldClickLoc,wordStart,wordEnd;
  1168.  
  1169.  
  1170.     if (theTE32KHandle && (**theTE32KHandle).active) {
  1171.         clickedTE32KH = theTE32KHandle;
  1172.         
  1173.         teLength = (**theTE32KHandle).teLength;
  1174.         
  1175.         selPt.h = (long) startPoint.h;
  1176.         selPt.v = (long) startPoint.v;
  1177.         
  1178.         selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1179.         
  1180.         oldClickLoc = (**theTE32KHandle).clickLoc;
  1181.         (**theTE32KHandle).clickLoc = selIndex;
  1182.  
  1183.         if ((**theTE32KHandle).caretState)
  1184.             xorCaret(theTE32KHandle);
  1185.         
  1186.         if (!extend && teLength > 0L && TickCount() < (**theTE32KHandle).clickTime + GetDblTime() && (**theTE32KHandle).dblClick && oldClickLoc == selIndex) {
  1187.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1188.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1189.             
  1190.             selLine = indexToLine(selIndex, theTE32KHandle);
  1191.             wordStart = (**theTE32KHandle).selStart = (**theTE32KHandle).lineStarts[selLine];
  1192.             wordEnd = (**theTE32KHandle).selEnd = (**theTE32KHandle).lineStarts[selLine+1];
  1193.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1194.             
  1195.             while (Button()) {
  1196.                 if ((**theTE32KHandle).clikLoop)
  1197.                     (*((**theTE32KHandle).clikLoop)) ();
  1198.                 
  1199.                 GetMouse(&mousePt);
  1200.                 selPt.h = mousePt.h;
  1201.                 selPt.v = mousePt.v;
  1202.             
  1203.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1204.                 
  1205.                 if (selIndex < 0)
  1206.                     selIndex = 0;
  1207.                 else if (selIndex > teLength)
  1208.                     selIndex = teLength;
  1209.                 
  1210.                 selLine = indexToLine(selIndex, theTE32KHandle);
  1211.                 if (selIndex < wordStart)
  1212.                     selIndex = (**theTE32KHandle).lineStarts[selLine];
  1213.                 else if (selIndex > wordEnd)
  1214.                     selIndex = (**theTE32KHandle).lineStarts[selLine+1];
  1215.             
  1216.                 if (selIndex < wordEnd && wordEnd < (**theTE32KHandle).selEnd) {
  1217.                     invertSelRange(wordEnd, (**theTE32KHandle).selEnd, theTE32KHandle);
  1218.                     (**theTE32KHandle).selEnd = wordEnd;
  1219.                 }
  1220.             
  1221.                 if (selIndex > wordStart && wordStart > (**theTE32KHandle).selStart) {
  1222.                     invertSelRange((**theTE32KHandle).selStart, wordStart, theTE32KHandle);
  1223.                     (**theTE32KHandle).selStart = wordStart;
  1224.                 }
  1225.             
  1226.                 if (selIndex < (**theTE32KHandle).selStart || (selIndex > (**theTE32KHandle).selStart && selIndex < wordStart)) {
  1227.                     invertSelRange(selIndex, (**theTE32KHandle).selStart, theTE32KHandle);
  1228.                     (**theTE32KHandle).selStart = selIndex;
  1229.                 } else if (selIndex > (**theTE32KHandle).selEnd || (selIndex < (**theTE32KHandle).selEnd && selIndex > wordEnd)) {
  1230.                     invertSelRange((**theTE32KHandle).selEnd, selIndex, theTE32KHandle);
  1231.                     (**theTE32KHandle).selEnd = selIndex;
  1232.                 }
  1233.             }
  1234.         } else if (!extend && teLength > 0L && TickCount() < (**theTE32KHandle).clickTime + GetDblTime() && oldClickLoc == selIndex) {
  1235.             (**theTE32KHandle).dblClick = true;
  1236.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1237.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1238.             
  1239.             selAnchor = selIndex;
  1240.             textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1241.             
  1242.             while (selIndex > 0L) {
  1243.                 ch = textPtr[selIndex-1L];
  1244.                 
  1245.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1246.                     selIndex--;
  1247.                 else
  1248.                     break;
  1249.             }
  1250.             
  1251.             if (selIndex < 0L)
  1252.                 (**theTE32KHandle).selStart =  0L;
  1253.             else
  1254.                 (**theTE32KHandle).selStart = selIndex;
  1255.             
  1256.             wordStart = (**theTE32KHandle).selStart;
  1257.             selIndex = selAnchor;
  1258.             teLength = (**theTE32KHandle).teLength;
  1259.             
  1260.             while (selIndex < teLength) {
  1261.                 ch = textPtr[selIndex];
  1262.                 
  1263.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1264.                     selIndex++;
  1265.                 else
  1266.                     break;
  1267.             }
  1268.             
  1269.             if (selIndex > teLength)
  1270.                 (**theTE32KHandle).selEnd =  teLength;
  1271.             else
  1272.                 (**theTE32KHandle).selEnd = selIndex;
  1273.             
  1274.             wordEnd = (**theTE32KHandle).selEnd;
  1275.             
  1276.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1277.             
  1278.             while (Button()) {
  1279.                 if ((**theTE32KHandle).clikLoop)
  1280.                     (*((**theTE32KHandle).clikLoop)) ();
  1281.                 
  1282.                 GetMouse(&mousePt);
  1283.                 selPt.h = mousePt.h;
  1284.                 selPt.v = mousePt.v;
  1285.             
  1286.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1287.                 
  1288.                 if (selIndex < 0)
  1289.                     selIndex = 0;
  1290.                 else if (selIndex > teLength)
  1291.                     selIndex = teLength;
  1292.                 
  1293.                 if (selIndex < wordStart)
  1294.                     while (selIndex > 0) {
  1295.                         ch = textPtr[selIndex - 1];
  1296.                         
  1297.                         if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1298.                             selIndex--;
  1299.                         else
  1300.                             break;
  1301.                     }
  1302.                 else if (selIndex > wordEnd)
  1303.                     while (selIndex < teLength) {
  1304.                         ch = textPtr[selIndex];
  1305.                         
  1306.                         if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1307.                             selIndex++;
  1308.                         else
  1309.                             break;
  1310.                     }
  1311.             
  1312.                 if (selIndex < wordEnd && wordEnd < (**theTE32KHandle).selEnd) {
  1313.                     invertSelRange(wordEnd, (**theTE32KHandle).selEnd, theTE32KHandle);
  1314.                     (**theTE32KHandle).selEnd = wordEnd;
  1315.                 }
  1316.             
  1317.                 if (selIndex > wordStart && wordStart > (**theTE32KHandle).selStart) {
  1318.                     invertSelRange((**theTE32KHandle).selStart, wordStart, theTE32KHandle);
  1319.                     (**theTE32KHandle).selStart = wordStart;
  1320.                 }
  1321.             
  1322.                 if (selIndex < (**theTE32KHandle).selStart || (selIndex > (**theTE32KHandle).selStart && selIndex < wordStart)) {
  1323.                     invertSelRange(selIndex, (**theTE32KHandle).selStart, theTE32KHandle);
  1324.                     (**theTE32KHandle).selStart = selIndex;
  1325.                 } else if (selIndex > (**theTE32KHandle).selEnd || (selIndex < (**theTE32KHandle).selEnd && selIndex > wordEnd)) {
  1326.                     invertSelRange((**theTE32KHandle).selEnd, selIndex, theTE32KHandle);
  1327.                     (**theTE32KHandle).selEnd = selIndex;
  1328.                 }
  1329.             }
  1330.         } else {
  1331.             (**theTE32KHandle).dblClick = false;
  1332.             if (!extend) {
  1333.                 if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1334.                     invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1335.                 
  1336.                 (**theTE32KHandle).selStart = selIndex;
  1337.                 (**theTE32KHandle).selEnd = selIndex;
  1338.                     
  1339.                 (**theTE32KHandle).clikStuff = false;
  1340.                 TE32KGetPoint(selIndex,&selPt,theTE32KHandle);
  1341.                 
  1342.                 if ((**theTE32KHandle).crOnly)
  1343.                     (**theTE32KHandle).selPoint = selPt;
  1344.                 else {
  1345.                     (**theTE32KHandle).clikStuff = true;
  1346.                     TE32KGetPoint(selIndex,&tempPt,theTE32KHandle);
  1347.                     
  1348.                     if ((selPt.h - startPoint.h)*(selPt.h - startPoint.h) + (selPt.v - startPoint.v)*(selPt.v - startPoint.v) <
  1349.                         (tempPt.h - startPoint.h)*(tempPt.h - startPoint.h) + (tempPt.v - startPoint.v)*(tempPt.v - startPoint.v))
  1350.                             (**theTE32KHandle).selPoint = selPt;
  1351.                     else
  1352.                             (**theTE32KHandle).selPoint = tempPt;
  1353.                 }
  1354.                 
  1355.                 xorCaret(theTE32KHandle);
  1356.             }
  1357.                 
  1358.                 
  1359.             
  1360.             if (extend || StillDown()) {
  1361.                 if (extend) {
  1362.                     if (selIndex >= (**theTE32KHandle).selEnd) {
  1363.                         selAnchor = (**theTE32KHandle).selStart;
  1364.                         selLast = (**theTE32KHandle).selEnd;
  1365.                     } else {
  1366.                         selAnchor = (**theTE32KHandle).selEnd;
  1367.                         selLast = (**theTE32KHandle).selStart;
  1368.                     }
  1369.                 } else {
  1370.                     selAnchor = selIndex;
  1371.                     selLast = selIndex;
  1372.                 }
  1373.                 
  1374.                 GetPort(&oldPort);
  1375.                 SetPort((**theTE32KHandle).inPort);
  1376.                 
  1377.                 if (extend) goto DOHILITE;
  1378.                 
  1379.                 while (StillDown()) {
  1380.                     if (selIndex >= selAnchor) {
  1381.                         (**theTE32KHandle).selStart = selAnchor;
  1382.                         (**theTE32KHandle).selEnd = selIndex;
  1383.                     } else {
  1384.                         (**theTE32KHandle).selStart = selIndex;
  1385.                         (**theTE32KHandle).selEnd = selAnchor;
  1386.                     }
  1387.                     
  1388.                     if ((**theTE32KHandle).clikLoop)
  1389.                         (*((**theTE32KHandle).clikLoop)) ();
  1390.                     
  1391.                     GetMouse(&mousePt);
  1392.                     
  1393.                     selPt.h = (long) mousePt.h;
  1394.                     selPt.v = (long) mousePt.v;
  1395.                     
  1396.                     selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1397.     
  1398.     DOHILITE:
  1399.                     if (selLast >= selAnchor) {
  1400.                         if (selIndex > selLast)
  1401.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1402.                         
  1403.                         else if (selIndex>=selAnchor && selIndex<selLast)
  1404.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1405.                         
  1406.                         else if (selIndex<selAnchor) {
  1407.                             invertSelRange(selAnchor,selLast,theTE32KHandle);
  1408.                             invertSelRange(selIndex,selAnchor,theTE32KHandle);
  1409.                         }
  1410.                     } else {
  1411.                         if (selIndex < selLast)
  1412.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1413.                         
  1414.                         else if (selIndex<=selAnchor && selIndex>selLast)
  1415.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1416.                         
  1417.                         else if (selIndex>selAnchor) {
  1418.                             invertSelRange(selLast,selAnchor,theTE32KHandle);
  1419.                             invertSelRange(selAnchor,selIndex,theTE32KHandle);
  1420.                         }
  1421.                     }
  1422.                     
  1423.                     selLast = selIndex;
  1424.                 }
  1425.                 
  1426.                 SetPort(oldPort);
  1427.                 
  1428.                 
  1429.                 if (selIndex >= selAnchor) {
  1430.                     (**theTE32KHandle).selStart = selAnchor;
  1431.                     (**theTE32KHandle).selEnd = selIndex;
  1432.                 } else {
  1433.                     (**theTE32KHandle).selStart = selIndex;
  1434.                     (**theTE32KHandle).selEnd = selAnchor;
  1435.                 }
  1436.             }
  1437.         }
  1438.         
  1439.         (**theTE32KHandle).clickTime = TickCount();
  1440.         
  1441.         clickedTE32KH = 0L;
  1442.     }
  1443. }
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449. void    TE32KSetSelect(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1450. {
  1451.     long temp;
  1452.     
  1453.     if (theTE32KHandle) {
  1454.         if (selStart > selEnd) {
  1455.             temp = selStart;
  1456.             selStart = selEnd;
  1457.             selEnd = temp;
  1458.         }
  1459.         
  1460.         // Constrain selection to bounds of text
  1461.         if (selStart > (**theTE32KHandle).teLength)
  1462.             selStart = (**theTE32KHandle).teLength;
  1463.         if (selEnd > (**theTE32KHandle).teLength)
  1464.             selEnd = (**theTE32KHandle).teLength;
  1465.         
  1466.         // Hilite new selection
  1467.         if (selStart != (**theTE32KHandle).selStart || selEnd != (**theTE32KHandle).selEnd) {
  1468.             // Dehilite old selection
  1469.             if ((**theTE32KHandle).active || TE32KFeatureFlag(te32KFOutlineHilite, TE32KBitTest, theTE32KHandle)) {
  1470.                 if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1471.                     invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1472.                 else if ((**theTE32KHandle).caretState)
  1473.                     xorCaret(theTE32KHandle);
  1474.             }
  1475.             
  1476.             // Store new selection        
  1477.             (**theTE32KHandle).selStart = selStart;
  1478.             (**theTE32KHandle).selEnd = selEnd;
  1479.             
  1480.             // Hilite new selection
  1481.             if ((**theTE32KHandle).active || TE32KFeatureFlag(te32KFOutlineHilite, TE32KBitTest, theTE32KHandle))
  1482.                 invertSelRange(selStart, selEnd, theTE32KHandle);
  1483.         }
  1484.     }
  1485. }
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491. OSErr TE32KToScrap(void)
  1492. {
  1493.     OSErr err = noErr;
  1494.  
  1495.     if (TE32KScrpHandle && TE32KScrpLength > 0L) {
  1496.         HLockHi(TE32KScrpHandle);
  1497.         err = PutScrap(TE32KScrpLength,'TEXT',(Ptr) *TE32KScrpHandle);
  1498.         HUnlock(TE32KScrpHandle);
  1499.         return(err);
  1500.     }
  1501.     
  1502.     return(noScrapErr);
  1503. }
  1504.  
  1505.  
  1506.  
  1507.  
  1508. OSErr TE32KFromScrap(void)
  1509. {
  1510.     long offset;
  1511.     OSErr err;
  1512.  
  1513.     if (TE32KScrpHandle && (err = GetScrap(0L,'TEXT',&offset)) > 0L) {
  1514.         TE32KScrpLength = GetScrap(TE32KScrpHandle, 'TEXT', &offset);
  1515.         
  1516.         if (TE32KScrpLength > 0L)
  1517.             return(noErr);
  1518.         else if (TE32KScrpLength == 0L)
  1519.             return(noTypeErr);
  1520.         else
  1521.             return(TE32KScrpLength);
  1522.     }
  1523.     
  1524.     if (TE32KScrpHandle == NULL)
  1525.         return(noScrapErr);
  1526.     else
  1527.         return(err);
  1528. }
  1529.  
  1530.  
  1531.  
  1532.  
  1533. void TE32KCopy(TE32KHandle theTE32KHandle)
  1534. {
  1535.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  1536.         SetHandleSize(TE32KScrpHandle,(**theTE32KHandle).selEnd - (**theTE32KHandle).selStart);
  1537.         
  1538.         if (!MemError() && GetHandleSize(TE32KScrpHandle) >= ((**theTE32KHandle).selEnd - (**theTE32KHandle).selStart)) {
  1539.             TE32KScrpLength = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1540.             
  1541.             HLock(TE32KScrpHandle);
  1542.             HLock((**theTE32KHandle).hText);
  1543.  
  1544.             BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*TE32KScrpHandle,TE32KScrpLength);
  1545.             
  1546.             HUnlock((**theTE32KHandle).hText);
  1547.             HUnlock(TE32KScrpHandle);
  1548.         }
  1549.     }
  1550. }
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556. void    TE32KCut(TE32KHandle theTE32KHandle)
  1557. {
  1558.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  1559.         TE32KCopy(theTE32KHandle);
  1560.         TE32KDelete(theTE32KHandle);
  1561.     }
  1562. }
  1563.  
  1564.  
  1565.  
  1566. void    TE32KDelete(TE32KHandle theTE32KHandle)
  1567. {
  1568.     LongRect            updateRect;
  1569.     register long        *theLine,*otherLine,theLineStart,i,delta;
  1570.     long                firstLine,lastLine;
  1571.     Rect                tempRect;
  1572.     RgnHandle            updateRgn;
  1573.     GrafPtr                oldPort;
  1574.  
  1575.  
  1576.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  1577.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1578.                 
  1579.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1580.         lastLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  1581.         
  1582.         updateRect = (**theTE32KHandle).viewRect;
  1583.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1584.         LongRectToRect(&updateRect,&tempRect);
  1585.         
  1586.         GetPort(&oldPort);
  1587.         SetPort((**theTE32KHandle).inPort);
  1588.                 
  1589.         ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight * (lastLine - firstLine),updateRgn = NewRgn());
  1590.         tempRect = (**updateRgn).rgnBBox;
  1591.         DisposeRgn(updateRgn);
  1592.         
  1593.         SetPort(oldPort);
  1594.         
  1595.         if ((**theTE32KHandle).selEnd != (**theTE32KHandle).teLength) {
  1596.             HLock((**theTE32KHandle).hText);
  1597.             BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selEnd,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1598.             HUnlock((**theTE32KHandle).hText);
  1599.         }
  1600.         
  1601.         delta = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1602.         
  1603.         theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1604.         otherLine = &((**theTE32KHandle).lineStarts[lastLine + 1L]);
  1605.         i = (**theTE32KHandle).nLines - lastLine;
  1606.         
  1607.         while (i--) {
  1608.             theLineStart = *(otherLine++);
  1609.             theLineStart -= delta;
  1610.             *(theLine++) = theLineStart;
  1611.         }
  1612.         
  1613.         (**theTE32KHandle).teLength -= delta;
  1614.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1615.         (**theTE32KHandle).nLines -= (lastLine - firstLine);
  1616.         
  1617.         RectToLongRect(&tempRect,&updateRect);
  1618.         TE32KUpdate(&updateRect,theTE32KHandle);    /* update scrolled stuff */
  1619.         
  1620.         updateLine(firstLine,theTE32KHandle,true,0L);
  1621.     }
  1622. }
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628. void    TE32KInsert(Ptr textPtr,register long textLength,TE32KHandle theTE32KHandle)
  1629. {
  1630.     register long                *theLine,*otherLine,i,numCRs;
  1631.     long                        firstLine,teLength,maxLineStarts,sizeTE32KHandle;
  1632.     register unsigned char        *charPtr,*charBase;
  1633.     RgnHandle                    updateRgn;
  1634.     Rect                        tempRect;
  1635.     GrafPtr                        oldPort;
  1636.  
  1637.  
  1638.     if (theTE32KHandle && textPtr && textLength > 0L) {
  1639.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1640.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1641.     
  1642.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1643.         
  1644.         teLength = (**theTE32KHandle).teLength + textLength;
  1645.         
  1646.         if (GetHandleSize((**theTE32KHandle).hText) < teLength) {
  1647.             SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  1648.             
  1649.             if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength + EXTRATEXTBUFF)
  1650.                 return;
  1651.         }
  1652.         
  1653.         HLock((**theTE32KHandle).hText);
  1654.         
  1655.         if ((**theTE32KHandle).teLength - (**theTE32KHandle).selStart)
  1656.             BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + textLength,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  1657.         
  1658.         BlockMoveData(textPtr,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,textLength);
  1659.         
  1660.         HUnlock((**theTE32KHandle).hText);
  1661.         
  1662.         i = textLength;
  1663.         numCRs = 0L;
  1664.         charPtr = (unsigned char *) textPtr;
  1665.         
  1666.         while (i--) {
  1667.             if (*charPtr == '\r' || *charPtr == '\n')
  1668.                 numCRs++;
  1669.             
  1670.             charPtr++;
  1671.         }
  1672.         
  1673.         if (numCRs) {
  1674.             sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  1675.             maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1676.             
  1677.             if ((**theTE32KHandle).nLines + numCRs >= maxLineStarts) {
  1678.                 sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + numCRs + EXTRALINESTARTS);
  1679.                 maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1680.                 
  1681.                 SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  1682.                 
  1683.                 if (MemError())
  1684.                     return;
  1685.             }
  1686.             
  1687.             theLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1688.             otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + numCRs]);
  1689.             i = (**theTE32KHandle).nLines - firstLine;
  1690.             
  1691.             while (i--)
  1692.                 *(otherLine--) = *(theLine--) + textLength;
  1693.             
  1694.             charPtr = (unsigned char *) (*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart) + 1L;
  1695.             charBase = (unsigned char *) *((**theTE32KHandle).hText);
  1696.             theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1697.             i = numCRs;
  1698.             
  1699.             while (i--) {
  1700.                 while (*charPtr != '\r' && *charPtr != '\n')
  1701.                     charPtr++;
  1702.                 
  1703.                 charPtr++;
  1704.                 
  1705.                 *theLine++ = charPtr - charBase;
  1706.             }
  1707.             
  1708.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  1709.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1710.             
  1711.             GetPort(&oldPort);
  1712.             SetPort((**theTE32KHandle).inPort);
  1713.             
  1714.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * numCRs,updateRgn = NewRgn());
  1715.             DisposeRgn(updateRgn);
  1716.             
  1717.             SetPort(oldPort);
  1718.         } else {
  1719.             theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1720.             i = (**theTE32KHandle).nLines - firstLine;
  1721.             
  1722.             while (i--)
  1723.                 *(theLine++) += textLength;
  1724.         }
  1725.         
  1726.         
  1727.         (**theTE32KHandle).teLength = teLength;
  1728.         (**theTE32KHandle).selStart += textLength;
  1729.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1730.         (**theTE32KHandle).nLines += numCRs;
  1731.         
  1732.         do {
  1733.             updateLine(firstLine,theTE32KHandle,true,0L);
  1734.             
  1735.             if (numCRs) {
  1736.                 theLine = (**theTE32KHandle).lineStarts;
  1737.                 charPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1738.                 
  1739.                 do { firstLine++; } while (firstLine < (**theTE32KHandle).nLines && charPtr[theLine[firstLine] - 1L] != '\r' && charPtr[theLine[firstLine] - 1L] != '\n');
  1740.             }    
  1741.         } while (numCRs--);
  1742.     }
  1743. }
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750. void    TE32KPaste(TE32KHandle theTE32KHandle)
  1751. {
  1752.     if (theTE32KHandle && TE32KScrpHandle && TE32KScrpLength > 0L) {
  1753.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1754.             TE32KDelete( theTE32KHandle);
  1755.         
  1756.         HLock(TE32KScrpHandle);    
  1757.         TE32KInsert(*TE32KScrpHandle,TE32KScrpLength,theTE32KHandle);
  1758.         HUnlock(TE32KScrpHandle);
  1759.     }
  1760. }
  1761.  
  1762.  
  1763.  
  1764.  
  1765. Handle TE32KScrapHandle()
  1766. {
  1767.     return(TE32KScrpHandle);
  1768. }
  1769.  
  1770.  
  1771. long TE32KGetScrapLen()
  1772. {
  1773.     return(TE32KScrpLength);
  1774. }
  1775.  
  1776.  
  1777. void TE32KSetScrapLen(long newLength)
  1778. {
  1779.     TE32KScrpLength = newLength;
  1780. }
  1781.  
  1782.  
  1783.  
  1784.  
  1785. void TE32KSelView(TE32KHandle theTE32KHandle)
  1786. {
  1787.     register long    deltaV, deltaH, screenLines, lineHeight, viewTop, viewBot, selPtV, ascent;
  1788.     LongPoint selPt;
  1789.  
  1790.     if (theTE32KHandle) {
  1791.         TE32KGetPoint((**theTE32KHandle).selStart, &selPt, theTE32KHandle);
  1792.         selPtV = selPt.v;
  1793.         viewTop = (**theTE32KHandle).viewRect.top;
  1794.         viewBot = (**theTE32KHandle).viewRect.bottom;
  1795.         lineHeight = (**theTE32KHandle).lineHeight;
  1796.         ascent = (**theTE32KHandle).fontAscent;
  1797.         
  1798.         deltaV = viewTop - (**theTE32KHandle).destRect.top;
  1799.         deltaV = deltaV - (deltaV/lineHeight)*lineHeight;
  1800.         
  1801.         if (selPtV - ascent < viewTop) {
  1802.             deltaV += viewTop - (selPtV - ascent);
  1803.         } else if (selPtV > viewBot) {
  1804.             screenLines = (viewBot - viewTop) / lineHeight;
  1805.             
  1806.             deltaV -= (selPtV - ascent + lineHeight) - (viewTop + screenLines * lineHeight);
  1807.         }
  1808.         
  1809.         
  1810.         if (selPt.h <= (**theTE32KHandle).viewRect.left) {
  1811.             deltaH = (**theTE32KHandle).viewRect.left - selPt.h;
  1812.             deltaH = (2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1813.             
  1814.             if ((**theTE32KHandle).destRect.left + deltaH > (**theTE32KHandle).viewRect.left)
  1815.                 deltaH = (**theTE32KHandle).viewRect.left - (**theTE32KHandle).destRect.left;
  1816.         } else if (selPt.h > (**theTE32KHandle).viewRect.right) {
  1817.             deltaH = selPt.h - (**theTE32KHandle).viewRect.right;
  1818.             deltaH = -(2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1819.         } else deltaH = 0L;
  1820.         
  1821.         if (deltaV || deltaH)
  1822.             TE32KScroll(deltaH,deltaV,theTE32KHandle);
  1823.     }
  1824. }
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830. static    void DoDeleteKey(TE32KHandle theTE32KHandle)
  1831. {
  1832.     Rect            tempRect;
  1833.     RgnHandle        updateRgn;
  1834.     short                chWidth;
  1835.     long            firstLine;
  1836.     register short    *theCharWidths;
  1837.     register long    i,*lineStarts,selIndex,*otherLine;
  1838.     LongRect        updateRect;
  1839.     LongPoint        selPt;
  1840.     unsigned char    ch,prevChar;
  1841.     GrafPtr            oldPort;
  1842.  
  1843.     if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1844.         TE32KDelete(theTE32KHandle);
  1845.     
  1846.     else if ((**theTE32KHandle).selStart > 0L) {
  1847.         ch = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  1848.         
  1849.         if ((**theTE32KHandle).selStart >= 2L)
  1850.             prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 2L];
  1851.         else
  1852.             prevChar = '\0';
  1853.         
  1854.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1855.         
  1856.         HLock((**theTE32KHandle).hText);
  1857.         BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart - 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1858.         HUnlock((**theTE32KHandle).hText);
  1859.         
  1860.         (**theTE32KHandle).teLength--;
  1861.         (**theTE32KHandle).selStart--;
  1862.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1863.         
  1864.         if ((ch == '\r' || ch == '\n') && ((**theTE32KHandle).crOnly || (**theTE32KHandle).teLength == (**theTE32KHandle).selStart || prevChar == '\r' || prevChar == '\n')) {
  1865.             lineStarts = &((**theTE32KHandle).lineStarts[firstLine]);
  1866.             otherLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1867.             
  1868.             i = (**theTE32KHandle).nLines - firstLine;
  1869.             
  1870.             while (i--) {
  1871.                 selIndex = *(otherLine++);
  1872.                 *(lineStarts++) = --selIndex;
  1873.             }
  1874.             
  1875.             (**theTE32KHandle).nLines--;
  1876.                         
  1877.             if (firstLine > 0L)
  1878.                 firstLine--;
  1879.                         
  1880.             updateRect = (**theTE32KHandle).viewRect;
  1881.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1882.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1883.             TE32KUpdate(&updateRect,theTE32KHandle);
  1884.             
  1885.             LongRectToRect(&updateRect,&tempRect);
  1886.             tempRect.top = tempRect.bottom;
  1887.             
  1888.             if ((**theTE32KHandle).viewRect.bottom < -32768L)
  1889.                 tempRect.bottom = -32768;
  1890.             else if ((**theTE32KHandle).viewRect.bottom > 32767L)
  1891.                 tempRect.bottom = 32767;
  1892.             else
  1893.                 tempRect.bottom = (short) (**theTE32KHandle).viewRect.bottom;
  1894.             
  1895.             
  1896.             GetPort(&oldPort);
  1897.             SetPort((**theTE32KHandle).inPort);
  1898.         
  1899.             ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight,updateRgn = NewRgn());
  1900.             tempRect = (**updateRgn).rgnBBox;
  1901.             DisposeRgn(updateRgn);
  1902.             
  1903.             SetPort(oldPort);
  1904.             
  1905.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1906.             (**theTE32KHandle).selPoint = selPt;
  1907.             
  1908.             RectToLongRect(&tempRect,&updateRect);
  1909.             
  1910.             if ((**theTE32KHandle).caretState)
  1911.                 xorCaret(theTE32KHandle);
  1912.         
  1913.             TE32KUpdate(&updateRect,theTE32KHandle);
  1914.         } else {
  1915.             lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1916.             i = (**theTE32KHandle).nLines - firstLine;
  1917.             
  1918.             if (ch == '\r' || ch == '\n')
  1919.                 i++;
  1920.             
  1921.             
  1922.             while (i--)
  1923.                 (*(lineStarts--))--;
  1924.             
  1925.             theCharWidths = (**theTE32KHandle).theCharWidths;
  1926.             
  1927.             if (ch == TAB)
  1928.                 chWidth = (**theTE32KHandle).tabWidth;
  1929.             else
  1930.                 chWidth = theCharWidths[ch];
  1931.             
  1932.             if (ch == '\r' || ch == '\n') {
  1933.                 firstLine--;
  1934.                 
  1935.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1936.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1937.                 updateRect.left = (**theTE32KHandle).viewRect.left;
  1938.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1939.  
  1940.             } else {
  1941.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1942.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1943.                 updateRect.left = (**theTE32KHandle).selPoint.h - chWidth;
  1944.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1945.             }
  1946.             
  1947.             if ((**theTE32KHandle).caretState)
  1948.                 xorCaret(theTE32KHandle);
  1949.             
  1950.             if ((**theTE32KHandle).crOnly)
  1951.                 TE32KUpdate(&updateRect,theTE32KHandle);
  1952.             else
  1953.                 updateLine(firstLine,theTE32KHandle,true,&updateRect);
  1954.         }
  1955.     }
  1956. }
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962. static void /*[esp] added void*/ DoArrowKeys(unsigned char ch, TE32KHandle theTE32KHandle)
  1963. {
  1964.     LongPoint        selPt,tempPt1,tempPt2;
  1965.     long            firstLine,selIndex;
  1966.     unsigned char    currentChar;
  1967.  
  1968.     if ((**theTE32KHandle).caretState)
  1969.         xorCaret(theTE32KHandle);
  1970.     
  1971.     if (!shiftKeyDown()) {
  1972.         if (ch==LEFTARROW || ch==RIGHTARROW) {
  1973.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1974.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1975.             
  1976.             if (ch==LEFTARROW && (**theTE32KHandle).selStart >= 0L) {
  1977.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selStart - 1L];
  1978.                 
  1979.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1980.                 
  1981.                 (**theTE32KHandle).clikStuff = false;
  1982.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1983.                 
  1984.                 if (!(**theTE32KHandle).crOnly && firstLine > 0 && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine] && currentChar != '\r'  && currentChar != '\n' && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v) {
  1985.                     (**theTE32KHandle).clikStuff = true;
  1986.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1987.                     
  1988.                     (**theTE32KHandle).selPoint = selPt;
  1989.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1990.                     
  1991.                     return;
  1992.                 } else {
  1993.                     if (((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd) && (**theTE32KHandle).selStart > 0)
  1994.                         (**theTE32KHandle).selStart--;
  1995.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1996.                 }
  1997.             } else if (ch==RIGHTARROW && (**theTE32KHandle).selEnd <= (**theTE32KHandle).teLength) {
  1998.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selEnd];
  1999.                 
  2000.                 firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2001.                 
  2002.                 (**theTE32KHandle).clikStuff = true;
  2003.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2004.                 
  2005.                 if (!(**theTE32KHandle).crOnly && firstLine > 0 && (**theTE32KHandle).selEnd == (**theTE32KHandle).lineStarts[firstLine] && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v) {
  2006.                     (**theTE32KHandle).clikStuff = false;
  2007.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2008.                     
  2009.                     (**theTE32KHandle).selPoint = selPt;
  2010.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2011.                     
  2012.                     return;
  2013.                 } else if (!(**theTE32KHandle).crOnly && firstLine < (**theTE32KHandle).nLines - 1L && (**theTE32KHandle).selStart + 1L == (**theTE32KHandle).lineStarts[firstLine + 1L] && currentChar != '\r' && currentChar != '\n') {
  2014.                     (**theTE32KHandle).selEnd++;
  2015.                     
  2016.                     (**theTE32KHandle).clikStuff = true;
  2017.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2018.                     
  2019.                     (**theTE32KHandle).selPoint = selPt;
  2020.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2021.                     
  2022.                     return;
  2023.                 } else {
  2024.                     if (((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd) && ((**theTE32KHandle).selEnd < (**theTE32KHandle).teLength))
  2025.                         (**theTE32KHandle).selEnd++;
  2026.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2027.                 }
  2028.             }
  2029.             
  2030.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2031.         }
  2032.         
  2033.         else if (ch==UPARROW || ch==DOWNARROW) {
  2034.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  2035.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2036.                 
  2037.                 if (ch == DOWNARROW)
  2038.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2039.                 else
  2040.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2041.                 
  2042.                 (**theTE32KHandle).selPoint = selPt;
  2043.             }
  2044.                 
  2045.                 
  2046.             if (ch==UPARROW) {
  2047.                 selPt = (**theTE32KHandle).selPoint;
  2048.                 
  2049.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2050.                 
  2051.                 if (firstLine > 0L) {
  2052.                     selPt.v -= (**theTE32KHandle).lineHeight;
  2053.                     (**theTE32KHandle).selStart = TE32KGetOffset(&selPt,theTE32KHandle);
  2054.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2055.                     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2056.                     
  2057.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine]) {
  2058.                         (**theTE32KHandle).clikStuff = false;
  2059.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2060.                         
  2061.                         (**theTE32KHandle).clikStuff = true;
  2062.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2063.                         
  2064.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2065.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2066.                                 (**theTE32KHandle).selPoint = tempPt1;
  2067.                         else
  2068.                                 (**theTE32KHandle).selPoint = tempPt2;
  2069.                         
  2070.                         return;
  2071.                     } else {
  2072.                         (**theTE32KHandle).clikStuff = false;
  2073.                         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2074.                         (**theTE32KHandle).selPoint = selPt;
  2075.                     }
  2076.                 }
  2077.             } else if (ch == DOWNARROW) {
  2078.                 selPt = (**theTE32KHandle).selPoint;
  2079.                 
  2080.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2081.                 
  2082.                 if (firstLine < (**theTE32KHandle).nLines) {
  2083.                     selPt.v += (**theTE32KHandle).lineHeight;
  2084.                     (**theTE32KHandle).selEnd = TE32KGetOffset(&selPt,theTE32KHandle);
  2085.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2086.                     firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2087.                     
  2088.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine]) {
  2089.                         (**theTE32KHandle).clikStuff = false;
  2090.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2091.                         
  2092.                         (**theTE32KHandle).clikStuff = true;
  2093.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2094.                         
  2095.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2096.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2097.                                 (**theTE32KHandle).selPoint = tempPt1;
  2098.                         else
  2099.                                 (**theTE32KHandle).selPoint = tempPt2;
  2100.                         
  2101.                         return;
  2102.                     } else {
  2103.                         (**theTE32KHandle).clikStuff = false;
  2104.                         TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2105.                         (**theTE32KHandle).selPoint = selPt;
  2106.                     }
  2107.                 }
  2108.             }
  2109.             
  2110.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2111.             
  2112.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2113.             }
  2114.         }
  2115.     
  2116.     else {
  2117.         if (ch==LEFTARROW) {
  2118.             if ((**theTE32KHandle).selStart > 0L) {
  2119.                 invertSelRange((**theTE32KHandle).selStart - 1L,(**theTE32KHandle).selStart,theTE32KHandle);
  2120.                 (**theTE32KHandle).selStart--;
  2121.             }
  2122.         } else if (ch==RIGHTARROW) {
  2123.             if ((**theTE32KHandle).selEnd < (**theTE32KHandle).teLength) {
  2124.                 invertSelRange((**theTE32KHandle).selEnd,(**theTE32KHandle).selEnd + 1L,theTE32KHandle);
  2125.                 (**theTE32KHandle).selEnd++;
  2126.             }
  2127.         } else if (ch==UPARROW) {
  2128.             firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2129.             
  2130.             if (firstLine > 0L) {
  2131.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2132.                 selPt.v -= (**theTE32KHandle).lineHeight;
  2133.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2134.                 
  2135.                 invertSelRange(selIndex,(**theTE32KHandle).selStart,theTE32KHandle);
  2136.                 
  2137.                 (**theTE32KHandle).selStart = selIndex;
  2138.             }
  2139.         } else if (ch==DOWNARROW) {
  2140.             firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2141.             
  2142.             if (firstLine < (**theTE32KHandle).nLines - 1L) {
  2143.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2144.                 selPt.v += (**theTE32KHandle).lineHeight;
  2145.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2146.                 
  2147.                 invertSelRange((**theTE32KHandle).selEnd,selIndex,theTE32KHandle);
  2148.                 
  2149.                 (**theTE32KHandle).selEnd = selIndex;
  2150.             }
  2151.         }
  2152.     }
  2153. }
  2154.  
  2155.  
  2156.  
  2157.  
  2158.  
  2159. static void /*[esp] added void*/ DoNormalChar(unsigned char ch,TE32KHandle theTE32KHandle)
  2160. {
  2161.     Rect            tempRect;
  2162.     RgnHandle        updateRgn;
  2163.     short            chWidth,destLeftSide;
  2164.     register short    *theCharWidths;
  2165.     long            teLength,firstLine;
  2166.     register long    i,*lineStarts,delta;
  2167.     LongPoint        selPt;
  2168.     unsigned char    prevChar;
  2169.     GrafPtr            oldPort;
  2170.     short            oldFont,oldFace,oldSize,oldMode;
  2171.     
  2172.     teLength = (**theTE32KHandle).teLength + 1L;
  2173.     
  2174.     if (GetHandleSize((**theTE32KHandle).hText) < teLength) {
  2175.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2176.         
  2177.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2178.             return;
  2179.     }
  2180.     
  2181.     if ((**theTE32KHandle).caretState)
  2182.         xorCaret(theTE32KHandle);
  2183.     
  2184.     selPt = (**theTE32KHandle).selPoint;
  2185.     
  2186.     selPt.h--;
  2187.     
  2188.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2189.     if ((**theTE32KHandle).selStart > 0L)
  2190.         prevChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).lineStarts[firstLine] - 1L];
  2191.     else
  2192.         prevChar = '\r';
  2193.     
  2194.     if ((**theTE32KHandle).crOnly || prevChar =='\r'  || prevChar =='\n' || !(ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])) {
  2195.         if (selPt.h < -32768L)
  2196.             tempRect.left = -32768;
  2197.         else if (selPt.h > 32767L)
  2198.             tempRect.left = 32767;
  2199.         else
  2200.             tempRect.left = (short) selPt.h;
  2201.         
  2202.         if ((**theTE32KHandle).viewRect.right < -32768L)
  2203.             tempRect.right = -32768;
  2204.         else if ((**theTE32KHandle).viewRect.right > 32767L)
  2205.             tempRect.right = 32767;
  2206.         else
  2207.             tempRect.right = (short) (**theTE32KHandle).viewRect.right;
  2208.         
  2209.         selPt.v -= (**theTE32KHandle).fontAscent;
  2210.         
  2211.         if (selPt.v < -32768L)
  2212.             tempRect.top = -32768;
  2213.         else if (selPt.v > 32767L)
  2214.             tempRect.top = 32767;
  2215.         else
  2216.             tempRect.top = (short) selPt.v;
  2217.         
  2218.         tempRect.bottom = tempRect.top + (**theTE32KHandle).lineHeight;
  2219.         
  2220.         GetPort(&oldPort);
  2221.         SetPort((**theTE32KHandle).inPort);
  2222.         
  2223.         oldFont = ((**theTE32KHandle).inPort)->txFont;
  2224.         oldFace = ((**theTE32KHandle).inPort)->txFace;
  2225.         oldSize = ((**theTE32KHandle).inPort)->txSize;
  2226.         oldMode = ((**theTE32KHandle).inPort)->txMode;
  2227.         
  2228.         TextFont((**theTE32KHandle).txFont);
  2229.         TextFace((**theTE32KHandle).txFace);
  2230.         TextSize((**theTE32KHandle).txSize);
  2231.         TextMode((**theTE32KHandle).txMode);
  2232.         
  2233.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2234.         
  2235.         if (ch == TAB) {
  2236.             destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  2237.             delta = (**theTE32KHandle).tabWidth;
  2238.             chWidth = (destLeftSide + ((tempRect.left + 1 - destLeftSide + delta)/delta)*delta) - (tempRect.left + 1);
  2239.         } else chWidth = theCharWidths[ch];
  2240.         
  2241.         if (tempRect.left < tempRect.right) {    
  2242.             ScrollRect(&tempRect,chWidth,0,updateRgn = NewRgn());
  2243.             
  2244.             if (tempRect.left+1 + chWidth > tempRect.right)
  2245.                 ClipRect(&tempRect);
  2246.             
  2247.             MoveTo(tempRect.left+1,tempRect.top + (**theTE32KHandle).fontAscent);
  2248.             if (ch != TAB)
  2249.                 DrawChar(ch);
  2250.             
  2251.             if (tempRect.left+1 + chWidth > tempRect.right) {
  2252.                 tempRect.left = -32768;
  2253.                 tempRect.top = -32768;
  2254.                 tempRect.right = 32767;
  2255.                 tempRect.bottom = 32767;
  2256.                 ClipRect(&tempRect);
  2257.             }
  2258.             
  2259.             DisposeRgn(updateRgn);
  2260.         }
  2261.         
  2262.         TextFont(oldFont);
  2263.         TextFace(oldFace);
  2264.         TextSize(oldSize);
  2265.         TextMode(oldMode);
  2266.         
  2267.         SetPort(oldPort);
  2268.     }
  2269.     
  2270.     HLock((**theTE32KHandle).hText);
  2271.     BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2272.     HUnlock((**theTE32KHandle).hText);
  2273.     
  2274.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = ch;
  2275.     
  2276.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2277.     i = (**theTE32KHandle).nLines - firstLine;
  2278.     
  2279.     if (!(**theTE32KHandle).crOnly && prevChar != '\r'  && prevChar != '\n' && ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2280.         i++;
  2281.     
  2282.     while (i--)
  2283.         (*(lineStarts--))++;
  2284.     
  2285.     
  2286.     (**theTE32KHandle).teLength++;
  2287.     (**theTE32KHandle).selStart++;
  2288.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2289.     (**theTE32KHandle).selPoint.h += (long) chWidth;
  2290.     
  2291.     if (!(**theTE32KHandle).crOnly)
  2292.         updateLine(firstLine,theTE32KHandle,false,0L);
  2293.     
  2294.     xorCaret(theTE32KHandle);
  2295. }
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301. static void /*[esp] added void*/ DoReturnChar(TE32KHandle theTE32KHandle)
  2302. {
  2303.     Rect            tempRect;
  2304.     RgnHandle        updateRgn;
  2305.     long            teLength,firstLine,lastLine,deltaLines,numAffected,tempFirstLine;
  2306.     register long    i,*lineStarts,selIndex,*otherLine;
  2307.     LongPoint        selPt;
  2308.     LongRect        updateRect;
  2309.     unsigned char    prevChar,doWrap;
  2310.     GrafPtr            oldPort;
  2311.  
  2312.     teLength = GetHandleSize((Handle) theTE32KHandle);
  2313.     lastLine  = (teLength - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2314.     
  2315.     if ((**theTE32KHandle).nLines + 1L >= lastLine) {
  2316.         teLength = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + 1L + EXTRALINESTARTS);
  2317.         
  2318.         SetHandleSize((Handle) theTE32KHandle,teLength);
  2319.         
  2320.         if (MemError()  || GetHandleSize((Handle) theTE32KHandle) < teLength)
  2321.             return;
  2322.     }
  2323.     
  2324.     
  2325.     teLength = (**theTE32KHandle).teLength + 1L;
  2326.     
  2327.     if (GetHandleSize((**theTE32KHandle).hText) < teLength) {
  2328.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2329.         
  2330.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2331.             return;
  2332.     }
  2333.     
  2334.     
  2335.     
  2336.     if ((**theTE32KHandle).selStart > 0L)
  2337.         prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  2338.     else
  2339.         prevChar = '\r';
  2340.     
  2341.     if ((**theTE32KHandle).caretState)
  2342.         xorCaret(theTE32KHandle);
  2343.     
  2344.     HLock((**theTE32KHandle).hText);
  2345.     BlockMoveData(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2346.     HUnlock((**theTE32KHandle).hText);
  2347.     
  2348.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = RETURN;
  2349.     
  2350.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2351.     
  2352.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2353.     otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + 1L]);
  2354.     i = (**theTE32KHandle).nLines - firstLine;
  2355.         
  2356.     while (i--) {
  2357.         selIndex = *(lineStarts--);
  2358.         *(otherLine--) = ++selIndex;
  2359.     }
  2360.     
  2361.     (**theTE32KHandle).lineStarts[firstLine + 1L] = (**theTE32KHandle).selStart + 1L;
  2362.     
  2363.     (**theTE32KHandle).nLines++;
  2364.     (**theTE32KHandle).teLength++;
  2365.     (**theTE32KHandle).selStart++;
  2366.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2367.     
  2368.     LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2369.     
  2370.     selPt = (**theTE32KHandle).selPoint;
  2371.     selPt.v -= (**theTE32KHandle).fontAscent;
  2372.     selPt.v += (**theTE32KHandle).lineHeight;
  2373.     
  2374.     if (selPt.v < -32768L)
  2375.         tempRect.top = -32768;
  2376.     else if (selPt.v > 32767L)
  2377.         tempRect.top = 32767;
  2378.     else
  2379.         tempRect.top = (short) selPt.v;
  2380.     
  2381.     GetPort(&oldPort);
  2382.     SetPort((**theTE32KHandle).inPort);
  2383.             
  2384.     ScrollRect(&tempRect, 0, (**theTE32KHandle).lineHeight, updateRgn = NewRgn());
  2385.     DisposeRgn(updateRgn);
  2386.     
  2387.     SetPort(oldPort);
  2388.     
  2389.     if (!(**theTE32KHandle).crOnly) {
  2390.         doWrap = false;
  2391.         tempFirstLine = firstLine;
  2392.         
  2393.         if (tempFirstLine > 0L && LineEndIndex(tempFirstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine]) {
  2394.             doWrap = true;
  2395.             tempFirstLine--;
  2396.         } else if (LineEndIndex(tempFirstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine + 1L])
  2397.             doWrap = true;
  2398.         
  2399.         
  2400.         if (doWrap) {
  2401.             CalParagraph(tempFirstLine,theTE32KHandle,&deltaLines,&numAffected);
  2402.             
  2403.             if (deltaLines == 0L) {
  2404.                 updateRect = (**theTE32KHandle).viewRect;
  2405.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2406.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2407.             } else if (deltaLines > 0L) {
  2408.                 firstLine += deltaLines;
  2409.                 
  2410.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2411.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2412.                 
  2413.                 GetPort(&oldPort);
  2414.                 SetPort((**theTE32KHandle).inPort);
  2415.                 
  2416.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn = NewRgn());
  2417.                 DisposeRgn(updateRgn);
  2418.                 
  2419.                 SetPort(oldPort);
  2420.                 
  2421.                 updateRect = (**theTE32KHandle).viewRect;
  2422.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2423.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2424.             } else {
  2425.                 firstLine += deltaLines;
  2426.                 
  2427.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2428.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2429.                 
  2430.                 GetPort(&oldPort);
  2431.                 SetPort((**theTE32KHandle).inPort);
  2432.     
  2433.                 updateRgn = NewRgn();
  2434.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2435.                 DisposeRgn(updateRgn);
  2436.                 
  2437.                 SetPort(oldPort);
  2438.                 
  2439.                 updateRect = (**theTE32KHandle).viewRect;
  2440.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2441.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2442.             }
  2443.             
  2444.             TE32KUpdate(&updateRect,theTE32KHandle);
  2445.         }
  2446.         
  2447.         
  2448.         firstLine++;
  2449.         
  2450.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2451.         
  2452.         if (deltaLines > 0L) {
  2453.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2454.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2455.             
  2456.             GetPort(&oldPort);
  2457.             SetPort((**theTE32KHandle).inPort);
  2458.     
  2459.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn = NewRgn());
  2460.             DisposeRgn(updateRgn);
  2461.             
  2462.             SetPort(oldPort);
  2463.         } else if (deltaLines < 0L) {
  2464.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2465.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2466.             
  2467.             GetPort(&oldPort);
  2468.             SetPort((**theTE32KHandle).inPort);
  2469.     
  2470.             updateRgn = NewRgn();
  2471.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2472.             DisposeRgn(updateRgn);
  2473.             
  2474.             SetPort(oldPort);
  2475.         }
  2476.         
  2477.         updateRect = (**theTE32KHandle).viewRect;
  2478.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L) * (**theTE32KHandle).lineHeight;
  2479.         updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * (numAffected + 1L);
  2480.         
  2481.         if ((**theTE32KHandle).caretState)
  2482.             xorCaret(theTE32KHandle);
  2483.         
  2484.         TE32KUpdate(&updateRect,theTE32KHandle);
  2485.         
  2486.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2487.         (**theTE32KHandle).selPoint = selPt;
  2488.     } else {
  2489.         updateRect = (**theTE32KHandle).viewRect;
  2490.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2491.         (**theTE32KHandle).selPoint = selPt;
  2492.         
  2493.         if ((**theTE32KHandle).nLines - firstLine >= 2L && (**theTE32KHandle).lineStarts[firstLine+1L]+1L < (**theTE32KHandle).lineStarts[firstLine + 2L]) {
  2494.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2495.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight + (**theTE32KHandle).lineHeight;
  2496.             TE32KUpdate(&updateRect,theTE32KHandle);
  2497.         } else {
  2498.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2499.             (**theTE32KHandle).selPoint = selPt;
  2500.             xorCaret(theTE32KHandle);
  2501.         }
  2502.     }
  2503. }
  2504.  
  2505.  
  2506.  
  2507. static    void OverTypeSelection(unsigned char ch,TE32KHandle theTE32KHandle)
  2508. {
  2509.     TE32KDelete(theTE32KHandle);
  2510.     
  2511.     if (ch==RETURN)
  2512.         DoReturnChar(theTE32KHandle);
  2513.     else if (ch==TAB || ch >= (unsigned char) 0x20)
  2514.         DoNormalChar(ch,theTE32KHandle);
  2515. }
  2516.  
  2517.  
  2518.  
  2519.  
  2520.  
  2521.  
  2522.  
  2523.  
  2524.  
  2525. void TE32KKey(unsigned char ch, TE32KHandle theTE32KHandle)
  2526. {
  2527.     if (theTE32KHandle && (**theTE32KHandle).active) {
  2528.         ObscureCursor();
  2529.         
  2530.         if (ch == ENTER)
  2531.             ch = RETURN;
  2532.         
  2533.         if (ch == DELETE)
  2534.             DoDeleteKey(theTE32KHandle);
  2535.             
  2536.         else if (ch==LEFTARROW || ch==RIGHTARROW || ch==UPARROW || ch==DOWNARROW)
  2537.             DoArrowKeys(ch,theTE32KHandle);
  2538.         
  2539.         else if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd && (ch >= 0x20 || ch==TAB || ch==RETURN))
  2540.             OverTypeSelection(ch,theTE32KHandle);
  2541.         
  2542.         else if (ch==TAB || ch >= (unsigned char) 0x20)
  2543.             DoNormalChar(ch,theTE32KHandle);
  2544.         
  2545.         else if (ch==RETURN)
  2546.             DoReturnChar(theTE32KHandle);
  2547.         
  2548.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && !(**theTE32KHandle).caretState)
  2549.             xorCaret(theTE32KHandle);
  2550.             
  2551.         TE32KSelView(theTE32KHandle);
  2552.     }
  2553. }
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559. static long paraLines(long firstLine, TE32KHandle theTE32KHandle)
  2560. {
  2561.     long                    lastLine, nLines;
  2562.     register unsigned char    *charBase;
  2563.     register long            *lineStarts;
  2564.  
  2565.     if ((**theTE32KHandle).crOnly)
  2566.         return(1L);
  2567.     
  2568.     lastLine = firstLine + 1L;
  2569.     nLines = (**theTE32KHandle).nLines;
  2570.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2571.     lineStarts = &((**theTE32KHandle).lineStarts[lastLine]);
  2572.     
  2573.     while (lastLine < nLines && charBase[*lineStarts - 1L] != '\r' && charBase[*lineStarts - 1L] != '\n') {
  2574.         lastLine++;
  2575.         lineStarts++;
  2576.     }
  2577.     
  2578.     return(lastLine - firstLine);
  2579. }
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585. static long    LineEndIndex(long firstLine, TE32KHandle theTE32KHandle)
  2586. {
  2587.     register unsigned char    *charPtr;
  2588.     register long            charCount;
  2589.     register short            *theCharWidths,crOnly,maxLineWidth,lineWidth;
  2590.     register unsigned char    ch;
  2591.     unsigned char            *charBase;
  2592.     Point                    cursorPt;
  2593.     short                        rightSide,destLeftSide,tabWidth;
  2594.     short                        lineStatus;
  2595.     unsigned char            *oldCharPtr;
  2596.     long                    maxRewind;
  2597.     
  2598.     if ((**theTE32KHandle).crOnly)
  2599.         return((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2600.     
  2601.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2602.     crOnly = (**theTE32KHandle).crOnly;
  2603.     
  2604.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2605.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2606.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2607.     
  2608.     if (charCount > (**theTE32KHandle).teLength)
  2609.         charCount = (**theTE32KHandle).teLength;
  2610.     
  2611.     lineStatus = 0;
  2612.     lineWidth = 0;
  2613.     
  2614.     if (charCount) {
  2615.         rightSide = (short) ((**theTE32KHandle).destRect.right);
  2616.         destLeftSide = (short) ((**theTE32KHandle).destRect.left + 1L);
  2617.         cursorPt.h = destLeftSide;
  2618.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2619.         
  2620.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2621.         
  2622.         ch = ' ';
  2623.         
  2624.         while (charCount-- && ch != '\r' && ch != '\n') {
  2625.             ch = *charPtr++;
  2626.             lineWidth++;
  2627.             
  2628.             if (ch == TAB)
  2629.                 cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2630.             else if (ch != '\r' && ch != '\n')
  2631.                 cursorPt.h += theCharWidths[ch];
  2632.             
  2633.             if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth)) {
  2634.                 maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[firstLine];
  2635.                 oldCharPtr = charPtr;
  2636.                 
  2637.                 charPtr--;
  2638.                 maxRewind--;
  2639.                 
  2640.                 while (*charPtr != ' ' && maxRewind > 0) {
  2641.                     charPtr--;
  2642.                     maxRewind--;
  2643.                 }
  2644.                 
  2645.                 if (maxRewind <= 0)
  2646.                     charPtr = oldCharPtr;
  2647.                 
  2648.                 else
  2649.                     charPtr++;
  2650.                 
  2651.                 charCount = 0;
  2652.             }
  2653.         }
  2654.     }
  2655.     
  2656.     return(charPtr - charBase);
  2657. }
  2658.  
  2659.  
  2660.  
  2661. #define    NUMTEMPLINES    32
  2662.  
  2663. static void    CalParagraph(long firstLine, TE32KHandle theTE32KHandle, long *theDeltaLines, long *theNumAffected)
  2664. {
  2665.     register unsigned char    *charPtr;
  2666.     register short            *theCharWidths;
  2667.     register long            charCount,*lineStarts,*otherLine,i;
  2668.     register long            crOnly,lineWidth,maxLineWidth;
  2669.     register unsigned char    ch;
  2670.     register long            nLines;
  2671.     long                    maxLineStarts,sizeTE32KHandle,oldCharCount;
  2672.     unsigned char            *charBase;
  2673.     Point                    cursorPt;
  2674.     short                        rightSide,destLeftSide,tabWidth,maxRewind;
  2675.     unsigned char            *oldCharPtr;
  2676.     long                    tempLineStarts[NUMTEMPLINES],oldNumLines,deltaLines;
  2677.     
  2678.     if ((**theTE32KHandle).crOnly) {
  2679.         *theDeltaLines = 0L;
  2680.         *theNumAffected = 0L;
  2681.         return;
  2682.     }
  2683.     
  2684.     deltaLines = 0L;
  2685.     
  2686.     oldNumLines = paraLines(firstLine,theTE32KHandle);
  2687.     
  2688.     for (i=0;i<oldNumLines && i <NUMTEMPLINES;i++)
  2689.         tempLineStarts[i] = (**theTE32KHandle).lineStarts[firstLine + i];
  2690.         
  2691.     sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  2692.     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2693.     
  2694.     nLines = 0;
  2695.     tempLineStarts[nLines] = (**theTE32KHandle).lineStarts[firstLine];
  2696.     
  2697.     crOnly = (**theTE32KHandle).crOnly;
  2698.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2699.     lineWidth = 0;
  2700.     
  2701.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2702.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2703.     
  2704.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2705.     ch = *charPtr;
  2706.     
  2707.     if (charCount > 0L) {
  2708.         rightSide = (short) ((**theTE32KHandle).destRect.right);
  2709.         destLeftSide = (short) ((**theTE32KHandle).destRect.left + 1L);
  2710.         cursorPt.h = destLeftSide;
  2711.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2712.         
  2713.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2714.         
  2715.         ch = ' ';
  2716.         
  2717.         while (ch != '\r' && ch != '\n' && charCount--) {
  2718.             ch = *charPtr++;
  2719.             lineWidth++;
  2720.             
  2721.             if (ch != '\r' && ch != '\n') {
  2722.                 if (ch == TAB)
  2723.                     cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2724.                 else
  2725.                     cursorPt.h += theCharWidths[ch];
  2726.                 
  2727.                 if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth)) {
  2728.                     maxRewind = charPtr - charBase - tempLineStarts[nLines];
  2729.                     oldCharPtr = charPtr;
  2730.                     oldCharCount = charCount;
  2731.                     
  2732.                     charPtr--;
  2733.                     charCount++;
  2734.                     maxRewind--;
  2735.                     
  2736.                     while (*charPtr != ' ' && maxRewind > 0) {
  2737.                         charPtr--;
  2738.                         charCount++;
  2739.                         maxRewind--;
  2740.                     }
  2741.                     
  2742.                     if (maxRewind <= 0) {
  2743.                         charPtr = oldCharPtr;
  2744.                         charCount = oldCharCount;
  2745.                     } else {
  2746.                         charPtr++;
  2747.                         charCount--;
  2748.                     }
  2749.                     
  2750.                     nLines++;
  2751.                     
  2752.                     if (nLines < NUMTEMPLINES) {
  2753.                         if (tempLineStarts[nLines] == charPtr - charBase) {
  2754.                             oldNumLines = nLines;
  2755.                             goto STOPWRAPPING;
  2756.                         } else tempLineStarts[nLines] = charPtr - charBase;
  2757.                     } else goto STOPWRAPPING;
  2758.                     
  2759.                     cursorPt.h = destLeftSide;
  2760.                     lineWidth = 0;
  2761.                 }
  2762.             }
  2763.         }
  2764.         
  2765.         nLines++;
  2766.         
  2767.         if (nLines < NUMTEMPLINES)
  2768.             tempLineStarts[nLines] = charPtr - charBase;
  2769.  
  2770. STOPWRAPPING:
  2771.  
  2772.         deltaLines = nLines - oldNumLines;
  2773.  
  2774.         if (nLines >= NUMTEMPLINES) {
  2775.             TE32KCalText(theTE32KHandle);
  2776.             deltaLines = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2777.         } else {
  2778.             if (deltaLines == 0L) {
  2779.                 for (i = 1;i <= nLines;i++)
  2780.                     (**theTE32KHandle).lineStarts[firstLine + i] = tempLineStarts[i];
  2781.             } else if (deltaLines < 0L) {
  2782.                 lineStarts = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2783.                 
  2784.                 for (i = 1;i <= nLines;i++)
  2785.                     *(lineStarts++) = tempLineStarts[i];
  2786.                 
  2787.                 otherLine = &((**theTE32KHandle).lineStarts[firstLine + oldNumLines + 1L]);
  2788.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines + 1L;
  2789.                 
  2790.                 while (i--)
  2791.                     *(lineStarts++) = *(otherLine++);
  2792.                 
  2793.                 (**theTE32KHandle).nLines += deltaLines;
  2794.             } else {
  2795.                 if ((**theTE32KHandle).nLines + deltaLines >= maxLineStarts) {
  2796.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + deltaLines + EXTRALINESTARTS);
  2797.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2798.                     
  2799.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  2800.                     
  2801.                     if (MemError()) {
  2802.                         nLines = (**theTE32KHandle).nLines;
  2803.                         deltaLines = (**theTE32KHandle).nLines;
  2804.                         goto EXITPOINT;
  2805.                     }
  2806.                 }
  2807.                 
  2808.                 lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2809.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + deltaLines]);
  2810.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2811.                 
  2812.                 while (i--)
  2813.                     *(otherLine--) = *(lineStarts--);
  2814.                     
  2815.                 for (i = nLines;i >= 0;i--)
  2816.                     *(otherLine--) = tempLineStarts[i];
  2817.                 
  2818.                 (**theTE32KHandle).nLines += deltaLines;
  2819.             }
  2820.         }
  2821.     }
  2822.     
  2823. EXITPOINT:
  2824.     *theNumAffected = nLines;
  2825.     *theDeltaLines = deltaLines;
  2826. }
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832. static void updateLine(register long firstLine, TE32KHandle theTE32KHandle,short doFirst, LongRect *updateClipRect)
  2833. {
  2834.     Rect            tempRect;
  2835.     RgnHandle        updateRgn;
  2836.     LongRect        updateRect;
  2837.     LongPoint        selPt;
  2838.     unsigned char    doWrap;
  2839.     long            deltaLines,numAffected;
  2840.     GrafPtr            oldPort;
  2841.  
  2842.     updateRect = (**theTE32KHandle).viewRect;
  2843.     updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2844.     updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  2845.     
  2846.     if (updateClipRect) {
  2847.         if (updateRect.top < updateClipRect->top)
  2848.             updateRect.top = updateClipRect->top;
  2849.         if (updateRect.bottom > updateClipRect->bottom)
  2850.             updateRect.bottom = updateClipRect->bottom;
  2851.         if (updateRect.left < updateClipRect->left)
  2852.             updateRect.left = updateClipRect->left;
  2853.         if (updateRect.right > updateClipRect->right)
  2854.             updateRect.right = updateClipRect->right;
  2855.     }
  2856.     
  2857.     doWrap = false;
  2858.     
  2859.     if (firstLine > 0L && LineEndIndex(firstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine]) {
  2860.         doWrap = true;
  2861.         firstLine--;
  2862.     } else if (LineEndIndex(firstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine + 1L])
  2863.         doWrap = true;
  2864.     
  2865.     
  2866.     if (!doWrap && doFirst)
  2867.         TE32KUpdate(&updateRect,theTE32KHandle);
  2868.     
  2869.     else if (doWrap) {
  2870.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2871.         
  2872.         if (deltaLines == 0L) {
  2873.             updateRect = (**theTE32KHandle).viewRect;
  2874.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2875.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2876.         } else if (deltaLines > 0L) {
  2877.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2878.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2879.             
  2880.             GetPort(&oldPort);
  2881.             SetPort((**theTE32KHandle).inPort);
  2882.     
  2883.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn = NewRgn());
  2884.             DisposeRgn(updateRgn);
  2885.             
  2886.             SetPort(oldPort);
  2887.             
  2888.             updateRect = (**theTE32KHandle).viewRect;
  2889.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2890.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2891.         } else {
  2892.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2893.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2894.             
  2895.             GetPort(&oldPort);
  2896.             SetPort((**theTE32KHandle).inPort);
  2897.     
  2898.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn = NewRgn());
  2899.             
  2900.             SetPort(oldPort);
  2901.             
  2902.             updateRect.left = (**updateRgn).rgnBBox.left;
  2903.             updateRect.top = (**updateRgn).rgnBBox.top;
  2904.             updateRect.right = (**updateRgn).rgnBBox.right;
  2905.             updateRect.bottom = (**updateRgn).rgnBBox.bottom;
  2906.             
  2907.             DisposeRgn(updateRgn);
  2908.             
  2909.             TE32KUpdate(&updateRect,theTE32KHandle);
  2910.             
  2911.             updateRect = (**theTE32KHandle).viewRect;
  2912.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2913.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2914.         }
  2915.         
  2916.         TE32KUpdate(&updateRect, theTE32KHandle);
  2917.     }
  2918.  
  2919.     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2920.     (**theTE32KHandle).selPoint = selPt;
  2921. }
  2922.  
  2923.  
  2924.  
  2925.  
  2926.  
  2927.  
  2928. static short shiftKeyDown()
  2929. {
  2930.     KeyMap    theKeyMap;
  2931.  
  2932.     GetKeys(theKeyMap);
  2933.     
  2934.     if (theKeyMap[1] & 0x01)
  2935.         return(true);
  2936.     else
  2937.         return(false);
  2938. }
  2939.  
  2940.  
  2941.  
  2942. static void MyClickLoop(void)
  2943. {
  2944.     short        lineHeight;
  2945.     Rect        viewRect;
  2946.     Point        mousePoint;
  2947.     RgnHandle    saveClip;
  2948.     long        hDelta,vDelta;
  2949.  
  2950.     if (clickedTE32KH) {
  2951.         LongRectToRect(&((**clickedTE32KH).viewRect), &viewRect);
  2952.         lineHeight = (**clickedTE32KH).lineHeight;
  2953.     
  2954.         hDelta = 0;
  2955.         vDelta = 0;
  2956.         
  2957.         GetMouse(&mousePoint);
  2958.         if (!PtInRect(mousePoint, &viewRect)) {
  2959.             if (mousePoint.v > viewRect.bottom && (**clickedTE32KH).viewRect.bottom < (**clickedTE32KH).destRect.top + (long) lineHeight * (**clickedTE32KH).nLines)
  2960.                 vDelta = (mousePoint.v - viewRect.bottom) / lineHeight;
  2961.             else if (mousePoint.v < viewRect.top && (**clickedTE32KH).viewRect.top > (**clickedTE32KH).destRect.top)
  2962.                 vDelta = (mousePoint.v - viewRect.top) / lineHeight;
  2963.             
  2964.             
  2965.             if (mousePoint.h > viewRect.right && (**clickedTE32KH).viewRect.right < (**clickedTE32KH).destRect.right)
  2966.                 hDelta = -lineHeight;
  2967.             else if (mousePoint.h<viewRect.left && (**clickedTE32KH).viewRect.left > (**clickedTE32KH).destRect.left)
  2968.                 hDelta = lineHeight;
  2969.         }
  2970.         
  2971.         if (hDelta || vDelta) {
  2972.             GetClip(saveClip = NewRgn());
  2973.             viewRect = (*((**clickedTE32KH).inPort)).portRect;
  2974.             ClipRect(&viewRect);
  2975.             
  2976.             TE32KScroll(hDelta, vDelta, clickedTE32KH);
  2977.             
  2978.             SetClip(saveClip);
  2979.             DisposeRgn(saveClip);
  2980.         }
  2981.     }
  2982. }
  2983.  
  2984.  
  2985.  
  2986.  
  2987. void TE32KAutoView(char autoView, TE32KHandle theTE32KHandle)
  2988. {
  2989. /*    if (theTE32KHandle) {
  2990.         if (!autoView) (**theTE32KHandle).clikLoop = nil;
  2991.         else (**theTE32KHandle).clikLoop = (TE32KProcPtr) MyClickLoop;
  2992.     }*/
  2993. }
  2994.  
  2995.  
  2996.  
  2997. short TE32KFeatureFlag(short feature, short action, TE32KHandle theTE32KHandle)
  2998. {
  2999.     switch (action) {
  3000.         case TE32KBitClear:
  3001.             (**theTE32KHandle).featureFlags &= ~(1 << feature);
  3002.             break;
  3003.         case TE32KBitSet:
  3004.             (**theTE32KHandle).featureFlags |= (1 << feature);
  3005.             break;
  3006.         case TE32KBitTest:
  3007.             return ((**theTE32KHandle).featureFlags >> feature) & 1;
  3008.     }
  3009.     return (0);
  3010. }
  3011.  
  3012.  
  3013.  
  3014. void TE32KSetClickLoop(TE32KProcPtr clickProc, TE32KHandle theTE32KHandle)
  3015. {
  3016.     (**theTE32KHandle).clikLoop = clickProc;
  3017. }
  3018.  
  3019.  
  3020.  
  3021.  
  3022. void TE32KCopyToScrap(TE32KHandle theTE32KHandle)
  3023. {
  3024.     long scrpLength;
  3025.     
  3026.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  3027.         scrpLength = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  3028.         
  3029.         HLock((**theTE32KHandle).hText);        
  3030.         (void) PutScrap(scrpLength, 'TEXT', *((**theTE32KHandle).hText) + (**theTE32KHandle).selStart);
  3031.         HUnlock((**theTE32KHandle).hText);
  3032.     }
  3033. }
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039. void TE32KCutToScrap(TE32KHandle theTE32KHandle)
  3040. {
  3041.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd) {
  3042.         TE32KCopyToScrap(theTE32KHandle);
  3043.         TE32KDelete(theTE32KHandle);
  3044.     }
  3045. }
  3046.  
  3047.  
  3048.  
  3049.  
  3050.  
  3051. void TE32KPasteFromScrap(TE32KHandle theTE32KHandle)
  3052. {
  3053.     Handle scrpHandle;
  3054.     long scrpLength, offset;
  3055.     
  3056.     if (theTE32KHandle) {
  3057.         scrpHandle = NewHandle(0);
  3058.         if (!MemError()) {
  3059.             scrpLength = GetScrap(scrpHandle, 'TEXT', &offset);
  3060.             if (scrpLength > 0) {
  3061.                 if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  3062.                     TE32KDelete(theTE32KHandle);
  3063.                 
  3064.                 HLock(scrpHandle);    
  3065.                 TE32KInsert(*scrpHandle, scrpLength, theTE32KHandle);
  3066.                 HUnlock(scrpHandle);
  3067.             }
  3068.             DisposeHandle(scrpHandle);
  3069.         }
  3070.     }
  3071. }
  3072.