home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscTableScroll / MiscTableScrollPB.M < prev    next >
Encoding:
Text File  |  1996-02-11  |  12.3 KB  |  396 lines

  1. //=============================================================================
  2. //
  3. //        Copyright (C) 1995, 1996 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.3 96/01/13 23:44:36 zarnuk Exp $
  23. // $Log:        MiscTableScrollPB.M,v $
  24. //    Revision 1.3  96/01/13    23:44:36  zarnuk
  25. //    Plugged memory leak.
  26. //    All allocations are now done from [self zone].
  27. //    
  28. //    Revision 1.2  95/10/08    16:12:47  zarnuk
  29. //    Cell* --> id.
  30. //    
  31. //    Revision 1.1  95/09/27    12:21:21  zarnuk
  32. //    Initial revision
  33. //    
  34. //-----------------------------------------------------------------------------
  35. #import <misckit/MiscTableScroll.h>
  36.  
  37. extern "Objective-C" {
  38. #import <appkit/Application.h>
  39. #import <appkit/Cell.h>
  40. #import <appkit/Pasteboard.h>
  41. }
  42.  
  43. #define MISC_PB_FIELD_SEPARATOR            '\t'
  44. #define MISC_PB_RECORD_TERMINATOR        '\n'
  45. #define MISC_PB_MAX_TYPES                32
  46.  
  47. @implementation MiscTableScroll(Pasteboard)
  48.  
  49. //-----------------------------------------------------------------------------
  50. // - sortSel:border:
  51. //-----------------------------------------------------------------------------
  52. - (void) sortSel:(MiscIntList*)sel_list border:(MiscBorderType)b
  53.     {
  54.     [self border:b physicalToVisual:sel_list];
  55.     [sel_list sort];
  56.     [self border:b visualToPhysical:sel_list];
  57.     }
  58.  
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // - builtinRegisterServicesTypes
  63. //
  64. // FIXME: Deal with these also:
  65. //        writeTypes[*] = NXRTFPboardType
  66. //        writeTypes[*] = NXFontPboardType;
  67. //        writeTypes[*] = NXColorPboardType
  68. //        writeTypes[*] = NXSelectionPboardType
  69. //        writeTypes[*] = NXDataLinkPboardType
  70. //-----------------------------------------------------------------------------
  71. - (void) builtinRegisterServicesTypes
  72.     {
  73.     NXAtom writeTypes[3];
  74.     NXAtom readTypes[1];
  75.  
  76.     writeTypes[0] = NXTabularTextPboardType;
  77.     writeTypes[1] = NXAsciiPboardType;
  78.     writeTypes[2] = 0;
  79.  
  80.     readTypes[0] = 0;
  81.  
  82.     [NXApp registerServicesMenuSendTypes:writeTypes andReturnTypes:readTypes];
  83.     }
  84.  
  85.  
  86.  
  87. //-----------------------------------------------------------------------------
  88. // - registerServicesTypes
  89. //-----------------------------------------------------------------------------
  90. - (void) registerServicesTypes
  91.     {
  92.     if (delegate != 0 &&
  93.         [delegate respondsTo:@selector(tableScrollRegisterServicesTypes:)])
  94.         [delegate tableScrollRegisterServicesTypes:self];
  95.  
  96.     else if (dataDelegate != 0 &&
  97.         [dataDelegate respondsTo:@selector(tableScrollRegisterServicesTypes:)])
  98.         [dataDelegate tableScrollRegisterServicesTypes:self];
  99.  
  100.     else
  101.         [self builtinRegisterServicesTypes];
  102.     }
  103.  
  104.  
  105.  
  106. //-----------------------------------------------------------------------------
  107. // - builtinValidRequestorForSendType:andReturnType:
  108. //-----------------------------------------------------------------------------
  109. - builtinValidRequestorForSendType:(NXAtom)t_write
  110.         andReturnType:(NXAtom)t_read
  111.     {
  112.     if (t_read == 0 &&            // We only send stuff, we never take stuff.
  113.         (t_write == NXTabularTextPboardType || t_write == NXAsciiPboardType) &&
  114.         ([self hasRowSelection] || [self hasColSelection]))
  115.         return self;
  116.  
  117.     return [super validRequestorForSendType:t_write andReturnType:t_read];
  118.     }
  119.  
  120.  
  121. //-----------------------------------------------------------------------------
  122. // - validRequestorForSendType:andReturnType:
  123. //-----------------------------------------------------------------------------
  124. - validRequestorForSendType:(NXAtom)t_write andReturnType:(NXAtom)t_read
  125.     {
  126.     if (delegate != 0 && [delegate respondsTo:
  127.         @selector(tableScroll:validRequestorForSendType:andReturnType:)])
  128.         return [delegate tableScroll:self
  129.                 validRequestorForSendType:t_write andReturnType:t_read];
  130.  
  131.     if (dataDelegate != 0 && [dataDelegate respondsTo:
  132.         @selector(tableScroll:validRequestorForSendType:andReturnType:)])
  133.         return [dataDelegate tableScroll:self
  134.                 validRequestorForSendType:t_write andReturnType:t_read];
  135.  
  136.     return [self builtinValidRequestorForSendType:t_write
  137.                 andReturnType:t_read];
  138.     }
  139.  
  140.  
  141. //-----------------------------------------------------------------------------
  142. // - builtinReadSelectionFromPasteboard:
  143. //-----------------------------------------------------------------------------
  144. - builtinReadSelectionFromPasteboard:pboard
  145.     {
  146.     return self;
  147.     }
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // - readSelectionFromPasteboard:
  152. //-----------------------------------------------------------------------------
  153. - readSelectionFromPasteboard:pb
  154.     {
  155.     if (delegate != 0 && [delegate respondsTo:
  156.         @selector(tableScroll:readSelectionFromPasteboard:)])
  157.         return [delegate tableScroll:self readSelectionFromPasteboard:pb];
  158.  
  159.     if (dataDelegate != 0 && [dataDelegate respondsTo:
  160.         @selector(tableScroll:readSelectionFromPasteboard:)])
  161.         return [dataDelegate tableScroll:self readSelectionFromPasteboard:pb];
  162.  
  163.     return [self builtinReadSelectionFromPasteboard:pb];
  164.     }
  165.  
  166.  
  167. //-----------------------------------------------------------------------------
  168. // - builtinCanWritePboardType:
  169. //-----------------------------------------------------------------------------
  170. - (BOOL) builtinCanWritePboardType:(NXAtom)type
  171.     {
  172.     return type == NXAsciiPboardType || type == NXTabularTextPboardType;
  173.     }
  174.  
  175.  
  176. //-----------------------------------------------------------------------------
  177. // - canWritePboardType:
  178. //-----------------------------------------------------------------------------
  179. - (BOOL) canWritePboardType:(NXAtom)type
  180.     {
  181.     if (delegate != 0 &&
  182.         [delegate respondsTo:@selector(tableScroll:canWritePboardType:)])
  183.         return [delegate tableScroll:self canWritePboardType:type];
  184.  
  185.     if (dataDelegate != 0 &&
  186.         [dataDelegate respondsTo:@selector(tableScroll:canWritePboardType:)])
  187.         return [dataDelegate tableScroll:self canWritePboardType:type];
  188.  
  189.     return [self builtinCanWritePboardType:type];
  190.     }
  191.  
  192.  
  193. //-----------------------------------------------------------------------------
  194. // - writeNXAsciiPboardTypeToStream:at::
  195. //-----------------------------------------------------------------------------
  196. - (void) writeNXAsciiPboardTypeToStream:(NXStream*)stream
  197.         at:(int)row :(int)col
  198.     {
  199.     char const* s = 0;
  200.     id cell = [self cellAt:row:col];
  201.  
  202.     if (cell != 0)
  203.         {
  204.         if ([cell respondsTo:@selector(title)])
  205.             s = [cell title];
  206.         else if ([cell respondsTo:@selector(stringValue)])
  207.             s = [cell stringValue];
  208.         }
  209.  
  210.     if (s != 0)
  211.         {
  212.         for ( ;     *s != '\0';  s++)
  213.             {
  214.             int c = *s;
  215.             if (c == MISC_PB_FIELD_SEPARATOR)
  216.                 c = ' ';
  217.             NXPutc( stream, c );
  218.             }
  219.         }
  220.     }
  221.  
  222.  
  223. //-----------------------------------------------------------------------------
  224. // - writeNXAsciiPboardTypeToStream:
  225. //-----------------------------------------------------------------------------
  226. - (void) writeNXAsciiPboardTypeToStream: (NXStream*) stream
  227.     {
  228.     unsigned int i, i_lim;
  229.     MiscCoord_V j, j_lim;
  230.     MiscCoord_P row, col;
  231.     MiscIntList* sel_list = [[MiscIntList allocFromZone:[self zone]] init];
  232.  
  233.     if ([self numSelectedRows] > 0)
  234.         {
  235.         [self selectedRows:sel_list];
  236.         [self sortSel:sel_list border:MISC_ROW_BORDER];
  237.         i_lim = [sel_list count];
  238.         j_lim = (MiscCoord_V) [self numCols];
  239.         for (i = 0;     i < i_lim;     i++)
  240.             {
  241.             row = (MiscCoord_P) [sel_list intAt:i];
  242.             for (j = 0;     j < j_lim;     j++)
  243.                 {
  244.                 if (j > 0) NXPutc( stream, MISC_PB_FIELD_SEPARATOR );
  245.                 col = [self colAtPosition:j];
  246.                 [self writeNXAsciiPboardTypeToStream:stream at:row:col];
  247.                 }
  248.             NXPutc( stream, MISC_PB_RECORD_TERMINATOR );
  249.             }
  250.         }
  251.     else if ([self numSelectedCols] > 0)
  252.         {
  253.         [self selectedCols:sel_list];
  254.         [self sortSel:sel_list border:MISC_COL_BORDER];
  255.         i_lim = [sel_list count];
  256.         j_lim = (MiscCoord_V) [self numRows];
  257.         for (j = 0;     j < j_lim;     j++)
  258.             {
  259.             row = [self rowAtPosition:j];
  260.             for (i = 0;     i < i_lim;     i++)
  261.                 {
  262.                 if (i > 0) NXPutc( stream, MISC_PB_FIELD_SEPARATOR );
  263.                 col = (MiscCoord_P) [sel_list intAt:i];
  264.                 [self writeNXAsciiPboardTypeToStream:stream at:row:col];
  265.                 }
  266.             NXPutc( stream, MISC_PB_RECORD_TERMINATOR );
  267.             }
  268.         }
  269.  
  270.     [sel_list free];
  271.     }
  272.  
  273.  
  274. //-----------------------------------------------------------------------------
  275. // - writeNXTabularTextPboardTypeToStream:
  276. //-----------------------------------------------------------------------------
  277. - (void) writeNXTabularTextPboardTypeToStream: (NXStream*) stream
  278.     {
  279.     [self writeNXAsciiPboardTypeToStream:stream];
  280.     }
  281.  
  282.  
  283. //-----------------------------------------------------------------------------
  284. // - builtinWritePboard:type:toStream:
  285. //-----------------------------------------------------------------------------
  286. - (void) builtinWritePboard:pb type:(NXAtom)type toStream:(NXStream*)stream
  287.     {
  288.     if (type == NXAsciiPboardType)
  289.         [self writeNXAsciiPboardTypeToStream:stream];
  290.     else if (type == NXTabularTextPboardType)
  291.         [self writeNXTabularTextPboardTypeToStream:stream];
  292.     }
  293.  
  294.  
  295. //-----------------------------------------------------------------------------
  296. // - writePboard:type:toStream:
  297. //-----------------------------------------------------------------------------
  298. - (void) writePboard:pb type:(NXAtom)t toStream:(NXStream*)s
  299.     {
  300.     if (delegate != 0 && [delegate respondsTo:
  301.         @selector(tableScroll:writePboard:type:toStream:)])
  302.         [delegate tableScroll:self writePboard:pb type:t toStream:s];
  303.  
  304.     else if (dataDelegate != 0 && [dataDelegate respondsTo:
  305.         @selector(tableScroll:writePboard:type:toStream:)])
  306.         [dataDelegate tableScroll:self writePboard:pb type:t toStream:s];
  307.     else
  308.         [self builtinWritePboard:pb type:t toStream:s];
  309.     }
  310.  
  311.  
  312. //-----------------------------------------------------------------------------
  313. // - builtinWriteSelectionToPasteboard:types:
  314. //-----------------------------------------------------------------------------
  315. - (BOOL) builtinWriteSelectionToPasteboard:pboard
  316.         types:(NXAtom*)original_types
  317.     {
  318.     BOOL result = NO;
  319.     NXAtom types[ MISC_PB_MAX_TYPES + 1 ];
  320.     int nTypes = 0;
  321.     NXAtom const* tp;
  322.  
  323.     if (original_types != 0)
  324.         for (tp = original_types;  *tp != 0;  tp++)
  325.             if ([self canWritePboardType:*tp])
  326.                 if (nTypes < MISC_PB_MAX_TYPES)
  327.                     types[ nTypes++ ] = *tp;
  328.  
  329.     types[ nTypes ] = 0;
  330.  
  331.     if (nTypes > 0 && ([self hasRowSelection] || [self hasColSelection]))
  332.         {
  333.         int i = 0;
  334.  
  335.         [pboard declareTypes:types num:nTypes owner:0];
  336.  
  337.         for (i = 0;     i < nTypes;  i++)
  338.             {
  339.             NXStream* stream = NXOpenMemory( 0, 0, NX_READWRITE );
  340.             [self writePboard:pboard type:types[i] toStream:stream];
  341.             NXSeek( stream, 0, NX_FROMSTART );
  342.             [pboard writeType:types[i] fromStream:stream];
  343.             NXCloseMemory( stream, NX_FREEBUFFER );
  344.             }
  345.  
  346.         result = YES;
  347.         }
  348.  
  349.     return result;
  350.     }
  351.  
  352.  
  353. //-----------------------------------------------------------------------------
  354. // - writeSelectionToPasteboard:types:
  355. //-----------------------------------------------------------------------------
  356. - (BOOL) writeSelectionToPasteboard:pboard types:(NXAtom*)types
  357.     {
  358.     if (delegate != 0 && [delegate respondsTo:
  359.         @selector(tableScroll:writeSelectionToPasteboard:types:)])
  360.         return [delegate tableScroll:self
  361.                 writeSelectionToPasteboard:pboard types:types];
  362.  
  363.     if (dataDelegate != 0 && [dataDelegate respondsTo:
  364.         @selector(tableScroll:writeSelectionToPasteboard:types:)])
  365.         return [dataDelegate tableScroll:self
  366.                 writeSelectionToPasteboard:pboard types:types];
  367.  
  368.     return [self builtinWriteSelectionToPasteboard:pboard types:types];
  369.     }
  370.  
  371.  
  372. //-----------------------------------------------------------------------------
  373. // - copy:
  374. //-----------------------------------------------------------------------------
  375. - copy:sender
  376.     {
  377.     NXAtom types[3];
  378.     types[0] = NXTabularTextPboardType;
  379.     types[1] = NXAsciiPboardType;
  380.     types[2] = 0;
  381.     [self writeSelectionToPasteboard:[Pasteboard new] types:types];
  382.     return self;
  383.     }
  384.  
  385.  
  386.  
  387. //-----------------------------------------------------------------------------
  388. // - cut:
  389. //-----------------------------------------------------------------------------
  390. - cut:sender
  391.     {
  392.     return [self copy:sender];
  393.     }
  394.  
  395. @end
  396.