Source/SVAEPaste.c

/*
    File:       SVAEPaste.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 "SVAEPaste.h"
 
#include "SVEditAEUtils.h"
#include "SVEditWindow.h"       // for DPtrFromWindowPtr()
 
#include "SVAESelect.h"
 
 
#pragma segment AppleEvent
 
// Handle a paste from scrap e.g 'paste last word of document 1' would change the
// last word in the front window to what's in the scrap.
// If no reference is given then the selection of the front window
// is used.
     
pascal OSErr    DoPaste(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 paste to - so paste to the current section instead.
    
    if (directObj.descriptorType != typeNull)
        err = PasteDesc(&directObj);
    else
    {           // Just paste to the selection of the front window
        err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
        if (noErr != err) goto done;
        
        err = PasteTextToken(&aTextToken);
    }
 
done:   
    if (directObj.dataHandle)
        AEDisposeDesc(&directObj);
        
    return(err);
}
 
 
OSErr   PasteTextToken(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;
 
    TEStylePaste(docPtr->theText);  
            
    docPtr->dirty = true;
    AdjustScrollbars(docPtr, false);
    DrawPageExtras(docPtr);
            
done:
    return(err);
}
 
OSErr   PasteTextDesc(AEDesc* textDesc)
{
    TextToken       aTextToken;
    Size            actualSize;
    OSErr           err;
 
    if (typeMyText != textDesc->descriptorType)
        return(errAETypeError);
        
    GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
 
    err = PasteTextToken(&aTextToken);
    
    return(err);
}
 
 
OSErr   PasteDesc(AEDesc* aDesc)
{
    AEDesc      pasteDesc = {typeNull, NULL},
                textDesc = {typeNull, NULL};
    OSErr       err;
    
    if (typeObjectSpecifier == aDesc->descriptorType)
        err = AEResolve(aDesc, kAEIDoMinimum, &pasteDesc);
    else
        err = AEDuplicateDesc(aDesc, &pasteDesc);
        
    if (noErr != err) goto done;
    
    switch (pasteDesc.descriptorType)
    {
        case typeAEList:
            err = errAETypeError;
            // We can't handle pasting more than one item from the scrap
            break;
            
        default:
            err = AECoerceDesc(&pasteDesc, typeMyText, &textDesc);
            if (noErr != err) goto done;
            err = PasteTextDesc(&textDesc);
    }
    
done:
    if (pasteDesc.dataHandle)
        AEDisposeDesc(&pasteDesc);
    if (textDesc.dataHandle)
        AEDisposeDesc(&textDesc);
    
    return(err);
}