home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / OpenDoc / CreditNow! / CreditNow! Source / $Sources / CPCreditNowDemo.DD.cpp < prev    next >
Encoding:
Text File  |  1995-04-23  |  8.7 KB  |  283 lines  |  [TEXT/MPCC]

  1. //----------------------------------------------------------------------------------------
  2. // DragAndDrop protocol
  3. //----------------------------------------------------------------------------------------
  4.  
  5. #ifndef _CREDITNOWDEMO_
  6. #include "CPCreditNowDemo.h"
  7. #endif
  8.  
  9. #ifndef __DRAG__
  10. #include <Drag.h>
  11. #endif
  12.  
  13. #ifndef SOM_ODDragItemIterator_xh
  14. #include <DgItmIt.xh>
  15. #endif
  16.  
  17. #ifndef SOM_ODDragAndDrop_xh
  18. #include <DragDrp.xh>
  19. #endif
  20.  
  21. #if GENERATING68K
  22. #pragma segment DragDrop
  23. #endif
  24.  
  25. //----------------------------------------------------------------------------------------
  26.  
  27. ODBoolean CPCreditNowDemo::PrivValidDataOnSU( Environment* ev, ODStorageUnit *storageUnit)
  28. {
  29.     HFSFlavor *valueData;
  30.     long valueSize;
  31.     ODBoolean validDataWasFound = kODFalse;
  32.  
  33.     if ( storageUnit->Exists( ev, kODPropContents, kODAppleTEXT, 0) || 
  34.          storageUnit->Exists( ev, kODPropContents, kCreditNowDemoKind, 0) )
  35.     {
  36.         validDataWasFound = kODTrue;
  37.     } else {
  38.         if ( storageUnit->Exists(ev, kODPropContents, kODApplehfs, 0) )
  39.         {
  40.             storageUnit->Focus( ev, kODPropContents, kODPosUndefined, kODApplehfs, 0, kODPosUndefined );        
  41.             valueSize = storageUnit->GetSize(ev);
  42.             valueData = (HFSFlavor *)::NewPtr(valueSize);
  43.             if (valueData)
  44.             {
  45.                 StorageUnitGetValue( storageUnit, ev, valueSize, (ODValue)valueData);
  46.                 if (valueData->fileType == 'TEXT')
  47.                     validDataWasFound = kODTrue;
  48.             }
  49.             ::DisposePtr((Ptr)valueData);
  50.         }
  51.     }
  52.     return validDataWasFound;
  53. }
  54.  
  55. //----------------------------------------------------------------------------------------
  56.  
  57. void CPCreditNowDemo::FulfillPromise(Environment* ev, ODStorageUnitView *promiseSUView)
  58. {
  59.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_FulfillPromise
  60.     EnteringMethod("\pCPCreditNowDemo::FulfillPromise");
  61.     ODUnused(promiseSUView);
  62. }
  63.  
  64. //----------------------------------------------------------------------------------------
  65.  
  66. void CPCreditNowDemo::DropCompleted(Environment* ev, ODPart* destPart, ODDropResult dropResult)
  67. {
  68.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_DropCompleted
  69.     EnteringMethod("\pCPCreditNowDemo::DropCompleted");
  70.     ODUnused(destPart);
  71.     ODUnused(dropResult);
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75.  
  76. ODDragResult CPCreditNowDemo::DragEnter(Environment* ev, ODDragItemIterator* dragInfo,
  77.                                                      ODFacet* facet, ODPoint* where)
  78. {
  79.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_DragEnter
  80.     EnteringMethod("\pCPCreditNowDemo::DragEnter");
  81.     ODUnused(facet);
  82.     ODUnused(where);
  83.  
  84.     ODStorageUnit *dragSU;
  85.     
  86.     fAcceptThisDrag = kODFalse;
  87.     fActiveFrameHilitedForDrop = kODFalse;
  88.     for (dragSU = dragInfo->First(ev); dragSU; dragSU = dragInfo->Next(ev))
  89.     {
  90.         fAcceptThisDrag = fAcceptThisDrag || this->PrivValidDataOnSU( ev,dragSU );
  91.     }
  92.  
  93.     return fAcceptThisDrag;
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97.  
  98. ODDragResult CPCreditNowDemo::DragWithin(Environment* ev, ODDragItemIterator* dragInfo,
  99.                                                       ODFacet* facet, ODPoint* where)
  100. {
  101.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_DragWithin
  102.     EnteringMethod("\pCPCreditNowDemo::DragWithin");
  103.     ODUnused(dragInfo);
  104.     ODUnused(where);
  105.  
  106.     if ( fAcceptThisDrag )
  107.     {
  108.         CFocus myFocus(ev, facet);
  109.  
  110.         ODFrame* displayFrame    = facet->GetFrame(ev);
  111.         ODShape* frameShape        = displayFrame->GetFrameShape(ev, kODNULL);
  112.         ODULong attributes        = fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  113.         DragReference theDrag    = fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  114.  
  115.         if ( (! fActiveFrameHilitedForDrop) && ((attributes & kODDragIsInSourcePart) == kODFalse) )
  116.         {
  117.             RgnHandle dragHiliteRgn = frameShape->GetQDRegion(ev);
  118.             ::ShowDragHilite(theDrag, dragHiliteRgn, kODTrue);
  119.             fActiveFrameHilitedForDrop = kODTrue;
  120.         }
  121.     }
  122.  
  123.     return fAcceptThisDrag;
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127.  
  128. void CPCreditNowDemo::DragLeave(Environment* ev, ODFacet* facet, ODPoint* where)
  129. {
  130.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_DragLeave
  131.     EnteringMethod("\pCPCreditNowDemo::DragLeave");
  132.     ODUnused(facet);
  133.     ODUnused(where);
  134.  
  135.     ODULong attributes        = fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  136.     DragReference theDrag    = fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  137.     ODPoint windowWhere        = facet->GetWindowFrameTransform(ev, kODNULL)->TransformPoint(ev,where);
  138.     ODFrame* displayFrame    = facet->GetFrame(ev);
  139.     
  140.     if( fAcceptThisDrag )
  141.     {
  142.         CFocus myFocus(ev, facet);
  143.         if (fActiveFrameHilitedForDrop ) 
  144.         {
  145.             ::HideDragHilite(theDrag);
  146.             fActiveFrameHilitedForDrop = kODFalse;
  147.         }
  148.     }
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152.  
  153. ODDropResult CPCreditNowDemo::Drop(Environment* ev, ODDragItemIterator* dropInfo,
  154.                                                 ODFacet* facet, ODPoint* where)
  155. {
  156.     // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_Drop
  157.     EnteringMethod("\pCPCreditNowDemo::Drop");
  158.     ODUnused(dropInfo);
  159.     ODUnused(facet);
  160.     ODUnused(where);
  161.  
  162.     ODDropResult dropResult = kODDropFail;
  163.     
  164.     if ( fActiveFrameHilitedForDrop )
  165.     {
  166.         short mouseDownModifiers;
  167.         short mouseUpModifiers;
  168.         Handle dropPicHandle = NULL;
  169.         Boolean notDone = kODTrue;
  170.     
  171.         ODULong attributes = fSession->GetDragAndDrop(ev)->GetDragAttributes(ev);
  172.         DragReference theDrag = fSession->GetDragAndDrop(ev)->GetDragReference(ev);
  173.         ::GetDragModifiers(theDrag, 0L, &mouseDownModifiers, &mouseUpModifiers);
  174.         
  175.         if (attributes & kODDropIsMove)
  176.             dropResult = kODDropMove;
  177.         else
  178.             dropResult = kODDropCopy;
  179.             
  180.         // make new TextEdit record
  181.         Rect theRect;
  182.         SetRect(&theRect, 0, 0, 0, 0);
  183.         TEHandle tempTE            = TEStyleNew(&theRect, &theRect);
  184.         StScrpHandle stylHandle = kODNULL;
  185.         OSErr error                = noErr;
  186.         short fileRefNum        = 0;
  187.         Size textSize            = 0;
  188.         ODPtr textData            = kODNULL;
  189.         ODPtr fileData            = kODNULL;
  190.     
  191.         // iterate over all SU's and collect the styled text
  192.         TRY
  193.         for (ODStorageUnit* dropSU = dropInfo->First(ev); dropSU && notDone; dropSU = dropInfo->Next(ev))
  194.         {
  195.             if (dropSU->Exists(ev, kODPropContents, kODAppleTEXT, 0))
  196.             {
  197.                 dropSU->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  198.                 textSize = dropSU->GetSize(ev);
  199.                 textData = ODNewPtr(textSize, kDefaultHeapID);
  200.                 StorageUnitGetValue(dropSU, ev, textSize, (ODValue)textData);
  201.         
  202.                 if (dropSU->Exists(ev, kODPropContents, kODApplestyl, 0))
  203.                 {
  204.                     dropSU->Focus(ev, kODPropContents, kODPosUndefined, kODApplestyl, 0, kODPosUndefined);
  205.                     Size stylSize = dropSU->GetSize(ev);
  206.                     stylHandle = (StScrpHandle) ODNewHandle(stylSize);
  207.                     StorageUnitGetValue(dropSU, ev, stylSize, (ODValue) ODLockHandle((ODHandle) stylHandle));
  208.                     ODUnlockHandle((ODHandle) stylHandle);
  209.                 }
  210.                 notDone = kODFalse;
  211.             } else {
  212.                 if (dropSU->Exists(ev, kODPropContents, kODApplehfs, 0))
  213.                 {
  214.                     dropSU->Focus(ev, kODPropContents, kODPosUndefined, kODApplehfs, 0, kODPosUndefined);        
  215.                     long fileSize = dropSU->GetSize(ev);        // really hfsflavor size
  216.                     fileData = ODNewPtr(fileSize, kDefaultHeapID);            // really hfsflavor data
  217.                     StorageUnitGetValue(dropSU, ev, fileSize, (ODValue)fileData);
  218.                     
  219.                     THROW_IF_ERROR(::FSpOpenDF((FSSpec*)&(((HFSFlavor*)fileData)->fileSpec), fsRdPerm, &fileRefNum));
  220.                     THROW_IF_ERROR(::GetEOF(fileRefNum, &textSize));
  221.                     if (textSize == 0)
  222.                         THROW(kODErrUndefined);
  223.                         
  224.                     textData = ODNewPtr(textSize, kDefaultHeapID);
  225.                     
  226.                     THROW_IF_ERROR(::SetFPos(fileRefNum,fsFromStart,0));
  227.                     THROW_IF_ERROR(::FSRead(fileRefNum,&textSize,textData));
  228.                     THROW_IF_ERROR(::FSClose(fileRefNum));
  229.                     notDone = kODFalse;
  230.                 }
  231.             }
  232.  
  233.             TESetSelect(32767, 32767, tempTE);
  234.             TEStyleInsert(textData, textSize, stylHandle, tempTE);
  235.     
  236.             if (textData)
  237.                 ODDisposePtr(textData);
  238.             if (stylHandle)
  239.                 ODDisposeHandle((ODHandle) stylHandle);
  240.         }
  241.  
  242.         CATCH_ALL
  243.         if (tempTE != kODNULL)
  244.             ::TEDispose(tempTE);
  245.         if (fileData != kODNULL)
  246.             ODDisposePtr(fileData);
  247.         if (stylHandle != kODNULL)
  248.             ODDisposeHandle((ODHandle) stylHandle);
  249.         if (fileRefNum != 0)
  250.             ::FSClose(fileRefNum);
  251.         dropResult = kODDropFail;
  252.         ENDTRY
  253.  
  254.         if ( (dropResult == kODDropCopy) || (dropResult == kODDropMove) )
  255.         {
  256.             // OK, we got the styled text and we're now moving that into a Str255
  257.             Size textSize = (**tempTE).teLength;
  258.             ODPtr textData = ODNewPtr(textSize, kDefaultHeapID);
  259.             if (! textData) 
  260.             {
  261.                 DebugStr("\pNewPtr error");
  262.                 ::TEDispose(tempTE);
  263.                 return kODDropFail ;
  264.             }
  265.             
  266.             // In reality, you would preserve the style information
  267.             if (textSize)
  268.             {
  269.                 Size maxSize = 255 - sizeof("\p by PartMaker");
  270.                 textSize = (textSize > maxSize) ? maxSize : textSize;
  271.                 ODBlockMove(*(**tempTE).hText, &fTextData[1], textSize);
  272.                 fTextData[0] = textSize;
  273.             }
  274.             ::TEDispose(tempTE);
  275.             
  276.             this->PrivSetDirty(ev);
  277.             facet->Invalidate(ev, kODNULL, kODNULL);
  278.         }
  279.     }
  280.     return dropResult;
  281. }
  282.  
  283.