home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / scrolls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-22  |  6.8 KB  |  307 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        scrolls.c
  4. #
  5. #        This segment handles the scroll bars.
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32.  
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37.  
  38. //----------------------------------------------------------------------
  39. //
  40. //    Globals - 
  41. //
  42. //----------------------------------------------------------------------
  43.  
  44. extern Boolean        gInBackground;
  45.  
  46.  
  47. //----------------------------------------------------------------------
  48. //
  49. //    InstallScrollBars -
  50. //                 
  51. //
  52. //----------------------------------------------------------------------
  53.  
  54. void InstallScrollBars(WindowRef window, DocHnd doc)
  55. {
  56.     Rect        bounds;
  57.     
  58.     
  59.     // calc horiz scroll bar
  60.     bounds = window->portRect;
  61.     
  62.     bounds.right++;
  63.     bounds.left = bounds.right - (kScrollWidth + 1);
  64.     bounds.top--;
  65.     bounds.bottom -= (kScrollWidth - 1);
  66.     
  67.     (**doc).hScroll = NewControl(window, &bounds, nil, true,
  68.                                   0, 0, 0, scrollBarProc, nil);
  69.                                   
  70.     // calc vert scroll bar
  71.     bounds = window->portRect;
  72.  
  73.     bounds.left--;
  74.     bounds.right -= (kScrollWidth - 1);
  75.     bounds.top = bounds.bottom - kScrollWidth;
  76.     bounds.bottom++;
  77.     
  78.  
  79.     (**doc).vScroll = NewControl(window, &bounds, nil, true,
  80.                                   0, 0, 0, scrollBarProc, nil);
  81.  
  82.                                   
  83. }
  84.  
  85.  
  86. //----------------------------------------------------------------------
  87. //
  88. //    AdjustScrollValues -
  89. //                 
  90. //
  91. //----------------------------------------------------------------------
  92.  
  93. void AdjustScrollValues(WindowRef window)
  94. {
  95.     Rect                    contRect;
  96.     Rect                    picFrame;
  97.     ControlHandle            hCntrl;
  98.     ControlHandle            vCntrl;
  99.     short                    oldValue;
  100.     short                    lines;
  101.     short                    max;
  102.     DocHnd                    doc;
  103.     
  104.     doc = (DocHnd)GetWRefCon(window);
  105.     
  106.     if (doc != nil) {
  107.         hCntrl = (**doc).hScroll;
  108.         vCntrl = (**doc).vScroll;
  109.  
  110.         GetContRect(window,&contRect);
  111.         
  112.         if ((**doc).pict != nil || (**doc).world != nil)
  113.             picFrame = (**doc).world->portRect;
  114.         else
  115.             SetRect(&picFrame, 0, 0, 0, 0);
  116.             
  117.         oldValue = GetCtlValue(vCntrl);
  118.         lines = picFrame.bottom - picFrame.top;
  119.         max = lines - contRect.bottom ;
  120.         if ( max < 0 ) max = 0;
  121.         SetCtlMax(vCntrl, max);
  122.     
  123.         if(oldValue + contRect.bottom > lines)
  124.             SetCtlValue(vCntrl, max);
  125.         
  126.         oldValue = GetCtlValue(hCntrl);
  127.         lines = picFrame.right - picFrame.left;
  128.         max = lines - contRect.right;
  129.         if ( max < 0 ) max = 0;
  130.         SetCtlMax(hCntrl, max);
  131.     
  132.         if(oldValue + contRect.right > lines)
  133.             SetCtlValue(hCntrl, max);
  134.                 
  135.     }        
  136.             
  137. }
  138.  
  139.  
  140. //----------------------------------------------------------------------
  141. //
  142. //    AdjustScrollbars -
  143. //                 
  144. //
  145. //----------------------------------------------------------------------
  146.  
  147. void AdjustScrollbars(WindowRef window, Boolean resize)
  148. {
  149.     ControlRef        hCtl;
  150.     ControlRef        vCtl;
  151.     Rect            r;
  152.     DocHnd             doc;
  153.  
  154.     
  155.     doc = (DocHnd)GetWRefCon(window);
  156.     
  157.     if (doc != nil && resize) {
  158.         hCtl = (**doc).hScroll;
  159.         vCtl = (**doc).vScroll;
  160.         GetContRect(window, &r);
  161.         
  162.         // resize horizontal
  163.         MoveControl(hCtl, -1, r.bottom);
  164.         SizeControl(hCtl, (r.right - r.left) + 2, kScrollWidth + 1);
  165.         
  166.         // resize vertical
  167.         MoveControl(vCtl, r.right, -1);
  168.         SizeControl(vCtl, kScrollWidth + 1, r.bottom + 2);
  169.     }
  170.     
  171.     // adjust scrollbar values to match
  172.     AdjustScrollValues(window);
  173. }
  174.  
  175.  
  176. //----------------------------------------------------------------------
  177. //
  178. //    ScrollActionProc -
  179. //                 
  180. //
  181. //----------------------------------------------------------------------
  182.  
  183. pascal void ScrollActionProc(ControlRef control, short part)
  184. {
  185.     WindowRef        window;
  186.     short            amount;
  187.     short            value;
  188.     short            max;
  189.     short            hAmount;
  190.     short            vAmount;
  191.     DocHnd            doc;
  192.     
  193.     window = (**control).contrlOwner;
  194.     doc = (DocHnd)GetWRefCon(window);
  195.     
  196.     if (doc != nil) {
  197.         if ( part != 0 ) {                
  198.             window = (*control)->contrlOwner;
  199.             hAmount = vAmount = 0;
  200.             
  201.             value = GetCtlValue(control);    
  202.             max = GetCtlMax(control);        
  203.         
  204.             switch ( part ) {
  205.                 case inUpButton:
  206.                 case inDownButton:        // one line
  207.                         amount = 8;
  208.                         break;
  209.                         
  210.                 case inPageUp:            // one page
  211.                 case inPageDown:
  212.                         amount = 64;
  213.                         break;
  214.             }
  215.             
  216.             if ( (part == inDownButton) || (part == inPageDown) )
  217.                 amount = -amount;        // reverse direction for a downer
  218.             
  219.             amount = value - amount;
  220.             if ( amount < 0 )
  221.                 amount = 0;
  222.             else if ( amount > max )
  223.                 amount = max; 
  224.             SetCtlValue(control, amount);
  225.             
  226.             amount = value - amount;        // calculate the real change
  227.             
  228.             if ( amount != 0 ) {
  229.                 if (control == (**doc).hScroll)
  230.                     hAmount = amount;
  231.                 else
  232.                     vAmount = amount ;
  233.                         
  234.                 MyScrollPicture(window,hAmount, vAmount);
  235.             }
  236.         }    
  237.     }    
  238. }
  239.     
  240.  
  241. //----------------------------------------------------------------------
  242. //
  243. //    MyScrollPicture -
  244. //                 
  245. //
  246. //----------------------------------------------------------------------
  247.  
  248. void MyScrollPicture(WindowRef window, short hs, short vs)
  249. {
  250.     RgnHandle        updateRgn;
  251.     GWorldPtr        oldWorld;
  252.     GWorldPtr        theWorld;
  253.     GDHandle        oldGD;
  254.     PixMapHandle    thePix;
  255.     Rect            contRect;
  256.     Rect            pictRect;
  257.     DocHnd            doc;
  258.     
  259.     doc = (DocHnd)GetWRefCon(window);
  260.     
  261.     if (doc != nil) {
  262.         GetGWorld(&oldWorld, &oldGD);
  263.         SetPort(window);
  264.         
  265.         GetContRect(window, &contRect);
  266.         ScrollRect(&contRect, hs, vs, updateRgn = NewRgn());
  267.         
  268.         theWorld = (**doc).world;
  269.         pictRect = theWorld->portRect;
  270.         
  271.         thePix = GetGWorldPixMap(theWorld);
  272.         if (LockPixels(thePix)) {
  273.             SetGWorld((CGrafPtr)window, nil);
  274.             OffsetRect(&pictRect, -GetCtlValue((**doc).hScroll), 
  275.                       -GetCtlValue((**doc).vScroll));
  276.  
  277.             CopyBits((BitMap *) *thePix, &window->portBits,
  278.                     &theWorld->portRect, &pictRect, srcCopy,updateRgn);
  279.             
  280.             UnlockPixels(thePix);
  281.             
  282.             SetGWorld(oldWorld, oldGD);
  283.         }            
  284.         ClipRect(&window->portRect);
  285.         DisposeRgn(updateRgn);
  286.     }
  287.         
  288. }    
  289.  
  290.  
  291. //----------------------------------------------------------------------
  292. //
  293. //    GetContRect -
  294. //                 
  295. //
  296. //----------------------------------------------------------------------
  297.  
  298. void GetContRect(WindowRef window,Rect *contRect)
  299. {
  300.     *contRect = window->portRect;    // get the content rect for our window
  301.     
  302.     contRect->right -= 15;            // subtract the scroll bars from the rect
  303.     contRect->bottom -= 15;
  304.  
  305.  
  306. }
  307.