home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
PRG
/
AESample.sit
/
AESample
/
sources
/
sample.drag.c
< prev
next >
Wrap
Text File
|
1996-06-22
|
7KB
|
248 lines
/*
*--------------------------------------------------------------
* sample.drag.c
*--------------------------------------------------------------
*/
#include <Gestalt.h>
#include <Drag.h>
#include <AppleEvents.h>
#include <Processes.h>
#include "sample.h"
#include "sample.send.h"
#include "custom.wind.h"
#include "sample.alrt.h"
#include "sample.drag.h"
/*
*--------------------------------------------------------------
* static globals for drag and drop handling
*--------------------------------------------------------------
*/
static DragTrackingHandlerUPP MyTrackUPP = nil;
static DragReceiveHandlerUPP MyReceiveUPP = nil;
static Boolean gThisIsAcceptable = false;
static Boolean gThisCanBeDropped = false;
/*
*--------------------------------------------------------------
* static function prototypes
*--------------------------------------------------------------
*/
static pascal OSErr TrackMyFileDragging(DragTrackingMessage, WindowRef, void *, DragReference);
static pascal OSErr ReceiveMyDroppedFile(WindowRef, void *, DragReference);
static OSErr CheckTheFlavorType(WindowRef, DragReference, Boolean *);
static OSErr GetTheFilesFlavor(DragReference, HFSFlavor *);
static OSErr SendASpecToMyself(const FSSpecPtr);
/*
*--------------------------------------------------------------
* SetUpMyDragAndDrop
*--------------------------------------------------------------
* set up drag and drop procedure
* if drag and drop manaher is not installed, do nothing in here
*--------------------------------------------------------------
*/
Boolean SetUpMyDragAndDrop(WindowRef theWindow)
{
long gestaltAnswer;
Boolean dragAvailable;
OSErr result;
/* check if Drag Manager is available */
result = Gestalt(gestaltDragMgrAttr, &gestaltAnswer);
dragAvailable =
(result == noErr && (gestaltAnswer & (1 << gestaltDragMgrPresent)) != 0);
/* then, install my handlers */
if (dragAvailable) {
MyTrackUPP = NewDragTrackingHandlerProc(TrackMyFileDragging);
if (MyTrackUPP != nil) {
InstallTrackingHandler(MyTrackUPP, theWindow, nil);
}
MyReceiveUPP = NewDragReceiveHandlerProc(ReceiveMyDroppedFile);
if (MyReceiveUPP != nil) {
InstallReceiveHandler(MyReceiveUPP, theWindow, nil);
}
} else {
MyTrackUPP = nil;
MyReceiveUPP = nil;
}
return (dragAvailable);
}
/*
*--------------------------------------------------------------
* RemoveMyDragAndDrop
*--------------------------------------------------------------
* remove drag and drop procedure
* if drag and drop manaher is not installed, do nothing in here
*--------------------------------------------------------------
*/
void RemoveMyDragAndDrop(void)
{
if (MyTrackUPP != nil) {
RemoveTrackingHandler(MyTrackUPP, gMainWindow);
MyTrackUPP = nil;
}
if (MyReceiveUPP != nil) {
RemoveReceiveHandler(MyReceiveUPP, gMainWindow);
MyReceiveUPP = nil;
}
}
/*
*--------------------------------------------------------------
* TrackMyFileDragging
*--------------------------------------------------------------
* drag and drop tracking handler for general files
*--------------------------------------------------------------
*/
static pascal OSErr TrackMyFileDragging(
DragTrackingMessage message,
WindowRef theWindow,
void *refCon,
DragReference theDrag)
{
#pragma unused (theWindow, refCon)
Point itsAxis;
OSErr result = dragNotAcceptedErr;
switch (message) {
case dragTrackingEnterHandler:
result = CheckTheFlavorType(theWindow, theDrag, &gThisIsAcceptable);
break;
case dragTrackingEnterWindow:
case dragTrackingInWindow:
case dragTrackingLeaveWindow:
if (gThisIsAcceptable) {
result = GetDragMouse(theDrag, &itsAxis, nil);
if (result == noErr) {
gThisCanBeDropped = IsMouseInTheButtons(itsAxis);
}
}
break;
case dragTrackingLeaveHandler:
itsAxis.v = itsAxis.h = -1; /* bogus axis */
IsMouseInTheButtons(itsAxis);
break;
}
return (result);
}
/*
*--------------------------------------------------------------
* CheckTheFlavorType
*--------------------------------------------------------------
* check the flavor type and set the flag
*--------------------------------------------------------------
*/
static OSErr CheckTheFlavorType(
WindowRef theWindow,
DragReference theDrag,
Boolean *isAcceptable)
{
#pragma unused (theWindow)
ItemReference contRef;
FlavorType itemType;
short items, i, j;
unsigned short flavorItems;
OSErr result;
*isAcceptable = false;
result = CountDragItems(theDrag, (void*)&items);
if (result == noErr) {
for (i = 1; i <= items; i++) {
result = GetDragItemReferenceNumber(theDrag, i, &contRef);
if (result == noErr) {
result = CountDragItemFlavors(theDrag, contRef, &flavorItems);
}
if (result == noErr) {
for (j = 1; j <= flavorItems; j++) {
result = GetFlavorType(theDrag, contRef, j, &itemType);
if (result == noErr) {
if (itemType == flavorTypeHFS) {
*isAcceptable = true;
}
}
}
}
}
}
return (result);
}
/*
*--------------------------------------------------------------
* ReceiveMyDroppedFile
*--------------------------------------------------------------
* drag and drop receiving handler
*--------------------------------------------------------------
*/
static pascal OSErr ReceiveMyDroppedFile(
WindowRef theWindow,
void * refCon,
DragReference theDrag)
{
#pragma unused (theWindow, refCon)
HFSFlavor itsFlavor;
OSErr result = dragNotAcceptedErr;
if (gThisIsAcceptable && gThisCanBeDropped) {
result = GetTheFilesFlavor(theDrag, &itsFlavor);
if (result == noErr) {
FSSpecPtr itsSpec = &itsFlavor.fileSpec;
Boolean isFolder, wasAlias;
/* check if it is an alias */
result = ResolveAliasFile(itsSpec, true, &isFolder, &wasAlias);
if (result != noErr) {
DoAliasAlert(itsSpec->name, result);
} else if (isFolder == false) {
/* pass the dropped file to the button hanlder */
Point itsAxis;
result = GetDragMouse(theDrag, &itsAxis, nil);
if (result == noErr) {
result = DoReceiveOnTheButtons(itsSpec, itsAxis);
}
} else {
/* omit folders */
result = dragNotAcceptedErr;
}
}
}
return (result);
}
/*
*--------------------------------------------------------------
* GetTheFilesFlavor
*--------------------------------------------------------------
* exsert the flavor from the drag reference
*--------------------------------------------------------------
*/
static OSErr GetTheFilesFlavor(DragReference theDrag, HFSFlavor *theFlavor)
{
ItemReference itsRefs;
FlavorFlags flvrFlag;
Size itsLength;
short items, index;
OSErr result;
result = CountDragItems(theDrag, (void *)&items);
if (result == noErr) {
for (index = 1; index <= 1; index++) { /* only one item */
result = GetDragItemReferenceNumber(theDrag, index, &itsRefs);
if (result == noErr) {
result = GetFlavorFlags(theDrag, itsRefs, flavorTypeHFS, &flvrFlag);
}
if (result == noErr) {
result = GetFlavorDataSize(theDrag, itsRefs, flavorTypeHFS, &itsLength);
}
if (result == noErr) {
result = GetFlavorData(theDrag, itsRefs, flavorTypeHFS, theFlavor, &itsLength, 0);
}
}
}
return (result);
}