2001-11-25 18:32:41 +00:00
|
|
|
#include "CClipboard.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CClipboard
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CClipboard::CClipboard() :
|
|
|
|
m_open(false),
|
|
|
|
m_owner(false)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-29 13:31:44 +00:00
|
|
|
open(0);
|
2002-05-27 16:22:59 +00:00
|
|
|
empty();
|
2002-04-29 13:31:44 +00:00
|
|
|
close();
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CClipboard::~CClipboard()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CClipboard::empty()
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
assert(m_open);
|
|
|
|
|
2001-11-25 18:32:41 +00:00
|
|
|
// clear all data
|
|
|
|
for (SInt32 index = 0; index < kNumFormats; ++index) {
|
|
|
|
m_data[index] = "";
|
|
|
|
m_added[index] = false;
|
|
|
|
}
|
2002-04-29 13:31:44 +00:00
|
|
|
|
|
|
|
// save time
|
2002-05-27 16:22:59 +00:00
|
|
|
m_timeOwned = m_time;
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// we're the owner now
|
|
|
|
m_owner = true;
|
2001-11-25 18:32:41 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
return true;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClipboard::add(
|
|
|
|
EFormat format,
|
|
|
|
const CString& data)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
assert(m_open);
|
|
|
|
assert(m_owner);
|
|
|
|
|
2001-11-25 18:32:41 +00:00
|
|
|
m_data[format] = data;
|
|
|
|
m_added[format] = true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CClipboard::open(
|
|
|
|
Time time) const
|
2002-05-27 16:22:59 +00:00
|
|
|
{
|
|
|
|
assert(!m_open);
|
|
|
|
|
|
|
|
m_open = true;
|
|
|
|
m_time = time;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClipboard::close() const
|
2002-05-27 16:22:59 +00:00
|
|
|
{
|
|
|
|
assert(m_open);
|
|
|
|
|
|
|
|
m_open = false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CClipboard::Time
|
|
|
|
CClipboard::getTime() const
|
2002-04-29 13:31:44 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
return m_timeOwned;
|
2002-04-29 13:31:44 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CClipboard::has(
|
|
|
|
EFormat format) const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
assert(m_open);
|
2001-11-25 18:32:41 +00:00
|
|
|
return m_added[format];
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
|
|
|
CClipboard::get(
|
|
|
|
EFormat format) const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
assert(m_open);
|
2001-11-25 18:32:41 +00:00
|
|
|
return m_data[format];
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CClipboard::copy(
|
|
|
|
IClipboard* dst,
|
|
|
|
const IClipboard* src)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-29 13:31:44 +00:00
|
|
|
assert(dst != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
return copy(dst, src, src->getTime());
|
2002-04-29 13:31:44 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
|
|
|
CClipboard::copy(
|
|
|
|
IClipboard* dst,
|
|
|
|
const IClipboard* src,
|
|
|
|
Time time)
|
2002-04-29 13:31:44 +00:00
|
|
|
{
|
|
|
|
assert(dst != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
bool success = false;
|
|
|
|
if (src->open(time)) {
|
|
|
|
if (dst->open(time)) {
|
|
|
|
if (dst->empty()) {
|
|
|
|
for (SInt32 format = 0;
|
|
|
|
format != IClipboard::kNumFormats; ++format) {
|
|
|
|
IClipboard::EFormat eFormat = (IClipboard::EFormat)format;
|
|
|
|
if (src->has(eFormat)) {
|
|
|
|
dst->add(eFormat, src->get(eFormat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
success = true;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
dst->close();
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
src->close();
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
|
|
|
|
return success;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClipboard::unmarshall(
|
|
|
|
const CString& data,
|
|
|
|
Time time)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
|
|
|
const char* index = data.data();
|
|
|
|
|
|
|
|
// clear existing data
|
2002-04-29 13:31:44 +00:00
|
|
|
open(time);
|
2002-05-27 16:22:59 +00:00
|
|
|
empty();
|
2001-11-25 18:32:41 +00:00
|
|
|
|
|
|
|
// read the number of formats
|
|
|
|
const UInt32 numFormats = readUInt32(index);
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
// read each format
|
2002-04-30 16:23:03 +00:00
|
|
|
for (UInt32 i = 0; i < numFormats; ++i) {
|
2001-11-25 18:32:41 +00:00
|
|
|
// get the format id
|
|
|
|
UInt32 format = readUInt32(index);
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
// get the size of the format data
|
|
|
|
UInt32 size = readUInt32(index);
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
// save the data
|
|
|
|
m_added[format] = true;
|
|
|
|
m_data[format] = CString(index, size);
|
|
|
|
index += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
|
|
|
CClipboard::marshall() const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
|
|
|
CString data;
|
|
|
|
|
|
|
|
// compute size of marshalled data
|
|
|
|
UInt32 size = 4;
|
|
|
|
UInt32 numFormats = 0;
|
|
|
|
UInt32 format;
|
|
|
|
for (format = 0; format != IClipboard::kNumFormats; ++format) {
|
|
|
|
if (m_added[format]) {
|
|
|
|
++numFormats;
|
|
|
|
size += 4 + 4 + m_data[format].size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate space
|
|
|
|
data.reserve(size);
|
|
|
|
|
|
|
|
// marshall the data
|
|
|
|
writeUInt32(&data, numFormats);
|
|
|
|
for (format = 0; format != IClipboard::kNumFormats; ++format) {
|
|
|
|
if (m_added[format]) {
|
|
|
|
writeUInt32(&data, format);
|
|
|
|
writeUInt32(&data, m_data[format].size());
|
|
|
|
data += m_data[format];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32
|
|
|
|
CClipboard::readUInt32(
|
|
|
|
const char* buf) const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-30 16:23:03 +00:00
|
|
|
const unsigned char* ubuf = reinterpret_cast<const unsigned char*>(buf);
|
|
|
|
return (static_cast<UInt32>(ubuf[0]) << 24) |
|
|
|
|
(static_cast<UInt32>(ubuf[1]) << 16) |
|
|
|
|
(static_cast<UInt32>(ubuf[2]) << 8) |
|
|
|
|
static_cast<UInt32>(ubuf[3]);
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClipboard::writeUInt32(
|
|
|
|
CString* buf,
|
|
|
|
UInt32 v) const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
|
|
|
*buf += static_cast<UInt8>((v >> 24) & 0xff);
|
|
|
|
*buf += static_cast<UInt8>((v >> 16) & 0xff);
|
|
|
|
*buf += static_cast<UInt8>((v >> 8) & 0xff);
|
|
|
|
*buf += static_cast<UInt8>( v & 0xff);
|
|
|
|
}
|