home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 8 Other
/
08-Other.zip
/
PMALARM.ZIP
/
ALARM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-07-11
|
9KB
|
269 lines
/* ALARM.C - Sample Alarm Clock Progeram */
/* Copyright (C) Lee S. Fields, 1989 */
/* */
/* The program takes the time and message for the alarm as */
/* parameters. The time is in 24:00 format and the message */
/* is a single word or a string in quotes. */
/* */
/* The alarm is a dialog box that beeps, displays the */
/* message, and waits for a response. The box is system */
/* modal so it pop up over any other windows. */
#define INCL_WIN
#include <os2.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "alarm.h"
/* declare functions to enforce type checking */
MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM);
MRESULT EXPENTRY AboutDlgProc (HWND, USHORT, MPARAM, MPARAM);
/* window anchor block handle */
HAB hab;
/* Common string prefixes */
CHAR szCaption[80] = "Alarm - ";
CHAR szAlarmMsg[256] = "Alarm Message:\n\'";
/* Global variables */
UCHAR uHours, uMinutes; /* time values */
BOOL bBadParms; /* bad parameter flag */
/* main function setup to accept command line parameters */
SHORT cdecl main (SHORT argc, CHAR **argv)
{
static CHAR szClientClass[] = "Alarm"; /* window class name */
HMQ hmq; /* message queue handle */
HWND hwndClient, hwndFrame; /* window handles */
QMSG qmsg; /* message queue structure */
/* start frame with icon, system menu and titlebar */
/* tasklist flag creates problem when selecting from tas */
ULONG flFrameFlags = FCF_ICON | FCF_SYSMENU | FCF_TITLEBAR;
/* start frame style as visible and minimized */
ULONG flFrameStyle = WS_VISIBLE | WS_MINIMIZED;
/* temporary variable to hold time */
CHAR szTime[6];
/* process parameters */
if (argc == 3)
{
strcpy (szTime, argv[1]); /* copy time */
uHours = (UCHAR) atoi (strtok (argv[1], ":")); /* get hours */
uMinutes = (UCHAR) atoi (strtok (NULL, ":")); /* get minutes */
strcat (szAlarmMsg, argv[2]); /* add message to message prefix */
strcat (szAlarmMsg, "\'");
bBadParms = FALSE;
if (uMinutes > 59 || uHours > 23) /* test for valid time */
bBadParms = TRUE;
strcat (szCaption, szTime);
}
else
{
bBadParms = TRUE;
strcat (szCaption, "Error"); /* add error to caption */
}
hab = WinInitialize (0); /* initialize window anchor block
* handle */
hmq = WinCreateMsgQueue (hab, 0); /* create window message queue */
WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0); /* register window class */
hwndFrame = WinCreateStdWindow (HWND_DESKTOP, /* parent window handle */
flFrameStyle, /* window frame style */
&flFrameFlags, /* window control flags */
szClientClass, /* window class name */
szCaption, /* title bar text */
0L,/* client window style */
NULL, /* resources in .exe */
ID_RESOURCE, /* icon resource id */
&hwndClient); /* pointer to client
* window handler */
/* Process messages, no preprocessing required so just dispatch them */
/* Exit when WM_QUIT message returns FALSE */
while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
WinDispatchMsg (hab, &qmsg);
WinDestroyWindow (hwndFrame); /* destroy the window */
WinDestroyMsgQueue (hmq); /* destroy the message queue */
WinTerminate (hab); /* destroy the anchor block handle */
return 0;
}
/* About and Help Dialog Box handler */
/* both boxes have only a single button, so can use same procedure */
MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
{
/* process messages */
switch (msg)
{
case WM_COMMAND: /* test for valid user input */
/* extract message type */
switch (COMMANDMSG (&msg)->cmd)
{
case MBID_OK: /* OK button */
case MBID_CANCEL: /* in case ESC was pressed */
WinDismissDlg (hwnd, TRUE);
return 0;
}
break;
}
return WinDefDlgProc (hwnd, msg, mp1, mp2);
}
MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
{
/* text for added system menu items */
static CHAR *szMenuText[3] = {NULL, "About Alarm...", "Help"};
/* structure for adding new system menu items */
static MENUITEM mi[3] = {
MIT_END, MIS_SEPARATOR, NULL, 0, NULL, NULL,
MIT_END, MIS_TEXT, NULL, IDM_ABOUT, NULL, NULL,
MIT_END, MIS_TEXT, NULL, IDM_HELP, NULL, NULL};
HWND hwndSysMenu, hwndSysSubMenu; /* system menu handles */
MENUITEM miSysMenu; /* menu item structure */
SHORT sItem, idSysMenu; /* counter and menu id */
DATETIME datetime; /* OS/2 date and time structure */
BOOL bRinging = FALSE, bRunning = FALSE; /* boolean flags */
/* process messages */
switch (msg)
{
/* Window is being created */
case WM_CREATE:
/* handle parameter errors upon window creation */
if (bBadParms)
{
/* adding a HELP button would be nice, but it doesn't */
/* work in this case since we are in WM_CREATE and the */
/* WM_HELP message would never get processed. Could */
/* move this message to latter in the code, but that */
/* greatly complicate the logic and overhead */
WinMessageBox (HWND_DESKTOP, hwnd,
"Invalid parameter list\nFormat: ALARM time \"message\"\ne.g. ALARM 12:00 \"Lunch!\"",
szCaption, 0, MB_OK | MB_ICONEXCLAMATION);
WinPostMsg (hwnd, WM_QUIT, 0L, 0L);
}
else
{
/* add new items to system menu */
/* get system menu handle */
hwndSysMenu = WinWindowFromID (
WinQueryWindow (hwnd, QW_PARENT, FALSE),
FID_SYSMENU);
/* get system menu id */
idSysMenu = (SHORT) WinSendMsg (hwndSysMenu,
MM_ITEMIDFROMPOSITION,
NULL, NULL);
/* get menuitem structure */
WinSendMsg (hwndSysMenu, MM_QUERYITEM,
MPFROM2SHORT (idSysMenu, FALSE),
MPFROMP (&miSysMenu));
/* get system submenu handle */
hwndSysSubMenu = miSysMenu.hwndSubMenu;
/* add 3 new system menu items */
for (sItem = 0; sItem < 3; sItem++)
WinSendMsg (hwndSysSubMenu, MM_INSERTITEM,
MPFROMP (mi + sItem),
MPFROMP (szMenuText[sItem]));
/* start timer to receive messages every second (1000ms) */
if (WinStartTimer (hab, hwnd, ID_TIMER, 1000))
bRunning = TRUE;
else
{
WinMessageBox (HWND_DESKTOP, hwnd, "Too many timers",
szCaption, 0, MB_OK | MB_ICONEXCLAMATION);
WinPostMsg (hwnd, WM_QUIT, 0L, 0L); /* send message to exit
* program */
}
}
return 0;
/* received timer message, roughly at one second intervals */
case WM_TIMER:
DosGetDateTime (&datetime);/* get system date and time */
/* test is current time matches alarm time */
if (datetime.hours == uHours && datetime.minutes == uMinutes)
{
bRinging = TRUE; /* set flag to show alarm triggered */
WinStopTimer (hab, hwnd, ID_TIMER); /* stop timer */
bRunning = FALSE; /* set timer running flag to false */
WinAlarm (HWND_DESKTOP, WA_NOTE); /* sound alarm */
WinMessageBox (HWND_DESKTOP, hwnd, szAlarmMsg,
szCaption, 0,
MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
WinPostMsg (hwnd, WM_QUIT, 0L, 0L); /* send message to exit
* program */
}
return 0;
/* received message from menu */
case WM_COMMAND:
/* extract menu choice */
switch (COMMANDMSG (&msg)->cmd)
{
/* process only the options this program added */
case IDM_ABOUT:
WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc, NULL,
IDD_ABOUT, NULL);
return 0;
case IDM_HELP:
WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc, NULL,
IDD_HELP, NULL);
return 0;
}
break;
case WM_CLOSE:
/*
* let default handler process if OK button pressed, otherwise return
* 0
*/
if (MBID_YES == WinMessageBox (HWND_DESKTOP, hwnd,
"Cancel the alarm?", szCaption, 0,
MB_YESNO | MB_ICONQUESTION))
break;
return 0;
case WM_DESTROY:
/* stop timer if running */
if (bRunning)
WinStopTimer (hab, hwnd, ID_TIMER);
return 0;
}
return WinDefWindowProc (hwnd, msg, mp1, mp2);
}