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
/
MLTETextEditor.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-05-31
|
18KB
|
708 lines
// *******************************************************************************************
// MLTETextEditor.c
// *******************************************************************************************
// ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
#include "MLTETextEditor.h"
// …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
SInt16 gAppResFileRefNum;
Boolean gRunningOnX = false;
Boolean gDone;
TXNFontMenuObject gTXNFontMenuObject;
RgnHandle gCursorRgnHdl;
extern SInt16 gCurrentNumberOfWindows;
// ************************************************************************************** main
void main(void)
{
MenuBarHandle menubarHdl;
SInt32 response;
MenuRef menuRef;
OSStatus osStatus = noErr;
// ……………………………………………………………………………………………………………………………………………………………………………………………… do preliminaries
doPreliminaries();
// ………………………………………………………………………………………… save application's resource file file reference number
gAppResFileRefNum = CurResFile();
// ……………………………………………………………………………………………………………………………………………………………………… set up menu bar and menus
menubarHdl = GetNewMBar(rMenubar);
if(menubarHdl == NULL)
doErrorAlert(MemError());
SetMenuBar(menubarHdl);
CreateStandardWindowMenu(0,&menuRef);
SetMenuID(menuRef,mWindow);
InsertMenu(menuRef,0);
DeleteMenuItem(menuRef,1);
Gestalt(gestaltMenuMgrAttr,&response);
if(response & gestaltMenuMgrAquaLayoutMask)
{
menuRef = GetMenuRef(mFile);
if(menuRef != NULL)
{
DeleteMenuItem(menuRef,iQuit);
DeleteMenuItem(menuRef,iQuit - 1);
}
gRunningOnX = true;
}
// ……………………………………………………………………………………………………………… build hierarchical font menu and draw menu bar
menuRef = GetMenuRef(mFont);
osStatus = TXNNewFontMenuObject(menuRef,mFont,mFirstHierarchical,&gTXNFontMenuObject);
if(osStatus != noErr)
doErrorAlert(osStatus);
DrawMenuBar();
// ……………………………………………………………………………………………………………………………………… install required Apple event handlers
doInstallAEHandlers();
// ……………………………………………………………………………………………………………………………………………………………………………………………… enter event loop
eventLoop();
}
// *************************************************************************** doPreliminaries
void doPreliminaries(void)
{
MoreMasterPointers(960);
InitCursor();
FlushEvents(everyEvent,0);
doInitialiseMTLE();
}
// ************************************************************************** doInitializeMTLE
void doInitialiseMTLE(void)
{
TXNMacOSPreferredFontDescription defaultFont[1];
OSStatus osStatus = noErr;
SInt16 fontID;
GetFNum("\pNew York",&fontID);
defaultFont[0].fontID = fontID;
defaultFont[0].pointSize = 0x000C0000;
defaultFont[0].fontStyle = kTXNDefaultFontStyle;
defaultFont[0].encoding = kTXNSystemDefaultEncoding;
osStatus = TXNInitTextension(&defaultFont[0],1,kTXNWantMoviesMask);
if(osStatus != noErr)
doErrorAlert(osStatus);
}
// *********************************************************************** doInstallAEHandlers
void doInstallAEHandlers(void)
{
OSStatus osStatus = noErr;
osStatus = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAppEventHandler),
0L,false);
if(osStatus != noErr) doErrorAlert(eInstallHandler);
osStatus = AEInstallEventHandler(kCoreEventClass,kAEReopenApplication,
NewAEEventHandlerUPP((AEEventHandlerProcPtr) reopenAppEventHandler),
0L,false);
if(osStatus != noErr) doErrorAlert(eInstallHandler);
osStatus = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAndPrintDocsEventHandler),
kOpen,false);
if(osStatus != noErr) doErrorAlert(eInstallHandler);
osStatus = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAndPrintDocsEventHandler),
kPrint,false);
if(osStatus != noErr) doErrorAlert(eInstallHandler);
osStatus = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
NewAEEventHandlerUPP((AEEventHandlerProcPtr) quitAppEventHandler),
0L,false);
if(osStatus != noErr) doErrorAlert(eInstallHandler);
}
// ********************************************************************************* eventLoop
void eventLoop(void)
{
EventRecord eventStructure;
gDone = false;
gCursorRgnHdl = NewRgn();
while(!gDone)
{
if(WaitNextEvent(everyEvent,&eventStructure,doGetSleepTime(),gCursorRgnHdl))
doEvents(&eventStructure);
else
{
if(eventStructure.what == nullEvent)
{
doIdle();
doSynchroniseFiles();
}
}
}
}
// **************************************************************************** doGetSleepTime
UInt32 doGetSleepTime(void)
{
WindowRef windowRef;
UInt32 sleepTime;
TXNObject txnObject = NULL;
windowRef = FrontWindow();
if(isApplicationWindow(windowRef,&txnObject))
sleepTime = TXNGetSleepTicks(txnObject);
else
sleepTime = GetCaretTime();
return sleepTime;
}
// ************************************************************************************ doIdle
void doIdle(void)
{
WindowRef windowRef;
TXNObject txnObject = NULL;
windowRef = FrontWindow();
if(isApplicationWindow(windowRef,&txnObject))
{
if(TXNGetChangeCount(txnObject))
SetWindowModified(windowRef,true);
}
}
// ********************************************************************************** doEvents
void doEvents(EventRecord *eventStrucPtr)
{
WindowRef windowRef;
TXNObject txnObject = NULL;
switch(eventStrucPtr->what)
{
case kHighLevelEvent:
AEProcessAppleEvent(eventStrucPtr);
break;
case mouseDown:
doMouseDown(eventStrucPtr);
break;
case keyDown:
if(eventStrucPtr->modifiers & cmdKey)
{
doAdjustAndPrepareMenus();
doMenuChoice(MenuEvent(eventStrucPtr));
}
break;
case updateEvt:
doUpdate(eventStrucPtr);
break;
case activateEvt:
doActivate(eventStrucPtr);
break;
case osEvt:
switch((eventStrucPtr->message >> 24) & 0x000000FF)
{
case suspendResumeMessage:
if(eventStrucPtr->message & resumeFlag)
SetThemeCursor(kThemeArrowCursor);
break;
case mouseMovedMessage:
windowRef = FrontWindow();
if(isApplicationWindow(windowRef,&txnObject))
TXNAdjustCursor(txnObject,gCursorRgnHdl);
}
break;
}
}
// ******************************************************************************* doMouseDown
void doMouseDown(EventRecord *eventStrucPtr)
{
WindowRef windowRef;
WindowPartCode partCode;
OSStatus osStatus = noErr;
TXNObject txnObject = NULL;
Boolean handled = false;
SInt32 itemSelected;
partCode = FindWindow(eventStrucPtr->where,&windowRef);
switch(partCode)
{
case inMenuBar:
doAdjustAndPrepareMenus();
doMenuChoice(MenuSelect(eventStrucPtr->where));
break;
case inContent:
if(windowRef != FrontWindow())
SelectWindow(windowRef);
else
{
if(isApplicationWindow(windowRef,&txnObject))
TXNClick(txnObject,eventStrucPtr);
}
break;
case inGoAway:
if(TrackGoAway(windowRef,eventStrucPtr->where))
doCloseCommand(kNavSaveChangesClosingDocument);
break;
case inProxyIcon:
osStatus = TrackWindowProxyDrag(windowRef,eventStrucPtr->where);
if(osStatus == errUserWantsToDragWindow)
handled = false;
else if(osStatus == noErr)
handled = true;
case inDrag:
if(!handled)
{
if(IsWindowPathSelectClick(windowRef,eventStrucPtr))
{
if(WindowPathSelect(windowRef,NULL,&itemSelected) == noErr)
{
if(LoWord(itemSelected) > 1)
doBringFinderToFront();
}
handled = true;
}
}
if(!handled)
DragWindow(windowRef,eventStrucPtr->where,NULL);
if(isApplicationWindow(windowRef,&txnObject))
TXNAdjustCursor(txnObject,gCursorRgnHdl);
break;
case inGrow:
if(isApplicationWindow(windowRef,&txnObject))
{
TXNGrowWindow(txnObject,eventStrucPtr);
TXNAdjustCursor(txnObject,gCursorRgnHdl);
}
break;
case inZoomIn:
case inZoomOut:
if(TrackBox(windowRef,eventStrucPtr->where,partCode))
{
if(isApplicationWindow(windowRef,&txnObject))
{
TXNZoomWindow(txnObject,partCode);
TXNAdjustCursor(txnObject,gCursorRgnHdl);
}
}
break;
}
}
// ********************************************************************** doBringFinderToFront
void doBringFinderToFront(void)
{
ProcessSerialNumber finderProcess;
if(doFindProcess('MACS','FNDR',&finderProcess) == noErr)
SetFrontProcess(&finderProcess);
else
doErrorAlert(eCantFindFinderProcess);
}
// ***************************************************************************** doFindProcess
OSStatus doFindProcess(OSType creator,OSType type,ProcessSerialNumber *outProcSerNo)
{
ProcessSerialNumber procSerialNo;
ProcessInfoRec procInfoStruc;
OSStatus osStatus = noErr;
procSerialNo.highLongOfPSN = 0;
procSerialNo.lowLongOfPSN = kNoProcess;
procInfoStruc.processInfoLength = sizeof(ProcessInfoRec);
procInfoStruc.processName = NULL;
procInfoStruc.processAppSpec = NULL;
procInfoStruc.processLocation = NULL;
while(true)
{
osStatus = GetNextProcess(&procSerialNo);
if(osStatus != noErr)
break;
osStatus = GetProcessInformation(&procSerialNo,&procInfoStruc);
if(osStatus != noErr)
break;
if((procInfoStruc.processSignature == creator) && (procInfoStruc.processType == type))
break;
}
*outProcSerNo = procSerialNo;
return osStatus;
}
// ******************************************************************************** doActivate
void doActivate(EventRecord *eventStrucPtr)
{
WindowRef windowRef;
TXNObject txnObject = NULL;
Boolean becomingActive;
TXNFrameID txnFrameID = 0;
windowRef = (WindowRef) eventStrucPtr->message;
if(isApplicationWindow(windowRef,&txnObject))
{
becomingActive = ((eventStrucPtr->modifiers & activeFlag) == activeFlag);
GetWindowProperty(windowRef,kFileCreator,'tFRM',sizeof(TXNFrameID),NULL,&txnFrameID);
if(becomingActive)
TXNActivate(txnObject,txnFrameID,becomingActive);
else
TXNActivate(txnObject,txnFrameID,becomingActive);
TXNFocus(txnObject,becomingActive);
}
}
// ********************************************************************************** doUpdate
void doUpdate(EventRecord *eventStrucPtr)
{
WindowRef windowRef;
GrafPtr oldPort;
TXNObject txnObject = NULL;
windowRef = (WindowRef) eventStrucPtr->message;
GetPort(&oldPort);
SetPortWindowPort(windowRef);
if(isApplicationWindow(windowRef,&txnObject))
TXNUpdate(txnObject);
SetPort(oldPort);
}
// *********************************************************************** isApplicationWindow
Boolean isApplicationWindow(WindowRef windowRef,TXNObject *txnObject)
{
OSStatus osStatus = noErr;
osStatus = GetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),NULL,
txnObject);
return (windowRef != NULL) && (GetWindowKind(windowRef) == kApplicationWindowKind);
}
// ***************************************************************************** doAboutDialog
void doAboutDialog(void)
{
DialogRef dialogRef;
SInt16 itemHit;
dialogRef = GetNewDialog(rAboutDialog,NULL,(WindowRef) -1);
ModalDialog(NULL,&itemHit);
DisposeDialog(dialogRef);
}
// ************************************************************************ doSynchroniseFiles
void doSynchroniseFiles(void)
{
UInt32 currentTicks;
WindowRef windowRef;
static UInt32 nextSynchTicks = 0;
OSStatus hasNoAliasHdl = noErr;
Boolean aliasChanged;
AliasHandle aliasHdl = NULL;
FSSpec newFSSpec;
OSStatus osStatus = noErr;
SInt16 trashVRefNum;
SInt32 trashDirID;
TXNObject txnObject = NULL;
currentTicks = TickCount();
windowRef = FrontWindow();
if(currentTicks > nextSynchTicks)
{
while(windowRef != NULL)
{
hasNoAliasHdl = GetWindowProperty(windowRef,kFileCreator,'tALH',sizeof(AliasHandle),
NULL,&aliasHdl);
if(hasNoAliasHdl)
break;
aliasChanged = false;
ResolveAlias(NULL,aliasHdl,&newFSSpec,&aliasChanged);
if(aliasChanged)
{
SetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),&newFSSpec);
SetWTitle(windowRef,newFSSpec.name);
}
osStatus = FindFolder(kUserDomain,kTrashFolderType,kDontCreateFolder,
&trashVRefNum,&trashDirID);
if(osStatus == noErr)
{
do
{
if(newFSSpec.parID == fsRtParID)
break;
if((newFSSpec.vRefNum == trashVRefNum) && (newFSSpec.parID == trashDirID))
{
GetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),NULL,
&txnObject);
TXNDeleteObject(txnObject);
DisposeWindow(windowRef);
gCurrentNumberOfWindows --;
break;
}
} while(FSMakeFSSpec(newFSSpec.vRefNum,newFSSpec.parID,"\p",&newFSSpec) == noErr);
}
windowRef = GetNextWindow(windowRef);
}
nextSynchTicks = currentTicks + 15;
}
}
// *********************************************************************** openAppEventHandler
OSStatus openAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,SInt32 handlerRefCon)
{
OSStatus osStatus = noErr;
osStatus = doHasGotRequiredParams(appEvent);
if(osStatus == noErr)
osStatus = doNewCommand();
return osStatus;
}
// ********************************************************************* reopenAppEventHandler
OSStatus reopenAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,
SInt32 handlerRefCon)
{
OSStatus osStatus = noErr;
osStatus = doHasGotRequiredParams(appEvent);
if(osStatus == noErr)
if(!FrontWindow())
osStatus = doNewCommand();
return osStatus;
}
// ************************************************************** openAndPrintDocsEventHandler
OSStatus openAndPrintDocsEventHandler(AppleEvent *appEvent,AppleEvent *reply,
SInt32 handlerRefcon)
{
FSSpec fileSpec;
AEDescList docList;
OSStatus osStatus, ignoreErr;
SInt32 index, numberOfItems;
Size actualSize;
AEKeyword keyWord;
DescType returnedType;
FInfo fileInfo;
TXNObject txnObject;
osStatus = AEGetParamDesc(appEvent,keyDirectObject,typeAEList,&docList);
if(osStatus == noErr)
{
osStatus = doHasGotRequiredParams(appEvent);
if(osStatus == noErr)
{
osStatus = AECountItems(&docList,&numberOfItems);
if(osStatus == noErr)
{
for(index=1;index<=numberOfItems;index++)
{
osStatus = AEGetNthPtr(&docList,index,typeFSS,&keyWord,&returnedType,
&fileSpec,sizeof(fileSpec),&actualSize);
if(osStatus == noErr)
{
osStatus = FSpGetFInfo(&fileSpec,&fileInfo);
if(osStatus == noErr)
{
if(osStatus = doOpenFile(fileSpec,fileInfo.fdType))
doErrorAlert(osStatus);
if(osStatus == noErr && handlerRefcon == kPrint)
{
if(isApplicationWindow(FrontWindow(),&txnObject))
{
if(osStatus = TXNPrint(txnObject))
doErrorAlert(osStatus);
if(osStatus = doCloseCommand(kNavSaveChangesOther))
doErrorAlert(osStatus);
}
}
}
}
else
doErrorAlert(osStatus);
}
}
}
else
doErrorAlert(osStatus);
ignoreErr = AEDisposeDesc(&docList);
}
else
doErrorAlert(osStatus);
return osStatus;
}
// *********************************************************************** quitAppEventHandler
OSStatus quitAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,SInt32 handlerRefcon)
{
OSStatus osStatus = noErr;
osStatus = doHasGotRequiredParams(appEvent);
if(osStatus == noErr)
{
while(FrontWindow())
{
osStatus = doCloseCommand(kNavSaveChangesQuittingApplication);
if(osStatus != noErr && osStatus != kNavAskSaveChangesCancel)
doErrorAlert(osStatus);
if(osStatus == kNavAskSaveChangesCancel)
return noErr;
}
}
gDone = true;
return osStatus;
}
// ******************************************************************** doHasGotRequiredParams
OSStatus doHasGotRequiredParams(AppleEvent *appEvent)
{
DescType returnedType;
Size actualSize;
OSStatus osStatus = noErr;
osStatus = AEGetAttributePtr(appEvent,keyMissedKeywordAttr,typeWildCard,&returnedType,
NULL,0,&actualSize);
if(osStatus == errAEDescNotFound)
osStatus = noErr;
else if(osStatus == noErr)
osStatus = errAEParamMissed;
return osStatus;
}
// ****************************************************************************** doErrorAlert
void doErrorAlert(SInt16 errorCode)
{
Str255 errorString, theString;
SInt16 itemHit;
if(errorCode == kATSUFontsMatched)
return;
if(errorCode == eInstallHandler)
GetIndString(errorString,rErrorStrings,1);
else if(errorCode == eMaxWindows)
GetIndString(errorString,rErrorStrings,2);
else if(errorCode == eCantFindFinderProcess)
GetIndString(errorString,rErrorStrings,3);
else
{
GetIndString(errorString,rErrorStrings,4);
NumToString((SInt32) errorCode,theString);
doConcatPStrings(errorString,theString);
}
if(errorCode != memFullErr)
StandardAlert(kAlertCautionAlert,errorString,NULL,NULL,&itemHit);
else
{
StandardAlert(kAlertStopAlert,errorString,NULL,NULL,&itemHit);
ExitToShell();
}
}
// ***************************************************************************** doCopyPString
void doCopyPString(Str255 sourceString,Str255 destinationString)
{
SInt16 stringLength;
stringLength = sourceString[0];
BlockMove(sourceString + 1,destinationString + 1,stringLength);
destinationString[0] = stringLength;
}
// ************************************************************************** doConcatPStrings
void doConcatPStrings(Str255 targetString,Str255 appendString)
{
SInt16 appendLength;
appendLength = MIN(appendString[0],255 - targetString[0]);
if(appendLength > 0)
{
BlockMoveData(appendString+1,targetString+targetString[0]+1,(SInt32) appendLength);
targetString[0] += appendLength;
}
}
// *******************************************************************************************