home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
macintosh-c
/
macc-carbon-demos-nonbinhex.sit
/
macc-carbon-demos-nonbinhex
/
chap26-demo
/
MLTENewOpenCloseSave.c
< prev
next >
Wrap
Text File
|
2001-06-05
|
17KB
|
589 lines
// *******************************************************************************************
// MLTENewOpenCloseSave.c
// *******************************************************************************************
// ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
#include "MLTETextEditor.h"
// …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
SInt16 gCurrentNumberOfWindows = 0;
SInt16 gUntitledWindowNumber = 0;
extern Boolean gRunningOnX = false;
extern SInt16 gAppResFileRefNum;
// ****************************************************************************** doNewCommand
OSStatus doNewCommand(void)
{
OSStatus osStatus = noErr;
WindowRef windowRef;
if(gCurrentNumberOfWindows == kMaxWindows)
return eMaxWindows;
osStatus = doNewDocWindow(&windowRef,NULL,kTXNTextensionFile);
if(osStatus == noErr)
SetWindowProxyCreatorAndType(windowRef,kFileCreator,kTXNTextensionFile,kUserDomain);
return osStatus;
}
// ***************************************************************************** doOpenCommand
OSStatus doOpenCommand(void)
{
OSStatus osStatus = noErr;
NavDialogOptions dialogOptions;
NavTypeListHandle fileTypeListHdl = NULL;
NavEventUPP navEventFunctionUPP;
NavReplyRecord navReplyStruc;
SInt32 count, index;
AEKeyword theKeyword;
DescType actualType;
FSSpec fileSpec;
Size actualSize;
FInfo fileInfo;
OSType fileType;
osStatus = NavGetDefaultDialogOptions(&dialogOptions);
if(osStatus == noErr)
{
GetIndString(dialogOptions.clientName,rMiscellaneousStrings,sApplicationName);
fileTypeListHdl = (NavTypeListHandle) GetResource('open',rOpenResource);
navEventFunctionUPP = NewNavEventUPP((NavEventProcPtr) navEventFunction);
osStatus = NavGetFile(NULL,&navReplyStruc,&dialogOptions,navEventFunctionUPP,
NULL,NULL,fileTypeListHdl,NULL);
DisposeNavEventUPP(navEventFunctionUPP);
if(osStatus == noErr && navReplyStruc.validRecord)
{
osStatus = AECountItems(&(navReplyStruc.selection),&count);
if(osStatus == noErr)
{
for(index=1;index<=count;index++)
{
osStatus = AEGetNthPtr(&(navReplyStruc.selection),index,typeFSS,&theKeyword,
&actualType,&fileSpec,sizeof(fileSpec),&actualSize);
if((osStatus = FSpGetFInfo(&fileSpec,&fileInfo)) == noErr)
{
fileType = fileInfo.fdType;
osStatus = doOpenFile(fileSpec,fileType);
}
}
}
osStatus = NavDisposeReply(&navReplyStruc);
}
if(fileTypeListHdl != NULL)
ReleaseResource((Handle) fileTypeListHdl);
}
if(osStatus == userCanceledErr)
osStatus = noErr;
return osStatus;
}
// **************************************************************************** doCloseCommand
OSStatus doCloseCommand(NavAskSaveChangesAction action)
{
WindowRef windowRef;
TXNObject txnObject = NULL;
OSStatus osStatus = noErr;
NavDialogOptions dialogOptions;
NavAskSaveChangesResult reply = 0;
NavEventUPP navEventFunctionUPP;
Str255 fileName;
osStatus = NavGetDefaultDialogOptions(&dialogOptions);
if(osStatus == noErr)
{
windowRef = FrontWindow();
if(isApplicationWindow(windowRef,&txnObject))
{
if(TXNGetChangeCount(txnObject))
{
GetWTitle(windowRef,fileName);
BlockMoveData(fileName,dialogOptions.savedFileName,fileName[0] + 1);
GetIndString(dialogOptions.clientName,rMiscellaneousStrings,sApplicationName);
navEventFunctionUPP = NewNavEventUPP((NavEventProcPtr) navEventFunction);
osStatus = NavAskSaveChanges(&dialogOptions,action,&reply,navEventFunctionUPP,0);
DisposeNavEventUPP(navEventFunctionUPP);
if(osStatus == noErr)
{
switch(reply)
{
case kNavAskSaveChangesSave:
if((osStatus = doSaveCommand()) == noErr)
doCloseWindow(windowRef,txnObject);
break;
case kNavAskSaveChangesDontSave:
doCloseWindow(windowRef,txnObject);
break;
case kNavAskSaveChangesCancel:
osStatus = kNavAskSaveChangesCancel;
break;
}
}
}
else
{
doCloseWindow(windowRef,txnObject);
}
}
}
return osStatus;
}
// ***************************************************************************** doSaveCommand
OSStatus doSaveCommand(void)
{
WindowRef windowRef;
OSStatus hasNoFileSpec;
OSStatus osStatus = noErr;
FSSpec fileSpec;
windowRef = FrontWindow();
hasNoFileSpec = GetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),NULL,
&fileSpec);
if(hasNoFileSpec)
osStatus = doSaveAsCommand();
else
osStatus = doWriteFile(windowRef,false);
if(osStatus == noErr)
SetWindowModified(windowRef,false);
return osStatus;
}
// *************************************************************************** doSaveAsCommand
OSStatus doSaveAsCommand(void)
{
OSStatus osStatus = noErr;
NavDialogOptions dialogOptions;
WindowRef windowRef;
NavEventUPP navEventFunctionUPP;
TXNFileType txnFileType;
NavReplyRecord navReplyStruc;
AEKeyword theKeyword;
DescType actualType;
FSSpec fileSpec;
Size actualSize;
AliasHandle aliasHdl;
osStatus = NavGetDefaultDialogOptions(&dialogOptions);
if(osStatus == noErr)
{
windowRef = FrontWindow();
GetWTitle(windowRef,dialogOptions.savedFileName);
GetIndString(dialogOptions.clientName,rMiscellaneousStrings,sApplicationName);
navEventFunctionUPP = NewNavEventUPP((NavEventProcPtr) navEventFunction);
GetWindowProperty(windowRef,kFileCreator,'FiTy',sizeof(TXNFileType),NULL,&txnFileType);
osStatus = NavPutFile(NULL,&navReplyStruc,&dialogOptions,navEventFunctionUPP,
txnFileType,kFileCreator,NULL);
DisposeNavEventUPP(navEventFunctionUPP);
if(navReplyStruc.validRecord && osStatus == noErr)
{
if((osStatus = AEGetNthPtr(&(navReplyStruc.selection),1,typeFSS,&theKeyword,
&actualType,&fileSpec,sizeof(fileSpec),&actualSize))
== noErr)
{
if(!navReplyStruc.replacing)
{
osStatus = FSpCreate(&fileSpec,kFileCreator,txnFileType,navReplyStruc.keyScript);
if(osStatus != noErr)
{
NavDisposeReply(&navReplyStruc);
return osStatus;
}
}
if(osStatus == noErr)
{
SetWTitle(windowRef,fileSpec.name);
SetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),&fileSpec);
SetPortWindowPort(windowRef);
SetWindowProxyFSSpec(windowRef,&fileSpec);
GetWindowProxyAlias(windowRef,&aliasHdl);
SetWindowProperty(windowRef,kFileCreator,'tALH',sizeof(AliasHandle),&aliasHdl);
SetWindowModified(windowRef,false);
osStatus = doWriteFile(windowRef,!navReplyStruc.replacing);
}
NavCompleteSave(&navReplyStruc,kNavTranslateInPlace);
}
NavDisposeReply(&navReplyStruc);
}
}
if(osStatus == userCanceledErr)
osStatus = noErr;
return osStatus;
}
// *************************************************************************** doRevertCommand
OSStatus doRevertCommand(void)
{
OSStatus osStatus = noErr;
NavDialogOptions dialogOptions;
NavEventUPP navEventFunctionUPP;
WindowRef windowRef;
Str255 fileName;
NavAskSaveChangesResult reply;
TXNObject txnObject = NULL;
osStatus = NavGetDefaultDialogOptions(&dialogOptions);
if(osStatus == noErr)
{
navEventFunctionUPP = NewNavEventUPP((NavEventProcPtr) navEventFunction);
windowRef = FrontWindow();
GetWTitle(windowRef,fileName);
BlockMoveData(fileName,dialogOptions.savedFileName,fileName[0] + 1);
osStatus = NavAskDiscardChanges(&dialogOptions,&reply,navEventFunctionUPP,0);
DisposeNavEventUPP(navEventFunctionUPP);
if(osStatus == noErr)
{
if(reply == kNavAskDiscardChanges)
{
if(isApplicationWindow(windowRef,&txnObject))
{
TXNRevert(txnObject);
if(TXNDataSize(txnObject))
SetWindowModified(windowRef,false);
}
}
}
}
return osStatus;
}
// ***************************************************************************** doQuitCommand
OSStatus doQuitCommand(NavAskSaveChangesAction action)
{
OSStatus osStatus = noErr;
while(FrontWindow())
{
osStatus = doCloseCommand(action);
if(osStatus != noErr)
return osStatus;
}
return osStatus;
}
// **************************************************************************** doNewDocWindow
OSStatus doNewDocWindow(WindowRef *outWindowRef,FSSpec *fileSpec,TXNFileType txnFileType)
{
WindowRef windowRef;
Str255 numberAsString, titleString = "\puntitled ";
Rect availableBoundsRect, portRect;
SInt16 windowHeight;
TXNFrameOptions txnFrameOptions;
OSStatus osStatus = noErr;
TXNObject txnObject = NULL;
TXNFrameID txnFrameID;
RGBColor frameColour = { 0xEEEE, 0xEEEE, 0xEEEE };
TXNControlTag txnControlTag[1];
TXNControlData txnControlData[1];
TXNMargins txnMargins;
CGContextRef cgContextRef;
// ……………………………………………………………………………………………………………………………………………………………………………………………………………… get window
if(!(windowRef = GetNewCWindow(rNewWindow,NULL,(WindowRef) -1)))
return MemError();
SetPortWindowPort(windowRef);
ChangeWindowAttributes(windowRef,kWindowInWindowMenuAttribute,0);
gUntitledWindowNumber++;
if(gUntitledWindowNumber != 1)
{
NumToString(gUntitledWindowNumber,numberAsString);
doConcatPStrings(titleString,numberAsString);
}
SetWTitle(windowRef,titleString);
// ………………………………………………………………………………………… extend window bottom to bottom of screen less the dock
GetAvailableWindowPositioningBounds(GetMainDevice(),&availableBoundsRect);
GetWindowPortBounds(windowRef,&portRect);
LocalToGlobal(&topLeft(portRect));
windowHeight = availableBoundsRect.bottom - portRect.top;
SizeWindow(windowRef,630,windowHeight,false);
// …………………………………………………………………………………………………………………………… get new TXNObject and attach window to it
txnFrameOptions = kTXNWantHScrollBarMask | kTXNWantVScrollBarMask | kTXNShowWindowMask;
osStatus = TXNNewObject(fileSpec,windowRef,NULL,txnFrameOptions,kTXNTextEditStyleFrameType,
txnFileType,kTXNSystemDefaultEncoding,&txnObject,&txnFrameID,
NULL);
if(osStatus == noErr)
{
// ……………………………………………………………………………………………………………… associate frame ID and TXNObject with window
SetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),&txnObject);
SetWindowProperty(windowRef,kFileCreator,'tFRM',sizeof(TXNFrameID),&txnFrameID);
if(fileSpec != NULL)
SetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),fileSpec);
SetWindowProperty(windowRef,kFileCreator,'FiTy',sizeof(TXNFileType),&txnFileType);
// ……………………………………………………………………………………………………………………………………………………………………………………………………… set margins
txnControlTag[0] = kTXNMarginsTag;
txnControlData[0].marginsPtr = &txnMargins;
txnMargins.leftMargin = txnMargins.topMargin = 10;
txnMargins.rightMargin = txnMargins.bottomMargin = 10;
TXNSetTXNObjectControls(txnObject,false,1,txnControlTag,txnControlData);
// …………………………………………………………………………………………………………… create core graphics context and pass to MLTE
if(gRunningOnX)
{
CreateCGContextForPort(GetWindowPort(windowRef),&cgContextRef);
txnControlTag[0] = kATSUCGContextTag;
txnControlData[0].uValue = (UInt32) cgContextRef;
TXNSetTXNObjectControls(txnObject,false,1,txnControlTag,txnControlData);
}
}
else
doErrorAlert(osStatus);
gCurrentNumberOfWindows ++;
if(gCurrentNumberOfWindows == 1)
doEnableDisableMenus(true);
*outWindowRef = windowRef;
return noErr;
}
// ******************************************************************************** doOpenFile
OSStatus doOpenFile(FSSpec fileSpec,OSType fileType)
{
OSStatus osStatus = noErr;
WindowRef windowRef;
AliasHandle aliasHdl;
if(osStatus = doNewDocWindow(&windowRef,&fileSpec,fileType))
return osStatus;
SetWTitle(windowRef,fileSpec.name);
SetWindowProxyFSSpec(windowRef,&fileSpec);
GetWindowProxyAlias(windowRef,&aliasHdl);
SetWindowProperty(windowRef,kFileCreator,'tALH',sizeof(AliasHandle),&aliasHdl);
SetWindowModified(windowRef,false);
return noErr;
}
// ******************************************************************************* doCloseFile
void doCloseWindow(WindowRef windowRef,TXNObject txnObject)
{
TXNDeleteObject(txnObject);
DisposeWindow(windowRef);
gCurrentNumberOfWindows --;
if(gCurrentNumberOfWindows == 0)
doEnableDisableMenus(false);
}
// ******************************************************************************* doWriteFile
OSStatus doWriteFile(WindowRef windowRef,Boolean newFile)
{
TXNPermanentTextEncodingType encodingType;
TXNObject txnObject = NULL;
FSSpec fileSpec, fileSpecTemp;
TXNFileType txnFileType;
UInt32 currentTime;
Str255 tempFileName;
OSStatus osStatus = noErr;
SInt16 tempFileVolNum, tempFileRefNum, tempResForkRefNum = -1;
SInt32 tempFileDirID;
Boolean hasResFile = false;
GetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),NULL,&txnObject);
GetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),NULL,&fileSpec);
GetWindowProperty(windowRef,kFileCreator,'FiTy',sizeof(TXNFileType),NULL,&txnFileType);
encodingType = (txnFileType == kTXNTextFile) ? kTXNMacOSEncoding : kTXNUnicodeEncoding;
GetDateTime(¤tTime);
NumToString((SInt32) currentTime,tempFileName);
osStatus = FindFolder(fileSpec.vRefNum,kTemporaryFolderType,kCreateFolder,&tempFileVolNum,
&tempFileDirID);
if(osStatus == noErr)
osStatus = FSMakeFSSpec(tempFileVolNum,tempFileDirID,tempFileName,&fileSpecTemp);
if(osStatus == noErr || osStatus == fnfErr)
osStatus = FSpCreate(&fileSpecTemp,'trsh','trsh',smSystemScript);
if(osStatus == noErr)
osStatus = FSpOpenDF(&fileSpecTemp,fsRdWrPerm,&tempFileRefNum);
if(osStatus == noErr)
{
if(txnFileType == kTXNTextFile)
{
FSpCreateResFile(&fileSpecTemp,'trsh','trsh',smSystemScript);
osStatus = ResError();
if(osStatus == noErr)
tempResForkRefNum = FSpOpenResFile(&fileSpecTemp,fsRdWrPerm);
hasResFile = true;
}
}
if(osStatus == noErr)
osStatus = TXNSave(txnObject,txnFileType,kTXNMultipleStylesPerTextDocumentResType,
encodingType,&fileSpec,tempFileRefNum,tempResForkRefNum);
if(osStatus == noErr)
osStatus = FSpExchangeFiles(&fileSpecTemp,&fileSpec);
if(osStatus == noErr)
osStatus = FSpDelete(&fileSpecTemp);
if(osStatus == noErr)
osStatus = FSClose(tempFileRefNum);
if(osStatus == noErr)
if(tempResForkRefNum != -1)
CloseResFile(tempResForkRefNum);
osStatus = ResError();
if(osStatus == noErr)
if(newFile)
osStatus = doCopyResources(fileSpec,txnFileType,hasResFile);
return osStatus;
}
// *************************************************************************** doCopyResources
OSStatus doCopyResources(FSSpec fileSpec,TXNFileType fileType,Boolean hasResFile)
{
OSStatus osStatus = noErr;
SInt16 fileRefNum;
if(!hasResFile)
FSpCreateResFile(&fileSpec,kFileCreator,fileType,smSystemScript);
osStatus = ResError();
if(osStatus == noErr)
fileRefNum = FSpOpenResFile(&fileSpec,fsRdWrPerm);
if(fileRefNum > 0)
osStatus = doCopyAResource('STR ',-16396,gAppResFileRefNum,fileRefNum);
else
osStatus = ResError();
if(osStatus == noErr)
CloseResFile(fileRefNum);
osStatus = ResError();
return osStatus;
}
// *************************************************************************** doCopyAResource
OSStatus doCopyAResource(ResType resourceType,SInt16 resourceID,SInt16 sourceFileRefNum,
SInt16 destFileRefNum)
{
Handle sourceResourceHdl;
Str255 sourceResourceName;
ResType ignoredType;
SInt16 ignoredID;
UseResFile(sourceFileRefNum);
sourceResourceHdl = GetResource(resourceType,resourceID);
if(sourceResourceHdl != NULL)
{
GetResInfo(sourceResourceHdl,&ignoredID,&ignoredType,sourceResourceName);
DetachResource(sourceResourceHdl);
UseResFile(destFileRefNum);
AddResource(sourceResourceHdl,resourceType,resourceID,sourceResourceName);
if(ResError() == noErr)
UpdateResFile(destFileRefNum);
}
ReleaseResource(sourceResourceHdl);
return ResError();
}
// ************************************************************************** navEventFunction
void navEventFunction(NavEventCallbackMessage callBackSelector,NavCBRecPtr callBackParms,
NavCallBackUserData callBackUD)
{
WindowRef windowRef;
switch(callBackSelector)
{
case kNavCBEvent:
switch(callBackParms->eventData.eventDataParms.event->what)
{
case updateEvt:
windowRef = (WindowRef) callBackParms->eventData.eventDataParms.event->message;
if(GetWindowKind(windowRef) != kDialogWindowKind)
doUpdate((EventRecord *) callBackParms->eventData.eventDataParms.event);
break;
}
break;
}
}
// *******************************************************************************************