home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / gvServices.m < prev    next >
Text File  |  1995-11-28  |  3KB  |  93 lines

  1. #import "draw.h"
  2.  
  3. @implementation GraphicView(Services)
  4.  
  5. /* Services menu methods */
  6.  
  7. /*
  8.  * Services in Draw are trivial to implement since we leverage heavily
  9.  * off of the copy/paste code.  Note that write/readSelectionTo/FromPasteboard:
  10.  * do little more than call the copy/paste code.
  11.  */
  12.  
  13. /*
  14.  * We are a valid requestor whenever any of the send or return types is
  15.  * PostScript, TIFF, or Draw (actually, any return type that NSImage can
  16.  * handle is okay with us).
  17.  */
  18.  
  19. - (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType
  20. {
  21.  
  22.     if ((!sendType || [sendType isEqual:@""] ||
  23.     (([sendType isEqual:NSPostScriptPboardType] ||
  24.       [sendType isEqual:NSTIFFPboardType] ||
  25.       [sendType isEqual:DrawPboardType]) && [slist count])) &&
  26.     (!returnType || [returnType isEqual:@""] ||
  27.       IncludesType([NSImage imagePasteboardTypes], returnType) ||
  28.       [returnType isEqual:DrawPboardType])) {
  29.     return self;
  30.     }
  31.  
  32.     return [super validRequestorForSendType:sendType returnType:returnType];
  33. }
  34.  
  35. /*
  36.  * If one of the requested types is one of the ones we handle,
  37.  * then we put our selection in the Pasteboard.  The serviceActsOnSelection
  38.  * flag is so that we can effectively undo a Service request.
  39.  */
  40.  
  41. - (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types
  42. {
  43.     int typeCount = [types count];
  44.     int index = 0;
  45.     
  46.     while (index < typeCount) {
  47.         NSString *currType = [types objectAtIndex:index];
  48.     
  49.     if ([currType isEqual:NSPostScriptPboardType] || [currType isEqual:NSTIFFPboardType] || [currType isEqual:DrawPboardType]) break;
  50.     index++;
  51.     }
  52.  
  53.     if ((index < typeCount) && [self copyToPasteboard:pboard types:types]) {
  54.     gvFlags.serviceActsOnSelection = YES;
  55.     return YES;
  56.     } else {
  57.     return NO;
  58.     }
  59. }
  60.  
  61. /*
  62.  * When a result comes back from the Services menu request,
  63.  * we replace the selection with the return value.
  64.  * If the user really wants the return value in addition to
  65.  * the current selection, she can simply copy, then paste
  66.  * twice to get two copies, then choose the Services menu item.
  67.  */
  68.  
  69. - readSelectionFromPasteboard:(NSPasteboard *)pboard
  70. {
  71.     id change;
  72.     NSRect sbbox;
  73.     NSPoint *position = &sbbox.origin;
  74.     
  75.     change = [[MultipleChange alloc] initChangeName:SERVICE_CALL_OP];
  76.     [change startChange];
  77.     if (gvFlags.serviceActsOnSelection) {
  78.         sbbox = [self getBBoxOfArray:slist extended:NO];
  79.         sbbox.origin.x += floor(sbbox.size.width / 2.0 + 0.5);
  80.         sbbox.origin.y += floor(sbbox.size.height / 2.0 + 0.5);
  81.         [self delete:self];
  82.         gvFlags.serviceActsOnSelection = NO;
  83.     } else {
  84.         position = NULL;
  85.     }
  86.     [self pasteFromPasteboard:pboard andLink:DontLink at:position];
  87.     [change endChange];
  88.  
  89.     return self;
  90. }
  91.  
  92. @end
  93.