diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-08-24 20:13:14 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-09-07 03:13:54 +0300 |
commit | 3f84fc198131ff706e18c56f612e38ff147b0005 (patch) | |
tree | a723a9a9163498cd880bdc23c9752d0b4e9373ca /src/utils | |
parent | 64f4e1132355eaac4f5594bee516b56adb2ad083 (diff) | |
download | plus-3f84fc198131ff706e18c56f612e38ff147b0005.tar.gz plus-3f84fc198131ff706e18c56f612e38ff147b0005.tar.bz2 plus-3f84fc198131ff706e18c56f612e38ff147b0005.tar.xz plus-3f84fc198131ff706e18c56f612e38ff147b0005.zip |
Add support for copy text to clipboard (Ctrl+C).
In X enviroments used xsel to copy string to clipboard.
In windows used native clipboard api.
MacOSX not supported.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/copynpaste.cpp | 87 | ||||
-rw-r--r-- | src/utils/copynpaste.h | 1 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/utils/copynpaste.cpp b/src/utils/copynpaste.cpp index 5b1ccb5bc..6d1c675d3 100644 --- a/src/utils/copynpaste.cpp +++ b/src/utils/copynpaste.cpp @@ -1,6 +1,7 @@ /* * Retrieve string pasted depending on OS mechanisms. * Copyright (C) 2001-2010 Wormux Team + * Copyright (C) 2011 ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -90,6 +91,40 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos) CloseClipboard(); return ret; } + +bool sendBuffer(std::string& text) +{ + int wCharsLen = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, NULL, 0); + if (!wCharsLen) + return false; + + HANDLE h; + WCHAR *out; + h = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, wCharsLen * sizeof(WCHAR)); + out = (WCHAR*)GlobalLock(h); + + MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, out, wCharsLen); + + if (!OpenClipboard(0)) + { + GlobalUnlock(h); + GlobalFree(h); + return false; + } + GlobalUnlock(h); + EmptyClipboard(); + if (!SetClipboardData(CF_UNICODETEXT, out)) + { + GlobalFree(h); + CloseClipboard(); + return false; + } + GlobalFree(h); + CloseClipboard(); + + return true; +} + #elif defined(__APPLE__) #ifdef Status @@ -243,6 +278,11 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos) } } +bool sendBuffer(std::string& text) +{ + return false; +} + #elif USE_X11 static char* getSelection2(Display *dpy, Window us, Atom selection, Atom request_target) @@ -362,9 +402,56 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos) } return false; } + +bool sendBuffer(std::string& text) +{ + pid_t pid; + int fd[2]; + + if (pipe(fd)) + return false; + + if ((pid = fork()) == -1) + { // fork error + return false; + } + else if (!pid) + { // child + close(fd[1]); + + if (fd[0] != STDIN_FILENO) + { + if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) + { + close(fd[0]); + exit(1); + } + close(fd[0]); + } + execl("/usr/bin/xsel", "xsel", "-i", (char *)0); + exit(1); + } + + // parent + close(fd[0]); + const int len = strlen(text.c_str()); + if (write(fd[1], text.c_str(), len) != len) + { + close(fd[1]); + return false; + } + close(fd[1]); + return true; +} + #else bool retrieveBuffer(std::string&, std::string::size_type&) { return false; } + +bool sendBuffer(std::string& text) +{ + return false; +} #endif diff --git a/src/utils/copynpaste.h b/src/utils/copynpaste.h index d09105139..03596d5d1 100644 --- a/src/utils/copynpaste.h +++ b/src/utils/copynpaste.h @@ -31,3 +31,4 @@ */ bool retrieveBuffer(std::string& text, std::string::size_type& pos); +bool sendBuffer(std::string& text); |