void COSXClipboard::add(EFormat format, const CString & data) { LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format)); for (ConverterList::const_iterator index = m_converters.begin(); index != m_converters.end(); ++index) { IOSXClipboardConverter* converter = *index; // skip converters for other formats if (converter->getFormat() == format) { CString osXData = converter->fromIClipboard(data); CFStringRef flavorType = converter->getOSXFormat(); CFDataRef dataRef = CFDataCreate(kCFAllocatorDefault, (UInt8 *)osXData.data(), osXData.size()); PasteboardPutItemFlavor( m_pboard, (PasteboardItemID) 0, flavorType, dataRef, kPasteboardFlavorNoFlags); LOG((CLOG_DEBUG "added %d bytes to clipboard format: %d", data.size(), format)); } } }
bool COSXClipboard::has(EFormat format) const { if (m_pboard == NULL) return false; PasteboardItemID item; PasteboardGetItemIdentifier(m_pboard, (CFIndex) 1, &item); for (ConverterList::const_iterator index = m_converters.begin(); index != m_converters.end(); ++index) { IOSXClipboardConverter* converter = *index; if (converter->getFormat() == format) { PasteboardFlavorFlags flags; CFStringRef type = converter->getOSXFormat(); OSStatus res; if ((res = PasteboardGetItemFlavorFlags(m_pboard, item, type, &flags)) == noErr) { return true; } } } return false; }
CString COSXClipboard::get(EFormat format) const { CFStringRef type; PasteboardItemID item; CString result; if (m_pboard == NULL) return result; PasteboardGetItemIdentifier(m_pboard, (CFIndex) 1, &item); // find the converter for the first clipboard format we can handle IOSXClipboardConverter* converter = NULL; for (ConverterList::const_iterator index = m_converters.begin(); index != m_converters.end(); ++index) { converter = *index; PasteboardFlavorFlags flags; type = converter->getOSXFormat(); if (converter->getFormat() == format && PasteboardGetItemFlavorFlags(m_pboard, item, type, &flags) == noErr) { break; } converter = NULL; } // if no converter then we don't recognize any formats if (converter == NULL) { LOG((CLOG_DEBUG "Unable to find converter for data")); return result; } // get the clipboard data. CFDataRef buffer = NULL; try { OSStatus err = PasteboardCopyItemFlavorData(m_pboard, item, type, &buffer); if (err != noErr) { throw err; } result = CString((char *) CFDataGetBytePtr(buffer), CFDataGetLength(buffer)); } catch (OSStatus err) { LOG((CLOG_DEBUG "exception thrown in COSXClipboard::get MacError (%d)", err)); } catch (...) { LOG((CLOG_DEBUG "unknown exception in COSXClipboard::get")); RETHROW_XTHREAD } if (buffer != NULL) CFRelease(buffer); return converter->toIClipboard(result); }
void COSXClipboard::add(EFormat format, const CString & data) { bool emptied = false; if (m_pboard == NULL) return; LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format)); if(format == IClipboard::kText) { LOG((CLOG_DEBUG " format of data to be added to clipboard was kText")); } else if(format == IClipboard::kBitmap) { LOG((CLOG_DEBUG " format of data to be added to clipboard was kBitmap")); } else if(format == IClipboard::kHTML) { LOG((CLOG_DEBUG " format of data to be added to clipboard was kHTML")); } for (ConverterList::const_iterator index = m_converters.begin(); index != m_converters.end(); ++index) { IOSXClipboardConverter* converter = *index; // skip converters for other formats if (converter->getFormat() == format) { CString osXData = converter->fromIClipboard(data); CFStringRef flavorType = converter->getOSXFormat(); CFDataRef dataRef = CFDataCreate(kCFAllocatorDefault, (UInt8 *)osXData.data(), osXData.size()); // integ tests showed that if you call add(...) twice, then the // second call will actually fail to set clipboard data. calling // empty() seems to solve this problem. but, only clear the clipboard // for the first converter, otherwise further converters will wipe out // what we just added. if (!emptied) { empty(); emptied = true; } PasteboardPutItemFlavor( m_pboard, (PasteboardItemID) 0, flavorType, dataRef, kPasteboardFlavorNoFlags); LOG((CLOG_DEBUG "added %d bytes to clipboard format: %d", data.size(), format)); } } }