home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Disk / puriTEXT 1.1 / source / scrollTE.c < prev   
Encoding:
Text File  |  1994-03-20  |  5.0 KB  |  203 lines  |  [TEXT/KAHL]

  1. //#define DEBUG 1
  2. // scrolling StyledTEXT procedures for 'puriTEXT'
  3. // Copyright (C) 1994 by Mizutori Tetsuya, 94/3/9, 94/3/21
  4.  
  5. #include "Defines.h"
  6.  
  7. // Prototypes (scrollTE.c)
  8. /***** scroll TEXT *****/
  9. void SetupText( WindowPtr window, short textID, Rect *rect, TEHandle *teHandle );
  10. short SetupLinePos( WindowPtr window, TEHandle teHndl, Handle *posHndl );
  11. void TEScrollProc( WindowPtr window, Point thePoint );
  12. pascal void myScrollProc( ControlHandle theControl, short thePart );
  13. void SetControl( ControlHandle theControl, short *shift );
  14.  
  15. // !!! CAUTION !!!
  16. // Some procedure depends on the 'myWindowRecord'.
  17. // The fields myWindowRecord.{userCntl,userTE,posHndl} are required.
  18. // !!! CAUTION !!!
  19.  
  20. /***** setup styled text *****/
  21. void SetupText( WindowPtr window, short textID, Rect *rect, TEHandle *teHandle )
  22. {
  23.     TEHandle        teHndl;
  24.     StScrpHandle    stHndl;
  25.     Rect            destRect, viewRect;
  26.     Handle            textHandle, stylHandle;
  27.     FontInfo        fontInfo;
  28.     Boolean            textStyled;
  29.     GrafPtr            oldPort;
  30.  
  31.     GetPort( &oldPort );
  32.     SetPort( (GrafPtr) window );
  33.  
  34.     textHandle = GetResource('TEXT',textID);
  35.     stylHandle = GetResource('styl',textID);
  36.     HLock(textHandle);
  37.     HLock(stylHandle);
  38.  
  39.     if ( stylHandle != nil ) textStyled = true; else textStyled = false;
  40.  
  41. //    textStyled = false;
  42.  
  43.     destRect = *rect;
  44.     viewRect = destRect;
  45.  
  46.     if ( textStyled ) {
  47.         teHndl = TEStylNew( &destRect, &viewRect );
  48.         stHndl = (StScrpHandle) stylHandle;
  49.         TEStylInsert( *textHandle, SizeResource(textHandle), stHndl, teHndl );
  50.     } else {
  51.         teHndl = TENew( &destRect, &viewRect );
  52.         TextFont(geneva);
  53.         TextSize(9);
  54.         GetFontInfo(&fontInfo);
  55.         (**teHndl).txFont = geneva;
  56.         (**teHndl).txSize = 9;
  57.         (**teHndl).fontAscent = fontInfo.ascent;
  58.         (**teHndl).lineHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  59.         TESetText( *textHandle, SizeResource(textHandle), teHndl );
  60.     }
  61.  
  62.     HUnlock(textHandle);
  63.     ReleaseResource(textHandle);
  64.     if ( textStyled ) {
  65.         HUnlock(stylHandle);
  66.         ReleaseResource(stylHandle);
  67.     }
  68.  
  69.     SetPort( oldPort );
  70.  
  71.     *teHandle = teHndl;
  72. }
  73.  
  74. short SetupLinePos( WindowPtr window, TEHandle teHndl, Handle *posHndl )
  75. {
  76.     short    **pHndl;
  77.     short    *p;
  78.     short    i,k,textHeight,viewHeight;
  79.  
  80.     textHeight = TEGetHeight( (long)(**teHndl).nLines, 1L, teHndl );
  81.     viewHeight = (**teHndl).viewRect.bottom - (**teHndl).viewRect.top;
  82.  
  83.     pHndl = (short **) NewHandle( ((**teHndl).nLines+1) * sizeof( short ) );
  84.  
  85.     HLock( (Handle) pHndl );
  86.     p = *pHndl;
  87.  
  88.     p[0] = 0;
  89.     if ( textHeight <= viewHeight ) i = 0;
  90.     else
  91.     for (i=1;i<(**teHndl).nLines+1;++i) {
  92.         k = TEGetHeight( (long)i, 1L, teHndl );
  93.         p[i] = k;
  94.         if ( k >= textHeight - viewHeight ) break;
  95.     }
  96.  
  97.     HUnlock( (Handle) pHndl );
  98.  
  99.     *posHndl = (Handle) pHndl;
  100.  
  101.     return( i );
  102. }
  103.  
  104. /***** Do Scroll *****/
  105. void TEScrollProc( WindowPtr window, Point thePoint )
  106. {
  107.     short            thePart;
  108.     ControlHandle    theControl;
  109.     TEHandle        teHndl;
  110.     short            curCtlValue, newCtlValue;
  111.     short            shift;
  112.  
  113. //    GetMouse( &thePoint );
  114. //    /* GlobalToLocal( &thePoint );    */
  115.  
  116.     thePart = FindControl( thePoint, window, &theControl );
  117.     if ( theControl != my(window,userCntl) ) return;    // window's userCntl
  118.  
  119. //    window = (**theControl).contrlOwner;
  120.     teHndl = my(window,userTE);                            // window's userTE
  121.  
  122.     switch ( thePart ) {
  123.         case inUpButton:
  124.         case inDownButton:
  125.         case inPageUp:
  126.         case inPageDown:        
  127.             thePart = TrackControl( theControl, thePoint, &myScrollProc );
  128.             break;
  129.         case inThumb:
  130.             curCtlValue = GetCtlValue( theControl );
  131.             thePart = TrackControl( theControl, thePoint, kNilActionProc );
  132.             if ( thePart != 0 ) {
  133.                 newCtlValue = GetCtlValue( theControl );
  134.                 shift = ShiftValue( window, curCtlValue, newCtlValue );
  135.                 TEPinScroll( kShiftHorizontal, -shift, teHndl );
  136. //                InvalRect( &(((GrafPtr)dialog)->portRect) );
  137.             }
  138.             break;
  139.     }
  140. }
  141.  
  142.  
  143. pascal void myScrollProc( ControlHandle theControl, short thePart )
  144. {
  145.     short        shift;
  146.     WindowPtr    window;
  147.     TEHandle    teHndl;
  148.  
  149.     window = (**theControl).contrlOwner;
  150.     teHndl = my(window,userTE);                    // window's userTE
  151.  
  152.     shift = kScrollStep1;
  153.  
  154.     switch( thePart ) {
  155.         case inPageDown:
  156.             shift = kScrollStep2;
  157.         case inDownButton:
  158.             SetControl( theControl, &shift );
  159.             if ( shift != 0 ) {
  160.                 TEPinScroll( kShiftHorizontal, -shift, teHndl );
  161. //                UpdateWindow( window );
  162.             }
  163.             break;
  164.         case inPageUp:
  165.             shift = kScrollStep2;
  166.         case inUpButton:
  167.             shift = -shift;
  168.             SetControl( theControl, &shift );
  169.             if ( shift != 0 ) {
  170.                 TEPinScroll( kShiftHorizontal, -shift, teHndl );
  171. //                UpdateWindow( window );
  172.             }
  173.             break;
  174.     }
  175. }
  176.  
  177.  
  178. void SetControl( ControlHandle theControl, short *shift )
  179. {
  180.     WindowPtr    window;
  181.     short        curCtlValue, maxCtlValue, minCtlValue;
  182.     short        newCtlValue;
  183.  
  184.     window = (**theControl).contrlOwner;
  185.  
  186.     maxCtlValue = GetCtlMax( theControl );
  187.     curCtlValue = GetCtlValue( theControl );
  188.     minCtlValue = GetCtlMin( theControl );
  189.  
  190.     newCtlValue = curCtlValue + *shift;
  191.  
  192.     if ( newCtlValue < minCtlValue )  newCtlValue = minCtlValue;
  193.     if ( newCtlValue > maxCtlValue )  newCtlValue = maxCtlValue;
  194.  
  195.     *shift = newCtlValue - curCtlValue;
  196.  
  197.     if ( *shift != 0 ) {
  198.         *shift = ShiftValue( window, curCtlValue, newCtlValue );    // window's posHndl
  199.         SetCtlValue( theControl, newCtlValue ); }
  200. }
  201.  
  202. /***** end of program *****/
  203.