home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: InfoMgt
/
InfoMgt.zip
/
PMCAL.ZIP
/
PMCAL.C
< prev
next >
Wrap
Text File
|
1991-04-12
|
17KB
|
540 lines
/*
* OS/2 PM Perpetual Calendar utility - SOURCE CODE
*
* 04/11/91 1.00 - Robert Mahoney initial creation.
*
*/
#define INCL_DOSDATETIME
#define INCL_WINWINDOWMGR
#define INCL_WINDIALOGS
#define INCL_WINBUTTONS
#define INCL_WINENTRYFIELDS
#define INCL_WINLISTBOXES
#define INCL_WINMENUS
#define INCL_WINFRAMEMGR
#define INCL_WINSYS
#define INCL_WINPOINTERS
#define NUL '\0'
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <string.h>
#include "pmcal.h"
HAB hab; /* handle to anchor block */
int iSavedMonth; /* contains value for saved month */
int iSavedYear; /* contains value for saved year */
/**/
/*
* main( VOID ) : VOID;
*
* This function is the system entry point for the application
* and is responsible for defining the appropriate window
* classes and for processing all the messages. Note how
* the dialog box manager is responsible for the operation of
* the file chksum window.
*
*/
void cdecl main( VOID )
{
HMQ hmq; /* handle to message queue */
HWND hwnd; /* handle to main window */
HPOINTER hptr; /* handle to window's icon */
/* initialize thread & create message queue */
hab = WinInitialize( 0 );
hmq = WinCreateMsgQueue( hab, DEFAULT_QUEUE_SIZE );
/* load the main dialog */
hwnd = WinLoadDlg(
HWND_DESKTOP,
HWND_DESKTOP,
PmcalDlgProc,
NUL,
ID_PMCAL,
NUL
);
/* set an icon for the dialog */
hptr = WinLoadPointer( HWND_DESKTOP, NUL, ID_PMCAL );
WinSendMsg( hwnd, WM_SETICON, (MPARAM)hptr, 0L );
/* process the dialog */
WinProcessDlg( hwnd );
/* destroy the dialog and its icon */
WinDestroyWindow( hwnd );
WinDestroyPointer( hptr );
/* destroy message queue & terminate thread */
WinDestroyMsgQueue( hmq );
WinTerminate( hab );
}
/**/
/*
* PmcalDlgProc( hwnd, usMsg, mp1, mp2 ) : MRESULT;
*
* hwnd handle to main dialog window
* usMsg message number
* mp1 message parameter 1
* mp2 message parameter 2
*
* This function is responsible for processing all the messages
* which relate to the calendar dialog box. This mainly
* involves the definition and retrieval of the various
* events generated by the user.
*
*/
MRESULT EXPENTRY PmcalDlgProc(
HWND hwnd,
USHORT usMsg,
MPARAM mp1,
MPARAM mp2 )
{
/* variable definition starts */
MRESULT mresRtnVal; /* function return value */
BOOL fPassToDef; /* pass to def window proc? */
static HWND hwndDayText; /* hwnd for the days static text */
static HWND hwndNumText; /* hwnd for the numbers static text */
static char days[31] = "Sun Mon Tue Wed Thu Fri Sat";
static char szMonthNumber[134]; /* string for the numbers in the mon*/
static char szMonth[16]; /* holds the month for the calendar */
char szInputMon[3]; /* string for month from entryfield */
char szInputYear[5]; /* string for year from entryfield */
DATETIME date_time; /* date_time of struct DateTime */
int iWeekDay; /* contains value for weekday */
int iMonth; /* contains value for month */
int iYear; /* contains value for year */
static BOOL bMonth ; /* if true than month is bMonth*/
/* variable definition ends */
mresRtnVal = FALSE;
fPassToDef = FALSE;
switch ( usMsg ) {
case WM_INITDLG:
{
HWND hwndSysMenu; /* system menu handle */
USHORT idSysMenu; /* system menu id */
MENUITEM miSysMenu; /* system menu item info */
MENUITEM miAbout; /* About menu item info */
/* add About item to system menu */
if ( hwndSysMenu = WinWindowFromID( hwnd, FID_SYSMENU ) ) {
/* get handle of system submenu */
idSysMenu = SHORT1FROMMR( WinSendMsg( hwndSysMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT( 0 ), 0L ) );
WinSendMsg( hwndSysMenu, MM_QUERYITEM, MPFROM2SHORT( idSysMenu, FALSE ), MPFROMP( &miSysMenu ) );
hwndSysMenu = miSysMenu.hwndSubMenu;
/* add menu separator */
miAbout.iPosition = MIT_END;
miAbout.afStyle = MIS_SEPARATOR;
miAbout.afAttribute = 0;
miAbout.id = 0;
miAbout.hwndSubMenu = NUL;
miAbout.hItem = NUL;
WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miAbout ), NUL );
/* add About item */
miAbout.afStyle = MIS_TEXT;
miAbout.id = IDM_ABOUT;
WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miAbout ), MPFROMP( "A~bout..." ) );
}
/* change the static text font to monospace */
hwndDayText=WinWindowFromID(hwnd,IDT_CALDAY);
WinSetPresParam(hwndDayText,
PP_FONTNAMESIZE,
24L,
(PVOID) "10.System Monospaced");
hwndNumText=WinWindowFromID(hwnd,IDT_CALNUM);
WinSetPresParam(hwndNumText,
PP_FONTNAMESIZE,
24L,
(PVOID) "10.System Monospaced");
/* get todays month and year */
DosGetDateTime((PDATETIME)&date_time);
iMonth = (int) date_time.month;
iYear = (int) date_time.year ;
/* save to month and year */
iSavedMonth = iMonth;
iSavedYear = iYear;
/* get what day of the week the first of the month is */
/* Sun = 0....Sat = 6 */
iWeekDay = CalculateDayOfWeek(iMonth,iYear);
/* Fill the month numbers static text with the digits of */
/* the month */
FillDateNumbers( iWeekDay,iMonth,iYear,szMonthNumber);
/* Show month and year */
FillMonthString( iMonth,iYear,szMonth);
/* set the text */
WinSetDlgItemText (hwnd, IDT_CALNUM, szMonthNumber);
WinSetDlgItemText (hwnd, IDT_MONTH, szMonth);
/* set the entryfield text limit for month and year */
WinSendDlgItemMsg(hwnd,IDE_MONTH,
EM_SETTEXTLIMIT,
MPFROM2SHORT(2,0),
NUL);
WinSendDlgItemMsg(hwnd,IDE_YEAR,
EM_SETTEXTLIMIT,
MPFROM2SHORT(4,0),
NUL);
/* check the month selection and set the bool to true */
WinSendDlgItemMsg(hwnd,IDB_MONTH,
BM_SETCHECK,
MPFROM2SHORT(TRUE,0),
NUL);
bMonth = TRUE;
}
break;
case WM_CONTROL:
switch ( SHORT1FROMMP (mp1) ) {
/* checked radio button so set the bool */
case IDB_MONTH:
bMonth = TRUE;
break;
case IDB_YEAR:
bMonth = FALSE;
break;
default:
fPassToDef = TRUE;
break;
}
case WM_COMMAND:
switch ( COMMANDMSG( &usMsg )->cmd ) {
/* a button was pressed */
case IDB_NEXT:
if( bMonth )
/* if month is checked show the next month calendar */
{
iSavedMonth += 1;
if (iSavedMonth > 12)
{
iSavedYear +=1;
iSavedMonth = 1;
}
iMonth = iSavedMonth;
iYear = iSavedYear;
}
else
/* else show the same month next year */
{
iSavedYear += 1;
iMonth = iSavedMonth;
iYear = iSavedYear;
}
iWeekDay = CalculateDayOfWeek(iMonth,iYear);
FillDateNumbers(iWeekDay,
iMonth,
iYear,
szMonthNumber);
FillMonthString(iMonth,iYear,szMonth);
WinSetDlgItemText (hwnd, IDT_CALNUM, szMonthNumber);
WinSetDlgItemText (hwnd, IDT_MONTH, szMonth);
break;
case IDB_PREV:
if( bMonth )
/* if month is checked show the prev month calendar */
{
iSavedMonth -= 1;
if (iSavedMonth < 1)
{
iSavedYear -=1;
iSavedMonth = 12;
}
iMonth = iSavedMonth;
iYear = iSavedYear;
}
else
/* else show the same month prev year */
{
iSavedYear -= 1;
iMonth = iSavedMonth;
iYear = iSavedYear;
}
iWeekDay = CalculateDayOfWeek(iMonth,iYear);
FillDateNumbers(iWeekDay,
iMonth,
iYear,
szMonthNumber);
FillMonthString(iMonth,iYear,szMonth);
WinSetDlgItemText (hwnd, IDT_CALNUM, szMonthNumber);
WinSetDlgItemText (hwnd, IDT_MONTH, szMonth);
break;
case IDB_ENTER:
/* Get data from the entryfields */
WinQueryDlgItemText(
hwnd,
IDE_MONTH,
sizeof(szInputMon),
szInputMon );
WinQueryDlgItemText(
hwnd,
IDE_YEAR,
sizeof(szInputYear),
szInputYear );
/* validate the data */
iMonth = atoi(szInputMon);
iYear = atoi(szInputYear);
if (iMonth < 1 || iMonth > 12)
WinMessageBox (HWND_DESKTOP,hwnd,
"Enter a Month between 1 and 12",
"Incorrect Month",
0,MB_OK | MB_ERROR);
else
if (iYear < 1583)
WinMessageBox (HWND_DESKTOP,hwnd,
"Enter a Year greater than 1582",
"Incorrect Year",
0,MB_OK | MB_ERROR);
else
{
/* show the calendar for the entered values */
iSavedMonth = iMonth;
iSavedYear = iYear;
iWeekDay = CalculateDayOfWeek(iMonth,iYear);
FillDateNumbers(iWeekDay,
iMonth,
iYear,
szMonthNumber);
FillMonthString(iMonth,iYear,szMonth);
WinSetDlgItemText (hwnd, IDT_CALNUM, szMonthNumber);
WinSetDlgItemText (hwnd, IDT_MONTH, szMonth);
}
break;
case IDM_ABOUT:
WinDlgBox( HWND_DESKTOP, hwnd, AboutDlgProc, NUL, IDD_ABOUTDLG, NUL );
break;
default:
fPassToDef = TRUE;
break;
}
break;
case WM_MINMAXFRAME:
{
PSWP pswp; /* pos change structure */
/* hide number static text when minimized */
/* so it doesn't overwrite icon */
pswp = PVOIDFROMMP( mp1 );
WinShowWindow(
WinWindowFromID( hwnd, IDT_CALNUM ),
!(pswp->fs & SWP_MINIMIZE)
);
}
break;
default:
fPassToDef = TRUE;
break;
}
/* pass to def dialog proc if needed */
if ( fPassToDef )
mresRtnVal = WinDefDlgProc( hwnd, usMsg, mp1, mp2 );
return mresRtnVal;
}
/**/
/*
* AboutDlgProc( hwndDlg, usMsg, mp1, mp2 ) : MRESULT;
*
* hwndDlg handle to dialog box
* usMsg message number
* mp1 message parameter 1
* mp2 message parameter 2
*
* This is the dialog procedure for the About dialog box.
*
*/
MRESULT EXPENTRY AboutDlgProc( HWND hwndDlg, USHORT usMsg, MPARAM mp1, MPARAM mp2 )
{
MRESULT mresRtnVal; /* function return value */
BOOL fPassToDef; /* pass to def dlg proc? */
mresRtnVal = FALSE;
fPassToDef = FALSE;
switch ( usMsg ) {
case WM_COMMAND:
switch ( COMMANDMSG( &usMsg )->cmd ) {
case IDB_OK:
WinDismissDlg( hwndDlg, TRUE );
break;
default:
fPassToDef = TRUE;
break;
}
break;
default:
fPassToDef = TRUE;
break;
}
if ( fPassToDef )
mresRtnVal = WinDefDlgProc( hwndDlg, usMsg, mp1, mp2 );
return mresRtnVal;
}
/**/
/* Calculate the weekday number of the first day of the month */
/* using Zeller's Congruence */
int CalculateDayOfWeek(int iMonth, int iYear)
{
int iDay = 1; /* day of the month will be 1 */
int iWeekDay ; /* weekday of the first of the month
0=Sun..6=Sat */
if (iMonth < 3 )
{
iMonth = iMonth + 12;
iYear = iYear - 1;
}
iWeekDay = (iDay+1 + (iMonth*2) +
((int) (iMonth + 1) * 3 / 5) +
iYear +
(int) (iYear / 4) -
(int) (iYear / 100) +
(int) (iYear / 400) ) % 7;
return(iWeekDay);
}
/**/
/* fill the numbers static text with the numbers of the month */
void FillDateNumbers(int DayOfWeek,int iMonth,int iYear,PSZ szMonthNumber)
{
int i;
/* all months have 28 days */
static char *szNumbers[28] ={
" 1 ", " 2 "," 3 ", " 4 "," 5 "," 6 ", " 7 ",
" 8 ", " 9 ", " 10 ", " 11 "," 12 "," 13 "," 14 ",
" 15 "," 16 "," 17 ", " 18 "," 19 "," 20 "," 21 ",
" 22 "," 23 "," 24 "," 25 "," 26 "," 27 "," 28 "};
szMonthNumber[0] = '\0';
/* format dlgbox string for display */
/* put a blank instead of a number */
for (i=0;i<DayOfWeek;i++)
strcat (szMonthNumber," ");
/* concatenate the numbers to the spaces*/
for (i=0;i<28;i++)
strcat (szMonthNumber,szNumbers[i]);
/* add the extra days according to month */
switch ( iMonth ) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
strcat (szMonthNumber," 29 ");
strcat (szMonthNumber," 30 ");
strcat (szMonthNumber," 31 ");
break;
case 2:
/* do Feburary processing */
if (!(iYear % 4))
/* evenly divisible by 4 - maybe a leap year */
{
if (!(iYear % 100))
/* leap yr cannot be evenly divisible by 100*/
{
if (!(iYear % 400))
/* leap yr can be evenly divisible by 400*/
{
strcat (szMonthNumber," 29 ");
}
}
else
/* it's not evenly divisible by 100*/
strcat (szMonthNumber," 29 ");
}
break;
default:
strcat (szMonthNumber," 29 ");
strcat (szMonthNumber," 30 ");
break;
}
}
/**/
/* Give the heading to the calendar - Month Year */
void FillMonthString(int iMonth,int iYear,PSZ szMonthString)
{
static char *szMonths[12] ={
"January","February","March","April","May","June",
"July","August","September","October","November","December"};
char szHoldStr[5];
strcpy(szMonthString,szMonths[iMonth-1]);
itoa(iYear,szHoldStr,10);
strcat(szMonthString," ");
strcat(szMonthString,szHoldStr);
}