mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-29 01:20:17 +00:00
Move code to handle Windows clipboard (text) to ui/clipboard_win.h
This commit is contained in:
parent
e082cd96f1
commit
51f70a170a
@ -10,69 +10,22 @@
|
|||||||
|
|
||||||
#include "ui/clipboard.h"
|
#include "ui/clipboard.h"
|
||||||
|
|
||||||
#include "base/string.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <allegro.h>
|
#include "ui/clipboard_win.h"
|
||||||
#include <winalleg.h>
|
#else
|
||||||
|
#include "ui/clipboard_none.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma warning(disable:4996) // To void MSVC warning about std::copy() with unsafe arguments
|
static std::string clipboard_text;
|
||||||
|
|
||||||
static base::string clipboard_text;
|
|
||||||
|
|
||||||
static void lowlevel_set_clipboard_text(const char *text)
|
|
||||||
{
|
|
||||||
clipboard_text = text ? text: "";
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* ui::clipboard::get_text()
|
const char* ui::clipboard::get_text()
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
get_system_clipboard_text(clipboard_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) {
|
|
||||||
lowlevel_set_clipboard_text(base::to_utf8(lpstr).c_str());
|
|
||||||
GlobalUnlock(hglobal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CloseClipboard();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return clipboard_text.c_str();
|
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);
|
clipboard_text = text ? text: "";
|
||||||
|
set_system_clipboard_text(clipboard_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
|
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,4 @@ namespace ui {
|
|||||||
} // namespace clipboard
|
} // namespace clipboard
|
||||||
} // namespace ui
|
} // namespace ui
|
||||||
|
|
||||||
#endif // ASE_JINETE_JCLIPBOARD_H
|
#endif
|
||||||
|
19
src/ui/clipboard_none.h
Normal file
19
src/ui/clipboard_none.h
Normal 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
58
src/ui/clipboard_win.h
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user