1 /* Public Domain Curses */
5 RCSID("$Id: pdcclip.c,v 1.30 2008/07/14 04:24:52 wmcbrine Exp $")
7 /*man-start**************************************************************
12 int PDC_getclipboard(char **contents, long *length);
13 int PDC_setclipboard(const char *contents, long length);
14 int PDC_freeclipboard(char *contents);
15 int PDC_clearclipboard(void);
18 PDC_getclipboard() gets the textual contents of the system's
19 clipboard. This function returns the contents of the clipboard
20 in the contents argument. It is the responsibilitiy of the
21 caller to free the memory returned, via PDC_freeclipboard().
22 The length of the clipboard contents is returned in the length
25 PDC_setclipboard copies the supplied text into the system's
26 clipboard, emptying the clipboard prior to the copy.
28 PDC_clearclipboard() clears the internal clipboard.
31 indicator of success/failure of call.
32 PDC_CLIP_SUCCESS the call was successful
33 PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
34 the clipboard contents
35 PDC_CLIP_EMPTY the clipboard contains no text
36 PDC_CLIP_ACCESS_ERROR no clipboard support
38 Portability X/Open BSD SYS V
39 PDC_getclipboard - - -
40 PDC_setclipboard - - -
41 PDC_freeclipboard - - -
42 PDC_clearclipboard - - -
44 **man-end****************************************************************/
47 # define PDC_TEXT CF_UNICODETEXT
49 # define PDC_TEXT CF_OEMTEXT
52 int PDC_getclipboard(char **contents
, long *length
)
57 PDC_LOG(("PDC_getclipboard() - called\n"));
59 if (!OpenClipboard(NULL
))
60 return PDC_CLIP_ACCESS_ERROR
;
62 if ((handle
= GetClipboardData(PDC_TEXT
)) == NULL
)
65 return PDC_CLIP_EMPTY
;
69 len
= wcslen((wchar_t *)handle
) * 3;
71 len
= strlen((char *)handle
);
73 *contents
= (char *)GlobalAlloc(GMEM_FIXED
, len
+ 1);
78 return PDC_CLIP_MEMORY_ERROR
;
82 len
= PDC_wcstombs((char *)*contents
, (wchar_t *)handle
, len
);
84 strcpy((char *)*contents
, (char *)handle
);
89 return PDC_CLIP_SUCCESS
;
92 int PDC_setclipboard(const char *contents
, long length
)
97 PDC_LOG(("PDC_setclipboard() - called\n"));
99 if (!OpenClipboard(NULL
))
100 return PDC_CLIP_ACCESS_ERROR
;
102 ptr1
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
,
103 (length
+ 1) * sizeof(TCHAR
));
106 return PDC_CLIP_MEMORY_ERROR
;
108 ptr2
= GlobalLock(ptr1
);
111 PDC_mbstowcs((wchar_t *)ptr2
, contents
, length
);
113 memcpy((char *)ptr2
, contents
, length
+ 1);
118 if (!SetClipboardData(PDC_TEXT
, ptr1
))
121 return PDC_CLIP_ACCESS_ERROR
;
127 return PDC_CLIP_SUCCESS
;
130 int PDC_freeclipboard(char *contents
)
132 PDC_LOG(("PDC_freeclipboard() - called\n"));
134 GlobalFree(contents
);
135 return PDC_CLIP_SUCCESS
;
138 int PDC_clearclipboard(void)
140 PDC_LOG(("PDC_clearclipboard() - called\n"));
144 return PDC_CLIP_SUCCESS
;