home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / mac / scrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-06  |  2.2 KB  |  142 lines  |  [TEXT/????]

  1. /* Macintosh Scrap (Clipboard) Interface */
  2.  
  3. #include "macwin.h"
  4. #ifdef MPW
  5. #include <Scrap.h>
  6. #include <Memory.h>
  7. #endif
  8. #ifdef THINK_C_PRE_5_0
  9. #include <ScrapMgr.h>
  10. #include <MemoryMgr.h>
  11. #endif
  12.  
  13. static Handle hclip;
  14. static long lenclip;
  15.  
  16. /* _wfreeclip is called from wgetevent and from wsetclip. */
  17.  
  18. void
  19. _wfreeclip()
  20. {
  21.     if (hclip != NULL) {
  22.         HUnlock(hclip);
  23.         DisposHandle(hclip);
  24.         hclip= NULL;
  25.     }
  26. }
  27.  
  28. char *
  29. wgetclip()
  30. {
  31.     long offset;
  32.     
  33.     /* NB if the use of hclip or lenclip changes,
  34.        also change wgetcutbuffer() below !!! */
  35.     
  36.     if (hclip == NULL)
  37.         hclip= NewHandle(1L);
  38.     else
  39.         HUnlock(hclip);
  40.     lenclip= GetScrap(hclip, 'TEXT', &offset);
  41.     if (lenclip < 0)
  42.         return NULL;
  43.     SetHandleSize(hclip, lenclip+1);
  44.     HLock(hclip);
  45.     (*hclip)[lenclip]= EOS;
  46. #ifndef CLEVERGLUE
  47.     {
  48.     /* Convert imported \r into \n */
  49.         char *p= *hclip;
  50.         while ((p= strchr(p, '\r')) != NULL)
  51.             *p++ = '\n';
  52.     }
  53. #endif
  54.     return *hclip;
  55. }
  56.  
  57. void
  58. wsetclip(p, len)
  59.     char *p;
  60.     int len;
  61. {
  62.     int err;
  63. #ifndef CLEVERGLUE
  64.     /* Convert \n into \r before exporting.  Must make a copy, shit! */
  65.     char *q= malloc(len+1);
  66.     if (q != NULL) {
  67.         strncpy(q, p, len);
  68.         q[len]= EOS;
  69.         p= q;
  70.         while ((p= strchr(p, '\n')) != NULL)
  71.             *p++ = '\r';
  72.         p= q;
  73.     }
  74.     /* If there's no memory, export with \n's left in... */
  75. #endif
  76.     _wfreeclip();
  77.     err= ZeroScrap();
  78.     if (err != 0) dprintf("wsetclip: ZeroScrap error %d", err);
  79.     err= PutScrap((long)len, 'TEXT', p);
  80.     if (err != 0) dprintf("wsetclip: PutScrap error %d", err);
  81. #ifndef CLEVERGLUE
  82.     if (q != NULL)
  83.         free(q);
  84. #endif
  85. }
  86.  
  87. /* For "compatibility" with X11 STDWIN: */
  88.  
  89. int
  90. wsetselection(win, sel, data, len)
  91.     WINDOW *win;
  92.     int sel;
  93.     char *data;
  94.     int len;
  95. {
  96.     return 0;
  97. }
  98.  
  99. char *
  100. wgetselection(sel, plen)
  101.     int sel;
  102.     int *plen;
  103. {
  104.     return NULL;
  105. }
  106.  
  107. void
  108. wresetselection(sel)
  109.     int sel;
  110. {
  111. }
  112.  
  113. void
  114. wsetcutbuffer(ibuffer, data, len)
  115.     int ibuffer;
  116.     char *data;
  117.     int len;
  118. {
  119.     if (ibuffer == 0)
  120.         wsetclip(data, len);
  121. }
  122.  
  123. char *
  124. wgetcutbuffer(ibuffer, plen)
  125.     int ibuffer;
  126.     int *plen;
  127. {
  128.     if (ibuffer != 0)
  129.         return NULL;
  130.     if (wgetclip() == NULL)
  131.         return NULL;
  132.     /* This knows about the implementation of wgetclip() */
  133.     *plen = lenclip; /* NB: long --> int, may truncate... */
  134.     return *hclip;
  135. }
  136.  
  137. void
  138. wrotatecutbuffers(n)
  139.     int n;
  140. {
  141. }
  142.