home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / drdc010.zip / CONTR010.ZIP / drusrctl.c < prev    next >
C/C++ Source or Header  |  2001-12-01  |  13KB  |  351 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. * DRUSRCTL.DLL                                                        *
  4. *                                                                     *
  5. * Copyright C. Wohlgemuth 2001                                        *
  6. *                                                                     *
  7. * This Rexx DLL contains a new percent bar control for use with       *
  8. * DrDialog.                                                           *
  9. *                                                                     *
  10. **********************************************************************/
  11. /* Include files */
  12.  
  13. #define  INCL_PM
  14. #define  INCL_WINSYS
  15. #define  INCL_DOS
  16. #define  INCL_DOSMISC
  17. #define  INCL_DOSNMPIPES
  18. #define  INCL_ERRORS
  19. #define  INCL_REXXSAA
  20. #define  _DLL
  21. #define  _MT
  22. #include <os2.h>
  23. #include <rexxsaa.h>
  24. #include <malloc.h>
  25. #include <fcntl.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <conio.h>
  31.  
  32. #define QWL_PERCENT 4 /* The location in the window words to store the percent value */
  33. #define QWL_TEXTPTR 8 /* The ptr to our percent bar text */
  34.  
  35. /*********************************************************************/
  36. /*  Declare all exported functions as REXX functions.                */
  37. /*********************************************************************/
  38.  
  39. RexxFunctionHandler DRCtrlDropFuncs;
  40. RexxFunctionHandler DRCtrlRegister;
  41. RexxFunctionHandler DRCtrlLoadFuncs;
  42.  
  43.  
  44. /*********************************************************************/
  45. /* RxFncTable                                                        */
  46. /*   Array of names of the REXXUTIL functions.                       */
  47. /*   This list is used for registration and deregistration.          */
  48. /*********************************************************************/
  49.  
  50. static PSZ  RxFncTable[] =
  51.    {
  52.       "DRCtrlRegister",
  53.       "DRCtrlLoadFuncs",
  54.       "DRCtrlDropFuncs",
  55.    };
  56.  
  57. /*********************************************************************/
  58. /* Numeric Error Return Strings                                      */
  59. /*********************************************************************/
  60.  
  61. #define  NO_UTIL_ERROR    "0"          /* No error whatsoever        */
  62. #define  ERROR_NOMEM      "2"          /* Insufficient memory        */
  63. #define  ERROR_FILEOPEN   "3"          /* Error opening text file    */
  64.  
  65. /*********************************************************************/
  66. /* Alpha Numeric Return Strings                                      */
  67. /*********************************************************************/
  68.  
  69. #define  ERROR_RETSTR   "ERROR:"
  70. #define  EMPTY_RETSTR   ""
  71.  
  72. /*********************************************************************/
  73. /* Numeric Return calls                                              */
  74. /*********************************************************************/
  75.  
  76. #define  INVALID_ROUTINE 40            /* Raise Rexx error           */
  77. #define  VALID_ROUTINE    0            /* Successful completion      */
  78.  
  79. /*********************************************************************/
  80. /* Some useful macros                                                */
  81. /*********************************************************************/
  82.  
  83. #define BUILDRXSTRING(t, s) { \
  84.   strcpy((t)->strptr,(s));\
  85.   (t)->strlength = strlen((s)); \
  86. }
  87.  
  88. /*
  89.  * Paint the percent bar and print the label if necessary.
  90.  */
  91.  
  92. static VOID _paintPercent(int iPercent, HWND hwnd, HPS hps)
  93. {
  94.     POINTL  ptl, ptlText, aptlText[TXTBOX_COUNT];
  95.     RECTL   rcl, rcl2;
  96.     BOOL    bVertical=FALSE;
  97.     CHAR  * ptrChr=NULL;
  98.  
  99.     WinQueryWindowRect(hwnd, &rcl);
  100.     /* Check if it's a vertical percent bar */
  101.     if(rcl.xRight<rcl.yTop)
  102.       bVertical=TRUE;
  103.     else
  104.       bVertical=FALSE;
  105.  
  106.     GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL);
  107.     
  108.     /* Draw the bar border */
  109.     WinDrawBorder(hps, &rcl, 1,1,0,0, 0x800);    
  110.     
  111.     rcl.xLeft = 1;
  112.     rcl.xRight -= 1;
  113.     rcl.yBottom = 1;
  114.     rcl.yTop -= 1;
  115.     
  116.     if((ptrChr=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
  117.       {
  118.         /* Text size */
  119.         GpiQueryTextBox(hps, strlen(ptrChr), ptrChr,
  120.                         TXTBOX_COUNT, (PPOINTL)&aptlText);
  121.    
  122.         ptlText.x = rcl.xLeft+(((rcl.xRight-rcl.xLeft)
  123.                                  -(aptlText[TXTBOX_BOTTOMRIGHT].x-aptlText[TXTBOX_BOTTOMLEFT].x))/2);
  124.         ptlText.y = 3 + rcl.yBottom+(((rcl.yTop-rcl.yBottom)
  125.                                       -(aptlText[TXTBOX_TOPLEFT].y-aptlText[TXTBOX_BOTTOMLEFT].y))/2);
  126.       }
  127.  
  128.     if(!bVertical) {
  129.       rcl2.xLeft = rcl.xLeft;
  130.       rcl2.xRight = (rcl.xRight-rcl.xLeft)*iPercent/100; 
  131.       rcl2.yBottom = rcl.yBottom;
  132.       rcl2.yTop = rcl.yTop-1;
  133.       rcl.xLeft=rcl2.xRight+1;
  134.     }
  135.     else {
  136.       rcl2.xLeft = rcl.xLeft;
  137.       rcl2.xRight = rcl.xRight-1;
  138.       rcl2.yBottom = rcl.yBottom;
  139.       rcl2.yTop = (rcl.yTop-rcl.yBottom)*iPercent/100; 
  140.       rcl.yBottom=rcl2.yTop+1;
  141.     }
  142.  
  143.     /* Background */
  144.     WinFillRect(hps, &rcl,
  145.                 WinQuerySysColor(HWND_DESKTOP, SYSCLR_DIALOGBACKGROUND, 0));
  146.  
  147.     /* Percentbar */
  148.     if ((rcl2.xRight > rcl2.xLeft && !bVertical)||(rcl2.yTop > rcl2.yBottom && bVertical)) {
  149.       ULONG ulBG;
  150.  
  151.       /* Find color. This color is the background color set within DrDialog */
  152.       if(!WinQueryPresParam(hwnd, PP_BACKGROUNDCOLOR, PP_BACKGROUNDCOLORINDEX, NULL, sizeof(ulBG),
  153.                         &ulBG, QPF_ID2COLORINDEX|QPF_NOINHERIT ))
  154.         ulBG=0x002020ff;
  155.       GpiSetColor(hps,ulBG );
  156.  
  157.       rcl2.yBottom+=1;
  158.       rcl2.xLeft+=1;
  159.  
  160.       WinFillRect(hps, &rcl2, ulBG);
  161.       WinDrawBorder(hps, &rcl2, 1,1,0,0, 0x400);
  162.     }
  163.  
  164.     /* now print the percentage */
  165.     if(ptrChr!=NULLHANDLE)
  166.       {
  167.         ULONG ulFG; 
  168.        
  169.         /* Find color. This color is the foreground color set within DrDialog */
  170.         if(!WinQueryPresParam(hwnd, PP_FOREGROUNDCOLOR, PP_FOREGROUNDCOLORINDEX, NULL, sizeof(ulFG),
  171.                               &ulFG, QPF_ID2COLORINDEX|QPF_NOINHERIT ))
  172.           ulFG=WinQuerySysColor(HWND_DESKTOP, SYSCLR_BUTTONDEFAULT, 0);
  173.         GpiSetColor(hps,ulFG );
  174.         GpiMove(hps, &ptlText);
  175.         GpiCharString(hps, strlen(ptrChr), ptrChr);
  176.       }
  177. }
  178.  
  179.  
  180. /*
  181.  * This is the window proc for the percentbar control
  182.  */
  183.  
  184. static MRESULT EXPENTRY _percentBarProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  185. {
  186.   MRESULT mrc;
  187.   HPS hps;
  188.   PWNDPARAMS pwp;
  189.   int iPercent;
  190.   RECTL rcl;
  191.  
  192.   switch(msg) {
  193.  
  194.   case WM_SETWINDOWPARAMS:
  195.     {
  196.       pwp=(PWNDPARAMS)mp1;
  197.       if(pwp->fsStatus==WPM_TEXT) {
  198.         /* The text changed */
  199.         char *ptr;
  200.         char *ptr2;
  201.  
  202.         /* Get the current percent value for the control */
  203.         iPercent=atol(pwp->pszText);
  204.         if(iPercent>100)
  205.           iPercent=100;
  206.         if(iPercent<0)
  207.           iPercent=0;
  208.  
  209.         /* Check if there is some text for the bar */
  210.         if((ptr=strchr(pwp->pszText, '#'))!=NULLHANDLE) {
  211.           /* Everything after the '#' is treated as the label */
  212.           if((ptr2=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
  213.             free(ptr2); /* Free the old text */
  214.           WinSetWindowPtr(hwnd,QWL_TEXTPTR, NULLHANDLE);
  215.           if(*(ptr++)!=0) {
  216.             /* There's additional text to print */
  217.             if((ptr2=malloc(strlen(ptr)))!=NULLHANDLE) {
  218.               strcpy(ptr2,ptr);
  219.               WinSetWindowPtr(hwnd,QWL_TEXTPTR,ptr2);
  220.             }
  221.           }
  222.         }
  223.         mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
  224.         WinSetWindowULong(hwnd, QWL_PERCENT,iPercent);
  225.         WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
  226.         return mrc;
  227.       }
  228.       break;
  229.     }
  230.   case WM_DESTROY:
  231.     {
  232.       char *ptrText;
  233.       /* Free the memory allocated for the text */
  234.       if((ptrText=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
  235.         free(ptrText);
  236.       break;
  237.     }
  238.   case WM_PRESPARAMCHANGED:
  239.     /* The color or the font has changed  */
  240.     /* Force a repaint of the percent bar */
  241.     mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
  242.     if(LONGFROMMP(mp1)==PP_FOREGROUNDCOLOR)
  243.       WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
  244.     else if(LONGFROMMP(mp1)==PP_BACKGROUNDCOLOR)
  245.       WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
  246.     else if(LONGFROMMP(mp1)==PP_FONTNAMESIZE)
  247.       WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
  248.     return mrc;
  249.   case WM_PAINT:
  250.     {
  251.       hps=WinBeginPaint(hwnd, NULLHANDLE, NULLHANDLE);
  252.       _paintPercent(WinQueryWindowULong(hwnd,QWL_PERCENT), hwnd, hps);
  253.       WinEndPaint(hps);
  254.     return (MRESULT) FALSE;
  255.     }
  256.   
  257.   default:
  258.     break;
  259.   }
  260.  
  261.   mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
  262.   return (mrc);
  263. }
  264.  
  265.  
  266.  
  267. /*************************************************************************
  268. * Function:  DRCtrlDropFuncs                                             *
  269. *                                                                        *
  270. * Syntax:    call DRCtrlDropFuncs                                        *
  271. *                                                                        *
  272. * Params:    none                                                        *
  273. *                                                                        *
  274. * Return:    null string                                                 *
  275. *************************************************************************/
  276.  
  277. ULONG DRCtrlDropFuncs(CHAR *name, ULONG numargs, RXSTRING args[],
  278.                           CHAR *queuename, RXSTRING *retstr)
  279. {
  280.   INT     entries;                     /* Num of entries             */
  281.   INT     j;                           /* Counter                    */
  282.  
  283.   if (numargs != 0)                    /* no arguments for this      */
  284.     return INVALID_ROUTINE;            /* raise an error             */
  285.  
  286.   retstr->strlength = 0;               /* return a null string result*/
  287.  
  288.   entries = sizeof(RxFncTable)/sizeof(PSZ);
  289.  
  290.   for (j = 0; j < entries; j++) {
  291.  
  292.     printf(RxFncTable[j]);
  293.     RexxDeregisterFunction(RxFncTable[j]);
  294.   }
  295.   return VALID_ROUTINE;                /* no error on call           */
  296. }
  297.  
  298.  
  299. /*************************************************************************
  300. *                                                                        *
  301. * Register the percent bar control with the calling PM process           *
  302. *                                                                        *
  303. *************************************************************************/
  304.  
  305. ULONG DRCtrlRegister(CHAR *name, ULONG numargs, RXSTRING args[],
  306.                      CHAR *queuename, RXSTRING *retstr)
  307. {
  308.  
  309.   WinRegisterClass(WinQueryAnchorBlock(HWND_DESKTOP),"DRD_PERCENTBAR",_percentBarProc,0L,12);
  310.  
  311.   return VALID_ROUTINE;                /* no error on call           */
  312. }
  313.  
  314.  
  315. /*************************************************************************
  316. * Function:  DRCtrlLoadFuncs                                             *
  317. *                                                                        *
  318. * Syntax:    call DRCtrlLoadFuncs                                        *
  319. *                                                                        *
  320. * Params:    none                                                        *
  321. *                                                                        *
  322. * Return:    null string                                                 *
  323. *************************************************************************/
  324.  
  325. ULONG DRCtrlLoadFuncs(CHAR *name, ULONG numargs, RXSTRING args[],
  326.                            CHAR *queuename, RXSTRING *retstr)
  327. {
  328.   INT    entries;                      /* Num of entries             */
  329.   INT    j;                            /* Counter                    */
  330.  
  331.   retstr->strlength = 0;               /* set return value           */
  332.                                        /* check arguments            */
  333.  
  334.   if (numargs > 0)
  335.     return INVALID_ROUTINE;
  336.  
  337.   entries = sizeof(RxFncTable)/sizeof(PSZ);
  338.  
  339.   for (j = 0; j < entries; j++) {
  340.  
  341.     printf(RxFncTable[j]);
  342.     printf(": ");
  343.     RexxRegisterFunctionDll(RxFncTable[j],
  344.           "DRUSRCTL", RxFncTable[j]);
  345.   }
  346.  
  347.   return VALID_ROUTINE;
  348. }
  349.  
  350.  
  351.