From fd88a4c2b784865200e96a16b6dc359cce708202 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 18 Jul 2013 03:03:01 +0000 Subject: [PATCH] Implementando metodos de clipboard no windows --- src/classlib/remote.cc | 36 +++++++----------------------------- src/classlib/session.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/classlib/testprogram.cc | 7 +++++++ 3 files changed, 76 insertions(+), 29 deletions(-) diff --git a/src/classlib/remote.cc b/src/classlib/remote.cc index 84a3957..4c64a54 100644 --- a/src/classlib/remote.cc +++ b/src/classlib/remote.cc @@ -1040,14 +1040,14 @@ } - int set_clipboard(const char *text) +#if defined(HAVE_DBUS) + string * get_clipboard(void) { -#if defined(WIN32) - - return -1; - -#elif defined(HAVE_DBUS) + return query_string("getClipboard"); + } + int set_clipboard(const char *text) + { DBusMessage * msg = create_message("setClipboard"); if(msg) { @@ -1056,30 +1056,8 @@ } return -1; - -#else - - return -1; - -#endif - - } - - string * get_clipboard(void) - { -#if defined(WIN32) - - return NULL; - -#elif defined(HAVE_DBUS) - - trace("%s",__FUNCTION__); - return query_string("getClipboard"); - -#endif - - return NULL; } +#endif // HAVE_DBUS }; diff --git a/src/classlib/session.cc b/src/classlib/session.cc index 20148ce..f032b33 100644 --- a/src/classlib/session.cc +++ b/src/classlib/session.cc @@ -214,13 +214,75 @@ string * session::get_clipboard(void) { +#if defined(WIN32) + + if (! OpenClipboard(0)) + { + throw exception(GetLastError(),"%s","Can´t open system clipboard"); + return NULL; + } + + HANDLE hData = GetClipboardData(CF_TEXT); + if(!hData) + { + throw exception(GetLastError(),"%s","Can´t get clipboard data"); + return NULL; + } + + char * pszText = static_cast( GlobalLock(hData) ); + if(!pszText) + { + throw exception(GetLastError(),"%s","Can´t lock clipboard"); + return NULL; + } + + string *text = new string ( pszText ); + + GlobalUnlock( hData ); + + CloseClipboard(); + + return text; + +#else errno = EINVAL; return NULL; + +#endif // WIN32 } int session::set_clipboard(const char *text) { +#if defined(WIN32) + if (! OpenClipboard(0)) + { + throw exception(GetLastError(),"%s","Can´t open system clipboard"); + return -1; + } + + EmptyClipboard(); + + size_t size = strlen(text)+1; + HGLOBAL hClipboardData = GlobalAlloc(GMEM_MOVEABLE , size); + + strcpy((char *) GlobalLock(hClipboardData), text); + + if(!SetClipboardData(CF_TEXT, hClipboardData)) + { + GlobalUnlock(hClipboardData); + CloseClipboard(); + throw exception(GetLastError(),"%s","Can´t set system clipboard"); + } + + GlobalUnlock(hClipboardData); + CloseClipboard(); + + return 0; +#else + return EINVAL; + +#endif // WIN32 } diff --git a/src/classlib/testprogram.cc b/src/classlib/testprogram.cc index 5a2e498..9f03ab6 100644 --- a/src/classlib/testprogram.cc +++ b/src/classlib/testprogram.cc @@ -50,6 +50,13 @@ cout << "\tSession state: " << session->get_cstate() << endl; cout << "\tCharset: " << session->get_charset() << endl; + string *str = session->get_clipboard(); + cout << "\nClipboard: " << str->c_str(); + + delete str; + + session->set_clipboard("Teste de clipboard"); + delete session; return 0; } -- libgit2 0.21.2