home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 01b / macsl / mpmscrol.c < prev    next >
C/C++ Source or Header  |  1988-10-04  |  13KB  |  427 lines

  1. /*-----------------------------------------------------------------*/
  2. /* MpmScroll.c                                                     */
  3. /* Window scrolling functions                                      */
  4. /*-----------------------------------------------------------------*/
  5.  
  6. #include "MacPM.h"
  7.  
  8. /*-----------------------------------------------------------------*/
  9.  
  10. #define P pascal
  11.  
  12. P LOCAL VOID    MpmTrackScrollBar( ControlHandle hctl, SHORT sPart );
  13.   LOCAL VOID    MpmTrackScrollBarNotify( SHORT cmd );
  14. P LOCAL VOID    MpmTrackThumb( VOID );
  15.   LOCAL SHORT   MpmTrackThumbCalc( Point point );
  16.   LOCAL VOID    MpmTrackThumbHideShow( VOID );
  17.   LOCAL VOID    MpmTrackThumbNotify( SHORT cmd, SHORT sValue );
  18.  
  19. #undef P
  20.  
  21. /*-----------------------------------------------------------------*/
  22.  
  23. HWND        _hwndTrack    = NULL;
  24. ControlHandle _hctlTrack;
  25.  
  26. #if 0  /* part of failed attempt at fancy thumb tracking */
  27.  
  28. RgnHandle   _hrgnTrack;
  29. Point       _pointTrack1;
  30. SHORT       _sTrackValue, _sTrackValue1;
  31. struct {
  32.     Rect      rectLimit;
  33.     Rect      rectSlop;
  34.     short     sAxis;
  35. } _thumbTrack;
  36.  
  37. #endif
  38.  
  39. /*-----------------------------------------------------------------*/
  40. /* Scroll a window.  The three prcl parameters must all be NULL.   */
  41. /* dx and dy give the scroll increments, fOptions tells whether to */
  42. /* scroll child windows or not (SW_SCROLLCHILDREN flag).           */
  43. /*-----------------------------------------------------------------*/
  44.  
  45. SHORT APIENTRY WinScrollWindow( hwnd, dx, dy, prclScroll, prclClip,
  46.                                   hrgnUpdate, prclUpdate, fOptions )
  47.     HWND        hwnd;
  48.     SHORT       dx, dy;
  49.     PRECTL      prclScroll, prclClip, prclUpdate;
  50.     HRGN        hrgnUpdate;
  51.     USHORT      fOptions;
  52. {
  53.     Rect        rect;
  54.     RgnHandle   hrgn;
  55.     HWND        hwndChild;
  56.  
  57.     if( ! MpmValidateWindow(hwnd) )
  58.       return FALSE;
  59.  
  60.     ASSERT( ! prclScroll && ! prclClip && ! hrgnUpdate && ! prclUpdate,
  61.             "WinScrollWindow: hrgn and prcl params must be NULL" );
  62.  
  63.     /* Adjust child window coordinates if requested */
  64.  
  65.     if( fOptions & SW_SCROLLCHILDREN )
  66.       for( hwndChild = MYWNDOF(hwnd).hwndTopChild;
  67.            hwndChild;
  68.            hwndChild = MYWNDOF(hwndChild).hwndNextSibling )
  69.       {
  70.         MYWNDOF(hwndChild).x += dx;
  71.         MYWNDOF(hwndChild).y += dy;
  72.       }
  73.  
  74.     /* Scroll the window, Mac-style */
  75.  
  76.     MpmQueryMacRect( hwnd, &rect );
  77.  
  78.     hrgn = NewRgn();
  79.     thePort = &PWINOFHWND(hwnd)->port;
  80.     ScrollRect( &rect, dx, -dy, hrgn );
  81.     InvalRgn( hrgn );
  82.     DisposeRgn( hrgn );
  83.  
  84.     return 2;  /* not really right, but no one cares */
  85. }
  86.  
  87. /*-----------------------------------------------------------------*/
  88. /* Redisplay all scroll bar controls within hwnd (and hwnd itself, */
  89. /* if it's a scroll bar), with the active or inactive appearance   */
  90. /* according to fActive.                                           */
  91. /*-----------------------------------------------------------------*/
  92.  
  93. LOCAL VOID MpmActivateScrollBars( hwnd, fActive )
  94.     HWND        hwnd;
  95.     BOOL        fActive;
  96. {
  97.     if( strcmp( MYCLASSOF(hwnd).szName, "#8" ) == 0 )
  98.       HiliteControl( MYWNDOF(hwnd).hctl, fActive ? 0 : 255 );
  99.  
  100.     for( hwnd = MYWNDOF(hwnd).hwndTopChild;
  101.          hwnd;
  102.          hwnd = MYWNDOF(hwnd).hwndNextSibling )
  103.       MpmActivateScrollBars( hwnd, fActive );
  104. }
  105.  
  106. /*-----------------------------------------------------------------*/
  107. /* Mac call-back function for scroll bar tracking.  Sends the      */
  108. /* appropriate WM_HSCROLL or WM_VSCROLL message with the SB_ code  */
  109. /* for the particular action in progress.                          */
  110. /*-----------------------------------------------------------------*/
  111.  
  112. pascal VOID MpmTrackScrollBar( hctl, sPart )
  113.     ControlHandle hctl;
  114.     SHORT       sPart;
  115. {
  116.     SHORT       cmd;
  117.  
  118.     ASSERT( (HWND)(**hctl).contrlRfCon == _hwndTrack,
  119.             "MpmTrackScrollBar: bad hctl or _hwndTrack" );
  120.  
  121.     switch( sPart )
  122.     {
  123.       case inUpButton:    cmd = SB_LINEUP;    break;
  124.       case inDownButton:  cmd = SB_LINEDOWN;  break;
  125.       case inPageUp:      cmd = SB_PAGEUP;    break;
  126.       case inPageDown:    cmd = SB_PAGEDOWN;  break;
  127.       default:            return;
  128.     }
  129.     MpmTrackScrollBarNotify( cmd );
  130. }
  131.  
  132. /*-----------------------------------------------------------------*/
  133. /* Send the actual WM_?SCROLL message for MpmTrackScrollBar.       */
  134. /*-----------------------------------------------------------------*/
  135.  
  136. LOCAL VOID MpmTrackScrollBarNotify( cmd )
  137.     SHORT       cmd;
  138. {
  139.     PMYWND      pwnd;
  140.  
  141.     pwnd = PMYWNDOF(_hwndTrack);
  142.  
  143.     if( pwnd->hwndOwner )
  144.       WinSendMsg(
  145.         pwnd->hwndOwner,
  146.         pwnd->flStyle & SBS_VERT ? WM_VSCROLL : WM_HSCROLL,
  147.         MPFROMSHORT(pwnd->id),
  148.         MPFROM2SHORT( (**pwnd->hctl).contrlValue, cmd )
  149.       );
  150. }
  151.  
  152. /*-----------------------------------------------------------------*/
  153. /* Part of failed attempt at scroll bar thumb tracking.            */
  154. /*-----------------------------------------------------------------*/
  155.  
  156. #if 0
  157.  
  158. LOCAL pascal VOID MpmTrackThumb()
  159. {
  160.     Point       point;
  161.     SHORT       sValue;
  162.  
  163.     GetMouse( &point );
  164.     sValue = MpmTrackThumbCalc( point );
  165.     if( sValue == _sTrackValue )
  166.       return;
  167.  
  168.     _sTrackValue = sValue;
  169.  
  170.     MpmTrackThumbHideShow();
  171.  
  172.     MpmTrackThumbNotify( SB_SLIDERTRACK, sValue );
  173.  
  174.     MpmTrackThumbHideShow();
  175. }
  176.  
  177. #endif
  178.  
  179. /*-----------------------------------------------------------------*/
  180. /* Part of failed attempt at scroll bar thumb tracking.            */
  181. /*-----------------------------------------------------------------*/
  182.  
  183. #if 0
  184.  
  185. LOCAL SHORT MpmTrackThumbCalc( point )
  186.     Point       point;
  187. {
  188.     SHORT       sValueMin, sValueRange, sPix, sPixMax;
  189.  
  190.     if( ! PtInRect( point, &_thumbTrack.rectSlop ) )
  191.       return _sTrackValue1;
  192.  
  193.     if( _thumbTrack.sAxis & hAxisOnly )
  194.     {
  195.       sPix = point.h - _thumbTrack.rectLimit.left;
  196.       sPixMax = _thumbTrack.rectLimit.right -
  197.                 _thumbTrack.rectLimit.left;
  198.     }
  199.     else
  200.     {
  201.       sPix = point.v - _thumbTrack.rectLimit.top;
  202.       sPixMax = _thumbTrack.rectLimit.bottom -
  203.                 _thumbTrack.rectLimit.top;
  204.     }
  205.  
  206.     if( sPix < 0 )
  207.       sPix = 0;
  208.  
  209.     if( sPix > sPixMax )
  210.       sPix = sPixMax;
  211.  
  212.     sValueMin = (**_hctlTrack).contrlMin;
  213.     sValueRange = (**_hctlTrack).contrlMax - sValueMin;
  214.  
  215.     return (SHORT)(
  216.       ( (LONG)sPix * (LONG)sValueRange + (LONG)sValueRange/2 ) /
  217.       (LONG)sPixMax
  218.     ) + sValueMin;
  219. }
  220.  
  221. #endif
  222.  
  223. /*-----------------------------------------------------------------*/
  224. /* Part of failed attempt at scroll bar thumb tracking.            */
  225. /*-----------------------------------------------------------------*/
  226.  
  227. #if 0
  228.  
  229. LOCAL VOID MpmTrackThumbHideShow()
  230. {
  231.     RgnHandle   hrgn;
  232.     Pattern     patSave;
  233.     SHORT       sModeSave;
  234.  
  235. return;
  236.  
  237.     hrgn = NewRgn();
  238.     CopyRgn( _hrgnTrack, hrgn );
  239.     InsetRgn( hrgn, 1, 1 );
  240.     DiffRgn( _hrgnTrack, hrgn, hrgn );
  241.  
  242.     thePort = &MYWNDOF(_hwndTrack).pwin->port;
  243.     memcpy( patSave, thePort->pnPat, sizeof(Pattern) );
  244.     sModeSave = thePort->pnMode;
  245.     memcpy( thePort->pnPat, dkGray, sizeof(Pattern) );
  246.     thePort->pnMode = notPatXor;
  247.  
  248.     PaintRgn( hrgn );
  249.  
  250.     memcpy( thePort->pnPat, patSave, sizeof(Pattern) );
  251.     thePort->pnMode = sModeSave;
  252.  
  253.     DisposeRgn( hrgn );
  254. }
  255.  
  256. #endif
  257.  
  258. /*-----------------------------------------------------------------*/
  259. /* Part of failed attempt at scroll bar thumb tracking.            */
  260. /*-----------------------------------------------------------------*/
  261.  
  262. #if 0
  263.  
  264. LOCAL VOID MpmTrackThumbNotify( cmd, sValue )
  265.     SHORT       cmd;
  266.     SHORT       sValue;
  267. {
  268. }
  269.  
  270. #endif
  271.  
  272. /*-----------------------------------------------------------------*/
  273. /* Window function for scroll bar controls.                        */
  274. /*-----------------------------------------------------------------*/
  275.  
  276. MRESULT EXPENTRY MpmFnwpScrollBar( hwnd, msg, mp1, mp2 )
  277.     HWND        hwnd;
  278.     USHORT      msg;
  279.     MPARAM      mp1;
  280.     MPARAM      mp2;
  281. {
  282.     USHORT      id;
  283.     ControlHandle hctl, hctl1;
  284.     Rect        rect;
  285.     Rect*       prect;
  286.     SHORT       sPart;
  287.     POINTL      ptl;
  288.     SHORT       cmd;
  289.     Handle      hcdef;
  290.     LONG        lPoint;
  291.     Point       point;
  292.     static BOOL fTrackThumb = FALSE;
  293.     static SHORT sInitValue;
  294.  
  295.     if( ! MpmValidateWindow(hwnd) )
  296.       return FALSE;
  297.  
  298.     id   = MYWNDOF(hwnd).id;
  299.     hctl = MYWNDOF(hwnd).hctl;
  300.  
  301.     switch( msg )
  302.     {
  303.       /* Return the current position */
  304.  
  305.       case SBM_QUERYPOS:
  306.         return MRFROMSHORT( fTrackThumb ? sInitValue
  307.                                         : (**hctl).contrlValue );
  308.  
  309.       /* Return the scroll bar range */
  310.  
  311.       case SBM_QUERYRANGE:
  312.         return MRFROM2SHORT( (**hctl).contrlMin, (**hctl).contrlMax );
  313.  
  314.       /* Set the scroll bar position and range */
  315.  
  316.       case SBM_SETSCROLLBAR:
  317.         (**hctl).contrlMin = SHORT1FROMMP(mp2);
  318.         (**hctl).contrlMax = SHORT2FROMMP(mp2);
  319.         /* fall through */
  320.  
  321.       /* Set the scroll bar position only */
  322.  
  323.       case SBM_SETPOS:
  324.         if( MYWNDOF(hwnd).flStyle & WS_VISIBLE )
  325.           SetCtlValue( hctl, SHORT1FROMMP(mp1) );
  326.         else
  327.           (**hctl).contrlValue = SHORT1FROMMP(mp1);
  328.         fTrackThumb = FALSE;
  329.         return MRFROMSHORT(1);
  330.  
  331.       /* Handle mouse button down: call TrackControl to track it */
  332.  
  333.       case WM_BUTTON1DOWN:
  334.         ptl.x = SHORT1FROMMP(mp1);
  335.         ptl.y = SHORT2FROMMP(mp1);
  336.         MpmMapMacOfPtl( hwnd, &point, &ptl );
  337.         sPart = FindControl( point, PWINOFHWND(hwnd), &hctl1 );
  338.         ASSERT( sPart  &&  hctl1 == hctl,
  339.                 "MpmFnwpScrollBar: FindControl failed" );
  340.         _hwndTrack = hwnd;
  341.         if( sPart == inThumb )
  342.         {
  343.           fTrackThumb = TRUE;
  344.           sInitValue = (**hctl).contrlValue;
  345.           if( TrackControl( hctl, point, NULL ) )
  346.             MpmTrackScrollBarNotify( SB_SLIDERPOSITION );
  347.           fTrackThumb = FALSE;
  348.         }
  349.         else
  350.         {
  351.           TrackControl( hctl, point, (ProcPtr)MpmTrackScrollBar );
  352.           MpmTrackScrollBarNotify( SB_ENDSCROLL );
  353.         }
  354.         return 0L;
  355.  
  356.       /* Handle scroll bar creation: call NewControl to create it */
  357.  
  358.       case WM_CREATE:
  359.         rect.left = rect.top = 0;
  360.         if( MYWNDOF(hwnd).flStyle & SBS_VERT )
  361.           rect.right = _alSysVal[SV_CXVSCROLL], rect.bottom = 100;
  362.         else
  363.           rect.bottom = _alSysVal[SV_CYHSCROLL], rect.right = 100;
  364.         hctl = NewControl( PWINOFHWND(hwnd), &rect, _szNull,
  365.                            MYWNDOF(hwnd).flStyle & WS_VISIBLE,
  366.                            0, 0, 1, scrollBarProc, (long)hwnd );
  367.         if( ! hctl )
  368.           return MRFROMSHORT(1);
  369.         MYWNDOF(hwnd).hctl = hctl;
  370.         return 0L;
  371.  
  372.       /* Destroy scroll bar */
  373.  
  374.       case WM_DESTROY:
  375.         MYWNDOF(hwnd).hctl = NULL;
  376.         DisposeControl( hctl );
  377.         return 0L;
  378.  
  379.       /* Handle window movement: use MoveControl to move the bar */
  380.  
  381.       case WM_MOVE:
  382.         MpmQueryMacRect( hwnd, &rect );
  383.         if( MYWNDOF(hwnd).flStyle & WS_VISIBLE )
  384.           MoveControl( hctl, rect.left, rect.top );
  385.         else
  386.         {
  387.           prect = &(**hctl).contrlRect;
  388.           prect->right  += rect.left - prect->left;
  389.           prect->left    = rect.left;
  390.           prect->bottom += rect.top  - prect->top;
  391.           prect->top     = rect.top;
  392.         }
  393.         return 0L;
  394.  
  395.       /* Show or hide the scroll bar */
  396.  
  397.       case WM_SHOW:
  398.         if( mp1 )
  399.           ShowControl( hctl );
  400.         else
  401.           HideControl( hctl );
  402.         return 0L;
  403.  
  404.       /* Handle window resizing: use SizeControl to resize it */
  405.  
  406.       case WM_SIZE:
  407.         if( SHORT1FROMMP(mp2) && SHORT2FROMMP(mp2) )
  408.         {
  409.           if( MYWNDOF(hwnd).flStyle & WS_VISIBLE )
  410.             SizeControl( hctl,
  411.                          SHORT1FROMMP(mp2),
  412.                          SHORT2FROMMP(mp2) );
  413.           else
  414.           {
  415.             prect = &(**hctl).contrlRect;
  416.             prect->right  = prect->left + SHORT1FROMMP(mp2);
  417.             prect->bottom = prect->top  + SHORT2FROMMP(mp2);
  418.           }
  419.         }
  420.         return 0L;
  421.     }
  422.     
  423.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  424. }
  425.  
  426. /*-----------------------------------------------------------------*/
  427.