home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz / TextEditControl.c < prev    next >
Encoding:
Text File  |  1991-02-21  |  50.3 KB  |  1,970 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         texteditcontrol.c
  5. ** Written by:      Eric Soldan
  6. ** Based on:        TESample, by Bryan Stearns
  7. ** Suggestions by:  Forrest Tanaka, Dave Radcliffe
  8. **
  9. ** Copyright © 1990-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13. /* This is a control implementation of TextEdit.  The advantages to this are:
  14. **
  15. ** 1) Makes using TextEdit in a non-dialog window easy.
  16. ** 2) The TextEdit record is automatically associated with the window, since
  17. **    it is in the window's control list.
  18. ** 3) The TextEdit control can have scrollbars associated with it, and these
  19. **    are also kept in the window's control list.
  20. ** 4) Updating of the TextEdit record is much simpler, since all that is
  21. **    necessary is to draw the control (or all the window's controls with
  22. **    a DrawControls call).
  23. ** 5) There are simple calls to handle TextEdit events.
  24. ** 6) Undo is already supported.
  25. ** 7) A document length can be specified.  This length will not be exceeded
  26. **    when editing the TextEdit record.
  27. ** 8) When you close the window, the TextEdit record is disposed of.
  28. **    (This automatic disposal can easily be defeated.)
  29. **
  30. **
  31. ** To create a TextEdit control, you only need a single call.  For example:
  32. **
  33. **    CTENew(rViewCtl,            Resource ID of view control for TextEdit control.
  34. **           window,                Window to hold TERecord.
  35. **           &teHndl,                Return handle for TERecord.
  36. **           &destRect,            destRect for TERecord
  37. **           &viewRect,            viewRect for TERecord
  38. **           &borderRect,            Used to frame a border.
  39. **           32000,                Max size for TERecord text.
  40. **           cteVScrollAndGrow    TERecord read-write, with vertical scroll,
  41. **                                plus space for grow box.
  42. **    );
  43. **
  44. ** If you create a TextEdit control that is read-only, you will not be able
  45. ** to edit it, of course.  There will also be no blinking caret for that
  46. ** TextEdit control.  You will be able to select text and copy to the
  47. ** clipboard, but that is all.
  48.  
  49. ** Simply create destRect, viewRect, and borderRect appropriately, and
  50. ** then call CTENew (which stands for Control TENew).  If teHndl is returned
  51. ** nil, then CTENew failed.  Otherwise, you now have a TextEdit control in
  52. ** the window.
  53. **
  54. ** NOTE: There is a TextEdit bug (no way!!) such that you may need to set the
  55. **       viewRect right edge 2 bigger than the right edge of destRect.  If you
  56. **       do not do this, then there will be some clipping on the right edge in
  57. **       some cases.  Of course, you may want this.  You may want horizontal
  58. **       scrolling, and therefore you would want the destRect substantially
  59. **       larger than the viewRect.  If you don't want horizontal scrolling,
  60. **       then you probably don't want any clipping horizontally, and therefore
  61. **       you will need to set destRect.right 2 less than viewRect.right.
  62. **
  63. **
  64. ** If the CTENew call succeeds, you then have a TextEdit control in your
  65. ** window.  It will be automatically disposed of when you close the window.
  66. ** If you don't waht this to happen, then you can detatch it from the
  67. ** view control which owns it.  To do this, you would to the following:
  68. **
  69. **  viewCtl = CTEViewFromTE(theTextEditHndl);
  70. **  if (viewCtl) SetCRefCon(viewCtl, nil);
  71. **
  72. ** The view control keeps a reference to the TextEdit record in the refCon.
  73. ** If the refCon is cleared, then the view control does nothing.  So, all that
  74. ** is needed to detatch a TextEdit record from a view control is to set the
  75. ** view control's refCon nil.  Now if you close the window, you will still
  76. ** have the TextEdit record.
  77. **
  78. **
  79. ** To remove a TextEdit control completely from a window, you make one call:
  80. **
  81. **  CTEDispose(theTextEditHndl);
  82. **
  83. ** This disposes of the TextEdit record, the view control, and any scrollbar
  84. ** controls that were created when the TextEdit control was created with
  85. ** the call CTENew.
  86. **
  87. **
  88. ** Events for TextEdit record are handled nearly automatically.  You can
  89. ** make one of 3 calls:
  90. **
  91. **  CTEClick(eventPtr);
  92. **  CTEEvent(eventPtr);
  93. **  CTEKey(eventPtr);
  94. **
  95. ** In each case, if the event was handled, true is returned.  CTEEvent simply
  96. ** calls either CTEClick or CTEKey, whichever is appropriate.
  97. **
  98. **
  99. ** Another call you will want to use is CTEEditMenu.  This is used to set the
  100. ** state of cut/copy/paste/clear for TextEdit controls.  It checks the active
  101. ** control to see if text is selected, if the control is read-only, etc.
  102. ** Based on this information, it sets cut/copy/paste/clear either active
  103. ** or inactive.  If any menu items are set active, it returns true.
  104. **
  105. **
  106. ** One more high-level call is CTEUndo().  In response to an undo menu item
  107. ** being selected by the user, just call CTEUndo(), and the edits the user
  108. ** has made will be undone.  (This includes undoing an undo.)
  109. **
  110. **
  111. ** The last high-level call is CTEClipboard.  Call it when you want to do a
  112. ** cut/copy/paste/clear for the active TextEdit control.  The value to pass
  113. ** is as follows:
  114. **
  115. **  2: cut
  116. **  3: copy
  117. **  4: paste
  118. **  5: clear
  119. **
  120. ** These are the same values you would pass to a DA for these actions.
  121. */
  122.  
  123.  
  124.  
  125. /*****************************************************************************/
  126.  
  127.  
  128.  
  129. #ifndef __TEXTEDITCONTROL__
  130. #include "TextEditControl.h"
  131. #endif
  132.  
  133. #ifndef __CONTROLS__
  134. #include <Controls.h>
  135. #endif
  136.  
  137. #ifndef __ERRORS__
  138. #include <Errors.h>
  139. #endif
  140.  
  141. #ifndef __EVENTS__
  142. #include <Events.h>
  143. #endif
  144.  
  145. #ifndef __MEMORY__
  146. #include <Memory.h>
  147. #endif
  148.  
  149. #ifndef __MENUS__
  150. #include <Menus.h>
  151. #endif
  152.  
  153. #ifndef __OSUTILS__
  154. #include <OSUtils.h>
  155. #endif
  156.  
  157. #ifndef __RESOURCES__
  158. #include <Resources.h>
  159. #endif
  160.  
  161. #ifndef __SCRAP__
  162. #include <Scrap.h>
  163. #endif
  164.  
  165.  
  166.  
  167. /*****************************************************************************/
  168.  
  169.  
  170.  
  171. typedef struct cdefRsrcJMP {
  172.     long    moveInst;
  173.     long    jsrInst;
  174.     short    jmpInst;
  175.     long    jmpAddress;
  176. } cdefRsrcJMP;
  177. typedef cdefRsrcJMP *cdefRsrcJMPPtr, **cdefRsrcJMPHndl;
  178.  
  179. typedef struct CTEDataRec {
  180.     short    maxTextLen;
  181.     Boolean    newUndo;
  182.     short    undoSelStart;
  183.     short    undoSelEnd;
  184.     Handle    undoText;
  185.     short    mode;
  186. } CTEDataRec;
  187. typedef CTEDataRec *CTEDataPtr, **CTEDataHndl;
  188.  
  189.  
  190.  
  191. /*****************************************************************************/
  192.  
  193.  
  194.  
  195. static pascal long    CTECtl(short varCode, ControlHandle ctl, short msg, long parm);
  196. static short        theViewID;
  197.  
  198.  
  199.  
  200. /*****************************************************************************/
  201.  
  202.  
  203.  
  204. static TEHandle            gActiveTEHndl;
  205.     /* Currently active TextEdit record.  (nil if none active.) */
  206.  
  207. static TEHandle            gFoundTEHndl;
  208.     /* Global value used to return info from the TextEdit control proc. */
  209.  
  210. static ControlHandle    gFoundViewCtl;
  211.     /* Global value used to return info from the TextEdit control proc. */
  212.  
  213. static pascal void        VActionProc(ControlHandle scrollCtl, short part);
  214. static pascal void        HActionProc(ControlHandle control, short part);
  215. static void                AdjustTEBottom(TEHandle teHndl);
  216. static void                AdjustScrollValues(TEHandle teHndl);
  217. static void                AdjustOneScrollValue(TEHandle teHndl, ControlHandle ctl, Boolean vert);
  218.  
  219. ClikLoopProcPtr        gDefaultClikLoop;
  220.     /* The clikLoop TextEdit wants to use.  Our custom clikLoop must call
  221.     ** this as well.  The default TextEdit clikLoop is stored here.
  222.     */
  223.  
  224. #define kTELastForWind    1
  225. #define kCrChar            13
  226.  
  227.  
  228. /*****************************************************************************/
  229. /*****************************************************************************/
  230.  
  231.  
  232.  
  233. /* Activate this TextEdit record.  If another is currently active, deactivate
  234. ** that one.  The view control for this TextEdit record is also flagged to
  235. ** indicate which was the last active one for this window.  If the previous
  236. ** active TextEdit record was in the same window, then flag the old one off
  237. ** for this window.  The whole point for this per-window flagging is so that
  238. ** activate events can reactivate the correct TextEdit control per window.
  239. */
  240.  
  241. #pragma segment Controls
  242. void    CTEActivate(TEHandle teHndl)
  243. {
  244.     WindowPtr        window, oldPort;
  245.     ControlHandle    viewCtl;
  246.     TEHandle        te;
  247.  
  248.     if (teHndl != gActiveTEHndl) CTEDeactivate();
  249.         /* If the soon-to-be active TextEdit control is different than the
  250.         ** old one, deactivate the old one.  CTEDeactivate checks for the
  251.         ** case that we don't have an old one. */
  252.  
  253.     if (!teHndl) return;
  254.         /* If the soon-to-be active TextEdit control is nil, then we are
  255.         ** done.  This is another way to have no active TextEdit control.
  256.         */
  257.  
  258.     window = (WindowPtr)(*teHndl)->inPort;
  259.     for (viewCtl = nil;;) {
  260.         viewCtl = CTENext(window, &te, viewCtl);
  261.         if (!viewCtl) break;
  262.         SetCtlValue(viewCtl, false);
  263.     }        /* Turn off the last-active flag for all TextEdit controls that
  264.             ** are in this window.
  265.             */
  266.  
  267.     gActiveTEHndl = teHndl;
  268.     viewCtl = CTEViewFromTE(teHndl);
  269.  
  270.     if (viewCtl) {
  271.         SetCtlValue(viewCtl, true);
  272.             /* Set last-active-for-this-window flag for the new one. */
  273.         GetPort(&oldPort);
  274.         SetPort(window);
  275.         TEActivate(teHndl);
  276.         SetPort(oldPort);
  277.             /* Let TextEdit know that it is supposed to be active. */
  278.     }
  279. }
  280.  
  281.  
  282.  
  283. /*****************************************************************************/
  284.  
  285.  
  286.  
  287. /* This is called when a mouseDown occurs in the content of a window.  It
  288. ** returns true if the mouseDown caused a TextEdit action to occur.  Events
  289. ** that are handled include if the user clicks on a scrollbar that is
  290. ** associated with a TextEdit control.
  291. */
  292.  
  293. #pragma segment Controls
  294. Boolean    CTEClick(EventRecord *event)
  295. {
  296.     WindowPtr        oldPort, window;
  297.     Point            mouseLoc;
  298.     TEHandle        te, realActiveTEHndl;
  299.     ControlHandle    ctlHit, viewCtl;
  300.     CTEDataHndl        teData;
  301.     Boolean            vert;
  302.     short            extendSelect, part, value;
  303.  
  304.     GetPort(&oldPort);
  305.     SetPort(window = FrontWindow());
  306.     mouseLoc = event->where;
  307.     GlobalToLocal(&mouseLoc);
  308.  
  309.     if (CTEFind(window, event, &te, &ctlHit)) {
  310.             /* See if the user clicked directly on the view control for a
  311.             ** TextEdit record.  If so, we definitely have some work to do.
  312.             */
  313.         if (te != gActiveTEHndl) {
  314.             CTEActivate(te);
  315.             return(true);
  316.                 /* If user clicked on TextEdit control other than the
  317.                 ** currently active control, then activate it.  This is our
  318.                 ** only action in this case.
  319.                 */
  320.         }
  321.         extendSelect = ((event->modifiers & shiftKey) != 0);
  322.             /* Extend-select may be occuring. */
  323.         TEClick(mouseLoc, extendSelect, te);
  324.             /* Do the extend-select thing.  Most of the work is handled by
  325.             ** TextEdit.  The only thing we have to do is to update the
  326.             ** scrollbars while the user is extending the select.  This is
  327.             ** taken care of by our custom clikLoop procedure.
  328.             */
  329.  
  330.         if (viewCtl = CTEViewFromTE(te)) {
  331.             teData = (CTEDataHndl)(*viewCtl)->contrlData;
  332.             (*teData)->newUndo = true;
  333.         }
  334.  
  335.         SetPort(oldPort);
  336.         return(true);
  337.     }
  338.  
  339. /* We didn't hit the view control for a TextEdit record, but don't give up yet.
  340. ** The user may be clicking on a related scrollbar.  Let's find out...
  341. */
  342.  
  343.     if (part = FindControl(mouseLoc, window, &ctlHit)) {
  344.             /* The user did click on a control.  But is it a scrollbar
  345.             ** for a TextEdit control?  Stay tuned...
  346.             */
  347.         te = CTEFromScroll(ctlHit, &viewCtl);
  348.  
  349.         if (te) {        /* It was a related scrollbar. */
  350.  
  351.             vert = ((*ctlHit)->contrlRect.top <= (*viewCtl)->contrlRect.top);
  352.                 /* Horizontal or vertical scroll.  Only the rect knows. */
  353.  
  354.             switch (part) {
  355.                 case inThumb:
  356.                     value = GetCtlValue(ctlHit);
  357.                     part = TrackControl(ctlHit, mouseLoc, nil);
  358.                     if (part) {
  359.                         value -= GetCtlValue(ctlHit);
  360.                             /* Value now has CHANGE in value.
  361.                             ** if value changed, scroll. */
  362.                         if (value) {
  363.                             if (vert)
  364.                                 TEScroll(0, value, te);
  365.                             else
  366.                                 TEScroll(value, 0, te);
  367.                         }
  368.                     }
  369.                     break;
  370.  
  371.                 default:
  372.                     realActiveTEHndl = gActiveTEHndl;
  373.                     gActiveTEHndl = te;
  374.                         /* This is a hack way to pass the action procedure
  375.                         ** which TextEdit record we are dealing with.
  376.                         */
  377.                     if (vert)
  378.                         TrackControl(ctlHit, mouseLoc, (ProcPtr)VActionProc);
  379.                     else
  380.                         TrackControl(ctlHit, mouseLoc, (ProcPtr)HActionProc);
  381.  
  382.                     gActiveTEHndl = realActiveTEHndl;
  383.                         /* Unhack our previous hack. */
  384.                     break;
  385.             }
  386.             SetPort(oldPort);
  387.             return(true);
  388.         }
  389.     }
  390.  
  391.     SetPort(oldPort);
  392.     return(false);
  393. }
  394.  
  395.  
  396.  
  397. /*****************************************************************************/
  398.  
  399.  
  400.  
  401. /* This is the custom clikLoop, which is called from the assembly glue code.
  402. ** This handles updating the scrollbars as the user is drag-selecting in
  403. ** the TextEdit control.
  404. */
  405.  
  406. #pragma segment Controls
  407. void    CTEClikLoop(void)
  408. {
  409.     WindowPtr        oldPort, window;
  410.     TEHandle        te;
  411.     Point            mouseLoc;
  412.     Rect            viewRct;
  413.     RgnHandle        rgn;
  414.     short            dl, dr, dt, db, lh;
  415.     long            tick;
  416.  
  417.     GetPort(&oldPort);
  418.     SetPort(window = (WindowPtr)(*gActiveTEHndl)->inPort);
  419.  
  420.     te = gActiveTEHndl;
  421.         /* This better be what the user is dragging, or we did something
  422.         ** wrong elsewhere.
  423.         */
  424.  
  425.     GetMouse(&mouseLoc);
  426.     viewRct = (*te)->viewRect;
  427.  
  428.     if (!PtInRect(mouseLoc, &viewRct)) {
  429.         /* User is outside the TextEdit area, so scrolling is happening. */
  430.  
  431.         tick = TickCount();
  432.             /* As an extra feature, there is a zone around the TextEdit
  433.             ** viewRect that the scroll will be slowed down.  This zone
  434.             ** is based on the lineHeight of the active TextEdit control.
  435.             ** If the user drags outside the viewRect further than the
  436.             ** value of lineHeight, then scrolling occurs as fast as possible.
  437.             */
  438.  
  439.         rgn = NewRgn();
  440.         GetClip(rgn);
  441.         ClipRect(&(window->portRect));
  442.             /* The clipRgn is set to protect everything outside viewRect.
  443.             ** This doesn't work very well when we want to change
  444.             ** the scrollbars.  Save the old and open it up.
  445.             */
  446.  
  447.         dl = viewRct.left - mouseLoc.h;
  448.         dr = mouseLoc.h   - viewRct.right;
  449.         dt = viewRct.top  - mouseLoc.v;
  450.         db = mouseLoc.v   - viewRct.bottom;
  451.             /* Check the delta value for each side of viewRect.  This will
  452.             ** be used to determine if we should scroll fast or slow.
  453.             */
  454.  
  455.         AdjustScrollValues(te);
  456.             /* Scroll them puppies. */
  457.  
  458.         SetClip(rgn);                                /* restore clip */
  459.         DisposeRgn(rgn);
  460.             /* Make Mr. clipRgn happy again. */
  461.  
  462.         lh = (*te)->lineHeight;
  463.         if ((dl < lh) && (dr < lh) && (dt < lh) && (db < lh))
  464.             while (TickCount() <= tick + 9);
  465.                 /* Do it really slow.  (This is important on an fx!!) */
  466.     }
  467.  
  468.     SetPort(oldPort);
  469. }
  470.  
  471.  
  472.  
  473. /*****************************************************************************/
  474.  
  475.  
  476.  
  477. /* Do the cut/copy/paste/clear operations for the currently active
  478. ** TextEdit control.  Caller assumes appropriateness of the call.  Typically,
  479. ** this routine won't be called at an inappropriate time, since the menu
  480. ** item should be enabled or disabled correctly.
  481. ** Use CTEEditMenu to set the menu items undo/cut/copy/paste/clear correctly
  482. ** for the active TextEdit control.  Since undo isn't currently supported,
  483. ** all that CTEEditMenu does for the undo case is to deactivate it right now.
  484. */
  485.  
  486. #pragma segment Controls
  487. void    CTEClipboard(short menuID)
  488. {
  489.     WindowPtr        oldPort;
  490.     TEHandle        te;
  491.     ControlHandle    viewCtl;
  492.     short            maxTextLen, charsToAdd;
  493.  
  494.     if (!(te = gActiveTEHndl)) return;
  495.  
  496.     GetPort(&oldPort);
  497.     SetPort((*te)->inPort);
  498.     viewCtl = CTEViewFromTE(te);
  499.     switch (menuID) {
  500.         case 2:
  501.             CTENewUndo(viewCtl, true);
  502.             TECut(te);
  503.             ZeroScrap();
  504.             if (TEToScrap()) ZeroScrap();
  505.             break;
  506.         case 3:
  507.             TECopy(te);
  508.             ZeroScrap();
  509.             if (TEToScrap()) ZeroScrap();
  510.             break;
  511.         case 4:
  512.             TEFromScrap();
  513.             if (viewCtl) {
  514.                 maxTextLen = (*(CTEDataHndl)((*viewCtl)->contrlData))->maxTextLen;
  515.                 charsToAdd = TEGetScrapLen() - ((*te)->selEnd - (*te)->selStart);
  516.                 if ((*te)->teLength + charsToAdd <= maxTextLen) {
  517.                     CTENewUndo(viewCtl, true);
  518.                     TEPaste(te);
  519.                 }
  520.             }
  521.             break;
  522.         case 5:
  523.             CTENewUndo(viewCtl, true);
  524.             TEDelete(te);
  525.             break;
  526.     }
  527.  
  528.     AdjustTEBottom(te);
  529.     AdjustScrollValues(te);
  530.     SetPort(oldPort);
  531. }
  532.  
  533.  
  534.  
  535. /*****************************************************************************/
  536.  
  537.  
  538.  
  539. #pragma segment Controls
  540. pascal long    CTECtl(short varCode, ControlHandle ctl, short msg, long parm)
  541. {
  542. #pragma unused (varCode)
  543.  
  544.     Rect            viewRct;
  545.     TEHandle        te;
  546.     CTEDataHndl        teData;
  547.     ControlHandle    scrollCtl;
  548.     short            i;
  549.  
  550.     if (te = (TEHandle)GetCRefCon(ctl)) viewRct = (*te)->viewRect;
  551.     else SetRect(&viewRct, 0, 0, 0, 0);
  552.  
  553.     switch (msg) {
  554.         case drawCntl:
  555.             CTEUpdate(te, ctl);
  556.             break;
  557.  
  558.         case testCntl:
  559.             if (PtInRect(*(Point *)&parm, &viewRct)) {
  560.                 gFoundViewCtl = ctl;
  561.                 gFoundTEHndl  = te;
  562.                 return(1);
  563.             }
  564.             return(0);
  565.             break;
  566.  
  567.         case calcCRgns:
  568.         case calcCntlRgn:
  569.             if (msg == calcCRgns) parm &= 0x00FFFFFF;
  570.             RectRgn((RgnHandle)parm, &viewRct);
  571.             break;
  572.  
  573.         case initCntl:
  574.             break;
  575.  
  576.         case dispCntl:
  577.             if (te) {
  578.                 if (te == gActiveTEHndl) gActiveTEHndl = nil;
  579.                 TEDispose(te);
  580.                 if (teData = (CTEDataHndl)(*ctl)->contrlData) {
  581.                     if ((*teData)->undoText) DisposHandle((Handle)(*teData)->undoText);
  582.                     DisposHandle((Handle)teData);
  583.                 }
  584.                 for (i = 0; i < 2; ++i)
  585.                     if (scrollCtl = CTEScrollFromView(ctl, i))
  586.                         DisposeControl(scrollCtl);
  587.             }
  588.             break;
  589.  
  590.         case posCntl:
  591.             break;
  592.  
  593.         case thumbCntl:
  594.             break;
  595.  
  596.         case dragCntl:
  597.             break;
  598.  
  599.         case autoTrack:
  600.             break;
  601.     }
  602.  
  603.     return(0);
  604. }
  605.  
  606.  
  607.  
  608. /*****************************************************************************/
  609.  
  610.  
  611.  
  612. #pragma segment Controls
  613. void    CTEDeactivate(void)
  614. {
  615.     WindowPtr    oldPort;
  616.  
  617.     if (gActiveTEHndl) {    /* If we have an active TextEdit control... */
  618.         GetPort(&oldPort);
  619.         SetPort((WindowPtr)(*gActiveTEHndl)->inPort);
  620.         TEDeactivate(gActiveTEHndl);
  621.         SetPort(oldPort);
  622.         gActiveTEHndl = nil;    /* We don't have one anymore. */
  623.     }
  624. }
  625.  
  626.  
  627.  
  628. /*****************************************************************************/
  629.  
  630.  
  631.  
  632. #pragma segment Controls
  633. void    CTEDispose(TEHandle teHndl)
  634. {
  635.     WindowPtr    oldPort;
  636.  
  637.     GetPort(&oldPort);
  638.     SetPort((*teHndl)->inPort);
  639.     TEDispose(CTEDisposeView(CTEViewFromTE(teHndl)));
  640.         /* Dispose of the TextEdit control completely.  This includes
  641.         ** scrollbars, as well as the TextEdit view control.
  642.         */
  643.     SetPort(oldPort);
  644. }
  645.  
  646.  
  647.  
  648. /*****************************************************************************/
  649.  
  650.  
  651.  
  652. /* Dispose of the view control and related scrollbars.  This function also
  653. ** returns the handle to the TextEdit record, since it was just orphaned.
  654. ** Use this function if you want to get rid of a TextEdit control, but you
  655. ** want to keep the TextEdit record.
  656. */
  657.  
  658. #pragma segment Controls
  659. TEHandle    CTEDisposeView(ControlHandle viewCtl)
  660. {
  661.     TEHandle        te;
  662.     short            vert;
  663.     ControlHandle    ctl;
  664.  
  665.     te = (TEHandle)GetCRefCon(viewCtl);
  666.     SetCRefCon(viewCtl, nil);
  667.  
  668.     for (vert = 0; vert < 2; ++vert)
  669.         if (ctl = CTEScrollFromView(viewCtl, vert))
  670.             DisposeControl(ctl);
  671.  
  672.     DisposeControl(viewCtl);
  673.     return(te);
  674. }
  675.  
  676.  
  677.  
  678. /*****************************************************************************/
  679.  
  680.  
  681.  
  682. #pragma segment Controls
  683. short    CTEDocHeight(TEHandle teHndl)
  684. {
  685.     return(CTENumTextLines(teHndl) * (*teHndl)->lineHeight);
  686. }
  687.  
  688.  
  689.  
  690. /*****************************************************************************/
  691.  
  692.  
  693.  
  694. /* Enable or disable edit menu items based on the active TextEdit control.
  695. ** You pass the menu ID of the undo item in undoID, and the menu ID of the
  696. ** cut item in cutID.  If undoID or cutID is non-zero, then some action is
  697. ** performed.  Since I don't support TextEdit control undo yet (next version?),
  698. ** all that passing a non-zero value for undoID does is disable the undo
  699. ** menu item.  If you pass a non-zero value for cutID, then the other menu
  700. ** items cut/copy/paste/clear are updated to reflect the status of the
  701. ** active TextEdit control.
  702. */
  703.  
  704. #pragma segment Controls
  705. Boolean    CTEEditMenu(Boolean *activeItem, short editMenu, short undoID, short cutID)
  706. {
  707.     TEHandle        te;
  708.     MenuHandle        menu;
  709.     Boolean            active;
  710.     ControlHandle    viewCtl;
  711.     CTEDataHndl        teData;
  712.  
  713.     *activeItem = active = false;
  714.     menu = GetMHandle(editMenu);
  715.  
  716.     if (undoID)
  717.         DisableItem(menu, undoID);
  718.     if (cutID) {
  719.         DisableItem(menu, cutID);            /* Disable cut. */
  720.         DisableItem(menu, cutID + 1);        /* Disable copy. */
  721.         DisableItem(menu, cutID + 2);        /* Disable paste. */
  722.         DisableItem(menu, cutID + 3);        /* Disable clear. */
  723.     }
  724.  
  725.     if (!(te = gActiveTEHndl)) return(false);
  726.  
  727.     if (undoID) {
  728.         if (viewCtl = CTEViewFromTE(te)) {
  729.             teData = (CTEDataHndl)(*viewCtl)->contrlData;
  730.             if ((*teData)->undoText) {
  731.                 EnableItem(menu, undoID);
  732.                 active = true;
  733.             }
  734.         }
  735.     }
  736.  
  737.     if (cutID) {
  738.         if ((*te)->selStart != (*te)->selEnd) {
  739.             if (!CTEReadOnly(te)) {
  740.                 EnableItem(menu, cutID);        /* Enable cut. */
  741.                 EnableItem(menu, cutID + 3);    /* Enable clear. */
  742.             }
  743.             active = true;
  744.             EnableItem(menu, cutID + 1);        /* Enable copy. */
  745.         }
  746.         if (!CTEReadOnly(te)) {
  747.             TEFromScrap();
  748.             if (TEGetScrapLen()) {
  749.                 active = true;
  750.                 EnableItem(menu, cutID + 2);        /* Enable paste. */
  751.             }
  752.         }
  753.     }
  754.  
  755.     *activeItem = active;
  756.     return(true);
  757. }
  758.  
  759.  
  760.  
  761. /*****************************************************************************/
  762.  
  763.  
  764.  
  765. /* Handle the event if it applies to the active TextEdit control.  If some
  766. ** action occured due to the event, return true.
  767. */
  768.  
  769. #pragma segment Controls
  770. Boolean    CTEEvent(EventRecord *event)
  771. {
  772.     WindowPtr    window;
  773.  
  774.     switch(event->what) {
  775.  
  776.         case mouseDown:
  777.             if (FindWindow(event->where, &window) == inContent)
  778.                 if (window == FrontWindow())
  779.                     return(CTEClick(event));
  780.             break;
  781.  
  782.         case autoKey:
  783.         case keyDown:
  784.             if (!(event->modifiers & cmdKey))
  785.                 return(CTEKey(event));
  786.             break;
  787.     }
  788.  
  789.     return(false);
  790. }
  791.  
  792.  
  793.  
  794. /*****************************************************************************/
  795.  
  796.  
  797.  
  798. /* This determines if a TextEdit control was clicked on directly.  This does
  799. ** not determine if a related scrollbar was clicked on.  If a TextEdit
  800. ** control was clicked on, then true is returned, as well as the TextEdit
  801. ** handle and the handle to the view control.
  802. */
  803.  
  804. #pragma segment Controls
  805. Boolean    CTEFind(WindowPtr window, EventRecord *event,
  806.                 TEHandle *teHndl, ControlHandle *ctlHit)
  807. {
  808.     WindowPtr        oldPort;
  809.     Point            mouseLoc;
  810.  
  811.     GetPort(&oldPort);
  812.     SetPort(window);
  813.     mouseLoc = event->where;
  814.     GlobalToLocal(&mouseLoc);
  815.     SetPort(oldPort);
  816.  
  817.     gFoundTEHndl = nil;
  818.     FindControl(mouseLoc, window, ctlHit);
  819.     if (*teHndl = gFoundTEHndl) return(true);
  820.  
  821.     *ctlHit = nil;
  822.     return(false);
  823. }
  824.  
  825.  
  826.  
  827. /*****************************************************************************/
  828.  
  829.  
  830.  
  831. /* Find the TextEdit record that is related to the indicated scrollbar. */
  832.  
  833. #pragma segment Controls
  834. TEHandle    CTEFromScroll(ControlHandle scrollCtl, ControlHandle *retCtl)
  835. {
  836.     WindowPtr        window;
  837.     ControlHandle    viewCtl;
  838.     TEHandle        te;
  839.  
  840.     window = (*scrollCtl)->contrlOwner;
  841.  
  842.     for (*retCtl = viewCtl = nil;;) {
  843.         viewCtl = CTENext(window, &te, viewCtl);
  844.         if (!viewCtl) return(nil);
  845.         if (viewCtl == (ControlHandle)GetCRefCon(scrollCtl)) {
  846.             *retCtl = viewCtl;
  847.             return(te);
  848.         }
  849.     }
  850. }
  851.  
  852.  
  853.  
  854. /*****************************************************************************/
  855.  
  856.  
  857.  
  858. #pragma segment Controls
  859. void    CTEHide(TEHandle teHndl)
  860. {
  861.     ControlHandle    viewCtl, scrollCtl;
  862.     short            i;
  863.  
  864.     if (teHndl == gActiveTEHndl) CTEDeactivate();
  865.     viewCtl = CTEViewFromTE(teHndl);
  866.     if (viewCtl) {
  867.         HideControl(viewCtl);
  868.         for (i = 0; i < 2; i++) {
  869.             scrollCtl = CTEScrollFromView(viewCtl, i);
  870.             if (scrollCtl) HideControl(scrollCtl);
  871.         }
  872.     }
  873. }
  874.  
  875.  
  876.  
  877. /*****************************************************************************/
  878.  
  879.  
  880.  
  881. /* Blink the caret in the active TextEdit control.  The active TextEdit
  882. ** control may be read-only, in which case the caret does not blink. */
  883.  
  884. #pragma segment Controls
  885. void    CTEIdle(void)
  886. {
  887.     WindowPtr    window;
  888.  
  889.     if (gActiveTEHndl)
  890.         if (window = FrontWindow())
  891.             if (((WindowPeek)window)->windowKind >= userKind)
  892.                 TEIdle(gActiveTEHndl);
  893. }
  894.  
  895.  
  896.  
  897. /*****************************************************************************/
  898.  
  899.  
  900.  
  901. /* See if the keypress event applies to the TextEdit control, and if it does,
  902. ** handle it and return true.
  903. */
  904.  
  905. #pragma segment Controls
  906. Boolean    CTEKey(EventRecord *event)
  907. {
  908.     TEHandle        te;
  909.     ControlHandle    viewCtl;
  910.     short            maxTextLen;
  911.     char            key;
  912.     CTEDataHndl        teData;
  913.  
  914.     if (!(te = gActiveTEHndl))          return(false);
  915.     if (CTEReadOnly(te))                return(false);
  916.     if (!(viewCtl = CTEViewFromTE(te))) return(false);
  917.  
  918.     teData     = (CTEDataHndl)(*viewCtl)->contrlData;
  919.     maxTextLen = (*teData)->maxTextLen;
  920.     key        = event->message & charCodeMask;
  921.  
  922.     if (
  923.         ((*te)->selStart != (*te)->selEnd) ||
  924.         (key == 8) ||
  925.         ((*te)->teLength < maxTextLen)
  926.     ) {
  927.         CTENewUndo(viewCtl, false);
  928.         TEKey(key, te);
  929.         AdjustTEBottom(te);
  930.         AdjustScrollValues(te);
  931.     }
  932.  
  933.     return(true);
  934. }
  935.  
  936.  
  937.  
  938. /*****************************************************************************/
  939.  
  940.  
  941.  
  942. /* This function is used to move a TextEdit control.  Pass it the TextEdit
  943. ** record to move, plus the new position.  It will move the TextEdit control,
  944. ** along with any scrollbars the control may have.  All areas that need
  945. ** updating are cleared and invalidated.
  946. */
  947.  
  948. #pragma segment Controls
  949. void    CTEMove(TEHandle teHndl, short newH, short newV)
  950. {
  951.     WindowPtr        oldPort;
  952.     Rect            viewRct, rct;
  953.     short            i, dx, dy, mode;
  954.     ControlHandle    viewCtl, ctl;
  955.     CTEDataHndl        teData;
  956.  
  957.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  958.  
  959.     GetPort(&oldPort);
  960.     SetPort((*teHndl)->inPort);
  961.  
  962.     viewRct = (*viewCtl)->contrlRect;
  963.     EraseRect(&viewRct);
  964.  
  965.     dx = newH - viewRct.left;
  966.     dy = newV - viewRct.top;
  967.  
  968.     for (i = 0; i < 2; ++i) {
  969.         if (ctl = CTEScrollFromView(viewCtl, i)) {
  970.             rct = (*ctl)->contrlRect;
  971.             MoveControl(ctl, rct.left + dx, rct.top + dy);
  972.         }
  973.     }
  974.  
  975.     OffsetRect(&(*viewCtl)->contrlRect, dx, dy);
  976.     OffsetRect(&(*teHndl)->destRect, dx, dy);
  977.     OffsetRect(&(*teHndl)->viewRect, dx, dy);
  978.  
  979.     rct = viewRct = (*viewCtl)->contrlRect;
  980.     InvalRect(&viewRct);
  981.  
  982.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  983.     mode   = (*teData)->mode;
  984.     rct.top  = rct.bottom - 16;
  985.     rct.left = rct.right - 16;
  986.  
  987.     if (mode & (cteVScrollAndGrow - cteVScroll + cteHScrollAndGrow - cteHScroll))
  988.         EraseRect(&rct);
  989.  
  990.     if (mode & (cteVScrollAndGrow - cteVScroll)) OffsetRect(&rct, 16, 0);
  991.     if (mode & (cteHScrollAndGrow - cteHScroll)) OffsetRect(&rct, 0, 16);
  992.     InvalRect(&rct);
  993.  
  994.     SetPort(oldPort);
  995. }
  996.  
  997.  
  998.  
  999. /*****************************************************************************/
  1000.  
  1001.  
  1002.  
  1003. /* Create a new TextEdit control.  See the comments at the beginning of this
  1004. ** file for more information.
  1005. */
  1006.  
  1007. #pragma segment Controls
  1008. void    CTENew(short viewID, WindowPtr window, TEHandle *teHndl, Rect *dRect,
  1009.                Rect *vRect, Rect *bRect, short maxTextLen, short mode)
  1010. {
  1011.     Rect            destRect, viewRect, brdrRect;
  1012.     WindowPtr        oldPort;
  1013.     TEHandle        te;
  1014.     Boolean            err;
  1015.     short            width, height;
  1016.     Rect            ctlRect;
  1017.     ControlHandle    viewCtl, hScrollCtl, vScrollCtl;
  1018.     cdefRsrcJMPHndl    cdefRsrc;
  1019.     CTEDataHndl        teData;
  1020.  
  1021.     theViewID = viewID;        /* Keep viewID that was passed in. */
  1022.  
  1023.     GetPort(&oldPort);
  1024.     SetPort(window);
  1025.  
  1026.     destRect = *dRect;
  1027.     viewRect = *vRect;
  1028.     brdrRect = *bRect;
  1029.         /* Make sure that the rects are not in memory that may move. */
  1030.  
  1031.     te = TENew(&destRect, &viewRect);
  1032.         /* Do the main thing. */
  1033.  
  1034.     err = false;
  1035.     viewCtl = hScrollCtl = vScrollCtl = nil;
  1036.         /* Prepare for various failures. */
  1037.  
  1038.     if (te) {        /* If we were able to create the TextEdit record... */
  1039.  
  1040.         TEAutoView(true, te);
  1041.             /* Let TextEdit 3.0 do most of the scrolling work. */
  1042.         gDefaultClikLoop = (*te)->clikLoop;
  1043.         (*te)->clikLoop  = AsmTEClikLoop;
  1044.             /* We will do the remainder of the work. */
  1045.  
  1046.         cdefRsrc = (cdefRsrcJMPHndl)GetResource('CDEF', viewID);
  1047.         (*cdefRsrc)->jmpAddress = (ProcPtr)CTECtl;
  1048.         viewCtl = NewControl(window, &brdrRect, nil, true, 0, 0, 0,
  1049.                              viewID * 16, (long)te);
  1050.             /* Use our custom view cdef.  It's wierd, but it's small. */
  1051.  
  1052.         if (!viewCtl) err = true;
  1053.         else {
  1054.             (*viewCtl)->contrlData = nil;
  1055.             if (teData = (CTEDataHndl)NewHandle(sizeof(CTEDataRec))) {
  1056.                 (*teData)->maxTextLen  = maxTextLen;
  1057.                 (*teData)->undoText    = nil;
  1058.                 (*teData)->mode        = mode;
  1059.                 (*viewCtl)->contrlData = (Handle)teData;
  1060.             }
  1061.             else err = true;
  1062.  
  1063.             if (mode & cteHScroll) {        /* Caller wants a horizontal scrollbar... */
  1064.                 SetRect(&ctlRect, 0, 0, 100, 16);
  1065.                 hScrollCtl = NewControl(window, &ctlRect, nil, true, 0, 0, 0,
  1066.                                         scrollBarProc, (long)viewCtl);
  1067.                 if (hScrollCtl) {
  1068.                     MoveControl(hScrollCtl, brdrRect.left, brdrRect.bottom - 1);
  1069.                     width = brdrRect.right - brdrRect.left;
  1070.                     if (mode & (cteHScrollAndGrow - cteHScroll))
  1071.                         if (!(mode & cteVScroll)) width -= 15;
  1072.                     SizeControl(hScrollCtl, width, 16);
  1073.                         /* Line the scrollbar up with the borderRect. */
  1074.                 }
  1075.                 else err = true;
  1076.             }
  1077.  
  1078.             if (mode & cteVScroll) {        /* Caller wants a vertical scrollbar... */
  1079.                 SetRect(&ctlRect, 0, 0, 16, 100);
  1080.                 vScrollCtl = NewControl(window, &ctlRect, nil, true, 0, 0, 0,
  1081.                                         scrollBarProc, (long)viewCtl);
  1082.                 if (vScrollCtl) {
  1083.                     MoveControl(vScrollCtl, brdrRect.right - 1, brdrRect.top);
  1084.                     height = brdrRect.bottom - brdrRect.top;
  1085.                     if (mode & (cteVScrollAndGrow - cteVScroll))
  1086.                         if (!(mode & cteHScroll)) height -= 15;
  1087.                     SizeControl(vScrollCtl, 16, height);
  1088.                         /* Line the scrollbar up with the borderRect. */
  1089.                 }
  1090.                 else err = true;
  1091.             }
  1092.         }
  1093.     }
  1094.     else err = true;
  1095.  
  1096.     SetPort(oldPort);
  1097.  
  1098.     if (err) {        /* Oops.  Somebody wasn't happy. */
  1099.         if (viewCtl)
  1100.             DisposeControl(viewCtl);
  1101.                 /* This also disposes of TextEdit handle! */
  1102.         else
  1103.             if (te) TEDispose(te);
  1104.                 /* We have to dispose of the TextEdit handle ourselves if
  1105.                 ** creating the view control failed. */
  1106.  
  1107.         te = nil;        /* Return that there is no TextEdit control. */
  1108.  
  1109.         if (hScrollCtl)
  1110.             DisposeControl(hScrollCtl);
  1111.                 /* More clean-up. */
  1112.  
  1113.         if (vScrollCtl)
  1114.             DisposeControl(vScrollCtl);
  1115.                 /* And still more clean-up. */
  1116.     }
  1117.     else {
  1118.         if (mode & cteReadOnly)
  1119.             (*te)->caretHook = (ProcPtr)AsmNoCaret;
  1120.                 /* If read-only, then disable caret for this
  1121.                 ** TextEdit control.
  1122.                 */
  1123.         CTEActivate(te);
  1124.     }
  1125.  
  1126.     AdjustScrollValues(*teHndl = te);
  1127.         /* Give the scrollbars an initial value.  This is because the
  1128.         ** TextEdit control could have been created with
  1129.         ** destRect.top < viewRect.top or destRect.left < viewRect.left.
  1130.         ** I don't know why anyone would want to, but it is legal.
  1131.         */
  1132. }
  1133.  
  1134.  
  1135.  
  1136. /*****************************************************************************/
  1137.  
  1138.  
  1139.  
  1140. /* Save the data (if appropriate) so that user can undo. */
  1141.  
  1142. #pragma segment Controls
  1143. void    CTENewUndo(ControlHandle viewCtl, Boolean alwaysNewUndo)
  1144. {
  1145.     TEHandle    teHndl;
  1146.     CTEDataHndl    teData;
  1147.     Handle        hText, uText;
  1148.  
  1149.     if (!viewCtl) return;
  1150.  
  1151.     teHndl = (TEHandle)GetCRefCon(viewCtl);
  1152.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1153.  
  1154.     hText = (*teHndl)->hText;
  1155.     uText = (*teData)->undoText;
  1156.         /* hText is text in the TextEdit record that is being edited.  */
  1157.         /* uText is the undo data (if any) prior to current edit task. */
  1158.  
  1159.     if (!uText) {                /* No undo text (therefore no editing) yet. */
  1160.         uText = NewHandle((*teHndl)->teLength);
  1161.         if (!uText) return;        /* Not enough memory to support undo. */
  1162.         (*teData)->undoText = uText;
  1163.         alwaysNewUndo = true;
  1164.             /* Since this is our first edit, we will want to cache the
  1165.             ** data, no matter for what reason we were called. */
  1166.     }
  1167.  
  1168.     if ((alwaysNewUndo) || ((*teData)->newUndo)) {
  1169.         SetHandleSize(uText, (*teHndl)->teLength);
  1170.         if (MemError()) return;
  1171.             /* Not enough memory to handle this undo. */
  1172.         BlockMove(*hText, *uText, (*teHndl)->teLength);
  1173.         (*teData)->newUndo      = false;
  1174.         (*teData)->undoSelStart = (*teHndl)->selStart;
  1175.         (*teData)->undoSelEnd   = (*teHndl)->selEnd;
  1176.     }
  1177. }
  1178.  
  1179.  
  1180.  
  1181. /*****************************************************************************/
  1182.  
  1183.  
  1184.  
  1185. /* Get the next TextEdit control in the window.  You pass it a control handle
  1186. ** for the view control, or nil to start at the beginning of the window.
  1187. ** It returns both a TextEdit handle and the view control handle for that
  1188. ** TextEdit record.  If none is found, nil is returned.  This allows you to
  1189. ** repeatedly call this function and walk through all the TextEdit controls
  1190. ** in a window.
  1191. */
  1192.  
  1193. #pragma segment Controls
  1194. ControlHandle    CTENext(WindowPtr window, TEHandle *teHndl, ControlHandle ctl)
  1195. {
  1196.     short    defProcID;
  1197.     ResType    defProcType;
  1198.     Str255    defProcName;
  1199.  
  1200.     *teHndl = nil;
  1201.  
  1202.     if (!ctl)
  1203.         ctl = ((WindowPeek)window)->controlList;
  1204.     else
  1205.         ctl = (*ctl)->nextControl;
  1206.  
  1207.     while (ctl) {
  1208.         defProcID = !theViewID;
  1209.         GetResInfo((*ctl)->contrlDefProc, &defProcID, &defProcType, defProcName);
  1210.         if (defProcID == theViewID) {
  1211.             *teHndl = (TEHandle)GetCRefCon(ctl);
  1212.             break;
  1213.         }
  1214.         ctl = (*ctl)->nextControl;
  1215.     }
  1216.  
  1217.     return(ctl);
  1218. }
  1219.  
  1220.  
  1221.  
  1222. /*****************************************************************************/
  1223.  
  1224.  
  1225.  
  1226. /* Return the number of lines of text.  This is because there is a bug in
  1227. ** TextEdit where the number of lines returned is incorrect if the text
  1228. ** ends with a c/r.  This function adjusts for this bug.
  1229. */
  1230.  
  1231. #pragma segment Controls
  1232. short    CTENumTextLines(TEHandle teHndl)
  1233. {
  1234.     short    lines;
  1235.     char    *cptr;
  1236.  
  1237.     lines = (*teHndl)->nLines;
  1238.  
  1239.     cptr = *((*teHndl)->hText);        /* Pointer to first TextEdit character. */
  1240.     if (cptr[(*teHndl)->teLength - 1] == kCrChar) ++lines;
  1241.         /* Since nLines isn’t right if the last character is a return,
  1242.         ** check for that case and fix it.
  1243.         */
  1244.  
  1245.     return(lines);
  1246. }
  1247.  
  1248.  
  1249.  
  1250. /*****************************************************************************/
  1251.  
  1252.  
  1253.  
  1254. /* Return the number of text lines in the view area. */
  1255.  
  1256. #pragma segment Controls
  1257. short    CTENumViewLines(TEHandle teHndl)
  1258. {
  1259.     short    viewHeight;
  1260.  
  1261.     viewHeight = (*teHndl)->viewRect.bottom - (*teHndl)->viewRect.top;
  1262.     return(viewHeight / (*teHndl)->lineHeight);
  1263. }
  1264.  
  1265.  
  1266.  
  1267. /*****************************************************************************/
  1268.  
  1269.  
  1270.  
  1271. /* Use this function to print the contents of a TextEdit record.  Pass it a
  1272. ** TextEdit handle, a pointer to a text offset, and a pointer to a rect to
  1273. ** print the text in.  The offset should be initialized to what character
  1274. ** in the TextEdit record you wish to start printing at (most likely 0).
  1275. ** The print function prints as much text as will fit in the rect, and
  1276. ** then updates the offset to tell you what is the first character that didn't
  1277. ** print.  You can then call the print function again with another rect with
  1278. ** this new offset, and it will print the text starting at the new offset.
  1279. ** This method is very useful when a single TextEdit record is longer than a
  1280. ** single page, and you wish the text to break at the end of the page.
  1281. ** The bottom of rect is also updated, along with the offset.  The bottom edge
  1282. ** of the rect is changed to reflect the actual bottom of the text printed.
  1283. ** This is useful because the rect passed in didn't necessarily hold an
  1284. ** integer number of lines of text.  The bottom of the rect is adjusted so
  1285. ** it exactly holds complete lines of text.
  1286. ** It is also possible that the rect could hold substantially more lines of
  1287. ** text than there are remaining.  Again, in this situation, the bottom of
  1288. ** rect is adjusted so that the rect tightly bounds the text printed.
  1289. ** The remaining piece of information passed back is an indicator that the
  1290. ** text through the end of the TextEdit record was printed.  When the end
  1291. ** of the text is reached, the offset for the next text to be printed is
  1292. ** returned as -1.  This indicates that processing of the TextEdit record
  1293. ** is complete.
  1294. */
  1295.  
  1296. #pragma segment Controls
  1297. OSErr    CTEPrint(TEHandle teHndl, short *teOffset, Rect *teRct)
  1298. {
  1299.     short        len, offset, numLines, rctHeight, rctLines, h;
  1300.     Handle        hText, keepHText;
  1301.     Rect        keepDestRect, keepViewRect;
  1302.     WindowPtr    tePort, printPort;
  1303.  
  1304.     len = (*teHndl)->teLength;
  1305.     if (!(keepHText = NewHandle(len))) return(memFullErr);
  1306.  
  1307.     if ((offset = *teOffset) >= len) {
  1308.         *teOffset = -1;        /* We are offset further than we have text. */
  1309.         return(noErr);        /* Just say that we have no more text. */
  1310.     }
  1311.  
  1312.     BlockMove(*(hText = (*teHndl)->hText), *keepHText, len);
  1313.     keepDestRect = (*teHndl)->destRect;
  1314.     keepViewRect = (*teHndl)->viewRect;
  1315.     tePort = (*teHndl)->inPort;
  1316.         /* Cache some information from the TextEdit record. */
  1317.  
  1318.     BlockMove(*hText + offset, *hText, len - offset);
  1319.     (*teHndl)->teLength = len - offset;
  1320.         /* Throw out the characters that have already been printed. */
  1321.  
  1322.     GetPort(&printPort);
  1323.     (*teHndl)->inPort = printPort;
  1324.     (*teHndl)->destRect = (*teHndl)->viewRect = *teRct;
  1325.     TECalText(teHndl);
  1326.         /* Install the print rect into the TextEdit record and then
  1327.         ** rewrap the unprinted text into this rectangle.  The text
  1328.         ** is now formatted correctly to print this rect's worth. */
  1329.  
  1330.     numLines  = CTENumTextLines(teHndl);
  1331.     rctHeight = teRct->bottom - teRct->top;
  1332.     rctLines  = rctHeight / (h = (*teHndl)->lineHeight);
  1333.  
  1334.     if (rctLines > numLines) rctLines = numLines;
  1335.     teRct->bottom = teRct->top + rctLines * h;
  1336.     (*teHndl)->destRect = (*teHndl)->viewRect = *teRct;
  1337.         /* We now have the minimum rectangle to hold as much of the text
  1338.         ** as would fit into the rectangle passed in.  The final calc on
  1339.         ** the rect prevents partial lines from being drawn. */
  1340.  
  1341.     TEUpdate(teRct, teHndl);        /* Draw this portion of the text. */
  1342.  
  1343.     if (rctLines == numLines)
  1344.         *teOffset = -1;
  1345.             /* Printed the last of the text for this record. */
  1346.     else
  1347.         *teOffset = (*teHndl)->lineStarts[rctLines] + offset;
  1348.             /* Offset to the first character not printed. */
  1349.  
  1350.     SetHandleSize(hText, len);
  1351.     BlockMove(*keepHText, *hText, len);
  1352.     DisposHandle(keepHText);
  1353.     (*teHndl)->teLength = len;
  1354.     (*teHndl)->inPort   = tePort;
  1355.     (*teHndl)->destRect = keepDestRect;
  1356.     (*teHndl)->viewRect = keepViewRect;
  1357.     TECalText(teHndl);
  1358.         /* Restore the TextEdit record to the way it was. */
  1359. }
  1360.  
  1361.  
  1362.  
  1363. /*****************************************************************************/
  1364.  
  1365.  
  1366.  
  1367. /* Return if the TextEdit control is read/write (true) or read-only (false). */
  1368.  
  1369. #pragma segment Controls
  1370. Boolean    CTEReadOnly(TEHandle teHndl)
  1371. {
  1372.     if (!teHndl) return(false);
  1373.     if ((*teHndl)->caretHook == AsmNoCaret) return(true);
  1374.     return(false);
  1375. }
  1376.  
  1377.  
  1378.  
  1379. /*****************************************************************************/
  1380.  
  1381.  
  1382.  
  1383. /* Return the control handle for the TextEdit control's scrollbar, either
  1384. ** vertical or horizontal.  If the scrollbar doesn't, nil is returned.
  1385. */
  1386.  
  1387. #pragma segment Controls
  1388. ControlHandle    CTEScrollFromTE(TEHandle teHndl, Boolean vertScroll)
  1389. {
  1390.     ControlHandle    viewCtl;
  1391.  
  1392.     viewCtl = CTEViewFromTE(teHndl);
  1393.     if (!viewCtl) return(nil);
  1394.  
  1395.     return(CTEScrollFromView(viewCtl, vertScroll));
  1396. }
  1397.  
  1398.  
  1399.  
  1400. /*****************************************************************************/
  1401.  
  1402.  
  1403.  
  1404. /* Return the control handle for the scrollbar related to the view control,
  1405. ** either horizontal or vertical.  If the scrollbar doesn't exist, return nil.
  1406. */
  1407.  
  1408. #pragma segment Controls
  1409. ControlHandle    CTEScrollFromView(ControlHandle viewCtl, Boolean vertScroll)
  1410. {
  1411.     ControlHandle    ctl;
  1412.     WindowPtr        window;
  1413.     Boolean            vert;
  1414.  
  1415.     window = (*viewCtl)->contrlOwner;
  1416.     ctl    = ((WindowPeek)window)->controlList;
  1417.  
  1418.     for (; ctl;) {
  1419.         if ((ControlHandle)GetCRefCon(ctl) == viewCtl) {
  1420.             vert = false;
  1421.             if ((*ctl)->contrlRect.right == (*ctl)->contrlRect.left + 16)
  1422.                 vert = true;
  1423.             if (vert == vertScroll) return(ctl);
  1424.         }
  1425.         ctl = (*ctl)->nextControl;
  1426.     }
  1427.     return(nil);
  1428. }
  1429.  
  1430.  
  1431.  
  1432. /*****************************************************************************/
  1433.  
  1434.  
  1435.  
  1436. /* Select a range of text.  TESetSelect can't be used alone because it doesn't
  1437. ** update the scrollbars.  This function calls TESetSelect, and then fixes up
  1438. ** the scrollbars.
  1439. */
  1440.  
  1441. #pragma segment Controls
  1442. void    CTESetSelect(short start, short end, TEHandle teHndl)
  1443. {
  1444.     WindowPtr        oldPort;
  1445.     ControlHandle    viewCtl;
  1446.     CTEDataHndl        teData;
  1447.  
  1448.     GetPort(&oldPort);
  1449.     SetPort((*teHndl)->inPort);
  1450.     TESetSelect(start, end, teHndl);
  1451.     AdjustScrollValues(teHndl);
  1452.     if (viewCtl = CTEViewFromTE(teHndl)) {
  1453.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1454.         (*teData)->newUndo = true;
  1455.     }
  1456.     SetPort(oldPort);
  1457. }
  1458.  
  1459.  
  1460.  
  1461. /*****************************************************************************/
  1462.  
  1463.  
  1464.  
  1465. #pragma segment Controls
  1466. void    CTEShow(TEHandle teHndl)
  1467. {
  1468.     ControlHandle    viewCtl, scrollCtl;
  1469.     short            i;
  1470.  
  1471.     if (teHndl == gActiveTEHndl) CTEDeactivate();
  1472.     viewCtl = CTEViewFromTE(teHndl);
  1473.     if (viewCtl) {
  1474.         ShowControl(viewCtl);
  1475.         for (i = 0; i < 2; i++) {
  1476.             scrollCtl = CTEScrollFromView(viewCtl, i);
  1477.             if (scrollCtl) ShowControl(scrollCtl);
  1478.         }
  1479.     }
  1480. }
  1481.  
  1482.  
  1483.  
  1484. /*****************************************************************************/
  1485.  
  1486.  
  1487.  
  1488. /* This function is used to resize a TextEdit control.  Pass it the TextEdit
  1489. ** record to resize, plus the new horizontal and vertical size.  It will
  1490. ** resize the TextEdit control, realign the text, if necessary, plus it will
  1491. ** resize and adjust any scrollbars the TextEdit control may have.  All areas
  1492. ** that need updating are cleared and invalidated.
  1493. */
  1494.  
  1495. #pragma segment Controls
  1496. void    CTESize(TEHandle teHndl, short newH, short newV)
  1497. {
  1498.     WindowPtr        oldPort;
  1499.     Rect            viewRct, rct;
  1500.     short            i, dx, dy, mode;
  1501.     ControlHandle    viewCtl, ctl;
  1502.     CTEDataHndl        teData;
  1503.  
  1504.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  1505.  
  1506.     GetPort(&oldPort);
  1507.     SetPort((*teHndl)->inPort);
  1508.  
  1509.     viewRct = (*viewCtl)->contrlRect;
  1510.     EraseRect(&viewRct);
  1511.  
  1512.     dx = newH - (viewRct.right  - viewRct.left);
  1513.     dy = newV - (viewRct.bottom - viewRct.top);
  1514.  
  1515.     for (i = 0; i < 2; ++i) {
  1516.         if (ctl = CTEScrollFromView(viewCtl, i)) {
  1517.             rct = (*ctl)->contrlRect;
  1518.             if (i) {
  1519.                 SizeControl(ctl, rct.right - rct.left, rct.bottom - rct.top + dy);
  1520.                 MoveControl(ctl, rct.left + dx, rct.top);
  1521.             }
  1522.             else {
  1523.                 SizeControl(ctl, rct.right - rct.left + dx, rct.bottom - rct.top);
  1524.                 MoveControl(ctl, rct.left, rct.top + dy);
  1525.             }
  1526.         }
  1527.     }
  1528.  
  1529.     (*viewCtl)->contrlRect.right  += dx;
  1530.     (*viewCtl)->contrlRect.bottom += dy;
  1531.     (*teHndl)->destRect.right  += dx;
  1532.     (*teHndl)->destRect.bottom += dy;
  1533.     (*teHndl)->viewRect.right  += dx;
  1534.     (*teHndl)->viewRect.bottom += dy;
  1535.  
  1536.     TECalText(teHndl);
  1537.     AdjustTEBottom(teHndl);
  1538.     AdjustScrollValues(teHndl);
  1539.  
  1540.     rct = viewRct = (*viewCtl)->contrlRect;
  1541.     InvalRect(&viewRct);
  1542.  
  1543.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1544.     mode   = (*teData)->mode;
  1545.     rct.top  = rct.bottom - 16;
  1546.     rct.left = rct.right - 16;
  1547.  
  1548.     if (mode & (cteVScrollAndGrow - cteVScroll + cteHScrollAndGrow - cteHScroll))
  1549.         EraseRect(&rct);
  1550.  
  1551.     if (mode & (cteVScrollAndGrow - cteVScroll)) OffsetRect(&rct, 16, 0);
  1552.     if (mode & (cteHScrollAndGrow - cteHScroll)) OffsetRect(&rct, 0, 16);
  1553.     InvalRect(&rct);
  1554.  
  1555.     SetPort(oldPort);
  1556. }
  1557.  
  1558.  
  1559.  
  1560. /*****************************************************************************/
  1561.  
  1562.  
  1563.  
  1564. /* Swap the TextEdit text handle with the text handle passed in. */
  1565.  
  1566. #pragma segment Controls
  1567. Handle    CTESwapText(TEHandle teHndl, Handle newText, Boolean update)
  1568. {
  1569.     WindowPtr    oldPort;
  1570.     Handle        hText;
  1571.     Rect        destRect, viewRect;
  1572.     short        oldTextLen, newTextLen;
  1573.  
  1574.     GetPort(&oldPort);
  1575.     SetPort((*teHndl)->inPort);
  1576.  
  1577.     hText = (*teHndl)->hText;
  1578.  
  1579.     newTextLen = GetHandleSize(newText);
  1580.     oldTextLen = (*teHndl)->teLength;
  1581.     SetHandleSize(hText, oldTextLen);
  1582.         /* Just to make sure TE is behaving itself. */
  1583.  
  1584.     (*teHndl)->hText = newText;
  1585.     (*teHndl)->teLength = newTextLen;
  1586.  
  1587.     TECalText(teHndl);
  1588.  
  1589.     if (update) {
  1590.         destRect = (*teHndl)->destRect;
  1591.         viewRect = (*teHndl)->viewRect;
  1592.  
  1593.         OffsetRect(&destRect,
  1594.             viewRect.left - destRect.left,
  1595.             viewRect.top  - destRect.top);
  1596.  
  1597.         (*teHndl)->destRect = destRect;
  1598.         (*teHndl)->selStart = (*teHndl)->selEnd = 0;
  1599.  
  1600.         EraseRect(&viewRect);
  1601.         TEUpdate(&viewRect, teHndl);
  1602.     }
  1603.  
  1604.     AdjustScrollValues(teHndl);
  1605.  
  1606.     SetPort(oldPort);
  1607.     return(hText);
  1608. }
  1609.  
  1610.  
  1611.  
  1612. /*****************************************************************************/
  1613.  
  1614.  
  1615.  
  1616. /* Return information for the currently active TextEdit control.  The currently
  1617. ** active TextEdit control is stored in gActiveTEHndl, and can be accessed
  1618. ** directly.  If gActiveTEHndl is nil, then there is no currently active one.
  1619. ** The information that we return is the viewRect and window of the active
  1620. ** TextEdit control.  This is information that could be gotten directly, but
  1621. ** this call makes it a little more convenient.
  1622. */
  1623.  
  1624. #pragma segment Controls
  1625. WindowPtr    CTETargetInfo(TEHandle *teHndl, Rect *teView)
  1626. {
  1627.     SetRect(teView, 0, 0, 0, 0);
  1628.     if (!(*teHndl = gActiveTEHndl)) return(nil);
  1629.  
  1630.     *teView = (*gActiveTEHndl)->viewRect;
  1631.     return((*gActiveTEHndl)->inPort);
  1632. }
  1633.  
  1634.  
  1635.  
  1636. /*****************************************************************************/
  1637.  
  1638.  
  1639.  
  1640. #pragma segment Controls
  1641. void    CTEUndo(void)
  1642. {
  1643.     TEHandle        teHndl;
  1644.     ControlHandle    viewCtl;
  1645.     CTEDataHndl        teData;
  1646.     Handle            hText, uText;
  1647.     short            oldStart, oldEnd;
  1648.  
  1649.     if (!(teHndl = gActiveTEHndl)) return;
  1650.  
  1651.     if (viewCtl = CTEViewFromTE(teHndl)) {
  1652.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1653.         hText  = (*teHndl)->hText;
  1654.         uText  = (*teData)->undoText;
  1655.         if (!uText) return;        /* There is no undo yet.  Getting called in
  1656.                                 ** this state is actually an error, but we
  1657.                                 ** check, just in case.
  1658.                                 */
  1659.         oldStart = (*teHndl)->selStart;
  1660.         oldEnd   = (*teHndl)->selEnd;
  1661.         (*teData)->undoText = CTESwapText(teHndl, (*teData)->undoText, false);
  1662.         (*teHndl)->selStart = (*teHndl)->selEnd = 0;
  1663.         CTEUpdate(teHndl, viewCtl);
  1664.         CTESetSelect((*teData)->undoSelStart, (*teData)->undoSelEnd, teHndl);
  1665.             /* CTESetSelect flags it as a new undo situation for us. */
  1666.         (*teData)->undoSelStart = oldStart;
  1667.         (*teData)->undoSelEnd   = oldEnd;
  1668.     }
  1669. }
  1670.  
  1671.  
  1672.  
  1673. /*****************************************************************************/
  1674.  
  1675.  
  1676.  
  1677. /* Draw the TextEdit control and frame. */
  1678.  
  1679. #pragma segment Controls
  1680. void    CTEUpdate(TEHandle teHndl, ControlHandle ctl)
  1681. {
  1682.     WindowPtr    oldPort;
  1683.     Rect        viewRect, brdrRect;
  1684.  
  1685.     if (teHndl) {
  1686.         GetPort(&oldPort);
  1687.         SetPort((*teHndl)->inPort);
  1688.         viewRect = (*teHndl)->viewRect;
  1689.         EraseRect(&viewRect);
  1690.         TEUpdate(&viewRect, teHndl);
  1691.         brdrRect = (*ctl)->contrlRect;
  1692.         FrameRect(&brdrRect);
  1693.         SetPort(oldPort);
  1694.     }
  1695. }
  1696.  
  1697.  
  1698.  
  1699. /*****************************************************************************/
  1700.  
  1701.  
  1702.  
  1703. /* Return the control handle for the view control that owns the TextEdit
  1704. ** record.  Use this to find the view to do customizations such as changing
  1705. ** the update procedure for this TextEdit control.
  1706. */
  1707.  
  1708. #pragma segment Controls
  1709. ControlHandle    CTEViewFromTE(TEHandle teHndl)
  1710. {
  1711.     WindowPtr        window;
  1712.     ControlHandle    viewCtl;
  1713.     TEHandle        te;
  1714.  
  1715.     window = (WindowPtr)(*teHndl)->inPort;
  1716.  
  1717.     for (viewCtl = nil;;) {
  1718.  
  1719.         viewCtl = CTENext(window, &te, viewCtl);
  1720.         if ((!viewCtl) || (te == teHndl)) return(viewCtl);
  1721.     }
  1722. }
  1723.  
  1724.  
  1725.  
  1726. /*****************************************************************************/
  1727.  
  1728.  
  1729.  
  1730. /* Call this when a window with TextEdit controls is being activated.  This
  1731. ** will make the TextEdit control that was last active in this window the
  1732. ** active TextEdit control again.  If it can't find one that was the last
  1733. ** active control, it makes the first one it finds the active control.
  1734. */
  1735.  
  1736. #pragma segment Controls
  1737. void    CTEWindActivate(WindowPtr window, Boolean activate)
  1738. {
  1739.     short            hilite, scrollNum;
  1740.     TEHandle        targetTE;
  1741.     ControlHandle    viewCtl, scrollCtl;
  1742.     TEHandle        te;
  1743.  
  1744.     hilite = 255;
  1745.     if (activate) hilite = 0;
  1746.  
  1747.     for (targetTE = nil, viewCtl = nil;;) {
  1748.  
  1749.         viewCtl = CTENext(window, &te, viewCtl);
  1750.         if (!viewCtl) break;
  1751.             /* The only way out of the loop. */
  1752.  
  1753.         if (GetCtlValue(viewCtl)) targetTE = te;
  1754.  
  1755.         for (scrollNum = 0; scrollNum < 2; ++scrollNum) {
  1756.             scrollCtl = CTEScrollFromTE(te, scrollNum);
  1757.             if (scrollCtl)
  1758.                 HiliteControl(scrollCtl, hilite);
  1759.         }
  1760.     }
  1761.  
  1762.     if (activate) {
  1763.         CTEActivate(targetTE);
  1764.         if (targetTE) return;
  1765.  
  1766.         viewCtl = CTENext(window, &te, nil);
  1767.         if (viewCtl) CTEActivate(te);
  1768.     }
  1769.     else
  1770.         CTEDeactivate();
  1771. }
  1772.  
  1773.  
  1774.  
  1775. /*****************************************************************************/
  1776. /*****************************************************************************/
  1777.  
  1778.  
  1779.  
  1780. #pragma segment Controls
  1781. pascal void    VActionProc(ControlHandle scrollCtl, short part)
  1782. {
  1783.     short        lineHeight, delta, value, teOffset;
  1784.     short        oldValue, max;
  1785.     TEHandle    te;
  1786.     
  1787.     if (part) {                        /* If it was actually in the control. */
  1788.  
  1789.         te = gActiveTEHndl;
  1790.         lineHeight = (*te)->lineHeight;
  1791.  
  1792.         switch (part) {
  1793.             case inUpButton:
  1794.             case inDownButton:        /* One line. */
  1795.                 delta = lineHeight;
  1796.                 break;
  1797.             case inPageUp:            /* One page. */
  1798.             case inPageDown:
  1799.                 delta = (*te)->viewRect.bottom - (*te)->viewRect.top;
  1800.                 if (delta > lineHeight) delta -= lineHeight;
  1801.                 break;
  1802.         }
  1803.         if ( (part == inUpButton) || (part == inPageUp) )
  1804.             delta = -delta;        /* Reverse direction for an upper. */
  1805.  
  1806.         value = (oldValue = GetCtlValue(scrollCtl)) + delta;
  1807.         if (value < 0) value = 0;
  1808.         if (value > (max = GetCtlMax(scrollCtl))) value = max;
  1809.  
  1810.         if (value != oldValue) {
  1811.             SetCtlValue(scrollCtl, value);
  1812.             teOffset = (*te)->viewRect.top - (*te)->destRect.top;
  1813.             if (value -= teOffset) TEScroll(0, -value, te);
  1814.         }
  1815.     }
  1816. }
  1817.  
  1818.  
  1819.  
  1820. /*****************************************************************************/
  1821.  
  1822.  
  1823.  
  1824. #pragma segment Controls
  1825. pascal void    HActionProc(ControlHandle scrollCtl, short part)
  1826. {
  1827.     short        lineHeight, delta, value, teOffset;
  1828.     short        oldValue, max;
  1829.     TEHandle    te;
  1830.     
  1831.     if (part) {                        /* If it was actually in the control. */
  1832.  
  1833.         te = gActiveTEHndl;
  1834.         lineHeight = (*te)->lineHeight;
  1835.  
  1836.         switch (part) {
  1837.             case inUpButton:
  1838.             case inDownButton:        /* One line. */
  1839.                 delta = lineHeight;
  1840.                 break;
  1841.             case inPageUp:            /* One page. */
  1842.             case inPageDown:
  1843.                 delta = (*te)->viewRect.right - (*te)->viewRect.left;
  1844.                 if (delta > lineHeight) delta -= lineHeight;
  1845.                 break;
  1846.         }
  1847.         if ( (part == inUpButton) || (part == inPageUp) )
  1848.             delta = -delta;        /* Reverse direction for an upper. */
  1849.  
  1850.         value = (oldValue = GetCtlValue(scrollCtl)) + delta;
  1851.         if (value < 0) value = 0;
  1852.         if (value > (max = GetCtlMax(scrollCtl))) value = max;
  1853.  
  1854.         if (value != oldValue) {
  1855.             SetCtlValue(scrollCtl, value);
  1856.             teOffset = (*te)->viewRect.left - (*te)->destRect.left;
  1857.             if (value -= teOffset) TEScroll(-value, 0, te);
  1858.         }
  1859.     }
  1860. }
  1861.  
  1862.  
  1863.  
  1864. /*****************************************************************************/
  1865.  
  1866.  
  1867.  
  1868. /* This function is called after an edit to make sure that there is no extra
  1869. ** white space at the bottom of the viewRect.  If there are blank lines at
  1870. ** the bottom of the viewRect, and there is text scrolled off the top of the
  1871. ** viewRect, then the TextEdit control is scrolled to fill this space, or as
  1872. ** much of it as possible.
  1873. */
  1874.  
  1875. #pragma segment Controls
  1876. void    AdjustTEBottom(TEHandle teHndl)
  1877. {
  1878.     Rect    destRect, viewRect;
  1879.     short    botDiff, topDiff;
  1880.  
  1881.     destRect = (*teHndl)->destRect;
  1882.     viewRect = (*teHndl)->viewRect;
  1883.     destRect.bottom = destRect.top + CTEDocHeight(teHndl);
  1884.  
  1885.     botDiff = viewRect.bottom - destRect.bottom;
  1886.     if (botDiff > 0) {
  1887.         topDiff = viewRect.top - destRect.top;
  1888.         if (botDiff > topDiff) botDiff = topDiff;
  1889.         if (botDiff) TEScroll(0, botDiff, teHndl);
  1890.     }
  1891.  
  1892. }
  1893.  
  1894.  
  1895.  
  1896. /*****************************************************************************/
  1897.  
  1898.  
  1899.  
  1900. /* Bring the scrollbar values up to date with the current document position
  1901. ** and length.
  1902. */
  1903.  
  1904. #pragma segment Controls
  1905. void    AdjustScrollValues(TEHandle teHndl)
  1906. {
  1907.     short            scrollNum;
  1908.     ControlHandle    scrollCtl;
  1909.  
  1910.     for (scrollNum = 0; scrollNum < 2; ++scrollNum) {
  1911.         scrollCtl = CTEScrollFromTE(teHndl, scrollNum);
  1912.         if (scrollCtl)
  1913.             AdjustOneScrollValue(teHndl, scrollCtl, scrollNum);
  1914.     }
  1915. }
  1916.  
  1917.  
  1918.  
  1919. /*****************************************************************************/
  1920.  
  1921.  
  1922.  
  1923. /* Bring one scrollbar value up to date with the current document position
  1924. ** and length.
  1925. */
  1926.  
  1927. #pragma segment Controls
  1928. void    AdjustOneScrollValue(TEHandle teHndl, ControlHandle ctl, Boolean vert)
  1929. {
  1930.     Boolean    front;
  1931.     short    textPix, viewPix;
  1932.     short    max, oldMax, value, oldValue;
  1933.  
  1934.     front = ((*ctl)->contrlOwner == FrontWindow());
  1935.  
  1936.     oldValue = GetCtlValue(ctl);
  1937.     oldMax   = GetCtlMax(ctl);
  1938.  
  1939.     if (vert) {
  1940.         textPix = CTEDocHeight(teHndl);
  1941.         viewPix = (*teHndl)->viewRect.bottom - (*teHndl)->viewRect.top;
  1942.     }
  1943.     else {
  1944.         textPix = (*teHndl)->destRect.right - (*teHndl)->destRect.left;
  1945.         viewPix = (*teHndl)->viewRect.right - (*teHndl)->viewRect.left;
  1946.     }
  1947.     max = textPix - viewPix;
  1948.  
  1949.     if (max < 0) max = 0;
  1950.     if (max != oldMax) {
  1951.         if (front) SetCtlMax(ctl, max);
  1952.         else       (*ctl)->contrlMax = max;
  1953.     }
  1954.  
  1955.     if (vert)
  1956.         value = (*teHndl)->viewRect.top  - (*teHndl)->destRect.top;
  1957.     else
  1958.         value = (*teHndl)->viewRect.left - (*teHndl)->destRect.left;
  1959.  
  1960.     if (value < 0)   value = 0;
  1961.     if (value > max) value = max;
  1962.     if (value != oldValue) {
  1963.         if (front) SetCtlValue(ctl, value);
  1964.         else       (*ctl)->contrlValue = value;
  1965.     }
  1966. }
  1967.  
  1968.  
  1969.  
  1970.