home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / MiscKit1.2.6 / Examples / DragViewTest / MiscImageDragView.m < prev    next >
Encoding:
Text File  |  1994-05-28  |  3.3 KB  |  165 lines

  1. /**************************************************************************
  2.  * CLASS:        MiscImageDragView
  3.  *
  4.  *    See the header file for more information on this class.
  5.  **************************************************************************/
  6.  
  7. #import <appkit/Pasteboard.h> 
  8. #import <misckit/MiscImageDragView.h>
  9.  
  10.  
  11. @implementation MiscImageDragView
  12.  
  13. // Register to accept dragging, and setup some defaults.
  14.  
  15. - initFrame: (NXRect *)frameRect
  16. {    
  17.   const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType,
  18.                                   NXFilenamePboardType };
  19.  
  20.     [super initFrame: frameRect];
  21.     [self registerForDraggedTypes: (const char *const *)&types count: 3];
  22.     [self setDragIconRepresentation: YES];
  23.  
  24.     return self;
  25. }
  26.  
  27.  
  28.  
  29. - awake
  30. {
  31.   const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType,     
  32.                                   NXFilenamePboardType };
  33.  
  34.     [super awake];
  35.     [self registerForDraggedTypes: (const char *const *)&types count: 3];
  36.     
  37.     return self;
  38. }
  39.  
  40.  
  41.  
  42. //  Sets whether the TIFF itself or its TIFF icon representation is dragged.
  43.  
  44. - setDragIconRepresentation: (BOOL)aBool
  45. {
  46.     dragIconRepresentation = aBool;
  47.     return self;
  48. }
  49.  
  50.  
  51.  
  52. - (BOOL)dragIconRepresentation
  53. {
  54.     return dragIconRepresentation;
  55. }
  56.  
  57.  
  58.  
  59. // Put the image data on the NXTIFFPboardType. Also set the image that is
  60. // going to be used for the drag, depending upon whether 
  61. // dragIconRepresentation is true.
  62.  
  63. - (BOOL)setupForSourceDrag
  64. {
  65.   id  dragPB = [Pasteboard newName: NXDragPboard];
  66.   NXStream  *imageStream = NXOpenMemory (NULL, 0, NX_WRITEONLY);
  67.   const char *const types[] = { NXTIFFPboardType };
  68.     
  69.     // write the NXImage in TIFF form (it's original size)
  70.     
  71.     [dragPB declareTypes: types num: 1 owner: self];
  72.     
  73.     // put the image back to regular size and write it
  74.     
  75.     [ [self image] setSize: &imageSize];
  76.     [ [self image] writeTIFF: imageStream];
  77.     [dragPB writeType: NXTIFFPboardType fromStream: imageStream];
  78.  
  79.     NXCloseMemory (imageStream, NX_FREEBUFFER);        
  80.  
  81.     // if use icon rep. grab the image from somewhere that everyone has
  82.     
  83.     if ([self dragIconRepresentation])
  84.         dragImage = [ [NXImage alloc] initFromFile:     
  85.                 "/NextApps/Preview.app/tiff.tiff"];    
  86.     else
  87.         dragImage = [self image];    
  88.  
  89.     return YES;
  90. }
  91.  
  92.  
  93.  
  94. // Adjust the dragPoint so that you grab the image in the center.
  95.  
  96. - calculateDragPoint: (NXPoint *)dragPoint andOffset: (NXPoint *)offset
  97. {
  98.     if ([self dragIconRepresentation])
  99.     {
  100.         dragPoint->x -= 24.0;
  101.         dragPoint->y -= 24.0;
  102.      }
  103.     else
  104.     {
  105.       NXSize  imSize;
  106.       
  107.           [ [self image] getSize: &imSize];
  108.         dragPoint->x -= imSize.width / 2.0;
  109.         dragPoint->y -= imSize.height / 2.0;
  110.      }
  111.      
  112.     return self;
  113. }
  114.  
  115.  
  116.  
  117. // This sure is a complicated method. Actually NXImage takes care
  118. // of everything. It checks for filenames, correct extensions, data
  119. // on the correct pasteboards, and even accepts non native images if 
  120. // you have the filters.
  121.         
  122. - (BOOL)prepareForDragOperation: sender
  123. {
  124.     if ([NXImage canInitFromPasteboard: [sender draggingPasteboard] ])
  125.         return YES;
  126.  
  127.     return NO;
  128. }
  129.  
  130.  
  131.  
  132. // Take the pasteboard data and create the image.
  133.  
  134. - (BOOL)performDragOperation: sender
  135. {
  136.     [self setImage: [ [NXImage alloc] initFromPasteboard: 
  137.             [sender draggingPasteboard] ] ];
  138.  
  139.     return YES;
  140. }
  141.  
  142.  
  143.  
  144. // Archiving methods
  145.  
  146. - read: (NXTypedStream *)stream
  147. {
  148.     [super read: stream];
  149. //    NXReadTypes ("c", &dragIconRepresentation);
  150.  
  151.     return self;
  152. }
  153.  
  154.  
  155.  
  156. - write: (NXTypedStream *)stream
  157. {
  158.     [super write: stream];
  159. //    NXWriteTypes ("c", &dragIconRepresentation);
  160.  
  161.     return self;
  162. }
  163.  
  164. @end
  165.