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 ClipBoard::setText(const string text) { #ifdef TARGET_OSX OSStatus err = noErr; static PasteboardRef pasteboard = NULL; PasteboardCreate( kPasteboardClipboard, &pasteboard ); err = PasteboardClear( pasteboard ); require_noerr( err, PasteboardClear_FAILED ); CFDataRef data; data = CFDataCreate(kCFAllocatorDefault, (UInt8*) text.c_str(), strlen(text.c_str())+1); err = PasteboardPutItemFlavor( pasteboard, (PasteboardItemID)1, kUTTypeUTF8PlainText, data, 0); require_noerr( err, PasteboardPutItemFlavor_FAILED ); return true; PasteboardPutItemFlavor_FAILED: ofLog(OF_LOG_ERROR, "ofxGLEditor: pasting from the pasteboard failed"); return false; PasteboardClear_FAILED: ofLog(OF_LOG_ERROR, "ofxGLEditor: clearing the cliboard failed"); return false; #else ofLog(OF_LOG_WARNING, "ofxGLEditor: sorry, pasting from the system clipboard is not supported on your OS yet"); return true; #endif }
static void RomInfoCopyToClipboard (void) { OSStatus err; PasteboardRef clipboard; PasteboardSyncFlags sync; CFDataRef cfdata; char text[1024]; RomInfoBuildInfoText(text); err = PasteboardCreate(kPasteboardClipboard, &clipboard); if (err == noErr) { err = PasteboardClear(clipboard); if (err == noErr) { sync = PasteboardSynchronize(clipboard); if (!(sync & kPasteboardModified) && (sync & kPasteboardClientIsOwner)) { cfdata = CFDataCreate(kCFAllocatorDefault, (UInt8 *) text, (CFIndex) strlen(text)); if (cfdata) { err = PasteboardPutItemFlavor(clipboard, (PasteboardItemID) 1, CFSTR("com.apple.traditional-mac-plain-text"), cfdata, 0); CFRelease(cfdata); } } } CFRelease(clipboard); } }
void Picture_copyToClipboard (Picture me) { /* * Find the clipboard and clear it. */ PasteboardRef clipboard = nullptr; PasteboardCreate (kPasteboardClipboard, & clipboard); PasteboardClear (clipboard); /* * Add a PDF flavour to the clipboard. */ static CGDataConsumerCallbacks callbacks = { appendBytes, nullptr }; CFDataRef data = CFDataCreateMutable (kCFAllocatorDefault, 0); CGDataConsumerRef consumer = CGDataConsumerCreate ((void *) data, & callbacks); int resolution = 600; CGRect rect = CGRectMake (0, 0, (my selx2 - my selx1) * resolution, (my sely1 - my sely2) * resolution); CGContextRef context = CGPDFContextCreate (consumer, & rect, nullptr); //my selx1 * RES, (12 - my sely2) * RES, my selx2 * RES, (12 - my sely1) * RES) {// scope autoGraphics graphics = Graphics_create_pdf (context, resolution, my selx1, my selx2, my sely1, my sely2); Graphics_play (my graphics.get(), graphics.get()); } PasteboardPutItemFlavor (clipboard, (PasteboardItemID) 1, kUTTypePDF, data, kPasteboardFlavorNoFlags); CFRelease (data); /* * Forget the clipboard. */ CFRelease (clipboard); }
void GHOST_SystemCarbon::putClipboard(GHOST_TInt8 *buffer, bool selection) const { if (selection) {return; } // for copying the selection, used on X11 PasteboardRef inPasteboard; CFDataRef textData = NULL; OSStatus err = noErr; /*For error checking*/ OSStatus syncFlags; err = PasteboardCreate(kPasteboardClipboard, &inPasteboard); if (err != noErr) { return; } syncFlags = PasteboardSynchronize(inPasteboard); /* as we always put in a new string, we can safely ignore sync flags */ if (syncFlags < 0) { return; } err = PasteboardClear(inPasteboard); if (err != noErr) { return; } textData = CFDataCreate(kCFAllocatorDefault, (UInt8 *)buffer, strlen(buffer)); if (textData) { err = PasteboardPutItemFlavor(inPasteboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), textData, 0); if (err != noErr) { if (textData) { CFRelease(textData); } return; } } if (textData) { CFRelease(textData); } }
void clipboard_push_text(char * text) { #ifdef MACOSX PasteboardRef newclipboard; if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return; if (PasteboardClear(newclipboard)!=noErr) return; PasteboardSynchronize(newclipboard); CFDataRef data = CFDataCreate(kCFAllocatorDefault, text, strlen(text)); PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0); #elif defined WIN32 if (OpenClipboard(NULL)) { HGLOBAL cbuffer; char * glbuffer; EmptyClipboard(); cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1); glbuffer = (char*)GlobalLock(cbuffer); strcpy(glbuffer, text); GlobalUnlock(cbuffer); SetClipboardData(CF_TEXT, cbuffer); CloseClipboard(); } #else printf("Not implemented: put text on clipboard \"%s\"\n", text); #endif }
void copy_to_clipboard(const std::string& text, const bool) { std::string new_str; new_str.reserve(text.size()); for (unsigned int i = 0; i < text.size(); ++i) { if (text[i] == '\n') { new_str.push_back('\r'); } else { new_str.push_back(text[i]); } } OSStatus err = noErr; PasteboardRef clipboard; PasteboardSyncFlags syncFlags; CFDataRef textData = NULL; err = PasteboardCreate(kPasteboardClipboard, &clipboard); if (err != noErr) return; err = PasteboardClear(clipboard); if (err != noErr) return; syncFlags = PasteboardSynchronize(clipboard); if ((syncFlags&kPasteboardModified) && !(syncFlags&kPasteboardClientIsOwner)) return; textData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)new_str.c_str(), text.size()); PasteboardPutItemFlavor(clipboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), textData, 0); }
/** * Paste buffer into guest clipboard. * * @param pPasteboard Guest PasteBoard reference. * @param pData Data to be pasted. * @param cbDataSize The size of *pData. * @param fFormat Buffer data format. * @param fClear Whether or not clear guest clipboard before insert data. * * @returns IPRT status code. */ static int vbclClipboardGuestPasteData(PasteboardRef pPasteboard, UInt8 *pData, CFIndex cbDataSize, CFStringRef sFormat, bool fClear) { PasteboardItemID itemId = (PasteboardItemID)1; CFDataRef textData = NULL; OSStatus rc; /* Ignoring sunchronization flags here */ PasteboardSynchronize(pPasteboard); if (fClear) { rc = PasteboardClear(pPasteboard); AssertReturn(rc == noErr, VERR_NOT_SUPPORTED); } /* Create a CData object which we could pass to the pasteboard */ if ((textData = CFDataCreate(kCFAllocatorDefault, pData, cbDataSize))) { /* Put the Utf-8 version to the pasteboard */ rc = PasteboardPutItemFlavor(pPasteboard, itemId, sFormat, textData, 0); CFRelease(textData); if (rc != noErr) { VBoxClientVerbose(3, "unable to put data into guest's clipboard: %d\n", rc); return VERR_GENERAL_FAILURE; } } else return VERR_NO_MEMORY; /* Synchronize updated content */ PasteboardSynchronize(pPasteboard); return VINF_SUCCESS; }
void QMacPasteboard::setMimeData(QMimeData *mime_src) { if (!paste) return; if (mime == mime_src || (!mime_src && mime && mac_mime_source)) return; mac_mime_source = false; delete mime; mime = mime_src; QList<QMacPasteboardMime*> availableConverters = QMacPasteboardMime::all(mime_type); if (mime != 0) { clear_helper(); QStringList formats = mime_src->formats(); #ifdef QT_MAC_USE_COCOA // QMimeData sub classes reimplementing the formats() might not expose the // temporary "application/x-qt-mime-type-name" mimetype. So check the existence // of this mime type while doing drag and drop. QString dummyMimeType(QLatin1String("application/x-qt-mime-type-name")); if (!formats.contains(dummyMimeType)) { QByteArray dummyType = mime_src->data(dummyMimeType); if (!dummyType.isEmpty()) { formats.append(dummyMimeType); } } #endif for(int f = 0; f < formats.size(); ++f) { QString mimeType = formats.at(f); for (QList<QMacPasteboardMime *>::Iterator it = availableConverters.begin(); it != availableConverters.end(); ++it) { QMacPasteboardMime *c = (*it); QString flavor(c->flavorFor(mimeType)); if(!flavor.isEmpty()) { QVariant mimeData = static_cast<QMacMimeData*>(mime_src)->variantData(mimeType); #if 0 //### Grrr, why didn't I put in a virtual int QMacPasteboardMime::count()? --Sam const int numItems = c->convertFromMime(mimeType, mimeData, flavor).size(); #else int numItems = 1; //this is a hack but it is much faster than allowing conversion above if(c->convertorName() == QLatin1String("FileURL")) numItems = mime_src->urls().count(); #endif for(int item = 0; item < numItems; ++item) { const int itemID = item+1; //id starts at 1 promises.append(QMacPasteboard::Promise(itemID, c, mimeType, mimeData, item)); PasteboardPutItemFlavor(paste, (PasteboardItemID)itemID, QCFString(flavor), 0, kPasteboardFlavorNoFlags); #ifdef DEBUG_PASTEBOARD qDebug(" - adding %d %s [%s] <%s> [%d]", itemID, qPrintable(mimeType), qPrintable(flavor), qPrintable(c->convertorName()), item); #endif } } } } } }
OSStatus QMacPasteboard::promiseKeeper(PasteboardRef paste, PasteboardItemID id, CFStringRef flavor, void *_qpaste) { QMacPasteboard *qpaste = (QMacPasteboard*)_qpaste; const long promise_id = (long)id; // Find the kept promise const QString flavorAsQString = QCFString::toQString(flavor); QMacPasteboard::Promise promise; for (int i = 0; i < qpaste->promises.size(); i++){ QMacPasteboard::Promise tmp = qpaste->promises[i]; if (tmp.itemId == promise_id && tmp.convertor->canConvert(tmp.mime, flavorAsQString)){ promise = tmp; break; } } if (!promise.itemId && flavorAsQString == QLatin1String("com.trolltech.qt.MimeTypeName")) { // we have promised this data, but wont be able to convert, so return null data. // This helps in making the application/x-qt-mime-type-name hidden from normal use. QByteArray ba; QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size()); PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags); return noErr; } if (!promise.itemId) { // There was no promise that could deliver data for the // given id and flavor. This should not happend. qDebug("Pasteboard: %d: Request for %ld, %s, but no promise found!", __LINE__, promise_id, qPrintable(flavorAsQString)); return cantGetFlavorErr; } #ifdef DEBUG_PASTEBOARD qDebug("PasteBoard: Calling in promise for %s[%ld] [%s] (%s) [%d]", qPrintable(promise.mime), promise_id, qPrintable(flavorAsQString), qPrintable(promise.convertor->convertorName()), promise.offset); #endif QList<QByteArray> md = promise.convertor->convertFromMime(promise.mime, promise.data, flavorAsQString); if (md.size() <= promise.offset) return cantGetFlavorErr; const QByteArray &ba = md[promise.offset]; QCFType<CFDataRef> data = CFDataCreate(0, (UInt8*)ba.constData(), ba.size()); PasteboardPutItemFlavor(paste, id, flavor, data, kPasteboardFlavorNoFlags); return noErr; }
//****************************************************************************** bool CClipboard::SetString( //Copies the given string into the system clipboard //Returns: true on success, false otherwise. // //Params: const WSTRING& sClip) //(in) { #ifdef WIN32 if (!OpenClipboard(NULL)) return false; EmptyClipboard(); HGLOBAL global = GlobalAlloc(GMEM_ZEROINIT, (sClip.size()+1)*sizeof(WCHAR)); if (global == NULL) { CloseClipboard(); return false; } LPWSTR data = (LPWSTR)GlobalLock(global); WCScpy(data, sClip.c_str()); GlobalUnlock(global); SetClipboardData(CF_UNICODETEXT, global); CloseClipboard(); return true; #elif defined(__APPLE__) PasteboardRef theClipboard; OSStatus err = PasteboardCreate(kPasteboardClipboard, &theClipboard); if (err != noErr) return false; PasteboardClear(theClipboard); PasteboardSynchronize(theClipboard); BYTE *pbOutStr = NULL; if (to_utf8(sClip.c_str(), pbOutStr)) { CFDataRef data = CFDataCreate(kCFAllocatorDefault, (UInt8*)pbOutStr, sClip.size() + 1); PasteboardPutItemFlavor(theClipboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), data, 0); } delete[] pbOutStr; return true; #elif defined(__linux__) || defined(__FreeBSD__) bool bSuccess = false; BYTE *pbOutStr = NULL; if (to_utf8(sClip.c_str(), pbOutStr)) bSuccess = SetStringUTF8((const char*)pbOutStr); delete[] pbOutStr; return bSuccess; #elif defined(__native_client__) return false; #else #error CClipboard::SetString -- Unicode not implemented #endif }
void g2LabelEdit::CopyBuffer() { // Win32 implementation #ifdef _WIN32 // Attempt to open clipboard if(!OpenClipboard(GetForegroundWindow())) return; // Empty current clipboard EmptyClipboard(); // Allocate a system resource (a memory buffer for the text) HGLOBAL TextHandle = GlobalAlloc(GMEM_MOVEABLE, (strlen(TextBuffer) + 1) * sizeof(char)); if(TextHandle == NULL) return; LPTSTR StringLock = (LPTSTR)GlobalLock(TextHandle); if(StringLock == NULL) return; strcpy(StringLock, TextBuffer); GlobalUnlock(TextHandle); // Copy to the clipboard SetClipboardData(CF_TEXT, TextHandle); // Close clipboard CloseClipboard(); // OSX implementation #elif __APPLE__ // Allocate or get a reference to the application's active clipboard PasteboardRef ClipboardHandle; if(PasteboardCreate(kPasteboardClipboard, &ClipboardHandle) != noErr) return; // Clear current clipboard if(PasteboardClear(ClipboardHandle) != noErr) return; // Explicitly update (after cleaning is important) PasteboardSynchronize(ClipboardHandle); // Create a system-wide buffer to give to the clipboard CFDataRef DataBuffer = CFDataCreate(kCFAllocatorDefault, (const UInt8*)TextBuffer, CFIndex((strlen(TextBuffer) + 1) * sizeof(char))); if(DataBuffer == NULL) return; // Paste into clipboard PasteboardPutItemFlavor(ClipboardHandle, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), DataBuffer, kPasteboardFlavorNoFlags); // Not a feature in Linux... #endif }
void QuartzBitmap_Output(QuartzDesc_t dev, QuartzBitmapDevice *qbd) { if(qbd->path && qbd->uti) { /* On 10.4+ we can employ the CGImageDestination API to create a variety of different bitmap formats */ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 char buf[PATH_MAX+1]; snprintf(buf, PATH_MAX, qbd->path, qbd->page); buf[PATH_MAX] = '\0'; CFStringRef pathString = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8*) buf, strlen(buf), kCFStringEncodingUTF8, FALSE); CFURLRef path; if(CFStringFind(pathString, CFSTR("://"), 0).location != kCFNotFound) { CFStringRef pathEscaped = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, pathString, NULL, NULL, kCFStringEncodingUTF8); path = CFURLCreateWithString(kCFAllocatorDefault, pathEscaped, NULL); CFRelease(pathEscaped); } else { path = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*) buf, strlen(buf), FALSE); } CFRelease(pathString); CFStringRef scheme = CFURLCopyScheme(path); CFStringRef type = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8*) qbd->uti, strlen(qbd->uti), kCFStringEncodingUTF8, FALSE); CGImageRef image = CGBitmapContextCreateImage(qbd->bitmap); if(CFStringCompare(scheme,CFSTR("file"), 0) == 0) { /* file output */ CGImageDestinationRef dest = CGImageDestinationCreateWithURL(path, type, 1, NULL); if(dest) { CGImageDestinationAddImage(dest, image, NULL); CGImageDestinationFinalize(dest); CFRelease(dest); } else error(_("QuartzBitmap_Output - unable to open file '%s'"), buf); } else if(CFStringCompare(scheme, CFSTR("clipboard"), 0) == 0) { /* clipboard output */ CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); CGImageDestinationRef dest = CGImageDestinationCreateWithData(data, type, 1, NULL); CGImageDestinationAddImage(dest, image, NULL); CGImageDestinationFinalize(dest); CFRelease(dest); PasteboardRef pb = NULL; if(PasteboardCreate(kPasteboardClipboard, &pb) == noErr) { PasteboardClear(pb); PasteboardSynchronize(pb); PasteboardPutItemFlavor(pb, (PasteboardItemID) 1, type, data, 0); } CFRelease(data); } else warning(_("not a supported scheme, no image data written")); CFRelease(scheme); CFRelease(type); CFRelease(path); CFRelease(image); #endif } }
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)); } } }
int QuartzWindow::put_scrap_text(const char* s, int len) { // See Pasteboard Manager Programming guide PasteboardRef clipboard; OSStatus err; CFDataRef textData = CFDataCreate(kCFAllocatorDefault, (const UInt8*)s, len); if (textData == NULL) return -1; if ((err = PasteboardCreate(kPasteboardClipboard, &clipboard)) != noErr) return err; if ((err = PasteboardClear(clipboard)) != noErr) return err; return PasteboardPutItemFlavor(clipboard, (PasteboardItemID)s, kUTTypeOldMacText, textData, kPasteboardFlavorNoFlags); }
void _appleputsnarf(char *s) { CFDataRef cfdata; PasteboardSyncFlags flags; /* fprint(2, "appleputsnarf\n"); */ if(strlen(s) >= SnarfSize) return; qlock(&clip.lk); strcpy(clip.buf, s); runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s); if(clip.apple == nil){ if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){ fprint(2, "apple pasteboard create failed\n"); qunlock(&clip.lk); return; } } if(PasteboardClear(clip.apple) != noErr){ fprint(2, "apple pasteboard clear failed\n"); qunlock(&clip.lk); return; } flags = PasteboardSynchronize(clip.apple); if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){ fprint(2, "apple pasteboard cannot assert ownership\n"); qunlock(&clip.lk); return; } cfdata = CFDataCreate(kCFAllocatorDefault, (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2); if(cfdata == nil){ fprint(2, "apple pasteboard cfdatacreate failed\n"); qunlock(&clip.lk); return; } if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1, CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){ fprint(2, "apple pasteboard putitem failed\n"); CFRelease(cfdata); qunlock(&clip.lk); return; } /* CFRelease(cfdata); ??? */ qunlock(&clip.lk); }
void copy_to_clipboard(const char* text) { OSStatus err = noErr; PasteboardRef gClipboard; CFDataRef textData; err = PasteboardCreate( kPasteboardClipboard, &gClipboard ); err = PasteboardClear( gClipboard ); // allocate data based on the size of the selection textData = CFDataCreate( kCFAllocatorSystemDefault, (UInt8*)text, strlen(text)); // add text data to the pasteboard err = PasteboardPutItemFlavor( gClipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), textData, 0 ); CFRelease(textData); CFRelease( gClipboard ); }
void clipboard_push_text(char * text) { #ifdef MACOSX PasteboardRef newclipboard; if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return; if (PasteboardClear(newclipboard)!=noErr) return; PasteboardSynchronize(newclipboard); CFDataRef data = CFDataCreate(kCFAllocatorDefault, text, strlen(text)); PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0); #elif defined WIN32 if (OpenClipboard(NULL)) { HGLOBAL cbuffer; char * glbuffer; EmptyClipboard(); cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1); glbuffer = (char*)GlobalLock(cbuffer); strcpy(glbuffer, text); GlobalUnlock(cbuffer); SetClipboardData(CF_TEXT, cbuffer); CloseClipboard(); } #elif (defined(LIN32) || defined(LIN64)) && defined(SDL_VIDEO_DRIVER_X11) if (clipboard_text!=NULL) { free(clipboard_text); clipboard_text = NULL; } clipboard_text = mystrdup(text); sdl_wminfo.info.x11.lock_func(); XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime); XFlush(sdl_wminfo.info.x11.display); sdl_wminfo.info.x11.unlock_func(); #else printf("Not implemented: put text on clipboard \"%s\"\n", text); #endif }
void osx_driver_setclipboard_loop( struct ts_display_t *d, ts_clipboard_p clipboard) { if (!clip) PasteboardCreate(kPasteboardClipboard, &clip); if (!clipboard->flavorCount) return; for (int i = 0; i < clipboard->flavorCount; i++) { if (!strcmp(clipboard->flavor[i].name, "text")) { V1("%s adding %d bytes of %s\n", __func__, (int)clipboard->flavor[i].size, clipboard->flavor[i].name); if (PasteboardClear(clip) != noErr) { V1("apple pasteboard clear failed"); return; } PasteboardSyncFlags flags = PasteboardSynchronize(clip); if ((flags & kPasteboardModified) || !(flags & kPasteboardClientIsOwner)) { V1("apple pasteboard cannot assert ownership"); return; } CFDataRef cfdata = CFDataCreate(kCFAllocatorDefault, (uint8_t*)clipboard->flavor[i].data, clipboard->flavor[i].size); if (cfdata == nil) { V1("apple pasteboard cfdatacreate failed"); return; } if (PasteboardPutItemFlavor(clip, (PasteboardItemID) 1, CFSTR("public.utf8-plain-text"), cfdata, 0) != noErr) { V1("apple pasteboard putitem failed"); CFRelease(cfdata); } CFRelease(cfdata); return; } } }
void TCOD_sys_clipboard_set(const char *value) { PasteboardRef clipboard; if (PasteboardCreate(kPasteboardClipboard, &clipboard) != noErr) return; if (PasteboardClear(clipboard) != noErr) { CFRelease(clipboard); return; } size_t len = strlen(value); CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)value, len, kCFAllocatorNull); if (data == NULL) { CFRelease(clipboard); return; } OSStatus err; err = PasteboardPutItemFlavor(clipboard, NULL, kUTTypePlainText, data, 0); CFRelease(clipboard); CFRelease(data); }
/** * Write clipboard content to the host clipboard from the internal clipboard * structure. * * @param pPasteboardRef Reference to the global pasteboard. * @param pv The source buffer. * @param cb The size of the source buffer. * @param fFormats The format type which should be written. * * @returns IPRT status code. */ int writeToPasteboard(PasteboardRef pPasteboard, void *pv, uint32_t cb, uint32_t fFormat) { Log(("writeToPasteboard: fFormat = %02X\n", fFormat)); /* Clear the pasteboard */ if (PasteboardClear(pPasteboard)) return VERR_NOT_SUPPORTED; /* Make sure all is in sync */ PasteboardSynchronize(pPasteboard); int rc = VERR_NOT_SUPPORTED; /* Handle the unicode text */ if (fFormat & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT) { PRTUTF16 pwszSrcText = static_cast <PRTUTF16>(pv); size_t cwSrc = cb / 2; size_t cwDest = 0; /* How long will the converted text be? */ rc = vboxClipboardUtf16GetLinSize(pwszSrcText, cwSrc, &cwDest); if (RT_FAILURE(rc)) { Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16GetLinSize returned %Rrc. Abandoning.\n", rc)); AssertRCReturn(rc, rc); } /* Empty clipboard? Not critical */ if (cwDest == 0) { Log(("writeToPasteboard: received empty clipboard data from the guest, returning false.\n")); return VINF_SUCCESS; } /* Allocate the necessary memory */ PRTUTF16 pwszDestText = static_cast <PRTUTF16>(RTMemAlloc(cwDest * 2)); if (pwszDestText == NULL) { Log(("writeToPasteboard: failed to allocate %d bytes\n", cwDest * 2)); return VERR_NO_MEMORY; } /* Convert the EOL */ rc = vboxClipboardUtf16WinToLin(pwszSrcText, cwSrc, pwszDestText, cwDest); if (RT_FAILURE(rc)) { Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Rrc. Abandoning.\n", rc)); RTMemFree(pwszDestText); AssertRCReturn(rc, rc); } CFDataRef textData = NULL; /* Item id is 1. Nothing special here. */ PasteboardItemID itemId = (PasteboardItemID)1; /* Create a CData object which we could pass to the pasteboard */ if ((textData = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8*>(pwszDestText), cwDest * 2))) { /* Put the Utf-16 version to the pasteboard */ PasteboardPutItemFlavor(pPasteboard, itemId, kUTTypeUTF16PlainText, textData, 0); } /* Create a Utf-8 version */ char *pszDestText; rc = RTUtf16ToUtf8(pwszDestText, &pszDestText); if (RT_SUCCESS(rc)) { /* Create a CData object which we could pass to the pasteboard */ if ((textData = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8*>(pszDestText), strlen(pszDestText)))) { /* Put the Utf-8 version to the pasteboard */ PasteboardPutItemFlavor(pPasteboard, itemId, kUTTypeUTF8PlainText, textData, 0); } RTStrFree(pszDestText); } RTMemFree(pwszDestText); rc = VINF_SUCCESS; } /* Handle the bitmap */ else if (fFormat & VBOX_SHARED_CLIPBOARD_FMT_BITMAP) { /* Create a full BMP from it */ void *pBmp; size_t cbBmpSize; CFDataRef bmpData = NULL; /* Item id is 1. Nothing special here. */ PasteboardItemID itemId = (PasteboardItemID)1; rc = vboxClipboardDibToBmp(pv, cb, &pBmp, &cbBmpSize); if (RT_SUCCESS(rc)) { /* Create a CData object which we could pass to the pasteboard */ if ((bmpData = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8*>(pBmp), cbBmpSize))) { /* Put the Utf-8 version to the pasteboard */ PasteboardPutItemFlavor(pPasteboard, itemId, kUTTypeBMP, bmpData, 0); } RTMemFree(pBmp); } rc = VINF_SUCCESS; } else rc = VERR_NOT_IMPLEMENTED; Log(("writeToPasteboard: rc = %02X\n", rc)); return rc; }
void wxDataObject::AddToPasteboard( void * pb, int itemID ) { PasteboardRef pasteboard = (PasteboardRef) pb; // get formats from wxDataObjects wxDataFormat *array = new wxDataFormat[ GetFormatCount() ]; GetAllFormats( array ); for (size_t i = 0; i < GetFormatCount(); i++) { wxDataFormat thisFormat = array[ i ]; // add four bytes at the end for data objs like text that // have a datasize = strlen but still need a buffer for the // string including trailing zero size_t datasize = GetDataSize( thisFormat ); size_t sz = datasize + 4; void* buf = malloc( sz ); if ( buf != NULL ) { // empty the buffer because in some case GetDataHere does not fill buf memset( buf, 0, sz ); if ( GetDataHere( array[ i ], buf ) ) { int counter = 1 ; if ( thisFormat.GetType() == wxDF_FILENAME ) { // the data is D-normalized UTF8 strings of filenames delimited with \n char *fname = strtok((char*) buf,"\n"); while (fname != NULL) { // translate the filepath into a fileurl and put that into the pasteobard CFStringRef path = CFStringCreateWithBytes(NULL,(UInt8*)fname,strlen(fname),kCFStringEncodingUTF8,false); CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path , kCFURLPOSIXPathStyle, false); CFRelease(path); CFDataRef data = CFURLCreateData(NULL,url,kCFStringEncodingUTF8,true); CFRelease(url); PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) counter, (CFStringRef) thisFormat.GetFormatId() , data, kPasteboardFlavorNoFlags); CFRelease( data ); counter++; fname = strtok (NULL,"\n"); } } else { CFDataRef data = CFDataCreate( kCFAllocatorDefault, (UInt8*)buf, datasize ); if ( thisFormat.GetType() == wxDF_TEXT ) PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) itemID, CFSTR("com.apple.traditional-mac-plain-text") , data, kPasteboardFlavorNoFlags); else PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) itemID, (CFStringRef) thisFormat.GetFormatId() , data, kPasteboardFlavorNoFlags); CFRelease( data ); } } free( buf ); } } delete [] array; }