home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / WINDOWS / WINCLIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-29  |  6.0 KB  |  260 lines

  1. /* 
  2. WINCLIP.C -- Windows Clipboard access -- implementation 
  3. 386|DOS Extender functions for Windows 3.x (Enhanced) 
  4. Clipboard 
  5. */ 
  6.  
  7. #include <stdlib.h> 
  8. #include <stdio.h> 
  9. #include <malloc.h> 
  10. #include <string.h> 
  11. #include <dos.h> 
  12. #include <pharlap.h>
  13. #include "winclip.h" 
  14.  
  15. static int clip_open=0; 
  16. static int do_convert=1; 
  17.  
  18. /**************************************************************/ 
  19.  
  20. int PutClipStrLen(char *s, unsigned len) 
  21.     int maj, min; 
  22.     int ret = 0; 
  23.     char *buf, *s2; 
  24.     unsigned len2; 
  25.      
  26.     if (! WinOldApVersion(&maj, &min)) 
  27.         return  1;  // Windows Enhanced mode isn't running 
  28.  
  29.     // change all 0d 0a to 0a 
  30.     if (do_convert) 
  31.     { 
  32.         if ((buf = calloc(len, 1)) == NULL) 
  33.             return 0;   // insufficient memory 
  34.         for (s2=buf, len2=len; len2--; s2++, s++) 
  35.         { 
  36.             if ((s[0] == 0x0d) && (s[1] == 0x0a)) 
  37.             { 
  38.                 s++; 
  39.                 len--;      // remove 0d 
  40.             } 
  41.             *s2 = *s; 
  42.         } 
  43.     } 
  44.     else 
  45.         buf=s; 
  46.          
  47.     if (! OpenClipboard()) 
  48.         return 0; 
  49.     clip_open++; 
  50.     /* note: if calling program dies between OpenClipboard and 
  51.        CloseClipboard, then no other program can access it until 
  52.        Windows has been restarted. Perhaps put CloseClipboard() 
  53.        call into ctrl-c handler */ 
  54.     EmptyClipboard(); 
  55.     if (CompactClipboard(len) < len) 
  56.         puts("couldn't compact clipboard"); 
  57.     else 
  58.         ret = SetClipboardData(buf, CF_TEXT, len); 
  59.     CloseClipboard(); 
  60.     clip_open--; 
  61.     free(buf); 
  62.     return ret? 1 : 0; 
  63.  
  64. int PutClipString(char *s) 
  65.     return PutClipStrLen(s, strlen(s)+1); 
  66.  
  67. char *GetClipString(void) 
  68.     char *s; 
  69.     REALPTR RealBufp;
  70.     int maj, min; 
  71.     unsigned long len; 
  72.      
  73.     if (! WinOldApVersion(&maj, &min)) 
  74.         return 0; 
  75.     if (! OpenClipboard()) 
  76.         return (char *)0; 
  77.     if ((len = GetClipboardSize(CF_TEXT)) == 0) 
  78.     { 
  79.         CloseClipboard(); 
  80.         return NULL; 
  81.     } 
  82.     if ((s = malloc(len+1)) == NULL) // add one for ASCIIZ 
  83.         return NULL; 
  84.     RealBufp = GetClipboardData(CF_TEXT); 
  85.     if (RealBufp == 0)
  86.     {
  87.         free(s);
  88.     return NULL;
  89.     }
  90.     ReadRealMem(s, RealBufp, len);
  91.     CloseClipboard();                   // important!! 
  92.     FreeClipboardData(RealBufp);        // in conv mem buffer 
  93.     s[len] = '\0';                      // make ASCIIZ 
  94.     return s; 
  95.  
  96. void FreeClipString(char *s) 
  97.     free(s); 
  98.  
  99. /**************************************************************/ 
  100.  
  101. /* INT 2Fh old application clipboard functions */ 
  102. #define MPLEX           0x2f 
  103. #define OPEN_CLIP       0x1701 
  104. #define EMPTY_CLIP      0x1702 
  105. #define SET_CLIP_DATA   0x1703 
  106. #define GET_CLIP_SIZE   0x1704 
  107. #define GET_CLIP_DATA   0x1705 
  108. #define CLOSE_CLIP      0x1708 
  109. #define CLIP_COMPACT    0x1709 
  110. #define GET_DEV_CAPS    0x170A 
  111.  
  112. #define MAKEULONG(a, b)  ((((ULONG) (a)) & 0xFFFF) | (((ULONG) (b)) << 16))
  113.  
  114. #define LO(x)           ((x) & 0xFF) 
  115. #define HI(x)           ((x) >> 8) 
  116. #define CLEAR(x)        memset(&x, 0, sizeof(x)) 
  117.  
  118. int WinOldApVersion(int *maj, int *min) 
  119.     CONFIG_INF config;
  120.     UCHAR buf[256];
  121.  
  122.     _dx_config_inf(&config, buf);
  123.     if (!config.c_windowsf || !config.c_winenhf)
  124.         return FALSE;
  125.     else
  126.     {
  127.         *maj = config.c_winmaj;
  128.     *min = config.c_winmin;
  129.     return TRUE;
  130.     }
  131.  
  132. int OpenClipboard(void) 
  133.     SWI_REGS r;
  134.     CLEAR(r); 
  135.     r.eax = OPEN_CLIP;
  136.     _dx_real_int(MPLEX, &r);
  137.     return r.eax;
  138.  
  139. void EmptyClipboard(void) 
  140.     SWI_REGS r; 
  141.     CLEAR(r); 
  142.     r.eax = EMPTY_CLIP; 
  143.     _dx_real_int(MPLEX, &r);
  144.  
  145. int SetClipboardData(void *data, CF_FORMAT format, unsigned long size) 
  146.     SWI_REGS r; 
  147.     USHORT addr; 
  148.     USHORT largest;
  149.     REALPTR RealBufp;
  150.      
  151.     CLEAR(r); 
  152.  
  153.     // allocate conventional memory, keep real mode seg addr 
  154.     if (_dx_real_alloc(1 + (size >> 4), &addr, &largest) != 0)
  155.         return 0; 
  156.      
  157.     // poke data into conv. mem. buffer using real mode selector 
  158.     RP_SET(RealBufp, 0, addr);
  159.     WriteRealMem(RealBufp, data, size);
  160.      
  161.     // pass the real mode address to the SET_CLIP_DATA function 
  162.     r.es = addr; 
  163.     r.ebx = 0; 
  164.     r.eax = SET_CLIP_DATA; 
  165.     r.edx = format; 
  166.     r.esi = size >> 16; 
  167.     r.ecx = size & 0xFFFF; 
  168.  
  169.     _dx_real_int(MPLEX, &r);
  170.     _dx_real_free(addr);
  171.     return (r.eax != 0); 
  172.  
  173. unsigned long GetClipboardSize(CF_FORMAT format) 
  174.     SWI_REGS r; 
  175.     CLEAR(r); 
  176.     r.eax = GET_CLIP_SIZE; 
  177.     r.edx = format; 
  178.     _dx_real_int(MPLEX, &r);
  179.     return MAKEULONG(r.eax, r.edx); 
  180.  
  181. REALPTR GetClipboardData(CF_FORMAT format) 
  182.     SWI_REGS r; 
  183.     unsigned long size; 
  184.     USHORT addr; 
  185.     USHORT largest;
  186.     REALPTR RealBufp;
  187.      
  188.     CLEAR(r); 
  189.      
  190.     size = GetClipboardSize(format); 
  191.  
  192.     // allocate conv. memory, keep real mode segment address 
  193.     if (_dx_real_alloc(1 + (size >> 4), &addr, &largest) != 0)
  194.         return 0; 
  195.      
  196.     // pass the real mode address to the GET_CLIP_DATA function 
  197.     r.es = addr; 
  198.     r.ebx = 0; 
  199.     r.eax = GET_CLIP_DATA; 
  200.     r.edx = format; 
  201.     _dx_real_int(MPLEX, &r);
  202.  
  203.     // return real mode FAR ptr to conventional memory buffer 
  204.     RP_SET(RealBufp, 0, addr);
  205.     return RealBufp; 
  206.  
  207. void FreeClipboardData(REALPTR RealBufp)
  208.     _dx_real_free(RP_SEG(RealBufp));
  209.  
  210. void CloseClipboard(void) 
  211.     SWI_REGS r; 
  212.     CLEAR(r); 
  213.     r.eax = CLOSE_CLIP; 
  214.     _dx_real_int(MPLEX, &r);
  215.  
  216. unsigned long CompactClipboard(unsigned long desire) 
  217.     SWI_REGS r; 
  218.     CLEAR(r); 
  219.     r.eax = CLIP_COMPACT; 
  220.     r.esi = desire >> 16; 
  221.     r.ecx = desire & 0xFFFF; 
  222.     _dx_real_int(MPLEX, &r);
  223.     return MAKEULONG(r.eax, r.edx); 
  224.  
  225. unsigned GetDeviceCaps(unsigned cap) 
  226.     SWI_REGS r; 
  227.     CLEAR(r); 
  228.     r.eax = GET_DEV_CAPS; 
  229.     r.edx = cap; 
  230.     _dx_real_int(MPLEX, &r);
  231.     return r.eax; 
  232.