bigwalls.net

/*------------------------------------------------------------------------------

# Programming by John Middendorf. I wrote this program to learn interface methods and to build some basic programs that can possibly be posted and used as useful programs in the future. I have written programs to create cam shapes previously in Basic and Pascal.

------------------------------------------------------------------------------*/

/* Version 1.0: 4/96 First program in C*/

 

#include <Printing.h>

#include <Types.h>

#include <Memory.h>

#include <Quickdraw.h>

#include <Fonts.h>

#include <Events.h>

#include <Menus.h>

#include <Windows.h>

#include <TextEdit.h>

#include <Dialogs.h>

#include <OSUtils.h>

#include <ToolUtils.h>

#include <SegLoad.h>

#include <fp.h>

/*#include <math.h>*/

//#include <stdlib.h>

#include <strings.h>

#include <SANE.h>

 

 

 

/* Constants */

#define test 311

/* Globals */

 

 

long gAngle;

long gAngleMinutes;

 

 

long gSize;

 

 

EventRecord TheEvent;

 

WindowPtr mainPtr;

WindowPtr TheWindow;

WindowPtr aWindow;

 

DialogPtr theDialog;

DialogPtr dlogSplash;

 

 

long count;

int CamDone = 0;

 

Boolean gDone = false;

 

 

/* Prototypes */

void Initialize(void);

void SetUpMenuBar( void );

void HandleMenuChoice( long );

void HandleMouseDown (EventRecord);

void SetUpWindow (void);

void EventLoop (void);

void Cam(long gAngle,long gAngleMinutes,long gSize);

void HandleUpdate( EventRecord );

void RunDialog(void);

void InputError(void);

void printCam( void );

void SplashScreen(void);

 

/*-----------------------------------------------------------------------------*/

/*Main body of program */

void main(void)

{

Initialize();

SetUpMenuBar();

SetUpWindow();

SplashScreen();

EventLoop();

}

/*-----------------------------------------------------------------------------*/

 

void SetUpWindow () //must check for presence of window (may have been closed)<now closebox quits>

/*Set a maximum size window:*/

{

Rect windRect;

int width;

int height;

 

windRect = qd.screenBits.bounds;

InsetRect(&windRect, 50, 50);

width = windRect.right - windRect.left;

height = windRect.bottom - windRect.top;

mainPtr = GetNewWindow( 128, 0L, (WindowPtr)-1L);

/*SizeWindow (mainPtr, width, height, true);*/

ShowWindow (mainPtr);

SetPort(mainPtr);

}

/*-----------------------------------------------------------------------------*/

 

void EventLoop( void )

{

while ( gDone == false )

{

WaitNextEvent( everyEvent, &TheEvent, 15L, nil );

switch ( TheEvent.what )

{

case keyDown:

MoveTo ( 10,10 );

DrawString( "\pA key has been pressed" );

count = 0; while (count < 100000) count++;

break;

case mouseDown:

HandleMouseDown( TheEvent );

break;

case updateEvt:

HandleUpdate( TheEvent );

break;

}

}

}

/*-----------------------------------------------------------------------------*/

void HandleUpdate( EventRecord TheEvent )

{

WindowPtr theWindow;

RgnHandle theOrigClipRgn;

Rect theClipMinusScrollRect;

theWindow = (WindowPtr)TheEvent.message;

BeginUpdate( theWindow );

SetPort( theWindow );

theOrigClipRgn = NewRgn();

GetClip( theOrigClipRgn );

EraseRect( &theWindow->portRect );

DrawGrowIcon( theWindow );

 

theClipMinusScrollRect = theWindow->portRect;

theClipMinusScrollRect.right -= 16;

theClipMinusScrollRect.bottom -=16;

ClipRect( &theClipMinusScrollRect );

if (CamDone == 1) Cam(gAngle,gAngleMinutes,gSize);

SetClip( theOrigClipRgn );

DisposeRgn( theOrigClipRgn );

EndUpdate( theWindow );

}

 

/*-----------------------------------------------------------------------------*/

void HandleMouseDown (EventRecord TheEvent)/*uses aWindow (not sure why this is diff)*/

