Source/SVAECut.c

/*
    File:       SVAECut.c
 
    Contains:   
 
    Written by: Original version by Jon Lansdell and Nigel Humphreys.
                3.1 updates by Greg Sutton.
 
    Copyright:  Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/20/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include "SVAECut.h"
 
#include "SVEditAEUtils.h"
#include "SVEditWindow.h"       // for DPtrFromWindowPtr()
 
#include "SVAESelect.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)
{
#pragma unused (reply, refcon)
 
    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:   
    if (directObj.dataHandle)
        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
        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:
    if (cutDesc.dataHandle)
        AEDisposeDesc(&cutDesc);
    if (textDesc.dataHandle)
        AEDisposeDesc(&textDesc);
    
    return(err);
}