]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/gtkutil/clipboard.cpp
portability fixes
[xonotic/netradiant.git] / libs / gtkutil / clipboard.cpp
index cbace0bf857c10125f9b9dd174dee9bebb13cbaf..f7b9466ac143e93c2c10af4ff440d86bac95e69a 100644 (file)
@@ -28,7 +28,69 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 /// \file
 /// \brief Platform-independent GTK clipboard support.
 /// \todo Using GDK_SELECTION_CLIPBOARD fails on win32, so we use the win32 API directly for now.
-#if defined (__linux__) || defined (__APPLE__)
+#if defined(WIN32)
+
+const char* c_clipboard_format = "RadiantClippings";
+
+#include <windows.h>
+
+void clipboard_copy(ClipboardCopyFunc copy)
+{
+  BufferOutputStream ostream;
+  copy(ostream);
+
+  bool bClipped = false;
+  UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
+  if (nClipboard > 0)
+  {
+    if (::OpenClipboard(0))
+    {
+      EmptyClipboard();
+      std::size_t length = ostream.size();
+      HANDLE h = ::GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, length + sizeof(std::size_t));
+      if (h != 0)
+      {
+        char *buffer = reinterpret_cast<char*>(::GlobalLock(h));
+        *reinterpret_cast<std::size_t*>(buffer) = length;
+        buffer += sizeof(std::size_t);
+        memcpy(buffer, ostream.data(), length);
+        ::GlobalUnlock(h);
+        ::SetClipboardData(nClipboard, h);
+        ::CloseClipboard();
+        bClipped = true;
+      }
+    }
+  }
+
+  if (!bClipped)
+  {
+    globalOutputStream() << "Unable to register Windows clipboard formats, copy/paste between editors will not be possible\n";
+  }
+}
+
+void clipboard_paste(ClipboardPasteFunc paste)
+{
+  UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
+  if (nClipboard > 0 && ::OpenClipboard(0))
+  {
+    if(IsClipboardFormatAvailable(nClipboard))
+    {
+      HANDLE h = ::GetClipboardData(nClipboard);
+      if(h)
+      {
+        const char *buffer = reinterpret_cast<const char*>(::GlobalLock(h));
+        std::size_t length = *reinterpret_cast<const std::size_t*>(buffer);
+        buffer += sizeof(std::size_t);
+        BufferInputStream istream(buffer, length);
+        paste(istream);
+        ::GlobalUnlock(h);
+      }
+    }
+    ::CloseClipboard();
+  }
+}
+
+#else
 
 #include <gtk/gtkclipboard.h>
 
@@ -96,67 +158,5 @@ void clipboard_paste(ClipboardPasteFunc paste)
   gtk_clipboard_request_contents (clipboard, gdk_atom_intern(clipboard_targets[0].target, FALSE), clipboard_received, &g_clipboardPasteFunc);
 }
 
-#elif defined(WIN32)
-
-const char* c_clipboard_format = "RadiantClippings";
-
-#include <windows.h>
-
-void clipboard_copy(ClipboardCopyFunc copy)
-{
-  BufferOutputStream ostream;
-  copy(ostream);
-
-  bool bClipped = false;
-  UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
-  if (nClipboard > 0)
-  {
-    if (::OpenClipboard(0))
-    {
-      EmptyClipboard();
-      std::size_t length = ostream.size();
-      HANDLE h = ::GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, length + sizeof(std::size_t));
-      if (h != 0)
-      {
-        char *buffer = reinterpret_cast<char*>(::GlobalLock(h));
-        *reinterpret_cast<std::size_t*>(buffer) = length;
-        buffer += sizeof(std::size_t);
-        memcpy(buffer, ostream.data(), length);
-        ::GlobalUnlock(h);
-        ::SetClipboardData(nClipboard, h);
-        ::CloseClipboard();
-        bClipped = true;
-      }
-    }
-  }
-
-  if (!bClipped)
-  {
-    globalOutputStream() << "Unable to register Windows clipboard formats, copy/paste between editors will not be possible\n";
-  }
-}
-
-void clipboard_paste(ClipboardPasteFunc paste)
-{
-  UINT nClipboard = ::RegisterClipboardFormat(c_clipboard_format);
-  if (nClipboard > 0 && ::OpenClipboard(0))
-  {
-    if(IsClipboardFormatAvailable(nClipboard))
-    {
-      HANDLE h = ::GetClipboardData(nClipboard);
-      if(h)
-      {
-        const char *buffer = reinterpret_cast<const char*>(::GlobalLock(h));
-        std::size_t length = *reinterpret_cast<const std::size_t*>(buffer);
-        buffer += sizeof(std::size_t);
-        BufferInputStream istream(buffer, length);
-        paste(istream);
-        ::GlobalUnlock(h);
-      }
-    }
-    ::CloseClipboard();
-  }
-}
-
 
 #endif