drag&drop + copy&paste

So… here is a function (for mac) for copy & paste text to & from the clipboard

greetings ascorbin

  
  
char*  getTextFromPasteboard() {  
  
	OSStatus             err = noErr;  
    ItemCount            itemCount;  
	PasteboardSyncFlags  syncFlags;  
	static PasteboardRef inPasteboard = NULL;  
	PasteboardCreate( kPasteboardClipboard, &inPasteboard );  
	char* data;  
	data = "";  
      
	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 data;  
}  
  
  
static OSStatus setTextToPasteboard(char* byteArrayIndex) {  
   
	OSStatus                err = noErr;  
	static PasteboardRef	pasteboard = NULL;  
	PasteboardCreate( kPasteboardClipboard, &pasteboard );  
      
	err = PasteboardClear( pasteboard );  
    require_noerr( err, PasteboardClear_FAILED );  
	  
	CFDataRef  data;  
  
	data = CFDataCreate(kCFAllocatorDefault, (uchar_p*) byteArrayIndex, strlen(byteArrayIndex)+1);  
	err = PasteboardPutItemFlavor( pasteboard, (PasteboardItemID)1, kUTTypeUTF8PlainText, data, 0);   
	require_noerr( err, PasteboardPutItemFlavor_FAILED );  
      
	PasteboardPutItemFlavor_FAILED:	  
	PasteboardClear_FAILED:  
    return err;  
}  
  

  
void testApp::keyReleased(int key){  
  
	if(key=='c'){  
		setTextToPasteboard("copy this to clipboard");  
	}	  
	if(key=='v'){  
		message = getTextFromPasteboard();  
	}				    
}  

Any chances of a Win32, X-Windows/Linux, or iPhone OS 3.x version? I know that (without jailbreaking and installing Clippy or another clipboard program) the iPhone OS 1.x/2.x just doesn’t have an OS-wide copy-paste functionality. (However, a Google search might find you the Clippy API if you want to go that route.) This would be a nice little addon for oF.

Just in case someone is looking for copy and paste text from clipboard functionality on windows, you can use something like:

  
#include <windows.h>  
...  
bool testApp::copyClipBoardText(string text){  
    bool ok = false;  
    if (OpenClipboard(NULL)){  
        EmptyClipboard();  
        HGLOBAL hClipboardData;  
        hClipboardData = GlobalAlloc(GMEM_DDESHARE, text.size()+1);  
        char * pchData = (char*)GlobalLock(hClipboardData);  
        strcpy(pchData, text.c_str());  
        GlobalUnlock(hClipboardData);  
        SetClipboardData(CF_TEXT,hClipboardData);  
        CloseClipboard();  
        ok = true;  
    }  
    return ok;  
}  
  
string testApp::pasteClipBoardText(){  
    string clipBoardText = "";  
    if (OpenClipboard(NULL)){  
        HANDLE hClipboardData = GetClipboardData(CF_TEXT);  
        if(IsClipboardFormatAvailable(CF_TEXT)){  
            char *pchData = (char*)GlobalLock(hClipboardData);  
            clipBoardText.assign(pchData);  
            GlobalUnlock(hClipboardData);  
            CloseClipboard();  
        }  
    }  
    return clipBoardText;  
}  

You could also use ofxFenster. It provides drag and drop, as well as clipboard copy and paste (only text at the moment, images are planed) on linux, mac and windows.

https://github.com/underdoeg/ofxFenster/
https://github.com/underdoeg/ofxFenster/blob/master/example/src/testApp.cpp#L99
https://github.com/underdoeg/ofxFenster/blob/master/example/src/testApp.cpp#L145

Hey thanks! Am about to try ofxFenster out for a project I’m doing right now…been meaning to for a while.