{

short ThePart;

long theMenuChoice;

Rect theGrowLimitsRect;

long theNewSize;

short theNewWidth;

short theNewHeight;

 

 

ThePart = FindWindow (TheEvent.where, &aWindow );

switch (ThePart)

{

case inMenuBar:

theMenuChoice = MenuSelect (TheEvent.where );

if (theMenuChoice > 0)

HandleMenuChoice(theMenuChoice);

break;

case inGrow:

SetRect( &theGrowLimitsRect, 50, 50, 1000, 1000 );

theNewSize = GrowWindow( aWindow, TheEvent.where, &theGrowLimitsRect );

if ( theNewSize != 0 )

{

theNewWidth = LoWord( theNewSize );

theNewHeight = HiWord( theNewSize );

SetPort( aWindow );

EraseRect( &aWindow->portRect );

SizeWindow( aWindow, theNewWidth, theNewHeight, true );

InvalRect( &aWindow->portRect );

}

break;

case inDrag:

DragWindow (aWindow, TheEvent.where, &qd.screenBits.bounds );

break;

case inGoAway:

DisposeWindow (aWindow);

gDone=true;

break;

}

}

/*-----------------------------------------------------------------------------*/

 

void HandleMenuChoice (long theMenuChoice)

{

short TheMenu;

short TheMenuItem;

 

MenuHandle theAppleMenu;

Str255 theItemName;

short theItemNumber;

 

TheMenu = HiWord (theMenuChoice);

TheMenuItem = LoWord ( theMenuChoice);

switch ( TheMenu)

{

case 128: switch (TheMenuItem)

{

case 1: SplashScreen();

break;

default:

theAppleMenu = GetMenuHandle (128);

GetMenuItemText (theAppleMenu, TheMenuItem, theItemName);

theItemNumber = OpenDeskAcc (theItemName);

break;

}

break;

case 129: switch (TheMenuItem)

{

case 1: printCam();

break;

case 2: gDone = true; break;

}

break;

case 131:

switch (TheMenuItem)

{

case 1: RunDialog();

CamDone = 1;

break;

case 2:SplashScreen();

break;

}

break;

}

HiliteMenu (0);

}

/*__________________________________________________________________*/

void SetUpMenuBar( void )

{

Handle theMenuBar;

MenuHandle theAppleMenu;

theMenuBar = GetNewMBar( 128 );

SetMenuBar( theMenuBar );

DisposeHandle( theMenuBar );

theAppleMenu = GetMenuHandle( 128 );

AppendResMenu( theAppleMenu, 'DRVR' );

 

DrawMenuBar();

}

/*-----------------------------------------------------------------------------*/

void SplashScreen(void)

{

DialogPtr spDialog;

short theItem;

 

 

spDialog = GetNewDialog (129, nil, (WindowPtr)-1L);

if ( spDialog == nil )

ExitToShell();

 

ModalDialog( nil, &theItem );

 

if (theItem == 1)

SysBeep (10);

DisposeDialog(spDialog); /*can't say I know how this serendipity method works.*/

 

}

/*-----------------------------------------------------------------------------*/

 

void RunDialog(void)

{

DialogPtr theDialog;

short theItem;

short theType;

Boolean doneWithDialog = false;

long maxrange = 30;

 

Rect dlogRect;

Handle dlogHandle;

 

Str255 intAngle = "\p14";

Str255 intAngleMinutes ="\p30";

Str255 intSize ="\p3";

 

 

theDialog = GetNewDialog (128, nil, (WindowPtr)-1L);

if ( theDialog == nil )

ExitToShell();

GetDialogItem( theDialog, 3, &theType, &dlogHandle, &dlogRect);

SetDialogItemText( dlogHandle, intAngle );

GetDialogItem( theDialog, 9, &theType, &dlogHandle, &dlogRect);

SetDialogItemText( dlogHandle, intAngleMinutes );

GetDialogItem (theDialog, 4, &theType, &dlogHandle, &dlogRect);

SetDialogItemText (dlogHandle, intSize);

ShowWindow (theDialog);

SetPort(theDialog);

 

while (doneWithDialog == false)

{

ModalDialog( nil, &theItem );

switch ( theItem )

{

case 1:/*this is the ok button*/

GetDialogItem( theDialog, 3, &theType, &dlogHandle, &dlogRect);

GetDialogItemText( dlogHandle , intAngle );

StringToNum (intAngle,&gAngle);

if (gAngle > maxrange)

InputError();

GetDialogItem( theDialog, 9, &theType, &dlogHandle, &dlogRect);

GetDialogItemText( dlogHandle , intAngleMinutes );

StringToNum (intAngleMinutes, &gAngleMinutes);

if (gAngleMinutes>59)

InputError();

/* big problem here with Str255 to double conversion.*/

/* This is an attept to convert to a frigging number

temp2 == P2CStr (temp);

str2dec(temp2, &index, &dec, &validPrefix);

gAngle = (double_t) dec2num(&dec);

*/

GetDialogItem (theDialog, 4, &theType, &dlogHandle, &dlogRect);

GetDialogItemText( dlogHandle , intSize );

StringToNum (intSize,&gSize);

if (gSize>5)

InputError();

doneWithDialog = true;

DisposeDialog (theDialog);

SetPort(mainPtr);

EraseRect (&mainPtr ->portRect);

DrawGrowIcon(mainPtr);

Cam(gAngle,gAngleMinutes,gSize);

break;

 

}

}

 

}

