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 >
Text File  |  1996-06-22  |  7KB  |  248 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * sample.drag.c
  4.  *--------------------------------------------------------------
  5.  */
  6. #include <Gestalt.h>
  7. #include <Drag.h>
  8. #include <AppleEvents.h>
  9. #include <Processes.h>
  10.  
  11. #include "sample.h"
  12. #include "sample.send.h"
  13. #include "custom.wind.h"
  14. #include "sample.alrt.h"
  15. #include "sample.drag.h"
  16.  
  17. /*
  18.  *--------------------------------------------------------------
  19.  * static globals for drag and drop handling
  20.  *--------------------------------------------------------------
  21.  */
  22. static DragTrackingHandlerUPP    MyTrackUPP  = nil;
  23. static DragReceiveHandlerUPP    MyReceiveUPP = nil;
  24. static Boolean    gThisIsAcceptable = false;
  25. static Boolean    gThisCanBeDropped = false;
  26.  
  27. /*
  28.  *--------------------------------------------------------------
  29.  * static function prototypes
  30.  *--------------------------------------------------------------
  31.  */
  32. static pascal OSErr TrackMyFileDragging(DragTrackingMessage, WindowRef, void *, DragReference);
  33. static pascal OSErr ReceiveMyDroppedFile(WindowRef, void *, DragReference);
  34. static OSErr CheckTheFlavorType(WindowRef, DragReference, Boolean *);
  35. static OSErr GetTheFilesFlavor(DragReference, HFSFlavor *);
  36. static OSErr SendASpecToMyself(const FSSpecPtr);
  37.  
  38. /*
  39.  *--------------------------------------------------------------
  40.  * SetUpMyDragAndDrop
  41.  *--------------------------------------------------------------
  42.  *    set up drag and drop procedure
  43.  *    if drag and drop manaher is not installed, do nothing in here
  44.  *--------------------------------------------------------------
  45.  */
  46. Boolean SetUpMyDragAndDrop(WindowRef theWindow)
  47. {
  48.     long    gestaltAnswer;
  49.     Boolean    dragAvailable;
  50.     OSErr    result;
  51.  
  52.     /* check if Drag Manager is available  */
  53.     result = Gestalt(gestaltDragMgrAttr, &gestaltAnswer);
  54.     dragAvailable =
  55.         (result == noErr && (gestaltAnswer & (1 << gestaltDragMgrPresent)) != 0);
  56.  
  57.     /* then, install my handlers */
  58.     if (dragAvailable) {
  59.         MyTrackUPP = NewDragTrackingHandlerProc(TrackMyFileDragging);
  60.         if (MyTrackUPP != nil) {
  61.             InstallTrackingHandler(MyTrackUPP, theWindow, nil);
  62.         }
  63.         MyReceiveUPP = NewDragReceiveHandlerProc(ReceiveMyDroppedFile);
  64.         if (MyReceiveUPP != nil) {
  65.             InstallReceiveHandler(MyReceiveUPP, theWindow, nil);
  66.         }
  67.     } else {
  68.         MyTrackUPP = nil;
  69.         MyReceiveUPP = nil;
  70.     }
  71.     return (dragAvailable);
  72. }
  73. /*
  74.  *--------------------------------------------------------------
  75.  * RemoveMyDragAndDrop
  76.  *--------------------------------------------------------------
  77.  *    remove drag and drop procedure
  78.  *    if drag and drop manaher is not installed, do nothing in here
  79.  *--------------------------------------------------------------
  80.  */
  81. void RemoveMyDragAndDrop(void)
  82. {
  83.     if (MyTrackUPP  != nil) {
  84.         RemoveTrackingHandler(MyTrackUPP, gMainWindow);
  85.         MyTrackUPP = nil;
  86.     }
  87.     if (MyReceiveUPP != nil) {
  88.         RemoveReceiveHandler(MyReceiveUPP, gMainWindow);
  89.         MyReceiveUPP = nil;
  90.     }
  91. }
  92. /*
  93.  *--------------------------------------------------------------
  94.  * TrackMyFileDragging
  95.  *--------------------------------------------------------------
  96.  *    drag and drop tracking handler for general files
  97.  *--------------------------------------------------------------
  98.  */
  99. static pascal OSErr TrackMyFileDragging(
  100.     DragTrackingMessage message,
  101.     WindowRef theWindow,
  102.     void *refCon,
  103.     DragReference theDrag)
  104. {
  105.     #pragma unused (theWindow, refCon)
  106.  
  107.     Point    itsAxis;
  108.     OSErr    result = dragNotAcceptedErr;
  109.  
  110.     switch (message) {
  111.     case dragTrackingEnterHandler:
  112.         result = CheckTheFlavorType(theWindow, theDrag, &gThisIsAcceptable);
  113.         break;
  114.     case dragTrackingEnterWindow:
  115.     case dragTrackingInWindow:
  116.     case dragTrackingLeaveWindow:
  117.         if (gThisIsAcceptable) {
  118.             result = GetDragMouse(theDrag, &itsAxis, nil);
  119.             if (result == noErr) {
  120.                 gThisCanBeDropped = IsMouseInTheButtons(itsAxis);
  121.             }
  122.         }
  123.         break;
  124.     case dragTrackingLeaveHandler:
  125.         itsAxis.v = itsAxis.h = -1;    /* bogus axis */
  126.         IsMouseInTheButtons(itsAxis);
  127.         break;
  128.     }
  129.     return (result);
  130. }
  131. /*
  132.  *--------------------------------------------------------------
  133.  * CheckTheFlavorType
  134.  *--------------------------------------------------------------
  135.  *    check the flavor type and set the flag
  136.  *--------------------------------------------------------------
  137.  */
  138. static OSErr CheckTheFlavorType(
  139.     WindowRef theWindow,
  140.     DragReference theDrag,
  141.     Boolean *isAcceptable)
  142. {
  143. #pragma unused (theWindow)
  144.  
  145.     ItemReference    contRef;
  146.     FlavorType        itemType;
  147.     short            items, i, j;
  148.     unsigned short    flavorItems;
  149.     OSErr            result;
  150.  
  151.     *isAcceptable = false;
  152.     result = CountDragItems(theDrag, (void*)&items);
  153.     if (result == noErr) {
  154.         for (i = 1; i <= items; i++)    {
  155.             result = GetDragItemReferenceNumber(theDrag, i, &contRef);
  156.             if (result == noErr) {
  157.                 result = CountDragItemFlavors(theDrag, contRef, &flavorItems);
  158.             }
  159.             if (result == noErr) {
  160.                 for (j = 1; j <= flavorItems; j++)    {
  161.                     result = GetFlavorType(theDrag, contRef, j, &itemType);
  162.                     if (result == noErr) {
  163.                         if (itemType == flavorTypeHFS) {
  164.                             *isAcceptable = true;
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.     }
  171.     return (result);
  172. }
  173. /*
  174.  *--------------------------------------------------------------
  175.  * ReceiveMyDroppedFile
  176.  *--------------------------------------------------------------
  177.  *    drag and drop receiving handler
  178.  *--------------------------------------------------------------
  179.  */
  180. static pascal OSErr ReceiveMyDroppedFile(
  181.     WindowRef theWindow,
  182.     void * refCon,
  183.     DragReference theDrag)
  184. {
  185. #pragma unused (theWindow, refCon)
  186.  
  187.     HFSFlavor    itsFlavor;
  188.     OSErr        result = dragNotAcceptedErr;
  189.  
  190.     if (gThisIsAcceptable && gThisCanBeDropped) {
  191.  
  192.         result = GetTheFilesFlavor(theDrag, &itsFlavor);
  193.         if (result == noErr) {
  194.             FSSpecPtr    itsSpec = &itsFlavor.fileSpec;
  195.             Boolean        isFolder, wasAlias;
  196.  
  197.             /* check if it is an alias */
  198.             result = ResolveAliasFile(itsSpec, true, &isFolder, &wasAlias);
  199.             if (result != noErr) {
  200.                 DoAliasAlert(itsSpec->name, result);
  201.             } else if (isFolder == false) {
  202.                 /* pass the dropped file to the button hanlder */
  203.                 Point    itsAxis;
  204.                 result = GetDragMouse(theDrag, &itsAxis, nil);
  205.                 if (result == noErr) {
  206.                     result =  DoReceiveOnTheButtons(itsSpec, itsAxis);
  207.                 }
  208.             } else {
  209.                 /* omit folders */
  210.                 result = dragNotAcceptedErr;
  211.             }
  212.         }
  213.     }    
  214.     return (result);
  215. }
  216. /*
  217.  *--------------------------------------------------------------
  218.  * GetTheFilesFlavor
  219.  *--------------------------------------------------------------
  220.  *    exsert the flavor from the drag reference
  221.  *--------------------------------------------------------------
  222.  */
  223. static OSErr GetTheFilesFlavor(DragReference theDrag, HFSFlavor *theFlavor)
  224. {
  225.     ItemReference    itsRefs;
  226.     FlavorFlags        flvrFlag;
  227.     Size    itsLength;
  228.     short    items, index;
  229.     OSErr    result;
  230.  
  231.     result = CountDragItems(theDrag, (void *)&items);
  232.     if (result == noErr) {
  233.         for (index = 1; index <= 1; index++) {    /* only one item */
  234.             result = GetDragItemReferenceNumber(theDrag, index, &itsRefs);
  235.             if (result == noErr) {
  236.                 result = GetFlavorFlags(theDrag, itsRefs, flavorTypeHFS, &flvrFlag);
  237.             }
  238.             if (result == noErr) {
  239.                 result = GetFlavorDataSize(theDrag, itsRefs, flavorTypeHFS, &itsLength);
  240.             }
  241.             if (result == noErr) {
  242.                 result = GetFlavorData(theDrag, itsRefs, flavorTypeHFS, theFlavor, &itsLength, 0);
  243.             }
  244.         }
  245.     }
  246.     return (result);
  247. }
  248.