home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / TOOLKIT / OS2 / QUEUES / SVRQPNT.C < prev    next >
C/C++ Source or Header  |  1994-11-17  |  14KB  |  460 lines

  1. /*==============================================================*\
  2.  *  Pnt.c - routines for the painting of the main window
  3.  *      Copyright 1990, 1992 IBM Corp.
  4.  *--------------------------------------------------------------
  5.  *
  6.  *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  7.  *  sample code created by IBM Corporation. This sample code is not
  8.  *  part of any standard or IBM product and is provided to you solely
  9.  *  for  the purpose of assisting you in the development of your
  10.  *  applications.  The code is provided "AS IS", without
  11.  *  warranty of any kind.  IBM shall not be liable for any damages
  12.  *  arising out of your use of the sample code, even if they have been
  13.  *  advised of the possibility of   such damages.
  14.  *
  15.  *--------------------------------------------------------------
  16.  *  This module contains the code for the main client window
  17.  *  painting
  18.  *
  19. \*==============================================================*/
  20.  
  21.  
  22.  
  23. /*--------------------------------------------------------------*\
  24.  *  Include files, macros, defined constants, and externs
  25. \*--------------------------------------------------------------*/
  26.  
  27. #define INCL_WINMESSAGEMGR
  28. #define INCL_WINWINDOWMGR
  29. #define INCL_GPICONTROL
  30. #define INCL_GPIPRIMITIVES
  31. #define INCL_GPILCIDS
  32. #define INCL_DOSPROCESS
  33. #define INCL_WINFRAMEMGR
  34. #define INCL_WINSCROLLBARS
  35.  
  36. #include <os2.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "svrqmain.h"
  40. #include "svrqglbl.h"
  41. /*--------------------------------------------------------------*\
  42.  *  Entry point declarations
  43. \*--------------------------------------------------------------*/
  44.  
  45. #include "svrqxtrn.h"
  46.  
  47.  
  48. /* returns offset from beginning of circular queue */
  49. #define  BUFF_OFFSET(index, offset)  (((index)+(offset) < MAX_MESSAGES) ? (index)+(offset) : (index)+(offset)-MAX_MESSAGES)
  50.  
  51. /*--------------------------------------------------------------*\
  52.  *  Global (module) variables
  53. \*--------------------------------------------------------------*/
  54.  
  55. HWND  hwndHscroll, hwndVscroll ;
  56. LONG  sHscrollMax=0, sVscrollMax=0, sHscrollPos=0, sVscrollPos=0, cxChar,
  57.       cxCaps, cyChar, cyDesc, cxClient, cyClient, cxTextTotal;
  58.  
  59.  
  60. /****************************************************************\
  61.  *  Main client painting routine
  62.  *--------------------------------------------------------------
  63.  *
  64.  *  Name:   MainPaint(hwnd)
  65.  *
  66.  *  Purpose: Paints the main client window.
  67.  *
  68.  *  Usage:  Routine is called whenver the client window
  69.  *          procedure receives a WM_PAINT message
  70.  *
  71.  *  Method:
  72.  *          - begins painting by calling WinBeginPaint
  73.  *              and retrieving the HPS for the window
  74.  *          - performs any painting desired
  75.  *          - ends painting by calling WinEndPaint
  76.  *
  77.  *  Returns:
  78.  *
  79. \****************************************************************/
  80. VOID MainPaint(HWND hwnd,                                    /* handle to window */
  81.                CHAR SvrQMsgs[MAX_MESSAGES][MESSAGE_LEN+38],  /* array of messages */
  82.                unsigned int uiIndex,                         /* beginning of circular array */
  83.                SHORT sTotalMsgs)                             /* total number of messages */
  84. {
  85.    RECTL         rclInvalid;
  86.    HPS           hps;
  87.    LONG          sPaintBeg, sPaintEnd, sLine;
  88.    POINTL        ptl;
  89.    unsigned int  uiOffset;
  90.  
  91.    uiOffset = (sTotalMsgs < MAX_MESSAGES) ? 0 : uiIndex;
  92.  
  93.    hps = WinBeginPaint(hwnd, NULLHANDLE, (PRECTL)&rclInvalid);
  94.    GpiErase(hps);
  95.  
  96.    sPaintBeg = max(0, sVscrollPos + (cyClient - rclInvalid.yTop) / cyChar);
  97.    sPaintEnd = min(sTotalMsgs, sVscrollPos + (cyClient - rclInvalid.yBottom) / cyChar + 1);
  98.  
  99.    for (sLine = sPaintBeg ; sLine < sPaintEnd ; sLine++)
  100.    {
  101.       ptl.x = cxCaps - sHscrollPos ;
  102.       ptl.y = cyClient - cyChar * (BUFF_OFFSET(uiOffset,sLine) + 1 - sVscrollPos) + cyDesc ;
  103.  
  104.       GpiCharStringAt (hps, &ptl,
  105.                        (LONG) strlen (SvrQMsgs[BUFF_OFFSET(uiOffset,sLine)]),
  106.                        SvrQMsgs[BUFF_OFFSET(uiOffset,sLine)]) ;
  107.    }
  108.  
  109.    WinEndPaint (hps) ;
  110. }   /* MainPaint() */
  111.  
  112.  
  113. /****************************************************************\
  114.  *  Initialization routine
  115.  *--------------------------------------------------------------
  116.  *
  117.  *  Name:   InitMainWindow(hwnd, mp1, mp2)
  118.  *
  119.  *  Purpose: Performs initialization functions required
  120.  *              when the main window is Copyright.
  121.  *
  122.  *  Usage:  Called once during the WM_CREATE processing when
  123.  *          the main window is Copyright.
  124.  *
  125.  *  Method:
  126.  *
  127.  *  Returns: value to be returned from the WM_CREATE message:
  128.  *          TRUE - window creation should stop
  129.  *          FALSE - window creation should continue
  130. \****************************************************************/
  131.  
  132. MRESULT InitMainWindow(HWND hwnd,      /* handle to the main client window */
  133.                        MPARAM mp1,     /* first parameter of WM_CREATE message */
  134.                        MPARAM mp2)     /* second parameter of WM_CREATE message */
  135. {
  136.    FONTMETRICS fm;
  137.    HPS         hps;
  138.  
  139.    hps = WinGetPS (hwnd) ;
  140.    GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  141.  
  142.    cxChar = fm.lAveCharWidth ;
  143.    cxCaps = fm.lEmInc ;
  144.    cyChar = fm.lMaxBaselineExt ;
  145.    cyDesc = fm.lMaxDescender ;
  146.  
  147.    WinReleasePS (hps) ;
  148.  
  149.    cxTextTotal = MESSAGE_LEN * cxChar ;
  150.  
  151.    hwndHscroll = WinWindowFromID (
  152.                        WinQueryWindow (hwnd, QW_PARENT),
  153.                        FID_HORZSCROLL) ;
  154.  
  155.    hwndVscroll = WinWindowFromID (
  156.                        WinQueryWindow (hwnd, QW_PARENT),
  157.                        FID_VERTSCROLL) ;
  158.  
  159.    return (MRFROMLONG(0));
  160. }
  161.  
  162.  
  163. /****************************************************************\
  164.  *  Window sizing routine
  165.  *--------------------------------------------------------------
  166.  *
  167.  *  Name:   InitMainWindow(hwnd, mp1, mp2)
  168.  *
  169.  *  Purpose: Adjusts scroll bars and window parameters
  170.  *              when the main window is resized.
  171.  *
  172.  *  Usage:  Called when the client window receives a WM_SIZE
  173.  *          message.
  174.  *
  175.  *  Method:
  176.  *
  177.  *  Returns:
  178. \****************************************************************/
  179.  
  180. MRESULT MainSize(HWND   hwnd,    /* handle to the main client window */
  181.                  USHORT sTotalMsgs,
  182.                  MPARAM mp1,     /* first parameter of WM_CREATE message */
  183.                  MPARAM mp2)     /* second parameter of WM_CREATE message */
  184. {
  185.  
  186.    cxClient = SHORT1FROMMP (mp2) ;
  187.    cyClient = SHORT2FROMMP (mp2) ;
  188.  
  189.    sHscrollMax = max (0, cxTextTotal - cxClient) ;
  190.    sHscrollPos = min (sHscrollPos, sHscrollMax) ;
  191.  
  192.    WinSendMsg (hwndHscroll, SBM_SETSCROLLBAR,
  193.                             MPFROM2SHORT (sHscrollPos, 0),
  194.                             MPFROM2SHORT (0, sHscrollMax)) ;
  195.  
  196.    WinEnableWindow (hwndHscroll, (BOOL)((sHscrollMax != 0) ? TRUE : FALSE)) ;
  197.  
  198.    sVscrollMax = max (0, sTotalMsgs - cyClient / cyChar) ;
  199.    sVscrollPos = min (sVscrollPos, sVscrollMax) ;
  200.  
  201.    WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  202.                             MPFROM2SHORT (sVscrollPos, 0),
  203.                             MPFROM2SHORT (0, sVscrollMax)) ;
  204.  
  205.    WinEnableWindow (hwndVscroll, (BOOL)((sVscrollMax != 0) ? TRUE : FALSE)) ;
  206.  
  207.    /* referenced to prevent compiler error */
  208.    return (MRFROMLONG(0));
  209. }
  210.  
  211.  
  212. /****************************************************************\
  213.  *  Horizontal scrolling routine
  214.  *--------------------------------------------------------------
  215.  *
  216.  *  Name:   MainHorizScroll(hwnd, mp1, mp2)
  217.  *
  218.  *  Purpose: Scrolls window horizontally.
  219.  *
  220.  *  Usage:  Called when the client window receives a WM_HSCROLL
  221.  *          message.
  222.  *
  223.  *  Method:
  224.  *
  225.  *  Returns:
  226. \****************************************************************/
  227.  
  228. MRESULT MainHorizScroll(HWND hwnd,   /* handle to the main client window */
  229.                         MPARAM mp2)  /* second parameter of WM_CREATE message */
  230. {
  231.    LONG sHscrollInc;
  232.  
  233.    switch (SHORT2FROMMP (mp2))
  234.    {
  235.       case SB_LINELEFT:
  236.          sHscrollInc = -cxCaps ;
  237.          break ;
  238.  
  239.       case SB_LINERIGHT:
  240.          sHscrollInc = cxCaps ;
  241.          break ;
  242.  
  243.       case SB_PAGELEFT:
  244.          sHscrollInc = -8 * cxCaps ;
  245.          break ;
  246.  
  247.       case SB_PAGERIGHT:
  248.          sHscrollInc = 8 * cxCaps ;
  249.          break ;
  250.  
  251.       case SB_SLIDERPOSITION:
  252.          sHscrollInc = SHORT1FROMMP (mp2) - sHscrollPos;
  253.          break ;
  254.  
  255.       default:
  256.          sHscrollInc = 0;
  257.          break ;
  258.    }
  259.  
  260.    sHscrollInc = max (-sHscrollPos,
  261.                  min (sHscrollInc, sHscrollMax - sHscrollPos)) ;
  262.  
  263.    if (sHscrollInc != 0)
  264.    {
  265.       sHscrollPos += sHscrollInc ;
  266.       WinScrollWindow ((HWND)hwnd, -sHscrollInc, 0, (PRECTL)NULL,
  267.                        (PRECTL)NULL, (HRGN)NULL, (PRECTL)NULL,
  268.                        SW_INVALIDATERGN);
  269.  
  270.       WinSendMsg (hwndHscroll, SBM_SETPOS,
  271.                   MPFROMSHORT (sHscrollPos), MPVOID) ;
  272.    }
  273.    return (MRFROMLONG(0));
  274. }
  275.  
  276.  
  277. /****************************************************************\
  278.  *  Vertical scrolling routine
  279.  *--------------------------------------------------------------
  280.  *
  281.  *  Name:   MainVertScroll(hwnd, mp2)
  282.  *
  283.  *  Purpose: Scrolls window vertically.
  284.  *
  285.  *  Usage:  Called when the client window receives a WM_VSCROLL
  286.  *          message.
  287.  *
  288.  *  Method:
  289.  *
  290.  *  Returns:
  291. \****************************************************************/
  292.  
  293. MRESULT MainVertScroll(HWND hwnd,      /* handle to the main client window */
  294.                        MPARAM mp2)     /* second parameter of WM_CREATE message */
  295. {
  296.    LONG sVscrollInc;
  297.  
  298.    switch (SHORT2FROMMP (mp2))
  299.    {
  300.       case SB_LINEUP:
  301.          sVscrollInc = -1 ;
  302.          break ;
  303.  
  304.       case SB_LINEDOWN:
  305.          sVscrollInc = 1 ;
  306.          break ;
  307.  
  308.       case SB_PAGEUP:
  309.          sVscrollInc = min (-1, -cyClient / cyChar) ;
  310.          break ;
  311.  
  312.       case SB_PAGEDOWN:
  313.          sVscrollInc = max (1, cyClient / cyChar) ;
  314.          break ;
  315.  
  316.       case SB_SLIDERTRACK:
  317.          sVscrollInc = SHORT1FROMMP (mp2) - sVscrollPos;
  318.          break ;
  319.  
  320.       default:
  321.          sVscrollInc = 0;
  322.          break ;
  323.    }
  324.  
  325.    sVscrollInc = max (-sVscrollPos,
  326.                  min (sVscrollInc, sVscrollMax - sVscrollPos)) ;
  327.  
  328.    if (sVscrollInc != 0)
  329.    {
  330.       sVscrollPos += sVscrollInc ;
  331.       WinScrollWindow ((HWND)hwnd, 0, (cyChar * sVscrollInc),
  332.                      (PRECTL)NULL, (PRECTL)NULL, (HRGN)NULL,
  333.                      (PRECTL)NULL, SW_INVALIDATERGN);
  334.  
  335.       WinSendMsg (hwndVscroll, SBM_SETPOS,
  336.                   MPFROMSHORT (sVscrollPos), MPVOID) ;
  337.       WinUpdateWindow (hwnd) ;
  338.    }
  339.    return (MRFROMLONG(0));
  340. }
  341.  
  342.  
  343. /****************************************************************\
  344.  *  Update messages
  345.  *--------------------------------------------------------------
  346.  *
  347.  *  Name:   MainCharHScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  348.  *
  349.  *  Purpose:
  350.  *
  351.  *  Usage:
  352.  *
  353.  *  Method:
  354.  *
  355.  *  Returns:
  356. \****************************************************************/
  357.  
  358. MRESULT MainCharHScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  359. {
  360.  
  361.    /* referenced to prevent compiler error */
  362.    hwnd;
  363.    return (WinSendMsg(hwndHscroll, msg, mp1, mp2));
  364. }
  365.  
  366.  
  367. /****************************************************************\
  368.  *  Update messages
  369.  *--------------------------------------------------------------
  370.  *
  371.  *  Name:   MainCharVScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  372.  *
  373.  *  Purpose:
  374.  *
  375.  *  Usage:
  376.  *
  377.  *  Method:
  378.  *
  379.  *  Returns:
  380. \****************************************************************/
  381.  
  382. MRESULT MainCharVScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  383. {
  384.  
  385.    return (WinSendMsg(hwndVscroll, msg, mp1, mp2));
  386. }
  387.  
  388.  
  389. /****************************************************************\
  390.  *  Update messages
  391.  *--------------------------------------------------------------
  392.  *
  393.  *  Name:   MainUpdateMsg(hwnd)
  394.  *
  395.  *  Purpose: Update portion of screen with new messages.
  396.  *
  397.  *  Usage:  Called when a new message is read from the queue.
  398.  *
  399.  *  Method:
  400.  *
  401.  *  Returns:
  402. \****************************************************************/
  403.  
  404. VOID MainUpdateMsg(HWND hwnd,
  405.                    CHAR SvrQMsgs[MAX_MESSAGES][MESSAGE_LEN+38],
  406.                    unsigned int index,
  407.                    SHORT sTotalMsgs)
  408. {
  409.    RECTL rclInvalid;
  410.  
  411.    /* if it will fit in the window */
  412.    if (sTotalMsgs <= cyClient/cyChar + sVscrollPos + 1)
  413.    {
  414.       rclInvalid.yBottom = (LONG)max(0, cyClient - sTotalMsgs * cyChar);
  415.       rclInvalid.yTop = (LONG)(cyClient - (sTotalMsgs - sVscrollPos - 1) * cyChar);
  416.       rclInvalid.xLeft = 0L;
  417.       rclInvalid.xRight = (LONG)cxClient;
  418.       WinInvalidateRect(hwnd, &rclInvalid, FALSE);
  419.       MainPaint(hwnd, SvrQMsgs, index, sTotalMsgs);
  420.    }
  421.  
  422.    /* if lines exceed window */
  423.    if (sTotalMsgs > cyClient/cyChar)
  424.    {
  425.       WinEnableWindow (hwndVscroll, TRUE) ;
  426.       sVscrollMax = sTotalMsgs - cyClient / cyChar;
  427.       sVscrollPos = min (sVscrollPos, sVscrollMax) ;
  428.  
  429.       WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  430.                                MPFROM2SHORT (sVscrollPos, 0),
  431.                                MPFROM2SHORT (0, sVscrollMax)) ;
  432.  
  433.       /* if new msg is just off screen */
  434.       if (sTotalMsgs == cyClient/cyChar + sVscrollPos + 1)
  435.       {
  436.          MainVertScroll(hwnd, MPFROM2SHORT(0, SB_LINEDOWN));
  437.       }
  438.  
  439.       else
  440.       {
  441.          DosBeep(1000, 100);
  442.       }
  443.    }
  444. }
  445.  
  446.  
  447.  
  448. VOID MainPurgeWindow()
  449. {
  450.  
  451.    sVscrollMax = 0;
  452.    sVscrollPos = 0;
  453.    WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  454.                             MPFROM2SHORT (sVscrollPos, 0),
  455.                             MPFROM2SHORT (0, sVscrollMax)) ;
  456.  
  457.    WinEnableWindow (hwndVscroll, FALSE) ;
  458.    WinInvalidateRect(hwndMain, NULL, FALSE);
  459. }
  460.