Sources/MSAECut.c

// MSAECut.c
//
// Original version by Jon Lansdell and Nigel Humphreys.
// 4.0 and 3.1 updates by Greg Sutton.
// ©Apple Computer Inc 1996, all rights reserved.
 
#include "MSAECut.h"
 
#include "MSAEUtils.h"
#include "MSWindow.h"       // for DPtrFromWindowPtr()
 
#include "MSAESelect.h"
 
#include <Scrap.h>
 
 
#pragma segment AppleEvent
 
// Handle a cut to scrap e.g 'copy last word of document 1'
// If no reference is given then the selection of the front window
// is used.
     
pascal OSErr    DoCut(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
{
#ifdef __MWERKS__
    #pragma unused (reply, refcon)
#endif
 
    AEDesc      directObj = {typeNull, NULL};
    TextToken   aTextToken;
    short       ignore;
    OSErr       err;
 
    err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
    // If we get an error here it just means that they haven't supplied a reference to
    // an object to cut - so cut the current section instead.
    
    if (directObj.descriptorType != typeNull)
        err = CutDesc(&directObj);
    else
    {           // Just cut the selection of the front window
        err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
        if (noErr != err) goto done;
        
        err = CutTextToken(&aTextToken);
    }
 
done:   
    (void)AEDisposeDesc(&directObj);
        
    return(err);
} // DoCut
 
 
OSErr   CutTextToken(TextToken* theToken)
{
    WindowPtr       aWindow;
    DPtr            docPtr;
    OSErr           err;
    
    aWindow = theToken->tokenWindow;
    docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
    
    if (! aWindow || ! docPtr)
        return(errAENoSuchObject);
 
                    // Set this tokens selection
    err = SelectTextToken(theToken);
    if (noErr != err) goto done;
 
    err = (OSErr)ZeroScrap();
    TECut(docPtr->theText);     
            
    docPtr->dirty = true;
    AdjustScrollbars(docPtr, false);
    DrawPageExtras(docPtr);
    
done:
    return(err);
}
 
OSErr   CutTextDesc(AEDesc* textDesc)
{
    TextToken       aTextToken;
    Size            actualSize;
    OSErr           err;
 
    if (typeMyText != textDesc->descriptorType)
        return(errAETypeError);
        
    GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
 
    err = CutTextToken(&aTextToken);
    
    return(err);
}
 
OSErr   CutDesc(AEDesc* aDesc)
{
    AEDesc      cutDesc = {typeNull, NULL},
                textDesc = {typeNull, NULL};
    OSErr       err;
    
    if (typeObjectSpecifier == aDesc->descriptorType)
        err = AEResolve(aDesc, kAEIDoMinimum, &cutDesc);
    else if (typeNull != aDesc->descriptorType)
        err = AEDuplicateDesc(aDesc, &cutDesc);
        
    if (noErr != err) goto done;
    
    switch (cutDesc.descriptorType)
    {
        case typeAEList:
            err = errAETypeError;
            // We can't handle cutting more than one item to the scrap
            break;
            
        default:
            err = AECoerceDesc(&cutDesc, typeMyText, &textDesc);
            if (noErr != err) goto done;
            err = CutTextDesc(&textDesc);
    }
    
done:
    (void)AEDisposeDesc(&cutDesc);
    (void)AEDisposeDesc(&textDesc);
    
    return(err);
}