home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / pmscroll.c < prev    next >
C/C++ Source or Header  |  1998-06-07  |  6KB  |  175 lines

  1. /*
  2.  * pmscroll.c
  3.  *
  4.  * Generic scroll bar utilities.
  5.  *
  6.  *      Created: 4th December, 1997
  7.  * Version 2.00: 4th December, 1997
  8.  *
  9.  * (C) 1997 Nicholas Paul Sheppard
  10.  *
  11.  * This file is distributed under the GNU General Public License. See the
  12.  * file copying.txt for details.
  13.  */
  14.  
  15. #define INCL_WIN
  16. #define INCL_GPI
  17. #include <os2.h>
  18. #include "pmutil.h"
  19.  
  20.  
  21. int ScrollMsgHandler(HWND hwndScroll, ULONG message, MPARAM mp1, MPARAM mp2, RECTL *prectlMain, int iStep)
  22. /*
  23.  * Generic routine for handling WM_VSCROLL and WM_HSCROLL messages. This routine will
  24.  * update the position of the scroll bar appropriately and return the change in the
  25.  * scroll bar's position.
  26.  *
  27.  * HWND hwndScroll    - window handle of the scroll bar being scrolled
  28.  * ULONG message    - the message we're handling (WM_VSCROLL or WM_HSCROLL)
  29.  * MPARAM mp1        - first m-parameter passed with the WM_VSCROLL message
  30.  * MPARAM mp2        - second m-parameter passed with the WM_VSCROLL message
  31.  * RECTL *prectlMain    - window rectangle of the window we are scrolling
  32.  * int iStep        - distance to move scroll bar in response to SB_LINE*
  33.  *
  34.  * Returns        - the amount the scroll bar has moved by
  35.  */
  36. {
  37.     int iInit, iFinal, iLower, iUpper;
  38.     MRESULT mr;
  39.  
  40.     /* idiot protection */
  41.     if ((message != WM_VSCROLL) && (message != WM_HSCROLL))
  42.         return (0);
  43.  
  44.     /* get initial position of scroll bar */
  45.     iFinal = iInit = SHORT1FROMMR(WinSendMsg(hwndScroll, SBM_QUERYPOS, NULL, NULL));
  46.  
  47.     /* get scroll bar limits */
  48.     mr = WinSendMsg(hwndScroll, SBM_QUERYRANGE, NULL, NULL);
  49.     iLower = SHORT1FROMMR(mr);
  50.     iUpper = SHORT2FROMMR(mr);
  51.  
  52.     /* calculate final position of scroll bar according to the m-parameters */
  53.     switch (SHORT2FROMMP(mp2)) {
  54.         case 1: /* = SB_LINEUP, SB_LINELEFT */
  55.             if ((iFinal = iInit - iStep) < iLower)
  56.                 iFinal = iLower;
  57.             break;
  58.  
  59.         case 2: /* = SB_LINEDOWN, SB_LINERIGHT */
  60.             if ((iFinal = iInit + iStep) > iUpper)
  61.                 iFinal = iUpper;
  62.             break;
  63.  
  64.         case 3: /* = SB_PAGEUP, SB_PAGELEFT */
  65.             if (message == WM_VSCROLL)
  66.                 iFinal = iInit - prectlMain->yTop + prectlMain->yBottom;
  67.             else
  68.                 iFinal = iInit - prectlMain->xRight + prectlMain->xLeft;
  69.             if (iFinal < iLower)
  70.                 iFinal = iLower;
  71.             break;
  72.  
  73.         case 4: /* = SB_PAGEDOWN, SB_PAGERIGHT */
  74.             if (message == WM_VSCROLL)
  75.                 iFinal = iInit + prectlMain->yTop - prectlMain->yBottom;
  76.             else
  77.                 iFinal = iInit + prectlMain->xRight - prectlMain->xLeft;
  78.             if (iFinal > iUpper)
  79.                 iFinal = iUpper;
  80.             break;
  81.  
  82.         case SB_SLIDERPOSITION:
  83.         case SB_SLIDERTRACK:
  84.             iFinal = SHORT1FROMMP(mp2);
  85.             break;
  86.     }
  87.     WinSendMsg(hwndScroll, SBM_SETPOS, MPFROMSHORT(iFinal), NULL);
  88.  
  89.     return (iFinal - iInit);
  90. }
  91.  
  92.  
  93. void ScrollMetafile(HPS hps, HWND hwndHorizScroll, HWND hwndVertScroll, HMF hmf)
  94. /*
  95.  * Translate and play a metafile so that it shows the correct portion according
  96.  * the position of the scroll bars. The scrolls bars should have ranges
  97.  * corresponding to the boundary of the metafile.
  98.  *
  99.  * HPS hps        - presentation space to play the metafile in
  100.  * HWND hwndHorizScroll    - handle of horizontal scroll bar (NULLHANDLE if none)
  101.  * HWND hwndVertScroll    - handle of vertical scroll bar (NULLHANDLE if none)
  102.  * HMF hmf        - the metafile
  103.  */
  104. {
  105.     long            alOptions[PMF_DEFAULTS + 1];
  106.     MATRIXLF        matlf;
  107.     POINTL            pointl;
  108.  
  109.     /* translate metafile according to scrollbar positions */
  110.     if (hwndHorizScroll != NULLHANDLE)
  111.         pointl.x = SHORT1FROMMR(WinSendMsg(hwndHorizScroll, SBM_QUERYPOS, NULL, NULL));
  112.     else
  113.         pointl.x = 0;
  114.     if (hwndVertScroll != NULLHANDLE)
  115.         pointl.y = SHORT1FROMMR(WinSendMsg(hwndVertScroll, SBM_QUERYPOS, NULL, NULL));
  116.     else
  117.         pointl.y = 0;
  118.     GpiTranslate(hps, &matlf, TRANSFORM_REPLACE, &pointl);
  119.     GpiSetDefaultViewMatrix(hps, 9L, &matlf, TRANSFORM_REPLACE);
  120.             
  121.     /* paint screen using the metafile */
  122.     alOptions[PMF_SEGBASE] = 0L;
  123.     alOptions[PMF_LOADTYPE] = LT_NOMODIFY;
  124.     alOptions[PMF_RESOLVE] = RS_DEFAULT;
  125.     alOptions[PMF_LCIDS] = LC_LOADDISC;
  126.     alOptions[PMF_RESET] = RES_DEFAULT;
  127.     alOptions[PMF_SUPPRESS] = SUP_DEFAULT;
  128.     alOptions[PMF_COLORTABLES] = CTAB_REPLACE;
  129.     alOptions[PMF_COLORREALIZABLE] = CREA_DEFAULT;
  130.     alOptions[PMF_DEFAULTS] = DDEF_IGNORE;
  131.     GpiPlayMetaFile(hps, hmf, PMF_DEFAULTS, alOptions, 0, 0, NULL);
  132. }
  133.  
  134.  
  135. void ScrollSetGeometry(HWND hwnd, HWND hwndHorizScroll, HWND hwndVertScroll, RECTL *prectl)
  136. /*
  137.  * Set the range and size of scroll bars such that they will scroll over the
  138.  * given rectangle. The thumbs will be initialised to the top and left.
  139.  * Unnecessary scroll bars will be disabled.
  140.  *
  141.  * HWND hwnd        - the window to have its scroll bars set
  142.  * HWND hwndHorizScroll    - handle of the horizontal scroll bar (NULLHANDLE if none)
  143.  * HWND hwndVertScroll  - handle of the vertical scroll bar (NULLHANDLE if none)
  144.  * RECTL *prectl    - the rectangle the window will scroll over
  145.  */
  146. {
  147.     RECTL rectlMain;
  148.     int iRange;
  149.  
  150.     /* retrieve the size of window */
  151.     WinQueryWindowRect(hwnd, &rectlMain);
  152.  
  153.     if (hwndHorizScroll != NULLHANDLE) {
  154.         if ((rectlMain.xRight - rectlMain.xLeft) < (prectl->xRight - prectl->xLeft)) {
  155.             WinEnableWindow(hwndHorizScroll, TRUE);
  156.             iRange = (prectl->xRight - prectl->xLeft) - (rectlMain.xRight - rectlMain.xLeft);
  157.             WinSendMsg(hwndHorizScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0), MPFROM2SHORT(0, iRange));
  158.             WinSendMsg(hwndHorizScroll, SBM_SETTHUMBSIZE, MPFROM2SHORT(rectlMain.xRight - rectlMain.xLeft, prectl->xRight - prectl->xLeft), NULL);
  159.         } else {
  160.             WinEnableWindow(hwndHorizScroll, FALSE);
  161.         }
  162.     }
  163.  
  164.     if (hwndVertScroll != NULLHANDLE) {
  165.         if ((rectlMain.yTop - rectlMain.yBottom) < (prectl->yTop - prectl->yBottom)) {
  166.             WinEnableWindow(hwndVertScroll, TRUE);
  167.             iRange = (prectl->yTop - prectl->yBottom) - (rectlMain.yTop - rectlMain.yBottom);
  168.             WinSendMsg(hwndVertScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0), MPFROM2SHORT(0, iRange));
  169.             WinSendMsg(hwndVertScroll, SBM_SETTHUMBSIZE, MPFROM2SHORT(rectlMain.yTop - rectlMain.yBottom, prectl->yTop - prectl->yBottom), NULL);
  170.         } else {
  171.             WinEnableWindow(hwndVertScroll, FALSE);
  172.         }
  173.     }
  174. }
  175.