home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / timup13.zip / source.zip / vmem.c < prev    next >
C/C++ Source or Header  |  1993-06-21  |  16KB  |  503 lines

  1.  
  2. /* VMEM.C - Copyright (c) 1992 M.B.Mallory
  3. // Compiler: IBM C Set/2 1.0 
  4.  
  5. // VMEM displays the amount of virtual memory available to the user
  6. // in the smallest window possible... a title bar. VMEM can run along
  7. // the edge of the screen (or under it's own icon) with no loss of 
  8. // screen real estate.*/
  9.  
  10. #define INCL_WIN
  11. #define INCL_GPIPRIMITIVES
  12. #define INCL_DOSMISC
  13.  
  14. #include <os2.h>
  15. #include <string.h>
  16. #include "vmem.h"
  17.  
  18. #define CMD (COMMANDMSG(&msg)->cmd)
  19.  
  20.  
  21. /* the following  3 lines were added by Ronald Van Iwaarden, 05/03/93 so
  22.    that this program would compile under EMX 0.8f */
  23. typedef LHANDLE HINI;
  24. typedef HINI *PHINI;
  25. #define HINI_USERPROFILE   (HINI) -1L
  26.  
  27.  
  28.  
  29. /* data structure and profile info to be written to OS2.INI */
  30. typedef struct
  31.    {
  32.    POINTL pt;
  33.    SHORT savescrpos;
  34.    SHORT format;
  35.    } INIINFO;
  36.  
  37. #define MEM_APP      "VMEM"
  38. #define MEM_KEY      "OptionsData"
  39.  
  40. /* define items to remove from the System menu*/
  41. #define MIR_COUNT    5
  42. #define MIR_DATA     SC_RESTORE, SC_SIZE, SC_HIDE, SC_MINIMIZE, SC_MAXIMIZE
  43.  
  44. /* define additions to the System menu*/
  45. #define MI_COUNT     4
  46. #define IDM_ABOUT    88
  47. #define IDM_OPTIONS  89
  48. #define MI_STR       0, 0, "~About  VMEM", "VMEM  ~Options"
  49. #define MI_DATA      MIT_END, MIS_SEPARATOR, 0, 0, 0, 0,\
  50.                      MIT_END, MIS_SEPARATOR, 0, 0, 0, 0,\
  51.                      MIT_END, MIS_TEXT, 0, IDM_ABOUT, 0, 0,\
  52.                      MIT_END, MIS_TEXT, 0, IDM_OPTIONS, 0, 0
  53.  
  54. /* initial title bar text*/
  55. #define TITLEBAR_TEXT "Virtual Memory"
  56.  
  57. /* no timers error message*/
  58. #define TIMER_TITLE  "Error initializing  VMEM"
  59. #define TIMER_TEXT   "VMEM cannot be initialized because"\
  60.                      " too many timers are already running."\
  61.                      " Close some applications to free system resources."
  62.  
  63. /* prototype for revectored (subclassed) system frame procedure*/
  64. MRESULT (* EXPENTRY DefaultFrameProc) (HWND, ULONG, MPARAM, MPARAM);
  65.  
  66. /* prototypes for new frame & dialog procedures*/
  67. MRESULT EXPENTRY NewFrameProc (HWND, ULONG, MPARAM, MPARAM);
  68. MRESULT EXPENTRY AboutProc (HWND, ULONG, MPARAM, MPARAM);
  69. MRESULT EXPENTRY OptionsProc (HWND, ULONG, MPARAM, MPARAM);
  70. MRESULT EXPENTRY HelpProc (HWND, ULONG, MPARAM, MPARAM);
  71.  
  72. /* misc. prototypes*/
  73. VOID ReviseSystemMenu (VOID);
  74. VOID SetFramePosition (PRECTL);
  75. LONG GetMemAvail (VOID);
  76. BOOL GetIniInfo (VOID);
  77. /* Commented out by Ronald Van Iwaarden, 05/03/93
  78.    CHAR * _System ULongToString (LONG number, LONG width, LONG dec_places);*/
  79. void _setuparg (void);
  80.  
  81.  
  82.  
  83. CHAR * ULongToString (LONG number, LONG width, LONG dec_places)
  84.  
  85. /* this was added by Ronald Van Iwaarden on 05/03/93 since it is not an
  86.    included call in EMX 0.8f.  I do not make any claims as to the quality
  87.    of this piece of code and neither do I claim that it models the code
  88.    in the icc compiler very well.
  89. */
  90.  
  91. {
  92.   static char str[12];
  93.   LONG pwrof10 = 1;
  94.   int temp;
  95.  
  96.   while(pwrof10<number) pwrof10 = pwrof10 * 10;
  97.   strcpy(str,"");
  98.   while (pwrof10 > 1){
  99.     if (dec_places == (int) log(pwrof10)/LOG_10) strcat(str,".");
  100.     if ((strlen(str) > 0) && (3 == (int) log(pwrof10)/LOG_10)) strcat(str,",");
  101.     if ((strlen(str) > 0) && (6 == (int) log(pwrof10)/LOG_10)) strcat(str,",");
  102.     if ((strlen(str) > 0) && (9 == (int) log(pwrof10)/LOG_10)) strcat(str,",");
  103.     pwrof10 = pwrof10/10;
  104.     temp=number/pwrof10;
  105.     switch(temp){
  106.     case 0: strcat(str,"0"); break;
  107.     case 1: strcat(str,"1"); break;
  108.     case 2: strcat(str,"2"); break;
  109.     case 3: strcat(str,"3"); break;
  110.     case 4: strcat(str,"4"); break;
  111.     case 5: strcat(str,"5"); break;
  112.     case 6: strcat(str,"6"); break;
  113.     case 7: strcat(str,"7"); break;
  114.     case 8: strcat(str,"8"); break;
  115.     case 9: strcat(str,"9"); break;
  116.     }
  117.     number = number - pwrof10*(temp);
  118.   };
  119.   return str;
  120. };
  121.  
  122.  
  123.  
  124.  
  125. /* globals*/
  126. HWND hwndFrame, hwndTitleBar;
  127. INIINFO ini;
  128. BOOL bRestoreFlag = FALSE;
  129. LONG lPrevMemAvail;
  130.  
  131. /* declare dummy function to eliminate command line parsing */
  132. void _setuparg (void)  
  133.     {}
  134.  
  135. int main (void)
  136.     {
  137.     HAB hab;
  138.     HMQ hmq;
  139.     QMSG qmsg;
  140.     RECTL rect;
  141.  
  142.     FRAMECDATA framecdata = {
  143.             sizeof framecdata,
  144.             FCF_TITLEBAR | FCF_SYSMENU | FCF_TASKLIST | FCF_ICON | FCF_BORDER,
  145.             0,
  146.             VMEM_ICON };
  147.  
  148.     hab = WinInitialize (0);            /* create anchor block*/
  149.     hmq = WinCreateMsgQueue (hab, 0);   /* create message queue*/
  150.  
  151.     /* initialize handle & create frame window*/
  152.     hwndFrame = WinCreateWindow (
  153.             HWND_DESKTOP,           /* window parent */
  154.             WC_FRAME,               /* frame window class */
  155.             TITLEBAR_TEXT,          /* window text */
  156.             0,                      /* frame creation style (invisible) */
  157.             0,0,                    /* x, y position */
  158.             0,0,                    /* width, height */
  159.             0,                      /* owner window */
  160.             HWND_TOP,               /* top of z-order */
  161.             ID_FRAME,               /* frame window ID */
  162.             &framecdata,            /* control data */
  163.             0);                     /* presentation parameters */
  164.  
  165.     /* initialize handle to title bar*/
  166.     hwndTitleBar = WinWindowFromID (hwndFrame, FID_TITLEBAR);
  167.  
  168.     /* hook the vector to this instance of the system frame procedure*/
  169.     DefaultFrameProc = WinSubclassWindow (hwndFrame, (PFNWP) NewFrameProc);
  170.  
  171.     ReviseSystemMenu ();   /* delete disabled stuff & add new items*/
  172.  
  173.     SetFramePosition (&rect);    /* initial window frame position*/
  174.  
  175.     WinSetWindowPos (hwndFrame, HWND_TOP, rect.xLeft, rect.yBottom,
  176.             rect.xRight, rect.yTop + 1, SWP_SIZE | SWP_MOVE | SWP_SHOW);
  177.  
  178.     if (WinStartTimer (hab, hwndFrame, ID_TIMER, 1000))
  179.         /* enter message loop*/
  180.         {
  181.         while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  182.             WinDispatchMsg (hab, &qmsg);
  183.  
  184.         WinStopTimer (hab, hwndFrame, ID_TIMER);
  185.         }
  186.  
  187.     else    /* display error message*/
  188.         WinMessageBox (HWND_DESKTOP, hwndFrame, TIMER_TEXT, TIMER_TITLE, 0,
  189.                 MB_OK | MB_WARNING | MB_MOVEABLE );
  190.  
  191.     /* destroy window frame and exit*/
  192.     WinDestroyWindow (hwndFrame);
  193.     WinDestroyMsgQueue (hmq);
  194.     WinTerminate (hab);
  195.     return 0;
  196.     }
  197.  
  198.  
  199. VOID ReviseSystemMenu (VOID)
  200.     {
  201.     HWND hwndSysMenu;
  202.     MENUITEM miSysMenu;
  203.     SHORT sSysMenu, i;
  204.     static PCHAR pItemIns[MI_COUNT]  = { MI_STR };
  205.     static SHORT sItemDel[MIR_COUNT] = { MIR_DATA };
  206.     static MENUITEM mi[MI_COUNT]     = { MI_DATA };
  207.  
  208.     /* get handle of the system menu*/
  209.     hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
  210.  
  211.     /* get ID of the system menu bitmap*/
  212.     sSysMenu = SHORT1FROMMR (
  213.             WinSendMsg (hwndSysMenu, MM_ITEMIDFROMPOSITION, 0, 0));
  214.  
  215.     /* get handle of system submenu (in miSysMenu structure)*/
  216.     WinSendMsg (hwndSysMenu, MM_QUERYITEM,
  217.             MPFROM2SHORT (sSysMenu, FALSE), MPFROMP (&miSysMenu));
  218.  
  219.     /* remove disabled and unneeded menu items*/
  220.     for (i = 0; i < MIR_COUNT; i++)
  221.         WinSendMsg (miSysMenu.hwndSubMenu, MM_REMOVEITEM,
  222.                 MPFROM2SHORT (sItemDel[i], FALSE), 0);
  223.  
  224.     /* insert new menu items*/
  225.     for (i = 0; i < MI_COUNT; i++)
  226.         WinSendMsg (miSysMenu.hwndSubMenu, MM_INSERTITEM,
  227.                 MPFROMP (mi + i), MPFROMP (pItemIns[i]));
  228.     }
  229.  
  230.  
  231. LONG GetMemAvail (VOID)
  232.     {
  233.     LONG X;
  234.     DosQuerySysInfo (19, 19, &X, sizeof X); /* get available virtual memory*/
  235.     return X;
  236.     }
  237.  
  238.  
  239. BOOL GetIniInfo (VOID)  /* get data from OS2.INI, if it exists*/
  240.     {
  241.     ULONG length = sizeof ini;
  242.     HINI hini = HINI_USERPROFILE;
  243.     BOOL rval;
  244.     rval = PrfQueryProfileData (hini, MEM_APP, MEM_KEY, &ini, &length);
  245.     if (!rval)
  246.         {
  247.         ini.savescrpos  = TRUE;     /* set defaults*/
  248.         ini.format  = ARB_DISPLAYMB;
  249.         }
  250.     return rval;
  251.     }
  252.  
  253.  
  254. VOID SetFramePosition (PRECTL prect)
  255.     {
  256.     CHAR pPtr[30];
  257.     HPS hps ;
  258.     POINTL apt[TXTBOX_COUNT];
  259.     SHORT xMax, yMax;
  260.  
  261.     /* produce initial string*/
  262.     strcpy (pPtr, ULongToString (GetMemAvail () / ONE_KB, 0, 0));
  263.     strcat (pPtr, "   K available    ");
  264.     
  265.     /* get length of text string*/
  266.     hps = WinGetPS (hwndFrame);
  267.     GpiQueryTextBox (hps, strlen (pPtr), pPtr, TXTBOX_COUNT, apt);
  268.     WinReleasePS (hps);
  269.  
  270.     /* put text length + room for system menu into RECTL */
  271.     prect->xLeft = prect->yBottom = prect->yTop = 0;
  272.     prect->xRight = (apt[2].x - apt[0].x) + (apt[0].y - apt[1].y);
  273.  
  274.     /* expand text string to frame coordinates*/
  275.     WinCalcFrameRect (hwndFrame, prect, FALSE);
  276.  
  277.     /* get current absolute screen size*/
  278.     xMax = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
  279.     yMax = WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN);
  280.  
  281.     if (GetIniInfo ())
  282.         {
  283.         /* set window position to OS2.INI data*/
  284.         prect->xLeft = ini.pt.x;
  285.         prect->yBottom = ini.pt.y;
  286.  
  287.         /* logic to ensure window displays on current screen size*/
  288.         if ((prect->xLeft + prect->xRight / 2 - 3) > xMax)
  289.             prect->xLeft = xMax - prect->xRight / 2 + 3;
  290.         if (prect->xLeft + 3 < 0)
  291.             prect->xLeft = -3;
  292.         if ((prect->yBottom + prect->yTop - 3) > yMax)
  293.             prect->yBottom = yMax - prect->yTop + 3;
  294.         if (prect->yBottom + 3 < 0)
  295.             prect->yBottom = -3;
  296.         }
  297.     else
  298.         {
  299.         /* set window position to center of screen (default)*/
  300.         prect->xLeft = (xMax - prect->xRight) / 2;
  301.         prect->yBottom = (yMax - prect->yTop) / 2;
  302.         }
  303.  
  304.     /* set INIINFO structure to current values*/
  305.     ini.pt.x = prect->xLeft;
  306.     ini.pt.y = prect->yBottom;
  307.     }
  308.  
  309.  
  310. MRESULT EXPENTRY 
  311.     NewFrameProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  312.     {
  313.     CHAR pTempText[40];
  314.     HINI hini = HINI_USERPROFILE;
  315.     POINTL pt;
  316.     LONG lTemp, lMemAvail;
  317.     MRESULT rc;
  318.  
  319.     switch (msg)
  320.         {
  321.         case WM_TIMER:
  322.             lMemAvail = GetMemAvail ();
  323.             if (lMemAvail != lPrevMemAvail)
  324.                 {
  325.                 if (lMemAvail > 0)
  326.                     {
  327.                     if (ini.format == ARB_DISPLAYMB)
  328.                         {
  329.                         lTemp = lMemAvail / TENTH_MB;
  330.                         if (lMemAvail % TENTH_MB > TENTH_MB / 2)
  331.                             lTemp++;
  332.                         strcpy (pTempText, ULongToString (lTemp, 0, 1));
  333.                         strcat (pTempText, " MB available");
  334.                         }
  335.                     else
  336.                         {
  337.                         lTemp = lMemAvail / ONE_KB;
  338.                         strcpy (pTempText, ULongToString (lTemp, 0, 0));
  339.                         strcat (pTempText, " K available");
  340.                         }
  341.                     }
  342.                 else
  343.                     strcpy (pTempText, "Memory Depleted.") ;
  344.  
  345.                 WinSetWindowText (hwndFrame, pTempText);
  346.                 lPrevMemAvail = lMemAvail;
  347.                 }
  348.             return 0 ;
  349.  
  350.         case WM_PAINT:
  351.         case WM_ACTIVATE:
  352.             /* set title bar hilite to always ON*/
  353.             rc = DefaultFrameProc (hwnd, msg, mp1, mp2);
  354.             WinSendMsg (hwndTitleBar, TBM_SETHILITE, (MPARAM) TRUE, 0);
  355.             return rc;
  356.  
  357.         case WM_COMMAND:
  358.             switch (CMD)
  359.                 {
  360.                 case IDM_ABOUT:     /* open About dialog box*/
  361.                     WinDlgBox (HWND_DESKTOP, hwnd, 
  362.                             (PFNWP) AboutProc, 0, IDD_ABOUT, 0);    
  363.                     return 0;
  364.  
  365.                 case IDM_OPTIONS:   /* open Options dialog box*/
  366.                     WinDlgBox (HWND_DESKTOP, hwnd, 
  367.                             (PFNWP) OptionsProc, 0, IDD_OPTIONS, 0);
  368.                     return 0;
  369.                 }
  370.             break;
  371.  
  372.         case WM_BUTTON2DBLCLK:
  373.             /* activate options dialog box with double-click of mouse*/
  374.             /*  button 2 on the title bar*/
  375.             WinDlgBox (HWND_DESKTOP, hwnd, 
  376.                     (PFNWP) OptionsProc, 0, IDD_OPTIONS, 0);
  377.             return 0;
  378.  
  379.         case WM_DESTROY:
  380.             if (bRestoreFlag)
  381.                 /* delete data from OS2.INI*/
  382.                 PrfWriteProfileData (hini, MEM_APP, 0, 0, 0);
  383.             else
  384.                 {
  385.                 if (ini.savescrpos)
  386.                     {
  387.                     /* get current screen position*/
  388.                     pt.x = pt.y = 0;
  389.                     WinMapWindowPoints (hwndFrame, HWND_DESKTOP, &pt, 1);
  390.                     ini.pt.x = pt.x;
  391.                     ini.pt.y = pt.y;
  392.                     }
  393.  
  394.                 /* write current settings to OS2.INI*/
  395.                 PrfWriteProfileData (hini, MEM_APP, MEM_KEY, &ini, (ULONG)sizeof ini);
  396.                 }
  397.         }
  398.     return DefaultFrameProc (hwnd, msg, mp1, mp2);
  399.     }
  400.  
  401.  
  402. MRESULT EXPENTRY 
  403.     AboutProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  404.     {
  405.     if (msg == WM_COMMAND)
  406.         if (CMD == DID_OK || CMD == DID_CANCEL)
  407.             {
  408.             WinDismissDlg (hwnd, TRUE);
  409.             return 0;
  410.             }
  411.     return WinDefDlgProc (hwnd, msg, mp1, mp2);
  412.     }
  413.  
  414.  
  415. MRESULT EXPENTRY 
  416.     HelpProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  417.     {
  418.     if (msg == WM_COMMAND)
  419.         if (CMD == DID_OK || CMD == DID_CANCEL)
  420.             {
  421.             WinDismissDlg (hwnd, TRUE);
  422.             return 0;
  423.             }
  424.     return WinDefDlgProc (hwnd, msg, mp1, mp2);
  425.     }
  426.  
  427.  
  428. MRESULT EXPENTRY 
  429.     OptionsProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  430.     {
  431.     SHORT sTemp;
  432.  
  433.     if (msg == WM_CONTROL)
  434.         {
  435.         switch (SHORT1FROMMP (mp1)) /* get settings as they are changed*/
  436.             {
  437.             case ACB_SAVESCRPOS:
  438.                 if (!bRestoreFlag)
  439.                     {
  440.                     ini.savescrpos ^= TRUE; /* toggle flag*/
  441.                     WinSendDlgItemMsg (hwnd, ACB_SAVESCRPOS, BM_SETCHECK,
  442.                             (MPARAM) ini.savescrpos, 0);
  443.                     }
  444.                 return 0;
  445.  
  446.             case ACB_RESTORE:
  447.                 bRestoreFlag ^= TRUE; /* toggle flag*/
  448.                 if (bRestoreFlag)
  449.                     WinSendDlgItemMsg (hwnd, ACB_SAVESCRPOS, BM_SETCHECK,
  450.                             (MPARAM) CB_3RDSTATE, 0);
  451.                 else
  452.                     WinSendDlgItemMsg (hwnd, ACB_SAVESCRPOS, BM_SETCHECK,
  453.                             (MPARAM) ini.savescrpos, 0);
  454.                 return 0;
  455.  
  456.             case ARB_DISPLAYMB:
  457.             case ARB_DISPLAYKB:
  458.                 ini.format = SHORT1FROMMP (mp1);
  459.                 lPrevMemAvail = 0;  /* force data update*/
  460.                 WinSendMsg (hwndFrame, WM_TIMER, 0, 0);
  461.                 return 0;
  462.             }
  463.         }
  464.  
  465.     if (msg == WM_COMMAND)
  466.         {
  467.         switch (CMD)
  468.             {
  469.             case DID_OK:        /* close dialog box */
  470.             case DID_CANCEL:
  471.                 WinDismissDlg (hwnd, TRUE);
  472.                 return 0;
  473.  
  474.             case DID_ABOUT:
  475.                 WinDlgBox (HWND_DESKTOP, hwnd, 
  476.                         (PFNWP) AboutProc, 0, IDD_ABOUT, 0);    
  477.                 return 0;
  478.  
  479.             case DID_HELP:
  480.                 WinDlgBox (HWND_DESKTOP, hwnd, 
  481.                         (PFNWP) HelpProc, 0, IDD_HELP, 0);
  482.                 return 0;
  483.             }
  484.         }
  485.  
  486.     if (msg == WM_INITDLG)
  487.         {
  488.         sTemp = bRestoreFlag ? CB_3RDSTATE : ini.savescrpos;
  489.   
  490.         /* send current settings to Options dialog */
  491.         WinCheckButton (hwnd, ini.format, BM_SETCHECK);
  492.         WinSendDlgItemMsg (hwnd, ACB_SAVESCRPOS, BM_SETCHECK,
  493.                 MPFROM2SHORT (sTemp, 0), 0);
  494.         WinSendDlgItemMsg (hwnd, ACB_RESTORE, BM_SETCHECK,
  495.                 (MPARAM) bRestoreFlag, 0);
  496.         return 0;
  497.         }
  498.  
  499.     return WinDefDlgProc (hwnd, msg, mp1, mp2);
  500.     }
  501.  
  502.  
  503.