home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / EOFramework / Querying / IconWell.m < prev    next >
Encoding:
Text File  |  1994-07-31  |  5.5 KB  |  256 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    IconWell
  9.  *
  10.  *    Inherits From:        View
  11.  *
  12.  *    Conforms To:        None
  13.  *
  14.  *    Declared In:        IconWell.h
  15.  *
  16.  *------------------------------------------------------------------------*/
  17. #import "IconWell.h"
  18. #import <appkit/appkit.h>
  19.  
  20.  
  21.  
  22.  
  23. @implementation IconWell
  24.  
  25. /*--------------------------------------------------------------------------
  26.  *    Private
  27.  *------------------------------------------------------------------------*/
  28. - (NXPoint) _centerPointForImage: anImage
  29. {
  30.     NXSize    imageSize;
  31.     NXRect    boundsRect;
  32.     
  33.     [self getBounds: &boundsRect];
  34.     [anImage getSize: &imageSize];
  35.     
  36.     if  (imageSize.width < NX_WIDTH (&boundsRect)) 
  37.         NX_X (&boundsRect) += (NX_WIDTH (&boundsRect) 
  38.             - imageSize.width ) / 2.0;
  39.     
  40.     if  (imageSize.height < NX_HEIGHT (&boundsRect)) 
  41.         NX_Y (&boundsRect) += (NX_HEIGHT (&boundsRect) 
  42.             - imageSize.height ) / 2.0;
  43.  
  44.     return boundsRect.origin;
  45. }
  46.  
  47.  
  48. - _dragOperationFor: (NXEvent*) originalEvent  nextEvent: (NXEvent*) nextEvent
  49. {
  50.      id         pasteboard;
  51.     NXPoint    offset = { 0.0, 0.0 };
  52.     NXRect    iconRect;
  53.        
  54.     if (path == NULL)  return self;
  55.     iconRect.origin = [self _centerPointForImage:icon];
  56.      pasteboard = [Pasteboard newName: NXDragPboard];
  57.     [pasteboard declareTypes:&NXFilenamePboardType num:1 owner:self];
  58.     [pasteboard writeType:NXFilenamePboardType data:path 
  59.         length:strlen(path)+1];
  60.         
  61.     [self dragImage:icon at:&iconRect.origin offset:&offset event:originalEvent 
  62.         pasteboard:pasteboard source:self slideBack:YES];
  63.         
  64.     if (pasteboard) [pasteboard free];
  65. //    [self notifyDelegateDidDrag];
  66.     return self;
  67. }
  68.  
  69.  
  70. /*--------------------------------------------------------------------------
  71.  *    Initialization and Freeing
  72.  *------------------------------------------------------------------------*/
  73. - initFrame:(const NXRect *)frameRect 
  74. {
  75.     [super initFrame: frameRect];
  76.     [self registerForDraggedTypes: &NXFilenamePboardType count: 1];
  77.       return self;
  78. }
  79.  
  80.  
  81. - free
  82. {
  83.     if (icon) [icon free];
  84.     if (path) NX_FREE (path);
  85.       return [super free];
  86. }
  87.  
  88.  
  89. /*--------------------------------------------------------------------------
  90.  *    Accessors
  91.  *------------------------------------------------------------------------*/
  92. - icon 
  93. {
  94.       return icon;
  95. }
  96.  
  97.  
  98. - setIcon:anIcon
  99. {
  100.     if (icon  &&  icon != anIcon)  [icon free];
  101.     icon = anIcon;
  102.     [self update];
  103.     return self;
  104. }
  105.     
  106.  
  107. - (const char *)path 
  108. {
  109.       return path;
  110. }
  111.  
  112.  
  113. - setPath:(const char *)aPath 
  114. {
  115.     if (path  &&  path != aPath)  NX_FREE (path);
  116.     path = aPath ? NXCopyStringBuffer(aPath) : NULL;
  117.     return self;
  118. }
  119.  
  120.  
  121. /*--------------------------------------------------------------------------
  122.  *    Event Handling
  123.  *------------------------------------------------------------------------*/
  124. - (BOOL)acceptsFirstMouse
  125. {
  126.       return YES;
  127. }
  128.  
  129.  
  130. - mouseDown: (NXEvent*) theEvent
  131. {
  132.     int        originalEventMask, newEventMask;
  133.     NXEvent    originalEvent = *theEvent, nextEvent;
  134.  
  135.     if (path == NULL) return self;
  136.     
  137.     [NXApp preventWindowOrdering];
  138.     originalEventMask = [window addToEventMask: NX_MOUSEDRAGGEDMASK];
  139.     newEventMask = (NX_MOUSEUPMASK | NX_MOUSEDRAGGEDMASK);
  140.     nextEvent = *([NXApp getNextEvent: newEventMask]);
  141.  
  142.     switch (nextEvent.type)
  143.         {
  144.         case NX_MOUSEDRAGGED:
  145. //            if ([self notifyDelegateWillDrag] == YES)
  146.             [self _dragOperationFor: &originalEvent  nextEvent: &nextEvent];
  147.             break;
  148.             
  149.         case NX_MOUSEUP:
  150.         default:
  151.             break;
  152.         }
  153.     
  154.     [window setEventMask: originalEventMask];
  155.         return self;
  156. }
  157.  
  158.  
  159. /*--------------------------------------------------------------------------
  160.  *    Delegate
  161.  *------------------------------------------------------------------------*/
  162. - delegate
  163. {
  164.       return delegate;
  165. }
  166.  
  167.  
  168. - setDelegate: anObject 
  169. {
  170.       delegate = anObject;
  171.       return self;
  172. }
  173.  
  174.  
  175. /*--------------------------------------------------------------------------
  176.  *    Drawing
  177.  *------------------------------------------------------------------------*/
  178. - drawSelf:(const NXRect *)rects :(int)rectCount 
  179. {
  180.     NXPoint    point = [self _centerPointForImage:icon];
  181.     
  182.      PSsetgray(NX_LTGRAY);
  183.       PSrectfill(bounds.origin.x, bounds.origin.y, bounds.size.width, 
  184.         bounds.size.height);
  185.  
  186.       [icon composite:NX_SOVER toPoint:&point];
  187.     return self;
  188. }
  189.  
  190.  
  191. /*--------------------------------------------------------------------------
  192.  *    Dragging
  193.  *------------------------------------------------------------------------*/
  194. - (NXDragOperation) draggingSourceOperationMaskForLocal: (BOOL)isLocal;
  195. {    
  196.     return NX_DragOperationCopy;
  197. }
  198.  
  199.  
  200. - (NXDragOperation) draggingEntered:dragSource
  201. {
  202.     return NX_DragOperationCopy;
  203. }
  204.  
  205.  
  206. - (NXDragOperation) draggingUpdated:dragSource
  207. {
  208.     if ([dragSource draggingSource] == self) 
  209.         return NX_DragOperationNone; 
  210.  
  211.     return NX_DragOperationCopy;
  212. }
  213.  
  214.  
  215. - (BOOL) prepareForDragOperation:dragSource
  216. {
  217. //    return [self notifyDelegateWillDrag];
  218.     return YES;
  219. }
  220.  
  221.  
  222. - (BOOL) performDragOperation:dragSource
  223. {
  224.     id        pasteboard;
  225.     char*    data;
  226.     int     length;
  227.  
  228.     pasteboard = [dragSource draggingPasteboard];
  229.     [pasteboard readType:NXFilenamePboardType data:&data length:&length];
  230.     if (strchr (data, '\t')) return NO;
  231.     [self setPath: data];
  232.     [self setIcon: [dragSource draggedImageCopy]];
  233.     [pasteboard deallocatePasteboardData:data length:length];
  234.     return YES;
  235. }
  236.  
  237.  
  238. - notifyDelegateDidDrop: aDragSource
  239. {
  240.     if ([delegate respondsTo: @selector (dragSource:didDropOnIconWell:)])
  241.         [delegate dragSource: aDragSource didDropOnIconWell: self];
  242.  
  243.     return self;
  244. }
  245.  
  246.  
  247. - concludeDragOperation:dragSource
  248. {
  249.     [self notifyDelegateDidDrop: dragSource];
  250.     [self update];
  251.     return self;
  252. }
  253.  
  254.  
  255. @end
  256.