/*-----------------------------------------------------------------------------*/

void InputError(void)

{

DialogPtr theDialog;

short theItem;

short theType;

Boolean doneWithDialog = false;

 

Rect dlogRect;

Handle dlogHandle;

 

theDialog = GetNewDialog (130, nil, (WindowPtr)-1L);

if ( theDialog == nil )

ExitToShell();

ShowWindow (theDialog);

SetPort(theDialog);

 

while (doneWithDialog == false)

{

ModalDialog( nil, &theItem );

switch ( theItem )

{

case 2:/*this is the ok button*/

DisposeDialog (theDialog);

doneWithDialog = true;

RunDialog();

break;

}

}

}

/*-----------------------------------------------------------------------------*/

void Cam(long gAngle,long gAngleMinutes, long gSize)

{

 

float camangle;

float r;

float theta;

float max;

float y;

float x;

float a;

int count;

int ox = 180;

int oy = 256;

Str255 teststr = "\pBloody Hell!";

Rect smallRect;

long frac;

Str255 TheFraction;

 

frac = (long)(10 * gAngleMinutes)/60;

NumToString (frac,TheFraction);

 

camangle = gAngle + (frac/10);

 

max = (gSize*=72);

 

MoveTo (15,15);

 

NumToString(gAngle,teststr);

DrawString ("\p Cam Angle equals ");DrawString (teststr); DrawString ("\p."); DrawString (TheFraction); DrawString ("\p degrees.");

 

MoveTo(ox,oy);

SetRect (&smallRect, ox-5,oy-5,ox+5,oy+5);

PenPat (&qd.black);

PaintOval (&smallRect);

PenSize(1,1);

 

 

a = sin (camangle * (3.14159/180));

 

 

count = -540;

while (count < 345)

 

{

theta = count;

theta = theta * (3.14159/180);

r = max * (exp(a * theta))/(exp(a * 3.14159));

x = (ox - (r * cos(theta)));

y = (oy - (r * sin(theta)));

ShowPen();

LineTo (x,y);

//DrawString (x);

count++;}

//LineTo (ox,oy);

}

/*-----------------------------------------------------------------------------*/

/*NEEDS ABILITY TO SAVE and Multiple Windows.*/

 

void printCam( void )

{

TPPrPort thePrinterPort;

THPrint thePrintRecord;

Boolean userClickedOK;

 

 

thePrintRecord = (THPrint)NewHandleClear( sizeof( TPrint ) );

 

PrOpen();

 

PrintDefault( thePrintRecord );

userClickedOK = PrStlDialog( thePrintRecord );

 

/*userClickedOK = PrJobDialog( thePrintRecord );*/

if ( userClickedOK == true )

{

thePrinterPort = PrOpenDoc( thePrintRecord, nil, nil );

PrOpenPage( thePrinterPort, nil );

Cam (gAngle,gAngleMinutes,gSize);

PrClosePage( thePrinterPort );

 

PrCloseDoc( thePrinterPort );

}

PrClose();

}

/*-----------------------------------------------------------------------------*/

 

/*Initialize all the needed managers.*/

void Initialize(void)

{

InitGraf(&qd.thePort);

InitFonts();

InitWindows();

InitMenus();

TEInit();

InitDialogs(nil);

FlushEvents (everyEvent,0);

InitCursor();

}

/*-----------------------------------------------------------------------------*/

 

/*Don't forget icons.*/