home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / TE32K 1.2 / TE32K.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-07  |  81.1 KB  |  3,279 lines  |  [TEXT/KAHL]

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