home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel
/
CAROUSEL.cdr
/
mactosh
/
lang
/
skel_azt.sha
/
filemenu.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-07-08
|
4KB
|
143 lines
/*
* filemenu.c -- handle selection of an item in the file menu
*/
#include <quickdraw.h>
#include <memory.h>
#include <menu.h>
#include <packages.h>
#include <syserr.h>
#include <textedit.h>
#include <window.h>
#include "def.h"
int
filemenu(item)
{
OSErr e;
extern TEHandle hTE;
long len;
extern MenuHandle mh[NMENUS + 1];
extern int paint; /* file refnum */
extern Point sfdtl; /* Standard File dialog window
* top left corner
*/
SFReply sfr;
Handle strrsrc;
extern int tf; /* text file reference number */
char tfname[64];
extern WindowPtr wp[NWINDOWS];
switch(item) {
case 1: /* New Text */
txinit();
break;
case 2: /* Open Text */
len = 'TEXT';
SFGetFile(pass(sfdtl), NIL, NIL, 1, &len, NIL, &sfr);
*((long *)0) = -1L; /* restore byte zero Apple just
* clobbered!
*/
if (sfr.good)
if (e = FSOpen(sfr.fName, sfr.vRefNum, &tf))
errmsg(3, e); /* File Open Error */
else {
txinit();
if (hTE && !GetEOF(tf, &len) &&
(strrsrc = NewHandle(len)) != NIL) {
DisposHandle((*hTE)->hText);
(*hTE)->teLength = len;
FSRead(tf, &len, *strrsrc);
FSClose(tf);
if (e)
errmsg(4, e); /* File Read Error */
if (len != (*hTE)->teLength)
errmsg(5, e); /* Incorrect length read:
*/
(*hTE)->hText = strrsrc;
TECalText(hTE);
crsm(); /* convert end-of-line to CR */
TECalText(hTE);
SetWTitle(wp[1], sfr.fName);
SetWRefCon(wp[1], (long)sfr.vRefNum);
teadjdest();
};
};
break;
case 4: /* Save As ... */
GetWTitle(wp[1], tfname);
SFPutFile(pass(sfdtl), "\Ptext file", tfname, NIL, &sfr);
*((long *)0) = -1L; /* restore byte zero Apple just
* clobbered!
*/
if (sfr.good) {
SetWTitle(wp[1], sfr.fName);
SetWRefCon(wp[1], (long)sfr.vRefNum);
}
else
break;
/* note a successful "Save As" continues into "Save" */
case 3: /* Save Text */
GetWTitle(wp[1], tfname);
if (e = FSOpen(tfname, (short)GetWRefCon(wp[1]), &tf)) {
if (e != fnfErr) {
errmsg(3, e); /* File Open Error */
break;
};
if (e = Create(tfname, (short)GetWRefCon(wp[1]),
'SKEL', 'TEXT')) {
errmsg(6, e); /* File Create Error */
break;
};
if (e = FSOpen(tfname, (short)GetWRefCon(wp[1]), &tf)) {
errmsg(3, e); /* File Open Error */
break;
};
};
len = (*hTE)->teLength;
if (e = FSWrite(tf, &len, *(*hTE)->hText))
errmsg(7, e); /* File Write Error */
if (len != (*hTE)->teLength)
errmsg(8, len); /* Incorrect length written: */
FSClose(tf);
break;
case 5: /* Open Paint */
len = 'PNTG';
SFGetFile(pass(sfdtl), NIL, NIL, 1, &len, NIL, &sfr);
*((long *)0) = -1L; /* restore byte zero Apple just
* clobbered!
*/
if (sfr.good)
if (e = FSOpen(sfr.fName, sfr.vRefNum, &paint)) {
errmsg(3, e); /* File Open Error */
paint = 0;
}
else {
InvalRect(&wp[0]->portRect);
if (wp[0] != FrontWindow())
SelectWindow(wp[0]);
DisableItem(mh[2], 5);
EnableItem(mh[2], 7);
};
break;
case 6: /* Close Text */
txfin();
break;
case 7: /* Close Paint */
FSClose(paint);
paint = 0;
InvalRect(&wp[0]->portRect);
if (wp[0] != FrontWindow())
SelectWindow(wp[0]);
EnableItem(mh[2], 5);
DisableItem(mh[2], 7);
break;
case 9: /* Quit */
return(TRUE);
break;
};
return(FALSE);
} /* end of filemenu */