home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 24 / CDACTUAL24.iso / SHARE / os2 / edm2 / common / snippets / pcbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  9.1 KB  |  269 lines

  1. /******************************************************************************
  2.     Painting a percent bar
  3.  
  4.     Please split this file into the various bits
  5.     before trying to use it!
  6. ******************************************************************************/
  7.  
  8.  
  9. /******************************************************************************
  10.     The header file....
  11.     pcbar.h
  12. ******************************************************************************/
  13.  
  14. //all this ifdef stuff prevents you from getting an error if you include the
  15. //header several times...
  16.  
  17. #ifndef PCBAR_DRAW_FUNCTION
  18.     #define PCBAR_DRAW_FUNCTION
  19.  
  20. //the dialog resource ID's
  21. #define     IDD_PCBARDIALOG     100
  22. #define     IDD_PERCENTBAR      101
  23.  
  24. //the bar color
  25. #define     USED_COLOR  CLR_RED
  26.  
  27. //the rest of the bar
  28. #define     FREE_COLOR  CLR_DARKBLUE
  29.  
  30. //export the function prototype
  31. void        StartPercentbar(HWND hWnd);
  32. void        StopPercentbar(void);
  33. void        SetPercentbar(ULONG curpos, ULONG total);
  34.  
  35.  
  36. #endif
  37.  
  38. /******************************************************************************
  39.     The dialog file....
  40.     pcbar.dlg
  41. ******************************************************************************/
  42. #include    <os2.h>
  43. #include    "pcbar.h"
  44. DLGTEMPLATE IDD_PCBARDIALOG LOADONCALL MOVEABLE DISCARDABLE
  45. BEGIN
  46.     DIALOG  "Percent bar", IDD_PCBARDIALOG, 97, 26, 138, 22, WS_VISIBLE, FCF_TITLEBAR
  47.     BEGIN
  48.         CTEXT         "", IDD_PERCENTBAR, 6, 6, 126, 10, DT_VCENTER
  49.     END
  50. END
  51.  
  52. /******************************************************************************
  53.     The C source file
  54.     pcbar.c
  55. ******************************************************************************/
  56. #define         INCL_WIN
  57. #define         INCL_GPI
  58. #define         INCL_DOS
  59.  
  60. #include        <os2.h>
  61. #include        <stdio.h>
  62. #include        <string.h>
  63. #include        <stdlib.h>
  64. #include        "pcbar.h"
  65.  
  66. /***
  67. This code uses _beginthread() from the IBM VisualAge C++ stdlib.h library.
  68. You can use the DosCreateThread() API if you also use DosExit() when done.
  69. ***/
  70.  
  71. #define DB_RAISED       0x0400
  72. #define DB_DEPRESSED    0x0800
  73.  
  74. /**** local functions ****/
  75. void                DrawPCbar(HWND hWnd, ULONG ID, ULONG curpos, ULONG total);
  76. void _Optlink       pcbarThread(void* handle);
  77. MRESULT EXPENTRY    pcbarDlgProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  78.  
  79. /**** Percent bar ****/
  80. HWND            hWndPercentbar;
  81. HWND            hWndpcbardlg;
  82. BOOL            pcbarDialog = FALSE;
  83. int             pcbarTID = 0;
  84. HAB             hab;
  85.  
  86. /******************************************************************************
  87.     Start percent bar
  88. ******************************************************************************/
  89. void StartPercentbar(HWND hWnd)
  90.     {
  91.         ULONG   x = 100;
  92.         pcbarTID = _beginthread(pcbarThread,NULL,32768,(void*)hWnd);
  93.         while(!pcbarDialog)
  94.             {
  95.                 DosSleep(1);
  96.                 x--;
  97.                 //exit rather than hang...
  98.                 if(!x) break;
  99.             }
  100.     }
  101.  
  102. /******************************************************************************
  103.     Stop percent bar
  104. ******************************************************************************/
  105. void StopPercentbar(void)
  106.     {
  107.         if(pcbarDialog)
  108.             {
  109.                 pcbarDialog = FALSE;
  110.                 DosWaitThread((PTID)&pcbarTID, DCWW_WAIT);
  111.             }
  112.     }
  113.  
  114. /******************************************************************************
  115.     Set percent bar
  116. ******************************************************************************/
  117. void SetPercentbar(ULONG curpos, ULONG total)
  118.     {
  119.         if(pcbarDialog) WinPostMsg( hWndpcbardlg,
  120.                                     WM_USER,
  121.                                     (MPARAM)curpos,
  122.                                     (MPARAM)total);
  123.     }
  124.  
  125. /******************************************************************************
  126.     Percent Bar thread
  127. ******************************************************************************/
  128. void _Optlink       pcbarThread(void* handle)
  129.     {
  130.         QMSG        qmsg;
  131.         HWND        hWnd = (HWND)handle;
  132.         HMQ         hmqpc;
  133.  
  134.         hab = WinInitialize(0L);
  135.         hmqpc = WinCreateMsgQueue(hab, 0L);
  136.         hWndpcbardlg = WinLoadDlg(HWND_DESKTOP, (HWND)handle, pcbarDlgProc, 0L, IDD_PCBARDIALOG, NULL);
  137.         pcbarDialog = TRUE;
  138.         do
  139.             {
  140.                 while (WinPeekMsg (hab, &qmsg, 0, 0, 0,PM_REMOVE))
  141.                     {
  142.                         WinDispatchMsg (hab, &qmsg);
  143.                     }
  144.                 DosSleep(1);
  145.             }
  146.         while(pcbarDialog);
  147.  
  148.         hWndpcbardlg = 0;
  149.         WinDestroyMsgQueue(hmqpc);
  150.         hmqpc = 0;
  151.         WinTerminate(hab);
  152.     }
  153.  
  154. /******************************************************************************
  155.     Percent Bar dialog
  156. ******************************************************************************/
  157. MRESULT EXPENTRY pcbarDlgProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  158.     {
  159.         BOOL            bHandled = TRUE;
  160.         MRESULT         mReturn  = 0;
  161.         ULONG           ulScrWidth, ulScrHeight;
  162.         ULONG           temp, x;
  163.         static ULONG    oldrc;
  164.         static RECTL    Rectl;
  165.         static SHORT    dtsize;
  166.         HPS             hps;
  167.  
  168.         switch (msg)
  169.             {
  170.                 case WM_INITDLG:
  171.                     /* Center dialog on screen */
  172.                     ulScrWidth  = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
  173.                     ulScrHeight = WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
  174.                     WinQueryWindowRect (hWnd, &Rectl);
  175.                     WinSetWindowPos (hWnd, HWND_TOP, (ulScrWidth-Rectl.xRight)/2,
  176.                     (ulScrHeight-Rectl.yTop)/2, 0, 0, SWP_MOVE | SWP_ACTIVATE);
  177.                     break;
  178.                 //use WM_USER to set the bar
  179.                 case WM_USER:
  180.                     DrawPCbar(hWnd, IDD_PERCENTBAR, (ULONG)mp1, (ULONG)mp2);
  181.                     break;
  182.                 default:
  183.                     bHandled = FALSE;
  184.                     break;
  185.             }
  186.         if (!bHandled) mReturn = WinDefDlgProc (hWnd, msg, mp1, mp2);
  187.         return(mReturn);
  188.     }
  189.  
  190. /******************************************************************************
  191.     Draw a precent bar
  192.     This call will draw a percent bar into a text dialog item.
  193.     It will show curpos percent of the total.
  194.  
  195.     hWnd    The dialog (parent) window handle
  196.     ID      The control ID
  197.     curpos  The fraction
  198.     total   The total
  199.  
  200.     The values are automatically normalized to percent
  201. ******************************************************************************/
  202. void    DrawPCbar(HWND hWnd, ULONG ID, ULONG curpos, ULONG total)
  203.     {
  204.         HWND        hWndpc;         //the percent bar window handle
  205.         RECTL       Rectl;          //the percent bar rectangle
  206.         HPS         hps;            //presentation space handle
  207.         ULONG       pc;             //percentage
  208.         ULONG       rface;          //the left and right faces of the percent bar
  209.         ULONG       lface;          //
  210.         ULONG       maxsz;          //the full size of the percent bar
  211.         char        string[12];     //the percent bar text
  212.  
  213.         //get the percent bar window handle and presentation space handle
  214.         hWndpc = WinWindowFromID(hWnd, ID);
  215.         hps = WinGetPS(hWndpc);
  216.  
  217.         //get the percent bar window rectangle
  218.         WinQueryWindowRect (hWndpc, &Rectl);
  219.  
  220.         //give a raised button appearance
  221.         WinDrawBorder(hps, &Rectl, 1L, 1L, SYSCLR_BUTTONDARK, SYSCLR_BUTTONMIDDLE, DB_RAISED);
  222.         WinInflateRect(hab, &Rectl, -1L, -1L);
  223.  
  224.         //check incoming parameters
  225.         if(curpos > total) curpos = total;
  226.         if(!total)
  227.             {
  228.                 //report that the total equals zero
  229.                 sprintf(string, "Total = 0 ?", pc);
  230.                 WinDrawText(hps, strlen(string), string, &Rectl, 0, 0, DT_TEXTATTRS |DT_VCENTER | DT_CENTER);
  231.                 WinReleasePS(hps);
  232.                 return;
  233.             }
  234.  
  235.         //calculate precentage
  236.         pc = (curpos * 100) /total;
  237.         sprintf(string, "%d%%", pc);
  238.  
  239.         //get left and right rectangle face
  240.         lface = Rectl.xLeft;
  241.         rface = maxsz = Rectl.xRight - Rectl.xLeft;
  242.  
  243.         //set rface to equal the split between left and right color areas
  244.         rface *= curpos;
  245.         rface /= total;
  246.         if(rface > maxsz) rface = maxsz;
  247.  
  248.         //paint area on left
  249.         Rectl.xRight = Rectl.xLeft + rface;
  250.         WinFillRect(hps, &Rectl, USED_COLOR);
  251.  
  252.         //get remaining area
  253.         Rectl.xLeft = Rectl.xRight;
  254.         Rectl.xRight = maxsz;
  255.  
  256.         //and fill
  257.         WinFillRect(hps, &Rectl, FREE_COLOR);
  258.  
  259.         //restore rectangle
  260.         Rectl.xLeft = lface;
  261.  
  262.         //xor text to ensure it is always visible
  263.         GpiSetMix(hps, FM_NOTXORSRC);
  264.         WinDrawText(hps, strlen(string), string, &Rectl, 0, 0, DT_TEXTATTRS |DT_VCENTER | DT_CENTER);
  265.  
  266.         //and return
  267.         WinReleasePS(hps);
  268.     }
  269.