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 "winboard.h"
\r
36 #include "frontend.h"
\r
37 #include "backend.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 fen = PositionToFEN(currentMove, NULL);
\r
55 DisplayError("Unable to convert position to FEN.", 0);
\r
58 if (!CopyTextToClipboard(fen))
\r
59 DisplayError("Unable to copy FEN to clipboard.", 0);
\r
64 HGLOBAL ExportGameListAsText();
\r
66 VOID CopyGameListToClipboard()
\r
68 HGLOBAL hMem = ExportGameListAsText();
\r
70 if( hMem != NULL ) {
\r
71 /* Assign memory block to clipboard */
\r
72 BOOL ok = OpenClipboard( hwndMain );
\r
75 ok = EmptyClipboard();
\r
78 if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {
\r
91 DisplayError( "Cannot copy list to clipboard.", 0 );
\r
97 CopyGameToClipboard()
\r
99 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
100 * file into the clipboard.
\r
104 unsigned long size;
\r
109 copyTemp = tempnam(NULL, "wbcp");
\r
112 DisplayError("Cannot create temporary file name.",0);
\r
115 f = fopen(copyTemp, "w");
\r
117 DisplayError("Cannot open temporary file.", 0);
\r
120 if (!SaveGame(f,0,"")) { /* call into backend */
\r
121 DisplayError("Cannot write to temporary file.", 0);
\r
122 goto copy_game_to_clipboard_cleanup;
\r
124 f = fopen(copyTemp, "rb");
\r
126 DisplayError("Cannot reopen temporary file.", 0);
\r
127 goto copy_game_to_clipboard_cleanup;
\r
129 if (fstat(fileno(f), &st) < 0) {
\r
130 DisplayError("Cannot determine size of file.", 0);
\r
131 goto copy_game_to_clipboard_cleanup;
\r
135 DisplayError("Cannot determine size of file.", 0);
\r
136 goto copy_game_to_clipboard_cleanup;
\r
139 buf = (char*)malloc(size+1);
\r
141 DisplayError("Cannot allocate clipboard buffer.", 0);
\r
142 goto copy_game_to_clipboard_cleanup;
\r
144 len = fread(buf, sizeof(char), size, f);
\r
146 DisplayError("Cannot read from temporary file.", 0);
\r
147 goto copy_game_to_clipboard_cleanup;
\r
149 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
150 DisplayError("Error reading from temporary file.", 0);
\r
151 goto copy_game_to_clipboard_cleanup;
\r
154 if (!CopyTextToClipboard(buf)) {
\r
155 DisplayError("Cannot copy text to clipboard", 0);
\r
158 copy_game_to_clipboard_cleanup:
\r
159 if (buf) free(buf);
\r
165 CopyTextToClipboard(char *text)
\r
167 /* some (most?) of the error checking may be overkill,
\r
168 * but hey, this is Windows
\r
170 HGLOBAL hGlobalMem;
\r
171 LPVOID lpGlobalMem;
\r
176 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
177 if (hGlobalMem == NULL) {
\r
178 DisplayError("Unable to allocate memory for clipboard.", 0);
\r
181 lpGlobalMem = GlobalLock(hGlobalMem);
\r
182 if (lpGlobalMem == NULL) {
\r
183 DisplayError("Unable to lock clipboard memory.", 0);
\r
184 GlobalFree(hGlobalMem);
\r
187 lstrcpy(lpGlobalMem, text);
\r
188 if (appData.debugMode) {
\r
189 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
190 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
192 SetLastError(NO_ERROR);
\r
193 locked = GlobalUnlock(hGlobalMem);
\r
194 err = GetLastError();
\r
195 if (appData.debugMode) {
\r
196 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
197 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
200 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
201 if (appData.debugMode) {
\r
203 "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked);
\r
207 DisplayError("Cannot unlock clipboard memory.", 0);
\r
208 GlobalFree(hGlobalMem);
\r
211 if (!OpenClipboard(hwndMain)) {
\r
212 DisplayError("Cannot open clipboard.", 0);
\r
213 GlobalFree(hGlobalMem);
\r
216 if (!EmptyClipboard()) {
\r
217 DisplayError("Cannot empty clipboard.", 0);
\r
220 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
221 DisplayError("Cannot copy text to clipboard.", 0);
\r
223 GlobalFree(hGlobalMem);
\r
226 if (!CloseClipboard())
\r
227 DisplayError("Cannot close clipboard.", 0);
\r
232 /* [AS] Reworked paste functions so they can work with strings too */
\r
234 VOID PasteFENFromString( char * fen )
\r
236 if (appData.debugMode) {
\r
237 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
\r
239 EditPositionPasteFEN(fen); /* call into backend */
\r
245 PasteFENFromClipboard()
\r
248 if (!PasteTextFromClipboard(&fen)) {
\r
249 DisplayError("Unable to paste FEN from clipboard.", 0);
\r
252 PasteFENFromString( fen );
\r
255 VOID PasteGameFromString( char * buf )
\r
260 pasteTemp = tempnam(NULL, "wbpt");
\r
262 f = fopen(pasteTemp, "w");
\r
264 DisplayError("Unable to create temporary file.", 0);
\r
265 free(buf); /* [AS] */
\r
268 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
270 if (len != strlen(buf)) {
\r
271 DisplayError("Error writing to temporary file.", 0);
\r
272 free(buf); /* [AS] */
\r
275 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
276 free( buf ); /* [AS] */
\r
281 PasteGameFromClipboard()
\r
283 /* Write the clipboard to a temp file, then let LoadGameFromFile()
\r
284 * do all the work. */
\r
286 if (!PasteTextFromClipboard(&buf)) {
\r
289 PasteGameFromString( buf );
\r
292 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
\r
293 VOID PasteGameOrFENFromClipboard()
\r
297 Board dummyBoard; int dummy; // [HGM] paste any
\r
299 if (!PasteTextFromClipboard(&buf)) {
\r
305 while( *tmp == ' ' || *tmp == '\t' || *tmp == '\r' || *tmp == '\n' ) {
\r
309 if( *tmp == '[' ) {
\r
311 // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead
\r
312 if(!ParseFEN(dummyBoard, &dummy, buf) ) {
\r
314 PasteGameFromString( buf );
\r
317 PasteFENFromString( buf );
\r
322 PasteTextFromClipboard(char **text)
\r
324 /* some (most?) of the error checking may be overkill,
\r
325 * but hey, this is Windows
\r
329 BOOL locked = FALSE;
\r
333 if (!OpenClipboard(hwndMain)) {
\r
334 DisplayError("Unable to open clipboard.", 0);
\r
337 hClipMem = GetClipboardData(CF_TEXT);
\r
338 if (hClipMem == NULL) {
\r
340 DisplayError("No text in clipboard.", 0);
\r
343 lpClipMem = GlobalLock(hClipMem);
\r
344 if (lpClipMem == NULL) {
\r
346 DisplayError("Unable to lock clipboard memory.", 0);
\r
349 *text = (char *) malloc(GlobalSize(hClipMem)+1);
\r
351 DisplayError("Unable to allocate memory for text string.", 0);
\r
355 lstrcpy(*text, lpClipMem);
\r
356 if (appData.debugMode) {
\r
357 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
358 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
360 SetLastError(NO_ERROR);
\r
362 /*suggested by Wilkin Ng*/
\r
363 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
365 locked = GlobalUnlock(hClipMem);
\r
368 locked = GlobalUnlock(hClipMem);
\r
370 err = GetLastError();
\r
371 if (appData.debugMode) {
\r
372 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
373 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
376 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
377 if (appData.debugMode) {
\r
379 "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked);
\r
383 DisplayError("Unable to unlock clipboard memory.", 0);
\r
385 if (!CloseClipboard())
\r
386 DisplayError("Unable to close clipboard.", 0);
\r
392 DeleteClipboardTempFiles()
\r
394 if (copyTemp) remove(copyTemp);
\r
395 if (pasteTemp) remove(pasteTemp);
\r