diff --git a/src/ui/clipboard.cpp b/src/ui/clipboard.cpp index 7b2864666..37ae21dc4 100644 --- a/src/ui/clipboard.cpp +++ b/src/ui/clipboard.cpp @@ -10,69 +10,22 @@ #include "ui/clipboard.h" -#include "base/string.h" - -#include - #ifdef WIN32 -#include -#include +#include "ui/clipboard_win.h" +#else +#include "ui/clipboard_none.h" #endif -#pragma warning(disable:4996) // To void MSVC warning about std::copy() with unsafe arguments - -static base::string clipboard_text; - -static void lowlevel_set_clipboard_text(const char *text) -{ - clipboard_text = text ? text: ""; -} +static std::string clipboard_text; const char* ui::clipboard::get_text() { -#ifdef WIN32 - if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { - if (OpenClipboard(win_get_window())) { - HGLOBAL hglobal = GetClipboardData(CF_UNICODETEXT); - if (hglobal != NULL) { - LPWSTR lpstr = static_cast(GlobalLock(hglobal)); - if (lpstr != NULL) { - lowlevel_set_clipboard_text(base::to_utf8(lpstr).c_str()); - GlobalUnlock(hglobal); - } - } - CloseClipboard(); - } - } -#endif - + get_system_clipboard_text(clipboard_text); return clipboard_text.c_str(); } -void ui::clipboard::set_text(const char *text) +void ui::clipboard::set_text(const char* text) { - lowlevel_set_clipboard_text(text); - -#ifdef WIN32 - if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { - if (OpenClipboard(win_get_window())) { - EmptyClipboard(); - - if (!clipboard_text.empty()) { - std::wstring wstr = base::from_utf8(clipboard_text); - int len = wstr.size(); - - HGLOBAL hglobal = GlobalAlloc(GMEM_MOVEABLE | - GMEM_ZEROINIT, sizeof(WCHAR)*(len+1)); - - LPWSTR lpstr = static_cast(GlobalLock(hglobal)); - std::copy(wstr.begin(), wstr.end(), lpstr); - GlobalUnlock(hglobal); - - SetClipboardData(CF_UNICODETEXT, hglobal); - } - CloseClipboard(); - } - } -#endif + clipboard_text = text ? text: ""; + set_system_clipboard_text(clipboard_text); } diff --git a/src/ui/clipboard.h b/src/ui/clipboard.h index d460f3fa2..5a449df9d 100644 --- a/src/ui/clipboard.h +++ b/src/ui/clipboard.h @@ -18,4 +18,4 @@ namespace ui { } // namespace clipboard } // namespace ui -#endif // ASE_JINETE_JCLIPBOARD_H +#endif diff --git a/src/ui/clipboard_none.h b/src/ui/clipboard_none.h new file mode 100644 index 000000000..c7e85caa3 --- /dev/null +++ b/src/ui/clipboard_none.h @@ -0,0 +1,19 @@ +// Aseprite UI Library +// Copyright (C) 2001-2014 David Capello +// +// This source file is distributed under MIT license, +// please read LICENSE.txt for more information. + +namespace { + +void get_system_clipboard_text(std::string& text) +{ + // Do nothing +} + +void set_system_clipboard_text(const std::string& text) +{ + // Do nothing +} + +} diff --git a/src/ui/clipboard_win.h b/src/ui/clipboard_win.h new file mode 100644 index 000000000..a9b7bf0cf --- /dev/null +++ b/src/ui/clipboard_win.h @@ -0,0 +1,58 @@ +// Aseprite UI Library +// Copyright (C) 2001-2014 David Capello +// +// This source file is distributed under MIT license, +// please read LICENSE.txt for more information. + +#include "base/string.h" + +#include +#include +#include + +#pragma warning(disable:4996) // To void MSVC warning about std::copy() with unsafe arguments + +namespace { + +void get_system_clipboard_text(std::string& text) +{ + if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { + if (OpenClipboard(win_get_window())) { + HGLOBAL hglobal = GetClipboardData(CF_UNICODETEXT); + if (hglobal != NULL) { + LPWSTR lpstr = static_cast(GlobalLock(hglobal)); + if (lpstr != NULL) { + text = base::to_utf8(lpstr).c_str(); + GlobalUnlock(hglobal); + } + } + CloseClipboard(); + } + } +} + +void set_system_clipboard_text(const std::string& text) +{ + if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { + if (OpenClipboard(win_get_window())) { + EmptyClipboard(); + + if (!text.empty()) { + std::wstring wstr = base::from_utf8(text); + int len = wstr.size(); + + HGLOBAL hglobal = GlobalAlloc(GMEM_MOVEABLE | + GMEM_ZEROINIT, sizeof(WCHAR)*(len+1)); + + LPWSTR lpstr = static_cast(GlobalLock(hglobal)); + std::copy(wstr.begin(), wstr.end(), lpstr); + GlobalUnlock(hglobal); + + SetClipboardData(CF_UNICODETEXT, hglobal); + } + CloseClipboard(); + } + } +} + +}