[comp.sys.mac.programmer] Help With A Dialog Box Question.

spencer@gertie.cgrg.ohio-state.edu (Stephen N. Spencer) (01/06/91)

I'm having a bear of a time with something that I think ought to be really
simple.  I'm trying to get a modal dialog to work.  (I've included a source
file at the end of this message.)  I used ResEdit to create the dialog, a
DLOG and DITL resource.  The first time it comes up it comes up just fine.
I can click on either button or press Return and the dialog box goes away.
It is at this point that I'm stymied.  With the code as it is right now (see
below) once the dialog box goes away the screen starts drawing funny jagged
lines and I have to reboot.  With minor modifications to this code I've had
everything but the mouse motion freeze up on me and also have had the app
quit on me, then THINK C give me an alert with 
	"The application "foo" has unexpectedly quit (2)."
Oh yes, it's a Mac Plus, 2.5Meg, System 6.0.5/Finder 6.1.5, and THINK C 4.0.

Can anyone help me with this?  I would love to be able to reuse this dialog
box in my application but heck, it just keeps walking on memory somewhere.

Thanks in advance!!

Stephen N. Spencer     |"Supreme executive power derives from a mandate from 
ACCAD   (614) 292-3416 | the masses, not from some farcical aquatic ceremony!
The Ohio State Univ.   | I mean, if I went 'round saying I was Emperor just
1224 Kinnear Road      | just because some moistened bint had lobbed a scimitar
Columbus, OH 43212-1163| at me, they'd put me away!"  -- MP and the Holy Grail
spencer@cgrg.ohio-state.edu||71160.3141@compuserve.com||stephen_spencer@osu.edu

/* The code in question, file 'dialogs.c'. */

#include "gendefines.h"
#undef PRIMLOCAL
#include "primitive.h"

#define OK_BUTTON     1
#define CANCEL_BUTTON 2

doFirstDialog()
{
	int     itemHit, itemType;
	Boolean done;
	register DialogPtr firstDialog;
	GrafPtr  gPtr;
	
	GetPort(&gPtr);
	
	firstDialog = GetNewDialog(132, 0L, -1L);
	if (!firstDialog) return;
	
	CenterDialog(firstDialog);
	ShowWindow(firstDialog);
	SetPort(firstDialog);
	DrawOKButton(firstDialog);
	
	done = FALSE;
	
	while (!done)
	{
		ModalDialog(NIL_POINTER, &itemHit);
		done = ((itemHit == OK_BUTTON) ||
			(itemHit == CANCEL_BUTTON));
	}
	SetPort(gPtr);
	DisposDialog(firstDialog);
}

DrawOKButton(dialog)
	DialogPtr dialog;
{
	int   itemType;
	Rect itemRect;
	Handle item;
	GrafPtr oldPort;
	
	GetDItem(dialog, OK_BUTTON, &itemType, &item, &itemRect);
	GetPort(&oldPort);
	SetPort(dialog);
	
	PenSize(3, 3);
	InsetRect(&itemRect, -4, -4);
	FrameRoundRect(&itemRect, 16, 16);
	PenNormal();
	
	SetPort(oldPort);
}
	
CenterDialog(dialog)
	DialogPtr dialog;
{
	Rect r;
	int  width, height, sW, sH, h, v;
	
	r = dialog->portRect;
	width = r.right - r.left;
	height = r.bottom - r.top;
	sW = screenBits.bounds.right - screenBits.bounds.left;
	sH = screenBits.bounds.bottom - screenBits.bounds.top;
	h = (sW - width) / 2;
	v = (sH - height) / 2;
	MoveWindow(dialog, h, v, FALSE);
}