home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-25 | 20.4 KB | 668 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Actions.h
- // Release Version: $ ODF 1 $
- //
- // Author: Jim Lloyd
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef ACTIONS_H
- #include "Actions.h"
- #endif
-
- #ifndef DEFINES_K
- #include "Defines.k"
- #endif
-
- #ifndef BINDING_K
- #include "Binding.k"
- #endif
-
- #ifndef ENVIRONS_H
- #include "Environs.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWFILESP_H
- #include "FWFileSp.h"
- #endif
-
- #ifndef FWRESOUR_H
- #include "FWResour.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef FWODTYPS_H
- #include "FWODTyps.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODTranslation_xh
- #include <Translt.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODDragItemIterator_xh
- #include <DgItmIt.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
- #include <Drag.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
- #include <Sound.h>
- #endif
-
- //========================================================================================
- // Static Allocations
- //========================================================================================
-
- ComponentInstance CScriptAction::fgComponentInstance;
-
- //========================================================================================
- // Runtime information
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfbutton
- #endif
-
- //========================================================================================
- // class CAction
- //========================================================================================
-
- FW_DEFINE_CLASS_M0(CAction)
-
- //----------------------------------------------------------------------------------------
- // CAction::CAction
- //----------------------------------------------------------------------------------------
-
- CAction::CAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CAction::~CAction
- //----------------------------------------------------------------------------------------
-
- CAction::~CAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CAction::IsInStorage
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
- {
- return storage->Exists(ev, kODPropContents, kODFButtonKind, 0);
- }
-
- //----------------------------------------------------------------------------------------
- // CAction::CreateFromStorage
- //----------------------------------------------------------------------------------------
-
- CAction* CAction::CreateFromStorage(Environment* ev, ODStorageUnit* storage)
- {
- CAction* action;
- FW_PStorageUnitSink sink(ev, storage, kODPropContents, kODFButtonKind);
- FW_CReadableStream stream(sink);
- FW_READ_DYNAMIC_OBJECT(stream, &action, CAction);
- return action;
- }
-
- //----------------------------------------------------------------------------------------
- // CAction::WriteToStorage
- //----------------------------------------------------------------------------------------
-
- void CAction::WriteToStorage(Environment* ev, CAction* action, ODStorageUnit* storage)
- {
- FW_PStorageUnitSink sink(ev, storage, kODPropContents, kODFButtonKind);
- FW_CWritableStream stream(sink);
- FW_WRITE_DYNAMIC_OBJECT(stream, action, CAction);
- }
-
- //========================================================================================
- // class CSoundAction
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(CSoundAction, CAction)
-
- const FW_ClassTypeConstant LSoundAction = FW_TYPE_CONSTANT('S','N','D','A');
- FW_REGISTER_ARCHIVABLE_CLASS(LSoundAction, CSoundAction, CSoundAction::Read, 0, 0, CSoundAction::Write)
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::CSoundAction
- //----------------------------------------------------------------------------------------
-
- CSoundAction::CSoundAction(Environment* ev, ODStorageUnit* storage)
- : fSoundHandle(0)
- {
- Internalize(ev, storage);
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::CSoundAction
- //----------------------------------------------------------------------------------------
-
- CSoundAction::CSoundAction(Environment* ev, FW_CReadableStream& stream)
- : fSoundHandle(0)
- {
- InternalizeFromStream(ev, stream);
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::~CSoundAction
- //----------------------------------------------------------------------------------------
-
- CSoundAction::~CSoundAction()
- {
- if (fSoundHandle)
- ::DisposeHandle(fSoundHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::IsInStorage
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CSoundAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean result = false;
- if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0)) // 'snd ' on Scrap
- result = true;
- else if (storage->Exists(ev, kODPropContents, kSoundFileKind, 0)) // sound file
- result = true;
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Internalize
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::Internalize(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean internalized = FALSE;
-
- if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
- {
- // Mac 'snd ' in Scrap
- storage->Focus(ev,
- kODPropContents,
- kODPosUndefined,
- kSoundScrapKind,
- 0,
- kODPosUndefined);
- ODStorageUnitView* view = storage->CreateView(ev);
- {
- FW_PStorageUnitSink sink(ev, view);
- FW_CReadableStream stream(sink);
- InternalizeFromStream(ev, stream);
- }
- delete view;
-
- }
- else if (storage->Exists(ev, kODPropContents, kSoundFileKind, 0))
- {
- // Mac sound file
- storage->Focus(ev,
- kODPropContents,
- kODPosUndefined,
- FW_CPart::gMachfsDataType,
- 0,
- kODPosUndefined);
- InternalizeSoundFile(ev, storage);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::InternalizeFromStream
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::InternalizeFromStream(Environment* ev, FW_CReadableStream& stream)
- {
- FW_OSink* sink = stream.GetSink();
- unsigned long size = sink->GetReadableBytes(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- stream.Read(handle.GetPointer(), size);
- fSoundHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::InternalizeSound
- //----------------------------------------------------------------------------------------
- #if 0
- void CSoundAction::InternalizeSound(Environment* ev, ODStorageUnit* storage)
- {
- // Assume the storage unit is already focused
- unsigned long size = storage->GetSize(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- FW_CByteArray byteArray;
- storage->GetValue(ev, size, byteArray);
- byteArray.CopyBuffer(handle.GetPointer(), size);
- fSoundHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::InternalizeSoundFile
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::InternalizeSoundFile(Environment* ev, ODStorageUnit* storage)
- {
- // StorageUnit should already be Focused on HFSFlavor
- unsigned long hfsSize = storage->GetSize(ev);
- if (hfsSize > 0)
- {
- // Get the HFS flavor
- HFSFlavor hfsInfo;
- FW_CByteArray byteArray;
- storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
- byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
-
- // Create platform independent file spec
- FW_PFileSpecification fileSpec(ev, hfsInfo.fileSpec);
-
- // Open the resource file
- FW_PResourceFile resourceFile(ev, fileSpec);
-
- // Get any (i.e the first) sound resource
- const FW_ResourceType resourceType = 'snd ';
- const FW_ResourceId resourceId = 1;
- Handle sound = ::Get1IndResource(resourceType, resourceId);
-
- if (sound != NULL)
- {
- // if succeeded, detach the handle and keep it
- ::DetachResource(sound);
- fSoundHandle = sound;
- }
- else
- {
- // No sound resource loaded, throw an exception
- FW_FailOnError(::ResError());
- FW_Failure(resNotFound);
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Externalize
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::Externalize(Environment* ev, ODStorageUnit* storage)
- {
- if (fSoundHandle != NULL)
- {
- if (!storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
- storage->AddValue(ev, kSoundScrapKind);
-
- storage->Focus(ev, kODPropContents, kODPosUndefined, kSoundScrapKind, 0, kODPosUndefined);
- ODStorageUnitView* view = storage->CreateView(ev);
- {
- FW_PStorageUnitSink sink(ev, view);
- FW_CWritableStream stream(sink);
- ExternalizeToStream(ev, stream);
- }
- delete view;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::ExternalizeToStream
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::ExternalizeToStream(Environment* ev, FW_CWritableStream& stream)
- {
- if (fSoundHandle != NULL)
- {
- FW_CAcquireLockedSystemHandle lock(fSoundHandle);
- stream.Write(*fSoundHandle, FW_CMemoryManager::GetSystemHandleSize(fSoundHandle));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::DoIt
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::DoIt()
- {
- if (fSoundHandle != NULL)
- {
- FW_CAcquireLockedSystemHandle lock(fSoundHandle);
- ::SndPlay(NULL, (SndListHandle)fSoundHandle, TRUE);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Read
- //----------------------------------------------------------------------------------------
-
- void* CSoundAction::Read(FW_CReadableStream& stream, FW_ClassTypeConstant type)
- {
- FW_SOMEnvironment ev;
- return new CSoundAction(ev, stream);
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Write
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::Write(FW_CWritableStream& stream, FW_ClassTypeConstant type, const void *object)
- {
- FW_SOMEnvironment ev;
- CSoundAction* action = (CSoundAction*) object;
- action->ExternalizeToStream(ev, stream);
- }
-
- //========================================================================================
- // class CScriptAction
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(CScriptAction, CAction)
-
- const FW_ClassTypeConstant LScriptAction = FW_TYPE_CONSTANT('S','C','R','A');
- FW_REGISTER_ARCHIVABLE_CLASS(LScriptAction, CScriptAction, CScriptAction::Read, 0, 0, CScriptAction::Write)
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::CScriptAction
- //----------------------------------------------------------------------------------------
-
- CScriptAction::CScriptAction(Environment* ev, ODStorageUnit* storage)
- : fScriptHandle(0)
- {
- Internalize(ev, storage);
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::CScriptAction
- //----------------------------------------------------------------------------------------
-
- CScriptAction::CScriptAction(Environment* ev, FW_CReadableStream& stream)
- : fScriptHandle(0)
- {
- InternalizeFromStream(ev, stream);
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::~CScriptAction
- //----------------------------------------------------------------------------------------
-
- CScriptAction::~CScriptAction()
- {
- if (fScriptHandle)
- ::DisposeHandle(fScriptHandle);
- ReleaseScriptingComponent();
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::IsInStorage
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CScriptAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean result = false;
- if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0)) // 'scpt' on Scrap
- result = true;
- else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0)) // compiled script file
- result = true;
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Internalize
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::Internalize(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean internalized = FALSE;
-
- if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
- {
- // 'scpt' on Scrap
- storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
- ODStorageUnitView* view = storage->CreateView(ev);
- {
- FW_PStorageUnitSink sink(ev, view);
- FW_CReadableStream stream(sink);
- InternalizeFromStream(ev, stream);
- }
- delete view;
- }
- else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0)) // compiled script file
- {
- storage->Focus(ev, kODPropContents, kODPosUndefined, FW_CPart::gMachfsDataType, 0, kODPosUndefined);
- InternalizeScriptFile(ev, storage);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::InternalizeFromStream
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::InternalizeFromStream(Environment* ev, FW_CReadableStream& stream)
- {
- FW_OSink* sink = stream.GetSink();
- unsigned long size = sink->GetReadableBytes(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- stream.Read(handle.GetPointer(), size);
- fScriptHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::InternalizeScript
- //----------------------------------------------------------------------------------------
- #if 0
- void CScriptAction::InternalizeScript(Environment* ev, ODStorageUnit* storage)
- {
- // Assume the storage unit is already focused
- unsigned long size = storage->GetSize(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- FW_CByteArray byteArray;
- storage->GetValue(ev, size, byteArray);
- byteArray.CopyBuffer(handle.GetPointer(), size);
- fScriptHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::InternalizeScriptFile
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::InternalizeScriptFile(Environment* ev, ODStorageUnit* storage)
- {
- // StorageUnit should already be Focused on HFSFlavor
- unsigned long hfsSize = storage->GetSize(ev);
- if (hfsSize > 0)
- {
- // Get the HFS flavor
- HFSFlavor hfsInfo;
- FW_CByteArray byteArray;
- storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
- byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
-
- // Make the file specification
- FW_PFileSpecification fileSpec(ev, hfsInfo.fileSpec);
-
- // Acquire an opened resource file,
- // will be closed automatically
- FW_PResourceFile resourceFile(ev, fileSpec);
-
- // Acquire an opened resource,
- // will be released automatically
- FW_PResource resource(ev, resourceFile, 128, 'scpt');
- FW_CAcquireResourceData lockedResource(ev, resource);
-
- // Copy the resource data into a new handle
- FW_CAcquireTemporarySystemHandle lockedHandle(lockedResource.GetSize());
- FW_CMemoryManager::CopyMemory(lockedResource.GetData(), lockedHandle.GetPointer(), lockedResource.GetSize());
- fScriptHandle = lockedHandle.GetPlatformHandle();
- lockedHandle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Externalize
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::Externalize(Environment* ev, ODStorageUnit* storage)
- {
- if (fScriptHandle != NULL)
- {
- if (!storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
- storage->AddValue(ev, kScriptScrapKind);
-
- storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
- ODStorageUnitView* view = storage->CreateView(ev);
- {
- FW_PStorageUnitSink sink(ev, view);
- FW_CWritableStream stream(sink);
- ExternalizeToStream(ev, stream);
- }
- delete view;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::ExternalizeToStream
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::ExternalizeToStream(Environment* ev, FW_CWritableStream& stream)
- {
- if (fScriptHandle != NULL)
- {
- FW_CAcquireLockedSystemHandle lock(fScriptHandle);
- stream.Write(*fScriptHandle, FW_CMemoryManager::GetSystemHandleSize(fScriptHandle));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // LoadAndExecuteScript
- //----------------------------------------------------------------------------------------
- // This function was lifted almost verbatim from
- // IM:IAC, page 10-16. (Actually, I used MPTA, but it says
- // the code is on the above page)
-
- void CScriptAction::LoadAndExecuteScript(Handle scriptData)
- {
- AEDesc scriptDesc;
- OSAID scriptID;
- OSAID resultID;
- OSAError error;
-
- if (!HasAppleScript())
- {
- FW_CString32 name("ODFButton");
- FW_CString message;
- FW_SOMEnvironment ev;
- FW_CSharedLibraryResourceFile resFile(ev);
-
- ::FW_LoadStringByID(ev, resFile, kButtonDialogStrings, MULTISTRINGRES, kNoAppleScriptMsg, message);
- ::FW_NoteAlert(name, message);
- }
- else
- {
- // load the script data
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = scriptData;
-
- ComponentInstance componentInstance = GetScriptingComponent();
-
- error = ::OSALoad(componentInstance, &scriptDesc, kOSAModeNull, &scriptID);
-
- if (error == noErr)
- {
- // execute the compiled script in the default context
- error = ::OSAExecute(componentInstance, scriptID, kOSANullScript, kOSAModeNull, &resultID);
- error = ::OSADispose(componentInstance, scriptID);
- error = ::OSADispose(componentInstance, resultID);
- }
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::DoIt
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::DoIt()
- {
- if (fScriptHandle != NULL)
- LoadAndExecuteScript(fScriptHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Read
- //----------------------------------------------------------------------------------------
-
- void* CScriptAction::Read(FW_CReadableStream& stream, FW_ClassTypeConstant type)
- {
- FW_SOMEnvironment ev;
- return new CScriptAction(ev, stream);
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Write
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::Write(FW_CWritableStream& stream, FW_ClassTypeConstant type, const void *object)
- {
- FW_SOMEnvironment ev;
- CScriptAction* action = (CScriptAction*) object;
- action->ExternalizeToStream(ev, stream);
- }
-
-