Move code to handle Windows clipboard (text) to ui/clipboard_win.h

This commit is contained in:
David Capello 2014-02-08 00:41:16 -03:00
parent e082cd96f1
commit 51f70a170a
4 changed files with 86 additions and 56 deletions

View File

@ -10,69 +10,22 @@
#include "ui/clipboard.h"
#include "base/string.h"
#include <algorithm>
#ifdef WIN32
#include <allegro.h>
#include <winalleg.h>
#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<LPWSTR>(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<LPWSTR>(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);
}

View File

@ -18,4 +18,4 @@ namespace ui {
} // namespace clipboard
} // namespace ui
#endif // ASE_JINETE_JCLIPBOARD_H
#endif

19
src/ui/clipboard_none.h Normal file
View File

@ -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
}
}

58
src/ui/clipboard_win.h Normal file
View File

@ -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 <algorithm>
#include <allegro.h>
#include <winalleg.h>
#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<LPWSTR>(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<LPWSTR>(GlobalLock(hglobal));
std::copy(wstr.begin(), wstr.end(), lpstr);
GlobalUnlock(hglobal);
SetClipboardData(CF_UNICODETEXT, hglobal);
}
CloseClipboard();
}
}
}
}