home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / comm / news102.sit / NewsWatcher / source / scrollstuff.c < prev    next >
Text File  |  1991-04-03  |  7KB  |  269 lines

  1. /*----------------------------------------------------------
  2. #
  3. #    NewsWatcher    - Macintosh NNTP Client Application
  4. #
  5. #    Written by Steven Falkenburg
  6. #    ⌐1990 Apple Computer, Inc.
  7. #
  8. #-----------------------------------------------------------
  9. #
  10. #    scrollstuff.c
  11. #
  12. #    This code module supports scrolling for textedit fields
  13. #    of text located in resizeable windows.
  14. #
  15. #-----------------------------------------------------------*/
  16.  
  17. #pragma segment userint
  18.  
  19. #include "compat.h"
  20. #include <stdlib.h>
  21.  
  22. #ifdef PROTOS
  23.  
  24. #include <Types.h>
  25. #include <Windows.h>
  26. #include <TextEdit.h>
  27. #include <Controls.h>
  28. #include <OSUtils.h>
  29. #include <Events.h>
  30. #include <StdLib.h>
  31. #include <Lists.h>
  32. #endif
  33.  
  34. #include "nntp.h"
  35. #include "ScrollStuff.h"
  36.  
  37.  
  38. ControlHandle hScrollCont(WindowPtr window)
  39. {
  40.     if (GetCRefCon( ((WindowPeek) window)->controlList ) == kVRef )
  41.         return((**(((WindowPeek) window)->controlList)).nextControl);
  42.     else
  43.         return(((WindowPeek) window)->controlList);
  44. }
  45.  
  46.  
  47. ControlHandle vScrollCont(WindowPtr window)
  48. {
  49.     if (GetCRefCon( ((WindowPeek) window)->controlList ) == kHRef )
  50.         return((**(((WindowPeek) window)->controlList)).nextControl);
  51.     else
  52.         return(((WindowPeek) window)->controlList);
  53. }
  54.  
  55.  
  56. long sign(long longVar)
  57. {
  58.     if (longVar>=0)
  59.         return(1);
  60.     else
  61.         return(-1);
  62. }
  63.  
  64.  
  65. short LinesInText(TEHandle theTE)
  66. {
  67.     short lines;
  68.     Handle textHandle;
  69.     
  70.     lines = (**theTE).nLines;
  71.     textHandle = (**theTE).hText;
  72.     if ( (**theTE).teLength > 0 ) {
  73.         if ( *(*textHandle+((**theTE).teLength - 1)) == CR )
  74.             lines++;
  75.         return(lines);
  76.     }
  77. }
  78.  
  79.  
  80. void MoveText(WindowPtr window,ControlHandle theControl)
  81. {
  82.     long    viewTop,destTop;
  83.     long    scrollValue;
  84.     long    scrollDiff,oldScroll,newScroll;
  85.     short    height;
  86.     TEHandle theTE;
  87.     
  88.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);    
  89.  
  90.     if (theControl == vScrollCont(window)) {
  91.         viewTop = (**theTE).viewRect.top;
  92.         destTop = (**theTE).destRect.top;
  93.         oldScroll = viewTop - destTop;
  94.         scrollValue = GetCtlValue(theControl);
  95.         height = (**theTE).lineHeight;
  96.         newScroll = scrollValue * height;
  97.         scrollDiff = oldScroll - newScroll;
  98.         if (abs(scrollDiff)>32000) {
  99.             TEScroll(0L,sign(scrollDiff) * 32000,theTE);
  100.             SysBeep(30);
  101.         }
  102.         else if (scrollDiff != 0)
  103.             TEScroll(0L,scrollDiff,theTE);
  104.     }
  105.     else {
  106.         viewTop = (**theTE).viewRect.left;
  107.         destTop = (**theTE).destRect.left;
  108.         oldScroll = viewTop - destTop;
  109.         scrollValue = GetCtlValue(theControl);
  110.         newScroll = scrollValue * kTextWidth;
  111.         scrollDiff = oldScroll - newScroll;
  112.         if (abs(scrollDiff)>32000) {
  113.             TEScroll(sign(scrollDiff) * 32000,0L,theTE);
  114.             SysBeep(30);
  115.         }
  116.         else if (scrollDiff != 0)
  117.             TEScroll(scrollDiff,0L,theTE);
  118.     }
  119. }
  120.  
  121.  
  122. void AdjustScrollBar(WindowPtr window)
  123. {
  124.     short windowLines,currentLines;
  125.     TEHandle theTE;
  126.     
  127.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);    
  128.  
  129.     windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
  130.     if ((currentLines = LinesInText(theTE)) >= windowLines)
  131.         SetCtlMax(vScrollCont(window),currentLines - windowLines);
  132.     else
  133.         SetCtlMax(vScrollCont(window),0L);
  134.     
  135.     windowLines = ((**theTE).viewRect.right - (**theTE).viewRect.left) / kTextWidth;
  136.     if (kMaxColumns >= windowLines)
  137.         SetCtlMax(hScrollCont(window),kMaxColumns);
  138.     else
  139.         SetCtlMax(hScrollCont(window),0L);
  140. }
  141.  
  142.  
  143. void ScrollChar(WindowPtr window,short charPos,Boolean toBottom)
  144. {
  145.     short theLine,windowLines;
  146.     TEHandle theTE;
  147.     
  148.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);    
  149.     
  150.     theLine = 0;
  151.     while ( (**theTE).lineStarts[theLine + 1] <= charPos )
  152.         theLine++;
  153.     if (toBottom) {
  154.         windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
  155.         theLine = theLine - (windowLines - 1);
  156.     }
  157.     SetCtlValue(vScrollCont(window),theLine);
  158.     MoveText(window,hScrollCont(window));
  159.     MoveText(window,vScrollCont(window));
  160. }
  161.  
  162.  
  163. void CheckInsertion(WindowPtr window)
  164. {
  165.     short topLine,bottomLine,windowLines;
  166.     TEHandle theTE;
  167.     
  168.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(window))->data);    
  169.     
  170.     windowLines = ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight;
  171.     topLine = GetCtlValue(vScrollCont(window));
  172.     bottomLine = topLine + windowLines;
  173.     if (GetCtlMax(vScrollCont(window)) == 0)
  174.         MoveText(window,vScrollCont(window));
  175.     else if ((**theTE).selEnd < (**theTE).lineStarts[topLine])
  176.         ScrollChar(window,(**theTE).selStart,false);
  177.     else if ((**theTE).selStart >= (**theTE).lineStarts[bottomLine])
  178.         ScrollChar(window,(**theTE).selEnd,true);
  179. }
  180.  
  181.  
  182. pascal void scroll_action(ControlHandle scrollBar,short part)
  183. {
  184.     short    scrollAmt = 0,pageSize;
  185.     short    theCtlValue;
  186.     TEHandle theTE;
  187.     
  188.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(FrontWindow()))->data);    
  189.     
  190.     pageSize =  ((**theTE).viewRect.bottom - (**theTE).viewRect.top) / (**theTE).lineHeight - 1;
  191.     switch (part) {
  192.         case inUpButton:
  193.             scrollAmt = -1;
  194.             break;
  195.         case inDownButton:
  196.             scrollAmt = 1;
  197.             break;
  198.         case inPageUp:
  199.             scrollAmt = -pageSize;
  200.             break;
  201.         case inPageDown:
  202.             scrollAmt = pageSize;
  203.             break;
  204.     }
  205.     theCtlValue = GetCtlValue(scrollBar)+scrollAmt;
  206.     if (part == inPageDown || part == inPageUp || theCtlValue <= GetCtlMax(scrollBar) && theCtlValue >= GetCtlMin(scrollBar)) {
  207.         SetCtlValue(scrollBar,GetCtlValue(scrollBar)+scrollAmt);
  208.         MoveText(FrontWindow(),scrollBar);
  209.     }
  210. }
  211.  
  212.  
  213. void DoScrollers(ControlHandle scrollBar,short part,Point localMouse)
  214. {
  215.     if (part == inThumb) {
  216.         TrackControl(scrollBar,localMouse,nil);
  217.         MoveText(FrontWindow(),scrollBar);
  218.     }
  219.     else
  220.         TrackControl(scrollBar,localMouse,(ProcPtr) scroll_action);
  221. }    
  222.  
  223.  
  224. pascal Boolean AutoScroll(void)
  225. {
  226.     RgnHandle    oldClip;
  227.     Point        mouseLoc;
  228.     Rect        textRect,tempRect;
  229.     short        deltaX = 0,deltaY = 0;
  230.     ControlHandle sBar;
  231.     TEHandle theTE;
  232.     
  233.     theTE = (TEHandle)(((TwindowInfo *)GetWRefCon(FrontWindow()))->data);    
  234.     
  235.     oldClip = NewRgn();
  236.     GetClip(oldClip);
  237.     tempRect = FrontWindow()->portRect;
  238.     ClipRect(&tempRect);
  239.     GetMouse(&mouseLoc);
  240.     textRect = (**theTE).viewRect;
  241.     if (mouseLoc.v < textRect.top)
  242.         deltaY = -1;
  243.     else if (mouseLoc.h < textRect.left)
  244.         deltaX = -1;
  245.     else if (mouseLoc.v > textRect.bottom)
  246.         deltaY = 1;
  247.     else if (mouseLoc.h > textRect.right)
  248.         deltaX = 1;
  249.     if (deltaY) {
  250.         sBar = vScrollCont(FrontWindow());
  251.         if (GetCtlValue(sBar)+deltaY <= GetCtlMax(sBar) &&
  252.             GetCtlValue(sBar)+deltaY >= GetCtlMin(sBar)) {
  253.             SetCtlValue(sBar,GetCtlValue(sBar)+deltaY);
  254.             MoveText(FrontWindow(),sBar);
  255.         }
  256.     }
  257.     else if (deltaX) {
  258.         sBar = hScrollCont(FrontWindow());
  259.         if (GetCtlValue(sBar)+deltaX <= GetCtlMax(sBar) &&
  260.             GetCtlValue(sBar)+deltaX >= GetCtlMin(sBar)) {
  261.             SetCtlValue(sBar,GetCtlValue(sBar)+deltaX);
  262.             MoveText(FrontWindow(),sBar);
  263.         }
  264.     }    
  265.     SetClip(oldClip);
  266.     DisposeRgn(oldClip);
  267.     return true;
  268. }
  269.