Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/wincon/pdcclip.c
3153 views
1
/* PDCurses */
2
3
#include "pdcwin.h"
4
5
#include <string.h>
6
7
/*man-start**************************************************************
8
9
clipboard
10
---------
11
12
### Synopsis
13
14
int PDC_getclipboard(char **contents, long *length);
15
int PDC_setclipboard(const char *contents, long length);
16
int PDC_freeclipboard(char *contents);
17
int PDC_clearclipboard(void);
18
19
### Description
20
21
PDC_getclipboard() gets the textual contents of the system's
22
clipboard. This function returns the contents of the clipboard in the
23
contents argument. It is the responsibility of the caller to free the
24
memory returned, via PDC_freeclipboard(). The length of the clipboard
25
contents is returned in the length argument.
26
27
PDC_setclipboard copies the supplied text into the system's
28
clipboard, emptying the clipboard prior to the copy.
29
30
PDC_clearclipboard() clears the internal clipboard.
31
32
### Return Values
33
34
indicator of success/failure of call.
35
PDC_CLIP_SUCCESS the call was successful
36
PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
37
the clipboard contents
38
PDC_CLIP_EMPTY the clipboard contains no text
39
PDC_CLIP_ACCESS_ERROR no clipboard support
40
41
### Portability
42
X/Open ncurses NetBSD
43
PDC_getclipboard - - -
44
PDC_setclipboard - - -
45
PDC_freeclipboard - - -
46
PDC_clearclipboard - - -
47
48
**man-end****************************************************************/
49
50
#ifdef PDC_WIDE
51
# define PDC_TEXT CF_UNICODETEXT
52
#else
53
# define PDC_TEXT CF_OEMTEXT
54
#endif
55
56
int PDC_getclipboard(char **contents, long *length)
57
{
58
HANDLE handle;
59
long len;
60
61
PDC_LOG(("PDC_getclipboard() - called\n"));
62
63
if (!OpenClipboard(NULL))
64
return PDC_CLIP_ACCESS_ERROR;
65
66
if ((handle = GetClipboardData(PDC_TEXT)) == NULL)
67
{
68
CloseClipboard();
69
return PDC_CLIP_EMPTY;
70
}
71
72
#ifdef PDC_WIDE
73
len = wcslen((wchar_t *)handle) * 3;
74
#else
75
len = strlen((char *)handle);
76
#endif
77
*contents = (char *)GlobalAlloc(GMEM_FIXED, len + 1);
78
79
if (!*contents)
80
{
81
CloseClipboard();
82
return PDC_CLIP_MEMORY_ERROR;
83
}
84
85
#ifdef PDC_WIDE
86
len = PDC_wcstombs((char *)*contents, (wchar_t *)handle, len);
87
#else
88
strcpy((char *)*contents, (char *)handle);
89
#endif
90
*length = len;
91
CloseClipboard();
92
93
return PDC_CLIP_SUCCESS;
94
}
95
96
int PDC_setclipboard(const char *contents, long length)
97
{
98
HGLOBAL ptr1;
99
LPTSTR ptr2;
100
101
PDC_LOG(("PDC_setclipboard() - called\n"));
102
103
if (!OpenClipboard(NULL))
104
return PDC_CLIP_ACCESS_ERROR;
105
106
ptr1 = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
107
(length + 1) * sizeof(TCHAR));
108
109
if (!ptr1)
110
return PDC_CLIP_MEMORY_ERROR;
111
112
ptr2 = GlobalLock(ptr1);
113
114
#ifdef PDC_WIDE
115
PDC_mbstowcs((wchar_t *)ptr2, contents, length);
116
#else
117
memcpy((char *)ptr2, contents, length + 1);
118
#endif
119
GlobalUnlock(ptr1);
120
EmptyClipboard();
121
122
if (!SetClipboardData(PDC_TEXT, ptr1))
123
{
124
GlobalFree(ptr1);
125
return PDC_CLIP_ACCESS_ERROR;
126
}
127
128
CloseClipboard();
129
GlobalFree(ptr1);
130
131
return PDC_CLIP_SUCCESS;
132
}
133
134
int PDC_freeclipboard(char *contents)
135
{
136
PDC_LOG(("PDC_freeclipboard() - called\n"));
137
138
GlobalFree(contents);
139
return PDC_CLIP_SUCCESS;
140
}
141
142
int PDC_clearclipboard(void)
143
{
144
PDC_LOG(("PDC_clearclipboard() - called\n"));
145
146
EmptyClipboard();
147
148
return PDC_CLIP_SUCCESS;
149
}
150
151