X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=winboard%2Fwclipbrd.c;h=700ccde0172cdd334b54f68ca479dd5c2ec67346;hb=HEAD;hp=66a3638102725434c07a0be2ad1cd7b8fd4b1091;hpb=05bc30b15e31c427ce208495a889e9ff36e6642b;p=xboard.git diff --git a/winboard/wclipbrd.c b/winboard/wclipbrd.c index 66a3638..700ccde 100644 --- a/winboard/wclipbrd.c +++ b/winboard/wclipbrd.c @@ -1,25 +1,28 @@ /* * wclipbrd.c -- Clipboard routines for WinBoard - * $Id$ * - * Copyright 2000 Free Software Foundation, Inc. + * Copyright 2000, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free + * Software Foundation, Inc. + * + * Enhancements Copyright 2005 Alessandro Scotti * * ------------------------------------------------------------------------ - * This program is free software; you can redistribute it and/or modify + * + * GNU XBoard is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU XBoard is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * ------------------------------------------------------------------------ - */ + * along with this program. If not, see http://www.gnu.org/licenses/. * + * + *------------------------------------------------------------------------ + ** See the file ChangeLog for a revision history. */ #include "config.h" @@ -27,15 +30,17 @@ #include #include #include -#include #include #include "common.h" -#include "winboard.h" #include "frontend.h" #include "backend.h" +#include "winboard.h" #include "wclipbrd.h" +#define _(s) T_(s) +#define N_(s) s + /* Imports from winboard.c */ extern HWND hwndMain; @@ -48,16 +53,49 @@ CopyFENToClipboard() { char *fen = NULL; - fen = PositionToFEN(currentMove); + if(gameMode == EditPosition) EditPositionDone(TRUE); // [HGM] mak sure castling rights are set consistently + fen = PositionToFEN(currentMove, NULL, 1); if (!fen) { - DisplayError("Unable to convert position to FEN.", 0); + DisplayError(_("Unable to convert position to FEN."), 0); return; } if (!CopyTextToClipboard(fen)) - DisplayError("Unable to copy FEN to clipboard.", 0); + DisplayError(_("Unable to copy FEN to clipboard."), 0); free(fen); } +/* [AS] */ +HGLOBAL ExportGameListAsText(); + +VOID CopyGameListToClipboard() +{ + HGLOBAL hMem = ExportGameListAsText(); + + if( hMem != NULL ) { + /* Assign memory block to clipboard */ + BOOL ok = OpenClipboard( hwndMain ); + + if( ok ) { + ok = EmptyClipboard(); + + if( ok ) { + if( hMem != SetClipboardData( CF_TEXT, hMem ) ) { + ok = FALSE; + } + } + + CloseClipboard(); + + if( ! ok ) { + GlobalFree( hMem ); + } + } + + if( ! ok ) { + DisplayError( "Cannot copy list to clipboard.", 0 ); + } + } +} VOID CopyGameToClipboard() @@ -72,53 +110,53 @@ CopyGameToClipboard() struct stat st; if (!copyTemp) { - copyTemp = tmpnam(NULL); + copyTemp = tempnam(NULL, "wbcp"); } if (!copyTemp) { - DisplayError("Cannot create temporary file name.",0); + DisplayError(_("Cannot create temporary file name."),0); return; } - f = fopen(copyTemp, "w"); + f = fopen(copyTemp, "w"); if (!f) { - DisplayError("Cannot open temporary file.", 0); + DisplayError(_("Cannot open temporary file."), 0); return; } if (!SaveGame(f,0,"")) { /* call into backend */ - DisplayError("Cannot write to temporary file.", 0); + DisplayError(_("Cannot write to temporary file."), 0); goto copy_game_to_clipboard_cleanup; } f = fopen(copyTemp, "rb"); if (!f) { - DisplayError("Cannot reopen temporary file.", 0); + DisplayError(_("Cannot reopen temporary file."), 0); goto copy_game_to_clipboard_cleanup; } if (fstat(fileno(f), &st) < 0) { - DisplayError("Cannot determine size of file.", 0); + DisplayError(_(_("Cannot determine size of file.")), 0); goto copy_game_to_clipboard_cleanup; } size = st.st_size; if (size == -1) { - DisplayError("Cannot determine size of file.", 0); + DisplayError(_(_("Cannot determine size of file.")), 0); goto copy_game_to_clipboard_cleanup; } rewind(f); buf = (char*)malloc(size+1); if (!buf) { - DisplayError("Cannot allocate clipboard buffer.", 0); + DisplayError(_("Cannot allocate clipboard buffer."), 0); goto copy_game_to_clipboard_cleanup; } len = fread(buf, sizeof(char), size, f); if (len == -1) { - DisplayError("Cannot read from temporary file.", 0); + DisplayError(_("Cannot read from temporary file."), 0); goto copy_game_to_clipboard_cleanup; } if ((unsigned long)size != (unsigned long)len) { /* sigh */ - DisplayError("Error reading from temporary file.", 0); + DisplayError(_("Error reading from temporary file."), 0); goto copy_game_to_clipboard_cleanup; } buf[size] = 0; if (!CopyTextToClipboard(buf)) { - DisplayError("Cannot copy text to clipboard", 0); + DisplayError(_("Cannot copy text to clipboard"), 0); } copy_game_to_clipboard_cleanup: @@ -141,16 +179,16 @@ CopyTextToClipboard(char *text) hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1); if (hGlobalMem == NULL) { - DisplayError("Unable to allocate memory for clipboard.", 0); + DisplayError(_("Unable to allocate memory for clipboard."), 0); return FALSE; } lpGlobalMem = GlobalLock(hGlobalMem); if (lpGlobalMem == NULL) { - DisplayError("Unable to lock clipboard memory.", 0); + DisplayError(_(_("Unable to lock clipboard memory.")), 0); GlobalFree(hGlobalMem); return FALSE; } - lstrcpy(lpGlobalMem, text); + safeStrCpy(lpGlobalMem, text, 1<<20); if (appData.debugMode) { lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT; fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount); @@ -166,35 +204,45 @@ CopyTextToClipboard(char *text) locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED)); if (appData.debugMode) { fprintf(debugFP, - "CopyTextToClipboard(): err %d locked %d\n", err, locked); + "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked); } } if (locked) { - DisplayError("Cannot unlock clipboard memory.", 0); + DisplayError(_("Cannot unlock clipboard memory."), 0); GlobalFree(hGlobalMem); return FALSE; } if (!OpenClipboard(hwndMain)) { - DisplayError("Cannot open clipboard.", 0); + DisplayError(_("Cannot open clipboard."), 0); GlobalFree(hGlobalMem); return FALSE; } if (!EmptyClipboard()) { - DisplayError("Cannot empty clipboard.", 0); + DisplayError(_("Cannot empty clipboard."), 0); return FALSE; } if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) { - DisplayError("Cannot copy text to clipboard.", 0); + DisplayError(_("Cannot copy text to clipboard."), 0); CloseClipboard(); GlobalFree(hGlobalMem); return FALSE; } if (!CloseClipboard()) - DisplayError("Cannot close clipboard.", 0); + DisplayError(_("Cannot close clipboard."), 0); return TRUE; } +/* [AS] Reworked paste functions so they can work with strings too */ + +VOID PasteFENFromString( char * fen ) +{ + if (appData.debugMode) { + fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen); + } + EditPositionPasteFEN(fen); /* call into backend */ + free(fen); +} VOID @@ -202,46 +250,73 @@ PasteFENFromClipboard() { char *fen = NULL; if (!PasteTextFromClipboard(&fen)) { - DisplayError("Unable to paste FEN from clipboard.", 0); + DisplayError(_("Unable to paste FEN from clipboard."), 0); return; } - if (appData.debugMode) { - fprintf(debugFP, "PasteFenFromClipboard(): fen '%s'\n", fen); - } - EditPositionPasteFEN(fen); /* call into backend */ - free(fen); + PasteFENFromString( fen ); } - -VOID -PasteGameFromClipboard() +VOID PasteGameFromString( char * buf ) { - /* Write the clipboard to a temp file, then let LoadGameFromFile() - * do all the work. */ - char *buf; FILE *f; size_t len; - if (!PasteTextFromClipboard(&buf)) { - return; - } + int flip = appData.flipView; if (!pasteTemp) { - pasteTemp = tmpnam(NULL); + pasteTemp = tempnam(NULL, "wbpt"); } - f = fopen(pasteTemp, "wb+"); + f = fopen(pasteTemp, "w"); if (!f) { - DisplayError("Unable to create temporary file.", 0); + DisplayError(_("Unable to create temporary file."), 0); + free(buf); /* [AS] */ return; } len = fwrite(buf, sizeof(char), strlen(buf), f); fclose(f); if (len != strlen(buf)) { - DisplayError("Error writing to temporary file.", 0); + DisplayError(_("Error writing to temporary file."), 0); + free(buf); /* [AS] */ return; } + if(!appData.autoFlipView) appData.flipView = flipView; LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE); + appData.flipView = flip; + free( buf ); /* [AS] */ } +VOID +PasteGameFromClipboard() +{ + /* Write the clipboard to a temp file, then let LoadGameFromFile() + * do all the work. */ + char *buf; + if (!PasteTextFromClipboard(&buf)) { + return; + } + PasteGameFromString( buf ); +} + +/* [AS] Try to detect whether the clipboard contains FEN or PGN data */ +VOID PasteGameOrFENFromClipboard() +{ + char *buf; +// char *tmp; + Board dummyBoard; int dummy; // [HGM] paste any + + if (!PasteTextFromClipboard(&buf)) { + + return; + } + + // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead + if(!ParseFEN(dummyBoard, &dummy, buf, 0) ) { + PasteGameFromString( buf ); + } + else { + PasteFENFromString( buf ); + } +} + int PasteTextFromClipboard(char **text) { @@ -255,42 +330,38 @@ PasteTextFromClipboard(char **text) UINT lockCount; if (!OpenClipboard(hwndMain)) { - DisplayError("Unable to open clipboard.", 0); + DisplayError(_("Unable to open clipboard."), 0); return FALSE; } hClipMem = GetClipboardData(CF_TEXT); if (hClipMem == NULL) { CloseClipboard(); - DisplayError("No text in clipboard.", 0); + DisplayError(_("No text in clipboard."), 0); return FALSE; } lpClipMem = GlobalLock(hClipMem); if (lpClipMem == NULL) { CloseClipboard(); - DisplayError("Unable to lock clipboard memory.", 0); + DisplayError(_(_("Unable to lock clipboard memory.")), 0); return FALSE; } *text = (char *) malloc(GlobalSize(hClipMem)+1); if (!*text) { - DisplayError("Unable to allocate memory for text string.", 0); + DisplayError(_("Unable to allocate memory for text string."), 0); CloseClipboard(); return FALSE; } - lstrcpy(*text, lpClipMem); + safeStrCpy(*text, lpClipMem, 1<<20 ); if (appData.debugMode) { lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT; fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount); } SetLastError(NO_ERROR); -#if 1 /*suggested by Wilkin Ng*/ lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT; if (lockCount) { locked = GlobalUnlock(hClipMem); } -#else - locked = GlobalUnlock(hClipMem); -#endif err = GetLastError(); if (appData.debugMode) { lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT; @@ -300,14 +371,14 @@ PasteTextFromClipboard(char **text) locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED)); if (appData.debugMode) { fprintf(debugFP, - "PasteTextFromClipboard(): err %d locked %d\n", err, locked); + "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked); } } if (locked) - DisplayError("Unable to unlock clipboard memory.", 0); + DisplayError(_("Unable to unlock clipboard memory."), 0); if (!CloseClipboard()) - DisplayError("Unable to close clipboard.", 0); + DisplayError(_("Unable to close clipboard."), 0); return TRUE; }