Fix multi-leg promotions
[xboard.git] / winboard / wclipbrd.c
index 55b9dfd..700ccde 100644 (file)
@@ -1,25 +1,28 @@
 /*\r
  * wclipbrd.c -- Clipboard routines for WinBoard\r
- * $Id: wclipbrd.c,v 2.1 2003/10/27 19:21:02 mann Exp $\r
  *\r
- * Copyright 2000 Free Software Foundation, Inc.\r
+ * Copyright 2000, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free\r
+ * Software Foundation, Inc.\r
+ *\r
+ * Enhancements Copyright 2005 Alessandro Scotti\r
  *\r
  * ------------------------------------------------------------------------\r
- * This program is free software; you can redistribute it and/or modify\r
+ *\r
+ * GNU XBoard is free software: you can redistribute it and/or modify\r
  * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
+ * the Free Software Foundation, either version 3 of the License, or (at\r
+ * your option) any later version.\r
  *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
+ * GNU XBoard is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details.\r
  *\r
  * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
- * ------------------------------------------------------------------------\r
- */\r
+ * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
+ *\r
+ *------------------------------------------------------------------------\r
+ ** See the file ChangeLog for a revision history.  */\r
 \r
 #include "config.h"\r
 \r
 #include <sys/stat.h>\r
 \r
 #include "common.h"\r
-#include "winboard.h"\r
 #include "frontend.h"\r
 #include "backend.h"\r
+#include "winboard.h"\r
 #include "wclipbrd.h"\r
 \r
+#define _(s) T_(s)\r
+#define N_(s) s\r
+\r
 /* Imports from winboard.c */\r
 extern HWND hwndMain;\r
 \r
@@ -47,13 +53,14 @@ CopyFENToClipboard()
 {\r
   char *fen = NULL;\r
 \r
-  fen = PositionToFEN(currentMove,1);\r
+  if(gameMode == EditPosition) EditPositionDone(TRUE); // [HGM] mak sure castling rights are set consistently\r
+  fen = PositionToFEN(currentMove, NULL, 1);\r
   if (!fen) {\r
-    DisplayError("Unable to convert position to FEN.", 0);\r
+    DisplayError(_("Unable to convert position to FEN."), 0);\r
     return;\r
   }\r
   if (!CopyTextToClipboard(fen))\r
-      DisplayError("Unable to copy FEN to clipboard.", 0);\r
+      DisplayError(_("Unable to copy FEN to clipboard."), 0);\r
   free(fen);\r
 }\r
 \r
