2 * wclipbrd.c -- Clipboard routines for WinBoard
\r
3 * $Id: wclipbrd.c,v 2.1 2003/10/27 19:21:02 mann Exp $
\r
5 * Copyright 2000,2009 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\r
9 * GNU XBoard is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or (at
\r
12 * your option) any later version.
\r
14 * GNU XBoard is distributed in the hope that it will be useful, but
\r
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
17 * General Public License for more details.
\r
19 * You should have received a copy of the GNU General Public License
\r
20 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
22 *------------------------------------------------------------------------
\r
23 ** See the file ChangeLog for a revision history. */
\r
27 #include <windows.h> /* required for all Windows applications */
\r
31 #include <sys/stat.h>
\r
34 #include "winboard.h"
\r
35 #include "frontend.h"
\r
36 #include "backend.h"
\r
37 #include "wclipbrd.h"
\r
39 /* Imports from winboard.c */
\r
40 extern HWND hwndMain;
\r
43 static char *copyTemp;
\r
44 static char *pasteTemp;
\r
47 CopyFENToClipboard()
\r
51 fen = PositionToFEN(currentMove,1);
\r
53 DisplayError("Unable to convert position to FEN.", 0);
\r
56 if (!CopyTextToClipboard(fen))
\r
57 DisplayError("Unable to copy FEN to clipboard.", 0);
\r
62 HGLOBAL ExportGameListAsText();
\r
64 VOID CopyGameListToClipboard()
\r
66 HGLOBAL hMem = ExportGameListAsText();
\r
68 if( hMem != NULL ) {
\r
69 /* Assign memory block to clipboard */
\r
70 BOOL ok = OpenClipboard( hwndMain );
\r
73 ok = EmptyClipboard();
\r
76 if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {
\r
89 DisplayError( "Cannot copy list to clipboard.", 0 );
\r
95 CopyGameToClipboard()
\r
97 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
98 * file into the clipboard.
\r
102 unsigned long size;
\r
107 copyTemp = tempnam(NULL, "wbcp");
\r
110 DisplayError("Cannot create temporary file name.",0);
\r
113 f = fopen(copyTemp, "w");
\r
115 DisplayError("Cannot open temporary file.", 0);
\r
118 if (!SaveGame(f,0,"")) { /* call into backend */
\r
119 DisplayError("Cannot write to temporary file.", 0);
\r
120 goto copy_game_to_clipboard_cleanup;
\r
122 f = fopen(copyTemp, "rb");
\r
124 DisplayError("Cannot reopen temporary file.", 0);
\r
125 goto copy_game_to_clipboard_cleanup;
\r
127 if (fstat(fileno(f), &st) < 0) {
\r
128 DisplayError("Cannot determine size of file.", 0);
\r
129 goto copy_game_to_clipboard_cleanup;
\r
133 DisplayError("Cannot determine size of file.", 0);
\r
134 goto copy_game_to_clipboard_cleanup;
\r
137 buf = (char*)malloc(size+1);
\r
139 DisplayError("Cannot allocate clipboard buffer.", 0);
\r
140 goto copy_game_to_clipboard_cleanup;
\r
142 len = fread(buf, sizeof(char), size, f);
\r
144 DisplayError("Cannot read from temporary file.", 0);
\r
145 goto copy_game_to_clipboard_cleanup;
\r
147 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
148 DisplayError("Error reading from temporary file.", 0);
\r
149 goto copy_game_to_clipboard_cleanup;
\r
152 if (!CopyTextToClipboard(buf)) {
\r
153 DisplayError("Cannot copy text to clipboard", 0);
\r
156 copy_game_to_clipboard_cleanup:
\r
157 if (buf) free(buf);
\r
163 CopyTextToClipboard(char *text)
\r
165 /* some (most?) of the error checking may be overkill,
\r
166 * but hey, this is Windows
\r
168 HGLOBAL hGlobalMem;
\r
169 LPVOID lpGlobalMem;
\r
174 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
175 if (hGlobalMem == NULL) {
\r
176 DisplayError("Unable to allocate memory for clipboard.", 0);
\r
179 lpGlobalMem = GlobalLock(hGlobalMem);
\r
180 if (lpGlobalMem == NULL) {
\r
181 DisplayError("Unable to lock clipboard memory.", 0);
\r
182 GlobalFree(hGlobalMem);
\r
185 lstrcpy(lpGlobalMem, text);
\r
186 if (appData.debugMode) {
\r
187 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
188 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
190 SetLastError(NO_ERROR);
\r
191 locked = GlobalUnlock(hGlobalMem);
\r
192 err = GetLastError();
\r
193 if (appData.debugMode) {
\r
194 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
195 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
198 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
199 if (appData.debugMode) {
\r
201 "CopyTextToClipboard(): err %d locked %d\n", err, locked);
\r
205 DisplayError("Cannot unlock clipboard memory.", 0);
\r
206 GlobalFree(hGlobalMem);
\r
209 if (!OpenClipboard(hwndMain)) {
\r
210 DisplayError("Cannot open clipboard.", 0);
\r
211 GlobalFree(hGlobalMem);
\r
214 if (!EmptyClipboard()) {
\r
215 DisplayError("Cannot empty clipboard.", 0);
\r
218 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
219 DisplayError("Cannot copy text to clipboard.", 0);
\r
221 GlobalFree(hGlobalMem);
\r
224 if (!CloseClipboard())
\r
225 DisplayError("Cannot close clipboard.", 0);
\r
230 /* [AS] Reworked paste functions so they can work with strings too */
\r
232 VOID PasteFENFromString( char * fen )
\r
234 if (appData.debugMode) {
\r
235 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
\r
237 EditPositionPasteFEN(fen); /* call into backend */
\r
243 PasteFENFromClipboard()
\r
246 if (!PasteTextFromClipboard(&fen)) {
\r
247 DisplayError("Unable to paste FEN from clipboard.", 0);
\r
250 PasteFENFromString( fen );
\r
253 VOID PasteGameFromString( char * buf )
\r
258 pasteTemp = tempnam(NULL, "wbpt");
\r
260 f = fopen(pasteTemp, "w");
\r
262 DisplayError("Unable to create temporary file.", 0);
\r
263 free(buf); /* [AS] */
\r
266 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
268 if (len != strlen(buf)) {
\r
269 DisplayError("Error writing to temporary file.", 0);
\r
270 free(buf); /* [AS] */
\r
273 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
274 free( buf ); /* [AS] */
\r
279 PasteGameFromClipboard()
\r
281 /* Write the clipboard to a temp file, then let LoadGameFromFile()
\r
282 * do all the work. */
\r
284 if (!PasteTextFromClipboard(&buf)) {
\r
287 PasteGameFromString( buf );
\r
290 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
\r
291 VOID PasteGameOrFENFromClipboard()
\r
296 if (!PasteTextFromClipboard(&buf)) {
\r
301 while( *tmp == ' ' || *tmp == '\t' || *tmp == '\r' || *tmp == '\n' ) {
\r
305 if( *tmp == '[' ) {
\r
306 PasteGameFromString( buf );
\r
309 PasteFENFromString( buf );
\r
314 PasteTextFromClipboard(char **text)
\r
316 /* some (most?) of the error checking may be overkill,
\r
317 * but hey, this is Windows
\r
321 BOOL locked = FALSE;
\r
325 if (!OpenClipboard(hwndMain)) {
\r
326 DisplayError("Unable to open clipboard.", 0);
\r
329 hClipMem = GetClipboardData(CF_TEXT);
\r
330 if (hClipMem == NULL) {
\r
332 DisplayError("No text in clipboard.", 0);
\r
335 lpClipMem = GlobalLock(hClipMem);
\r
336 if (lpClipMem == NULL) {
\r
338 DisplayError("Unable to lock clipboard memory.", 0);
\r
341 *text = (char *) malloc(GlobalSize(hClipMem)+1);
\r
343 DisplayError("Unable to allocate memory for text string.", 0);
\r
347 lstrcpy(*text, lpClipMem);
\r
348 if (appData.debugMode) {
\r
349 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
350 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
352 SetLastError(NO_ERROR);
\r
354 /*suggested by Wilkin Ng*/
\r
355 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
357 locked = GlobalUnlock(hClipMem);
\r
360 locked = GlobalUnlock(hClipMem);
\r
362 err = GetLastError();
\r
363 if (appData.debugMode) {
\r
364 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
365 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
368 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
369 if (appData.debugMode) {
\r
371 "PasteTextFromClipboard(): err %d locked %d\n", err, locked);
\r
375 DisplayError("Unable to unlock clipboard memory.", 0);
\r
377 if (!CloseClipboard())
\r
378 DisplayError("Unable to close clipboard.", 0);
\r
384 DeleteClipboardTempFiles()
\r
386 if (copyTemp) remove(copyTemp);
\r
387 if (pasteTemp) remove(pasteTemp);
\r