Source/SVAECoercions.c

/*
    File:       SVAECoercions.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 "SVAECoercions.h"
 
#include "SVEditAEUtils.h"
#include "SVAETextUtils.h"
 
#include "SVAEAccessors.h"
#include "SVAESelect.h"
#include "SVToken.h"
 
 
#pragma segment AppleEvent
 
// Install coercion handlers that coerce a descriptor from one type to another.
 
OSErr   InstallCoercions(void)
{
    OSErr   err;
 
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyAppl,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyWndw,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyText,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyTextProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyWindowProp,(AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
    err = AEInstallCoercionHandler(typeObjectSpecifier, typeMyApplProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
 
    err = AEInstallCoercionHandler(typeMyWndw, typeMyText,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceWindowToText), 0, true, false);
    err = AEInstallCoercionHandler(typeMyWindowProp, typeMyText,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceWindowPropertyToText), 0, true, false);
 
    return(err);
}
 
 
// Takes an object specifier that it resolves using AEResolve
// then tries to coerce this result into the type specified by toType.
 
pascal OSErr    CoerceObjToAnything(const AEDesc    *theAEDesc,
                                    DescType        toType,
                                    long            handlerRefCon,
                                    AEDesc          *result)
{
#pragma unused (handlerRefCon)
 
    AEDesc  objDesc = {typeNull, NULL};
    OSErr   err;    
    
    if (theAEDesc->descriptorType != typeObjectSpecifier)
        return(errAEWrongDataType);
    
        // resolve the object specifier
    err = AEResolve(theAEDesc, kAEIDoMinimum, &objDesc);
    if (noErr != err) goto done;
        
        // hopefully it's the right type by now, but we'll give it a nudge
    err = AECoerceDesc(&objDesc, toType, result);
 
done:
    if (objDesc.dataHandle)
        AEDisposeDesc(&objDesc);
            
    return(err);
}   // CoerceObjToAnything
 
 
// A window is effectively a text item that contains all the text in the window
// so allow coercion of windows to text.
 
pascal OSErr    CoerceWindowToText(AEDesc   *theAEDesc,
                                    DescType        toType,
                                    long            handlerRefCon,
                                    AEDesc          *result)
{
#pragma unused (toType, handlerRefCon)
    
    return(TextDescFromWindowDesc(theAEDesc, result));
}
 
// Some window properties are effectively text so allow a coercion
// e.g. set selection of window 1 to "Something"
 
pascal OSErr    CoerceWindowPropertyToText(AEDesc       *theAEDesc,
                                            DescType    toType,
                                            long        handlerRefCon,
                                            AEDesc      *result)
{
#pragma unused (toType, handlerRefCon)
    
    WindowPropToken     aWindowPropToken;
    TextToken           aTextToken;
    Size                actualSize;
    short               ignore;
    OSErr               err;
 
    GetRawDataFromDescriptor(theAEDesc, (Ptr)&aWindowPropToken,
                                        sizeof(aWindowPropToken), &actualSize);
    
    switch (aWindowPropToken.tokenProperty)
    {
        case pText:
        case pContents:
            err = TextDescFromWindowToken(&(aWindowPropToken.tokenWindowToken), result);
            break;
 
        case pSelection:
            err = GetWindowSelection(aWindowPropToken.tokenWindowToken.tokenWindow,
                                                                    &aTextToken, &ignore);
            if (noErr != err) goto done;
            
            err = AECreateDesc(typeMyText, (Ptr)&aTextToken, sizeof(aTextToken), result);
            break;
            
        default:            // Most properties don't make sense to be coerced from
            err = errAECoercionFail;
    }                       // e.g 'select insertion point before bounds of document 1'
    
done:   
    return(err);
}