home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Palettes / MiscDragViews / MiscViews.subproj / MiscIconWell.m < prev    next >
Encoding:
Text File  |  1994-03-17  |  4.3 KB  |  211 lines

  1. /***************************************************************************
  2.  * CLASS:        MiscIconWell
  3.  *
  4.  *    See the header file for more information.
  5.  *
  6.  * This object is included in the MiscKit by permission from the author
  7.  * and its use is governed by the MiscKit license, found in the file
  8.  * "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9.  * for a list of all applicable permissions and restrictions.
  10.  ***************************************************************************/
  11.  
  12. #import <misckit/MiscString.h>
  13. #import <misckit/MiscIconWell.h>
  14.  
  15.  
  16. @implementation MiscIconWell
  17.  
  18. - initFrame: (const NXRect *)frameRect
  19. {
  20.   const char *const types[] = {NXFilenamePboardType};
  21.   
  22.       [super initFrame: frameRect];
  23.     [self registerForDraggedTypes: (const char *const *)&types count: 1];
  24.     
  25.     filename = [ [MiscString alloc] init];
  26.     [self setAllowDoubleClickLaunch: YES];
  27.     
  28.     return self;
  29. }
  30.  
  31.  
  32.  
  33. - awake
  34.   const char *const types[] = {NXFilenamePboardType};
  35.   
  36.     [super awake];    
  37.     [self registerForDraggedTypes: (const char *const *)&types count: 1];
  38.     
  39.     return self;
  40. }
  41.  
  42.  
  43.  
  44. - free
  45. {
  46.     [filename free];
  47.     return [super free];
  48. }
  49.  
  50.  
  51. // Return the filename associated with the image. Kind of a hack to return
  52. // nil if no filename set, I suppose.
  53.  
  54. - (char *)filename
  55. {   
  56.      if (strcmp([filename stringValue], "") == 0)
  57.         return NULL;
  58.     else
  59.           return ((char *)[filename stringValue]);
  60. }
  61.  
  62.  
  63.  
  64. // Overridden from MiscDragView so the icon representation of the file
  65. // is fetched instead of the image itself (likely the filename is not
  66. // an image). It also has a small hack that if there are more than one
  67. // filename that it steals that multiple.tiff from Librarian.app. 
  68.  
  69. - setImageByFilename: (char *)aFilename
  70. {
  71.     [filename setStringValue: aFilename];
  72.         
  73.     if ([filename numWords] > 1)
  74.         [super setImageByFilename: "/NextApps/Librarian.app/multiple.tiff"];
  75.     else    
  76.         [self setImage: [ [Application workspace] getIconForFile: aFilename] ];
  77.     
  78.     return self;
  79. }
  80.  
  81.  
  82.  
  83. // Add additional options that only apply to MiscIconWell.
  84.  
  85. - setAllowDoubleClickLaunch: (BOOL)aBool
  86. {
  87.     allowDoubleClickLaunch = aBool;
  88.     return self;
  89. }
  90.  
  91.  
  92.  
  93. - (BOOL)allowDoubleClickLaunch
  94. {
  95.     return allowDoubleClickLaunch;
  96. }
  97.  
  98.  
  99.  
  100. // Override mouseDown to check if the icon was double clicked. If so, then
  101. // launch it from workspace, else let super handle it.
  102.  
  103. - mouseDown: (NXEvent *)theEvent
  104. {
  105.     if ([self allowDoubleClickLaunch] && theEvent->data.mouse.click > 1 && 
  106.             filename)
  107.         [ [Application workspace] openFile: [filename stringValue] ];
  108.     else
  109.         [super mouseDown: theEvent];
  110.     return self;
  111. }
  112.  
  113.  
  114.  
  115. // Make the dragPoint be the middle of the image, so it looks nice.
  116.  
  117. - calculateDragPoint: (NXPoint *)dragPoint andOffset: (NXPoint *)offset
  118. {
  119.     dragPoint->x -= imageSize.width/2.0;
  120.     dragPoint->y -= imageSize.width/2.0;
  121.  
  122.     return self;
  123. }
  124.  
  125.  
  126.     
  127. // Put the data on the pasteboard when a source drag takes place, and
  128. // also choose the image to drag. 
  129.  
  130. - (BOOL)setupForSourceDrag
  131. {
  132.   id  dragPB = [Pasteboard newName: NXDragPboard];
  133.   
  134.     if (filename != nil)
  135.     {
  136.         [dragPB declareTypes:&NXFilenamePboardType num:1 owner:self];
  137.  
  138.          [dragPB writeType: NXFilenamePboard data: [filename stringValue]
  139.                      length: [filename length] ];
  140.         dragImage = theImage;
  141.          
  142.         return YES;
  143.      }
  144.  
  145.     return NO;
  146. }
  147.  
  148.  
  149.  
  150.  
  151. // Check if an incoming dragged icon is using the NXFilenamePboardType. If
  152. // not, then don't accept the drag.
  153.  
  154. - (BOOL)performDragOperation: sender
  155. {
  156.   id  dragPB = [Pasteboard newName: NXDragPboard];
  157.   char  *pbData;
  158.   int  pbLength;
  159.  
  160.     if ([dragPB readType: NXFilenamePboardType data: &pbData 
  161.             length: &pbLength] == nil)
  162.         return NO;    
  163.      
  164.     return YES;
  165. }
  166.  
  167.  
  168. // Take the data from the pasteboard and set the new image.
  169.  
  170. - concludeDragOperation: sender
  171. {
  172.   id  dragPB = [Pasteboard newName: NXDragPboard];
  173.   char  *pbData;
  174.   int  pbLength;
  175.   
  176.     [dragPB readType: NXFilenamePboardType data: &pbData 
  177.             length: &pbLength];
  178.  
  179.     [self setImageByFilename: pbData];
  180.             
  181.     [dragPB deallocatePasteboardData: pbData length: pbLength];
  182.     
  183.     [super concludeDragOperation: sender];
  184.     
  185.     return self;
  186. }
  187.  
  188.  
  189.  
  190. // Archiving methods
  191.  
  192. - read: (NXTypedStream *)stream
  193. {
  194.     [super read: stream];
  195.     filename = NXReadObject (stream);
  196.     NXReadTypes (stream, "c", &allowDoubleClickLaunch);
  197.     return self;
  198. }
  199.  
  200.  
  201.  
  202. - write: (NXTypedStream *)stream
  203. {
  204.     [super write: stream];
  205.     NXWriteObject (stream, filename);
  206.     NXWriteTypes (stream, "c", &allowDoubleClickLaunch);
  207.     return self;
  208. }
  209.  
  210. @end