2 * wclipbrd.c -- Clipboard routines for WinBoard
\r
4 * Copyright 2000, 2009, 2010, 2011, 2012 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
45 Boolean ParseFEN(Board b, int *stm, char *FEN);
\r
48 static char *copyTemp;
\r
49 static char *pasteTemp;
\r
52 CopyFENToClipboard()
\r
56 if(gameMode == EditPosition) EditPositionDone(TRUE); // [HGM] mak sure castling rights are set consistently
\r
57 fen = PositionToFEN(currentMove, NULL);
\r
59 DisplayError(_("Unable to convert position to FEN."), 0);
\r
62 if (!CopyTextToClipboard(fen))
\r
63 DisplayError(_("Unable to copy FEN to clipboard."), 0);
\r
68 HGLOBAL ExportGameListAsText();
\r
70 VOID CopyGameListToClipboard()
\r
72 HGLOBAL hMem = ExportGameListAsText();
\r
74 if( hMem != NULL ) {
\r
75 /* Assign memory block to clipboard */
\r
76 BOOL ok = OpenClipboard( hwndMain );
\r
79 ok = EmptyClipboard();
\r
82 if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {
\r
95 DisplayError( "Cannot copy list to clipboard.", 0 );
\r
101 CopyGameToClipboard()
\r
103 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
104 * file into the clipboard.
\r
108 unsigned long size;
\r
113 copyTemp = tempnam(NULL, "wbcp");
\r
116 DisplayError(_("Cannot create temporary file name."),0);
\r
119 f = fopen(copyTemp, "w");
\r
121 DisplayError(_("Cannot open temporary file."), 0);
\r
124 if (!SaveGame(f,0,"")) { /* call into backend */
\r
125 DisplayError(_("Cannot write to temporary file."), 0);
\r
126 goto copy_game_to_clipboard_cleanup;
\r
128 f = fopen(copyTemp, "rb");
\r
130 DisplayError(_("Cannot reopen temporary file."), 0);
\r
131 goto copy_game_to_clipboard_cleanup;
\r
133 if (fstat(fileno(f), &st) < 0) {
\r
134 DisplayError(_(_("Cannot determine size of file.")), 0);
\r
135 goto copy_game_to_clipboard_cleanup;
\r
139 DisplayError(_(_("Cannot determine size of file.")), 0);
\r
140 goto copy_game_to_clipboard_cleanup;
\r
143 buf = (char*)malloc(size+1);
\r
145 DisplayError(_("Cannot allocate clipboard buffer."), 0);
\r
146 goto copy_game_to_clipboard_cleanup;
\r
148 len = fread(buf, sizeof(char), size, f);
\r
150 DisplayError(_("Cannot read from temporary file."), 0);
\r
151 goto copy_game_to_clipboard_cleanup;
\r
153 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
154 DisplayError(_("Error reading from temporary file."), 0);
\r
155 goto copy_game_to_clipboard_cleanup;
\r
158 if (!CopyTextToClipboard(buf)) {
\r
159 DisplayError(_("Cannot copy text to clipboard"), 0);
\r
162 copy_game_to_clipboard_cleanup:
\r
163 if (buf) free(buf);
\r
169 CopyTextToClipboard(char *text)
\r
171 /* some (most?) of the error checking may be overkill,
\r
172 * but hey, this is Windows
\r
174 HGLOBAL hGlobalMem;
\r
175 LPVOID lpGlobalMem;
\r
180 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
181 if (hGlobalMem == NULL) {
\r
182 DisplayError(_("Unable to allocate memory for clipboard."), 0);
\r
185 lpGlobalMem = GlobalLock(hGlobalMem);
\r
186 if (lpGlobalMem == NULL) {
\r
187 DisplayError(_(_("Unable to lock clipboard memory.")), 0);
\r
188 GlobalFree(hGlobalMem);
\r
191 safeStrCpy(lpGlobalMem, text, 1<<20);
\r
192 if (appData.debugMode) {
\r
193 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
194 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
196 SetLastError(NO_ERROR);
\r
197 locked = GlobalUnlock(hGlobalMem);
\r
198 err = GetLastError();
\r
199 if (appData.debugMode) {
\r
200 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
201 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
204 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
205 if (appData.debugMode) {
\r
207 "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked);
\r
211 DisplayError(_("Cannot unlock clipboard memory."), 0);
\r
212 GlobalFree(hGlobalMem);
\r
215 if (!OpenClipboard(hwndMain)) {
\r
216 DisplayError(_("Cannot open clipboard."), 0);
\r
217 GlobalFree(hGlobalMem);
\r
220 if (!EmptyClipboard()) {
\r
221 DisplayError(_("Cannot empty clipboard."), 0);
\r
224 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
225 DisplayError(_("Cannot copy text to clipboard."), 0);
\r
227 GlobalFree(hGlobalMem);
\r
230 if (!CloseClipboard())
\r
231 DisplayError(_("Cannot close clipboard."), 0);
\r
236 /* [AS] Reworked paste functions so they can work with strings too */
\r
238 VOID PasteFENFromString( char * fen )
\r
240 if (appData.debugMode) {
\r
241 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
\r
243 EditPositionPasteFEN(fen); /* call into backend */
\r
249 PasteFENFromClipboard()
\r
252 if (!PasteTextFromClipboard(&fen)) {
\r
253 DisplayError(_("Unable to paste FEN from clipboard."), 0);
\r
256 PasteFENFromString( fen );
\r
259 VOID PasteGameFromString( char * buf )
\r
264 pasteTemp = tempnam(NULL, "wbpt");
\r
266 f = fopen(pasteTemp, "w");
\r
268 DisplayError(_("Unable to create temporary file."), 0);
\r
269 free(buf); /* [AS] */
\r
272 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
274 if (len != strlen(buf)) {
\r
275 DisplayError(_("Error writing to temporary file."), 0);
\r
276 free(buf); /* [AS] */
\r
279 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
280 free( buf ); /* [AS] */
\r
285 PasteGameFromClipboard()
\r
287 /* Write the clipboard to a temp file, then let LoadGameFromFile()
\r
288 * do all the work. */
\r
290 if (!PasteTextFromClipboard(&buf)) {
\r
293 PasteGameFromString( buf );
\r
296 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
\r
297 VOID PasteGameOrFENFromClipboard()
\r
301 Board dummyBoard; int dummy; // [HGM] paste any
\r
303 if (!PasteTextFromClipboard(&buf)) {
\r
307 // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead
\r
308 if(!ParseFEN(dummyBoard, &dummy, buf) ) {
\r
309 PasteGameFromString( buf );
\r
312 PasteFENFromString( buf );
\r
317 PasteTextFromClipboard(char **text)
\r
319 /* some (most?) of the error checking may be overkill,
\r
320 * but hey, this is Windows
\r
324 BOOL locked = FALSE;
\r
328 if (!OpenClipboard(hwndMain)) {
\r
329 DisplayError(_("Unable to open clipboard."), 0);
\r
332 hClipMem = GetClipboardData(CF_TEXT);
\r
333 if (hClipMem == NULL) {
\r
335 DisplayError(_("No text in clipboard."), 0);
\r
338 lpClipMem = GlobalLock(hClipMem);
\r
339 if (lpClipMem == NULL) {
\r
341 DisplayError(_(_("Unable to lock clipboard memory.")), 0);
\r
344 *text = (char *) malloc(GlobalSize(hClipMem)+1);
\r
346 DisplayError(_("Unable to allocate memory for text string."), 0);
\r
350 safeStrCpy(*text, lpClipMem, 1<<20 );
\r
351 if (appData.debugMode) {
\r
352 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
353 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
355 SetLastError(NO_ERROR);
\r
356 /*suggested by Wilkin Ng*/
\r
357 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
359 locked = GlobalUnlock(hClipMem);
\r
361 err = GetLastError();
\r
362 if (appData.debugMode) {
\r
363 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
364 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
367 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
368 if (appData.debugMode) {
\r
370 "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked);
\r
374 DisplayError(_("Unable to unlock clipboard memory."), 0);
\r
376 if (!CloseClipboard())
\r
377 DisplayError(_("Unable to close clipboard."), 0);
\r
383 DeleteClipboardTempFiles()
\r
385 if (copyTemp) remove(copyTemp);
\r
386 if (pasteTemp) remove(pasteTemp);
\r