cleaned up an old #ifdef in zippy
[xboard.git] / winboard / wclipbrd.c
index 66a3638..6f546a4 100644 (file)
@@ -1,25 +1,27 @@
 /*\r
  * wclipbrd.c -- Clipboard routines for WinBoard\r
- * $Id$\r
  *\r
- * Copyright 2000 Free Software Foundation, Inc.\r
+ * Copyright 2000,2009 Free 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
@@ -27,7 +29,6 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <malloc.h>\r
-#include <io.h>\r
 #include <sys/stat.h>\r
 \r
 #include "common.h"\r
@@ -38,6 +39,7 @@
 \r
 /* Imports from winboard.c */\r
 extern HWND hwndMain;\r
+Boolean ParseFEN(Board b, int *stm, char *FEN);\r
 \r
 /* File globals */\r
 static char *copyTemp;\r
@@ -48,7 +50,7 @@ CopyFENToClipboard()
 {\r
   char *fen = NULL;\r
 \r
-  fen = PositionToFEN(currentMove);\r
+  fen = PositionToFEN(currentMove, NULL);\r
   if (!fen) {\r
     DisplayError("Unable to convert position to FEN.", 0);\r
     return;\r
@@ -58,6 +60,38 @@ CopyFENToClipboard()
   free(fen);\r
 }\r
 \r
+/* [AS] */\r
+HGLOBAL ExportGameListAsText();\r
+\r
+VOID CopyGameListToClipboard()\r
+{\r
+    HGLOBAL hMem = ExportGameListAsText();\r
+    \r
+    if( hMem != NULL ) {\r
+        /* Assign memory block to clipboard */\r
+        BOOL ok = OpenClipboard( hwndMain );\r
+\r
+        if( ok ) {\r
+            ok = EmptyClipboard();\r
+\r
+            if( ok ) {\r
+                if( hMem != SetClipboardData( CF_TEXT, hMem ) ) {\r
+                    ok = FALSE;\r
+                }\r
+            }\r
+\r
+            CloseClipboard();\r
+\r
+            if( ! ok ) {\r
+                GlobalFree( hMem );\r
+            }\r
+        }\r
+\r
+        if( ! ok ) {\r
+            DisplayError( "Cannot copy list to clipboard.", 0 );\r
+        }\r
+    }\r
+}\r
 \r
 VOID\r
 CopyGameToClipboard()\r
@@ -72,13 +106,13 @@ CopyGameToClipboard()
   struct stat st;\r
 \r
   if (!copyTemp) {\r
-    copyTemp = tmpnam(NULL);\r
+    copyTemp = tempnam(NULL, "wbcp");\r
   }\r
   if (!copyTemp) {\r
       DisplayError("Cannot create temporary file name.",0);\r
       return;\r
   }\r
-  f = fopen(copyTemp, "w"); \r
+  f = fopen(copyTemp, "w");\r
   if (!f) {\r
     DisplayError("Cannot open temporary file.", 0);\r
     return;\r
@@ -166,7 +200,7 @@ 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
@@ -195,6 +229,16 @@ CopyTextToClipboard(char *text)
   return TRUE;\r
 }\r
 \r
+/* [AS] Reworked paste functions so they can work with strings too */\r
+\r
+VOID PasteFENFromString( char * fen )\r
+{\r
+  if (appData.debugMode) {\r
+    fprintf(debugFP, "PasteFenFromString(): fen '%s'\n", fen);\r
+  }\r
+  EditPositionPasteFEN(fen); /* call into backend */\r
+  free(fen);\r
+}\r
 \r
 \r
 VOID\r
@@ -205,43 +249,66 @@ PasteFENFromClipboard()
       DisplayError("Unable to paste FEN from clipboard.", 0);\r
       return;\r
   }\r
-  if (appData.debugMode) {\r
-    fprintf(debugFP, "PasteFenFromClipboard(): fen '%s'\n", fen);\r
-  }\r
-  EditPositionPasteFEN(fen); /* call into backend */\r
-  free(fen);\r
+  PasteFENFromString( fen );\r
 }\r
 \r
-\r
-VOID\r
-PasteGameFromClipboard()\r
+VOID PasteGameFromString( char * buf )\r
 {\r
-  /* Write the clipboard to a temp file, then let LoadGameFromFile()\r
-   * do all the work.  */\r
-  char *buf;\r
   FILE *f;\r
   size_t len;\r
-  if (!PasteTextFromClipboard(&buf)) {\r
-    return;\r
-  }\r
   if (!pasteTemp) {\r
-    pasteTemp = tmpnam(NULL);\r
+    pasteTemp = tempnam(NULL, "wbpt");\r
   }\r
-  f = fopen(pasteTemp, "wb+");\r
+  f = fopen(pasteTemp, "w");\r
   if (!f) {\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
+    free(buf); /* [AS] */\r
     return;\r
   }\r
   LoadGameFromFile(pasteTemp, 0, "Clipboard", TRUE);\r
+  free( buf ); /* [AS] */\r
 }\r
 \r
 \r
+VOID\r
+PasteGameFromClipboard()\r
+{\r
+  /* Write the clipboard to a temp file, then let LoadGameFromFile()\r
+   * do all the work.  */\r
+  char *buf;\r
+  if (!PasteTextFromClipboard(&buf)) {\r
+    return;\r
+  }\r
+  PasteGameFromString( buf );\r
+}\r
+\r
+/* [AS] Try to detect whether the clipboard contains FEN or PGN data */\r
+VOID PasteGameOrFENFromClipboard()\r
+{\r
+  char *buf;\r
+//  char *tmp;\r
+  Board dummyBoard; int dummy; // [HGM] paste any\r
+\r
+  if (!PasteTextFromClipboard(&buf)) {\r
+    return;\r
+  }\r
+\r
+  // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead\r
+  if(!ParseFEN(dummyBoard, &dummy, buf) ) {\r
+      PasteGameFromString( buf );\r
+  }\r
+  else {\r
+      PasteFENFromString( buf );\r
+  }\r
+}\r
+\r
 int \r
 PasteTextFromClipboard(char **text)\r
 {\r
@@ -282,15 +349,11 @@ PasteTextFromClipboard(char **text)
     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
@@ -300,7 +363,7 @@ 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