Category : C Source Code
Archive   : MEWELDT.ZIP
Filename : CALC.C

 
Output of file : CALC.C contained in archive : MEWELDT.ZIP
/*===========================================================================*/
/* */
/* File : CALC.C */
/* */
/* Purpose : Demo of the MS-Windows calculator. */
/* */
/* History : */
/* */
/* (C) Copyright 1989 Marc Adler/Magma Systems All Rights Reserved */
/*===========================================================================*/

#include
#include "window.h"
#include "keys.h"
#include "rc.h"


#define DLG_CALC 200
#define ID_DISPLAY 110
#define ID_MC 111
#define ID_MR 112
#define ID_MP 113
#define ID_MM 114

#include "desktop.h"

#ifdef DESKTOP
#define CALC_RESFILE NULL
#else
#define CALC_RESFILE "C:\\MEWEL\\CALC.RES"
#endif


HWND hMain;
HWND hWndCalc = NULLHWND;
static HWND hWndLastFocusItem = NULLHWND;

extern HWND hWndDir;

extern int pascal MainWndProc();
extern int pascal CalcDialogDriver(WORD idDlg, int (pascal *pfnDlg)());
extern int pascal CalcDialogProc();
extern double Calculate(double n1, double n2, int op);


#ifndef DESKTOP
main(argc, argv)
int argc;
char **argv;
{
EVENT event;
WNDCLASS wndClass;
WORD hAccel;

/*
Initialize the window system - this *must* be done once in every appl
*/
WinInit();
WinUseSysColors(NULLHWND, TRUE);

/*
Create the main window and set the main window procedure
*/
hMain = CreateWindow("Normal", /* class */
"DESKTOP", /* title */
WIN_TITLEBAR | WIN_SYSMENU | WIN_MINMAXBOX |
WIN_HAS_BORDER,
0,0, 80,25, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
0, /* id */
NULLHWND, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
WinSetWinProc(hMain, MainWndProc);
ShowWindow(hMain, TRUE);

hWndCalc = (HWND) CalcDialogDriver(DLG_CALC, CalcDialogProc);
#if MODELESS
if (hWndCalc)
ShowWindow(hWndCalc);
#endif

/*
Main message loop
*/
while (GetMessage(&event) != WM_QUIT)
{
#if MODELESS
if (hWndCalc && IsDialogMessage(hWndCalc, &event))
continue;
#endif

TranslateAccelerator(&event);
DispatchMessage(&event);
}

exit(0);
}
#endif


#ifndef DESKTOP
/*
MainWndProc - window procedure for the main window
*/
int pascal MainWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;

WORD wParam;
DWORD lParam;
{
switch (message)
{
case WM_SYSCOMMAND :
switch (wParam)
{
case SC_CLOSE :
PostQuitMessage(0);
break;
}
break;

default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}
#endif


Calculator()
{
#if MODELESS
if (hWndCalc)
{
/*
It's active, but hidden...
*/
BringWindowToTop(hWndCalc);
ShowWindow(hWndCalc, TRUE);
SetFocus(hWndLastFocusItem);
return TRUE;
}
#endif

hWndCalc = (HWND) CalcDialogDriver(DLG_CALC, CalcDialogProc);
#if MODELESS
if (hWndCalc)
{
ShowWindow(hWndCalc, TRUE);
SetFocus(hWndLastFocusItem = GetDlgItem(hWndCalc, IDCANCEL));
}
#endif
}


int pascal CalcDialogDriver(WORD idDlg, int (pascal *pfnDlg)())
{
WORD hModule;
HDLG hDlg;

if ((hModule = OpenResourceFile(CALC_RESFILE)) == FALSE)
{
MessageBox("Can't open CALC.RES", NULL, NULL, "Error", MB_OK);
return FALSE;
}
if ((hDlg = LoadDialog(hModule, idDlg, hWndDir, (PFBOOL) pfnDlg)) == NULLHWND)
{
MessageBox("Can't load the dialog box", NULL, NULL, "Error", MB_OK);
return FALSE;
}
CloseResourceFile(hModule);

#if MODELESS
WinSetFlags(hDlg, WS_CLIP);
return (int) hDlg;
#else
return DialogBox(hDlg);
#endif
}


int pascal CalcDialogProc(hDlg, message, wParam, lParam)
HWND hDlg;
WORD message;
WORD wParam;
DWORD lParam;
{
static double ulTotal = 0,
ulFirstNum = 0,
ulMemory = 0;
static int nDecimals = 0;
static double ulMultiplier = 0.1;
static BOOL bNewNumber = FALSE;
static int op = '=';
static BOOL bMemoryOn = FALSE;
static BOOL bGotM = FALSE;
static int iLastNumber;
char buf[80];

switch (message)
{
case WM_INITDIALOG :
ulMemory = ulTotal = 0;
ulMultiplier = 0.1;
nDecimals = 0;
op = '=';
bNewNumber = TRUE;
bMemoryOn = FALSE;
bGotM = FALSE;
DisplayNumber(hDlg, ulTotal, bMemoryOn, nDecimals);
return TRUE;

case WM_CHAR :
switch (toupper(wParam))
{
case VK_TAB :
case VK_BACKTAB :
return FALSE;

case VK_BACKSPACE :
if (nDecimals)
{
ulTotal -= iLastNumber * ulMultiplier;
ulMultiplier *= 10;
nDecimals--;
DisplayNumber(hDlg, ulTotal, bMemoryOn, nDecimals);
}
else
DisplayNumber(hDlg, ulTotal /= 10, bMemoryOn, nDecimals);
return TRUE;

case VK_ESC :
wParam = 'C';
break;

case VK_RETURN :
wParam = '=';
break;

case 'N' :
wParam = 'ñ';
break;

case 'Q' :
wParam = IDCANCEL;
break;

case 'S' :
wParam = 'û';
break;

case 'M' :
bGotM = TRUE;
return TRUE;

case 'R' :
if (bGotM)
{
bGotM = FALSE;
wParam = ID_MR;
break;
}
return TRUE;

case '+' :
case '-' :
if (bGotM)
{
bGotM = FALSE;
wParam = (wParam == '+') ? ID_MP : ID_MM;
}
break;

}
/* fall through ... */


case WM_COMMAND :
command:
switch (wParam)
{
case IDCANCEL :
#if MODELESS
DestroyWindow(hWndCalc);
hWndCalc = NULLHWND;
#else
EndDialog(hDlg, TRUE);
#endif
break;

case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
case ID_MR:
if (bNewNumber)
{
ulFirstNum = ulTotal;
ulTotal = 0;
nDecimals = 0;
ulMultiplier = 0.1;
}
bNewNumber = FALSE;

if (wParam == ID_MR)
ulTotal = ulMemory;
else
{
iLastNumber = wParam - '0';
if (nDecimals)
{
ulTotal += iLastNumber * ulMultiplier;
ulMultiplier *= 0.1;
nDecimals++;
}
else
ulTotal = ulTotal * 10L + iLastNumber;
}

DisplayNumber(hDlg, ulTotal, bMemoryOn, nDecimals);
break;

case '.' :
if (nDecimals == 0)
nDecimals++;
break;

case ID_MC :
mc:
ulMemory = 0;
ulMultiplier = 0.1;
nDecimals = 0;
bNewNumber = TRUE;
DisplayNumber(hDlg, ulTotal, bMemoryOn = FALSE, nDecimals);
break;

case ID_MM :
case ID_MP :
ulMemory = Calculate(ulMemory, ulTotal, wParam == ID_MP ? '+' : '-');
bNewNumber = TRUE;
ulMultiplier = 0.1;
nDecimals = 0;
DisplayNumber(hDlg, ulTotal, bMemoryOn = TRUE, nDecimals);
break;

case 'C' :
if (bGotM)
{
bGotM = FALSE;
goto mc;
}
ulMultiplier = 0.1;
nDecimals = 0;
DisplayNumber(hDlg, ulTotal = 0, bMemoryOn, nDecimals);
bNewNumber = TRUE;
break;

default :
if (wParam == 'ñ')
DisplayNumber(hDlg, ulTotal = -ulTotal, bMemoryOn, nDecimals);
else if (!bNewNumber)
{
ulTotal = Calculate(ulFirstNum, ulTotal, op);
DisplayNumber(hDlg, ulTotal, bMemoryOn, nDecimals);
}
bNewNumber = TRUE;
ulMultiplier = 0.1;
nDecimals = 0;
bGotM = FALSE;
op = wParam;
break;
} /* switch */

SetFocus(hWndLastFocusItem = GetDlgItem(hDlg, wParam));
return TRUE;


#if MODELESS
case WM_LBUTTONDOWN:
{
RECT rWindow;
int mouserow = HIWORD(lParam);
int mousecol = LOWORD(lParam);

GetWindowRect(hDlg, &rWindow);
if (mouserow == rWindow.top)
{
Rubberband(hDlg, TRUE);
ShowWindow(hDlg, TRUE);
return TRUE;
}
return FALSE;
}
#endif

}

/*
Return FALSE if you did not process the message. This gives the default
dialog proc a crack at handling it. Note - do NOT call any DefWindowProc
here; returning FALSE does the same thing.
*/
return FALSE;
}


DisplayNumber(HDLG hDlg, double ulNum, BOOL bMem, int nDec)
{
char buf[80];
char fmt[16];

if (nDec)
{
sprintf(fmt, "%%c%%27.%dlf", nDec-1);
sprintf(buf, fmt, (bMem) ? 'M' : ' ', ulNum);
}
else
{
sprintf(buf, "%c%27ld", (bMem) ? 'M' : ' ', (long) ulNum);
}
SetDlgItemText(hDlg, ID_DISPLAY, buf);
}


double Calculate(double n1, double n2, int op)
{
switch (op)
{
case '+' : return n1 + n2;
case '-' : return n1 - n2;
case '*' : return n1 * n2;
case '/' : return (n1) ? n1 / n2 : 0L;
case '=' : return n2;
case 'ñ' : return -n2;
case '%' : return n1 * (n2 * 0.01);
case 'û' : return sqrt(n2);
case 'C' :
default : return 0L;
}
}


  3 Responses to “Category : C Source Code
Archive   : MEWELDT.ZIP
Filename : CALC.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/