home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Languages / python / PyObjC-0.47-MIHS / pyobjc-0.47-src / Modules / OC_Pasteboard.m < prev    next >
Encoding:
Text File  |  1996-11-14  |  4.2 KB  |  173 lines

  1. /* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
  2.  *
  3.  * This software may be used and distributed freely for any purpose
  4.  * provided that this notice is included unchanged on any and all
  5.  * copies. The author does not warrant or guarantee this software in
  6.  * any way.
  7.  *
  8.  * This file is part of the PyObjC package.
  9.  *
  10.  * $RCSfile: OC_Pasteboard.m,v $
  11.  * $Revision: 1.4 $
  12.  * $Date: 1996/11/15 02:33:41 $
  13.  *
  14.  * Created Thu Oct 17 17:07:39 1996.
  15.  *
  16.  * Credits:
  17.  * Bill Bumgarner <bbum@friday.com> wrote a Python extension module with
  18.  * a similar goal. Thanx for his effort, that made easy implement this
  19.  * directly in Objective-C for efficiency, simplicity and lbnl portability.
  20.  */
  21.  
  22. #include "ObjC.h"
  23. #include "OC_Pasteboard.h"
  24. #include "OC_PythonString.h"
  25.  
  26. void oc_pasteboard_ensure_link() {}
  27.  
  28. #ifndef WITH_FOUNDATION
  29.  
  30. /* Thanx to Bill Bumgarner for these */
  31.  
  32. /* We must follow this way, since the names and types associated to
  33.    Pasteboards on NeXTSTEP <= 3.3, altough NXAtoms, are *NOT* in the
  34.    UniqueStrings database */
  35.  
  36. #define RET_IF_EQUAL(x, o) if ((!strcmp (x,#o)) || (!strcmp (x,o))) return o
  37.  
  38. static inline NXAtom
  39. get_real_unique_pb_name (const char *name)
  40. {
  41.   if (name[0] == 'N' && name[1] == 'X')
  42.     {
  43.       RET_IF_EQUAL (name, NXGeneralPboard);
  44.       RET_IF_EQUAL (name, NXFontPboard);
  45.       RET_IF_EQUAL (name, NXRulerPboard);
  46.       RET_IF_EQUAL (name, NXFindPboard);
  47.       RET_IF_EQUAL (name, NXDragPboard);
  48.     }
  49.   return NXUniqueString (name);
  50. }
  51.  
  52. static inline NXAtom
  53. get_real_unique_pb_type (const char *type)
  54. {
  55.   if (type[0] == 'N' && type[1] == 'X')
  56.     {
  57.       RET_IF_EQUAL (type, NXAsciiPboardType);
  58.       RET_IF_EQUAL (type, NXPostScriptPboardType);
  59.       RET_IF_EQUAL (type, NXTIFFPboardType);
  60.       RET_IF_EQUAL (type, NXRTFPboardType);
  61.       RET_IF_EQUAL (type, NXFilenamePboardType);
  62.       RET_IF_EQUAL (type, NXTabularTextPboardType);
  63.       RET_IF_EQUAL (type, NXFontPboardType);
  64.       RET_IF_EQUAL (type, NXRulerPboardType);
  65.       RET_IF_EQUAL (type, NXFileContentsPboardType);
  66.       RET_IF_EQUAL (type, NXColorPboardType);
  67.     }
  68.   return NXUniqueString (type);
  69. }
  70.  
  71. #endif
  72.  
  73. @implementation OC_PASTEBOARD_SUPER_CLASS (OC_Pasteboard)
  74.  
  75. #ifndef WITH_FOUNDATION
  76.  
  77. + (OC_PASTEBOARD_SUPER_CLASS *) pasteboardWithName:(OC_PASTEBOARD_STRING *) name
  78. {
  79.   NXAtom uniquename = get_real_unique_pb_name (name);
  80.   
  81.   return [self newName:uniquename];
  82. }
  83.  
  84. - (OC_Stream *) dataForType:(OC_PASTEBOARD_STRING *) type
  85. {
  86.   NXAtom uniquetype = get_real_unique_pb_type (type);
  87.   NXStream *stream;
  88.  
  89.   NX_DURING
  90.     stream = [self readTypeToStream:uniquetype];
  91.   NX_HANDLER
  92.     PyErr_SetString (ObjC_Error, "Pasteboard Read Error"); // XXX
  93.     return nil;
  94.   NX_ENDHANDLER
  95.  
  96.   if (stream)
  97.     {
  98.       id data = [OC_Stream newFromStream:stream];
  99.   
  100.       [data freeWhenDone:YES];
  101.       return data;
  102.     }
  103.   else
  104.     {
  105.       PyErr_SetString (ObjCStreams_Error, "Pasteboard Stream Error"); // XXX
  106.       return nil;
  107.     }  
  108. }
  109.  
  110. - (BOOL) setData:(OC_PythonObject *) data
  111.      forType:(OC_PASTEBOARD_STRING *) dataType
  112. {
  113.     NXAtom uniquetype = get_real_unique_pb_type (dataType);
  114.     BOOL result;
  115.   
  116.     [self declareTypes:&uniquetype num:1 owner:nil];
  117.     NX_DURING
  118.     if ([data isKindOf: [OC_Stream class]])
  119.     result = ([self writeType:uniquetype
  120.                fromStream:[(OC_Stream *) data  stream]] != nil);
  121.     else if ([data isKindOf: [OC_PythonObject class]] &&
  122.          PyString_Check([data object]))
  123.     result = ([self writeType:uniquetype
  124.             data: PyString_AsString([data object])
  125.             length: PyString_Size([data object])] != nil);
  126.     NX_HANDLER
  127.     PyErr_SetString (ObjC_Error, "Pasteboard Write Error"); // XXX
  128.     return NO;
  129.     NX_ENDHANDLER
  130.  
  131.     return result;
  132. }
  133.  
  134. - (List *) listOfTypes
  135. {
  136.   const NXAtom *t;
  137.   register const NXAtom *ti;
  138.   register unsigned int i;
  139.   List *list;
  140.   
  141.   t = [self types];        // XXX should we free this array?
  142.   for (i=0, ti=t; *ti; ti++, i++)
  143.     /* do nothing */;
  144.  
  145.   list = [[List alloc] initCount:i];
  146.  
  147.   if (i)
  148.     {
  149.       list->numElements = i;
  150.       while (i--)
  151.     [list replaceObjectAt:i
  152.           with:[OC_PythonString fromString:(char *) *(--ti)]];
  153.     }
  154.   
  155.   return list;
  156. }
  157.  
  158. #else
  159.  
  160. - (NSArray *) listOfTypes
  161. {
  162.   return [self types];
  163. }
  164.  
  165. #endif
  166. @end
  167.  
  168. /*
  169. ** Local Variables:
  170. ** change-log-default-name:"../ChangeLog.PyObjC"
  171. ** End:
  172. */
  173.