@@ -106,50 +113,50 @@ CopyGameToClipboard()
     copyTemp = tempnam(NULL, "wbcp");\r
   }\r
   if (!copyTemp) {\r
-      DisplayError("Cannot create temporary file name.",0);\r
+      DisplayError(_("Cannot create temporary file name."),0);\r
       return;\r
   }\r
   f = fopen(copyTemp, "w");\r
   if (!f) {\r
-    DisplayError("Cannot open temporary file.", 0);\r
+    DisplayError(_("Cannot open temporary file."), 0);\r
     return;\r
   }\r
   if (!SaveGame(f,0,"")) {                     /* call into backend */\r
-    DisplayError("Cannot write to temporary file.", 0);\r
+    DisplayError(_("Cannot write to temporary file."), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   f = fopen(copyTemp, "rb");\r
   if (!f) {\r
-    DisplayError("Cannot reopen temporary file.", 0);\r
+    DisplayError(_("Cannot reopen temporary file."), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   if (fstat(fileno(f), &st) < 0) {\r
-    DisplayError("Cannot determine size of file.", 0);\r
+    DisplayError(_(_("Cannot determine size of file.")), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   size = st.st_size;\r
   if (size == -1) {\r
-    DisplayError("Cannot determine size of file.", 0);\r
+    DisplayError(_(_("Cannot determine size of file.")), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   rewind(f);\r
   buf = (char*)malloc(size+1);\r
   if (!buf) {\r
-    DisplayError("Cannot allocate clipboard buffer.", 0);\r
+    DisplayError(_("Cannot allocate clipboard buffer."), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   len = fread(buf, sizeof(char), size, f);\r
   if (len == -1) {\r
-    DisplayError("Cannot read from temporary file.", 0);\r
+    DisplayError(_("Cannot read from temporary file."), 0);\r
     goto copy_game_to_clipboard_cleanup;\r
   }\r
   if ((unsigned long)size != (unsigned long)len) { /* sigh */ \r
-      DisplayError("Error reading from temporary file.", 0);\r
+      DisplayError(_("Error reading from temporary file."), 0);\r
       goto copy_game_to_clipboard_cleanup;\r
   }\r
   buf[size] = 0;\r
   if (!CopyTextToClipboard(buf)) {\r
-      DisplayError("Cannot copy text to clipboard", 0);\r
+      DisplayError(_("Cannot copy text to clipboard"), 0);\r
   }\r
 \r
 copy_game_to_clipboard_cleanup:\r
@@ -172,16 +179,16 @@ CopyTextToClipboard(char *text)
 \r
   hGlobalMem = GlobalAlloc(GHND, (DWORD)lstrlen(text)+1);\r
   if (hGlobalMem == NULL) {\r
-    DisplayError("Unable to allocate memory for clipboard.", 0);\r
+    DisplayError(_("Unable to allocate memory for clipboard."), 0);\r
     return FALSE;\r
   }\r
   lpGlobalMem = GlobalLock(hGlobalMem);\r
   if (lpGlobalMem == NULL) {\r
-    DisplayError("Unable to lock clipboard memory.", 0);\r
+    DisplayError(_(_("Unable to lock clipboard memory.")), 0);\r
     GlobalFree(hGlobalMem);\r
     return FALSE;\r
   }\r
-  lstrcpy(lpGlobalMem, text);\r
+  safeStrCpy(lpGlobalMem, text, 1<<20);\r
   if (appData.debugMode) {\r
     lockCount = GlobalFlags(hGlobalMem) & GMEM_LOCKCOUNT;\r
     fprintf(debugFP, "CopyTextToClipboard(): lock count %d\n", lockCount);\r
@@ -197,31 +204,31 @@ CopyTextToClipboard(char *text)
     locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));\r
     if (appData.debugMode) {\r
       fprintf(debugFP, \r
-             "CopyTextToClipboard(): err %d locked %d\n", err, locked);\r
+             "CopyTextToClipboard(): err %d locked %d\n", (int)err, locked);\r
     }\r
   }\r
   if (locked) {\r
-    DisplayError("Cannot unlock clipboard memory.", 0);\r
+    DisplayError(_("Cannot unlock clipboard memory."), 0);\r
     GlobalFree(hGlobalMem);\r
     return FALSE;\r
   }\r
   if (!OpenClipboard(hwndMain)) {\r
-    DisplayError("Cannot open clipboard.", 0);\r
+    DisplayError(_("Cannot open clipboard."), 0);\r
     GlobalFree(hGlobalMem);\r
     return FALSE;\r
   }\r
   if (!EmptyClipboard()) {\r
-    DisplayError("Cannot empty clipboard.", 0);\r
+    DisplayError(_("Cannot empty clipboard."), 0);\r
     return FALSE;\r
   }\r
   if (hGlobalMem != SetClipboardData(CF_TEXT, hGlobalMem)) {\r
-    DisplayError("Cannot copy text to clipboard.", 0);\r
+    DisplayError(_("Cannot copy text to clipboard."), 0);\r
     CloseClipboard();\r
     GlobalFree(hGlobalMem);\r
     return FALSE;\r
   }\r
   if (!CloseClipboard())\r
-    DisplayError("Cannot close clipboard.", 0);\r
+    DisplayError(_("Cannot close clipboard."), 0);\r
   \r
   return TRUE;\r
 }\r
@@ -243,7 +250,7 @@ PasteFENFromClipboard()
 {\r
   char *fen = NULL;\r
   if (!PasteTextFromClipboard(&fen)) {\r
-      DisplayError("Unable to paste FEN from clipboard.", 0);\r
+      DisplayError(_("Unable to paste FEN from clipboard."), 0);\r
       return;\r
   }\r
   PasteFENFromString( fen );\r
@@ -253,23 +260,26 @@ VOID PasteGameFromString( char * buf )
 {\r
   FILE *f;\r
   size_t len;\r
+  int flip = appData.flipView;\r
   if (!pasteTemp) {\r
     pasteTemp = tempnam(NULL, "wbpt");\r
   }\r
   f = fopen(pasteTemp, "w");\r
   if (!f) {\r
-    DisplayError("Unable to create temporary file.", 0);\r
+    DisplayError(_("Unable to create temporary file."), 0);\r
     free(buf); /* [AS] */\r
     return;\r
   }\r
   len = fwrite(buf, sizeof(char), strlen(buf), f);\r
   fclose(f);\r
   if (len != strlen(buf)) {\r
-    DisplayError("Error writing to temporary file.", 0);\r
+    DisplayError(_("Error writing to temporary file."), 0);\r
     free(buf); /* [AS] */\r
     return;\r
   }\r
+  if(!appData.autoFlipView) appData.flipView = flipView;\r
   LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);\r
+  appData.flipView = flip;\r
   free( buf ); /* [AS] */\r
 }\r
 \r
@@ -290,18 +300,16 @@ PasteGameFromClipboard()
 VOID PasteGameOrFENFromClipboard()\r
 {\r
   char *buf;\r
-  char *tmp;\r
+//  char *tmp;\r
+  Board dummyBoard; int dummy; // [HGM] paste any\r
 \r
   if (!PasteTextFromClipboard(&buf)) {\r
-    return;\r
-  }\r
 \r
-  tmp = buf;\r
-  while( *tmp == ' ' || *tmp == '\t' || *tmp == '\r' || *tmp == '\n' ) {\r
-      tmp++;\r
+    return;\r
   }\r
 \r
-  if( *tmp == '[' ) {\r
+  // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead\r
+  if(!ParseFEN(dummyBoard, &dummy, buf, 0) ) {\r
       PasteGameFromString( buf );\r
   }\r
   else {\r
@@ -322,42 +330,38 @@ PasteTextFromClipboard(char **text)
   UINT lockCount;\r
 \r
   if (!OpenClipboard(hwndMain)) {\r
-    DisplayError("Unable to open clipboard.", 0);\r
+    DisplayError(_("Unable to open clipboard."), 0);\r
     return FALSE;\r
   }\r
   hClipMem = GetClipboardData(CF_TEXT);\r
   if (hClipMem == NULL) {\r
     CloseClipboard();\r
-    DisplayError("No text in clipboard.", 0);\r
+    DisplayError(_("No text in clipboard."), 0);\r
     return FALSE;\r
   }\r
   lpClipMem = GlobalLock(hClipMem);\r
   if (lpClipMem == NULL) {\r
     CloseClipboard();\r
-    DisplayError("Unable to lock clipboard memory.", 0);\r
+    DisplayError(_(_("Unable to lock clipboard memory.")), 0);\r
     return FALSE;\r
   }\r
   *text = (char *) malloc(GlobalSize(hClipMem)+1);\r
   if (!*text) {\r
-    DisplayError("Unable to allocate memory for text string.", 0);\r
+    DisplayError(_("Unable to allocate memory for text string."), 0);\r
     CloseClipboard();\r
     return FALSE;\r
   }\r
-  lstrcpy(*text, lpClipMem);\r
+  safeStrCpy(*text, lpClipMem, 1<<20 );\r
   if (appData.debugMode) {\r
     lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;\r
     fprintf(debugFP, "PasteTextFromClipboard(): lock count %d\n", lockCount);\r
   }\r
   SetLastError(NO_ERROR);\r
-#if 1\r
   /*suggested by Wilkin Ng*/\r
   lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;\r
   if (lockCount) {\r
     locked = GlobalUnlock(hClipMem);\r
   }\r
-#else\r
-  locked = GlobalUnlock(hClipMem);\r
-#endif\r
   err = GetLastError();\r
   if (appData.debugMode) {\r
     lockCount = GlobalFlags(hClipMem) & GMEM_LOCKCOUNT;\r
@@ -367,14 +371,14 @@ PasteTextFromClipboard(char **text)
     locked = !((err == NO_ERROR) || (err == ERROR_NOT_LOCKED));\r
     if (appData.debugMode) {\r
       fprintf(debugFP, \r
-             "PasteTextFromClipboard(): err %d locked %d\n", err, locked);\r
+             "PasteTextFromClipboard(): err %d locked %d\n", (int)err, locked);\r
     }\r
   }\r
   if (locked) \r
-    DisplayError("Unable to unlock clipboard memory.", 0);\r
+    DisplayError(_("Unable to unlock clipboard memory."), 0);\r
   \r
   if (!CloseClipboard())\r
-    DisplayError("Unable to close clipboard.", 0);\r
+    DisplayError(_("Unable to close clipboard."), 0);\r
   \r
   return TRUE;\r
 }\r