home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / npipe / svrppnt.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  14KB  |  458 lines

  1. /*==============================================================*\
  2.  *  svrppnt.c - routines for the painting of the main window
  3.  *      Copyright 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.  *
  17.  *  This module contains the code for the main client window
  18.  *  painting
  19.  *
  20. \*==============================================================*/
  21.  
  22.  
  23.  
  24. /*--------------------------------------------------------------*\
  25.  *  Include files, macros, defined constants, and externs
  26. \*--------------------------------------------------------------*/
  27.  
  28. #define INCL_WINMESSAGEMGR
  29. #define INCL_WINWINDOWMGR
  30. #define INCL_GPICONTROL
  31. #define INCL_GPIPRIMITIVES
  32. #define INCL_GPILCIDS
  33. #define INCL_DOSPROCESS
  34. #define INCL_WINFRAMEMGR
  35. #define INCL_WINSCROLLBARS
  36.  
  37. #include <os2.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "svrpmain.h"
  41. #include "svrpglbl.h"
  42. /*--------------------------------------------------------------*\
  43.  *  Entry point declarations
  44. \*--------------------------------------------------------------*/
  45.  
  46. #include "svrpxtrn.h"
  47.  
  48.  
  49. /* returns offset from beginning of circular array */
  50. #define  BUFF_OFFSET(index, offset)  (((index)+(offset) < MAX_MESSAGES) ? (index)+(offset) : (index)+(offset)-MAX_MESSAGES)
  51.  
  52. /*--------------------------------------------------------------*\
  53.  *  Global (module) variables
  54. \*--------------------------------------------------------------*/
  55.  
  56. HWND  hwndHscroll, hwndVscroll ;
  57. LONG  sHscrollMax=0, sVscrollMax=0, sHscrollPos=0, sVscrollPos=0, cxChar,
  58.       cxCaps, cyChar, cyDesc, cxClient, cyClient, cxTextTotal;
  59.  
  60.  
  61. /****************************************************************\
  62.  *  Main client painting routine
  63.  *--------------------------------------------------------------
  64.  *
  65.  *  Name:   MainPaint(hwnd)
  66.  *
  67.  *  Purpose: Paints the main client window.
  68.  *
  69.  *  Usage:  Routine is called whenver the client window
  70.  *          procedure receives a WM_PAINT message
  71.  *
  72.  *  Method:
  73.  *          - begins painting by calling WinBeginPaint
  74.  *              and retrieving the HPS for the window
  75.  *          - performs any painting desired
  76.  *          - ends painting by calling WinEndPaint
  77.  *
  78.  *  Returns:
  79.  *
  80. \****************************************************************/
  81. VOID MainPaint(HWND hwnd,                                 /* handle to window */
  82.                CHAR PipeMsgs[MAX_MESSAGES][MESSAGE_LEN],  /* array of messages */
  83.                unsigned int uiIndex,                      /* beginning of circular array */
  84.                SHORT sTotalMsgs)                          /* total number of messages */
  85. {
  86.    RECTL         rclInvalid;
  87.    HPS           hps;
  88.    LONG          sPaintBeg, sPaintEnd, sLine;
  89.    POINTL        ptl;
  90.    unsigned int  uiOffset;
  91.  
  92.    uiOffset = (sTotalMsgs < MAX_MESSAGES) ? 0 : uiIndex;
  93.  
  94.    hps = WinBeginPaint(hwnd, NULLHANDLE, (PRECTL)&rclInvalid);
  95.    GpiErase(hps);
  96.  
  97.    sPaintBeg = max(0, sVscrollPos + (cyClient - rclInvalid.yTop) / cyChar);
  98.    sPaintEnd = min(sTotalMsgs, sVscrollPos + (cyClient - rclInvalid.yBottom) / cyChar + 1);
  99.  
  100.    for (sLine = sPaintBeg ; sLine < sPaintEnd ; sLine++)
  101.    {
  102.       ptl.x = cxCaps - sHscrollPos ;
  103.       ptl.y = cyClient - cyChar * (BUFF_OFFSET(uiOffset,sLine) + 1 - sVscrollPos) + cyDesc ;
  104.  
  105.       GpiCharStringAt (hps, &ptl,
  106.                        (LONG) strlen (PipeMsgs[BUFF_OFFSET(uiOffset,sLine)]),
  107.                        PipeMsgs[BUFF_OFFSET(uiOffset,sLine)]) ;
  108.    }
  109.  
  110.    WinEndPaint (hps) ;
  111. }   /* MainPaint() */
  112.  
  113.  
  114. /****************************************************************\
  115.  *  Initialization routine
  116.  *--------------------------------------------------------------
  117.  *
  118.  *  Name:   InitMainWindow(hwnd, mp1, mp2)
  119.  *
  120.  *  Purpose: Performs initialization functions required
  121.  *              when the main window is created.
  122.  *
  123.  *  Usage:  Called once during the WM_CREATE processing when
  124.  *          the main window is created.
  125.  *
  126.  *  Method:
  127.  *
  128.  *  Returns: value to be returned from the WM_CREATE message:
  129.  *          TRUE - window creation should stop
  130.  *          FALSE - window creation should continue
  131. \****************************************************************/
  132.  
  133. MRESULT InitMainWindow(HWND hwnd,      /* handle to the main client window */
  134.                        MPARAM mp1,     /* first parameter of WM_CREATE message */
  135.                        MPARAM mp2)     /* second parameter of WM_CREATE message */
  136. {
  137.    FONTMETRICS fm;
  138.    HPS         hps;
  139.  
  140.    hps = WinGetPS (hwnd) ;
  141.    GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  142.  
  143.    cxChar = fm.lAveCharWidth ;
  144.    cxCaps = fm.lEmInc ;
  145.    cyChar = fm.lMaxBaselineExt ;
  146.    cyDesc = fm.lMaxDescender ;
  147.  
  148.    WinReleasePS (hps) ;
  149.  
  150.    cxTextTotal = MESSAGE_LEN * cxChar ;
  151.  
  152.    hwndHscroll = WinWindowFromID (
  153.                        WinQueryWindow (hwnd, QW_PARENT),
  154.                        FID_HORZSCROLL) ;
  155.  
  156.    hwndVscroll = WinWindowFromID (
  157.                        WinQueryWindow (hwnd, QW_PARENT),
  158.                        FID_VERTSCROLL) ;
  159.  
  160.    return (MRFROMLONG(0));
  161. }
  162.  
  163.  
  164. /****************************************************************\
  165.  *  Window sizing routine
  166.  *--------------------------------------------------------------
  167.  *
  168.  *  Name:   InitMainWindow(hwnd, mp1, mp2)
  169.  *
  170.  *  Purpose: Adjusts scroll bars and window parameters
  171.  *              when the main window is resized.
  172.  *
  173.  *  Usage:  Called when the client window receives a WM_SIZE
  174.  *          message.
  175.  *
  176.  *  Method:
  177.  *
  178.  *  Returns:
  179. \****************************************************************/
  180.  
  181. MRESULT MainSize(HWND hwnd,      /* handle to the main client window */
  182.                  SHORT sTotalMsgs,
  183.                  MPARAM mp1,     /* first parameter of WM_SIZE message */
  184.                  MPARAM mp2)     /* second parameter of WM_SIZE message */
  185. {
  186.  
  187.    cxClient = SHORT1FROMMP (mp2) ;
  188.    cyClient = SHORT2FROMMP (mp2) ;
  189.  
  190.    sHscrollMax = max (0, cxTextTotal - cxClient) ;
  191.    sHscrollPos = min (sHscrollPos, sHscrollMax) ;
  192.  
  193.    WinSendMsg (hwndHscroll, SBM_SETSCROLLBAR,
  194.                             MPFROM2SHORT (sHscrollPos, 0),
  195.                             MPFROM2SHORT (0, sHscrollMax)) ;
  196.  
  197.    WinEnableWindow (hwndHscroll, (BOOL)((sHscrollMax != 0) ? TRUE : FALSE)) ;
  198.  
  199.    sVscrollMax = max (0, sTotalMsgs - cyClient / cyChar) ;
  200.    sVscrollPos = min (sVscrollPos, sVscrollMax) ;
  201.  
  202.    WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  203.                             MPFROM2SHORT (sVscrollPos, 0),
  204.                             MPFROM2SHORT (0, sVscrollMax)) ;
  205.  
  206.    WinEnableWindow (hwndVscroll, (BOOL)((sVscrollMax != 0) ? TRUE : FALSE)) ;
  207.  
  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)
  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)
  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.  *
  345.  *  Name:   MainCharHScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  346.  *
  347.  *  Purpose:
  348.  *
  349.  *  Usage:
  350.  *
  351.  *  Method:
  352.  *
  353.  *  Returns:
  354. \****************************************************************/
  355.  
  356. MRESULT MainCharHScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  357. {
  358.  
  359.    return (WinSendMsg(hwndHscroll, msg, mp1, mp2));
  360. }
  361.  
  362.  
  363. /****************************************************************\
  364.  *
  365.  *  Name:   MainCharVScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  366.  *
  367.  *  Purpose:
  368.  *
  369.  *  Usage:
  370.  *
  371.  *  Method:
  372.  *
  373.  *  Returns:
  374. \****************************************************************/
  375.  
  376. MRESULT MainCharVScroll(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  377. {
  378.  
  379.    return (WinSendMsg(hwndVscroll, msg, mp1, mp2));
  380. }
  381.  
  382.  
  383. /****************************************************************\
  384.  *  Update messages
  385.  *--------------------------------------------------------------
  386.  *
  387.  *  Name:   MainUpdateMsg(hwnd)
  388.  *
  389.  *  Purpose: Update portion of screen with new messages.
  390.  *
  391.  *  Usage:  Called when a new message is read
  392.             from or written to the pipe.
  393.  *
  394.  *  Method:
  395.  *
  396.  *  Returns:
  397. \****************************************************************/
  398.  
  399. VOID MainUpdateMsg(HWND hwnd,
  400.                    CHAR PipeMsgs[MAX_MESSAGES][MESSAGE_LEN],
  401.                    unsigned int index,
  402.                    SHORT sTotalMsgs)
  403. {
  404.    RECTL rclInvalid;
  405.  
  406.    /* if it will fit in the window */
  407.    if (sTotalMsgs <= cyClient/cyChar + sVscrollPos + 1)
  408.    {
  409.       rclInvalid.yBottom = max(0, cyClient - sTotalMsgs * cyChar);
  410.       rclInvalid.yTop = (cyClient - (sTotalMsgs - sVscrollPos - 1) * cyChar);
  411.       rclInvalid.xLeft = 0;
  412.       rclInvalid.xRight = cxClient;
  413.       WinInvalidateRect(hwnd, &rclInvalid, FALSE);
  414.       MainPaint(hwnd, PipeMsgs, index, sTotalMsgs);
  415.    }
  416.  
  417.    /* if lines exceed window */
  418.    if (sTotalMsgs > cyClient/cyChar)
  419.    {
  420.       WinEnableWindow (hwndVscroll, TRUE) ;
  421.       sVscrollMax = sTotalMsgs - cyClient / cyChar;
  422.       sVscrollPos = min (sVscrollPos, sVscrollMax) ;
  423.  
  424.       WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  425.                                MPFROM2SHORT (sVscrollPos, 0),
  426.                                MPFROM2SHORT (0, sVscrollMax)) ;
  427.  
  428.       /* if new msg is just off screen */
  429.       if (sTotalMsgs == cyClient/cyChar + sVscrollPos + 1)
  430.       {
  431.          MainVertScroll(hwnd, MPFROM2SHORT(0, SB_LINEDOWN));
  432.       }
  433.  
  434.       /* tell user message was received */
  435.       else
  436.       {
  437.          DosBeep(1000, 100);
  438.       }
  439.    }
  440. }
  441.  
  442.  
  443. /*
  444.  *  Empty Client window of messages
  445.  */
  446. VOID MainPurgeWindow()
  447. {
  448.  
  449.    sVscrollMax = 0;
  450.    sVscrollPos = 0;
  451.    WinSendMsg (hwndVscroll, SBM_SETSCROLLBAR,
  452.                             MPFROM2SHORT (sVscrollPos, 0),
  453.                             MPFROM2SHORT (0, sVscrollMax)) ;
  454.  
  455.    WinEnableWindow (hwndVscroll, FALSE) ;
  456.    WinInvalidateRect(hwndMain, NULL, FALSE);
  457. }
  458.