2 * wclipbrd.c -- Clipboard routines for WinBoard
\r
4 * Copyright 2000,2009 Free Software Foundation, Inc.
\r
6 * Enhancements Copyright 2005 Alessandro Scotti
\r
8 * ------------------------------------------------------------------------
\r
10 * GNU XBoard is free software: you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License as published by
\r
12 * the Free Software Foundation, either version 3 of the License, or (at
\r
13 * your option) any later version.
\r
15 * GNU XBoard is distributed in the hope that it will be useful, but
\r
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
18 * General Public License for more details.
\r
20 * You should have received a copy of the GNU General Public License
\r
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
23 *------------------------------------------------------------------------
\r
24 ** See the file ChangeLog for a revision history. */
\r
28 #include <windows.h> /* required for all Windows applications */
\r
32 #include <sys/stat.h>
\r
35 #include "frontend.h"
\r
36 #include "backend.h"
\r
37 #include "winboard.h"
\r
38 #include "wclipbrd.h"
\r
40 /* Imports from winboard.c */
\r
41 extern HWND hwndMain;
\r
42 Boolean ParseFEN(Board b, int *stm, char *FEN);
\r
45 static char *copyTemp;
\r
46 static char *pasteTemp;
\r
49 CopyFENToClipboard()
\r
53 if(gameMode == EditPosition) EditPositionDone(TRUE); // [HGM] mak sure castling rights are set consistently
\r
54 fen = PositionToFEN(currentMove, NULL);
\r
56 DisplayError("Unable to convert position to FEN.", 0);
\r
59 if (!CopyTextToClipboard(fen))
\r
60 DisplayError("Unable to copy FEN to clipboard.", 0);
\r
65 HGLOBAL ExportGameListAsText();
\r
67 VOID CopyGameListToClipboard()
\r
69 HGLOBAL hMem = ExportGameListAsText();
\r
71 if( hMem != NULL ) {
\r
72 /* Assign memory block to clipboard */
\r
73 BOOL ok = OpenClipboard( hwndMain );
\r
76 ok = EmptyClipboard();
\r
79 if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {
\r
92 DisplayError( "Cannot copy list to clipboard.", 0 );
\r
98 CopyGameToClipboard()
\r
100 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
101 * file into the clipboard.
\r
105 unsigned long size;
\r
110 copyTemp = tempnam(NULL, "wbcp");
\r
113 DisplayError("Cannot create temporary file name.",0);
\r
116 f = fopen(copyTemp, "w");
\r
118 DisplayError("Cannot open temporary file.", 0);
\r
121 if (!SaveGame(f,0,"")) { /* call into backend */
\r
122 DisplayError("Cannot write to temporary file.", 0);
\r
123 goto copy_game_to_clipboard_cleanup;
\r
125 f = fopen(copyTemp, "rb");
\r
127 DisplayError("Cannot reopen temporary file.", 0);
\r
128 goto copy_game_to_clipboard_cleanup;
\r
130 if (fstat(fileno(f), &st) < 0) {
\r
131 DisplayError("Cannot determine size of file.", 0);
\r
132 goto copy_game_to_clipboard_cleanup;
\r
136 DisplayError("Cannot determine size of file.", 0);
\r
137 goto copy_game_to_clipboard_cleanup;
\r
140 buf = (char*)malloc(size+1);
\r
142 DisplayError("Cannot allocate clipboard buffer.", 0);
\r
143 goto copy_game_to_clipboard_cleanup;
\r
145 len = fread(buf, sizeof(char), size, f);
\r
147 DisplayError("Cannot read from temporary file.", 0);
\r
148 goto copy_game_to_clipboard_cleanup;
\r
150 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
151 DisplayError("Error reading from temporary file.", 0);
\r
152 goto copy_game_to_clipboard_cleanup;
\r
155 if (!CopyTextToClipboard(buf)) {
\r
156 DisplayError("Cannot copy text to clipboard", 0);
\r
159 copy_game_to_clipboard_cleanup:
\r
160 if (buf) free(buf);
\r
166 CopyTextToClipboard(char *text)
\r
168 /* some (most?) of the error checking may be overkill,
\r
169 * but hey, this is Windows
\r
171 HGLOBAL hGlobalMem;
\r
172 LPVOID lpGlobalMem;
\r
177 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
178 if (hGlobalMem == NULL) {
\r
179 DisplayError("Unable to allocate memory for clipboard.", 0);
\r
182 lpGlobalMem = GlobalLock(hGlobalMem);
\r
183 if (lpGlobalMem == NULL) {
\r
184 DisplayError("Unable to lock clipboard memory.", 0);
\r
185 GlobalFree(hGlobalMem);
\r
188 lstrcpy(lpGlobalMem, text);
\r
189 if (appData.debugMode) {
\r
190 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
191 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
193 SetLastError(NO_ERROR);
\r
194 locked = GlobalUnlock(hGlobalMem);
\r
195 err = GetLastError();
\r
196 if (appData.debugMode) {
\r
197 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
198 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
201 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
202 if (appData.debugMode) {
\r
204 "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked);
\r
208 DisplayError("Cannot unlock clipboard memory.", 0);
\r
209 GlobalFree(hGlobalMem);
\r
212 if (!OpenClipboard(hwndMain)) {
\r
213 DisplayError("Cannot open clipboard.", 0);
\r
214 GlobalFree(hGlobalMem);
\r
217 if (!EmptyClipboard()) {
\r
218 DisplayError("Cannot empty clipboard.", 0);
\r
221 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
222 DisplayError("Cannot copy text to clipboard.", 0);
\r
224 GlobalFree(hGlobalMem);
\r
227 if (!CloseClipboard())
\r
228 DisplayError("Cannot close clipboard.", 0);
\r
233 /* [AS] Reworked paste functions so they can work with strings too */
\r
235 VOID PasteFENFromString( char * fen )
\r
237 if (appData.debugMode) {
\r
238 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
\r
240 EditPositionPasteFEN(fen); /* call into backend */
\r
246 PasteFENFromClipboard()
\r
249 if (!PasteTextFromClipboard(&fen)) {
\r
250 DisplayError("Unable to paste FEN from clipboard.", 0);
\r
253 PasteFENFromString( fen );
\r
256 VOID PasteGameFromString( char * buf )
\r
261 pasteTemp = tempnam(NULL, "wbpt");
\r
263 f = fopen(pasteTemp, "w");
\r
265 DisplayError("Unable to create temporary file.", 0);
\r
266 free(buf); /* [AS] */
\r
269 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
271 if (len != strlen(buf)) {
\r
272 DisplayError("Error writing to temporary file.", 0);
\r
273 free(buf); /* [AS] */
\r
276 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
277 free( buf ); /* [AS] */
\r
282 PasteGameFromClipboard()
\r
284 /* Write the clipboard to a temp file, then let LoadGameFromFile()
\r
285 * do all the work. */
\r
287 if (!PasteTextFromClipboard(&buf)) {
\r
290 PasteGameFromString( buf );
\r
293 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
\r
294 VOID PasteGameOrFENFromClipboard()
\r
298 Board dummyBoard; int dummy; // [HGM] paste any
\r
300 if (!PasteTextFromClipboard(&buf)) {
\r
304 // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead
\r
305 if(!ParseFEN(dummyBoard, &dummy, buf) ) {
\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
353 /*suggested by Wilkin Ng*/
\r
354 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
356 locked = GlobalUnlock(hClipMem);
\r
358 err = GetLastError();
\r
359 if (appData.debugMode) {
\r
360 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
361 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
364 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
365 if (appData.debugMode) {
\r
367 "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked);
\r
371 DisplayError("Unable to unlock clipboard memory.", 0);
\r
373 if (!CloseClipboard())
\r
374 DisplayError("Unable to close clipboard.", 0);
\r
380 DeleteClipboardTempFiles()
\r
382 if (copyTemp) remove(copyTemp);
\r
383 if (pasteTemp) remove(pasteTemp);
\r