home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / PMALARM.ZIP / ALARM.C < prev    next >
C/C++ Source or Header  |  1989-07-11  |  9KB  |  269 lines

  1. /* ALARM.C - Sample Alarm Clock Progeram                     */
  2. /* Copyright (C) Lee S. Fields, 1989                         */
  3. /*                                                             */
  4. /* The program takes the time and message for the alarm as     */
  5. /* parameters.    The time is in 24:00 format and the message  */
  6. /* is a single word or a string in quotes.                     */
  7. /*                                                             */
  8. /* The alarm is a dialog box that beeps, displays the         */
  9. /* message, and waits for a response.  The box is system     */
  10. /* modal so it pop up over any other windows.                 */
  11.  
  12.  
  13. #define INCL_WIN
  14.  
  15. #include <os2.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "alarm.h"
  21.  
  22. /* declare functions to enforce type checking */
  23. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM);
  24. MRESULT EXPENTRY AboutDlgProc (HWND, USHORT, MPARAM, MPARAM);
  25.  
  26. /* window anchor block handle */
  27. HAB hab;
  28.  
  29. /* Common string prefixes */
  30. CHAR szCaption[80] = "Alarm - ";
  31. CHAR szAlarmMsg[256] = "Alarm Message:\n\'";
  32.  
  33. /* Global variables */
  34. UCHAR uHours, uMinutes;           /* time values */
  35. BOOL bBadParms;                   /* bad parameter flag */
  36.  
  37.  
  38. /* main function setup to accept command line parameters */
  39. SHORT cdecl main (SHORT argc, CHAR **argv)
  40. {
  41.     static CHAR szClientClass[] = "Alarm";    /* window class name */
  42.     HMQ hmq;                   /* message queue handle */
  43.     HWND hwndClient, hwndFrame;           /* window handles */
  44.     QMSG qmsg;                   /* message queue structure */
  45.  
  46.  /* start frame with icon, system menu and titlebar                 */
  47.  /* tasklist flag creates problem when selecting from tas     */
  48.     ULONG flFrameFlags = FCF_ICON | FCF_SYSMENU | FCF_TITLEBAR;
  49.  
  50.  /* start frame style as visible and minimized */
  51.     ULONG flFrameStyle = WS_VISIBLE | WS_MINIMIZED;
  52.  
  53.  /* temporary variable to hold time */
  54.     CHAR szTime[6];
  55.  
  56.  /* process parameters */
  57.     if (argc == 3)
  58.     {
  59.     strcpy (szTime, argv[1]);      /* copy time */
  60.  
  61.     uHours = (UCHAR) atoi (strtok (argv[1], ":"));    /* get hours */
  62.     uMinutes = (UCHAR) atoi (strtok (NULL, ":"));    /* get minutes */
  63.  
  64.     strcat (szAlarmMsg, argv[2]);  /* add message to message prefix */
  65.     strcat (szAlarmMsg, "\'");
  66.  
  67.     bBadParms = FALSE;
  68.     if (uMinutes > 59 || uHours > 23)    /* test for valid time */
  69.         bBadParms = TRUE;
  70.  
  71.     strcat (szCaption, szTime);
  72.     }
  73.     else
  74.     {
  75.     bBadParms = TRUE;
  76.     strcat (szCaption, "Error");   /* add error to caption */
  77.     }
  78.  
  79.  
  80.     hab = WinInitialize (0);           /* initialize window anchor block
  81.                         * handle */
  82.     hmq = WinCreateMsgQueue (hab, 0);  /* create window message queue */
  83.  
  84.     WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0);    /* register window class */
  85.  
  86.     hwndFrame = WinCreateStdWindow (HWND_DESKTOP,    /* parent window handle */
  87.                     flFrameStyle,    /* window frame style */
  88.                     &flFrameFlags,    /* window control flags */
  89.                     szClientClass,    /* window class name */
  90.                     szCaption,    /* title bar text */
  91.                     0L,/* client window style */
  92.                     NULL,    /* resources in .exe */
  93.                     ID_RESOURCE,    /* icon resource id */
  94.                     &hwndClient);    /* pointer to client
  95.                              * window handler */
  96.  
  97.  /* Process messages, no preprocessing required so just dispatch them */
  98.  /* Exit when WM_QUIT message returns FALSE */
  99.     while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  100.     WinDispatchMsg (hab, &qmsg);
  101.  
  102.     WinDestroyWindow (hwndFrame);      /* destroy the window */
  103.     WinDestroyMsgQueue (hmq);           /* destroy the message queue */
  104.     WinTerminate (hab);               /* destroy the anchor block handle */
  105.     return 0;
  106. }
  107.  
  108.  
  109. /* About and Help Dialog Box handler */
  110. /* both boxes have only a single button, so can use same procedure */
  111. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  112. {
  113.  /* process messages */
  114.     switch (msg)
  115.     {
  116.         case WM_COMMAND:           /* test for valid user input */
  117.     /* extract message type */
  118.         switch (COMMANDMSG (&msg)->cmd)
  119.         {
  120.             case MBID_OK:      /* OK button */
  121.             case MBID_CANCEL:  /* in case ESC was pressed */
  122.               WinDismissDlg (hwnd, TRUE);
  123.             return 0;
  124.         }
  125.         break;
  126.     }
  127.     return WinDefDlgProc (hwnd, msg, mp1, mp2);
  128. }
  129.  
  130.  
  131. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  132. {
  133.  /* text for added system menu items */
  134.     static CHAR *szMenuText[3] = {NULL, "About Alarm...", "Help"};
  135.  
  136.  /* structure for adding new system menu items */
  137.     static MENUITEM mi[3] = {
  138.                  MIT_END, MIS_SEPARATOR, NULL, 0, NULL, NULL,
  139.                  MIT_END, MIS_TEXT, NULL, IDM_ABOUT, NULL, NULL,
  140.                  MIT_END, MIS_TEXT, NULL, IDM_HELP, NULL, NULL};
  141.  
  142.     HWND hwndSysMenu, hwndSysSubMenu;  /* system menu handles */
  143.     MENUITEM miSysMenu;               /* menu item structure */
  144.     SHORT sItem, idSysMenu;           /* counter and menu id */
  145.  
  146.     DATETIME datetime;               /* OS/2 date and time structure */
  147.     BOOL bRinging = FALSE, bRunning = FALSE;    /* boolean flags */
  148.  
  149.  /* process messages */
  150.     switch (msg)
  151.     {
  152.     /* Window is being created */
  153.     case WM_CREATE:
  154.     /* handle parameter errors upon window creation */
  155.         if (bBadParms)
  156.         {
  157.         /* adding a HELP button would be nice, but it doesn't  */
  158.         /* work in this case since we are in WM_CREATE and the */
  159.         /* WM_HELP message would never get processed.  Could   */
  160.         /* move this message to latter in the code, but that   */
  161.         /* greatly complicate the logic and overhead            */
  162.         WinMessageBox (HWND_DESKTOP, hwnd,
  163.                    "Invalid parameter list\nFormat: ALARM time \"message\"\ne.g. ALARM 12:00 \"Lunch!\"",
  164.                    szCaption, 0, MB_OK | MB_ICONEXCLAMATION);
  165.  
  166.         WinPostMsg (hwnd, WM_QUIT, 0L, 0L);
  167.         }
  168.         else
  169.         {
  170.         /* add new items to system menu */
  171.         /* get system menu handle */
  172.         hwndSysMenu = WinWindowFromID (
  173.                     WinQueryWindow (hwnd, QW_PARENT, FALSE),
  174.                            FID_SYSMENU);
  175.  
  176.         /* get system menu id */
  177.         idSysMenu = (SHORT) WinSendMsg (hwndSysMenu,
  178.                         MM_ITEMIDFROMPOSITION,
  179.                         NULL, NULL);
  180.  
  181.         /* get menuitem structure */
  182.         WinSendMsg (hwndSysMenu, MM_QUERYITEM,
  183.                 MPFROM2SHORT (idSysMenu, FALSE),
  184.                 MPFROMP (&miSysMenu));
  185.  
  186.         /* get system submenu handle */
  187.         hwndSysSubMenu = miSysMenu.hwndSubMenu;
  188.  
  189.         /* add 3 new system menu items */
  190.         for (sItem = 0; sItem < 3; sItem++)
  191.             WinSendMsg (hwndSysSubMenu, MM_INSERTITEM,
  192.                 MPFROMP (mi + sItem),
  193.                 MPFROMP (szMenuText[sItem]));
  194.  
  195.         /* start timer to receive messages every second (1000ms) */
  196.         if (WinStartTimer (hab, hwnd, ID_TIMER, 1000))
  197.             bRunning = TRUE;
  198.         else
  199.         {
  200.             WinMessageBox (HWND_DESKTOP, hwnd, "Too many timers",
  201.                    szCaption, 0, MB_OK | MB_ICONEXCLAMATION);
  202.             WinPostMsg (hwnd, WM_QUIT, 0L, 0L);    /* send message to exit
  203.                              * program */
  204.         }
  205.         }
  206.  
  207.         return 0;
  208.  
  209.  
  210.     /* received timer message, roughly at one second intervals */
  211.     case WM_TIMER:
  212.         DosGetDateTime (&datetime);/* get system date and time */
  213.     /* test is current time matches alarm time */
  214.         if (datetime.hours == uHours && datetime.minutes == uMinutes)
  215.         {
  216.         bRinging = TRUE;       /* set flag to show alarm triggered */
  217.         WinStopTimer (hab, hwnd, ID_TIMER);    /* stop timer */
  218.         bRunning = FALSE;      /* set timer running flag to false */
  219.         WinAlarm (HWND_DESKTOP, WA_NOTE);    /* sound alarm */
  220.         WinMessageBox (HWND_DESKTOP, hwnd, szAlarmMsg,
  221.                    szCaption, 0,
  222.                    MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
  223.         WinPostMsg (hwnd, WM_QUIT, 0L, 0L);    /* send message to exit
  224.                              * program */
  225.         }
  226.         return 0;
  227.  
  228.  
  229.     /* received message from menu */
  230.     case WM_COMMAND:
  231.     /* extract menu choice */
  232.         switch (COMMANDMSG (&msg)->cmd)
  233.         {
  234.         /* process only the options this program added */
  235.         case IDM_ABOUT:
  236.             WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc, NULL,
  237.                    IDD_ABOUT, NULL);
  238.             return 0;
  239.  
  240.         case IDM_HELP:
  241.             WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc, NULL,
  242.                    IDD_HELP, NULL);
  243.             return 0;
  244.         }
  245.         break;
  246.  
  247.  
  248.     case WM_CLOSE:
  249.     /*
  250.      * let default handler process if OK button pressed, otherwise return
  251.      * 0 
  252.      */
  253.         if (MBID_YES == WinMessageBox (HWND_DESKTOP, hwnd,
  254.                        "Cancel the alarm?", szCaption, 0,
  255.                        MB_YESNO | MB_ICONQUESTION))
  256.         break;
  257.  
  258.         return 0;
  259.  
  260.     case WM_DESTROY:
  261.     /* stop timer if running */
  262.         if (bRunning)
  263.         WinStopTimer (hab, hwnd, ID_TIMER);
  264.         return 0;
  265.     }
  266.     return WinDefWindowProc (hwnd, msg, mp1, mp2);
  267. }
  268.  
  269.