2 * wclipbrd.c -- Clipboard routines for WinBoard
\r
4 * Copyright 2000, 2009, 2010, 2011, 2012, 2013, 2014 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
43 /* Imports from winboard.c */
\r
44 extern HWND hwndMain;
\r
47 static char *copyTemp;
\r
48 static char *pasteTemp;
\r
51 CopyFENToClipboard()
\r
55 if(gameMode == EditPosition) EditPositionDone(TRUE); // [HGM] mak sure castling rights are set consistently
\r
56 fen = PositionToFEN(currentMove, NULL, 1);
\r
58 DisplayError(_("Unable to convert position to FEN."), 0);
\r
61 if (!CopyTextToClipboard(fen))
\r
62 DisplayError(_("Unable to copy FEN to clipboard."), 0);
\r
67 HGLOBAL ExportGameListAsText();
\r
69 VOID CopyGameListToClipboard()
\r
71 HGLOBAL hMem = ExportGameListAsText();
\r
73 if( hMem != NULL ) {
\r
74 /* Assign memory block to clipboard */
\r
75 BOOL ok = OpenClipboard( hwndMain );
\r
78 ok = EmptyClipboard();
\r
81 if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {
\r
94 DisplayError( "Cannot copy list to clipboard.", 0 );
\r
100 CopyGameToClipboard()
\r
102 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
103 * file into the clipboard.
\r
107 unsigned long size;
\r
112 copyTemp = tempnam(NULL, "wbcp");
\r
115 DisplayError(_("Cannot create temporary file name."),0);
\r
118 f = fopen(copyTemp, "w");
\r
120 DisplayError(_("Cannot open temporary file."), 0);
\r
123 if (!SaveGame(f,0,"")) { /* call into backend */
\r
124 DisplayError(_("Cannot write to temporary file."), 0);
\r
125 goto copy_game_to_clipboard_cleanup;
\r
127 f = fopen(copyTemp, "rb");
\r
129 DisplayError(_("Cannot reopen temporary file."), 0);
\r
130 goto copy_game_to_clipboard_cleanup;
\r
132 if (fstat(fileno(f), &st) < 0) {
\r
133 DisplayError(_(_("Cannot determine size of file.")), 0);
\r
134 goto copy_game_to_clipboard_cleanup;
\r
138 DisplayError(_(_("Cannot determine size of file.")), 0);
\r
139 goto copy_game_to_clipboard_cleanup;
\r
142 buf = (char*)malloc(size+1);
\r
144 DisplayError(_("Cannot allocate clipboard buffer."), 0);
\r
145 goto copy_game_to_clipboard_cleanup;
\r
147 len = fread(buf, sizeof(char), size, f);
\r
149 DisplayError(_("Cannot read from temporary file."), 0);
\r
150 goto copy_game_to_clipboard_cleanup;
\r
152 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
153 DisplayError(_("Error reading from temporary file."), 0);
\r
154 goto copy_game_to_clipboard_cleanup;
\r
157 if (!CopyTextToClipboard(buf)) {
\r
158 DisplayError(_("Cannot copy text to clipboard"), 0);
\r
161 copy_game_to_clipboard_cleanup:
\r
162 if (buf) free(buf);
\r
168 CopyTextToClipboard(char *text)
\r
170 /* some (most?) of the error checking may be overkill,
\r
171 * but hey, this is Windows
\r
173 HGLOBAL hGlobalMem;
\r
174 LPVOID lpGlobalMem;
\r
179 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
180 if (hGlobalMem == NULL) {
\r
181 DisplayError(_("Unable to allocate memory for clipboard."), 0);
\r
184 lpGlobalMem = GlobalLock(hGlobalMem);
\r
185 if (lpGlobalMem == NULL) {
\r
186 DisplayError(_(_("Unable to lock clipboard memory.")), 0);
\r
187 GlobalFree(hGlobalMem);
\r
190 safeStrCpy(lpGlobalMem, text, 1<<20);
\r
191 if (appData.debugMode) {
\r
192 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
193 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
195 SetLastError(NO_ERROR);
\r
196 locked = GlobalUnlock(hGlobalMem);
\r
197 err = GetLastError();
\r
198 if (appData.debugMode) {
\r
199 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
200 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
203 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
204 if (appData.debugMode) {
\r
206 "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked);
\r
210 DisplayError(_("Cannot unlock clipboard memory."), 0);
\r
211 GlobalFree(hGlobalMem);
\r
214 if (!OpenClipboard(hwndMain)) {
\r
215 DisplayError(_("Cannot open clipboard."), 0);
\r
216 GlobalFree(hGlobalMem);
\r
219 if (!EmptyClipboard()) {
\r
220 DisplayError(_("Cannot empty clipboard."), 0);
\r
223 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
224 DisplayError(_("Cannot copy text to clipboard."), 0);
\r
226 GlobalFree(hGlobalMem);
\r
229 if (!CloseClipboard())
\r
230 DisplayError(_("Cannot close clipboard."), 0);
\r
235 /* [AS] Reworked paste functions so they can work with strings too */
\r
237 VOID PasteFENFromString( char * fen )
\r
239 if (appData.debugMode) {
\r
240 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
\r
242 EditPositionPasteFEN(fen); /* call into backend */
\r
248 PasteFENFromClipboard()
\r
251 if (!PasteTextFromClipboard(&fen)) {
\r
252 DisplayError(_("Unable to paste FEN from clipboard."), 0);
\r
255 PasteFENFromString( fen );
\r
258 VOID PasteGameFromString( char * buf )
\r
263 pasteTemp = tempnam(NULL, "wbpt");
\r
265 f = fopen(pasteTemp, "w");
\r
267 DisplayError(_("Unable to create temporary file."), 0);
\r
268 free(buf); /* [AS] */
\r
271 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
273 if (len != strlen(buf)) {
\r
274 DisplayError(_("Error writing to temporary file."), 0);
\r
275 free(buf); /* [AS] */
\r
278 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
279 free( buf ); /* [AS] */
\r
284 PasteGameFromClipboard()
\r
286 /* Write the clipboard to a temp file, then let LoadGameFromFile()
\r
287 * do all the work. */
\r
289 if (!PasteTextFromClipboard(&buf)) {
\r
292 PasteGameFromString( buf );
\r
295 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
\r
296 VOID PasteGameOrFENFromClipboard()
\r
300 Board dummyBoard; int dummy; // [HGM] paste any
\r
302 if (!PasteTextFromClipboard(&buf)) {
\r
306 // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead
\r
307 if(!ParseFEN(dummyBoard, &dummy, buf, 0) ) {
\r
308 PasteGameFromString( buf );
\r
311 PasteFENFromString( buf );
\r
316 PasteTextFromClipboard(char **text)
\r
318 /* some (most?) of the error checking may be overkill,
\r
319 * but hey, this is Windows
\r
323 BOOL locked = FALSE;
\r
327 if (!OpenClipboard(hwndMain)) {
\r
328 DisplayError(_("Unable to open clipboard."), 0);
\r
331 hClipMem = GetClipboardData(CF_TEXT);
\r
332 if (hClipMem == NULL) {
\r
334 DisplayError(_("No text in clipboard."), 0);
\r
337 lpClipMem = GlobalLock(hClipMem);
\r
338 if (lpClipMem == NULL) {
\r
340 DisplayError(_(_("Unable to lock clipboard memory.")), 0);
\r
343 *text = (char *) malloc(GlobalSize(hClipMem)+1);
\r
345 DisplayError(_("Unable to allocate memory for text string."), 0);
\r
349 safeStrCpy(*text, lpClipMem, 1<<20 );
\r
350 if (appData.debugMode) {
\r
351 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
352 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
354 SetLastError(NO_ERROR);
\r
355 /*suggested by Wilkin Ng*/
\r
356 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
358 locked = GlobalUnlock(hClipMem);
\r
360 err = GetLastError();
\r
361 if (appData.debugMode) {
\r
362 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
363 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
366 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
367 if (appData.debugMode) {
\r
369 "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked);
\r
373 DisplayError(_("Unable to unlock clipboard memory."), 0);
\r
375 if (!CloseClipboard())
\r
376 DisplayError(_("Unable to close clipboard."), 0);
\r
382 DeleteClipboardTempFiles()
\r
384 if (copyTemp) remove(copyTemp);
\r
385 if (pasteTemp) remove(pasteTemp);
\r