2001-11-19 03:33:36 +03:00
|
|
|
#include "CMSWindowsClipboard.h"
|
|
|
|
#include "CLog.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CMSWindowsClipboard
|
|
|
|
//
|
|
|
|
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::CMSWindowsClipboard(HWND window) :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_window(window),
|
|
|
|
m_time(0)
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2001-11-25 21:32:41 +03:00
|
|
|
// do nothing
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMSWindowsClipboard::~CMSWindowsClipboard()
|
|
|
|
{
|
2001-11-25 21:32:41 +03:00
|
|
|
// do nothing
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CMSWindowsClipboard::empty()
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2002-05-27 22:55:51 +04:00
|
|
|
log((CLOG_DEBUG "empty clipboard"));
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-05-27 22:55:51 +04:00
|
|
|
if (!EmptyClipboard()) {
|
|
|
|
log((CLOG_DEBUG "failed to grab clipboard"));
|
2002-04-30 20:23:03 +04:00
|
|
|
return false;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
|
2001-11-25 21:32:41 +03:00
|
|
|
return true;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::add(EFormat format, const CString& data)
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2002-05-22 21:08:37 +04:00
|
|
|
log((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));
|
2001-11-25 21:32:41 +03:00
|
|
|
|
|
|
|
// convert data to win32 required form
|
|
|
|
const UINT win32Format = convertFormatToWin32(format);
|
|
|
|
HANDLE win32Data;
|
|
|
|
switch (win32Format) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case CF_TEXT:
|
2001-11-25 21:32:41 +03:00
|
|
|
win32Data = convertTextToWin32(data);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-11-25 21:32:41 +03:00
|
|
|
win32Data = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put the data on the clipboard
|
|
|
|
if (win32Data != NULL) {
|
|
|
|
SetClipboardData(win32Format, win32Data);
|
|
|
|
}
|
2002-05-27 22:55:51 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::open(Time time) const
|
2002-05-27 22:55:51 +04:00
|
|
|
{
|
|
|
|
log((CLOG_DEBUG "open clipboard"));
|
|
|
|
|
|
|
|
if (!OpenClipboard(m_window)) {
|
|
|
|
log((CLOG_WARN "failed to open clipboard"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_time = time;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CMSWindowsClipboard::close() const
|
2002-05-27 22:55:51 +04:00
|
|
|
{
|
|
|
|
log((CLOG_DEBUG "close clipboard"));
|
2001-11-25 21:32:41 +03:00
|
|
|
CloseClipboard();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
IClipboard::Time
|
|
|
|
CMSWindowsClipboard::getTime() const
|
2002-04-30 20:23:03 +04:00
|
|
|
{
|
|
|
|
return m_time;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::has(EFormat format) const
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
const UINT win32Format = convertFormatToWin32(format);
|
|
|
|
return (win32Format != 0 && IsClipboardFormatAvailable(win32Format) != 0);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::get(EFormat format) const
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
// get the win32 format. return empty data if unknown format.
|
|
|
|
const UINT win32Format = convertFormatToWin32(format);
|
2002-06-11 02:06:45 +04:00
|
|
|
if (win32Format == 0) {
|
2001-11-25 21:32:41 +03:00
|
|
|
return CString();
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
|
|
|
|
// get a handle to the clipboard data and convert it
|
|
|
|
HANDLE win32Data = GetClipboardData(win32Format);
|
|
|
|
CString data;
|
|
|
|
if (win32Data != NULL) {
|
|
|
|
// convert the data
|
|
|
|
switch (win32Format) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case CF_TEXT:
|
2001-11-25 21:32:41 +03:00
|
|
|
data = convertTextFromWin32(win32Data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
UINT
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::convertFormatToWin32(EFormat format) const
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
switch (format) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case kText:
|
2001-11-25 21:32:41 +03:00
|
|
|
return CF_TEXT;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-11-25 21:32:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
HANDLE
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::convertTextToWin32(const CString& data) const
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2001-11-25 21:32:41 +03:00
|
|
|
// compute size of converted text
|
2002-04-30 20:23:03 +04:00
|
|
|
UInt32 dstSize = 1;
|
2001-11-25 21:32:41 +03:00
|
|
|
const UInt32 srcSize = data.size();
|
|
|
|
const char* src = data.c_str();
|
|
|
|
for (UInt32 index = 0; index < srcSize; ++index) {
|
|
|
|
if (src[index] == '\n') {
|
|
|
|
// add \r
|
|
|
|
++dstSize;
|
|
|
|
}
|
|
|
|
++dstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate
|
|
|
|
HGLOBAL gData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dstSize);
|
|
|
|
if (gData != NULL) {
|
|
|
|
// get a pointer to the allocated memory
|
|
|
|
char* dst = (char*)GlobalLock(gData);
|
|
|
|
if (dst != NULL) {
|
|
|
|
// convert text. we change LF to CRLF.
|
|
|
|
dstSize = 0;
|
|
|
|
for (UInt32 index = 0; index < srcSize; ++index) {
|
|
|
|
if (src[index] == '\n') {
|
|
|
|
// add \r
|
|
|
|
dst[dstSize++] = '\r';
|
|
|
|
}
|
|
|
|
dst[dstSize++] = src[index];
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
dst[dstSize] = '\0';
|
2001-11-25 21:32:41 +03:00
|
|
|
|
|
|
|
// done converting
|
|
|
|
GlobalUnlock(gData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gData;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
2002-06-17 17:31:21 +04:00
|
|
|
CMSWindowsClipboard::convertTextFromWin32(HANDLE handle) const
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2001-11-25 21:32:41 +03:00
|
|
|
// get source data and it's size
|
|
|
|
const char* src = (const char*)GlobalLock(handle);
|
|
|
|
UInt32 srcSize = (SInt32)GlobalSize(handle);
|
2002-06-11 02:06:45 +04:00
|
|
|
if (src == NULL || srcSize <= 1) {
|
2001-11-25 21:32:41 +03:00
|
|
|
return CString();
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-30 20:23:03 +04:00
|
|
|
// ignore trailing NUL
|
|
|
|
--srcSize;
|
|
|
|
|
2001-11-25 21:32:41 +03:00
|
|
|
// compute size of converted text
|
|
|
|
UInt32 dstSize = 0;
|
|
|
|
UInt32 index;
|
|
|
|
for (index = 0; index < srcSize; ++index) {
|
|
|
|
if (src[index] == '\r') {
|
|
|
|
// skip \r
|
2002-06-11 02:06:45 +04:00
|
|
|
if (index + 1 < srcSize && src[index + 1] == '\n') {
|
2001-11-25 21:32:41 +03:00
|
|
|
++index;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
++dstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate
|
|
|
|
CString data;
|
|
|
|
data.reserve(dstSize);
|
|
|
|
|
|
|
|
// convert text. we change CRLF to LF.
|
|
|
|
for (index = 0; index < srcSize; ++index) {
|
|
|
|
if (src[index] == '\r') {
|
|
|
|
// skip \r
|
2002-06-11 02:06:45 +04:00
|
|
|
if (index + 1 < srcSize && src[index + 1] == '\n') {
|
2001-11-25 21:32:41 +03:00
|
|
|
++index;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
data += src[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|