[comp.sys.mac.programmer] More on MPW tool graphics

mkb@ROVER.RI.CMU.EDU (Mike Blackwell) (04/15/88)

Okay, I can now open windows from a MPW tool, and draw in them. It works
okay - at least now I can see my data, but it's still pretty fragile, and
leaves a lot to be desired. Here are some of the problems I have:

Can't get events (update, in particular) from MPW, so I can't redraw the
window if it's been obscured, and then re-exposed.

If the graphic window is not the active window, then clicking on it will
make it active, as expected. The window can also be dragged. But, if the
window is active, and I click in the content region, MPW crashes doing a
GetPort.

There seems to be some problems using stdio to the Worksheet - sometimes it
takes a while for output to show up. Maybe just an occasional flush in the
tool will work - haven't tried that. Also, the cursor in the Worksheet
blinks very rapidly and erratically when the graphic window exists.

I tried saving the current port, and only switching to the graphic port when
I was going to draw, and switching back when I was done, but that caused
things to crash...

If I command-. out of the tool, then the graphic window never gets closed,
and becomes unhooked from reality. No way to get rid of it, and MPW isn't
really happy...

Anyway, I've included the code fragments I've been using. If you have any
ideas on how to do things better, I'd really appreciate them!

		Mike Blackwell		mkb@rover.ri.cmu.edu

/* tool_graphics.c  --  Routines to manage a graphics window from an MPW tool */

/* Mike Blackwell, Robotics Institute, Carnegie-Mellon University */

#define __ALLNU__
#include <Types.h>
#include <Quickdraw.h>
#include <Windows.h>

extern struct qd qd;
Rect graphicRect;
WindowPtr graphicWindow;
WindowRecord graphicWRecord;

/* Open a graphic window with inside diminsions X by Y, centered */
/* on the second graphic device screen if it exists, otherwise put it */
/* on the main screen. The window is placed in the background. */
open_graphic_window(X, Y, title)
int X, Y;
char *title;
{
    GDHandle gdh;
    int top, left, height, width;

    InitGraf(&qd.thePort);
    gdh = GetMainDevice();
    if (GetNextDevice(gdh) != nil) gdh = GetNextDevice(gdh);
    height = (*gdh)->gdRect.bottom - (*gdh)->gdRect.top;
    width = (*gdh)->gdRect.right - (*gdh)->gdRect.left;
    top = (*gdh)->gdRect.top + (height - Y) / 2;
    left = (*gdh)->gdRect.left + (width - X) / 2;

    SetRect(&graphicRect, left, top, left + X, top + Y);
    graphicWindow = NewWindow(&graphicWRecord, &graphicRect, title,
			      true, 0, nil, false, nil);
    SetPort(graphicWindow);
}


/* This must be called before the program exits! */
close_graphic_window()
{
    CloseWindow(graphicWindow);
}

darin@Apple.COM (Darin Adler) (04/16/88)

In article <1422@pt.cs.cmu.edu> mkb@rover.ri.cmu.edu (Mike Blackwell) writes:
> If I command-. out of the tool, then the graphic window never gets closed,
> and becomes unhooked from reality. No way to get rid of it, and MPW isn't
> really happy...

You can use the MPW routine onexit (in StdLib.h) to add a routine to a list
of routines automatically called when the tool exits due to a signal (such
as the abort signal sent by Command-.).
-- 
Darin Adler, Apple Computer                          AppleLink:Adler4
UUCP: {sun,voder,nsc,mtxinu,dual}!apple!darin  CSNET: darin@Apple.com