home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Kits / MiscTableScroll-138.1 / Palettes / MiscTableScroll / Framework / MiscTableScrollPB.M < prev    next >
Encoding:
Text File  |  1998-03-31  |  11.9 KB  |  375 lines

  1. //=============================================================================
  2. //
  3. //    Copyright (C) 1995-1997 by Paul S. McCarthy and Eric Sunshine.
  4. //        Written by Paul S. McCarthy and Eric Sunshine.
  5. //                All Rights Reserved.
  6. //
  7. //    This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the authors
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "License.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14. //=============================================================================
  15. //-----------------------------------------------------------------------------
  16. // MiscTableScrollPB.M
  17. //
  18. //    Pasteboard and services support for MiscTableScroll.
  19. //
  20. //-----------------------------------------------------------------------------
  21. //-----------------------------------------------------------------------------
  22. // $Id: MiscTableScrollPB.M,v 1.9 97/06/18 10:06:23 sunshine Exp $
  23. // $Log:    MiscTableScrollPB.M,v $
  24. //  Revision 1.9  97/06/18  10:06:23  sunshine
  25. //  v125.9: Worked around Objective-C++ compiler crash in OPENSTEP 4.2 for NT
  26. //  when sending message to 'super' from within a category.
  27. //  numSelected{Rows|Cols} --> numberOfSelected{Rows|Cols}
  28. //  
  29. //  Revision 1.8  97/04/15  09:08:59  sunshine
  30. //  v0.125.8: Added "MiscTableScroll/" prefix to #import to facilitate new
  31. //  framework organization.
  32. //  
  33. //  Revision 1.7  97/03/10  10:41:21  sunshine
  34. //  v113.1: For OpenStep conformance, many 'col' methods renamed to 'column'.
  35. //-----------------------------------------------------------------------------
  36. #import <MiscTableScroll/MiscTableScroll.h>
  37. #import "MiscTableScrollPrivate.h"
  38.  
  39. extern "Objective-C" {
  40. #import <AppKit/NSApplication.h>
  41. #import <AppKit/NSCell.h>
  42. #import <AppKit/NSPasteboard.h>
  43. }
  44.  
  45. #define    MISC_PB_FIELD_SEPARATOR        @"\t"
  46. #define    MISC_PB_RECORD_TERMINATOR    @"\n"
  47.  
  48. @implementation MiscTableScroll(Pasteboard)
  49.  
  50. //-----------------------------------------------------------------------------
  51. // - sortSel:border:
  52. //-----------------------------------------------------------------------------
  53. - (NSArray*)sortSel:(NSArray*)sel_list border:(MiscBorderType)b
  54.     {
  55.     return [self border:b visualToPhysical:
  56.         [[self border:b physicalToVisual:sel_list]
  57.         sortedArrayUsingSelector:@selector(compare:)]];
  58.     }
  59.  
  60.  
  61.  
  62. //-----------------------------------------------------------------------------
  63. // - builtinRegisterServicesTypes
  64. //
  65. // FIXME: Deal with these also:
  66. //    returnTypes[*] = NSRTFPboardType
  67. //    returnTypes[*] = NSFontPboardType
  68. //    returnTypes[*] = NSColorPboardType
  69. //-----------------------------------------------------------------------------
  70. - (void)builtinRegisterServicesTypes
  71.     {
  72.     NSArray* sendTypes = [NSArray arrayWithObjects:
  73.         NSTabularTextPboardType, NSStringPboardType, 0];
  74.     NSArray* returnTypes = [NSArray array];
  75.     [NSApp registerServicesMenuSendTypes:sendTypes returnTypes:returnTypes];
  76.     }
  77.  
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // - registerServicesTypes
  82. //-----------------------------------------------------------------------------
  83. - (void)registerServicesTypes
  84.     {
  85.     id del = [self responsibleDelegate:
  86.             MiscDelegateFlags::DEL_REGISTER_SERVICE_TYPES];
  87.     if (del != 0)
  88.     [del tableScrollRegisterServicesTypes:self];
  89.     else
  90.     [self builtinRegisterServicesTypes];
  91.     }
  92.  
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // - builtinValidRequestorForSendType:returnType:
  97. //-----------------------------------------------------------------------------
  98. - (id)builtinValidRequestorForSendType:(NSString*)t_send
  99.     returnType:(NSString*)t_return
  100.     {
  101.     if (t_return == 0 &&    // We only send stuff, we never take stuff.
  102.     ([t_send isEqualToString:NSTabularTextPboardType] ||
  103.      [t_send isEqualToString:NSStringPboardType]) &&
  104.     ([self hasRowSelection] || [self hasColumnSelection]))
  105.     return self;
  106.  
  107.     return [self superValidRequestorForSendType:t_send returnType:t_return];
  108.     }
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112. // - validRequestorForSendType:returnType:
  113. //-----------------------------------------------------------------------------
  114. - (id)validRequestorForSendType:(NSString*)t_send
  115.     returnType:(NSString*)t_return
  116.     {
  117.     id del = [self responsibleDelegate:MiscDelegateFlags::DEL_VALID_REQUESTOR];
  118.     if (del != 0)
  119.     return [del tableScroll:self
  120.         validRequestorForSendType:t_send returnType:t_return];
  121.  
  122.     return [self builtinValidRequestorForSendType:t_send returnType:t_return];
  123.     }
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // - builtinReadSelectionFromPasteboard:
  128. //-----------------------------------------------------------------------------
  129. - (BOOL)builtinReadSelectionFromPasteboard:(NSPasteboard*)pboard
  130.     {
  131.     return NO;
  132.     }
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136. // - readSelectionFromPasteboard:
  137. //-----------------------------------------------------------------------------
  138. - (BOOL)readSelectionFromPasteboard:(NSPasteboard*)pb
  139.     {
  140.     id del = [self responsibleDelegate:
  141.             MiscDelegateFlags::DEL_READ_SEL_FROM_PB];
  142.     if (del != 0)
  143.     return [del tableScroll:self readSelectionFromPasteboard:pb];
  144.  
  145.     return [self builtinReadSelectionFromPasteboard:pb];
  146.     }
  147.  
  148.  
  149. //-----------------------------------------------------------------------------
  150. // - builtinCanWritePboardType:
  151. //-----------------------------------------------------------------------------
  152. - (BOOL)builtinCanWritePboardType:(NSString*)type
  153.     {
  154.     return ([type isEqualToString:NSStringPboardType] ||
  155.         [type isEqualToString:NSTabularTextPboardType]);
  156.     }
  157.  
  158.  
  159. //-----------------------------------------------------------------------------
  160. // - canWritePboardType:
  161. //-----------------------------------------------------------------------------
  162. - (BOOL)canWritePboardType:(NSString*)type
  163.     {
  164.     id del = [self responsibleDelegate:
  165.             MiscDelegateFlags::DEL_CAN_WRITE_PB_TYPE];
  166.     if (del != 0)
  167.     return [del tableScroll:self canWritePboardType:type];
  168.  
  169.     return [self builtinCanWritePboardType:type];
  170.     }
  171.  
  172.  
  173. //-----------------------------------------------------------------------------
  174. // - stringForNSStringPboardTypeAtRow:column:
  175. //-----------------------------------------------------------------------------
  176. - (NSString*)stringForNSStringPboardTypeAtRow:(int)row column:(int)col
  177.     {
  178.     NSString* s = 0;
  179.     id cell = [self cellAtRow:row column:col];
  180.  
  181.     if (cell != 0)
  182.     {
  183.     if ([cell respondsToSelector:@selector(title)])
  184.         s = [cell title];
  185.     else if ([cell respondsToSelector:@selector(stringValue)])
  186.         s = [cell stringValue];
  187.     }
  188.  
  189.     if (s == 0)
  190.     s = @"";
  191.     else
  192.     {
  193.     NSRange r = { 0, 0 };
  194.     NSMutableString* ms = [[s mutableCopy] autorelease];
  195.     for (;;)
  196.         {
  197.         r = [ms rangeOfString:MISC_PB_FIELD_SEPARATOR options:0
  198.         range:(NSRange){ r.location, [s length] - r.location }];
  199.         if (r.length == 0)
  200.         break;
  201.         else
  202.         [ms replaceCharactersInRange:r withString:@" "];
  203.         }
  204.     s = ms;
  205.     }
  206.  
  207.     return s;
  208.     }
  209.  
  210.  
  211. //-----------------------------------------------------------------------------
  212. // - stringForNSStringPboardType
  213. //-----------------------------------------------------------------------------
  214. - (NSString*)stringForNSStringPboardType
  215.     {
  216.     NSMutableString* s =
  217.     [[[NSMutableString allocWithZone:[self zone]] init] autorelease];
  218.     unsigned int i, i_lim;
  219.     MiscCoord_V j, j_lim;
  220.     MiscCoord_P row, col;
  221.  
  222.     if ([self numberOfSelectedRows] > 0)
  223.     {
  224.     NSArray* sel_list =
  225.         [self sortSel:[self selectedRows] border:MISC_ROW_BORDER];
  226.     i_lim = [sel_list count];
  227.     j_lim = (MiscCoord_V) [self numberOfColumns];
  228.     for (i = 0;  i < i_lim;  i++)
  229.         {
  230.         row = (MiscCoord_P) [[sel_list objectAtIndex:i] intValue];
  231.         for (j = 0;  j < j_lim;  j++)
  232.         {
  233.         if (j > 0) [s appendString:MISC_PB_FIELD_SEPARATOR];
  234.         col = [self columnAtPosition:j];
  235.         [s appendString:
  236.             [self stringForNSStringPboardTypeAtRow:row column:col]];
  237.         }
  238.         [s appendString:MISC_PB_RECORD_TERMINATOR];
  239.         }
  240.     }
  241.     else if ([self numberOfSelectedColumns] > 0)
  242.     {
  243.     NSArray* sel_list =
  244.         [self sortSel:[self selectedColumns] border:MISC_COL_BORDER];
  245.     i_lim = [sel_list count];
  246.     j_lim = (MiscCoord_V) [self numberOfRows];
  247.     for (j = 0;  j < j_lim;  j++)
  248.         {
  249.         row = [self rowAtPosition:j];
  250.         for (i = 0;  i < i_lim;  i++)
  251.         {
  252.         if (i > 0) [s appendString:MISC_PB_FIELD_SEPARATOR];
  253.         col = (MiscCoord_P) [[sel_list objectAtIndex:i] intValue];
  254.         [s appendString:
  255.             [self stringForNSStringPboardTypeAtRow:row column:col]];
  256.         }
  257.         [s appendString:MISC_PB_RECORD_TERMINATOR];
  258.         }
  259.     }
  260.     return s;
  261.     }
  262.  
  263.  
  264. //-----------------------------------------------------------------------------
  265. // - stringForNSTabularTextPBoardType
  266. //-----------------------------------------------------------------------------
  267. - (NSString*)stringForNSTabularTextPBoardType
  268.     {
  269.     return [self stringForNSStringPboardType];
  270.     }
  271.  
  272.  
  273. //-----------------------------------------------------------------------------
  274. // - builtinStringForPboardType:
  275. //-----------------------------------------------------------------------------
  276. - (NSString*)builtinStringForPboardType:(NSString*)type
  277.     {
  278.     NSString* s = @"";
  279.     if ([type isEqualToString:NSStringPboardType])
  280.     s = [self stringForNSStringPboardType];
  281.     else if ([type isEqualToString:NSTabularTextPboardType])
  282.     s = [self stringForNSTabularTextPBoardType];
  283.     return s;
  284.     }
  285.  
  286.  
  287. //-----------------------------------------------------------------------------
  288. // - stringForPboardType:
  289. //-----------------------------------------------------------------------------
  290. - (NSString*)stringForPboardType:(NSString*)t
  291.     {
  292.     id del = [self responsibleDelegate:
  293.             MiscDelegateFlags::DEL_STRING_FOR_PB_TYPE];
  294.     if (del != 0)
  295.     return [del tableScroll:self stringForPboardType:t];
  296.     else
  297.     return [self builtinStringForPboardType:t];
  298.     }
  299.  
  300.  
  301. //-----------------------------------------------------------------------------
  302. // - builtinWriteSelectionToPasteboard:types:
  303. //-----------------------------------------------------------------------------
  304. - (BOOL)builtinWriteSelectionToPasteboard:(NSPasteboard*)pboard
  305.     types:(NSArray*)original_types
  306.     {
  307.     BOOL result = NO;
  308.     NSMutableArray* types = [NSMutableArray array];
  309.  
  310.     if (original_types != 0)
  311.     {
  312.     for (unsigned int i = 0, lim = [original_types count]; i < lim; i++)
  313.         {
  314.         id t = [original_types objectAtIndex:i];
  315.         if ([self canWritePboardType:t])
  316.         [types addObject:t];
  317.         }
  318.     }
  319.  
  320.     unsigned int const nTypes = [types count];
  321.     if (nTypes > 0 && ([self hasRowSelection] || [self hasColumnSelection]))
  322.     {
  323.     [pboard declareTypes:types owner:0];
  324.  
  325.     for (unsigned int i = 0;  i < nTypes;  i++)
  326.         {
  327.         NSString* s = [types objectAtIndex:i];
  328.         [pboard setString:[self stringForPboardType:s] forType:s];
  329.         }
  330.  
  331.     result = YES;
  332.     }
  333.  
  334.     return result;
  335.     }
  336.  
  337.  
  338. //-----------------------------------------------------------------------------
  339. // - writeSelectionToPasteboard:types:
  340. //-----------------------------------------------------------------------------
  341. - (BOOL)writeSelectionToPasteboard:(NSPasteboard*)pboard types:(NSArray*)types
  342.     {
  343.     id del = [self responsibleDelegate:
  344.             MiscDelegateFlags::DEL_WRITE_SEL_TO_PB_TYPES];
  345.     if (del != 0)
  346.     return [del tableScroll:self
  347.         writeSelectionToPasteboard:pboard types:types];
  348.  
  349.     return [self builtinWriteSelectionToPasteboard:pboard types:types];
  350.     }
  351.  
  352.  
  353. //-----------------------------------------------------------------------------
  354. // - copy:
  355. //-----------------------------------------------------------------------------
  356. - (void)copy:(id)sender
  357.     {
  358.     NSArray* types = [NSArray arrayWithObjects:
  359.             NSTabularTextPboardType, NSStringPboardType, 0];
  360.     [self writeSelectionToPasteboard:[NSPasteboard generalPasteboard]
  361.             types:types];
  362.     }
  363.  
  364.  
  365.  
  366. //-----------------------------------------------------------------------------
  367. // - cut:
  368. //-----------------------------------------------------------------------------
  369. - (void)cut:(id)sender
  370.     {
  371.     [self copy:sender];
  372.     }
  373.  
  374. @end
  375.