コード例 #1
0
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);
	}
}
コード例 #2
0
ファイル: Picture.cpp プロジェクト: DsRQuicke/praat
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);
}
コード例 #3
0
ファイル: mac-dialog.cpp プロジェクト: OV2/snes9x-libsnes
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);
	}
}
コード例 #4
0
ファイル: clipboard.cpp プロジェクト: Coffee--/wesnoth-old
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);
}
コード例 #5
0
ファイル: misc.c プロジェクト: 321boom/The-Powder-Toy
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
}
コード例 #6
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;
}
コード例 #7
0
ファイル: ClipBoard.cpp プロジェクト: decebel/dataAtom_alpha
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	
}
コード例 #8
0
ファイル: Clipboard.cpp プロジェクト: binji/drod-nacl
//******************************************************************************
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
}
コード例 #9
0
void wxClipboard::Clear()
{
    wxDELETE(m_data);

    OSStatus err = PasteboardClear( m_pasteboard );
    if (err != noErr)
    {
        wxLogSysError( wxT("Failed to empty the clipboard.") );
    }
}
コード例 #10
0
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
}
コード例 #11
0
ファイル: qdBitmap.c プロジェクト: Bgods/r-source
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
    }
}
コード例 #12
0
ファイル: quartzWindow.cpp プロジェクト: ardeujho/self
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);
  
}
コード例 #13
0
	bool
COSXClipboard::empty()
{
	LOG((CLOG_DEBUG "emptying clipboard"));
	if (m_pboard == NULL)
		return false;

	OSStatus err = PasteboardClear(m_pboard);
	if (err != noErr) {
		LOG((CLOG_DEBUG "failed to clear clipboard: error %i", err));
		return false;
	}

	return true;
}
コード例 #14
0
bool
COSXClipboard::empty()
{
	LOG((CLOG_DEBUG "empty clipboard"));
	assert(m_pboard != NULL);

	OSStatus err = PasteboardClear(m_pboard);
	if (err != noErr) {
		LOG((CLOG_DEBUG "failed to grab clipboard"));
		return false;
	}


	return true;
}
コード例 #15
0
ファイル: x11-itrans.c プロジェクト: 00001/plan9port
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);
}
コード例 #16
0
ファイル: paste.c プロジェクト: Sir-Odie/Eternal-Lands
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 );
}
コード例 #17
0
ファイル: misc.c プロジェクト: syrus448/The-Powder-Toy
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
}
コード例 #18
0
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;
		}
	}
}
コード例 #19
0
ファイル: sys_c.c プロジェクト: chilamkatana/ASCIIWar
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);
}
コード例 #20
0
ファイル: qclipboard_mac.cpp プロジェクト: Afreeca/qt
void QMacPasteboard::clear_helper()
{
    if (paste)
        PasteboardClear(paste);
    promises.clear();
}
コード例 #21
0
ファイル: dnd.cpp プロジェクト: BloodRedd/gamekit
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;
}
コード例 #22
0
/**
 * 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;
}