/** * 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; }
/** * Inspect the global pasteboard for new content. Check if there is some type * that is supported by vbox and return it. * * @param pPasteboardRef Reference to the global pasteboard. * @param pfFormats Pointer for the bit combination of the * supported types. * @param pbChanged True if something has changed after the * last call. * * @returns IPRT status code. (Always VINF_SUCCESS atm.) */ int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged) { Log(("queryNewPasteboardFormats\n")); OSStatus err = noErr; *pfChanged = true; PasteboardSyncFlags syncFlags; /* Make sure all is in sync */ syncFlags = PasteboardSynchronize(pPasteboard); /* If nothing changed return */ if (!(syncFlags & kPasteboardModified)) { *pfChanged = false; return VINF_SUCCESS; } /* Are some items in the pasteboard? */ ItemCount itemCount; err = PasteboardGetItemCount(pPasteboard, &itemCount); if (itemCount < 1) return VINF_SUCCESS; /* The id of the first element in the pasteboard */ int rc = VINF_SUCCESS; PasteboardItemID itemID; if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID))) { /* Retrieve all flavors in the pasteboard, maybe there * is something we can use. */ CFArrayRef flavorTypeArray; if (!(err = PasteboardCopyItemFlavors(pPasteboard, itemID, &flavorTypeArray))) { CFIndex flavorCount; flavorCount = CFArrayGetCount(flavorTypeArray); for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++) { CFStringRef flavorType; flavorType = static_cast <CFStringRef>(CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex)); /* Currently only unicode supported */ if (UTTypeConformsTo(flavorType, kUTTypeUTF8PlainText) || UTTypeConformsTo(flavorType, kUTTypeUTF16PlainText)) { Log(("Unicode flavor detected.\n")); *pfFormats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT; } else if (UTTypeConformsTo(flavorType, kUTTypeBMP)) { Log(("BMP flavor detected.\n")); *pfFormats |= VBOX_SHARED_CLIPBOARD_FMT_BITMAP; } } CFRelease(flavorTypeArray); } } Log(("queryNewPasteboardFormats: rc = %02X\n", rc)); return rc; }
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); } }
char* _applegetsnarf(void) { char *s, *t; CFArrayRef flavors; CFDataRef data; CFIndex nflavor, ndata, j; CFStringRef type; ItemCount nitem; PasteboardItemID id; PasteboardSyncFlags flags; UInt32 i; /* fprint(2, "applegetsnarf\n"); */ qlock(&clip.lk); if(clip.apple == nil){ if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){ fprint(2, "apple pasteboard create failed\n"); qunlock(&clip.lk); return nil; } } flags = PasteboardSynchronize(clip.apple); if(flags&kPasteboardClientIsOwner){ s = strdup(clip.buf); qunlock(&clip.lk); return s; } if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){ fprint(2, "apple pasteboard get item count failed\n"); qunlock(&clip.lk); return nil; } for(i=1; i<=nitem; i++){ if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr) continue; if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr) continue; nflavor = CFArrayGetCount(flavors); for(j=0; j<nflavor; j++){ type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j); if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text"))) continue; if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr) continue; ndata = CFDataGetLength(data); qunlock(&clip.lk); s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data)); CFRelease(flavors); CFRelease(data); for(t=s; *t; t++) if(*t == '\r') *t = '\n'; return s; } CFRelease(flavors); } qunlock(&clip.lk); return nil; }
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 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); }
COSXClipboard::COSXClipboard() : m_time(0), m_pboard(NULL) { m_converters.push_back(new COSXClipboardHTMLConverter); m_converters.push_back(new COSXClipboardBMPConverter); m_converters.push_back(new COSXClipboardUTF16Converter); m_converters.push_back(new COSXClipboardTextConverter); OSStatus createErr = PasteboardCreate(kPasteboardClipboard, &m_pboard); if (createErr != noErr) { LOG((CLOG_DEBUG "failed to create clipboard reference: error %i", createErr)); LOG((CLOG_ERR "unable to connect to pasteboard, clipboard sharing disabled", createErr)); m_pboard = NULL; return; } OSStatus syncErr = PasteboardSynchronize(m_pboard); if (syncErr != noErr) { LOG((CLOG_DEBUG "failed to syncronize clipboard: error %i", syncErr)); } }
bool wxDataObject::HasDataInPasteboard( void * pb ) { PasteboardRef pasteboard = (PasteboardRef) pb; size_t formatcount = GetFormatCount() + 1; wxDataFormat *array = new wxDataFormat[ formatcount ]; array[0] = GetPreferredFormat(); GetAllFormats( &array[1] ); ItemCount itemCount = 0; bool hasData = false; // we synchronize here once again, so we don't mind which flags get returned PasteboardSynchronize( pasteboard ); OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); if ( err == noErr ) { for (size_t i = 0; !hasData && i < formatcount; i++) { // go through the data in our order of preference wxDataFormat dataFormat = array[ i ]; for( UInt32 itemIndex = 1; itemIndex <= itemCount && hasData == false ; itemIndex++ ) { PasteboardItemID itemID = 0; CFArrayRef flavorTypeArray = NULL; CFIndex flavorCount = 0; err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); if ( err != noErr ) continue; err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); if ( err != noErr ) continue; flavorCount = CFArrayGetCount( flavorTypeArray ); for( CFIndex flavorIndex = 0; !hasData && flavorIndex < flavorCount ; flavorIndex++ ) { CFStringRef flavorType; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, flavorIndex ); wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); if ( dataFormat == flavorFormat || dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) { hasData = true; } } CFRelease( flavorTypeArray ); } } } return hasData; }
COSXClipboard::COSXClipboard() : m_time(0), m_pboard(NULL) { m_converters.push_back(new COSXClipboardUTF16Converter); m_converters.push_back(new COSXClipboardTextConverter); PasteboardCreate( kPasteboardClipboard, &m_pboard); PasteboardSynchronize(m_pboard); }
//****************************************************************************** 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 }
bool COSXClipboard::synchronize() { PasteboardSyncFlags flags = PasteboardSynchronize(m_pboard); LOG((CLOG_DEBUG "flags: %x", flags)); if (flags & kPasteboardModified) { return true; } return false; }
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 } }
bool wxDataObject::IsFormatInPasteboard( void * pb, const wxDataFormat &dataFormat ) { PasteboardRef pasteboard = (PasteboardRef) pb; bool hasData = false; OSStatus err = noErr; ItemCount itemCount; // we synchronize here once again, so we don't mind which flags get returned PasteboardSynchronize( pasteboard ); err = PasteboardGetItemCount( pasteboard, &itemCount ); if ( err == noErr ) { for( UInt32 itemIndex = 1; itemIndex <= itemCount && hasData == false ; itemIndex++ ) { PasteboardItemID itemID; CFArrayRef flavorTypeArray; CFIndex flavorCount; err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); if ( err != noErr ) continue; err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); if ( err != noErr ) continue; flavorCount = CFArrayGetCount( flavorTypeArray ); for( CFIndex flavorIndex = 0; flavorIndex < flavorCount && hasData == false ; flavorIndex++ ) { CFStringRef flavorType; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, flavorIndex ); wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); if ( dataFormat == flavorFormat ) hasData = true; else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) hasData = true; } CFRelease (flavorTypeArray); } } return hasData; }
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); }
bool QMacPasteboard::sync() const { if (!paste) return false; const bool fromGlobal = PasteboardSynchronize(paste) & kPasteboardModified; if (fromGlobal) const_cast<QMacPasteboard *>(this)->setMimeData(0); #ifdef DEBUG_PASTEBOARD if(fromGlobal) qDebug("Pasteboard: Synchronize!"); #endif return fromGlobal; }
std::string copy_from_clipboard(const bool) { OSStatus err = noErr; PasteboardRef clipboard; PasteboardSyncFlags syncFlags; ItemCount count; err = PasteboardCreate(kPasteboardClipboard, &clipboard); if (err != noErr) return ""; syncFlags = PasteboardSynchronize(clipboard); if (syncFlags&kPasteboardModified) return ""; err = PasteboardGetItemCount(clipboard, &count); if (err != noErr) return ""; for (UInt32 k = 1; k <= count; k++) { PasteboardItemID itemID; CFArrayRef flavorTypeArray; CFIndex flavorCount; err = PasteboardGetItemIdentifier(clipboard, k, &itemID); if (err != noErr) return ""; err = PasteboardCopyItemFlavors(clipboard, itemID, &flavorTypeArray); if (err != noErr) return ""; flavorCount = CFArrayGetCount(flavorTypeArray); for (CFIndex j = 0; j < flavorCount; j++) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; flavorType = (CFStringRef)CFArrayGetValueAtIndex(flavorTypeArray, j); if (UTTypeConformsTo(flavorType, CFSTR("public.utf8-plain-text"))) { err = PasteboardCopyItemFlavorData(clipboard, itemID, flavorType, &flavorData); if (err != noErr) { CFRelease(flavorTypeArray); return ""; } flavorDataSize = CFDataGetLength(flavorData); std::string str; str.reserve(flavorDataSize); str.resize(flavorDataSize); CFDataGetBytes(flavorData, CFRangeMake(0, flavorDataSize), (UInt8 *)str.data()); for (unsigned int i = 0; i < str.size(); ++i) { if (str[i] == '\r') str[i] = '\n'; } return str; } } } return ""; }
char *TCOD_sys_clipboard_get() { PasteboardSyncFlags syncFlags; ItemCount itemCount; PasteboardRef clipboard; UInt32 itemIndex; if (PasteboardCreate(kPasteboardClipboard, &clipboard) != noErr) return NULL; syncFlags = PasteboardSynchronize(clipboard); if (PasteboardGetItemCount(clipboard, &itemCount) != noErr) return NULL; if (itemCount == 0) return NULL; for(itemIndex = 1; itemIndex <= itemCount; itemIndex++) { PasteboardItemID itemID; CFArrayRef flavorTypeArray; CFIndex flavorCount; CFIndex flavorIndex; if (PasteboardGetItemIdentifier(clipboard, itemIndex, &itemID ) != noErr) return NULL; if (PasteboardCopyItemFlavors(clipboard, itemID, &flavorTypeArray) != noErr) return NULL; flavorCount = CFArrayGetCount(flavorTypeArray); for(flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; flavorType = (CFStringRef)CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex); if (UTTypeConformsTo(flavorType, CFSTR("public.plain-text"))) { if (PasteboardCopyItemFlavorData(clipboard, itemID, flavorType, &flavorData) != noErr) { CFRelease(flavorData); return NULL; } flavorDataSize = CFDataGetLength( flavorData ); flavorDataSize = (flavorDataSize<254) ? flavorDataSize : 254; short dataIndex; for(dataIndex = 0; dataIndex <= flavorDataSize; dataIndex++) { clipboardText[dataIndex] = *(CFDataGetBytePtr(flavorData) + dataIndex); } clipboardText[flavorDataSize] = '\0'; clipboardText[flavorDataSize+1] = '\n'; CFRelease (flavorData); } } } return clipboardText; }
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; } } }
string ClipBoard::getText() { #ifdef TARGET_OSX OSStatus err = noErr; ItemCount itemCount; PasteboardSyncFlags syncFlags; static PasteboardRef inPasteboard = NULL; PasteboardCreate( kPasteboardClipboard, &inPasteboard ); char* data; data = NULL; syncFlags = PasteboardSynchronize( inPasteboard ); err = badPasteboardSyncErr; err = PasteboardGetItemCount( inPasteboard, &itemCount ); require_noerr( err, CantGetPasteboardItemCount ); for( int itemIndex = 1; itemIndex <= itemCount; itemIndex++ ) { PasteboardItemID itemID; CFDataRef flavorData; err = PasteboardGetItemIdentifier( inPasteboard, itemIndex, &itemID ); require_noerr( err, CantGetPasteboardItemIdentifier ); err = PasteboardCopyItemFlavorData( inPasteboard, itemID, CFSTR("public.utf8-plain-text"), &flavorData ); data = (char*)CFDataGetBytePtr(flavorData); CantGetPasteboardItemIdentifier: ; } CantGetPasteboardItemCount: return (string) data; #else ofLog(OF_LOG_WARNING, "ofxGLEditor: sorry, copying to the system clipboard is not supported on your OS yet"); return ""; #endif }
bool wxClipboard::AddData( wxDataObject *data ) { if ( IsUsingPrimarySelection() ) return false; wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); wxCHECK_MSG( data, false, wxT("data is invalid") ); // we can only store one wxDataObject Clear(); PasteboardSyncFlags syncFlags = PasteboardSynchronize( m_pasteboard ); wxCHECK_MSG( !(syncFlags&kPasteboardModified), false, wxT("clipboard modified after clear") ); wxCHECK_MSG( (syncFlags&kPasteboardClientIsOwner), false, wxT("client couldn't own clipboard") ); m_data = data; data->AddToPasteboard( m_pasteboard, 1 ); return true; }
GHOST_TUns8 *GHOST_SystemCarbon::getClipboard(bool selection) const { PasteboardRef inPasteboard; PasteboardItemID itemID; CFDataRef flavorData; OSStatus err = noErr; GHOST_TUns8 *temp_buff; CFRange range; OSStatus syncFlags; err = PasteboardCreate(kPasteboardClipboard, &inPasteboard); if (err != noErr) { return NULL; } syncFlags = PasteboardSynchronize(inPasteboard); /* as we always get in a new string, we can safely ignore sync flags if not an error*/ if (syncFlags < 0) { return NULL; } err = PasteboardGetItemIdentifier(inPasteboard, 1, &itemID); if (err != noErr) { return NULL; } err = PasteboardCopyItemFlavorData(inPasteboard, itemID, CFSTR("public.utf8-plain-text"), &flavorData); if (err != noErr) { return NULL; } range = CFRangeMake(0, CFDataGetLength(flavorData)); temp_buff = (GHOST_TUns8 *) malloc(range.length + 1); CFDataGetBytes(flavorData, range, (UInt8 *)temp_buff); temp_buff[range.length] = '\0'; if (temp_buff) { return temp_buff; } else { return NULL; } }
wxDragResult wxDropSource::DoDragDrop(int flags) { wxASSERT_MSG( m_data, wxT("Drop source: no data") ); if ((m_data == NULL) || (m_data->GetFormatCount() == 0)) return (wxDragResult)wxDragNone; DragReference theDrag; RgnHandle dragRegion; OSStatus err = noErr; PasteboardRef pasteboard; // add data to drag err = PasteboardCreate( kPasteboardUniqueName, &pasteboard ); if ( err != noErr ) return wxDragNone; // we add a dummy promise keeper because of strange messages when linking against carbon debug err = PasteboardSetPromiseKeeper( pasteboard, wxMacPromiseKeeper, this ); if ( err != noErr ) { CFRelease( pasteboard ); return wxDragNone; } err = PasteboardClear( pasteboard ); if ( err != noErr ) { CFRelease( pasteboard ); return wxDragNone; } PasteboardSynchronize( pasteboard ); m_data->AddToPasteboard( pasteboard, 1 ); if (NewDragWithPasteboard( pasteboard , &theDrag) != noErr) { CFRelease( pasteboard ); return wxDragNone; } dragRegion = NewRgn(); RgnHandle tempRgn = NewRgn(); EventRecord rec; ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent(), &rec ); const short dragRegionOuterBoundary = 10; const short dragRegionInnerBoundary = 9; SetRectRgn( dragRegion, rec.where.h - dragRegionOuterBoundary, rec.where.v - dragRegionOuterBoundary, rec.where.h + dragRegionOuterBoundary, rec.where.v + dragRegionOuterBoundary ); SetRectRgn( tempRgn, rec.where.h - dragRegionInnerBoundary, rec.where.v - dragRegionInnerBoundary, rec.where.h + dragRegionInnerBoundary, rec.where.v + dragRegionInnerBoundary ); DiffRgn( dragRegion, tempRgn, dragRegion ); DisposeRgn( tempRgn ); // TODO: work with promises in order to return data // only when drag was successfully completed gTrackingGlobals.m_currentSource = this; gTrackingGlobals.m_result = wxDragNone; gTrackingGlobals.m_flags = flags; err = TrackDrag( theDrag, &rec, dragRegion ); DisposeRgn( dragRegion ); DisposeDrag( theDrag ); CFRelease( pasteboard ); gTrackingGlobals.m_currentSource = NULL; return gTrackingGlobals.m_result; }
char *osd_get_clipboard_text(void) { OSStatus err; PasteboardRef pasteboard_ref; err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref); if (err) return nullptr; PasteboardSynchronize(pasteboard_ref); ItemCount item_count; err = PasteboardGetItemCount(pasteboard_ref, &item_count); char *result = nullptr; // core expects a malloced C string of uft8 data for (UInt32 item_index = 1; (item_index <= item_count) && !result; item_index++) { PasteboardItemID item_id; err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id); if (err) continue; CFArrayRef flavor_type_array; err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array); if (err) continue; CFIndex const flavor_count = CFArrayGetCount(flavor_type_array); for (CFIndex flavor_index = 0; (flavor_index < flavor_count) && !result; flavor_index++) { CFStringRef const flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index); CFStringEncoding encoding; if (UTTypeConformsTo(flavor_type, kUTTypeUTF16PlainText)) encoding = kCFStringEncodingUTF16; else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText)) encoding = kCFStringEncodingUTF8; else if (UTTypeConformsTo (flavor_type, kUTTypePlainText)) encoding = kCFStringEncodingMacRoman; else continue; CFDataRef flavor_data; err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data); if (!err) { CFStringRef string_ref = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, flavor_data, encoding); CFDataRef data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?'); CFRelease(string_ref); CFRelease(flavor_data); CFIndex const length = CFDataGetLength(data_ref); CFRange const range = CFRangeMake(0, length); result = reinterpret_cast<char *>(malloc(length + 1)); if (result) { CFDataGetBytes(data_ref, range, reinterpret_cast<unsigned char *>(result)); result[length] = 0; } CFRelease(data_ref); } } CFRelease(flavor_type_array); } CFRelease(pasteboard_ref); return result; }
void g2LabelEdit::PasteBuffer() { // Win32 implementation #ifdef _WIN32 // Attempt to open clipboard if(!OpenClipboard(GetForegroundWindow())) return; // Get the windows clipboard text buffer HGLOBAL ClipboardHandle = GetClipboardData(CF_TEXT); if(ClipboardHandle != NULL) { // Actually copy the text LPTSTR StringLock = (LPTSTR)GlobalLock(ClipboardHandle); if (StringLock != NULL) { // Copy as much as we can char TempClipBuffer[g2LabelEdit_TextBufferLength]; strncpy(TempClipBuffer, StringLock, g2LabelEdit_TextBufferLength - strlen(TextBuffer) - strlen(StringLock) - 1); // Copy the current text buffer char TempTextBuffer[g2LabelEdit_TextBufferLength]; strcpy(TempTextBuffer, TextBuffer); // Copy into the full buffer (only if text is small enough) if(strlen(TempTextBuffer) + strlen(TempClipBuffer) + 1 < g2LabelEdit_TextBufferLength) { char NewTextBuffer[g2LabelEdit_TextBufferLength]; sprintf(NewTextBuffer, "%s%s", TempTextBuffer, TempClipBuffer); SetText(NewTextBuffer); } // Release the lock GlobalUnlock(StringLock); } } // 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; // Explicitly update (possibly not needed...) PasteboardSynchronize(ClipboardHandle); // Get the item count ItemCount ClipboardItems; if(PasteboardGetItemCount(ClipboardHandle, &ClipboardItems) != noErr) return; // Keep searching until we find valid text for(ItemCount ItemIndex = 1; ItemIndex <= ClipboardItems; ItemIndex++) { // Get item's ID PasteboardItemID ItemID; if(PasteboardGetItemIdentifier(ClipboardHandle, ItemIndex, &ItemID) != noErr) continue; // Get this item's data types CFArrayRef ItemTypes; if(PasteboardCopyItemFlavors(ClipboardHandle, ItemID, &ItemTypes) != noErr) continue; // For each data type for this clipboard item CFIndex ItemCount = CFArrayGetCount(ItemTypes); for(CFIndex ItemTypeIndex = 0; ItemTypeIndex < ItemCount; ItemTypeIndex++) { // Get the data type CFStringRef ItemType = (CFStringRef)CFArrayGetValueAtIndex(ItemTypes, ItemTypeIndex); // If we have any text-type, then paste and stop if(UTTypeConformsTo(ItemType, CFSTR("public.utf8-plain-text"))) { // Copy from clipboard CFDataRef ItemData; if(PasteboardCopyItemFlavorData(ClipboardHandle, ItemID, ItemType, &ItemData) != noErr) continue; // Paste into active buffer CFIndex DateLength = CFDataGetLength(ItemData); size_t StartIndex = strlen(TextBuffer); char NewTempBuffer[g2LabelEdit_TextBufferLength]; strcpy(NewTempBuffer, TextBuffer); for(CFIndex i = 0; (i < DateLength) && (StartIndex + i < g2LabelEdit_TextBufferLength); i++) { char byte = *(CFDataGetBytePtr(ItemData) + i); NewTempBuffer[StartIndex + i] = byte; } // Cap string and set to current buffer NewTempBuffer[StartIndex + DateLength] = '\0'; SetText(NewTempBuffer); SetCursorPos((int)strlen(NewTempBuffer)); // Release CFRelease(ItemData); // Pasted and done! return; } } } // Linux clipboard implementation #elif __linux__ // Paste into UI from linux buffer SetText(__LinuxClipboard); SetCursorPos((int)strlen(__LinuxClipboard)); #endif }
oop QuartzWindow::get_scrap_text() { // See Pasteboard Manager Programming guide PasteboardRef clipboard; PasteboardSyncFlags syncFlags; CFDataRef textData = NULL; ItemCount itemCount; if ( PasteboardCreate(kPasteboardClipboard, &clipboard) != noErr || (PasteboardSynchronize(clipboard) & kPasteboardModified) || PasteboardGetItemCount(clipboard, &itemCount) != noErr ) return new_string("", 0); for( UInt32 itemIndex = 1; itemIndex <= itemCount; itemIndex++ ) { PasteboardItemID itemID = 0; CFArrayRef flavorTypeArray = NULL; CFIndex flavorCount = 0; if (PasteboardGetItemIdentifier(clipboard, itemIndex, &itemID) != noErr) continue; if (PasteboardCopyItemFlavors(clipboard, itemID, &flavorTypeArray) != noErr) continue; flavorCount = CFArrayGetCount(flavorTypeArray); for(CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; char flavorText[256]; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray,// 6 flavorIndex ); if (UTTypeConformsTo(flavorType, kUTTypeOldMacText)) { if (PasteboardCopyItemFlavorData(clipboard, itemID, flavorType, &flavorData) != noErr) continue; flavorDataSize = CFDataGetLength(flavorData); // allocate new string. byteVectorOop r = Memory->byteVectorObj->cloneSize(flavorDataSize, CANFAIL); if (r->is_mark()) { CFRelease (flavorData); CFRelease (flavorTypeArray); return new_string("", 0); } // copy over CFDataGetBytes(flavorData, CFRangeMake(0,CFDataGetLength(flavorData)), (UInt8 *)r->bytes()); CFRelease(flavorData); CFRelease(flavorTypeArray); return r; } // else try next } CFRelease(flavorTypeArray); } }
bool wxDataObject::GetFromPasteboard( void * pb ) { PasteboardRef pasteboard = (PasteboardRef) pb; size_t formatcount = GetFormatCount() + 1; wxDataFormat *array = new wxDataFormat[ formatcount ]; array[0] = GetPreferredFormat(); GetAllFormats( &array[1] ); ItemCount itemCount = 0; wxString filenamesPassed; bool transferred = false; // we synchronize here once again, so we don't mind which flags get returned PasteboardSynchronize( pasteboard ); OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); if ( err == noErr ) { for (size_t i = 0; !transferred && i < formatcount; i++) { // go through the data in our order of preference wxDataFormat dataFormat = array[ i ]; for( UInt32 itemIndex = 1; itemIndex <= itemCount && transferred == false ; itemIndex++ ) { PasteboardItemID itemID = 0; CFArrayRef flavorTypeArray = NULL; CFIndex flavorCount = 0; err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); if ( err != noErr ) continue; err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); if ( err != noErr ) continue; flavorCount = CFArrayGetCount( flavorTypeArray ); for( CFIndex flavorIndex = 0; !transferred && flavorIndex < flavorCount ; flavorIndex++ ) { CFStringRef flavorType; CFDataRef flavorData; CFIndex flavorDataSize; flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, flavorIndex ); // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion if ( UTTypeConformsTo(flavorType, CFSTR("public.utf8-plain-text") ) ) { flavorType = CFSTR("public.utf16-plain-text"); } wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); if ( dataFormat == flavorFormat ) { err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType , &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); if (dataFormat.GetType() == wxDF_FILENAME ) { // revert the translation and decomposition to arrive at a proper utf8 string again CFURLRef url = CFURLCreateWithBytes( kCFAllocatorDefault, CFDataGetBytePtr( flavorData ), flavorDataSize, kCFStringEncodingUTF8, NULL ); CFStringRef cfString = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ); CFRelease( url ); CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); CFRelease( cfString ); CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); wxString path = wxMacCFStringHolder(cfMutableString).AsString(); if (!path.empty()) filenamesPassed += path + wxT("\n"); } else { // because some data implementation expect trailing a trailing NUL, we add some headroom void *buf = malloc( flavorDataSize + 4 ); if ( buf ) { memset( buf, 0, flavorDataSize + 4 ); memcpy( buf, CFDataGetBytePtr( flavorData ), flavorDataSize ); if (dataFormat.GetType() == wxDF_TEXT) wxMacConvertNewlines10To13( (char*) buf ); SetData( flavorFormat, flavorDataSize, buf ); transferred = true; free( buf ); } } CFRelease (flavorData); } } else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) { err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType, &flavorData ); if ( err == noErr ) { flavorDataSize = CFDataGetLength( flavorData ); void *asciibuf = malloc( flavorDataSize + 1 ); if ( asciibuf ) { memset( asciibuf, 0, flavorDataSize + 1 ); memcpy( asciibuf, CFDataGetBytePtr( flavorData ), flavorDataSize ); CFRelease (flavorData); SetData( wxDF_TEXT, flavorDataSize, asciibuf ); transferred = true; free( asciibuf ); } else CFRelease (flavorData); } } } CFRelease( flavorTypeArray ); } if (filenamesPassed.length() > 0) { wxCharBuffer buf = filenamesPassed.fn_str(); SetData( wxDF_FILENAME, strlen( buf ), (const char*)buf ); transferred = true; } } } return transferred; }
char *osd_get_clipboard_text(void) { char *result = NULL; /* core expects a malloced C string of uft8 data */ PasteboardRef pasteboard_ref; OSStatus err; PasteboardSyncFlags sync_flags; PasteboardItemID item_id; CFIndex flavor_count; CFArrayRef flavor_type_array; CFIndex flavor_index; ItemCount item_count; UInt32 item_index; Boolean success = false; err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref); if (!err) { sync_flags = PasteboardSynchronize( pasteboard_ref ); err = PasteboardGetItemCount(pasteboard_ref, &item_count ); for (item_index=1; item_index<=item_count; item_index++) { err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id); if (!err) { err = PasteboardCopyItemFlavors(pasteboard_ref, item_id, &flavor_type_array); if (!err) { flavor_count = CFArrayGetCount(flavor_type_array); for (flavor_index = 0; flavor_index < flavor_count; flavor_index++) { CFStringRef flavor_type; CFDataRef flavor_data; CFStringEncoding encoding; CFStringRef string_ref; CFDataRef data_ref; CFIndex length; CFRange range; flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index); if (UTTypeConformsTo (flavor_type, kUTTypeUTF16PlainText)) encoding = kCFStringEncodingUTF16; else if (UTTypeConformsTo (flavor_type, kUTTypeUTF8PlainText)) encoding = kCFStringEncodingUTF8; else if (UTTypeConformsTo (flavor_type, kUTTypePlainText)) encoding = kCFStringEncodingMacRoman; else continue; err = PasteboardCopyItemFlavorData(pasteboard_ref, item_id, flavor_type, &flavor_data); if( !err ) { string_ref = CFStringCreateFromExternalRepresentation (kCFAllocatorDefault, flavor_data, encoding); data_ref = CFStringCreateExternalRepresentation (kCFAllocatorDefault, string_ref, kCFStringEncodingUTF8, '?'); length = CFDataGetLength (data_ref); range = CFRangeMake (0,length); result = (char *)malloc (length+1); if (result != NULL) { CFDataGetBytes (data_ref, range, (unsigned char *)result); result[length] = 0; success = true; break; } CFRelease(data_ref); CFRelease(string_ref); CFRelease(flavor_data); } } CFRelease(flavor_type_array); } } if (success) break; } CFRelease(pasteboard_ref); } return result; }