pdcclip.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* PDCurses */
  2. #include "pdcwin.h"
  3. #include <string.h>
  4. /*man-start**************************************************************
  5. clipboard
  6. ---------
  7. ### Synopsis
  8. int PDC_getclipboard(char **contents, long *length);
  9. int PDC_setclipboard(const char *contents, long length);
  10. int PDC_freeclipboard(char *contents);
  11. int PDC_clearclipboard(void);
  12. ### Description
  13. PDC_getclipboard() gets the textual contents of the system's
  14. clipboard. This function returns the contents of the clipboard in the
  15. contents argument. It is the responsibility of the caller to free the
  16. memory returned, via PDC_freeclipboard(). The length of the clipboard
  17. contents is returned in the length argument.
  18. PDC_setclipboard copies the supplied text into the system's
  19. clipboard, emptying the clipboard prior to the copy.
  20. PDC_clearclipboard() clears the internal clipboard.
  21. ### Return Values
  22. indicator of success/failure of call.
  23. PDC_CLIP_SUCCESS the call was successful
  24. PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
  25. the clipboard contents
  26. PDC_CLIP_EMPTY the clipboard contains no text
  27. PDC_CLIP_ACCESS_ERROR no clipboard support
  28. ### Portability
  29. X/Open ncurses NetBSD
  30. PDC_getclipboard - - -
  31. PDC_setclipboard - - -
  32. PDC_freeclipboard - - -
  33. PDC_clearclipboard - - -
  34. **man-end****************************************************************/
  35. #ifdef PDC_WIDE
  36. # define PDC_TEXT CF_UNICODETEXT
  37. #else
  38. # define PDC_TEXT CF_OEMTEXT
  39. #endif
  40. int PDC_getclipboard(char **contents, long *length)
  41. {
  42. HANDLE handle;
  43. long len;
  44. PDC_LOG(("PDC_getclipboard() - called\n"));
  45. if (!OpenClipboard(NULL))
  46. return PDC_CLIP_ACCESS_ERROR;
  47. if ((handle = GetClipboardData(PDC_TEXT)) == NULL)
  48. {
  49. CloseClipboard();
  50. return PDC_CLIP_EMPTY;
  51. }
  52. #ifdef PDC_WIDE
  53. len = wcslen((wchar_t *)handle) * 3;
  54. #else
  55. len = strlen((char *)handle);
  56. #endif
  57. *contents = (char *)GlobalAlloc(GMEM_FIXED, len + 1);
  58. if (!*contents)
  59. {
  60. CloseClipboard();
  61. return PDC_CLIP_MEMORY_ERROR;
  62. }
  63. #ifdef PDC_WIDE
  64. len = PDC_wcstombs((char *)*contents, (wchar_t *)handle, len);
  65. #else
  66. strcpy((char *)*contents, (char *)handle);
  67. #endif
  68. *length = len;
  69. CloseClipboard();
  70. return PDC_CLIP_SUCCESS;
  71. }
  72. int PDC_setclipboard(const char *contents, long length)
  73. {
  74. HGLOBAL ptr1;
  75. LPTSTR ptr2;
  76. PDC_LOG(("PDC_setclipboard() - called\n"));
  77. if (!OpenClipboard(NULL))
  78. return PDC_CLIP_ACCESS_ERROR;
  79. ptr1 = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
  80. (length + 1) * sizeof(TCHAR));
  81. if (!ptr1)
  82. return PDC_CLIP_MEMORY_ERROR;
  83. ptr2 = GlobalLock(ptr1);
  84. #ifdef PDC_WIDE
  85. PDC_mbstowcs((wchar_t *)ptr2, contents, length);
  86. #else
  87. memcpy((char *)ptr2, contents, length + 1);
  88. #endif
  89. GlobalUnlock(ptr1);
  90. EmptyClipboard();
  91. if (!SetClipboardData(PDC_TEXT, ptr1))
  92. {
  93. GlobalFree(ptr1);
  94. return PDC_CLIP_ACCESS_ERROR;
  95. }
  96. CloseClipboard();
  97. GlobalFree(ptr1);
  98. return PDC_CLIP_SUCCESS;
  99. }
  100. int PDC_freeclipboard(char *contents)
  101. {
  102. PDC_LOG(("PDC_freeclipboard() - called\n"));
  103. GlobalFree(contents);
  104. return PDC_CLIP_SUCCESS;
  105. }
  106. int PDC_clearclipboard(void)
  107. {
  108. PDC_LOG(("PDC_clearclipboard() - called\n"));
  109. EmptyClipboard();
  110. return PDC_CLIP_SUCCESS;
  111. }