2 * wclipbrd.c -- Clipboard routines for WinBoard
\r
3 * $Id: wclipbrd.c,v 2.1 2003/10/27 19:21:02 mann Exp $
5 * Copyright 2000 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program; if not, write to the Free Software
\r
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
21 * ------------------------------------------------------------------------
\r
26 #include <windows.h> /* required for all Windows applications */
\r
30 #include <sys/stat.h>
\r
33 #include "winboard.h"
\r
34 #include "frontend.h"
\r
35 #include "backend.h"
\r
36 #include "wclipbrd.h"
\r
38 /* Imports from winboard.c */
\r
39 extern HWND hwndMain;
\r
42 static char *copyTemp;
\r
43 static char *pasteTemp;
\r
46 CopyFENToClipboard()
\r
50 fen = PositionToFEN(currentMove);
\r
52 DisplayError("Unable to convert position to FEN.", 0);
\r
55 if (!CopyTextToClipboard(fen))
\r
56 DisplayError("Unable to copy FEN to clipboard.", 0);
\r
62 CopyGameToClipboard()
\r
64 /* A rather cheesy hack here. Write the game to a file, then read from the
\r
65 * file into the clipboard.
\r
74 copyTemp = tempnam(NULL, "wbcp");
\r
77 DisplayError("Cannot create temporary file name.",0);
\r
80 f = fopen(copyTemp, "w");
\r
82 DisplayError("Cannot open temporary file.", 0);
\r
85 if (!SaveGame(f,0,"")) { /* call into backend */
\r
86 DisplayError("Cannot write to temporary file.", 0);
\r
87 goto copy_game_to_clipboard_cleanup;
\r
89 f = fopen(copyTemp, "rb");
\r
91 DisplayError("Cannot reopen temporary file.", 0);
\r
92 goto copy_game_to_clipboard_cleanup;
\r
94 if (fstat(fileno(f), &st) < 0) {
\r
95 DisplayError("Cannot determine size of file.", 0);
\r
96 goto copy_game_to_clipboard_cleanup;
\r
100 DisplayError("Cannot determine size of file.", 0);
\r
101 goto copy_game_to_clipboard_cleanup;
\r
104 buf = (char*)malloc(size+1);
\r
106 DisplayError("Cannot allocate clipboard buffer.", 0);
\r
107 goto copy_game_to_clipboard_cleanup;
\r
109 len = fread(buf, sizeof(char), size, f);
\r
111 DisplayError("Cannot read from temporary file.", 0);
\r
112 goto copy_game_to_clipboard_cleanup;
\r
114 if ((unsigned long)size != (unsigned long)len) { /* sigh */
\r
115 DisplayError("Error reading from temporary file.", 0);
\r
116 goto copy_game_to_clipboard_cleanup;
\r
119 if (!CopyTextToClipboard(buf)) {
\r
120 DisplayError("Cannot copy text to clipboard", 0);
\r
123 copy_game_to_clipboard_cleanup:
\r
124 if (buf) free(buf);
\r
130 CopyTextToClipboard(char *text)
\r
132 /* some (most?) of the error checking may be overkill,
\r
133 * but hey, this is Windows
\r
135 HGLOBAL hGlobalMem;
\r
136 LPVOID lpGlobalMem;
\r
141 hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);
\r
142 if (hGlobalMem == NULL) {
\r
143 DisplayError("Unable to allocate memory for clipboard.", 0);
\r
146 lpGlobalMem = GlobalLock(hGlobalMem);
\r
147 if (lpGlobalMem == NULL) {
\r
148 DisplayError("Unable to lock clipboard memory.", 0);
\r
149 GlobalFree(hGlobalMem);
\r
152 lstrcpy(lpGlobalMem, text);
\r
153 if (appData.debugMode) {
\r
154 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
155 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
157 SetLastError(NO_ERROR);
\r
158 locked = GlobalUnlock(hGlobalMem);
\r
159 err = GetLastError();
\r
160 if (appData.debugMode) {
\r
161 lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;
\r
162 fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);
\r
165 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
166 if (appData.debugMode) {
\r
168 "CopyTextToClipboard(): err %d locked %d\n", err, locked);
\r
172 DisplayError("Cannot unlock clipboard memory.", 0);
\r
173 GlobalFree(hGlobalMem);
\r
176 if (!OpenClipboard(hwndMain)) {
\r
177 DisplayError("Cannot open clipboard.", 0);
\r
178 GlobalFree(hGlobalMem);
\r
181 if (!EmptyClipboard()) {
\r
182 DisplayError("Cannot empty clipboard.", 0);
\r
185 if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {
\r
186 DisplayError("Cannot copy text to clipboard.", 0);
\r
188 GlobalFree(hGlobalMem);
\r
191 if (!CloseClipboard())
\r
192 DisplayError("Cannot close clipboard.", 0);
\r
197 /* [AS] Reworked paste functions so they can work with strings too */
199 VOID PasteFENFromString( char * fen )
201 if (appData.debugMode) {
202 fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);
204 EditPositionPasteFEN(fen); /* call into backend */
210 PasteFENFromClipboard()
\r
213 if (!PasteTextFromClipboard(&fen)) {
\r
214 DisplayError("Unable to paste FEN from clipboard.", 0);
\r
217 PasteFENFromString( fen );
220 VOID PasteGameFromString( char * buf )
225 pasteTemp = tempnam(NULL, "wbpt");
\r
227 f = fopen(pasteTemp, "w");
\r
229 DisplayError("Unable to create temporary file.", 0);
\r
230 free(buf); /* [AS] */
233 len = fwrite(buf, sizeof(char), strlen(buf), f);
\r
235 if (len != strlen(buf)) {
\r
236 DisplayError("Error writing to temporary file.", 0);
\r
237 free(buf); /* [AS] */
240 LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);
\r
241 free( buf ); /* [AS] */
246 PasteGameFromClipboard()
248 /* Write the clipboard to a temp file, then let LoadGameFromFile()
249 * do all the work. */
251 if (!PasteTextFromClipboard(&buf)) {
254 PasteGameFromString( buf );
257 /* [AS] Try to detect whether the clipboard contains FEN or PGN data */
258 VOID PasteGameOrFENFromClipboard()
263 if (!PasteTextFromClipboard(&buf)) {
268 while( *tmp == ' ' || *tmp == '\t' || *tmp == '\r' || *tmp == '\n' ) {
273 PasteGameFromString( buf );
276 PasteFENFromString( buf );
281 PasteTextFromClipboard(char **text)
\r
283 /* some (most?) of the error checking may be overkill,
\r
284 * but hey, this is Windows
\r
288 BOOL locked = FALSE;
\r
292 if (!OpenClipboard(hwndMain)) {
\r
293 DisplayError("Unable to open clipboard.", 0);
\r
296 hClipMem = GetClipboardData(CF_TEXT);
\r
297 if (hClipMem == NULL) {
\r
299 DisplayError("No text in clipboard.", 0);
\r
302 lpClipMem = GlobalLock(hClipMem);
\r
303 if (lpClipMem == NULL) {
\r
305 DisplayError("Unable to lock clipboard memory.", 0);
\r
308 *text = (char *) malloc(GlobalSize(hClipMem)+1);
\r
310 DisplayError("Unable to allocate memory for text string.", 0);
\r
314 lstrcpy(*text, lpClipMem);
\r
315 if (appData.debugMode) {
\r
316 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
317 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
319 SetLastError(NO_ERROR);
\r
321 /*suggested by Wilkin Ng*/
\r
322 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
324 locked = GlobalUnlock(hClipMem);
\r
327 locked = GlobalUnlock(hClipMem);
\r
329 err = GetLastError();
\r
330 if (appData.debugMode) {
\r
331 lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;
\r
332 fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);
\r
335 locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));
\r
336 if (appData.debugMode) {
\r
338 "PasteTextFromClipboard(): err %d locked %d\n", err, locked);
\r
342 DisplayError("Unable to unlock clipboard memory.", 0);
\r
344 if (!CloseClipboard())
\r
345 DisplayError("Unable to close clipboard.", 0);
\r
351 DeleteClipboardTempFiles()
\r
353 if (copyTemp) remove(copyTemp);
\r
354 if (pasteTemp) remove(pasteTemp);
\r