Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
source/DragCode.c
/* |
File: DragCode.c |
Description:Code that works with the Drag Manager to support drag and drop of pictures. |
Incoming drags only at this point. |
Author: MC |
Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
*/ |
#define TARGET_API_MAC_CARBON 1 |
#include <Drag.h> |
#include <QuickTimeComponents.h> |
#include <Windows.h> |
#include "DragCode.h" |
#include "GraphicImportDrawCode.h" |
#include "Structs.h" |
#include "Defines.h" |
#include "WindowCode.h" |
// Globals |
Boolean canAcceptDrag; |
extern OSErr gErr; |
extern OSType * gAcceptedFlavorTypes; |
extern long gNumGIComponents; |
static Boolean CanWeAcceptThis (DragReference theDrag) { |
unsigned short items, |
index; |
FlavorFlags theFlags; |
ItemReference theItem; |
OSErr err; |
short i; |
err = CountDragItems (theDrag, &items); |
if (err == noErr) { |
for (index = 1; index <= items; index++) { |
err = GetDragItemReferenceNumber (theDrag, index, &theItem); |
if (err == noErr) { |
i = 0; |
while (i <= gNumGIComponents) { |
err = GetFlavorFlags (theDrag, theItem, gAcceptedFlavorTypes[i++], &theFlags); |
if (err == noErr) { |
return (true); |
} |
} |
} |
} |
} |
return (false); |
} |
static Boolean GoodType (OSType theType) { |
Boolean isGood; |
short i; |
isGood = false; |
i = 0; |
do { |
if (theType == gAcceptedFlavorTypes[i++]) |
isGood = true; |
} while (isGood == false && i <= gNumGIComponents); |
return isGood; |
} |
pascal OSErr MyDefaultTrackingHandler (DragTrackingMessage theMessage, WindowPtr theWindow, void* handlerRefCon, DragReference theDrag) { |
#pragma unused (handlerRefCon) |
OSErr err; |
Rect winRect; |
err = noErr; |
switch (theMessage) { |
case kDragTrackingEnterHandler: |
break; |
case kDragTrackingEnterWindow: |
canAcceptDrag = CanWeAcceptThis (theDrag); |
if (canAcceptDrag == true) { |
RgnHandle winRgn = NewRgn (), |
insetWinRgn = NewRgn (), |
borderRgn = NewRgn (); |
GetWindowPortBounds (theWindow, &winRect); |
RectRgn (winRgn, &winRect); |
RectRgn (insetWinRgn, &winRect); |
InsetRgn (insetWinRgn, 2, 2); |
DiffRgn (winRgn, insetWinRgn, borderRgn); |
EraseRgn (borderRgn); |
InvalWindowRgn (theWindow, borderRgn); |
ShowDragHilite (theDrag, winRgn, true); |
DisposeRgn (winRgn); |
DisposeRgn (insetWinRgn); |
DisposeRgn (borderRgn); |
} |
break; |
case kDragTrackingInWindow: |
if (canAcceptDrag == false) { |
break; |
} else { |
// do whatever inside the window (like move an insertion point) |
} |
break; |
case kDragTrackingLeaveWindow: |
if (canAcceptDrag == true) { |
HideDragHilite (theDrag); |
} |
canAcceptDrag = false; |
break; |
case kDragTrackingLeaveHandler: |
break; |
} |
return err; |
} |
pascal OSErr MyDefaultReceiveHandler (WindowPtr theWindow, void* handlerRefCon, DragReference theDrag) { |
#pragma unused (handlerRefCon) |
unsigned short items, |
index; |
ItemReference theItem; |
OSType theType; |
unsigned short flavorNum; |
Size dataSize; |
WindowInfoHandle winInfo; |
SInt8 handleState; |
if (theWindow != nil) { |
winInfo = (WindowInfoHandle)GetWRefCon (theWindow); |
} |
if (winInfo != nil && (**winInfo).sig == kMySig) { |
if (canAcceptDrag == true) { |
gErr = CountDragItems (theDrag, &items); |
for (index = 1; index <= items; index++) { |
gErr = GetDragItemReferenceNumber (theDrag, index, &theItem); |
flavorNum = 1; |
do { |
gErr = GetFlavorType (theDrag, theItem, flavorNum++, &theType); |
} while (gErr == noErr && GoodType (theType) == false); |
if (gErr == noErr) { |
GetFlavorDataSize (theDrag, theItem, theType, &dataSize); |
if (dataSize > 0) { |
handleState = HGetState ((Handle)winInfo); |
HLock ((Handle)winInfo); |
ReleaseMemory (winInfo, false); |
(**winInfo).winGraphicType = theType; |
if (theType == 'PICT') { |
(**winInfo).winDataHandle = TempNewHandle (dataSize + 512, &gErr); |
} else { |
(**winInfo).winDataHandle = TempNewHandle (dataSize, &gErr); |
} |
if ((**winInfo).winDataHandle != nil) { |
if (theType == 'PICT') { |
gErr = GetFlavorData (theDrag, theItem, theType, *(**winInfo).winDataHandle + 512, &dataSize, 0); |
} else { |
gErr = GetFlavorData (theDrag, theItem, theType, *(**winInfo).winDataHandle, &dataSize, 0); |
} |
} else { |
gErr = cantGetFlavorErr; |
} |
if (gErr == noErr) { |
gErr = DrawHandle (theWindow); |
} |
HSetState ((Handle)winInfo, handleState); |
} |
} |
} |
} |
} |
return gErr; |
} |
OSErr InitDragManager (void) { |
OSErr err; |
DragTrackingHandlerUPP MyDefaultTrackingHandlerUPP; |
DragReceiveHandlerUPP MyDefaultReceiveHandlerUPP; |
MyDefaultTrackingHandlerUPP = NewDragTrackingHandlerProc (MyDefaultTrackingHandler); |
err = InstallTrackingHandler (MyDefaultTrackingHandlerUPP, 0L, nil); |
if (err == noErr) { |
MyDefaultReceiveHandlerUPP = NewDragReceiveHandlerProc (MyDefaultReceiveHandler); |
err = InstallReceiveHandler (MyDefaultReceiveHandlerUPP, 0L, nil); |
} |
return (err); |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14