Release 940505
[wine] / misc / clipboard.c
1 /*
2  * 'Wine' Clipboard function handling
3  *
4  * Copyright 1994 Martin Ayotte
5  */
6
7 static char Copyright[] = "Copyright Martin Ayotte, 1994";
8
9 /*
10 #define DEBUG_CLIPBOARD
11 */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <windows.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include "prototypes.h"
21 #include "heap.h"
22 #include "win.h"
23
24 typedef struct tagCLIPFORMAT {
25     WORD        wFormatID;
26     WORD        wRefCount;
27     LPSTR       Name;
28     HANDLE      hData;
29     DWORD       BufSize;
30     void        *PrevFormat;
31     void        *NextFormat;
32 } CLIPFORMAT;
33 typedef CLIPFORMAT FAR* LPCLIPFORMAT;
34
35 static HWND hWndClipboardOwner = 0;
36 static HWND hWndViewer = 0;
37 static WORD LastRegFormat = 0xC000;
38
39 CLIPFORMAT ClipFormats[12]  = {
40     { CF_TEXT, 1, "Text", (HANDLE)NULL, 0, NULL, &ClipFormats[1] },
41     { CF_BITMAP, 1, "Bitmap", (HANDLE)NULL, 0, &ClipFormats[0], &ClipFormats[2] },
42     { CF_METAFILEPICT, 1, "MetaFile Picture", (HANDLE)NULL, 0, &ClipFormats[1], &ClipFormats[3] },
43     { CF_SYLK, 1, "Sylk", (HANDLE)NULL, 0, &ClipFormats[2], &ClipFormats[4] },
44     { CF_DIF, 1, "DIF", (HANDLE)NULL, 0, &ClipFormats[3], &ClipFormats[5] },
45     { CF_TIFF, 1, "TIFF", (HANDLE)NULL, 0, &ClipFormats[4], &ClipFormats[6] },
46     { CF_OEMTEXT, 1, "OEM Text", (HANDLE)NULL, 0, &ClipFormats[5], &ClipFormats[7] },
47     { CF_DIB, 1, "DIB", (HANDLE)NULL, 0, &ClipFormats[6], &ClipFormats[8] },
48     { CF_PALETTE, 1, "Palette", (HANDLE)NULL, 0, &ClipFormats[7], &ClipFormats[9] },
49     { CF_PENDATA, 1, "PenData", (HANDLE)NULL, 0, &ClipFormats[8], &ClipFormats[10] },
50     { CF_RIFF, 1, "RIFF", (HANDLE)NULL, 0, &ClipFormats[9], &ClipFormats[11] },
51     { CF_WAVE, 1, "Wave", (HANDLE)NULL, 0, &ClipFormats[10], NULL }
52     };
53
54 /**************************************************************************
55  *                      OpenClipboard           [USER.137]
56  */
57 BOOL OpenClipboard(HWND hWnd)
58 {
59     if (hWndClipboardOwner != 0) return FALSE;
60     hWndClipboardOwner = hWnd;
61 #ifdef DEBUG_CLIPBOARD
62     printf("OpenClipboard(%04X); !\n", hWnd);
63 #endif
64     return TRUE;
65 }
66
67
68 /**************************************************************************
69  *                      CloseClipboard          [USER.138]
70  */
71 BOOL CloseClipboard()
72 {
73     if (hWndClipboardOwner == 0) return FALSE;
74     hWndClipboardOwner = 0;
75 #ifdef DEBUG_CLIPBOARD
76     printf("CloseClipboard(); !\n");
77 #endif
78     return TRUE;
79 }
80
81
82 /**************************************************************************
83  *                      EmptyClipboard          [USER.139]
84  */
85 BOOL EmptyClipboard()
86 {
87     LPCLIPFORMAT lpFormat = ClipFormats; 
88     if (hWndClipboardOwner == 0) return FALSE;
89 #ifdef DEBUG_CLIPBOARD
90     printf("EmptyClipboard(); !\n");
91 #endif
92     while(TRUE) {
93         if (lpFormat == NULL) break;
94         if (lpFormat->hData != 0) {
95             GlobalFree(lpFormat->hData);
96             lpFormat->hData = 0;
97             }
98         lpFormat = lpFormat->NextFormat;
99         }
100     return TRUE;
101 }
102
103
104 /**************************************************************************
105  *                      GetClipboardOwner       [USER.140]
106  */
107 HWND GetClipboardOwner()
108 {
109 #ifdef DEBUG_CLIPBOARD
110     printf("GetClipboardOwner() = %04X !\n", hWndClipboardOwner);
111 #endif
112     return hWndClipboardOwner;
113 }
114
115
116 /**************************************************************************
117  *                      SetClipboardData        [USER.141]
118  */
119 HANDLE SetClipboardData(WORD wFormat, HANDLE hData)
120 {
121     LPCLIPFORMAT lpFormat = ClipFormats; 
122 #ifdef DEBUG_CLIPBOARD
123     printf("SetClipboardDate(%04X, %04X) !\n", wFormat, hData);
124 #endif
125     while(TRUE) {
126         if (lpFormat == NULL) return 0;
127         if (lpFormat->wFormatID == wFormat) break;
128         lpFormat = lpFormat->NextFormat;
129         }
130     if (lpFormat->hData != 0) GlobalFree(lpFormat->hData);
131     lpFormat->hData = hData;
132     return lpFormat->hData;
133 }
134
135
136 /**************************************************************************
137  *                      GetClipboardData        [USER.142]
138  */
139 HANDLE GetClipboardData(WORD wFormat)
140 {
141     LPCLIPFORMAT lpFormat = ClipFormats; 
142 #ifdef DEBUG_CLIPBOARD
143     printf("GetClipboardData(%04X) !\n", wFormat);
144 #endif
145     while(TRUE) {
146         if (lpFormat == NULL) return 0;
147         if (lpFormat->wFormatID == wFormat) break;
148         lpFormat = lpFormat->NextFormat;
149         }
150     return lpFormat->hData;
151 }
152
153
154 /**************************************************************************
155  *                      CountClipboardFormats   [USER.143]
156  */
157 int CountClipboardFormats()
158 {
159     int FormatCount = 0;
160     LPCLIPFORMAT lpFormat = ClipFormats; 
161     while(TRUE) {
162         if (lpFormat == NULL) break;
163         if (lpFormat->hData != 0) {
164 #ifdef DEBUG_CLIPBOARD
165             printf("CountClipboardFormats // Find Not Empty (%04X) !\n", 
166                                         lpFormat->hData);
167 #endif
168             FormatCount++;
169             }
170         lpFormat = lpFormat->NextFormat;
171         }
172 #ifdef DEBUG_CLIPBOARD
173     printf("CountClipboardFormats() = %d !\n", FormatCount);
174 #endif
175     return FormatCount;
176 }
177
178
179 /**************************************************************************
180  *                      EnumClipboardFormats    [USER.144]
181  */
182 WORD EnumClipboardFormats(WORD wFormat)
183 {
184     LPCLIPFORMAT lpFormat = ClipFormats; 
185 #ifdef DEBUG_CLIPBOARD
186     printf("EnumClipboardFormats(%04X) !\n", wFormat);
187 #endif
188     if (wFormat == 0) {
189         if (lpFormat->hData != 0) 
190             return lpFormat->wFormatID;
191         else 
192             wFormat = lpFormat->wFormatID;
193         }
194     while(TRUE) {
195         if (lpFormat == NULL) return 0;
196         if (lpFormat->wFormatID == wFormat) break;
197         lpFormat = lpFormat->NextFormat;
198         }
199 #ifdef DEBUG_CLIPBOARD
200     printf("EnumClipboardFormats // Find Last (%04X) !\n", 
201                                 lpFormat->wFormatID);
202 #endif
203     lpFormat = lpFormat->NextFormat;
204     while(TRUE) {
205         if (lpFormat == NULL) return 0;
206         if (lpFormat->hData != 0) break;
207         lpFormat = lpFormat->NextFormat;
208         }
209 #ifdef DEBUG_CLIPBOARD
210     printf("EnumClipboardFormats // Find Not Empty Id=%04X hData=%04X !\n",
211                                 lpFormat->wFormatID, lpFormat->hData);
212 #endif
213     return lpFormat->wFormatID;
214 }
215
216
217 /**************************************************************************
218  *                      RegisterClipboardFormat [USER.145]
219  */
220 WORD RegisterClipboardFormat(LPCSTR FormatName)
221 {
222     LPCLIPFORMAT lpNewFormat; 
223     LPCLIPFORMAT lpFormat = ClipFormats; 
224     if (FormatName == NULL) return 0;
225     while(TRUE) {
226         if (lpFormat->NextFormat == NULL) break;
227         lpFormat = lpFormat->NextFormat;
228         }
229     lpNewFormat = (LPCLIPFORMAT)malloc(sizeof(CLIPFORMAT));
230     if (lpNewFormat == NULL) return 0;
231     lpFormat->NextFormat = lpNewFormat;
232 #ifdef DEBUG_CLIPBOARD
233     printf("RegisterClipboardFormat('%s') !\n", FormatName);
234 #endif
235     lpNewFormat->wFormatID = LastRegFormat;
236     lpNewFormat->wRefCount = 1;
237     lpNewFormat->Name = (LPSTR)malloc(strlen(FormatName) + 1);
238     if (lpNewFormat->Name == NULL) {
239         free(lpNewFormat);
240         return 0;
241         }
242     strcpy(lpNewFormat->Name, FormatName);
243     lpNewFormat->hData = 0;
244     lpNewFormat->BufSize = 0;
245     lpNewFormat->PrevFormat = lpFormat;
246     lpNewFormat->NextFormat = NULL;
247     return LastRegFormat++;
248 }
249
250
251 /**************************************************************************
252  *                      GetClipboardFormatName  [USER.146]
253  */
254 int GetClipboardFormatName(WORD wFormat, LPSTR retStr, short maxlen)
255 {
256     LPCLIPFORMAT lpFormat = ClipFormats; 
257 #ifdef DEBUG_CLIPBOARD
258     printf("GetClipboardFormat(%04X, %08X, %d) !\n", wFormat, retStr, maxlen);
259 #endif
260     while(TRUE) {
261         if (lpFormat == NULL) return 0;
262         if (lpFormat->wFormatID == wFormat) break;
263         lpFormat = lpFormat->NextFormat;
264         }
265     if (lpFormat->Name == NULL) return 0;
266 #ifdef DEBUG_CLIPBOARD
267     printf("GetClipboardFormat // Name='%s' !\n", lpFormat->Name);
268 #endif
269     maxlen = min(maxlen - 1, strlen(lpFormat->Name));
270     printf("GetClipboardFormat // maxlen=%d !\n", maxlen);
271     memcpy(retStr, lpFormat->Name, maxlen);
272     retStr[maxlen] = 0;
273     return maxlen;
274 }
275
276
277 /**************************************************************************
278  *                      SetClipboardViewer      [USER.147]
279  */
280 HWND SetClipboardViewer(HWND hWnd)
281 {
282 #ifdef DEBUG_CLIPBOARD
283     printf("SetClipboardFormat(%04X) !\n", hWnd);
284 #endif
285     hWndViewer = hWnd;
286 }
287
288
289 /**************************************************************************
290  *                      GetClipboardViewer      [USER.148]
291  */
292 HWND GetClipboardViewer()
293 {
294 #ifdef DEBUG_CLIPBOARD
295     printf("GetClipboardFormat() = %04X !\n", hWndViewer);
296 #endif
297 }
298
299
300 /**************************************************************************
301  *                      ChangeClipboardChain    [USER.149]
302  */
303 BOOL ChangeClipboardChain(HWND hWnd, HWND hWndNext)
304 {
305 #ifdef DEBUG_CLIPBOARD
306     printf("ChangeClipboardChain(%04X, %04X) !\n", hWnd, hWndNext);
307 #endif
308 }
309
310
311 /**************************************************************************
312  *                      IsClipboardFormatAvailable      [USER.193]
313  */
314 BOOL IsClipboardFormatAvailable(WORD wFormat)
315 {
316     LPCLIPFORMAT lpFormat = ClipFormats; 
317 #ifdef DEBUG_CLIPBOARD
318     printf("IsClipboardFormatAvailable(%04X) !\n", wFormat);
319 #endif
320     while(TRUE) {
321         if (lpFormat == NULL) return FALSE;
322         if (lpFormat->wFormatID == wFormat) break;
323         lpFormat = lpFormat->NextFormat;
324         }
325     return (lpFormat->hData != 0);
326 }
327
328
329 /**************************************************************************
330  *                      GetOpenClipboardWindow  [USER.248]
331  */
332 HWND GetOpenClipboardWindow()
333 {
334 #ifdef DEBUG_CLIPBOARD
335     printf("GetOpenClipboardWindow() = %04X !\n", hWndClipboardOwner);
336 #endif
337     return hWndClipboardOwner;
338 }
339
340
341 /**************************************************************************
342  *                      GetPriorityClipboardFormat      [USER.402]
343  */
344 int GetPriorityClipboardFormat(WORD FAR *lpPriorityList, short nCount)
345 {
346 #ifdef DEBUG_CLIPBOARD
347     printf("GetPriorityClipboardFormat(%08X, %d) !\n", lpPriorityList, nCount);
348 #endif
349 }
350
351
352