clipboard.cpp 3.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include "clipboard.h"

#if defined(_WIN32)
#include <windows.h>
bool  setTextToPasteboard(std::string as)
{
    
    size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0 );
    
    std::wstring text( reqLength, L'\0' );
    
    ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), &text[0], (int)text.length() );
    
    bool ok = false;
    if (OpenClipboard(NULL))
	{
        EmptyClipboard();
        HGLOBAL hClipboardData;
        size_t bytes = text.length()+1 * sizeof(wchar_t);
        
        hClipboardData = GlobalAlloc(GMEM_DDESHARE, bytes*2);
        wchar_t * pchData = (wchar_t*)GlobalLock(hClipboardData);
        
        wcscpy(pchData, text.c_str());
        
        GlobalUnlock(hClipboardData);
        SetClipboardData(CF_UNICODETEXT,hClipboardData);
        CloseClipboard();
        ok = true;
    }
    return ok;
}
std::string getTextFromPasteboard()
{
    std::string clipBoardText="";
    if (OpenClipboard(NULL))
	{
        HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
        if(IsClipboardFormatAvailable(CF_UNICODETEXT))
		{
            wchar_t * pszText =NULL;
            pszText = (wchar_t *)GlobalLock(hClipboardData);
            if (pszText == NULL)
			{
                
            }
			else
			{
                std::wstring  pchData = pszText;
                char * mbstr2 = new char[pchData.size()*4];
                size_t bytes = pchData.length()+1 * sizeof(wchar_t);
                WideCharToMultiByte(CP_UTF8,0,pchData.c_str(),bytes*2,mbstr2,bytes*2,NULL,NULL);
                clipBoardText.append(mbstr2);
				delete [] mbstr2;
            }
            GlobalUnlock(hClipboardData);
            CloseClipboard();
        }
    }
    return clipBoardText;
}
#elif defined(__APPLE__)

#include <Carbon/Carbon.h>
//AUTOFRAMEWORK(Carbon)


bool setTextToPasteboard(std::string str) 
{
    const char * byteArrayIndex = str.c_str();
    OSStatus err = noErr;
    static PasteboardRef	pasteboard = NULL;
    PasteboardCreate( kPasteboardClipboard, &pasteboard );
    
    err = PasteboardClear( pasteboard );
76
    //require_noerr( err, PasteboardClear_FAILED );
77 78 79 80 81 82
    
    CFDataRef data;
    
    data =   CFDataCreate(kCFAllocatorDefault, (UInt8*)byteArrayIndex, strlen(byteArrayIndex)+1);
    
    err = PasteboardPutItemFlavor( pasteboard, (PasteboardItemID)1, kUTTypeUTF8PlainText, data, 0);
83
    //require_noerr( err, PasteboardPutItemFlavor_FAILED );
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    
PasteboardPutItemFlavor_FAILED:
PasteboardClear_FAILED:
    return err == noErr;
}
std::string getTextFromPasteboard() 
{
    
    std::string clipBoard = "";
    OSStatus err = noErr;
    ItemCount  itemCount;
    PasteboardSyncFlags  syncFlags;
    static PasteboardRef inPasteboard = NULL;
    PasteboardCreate( kPasteboardClipboard, &inPasteboard );
    char* data = NULL;
    
    syncFlags = PasteboardSynchronize( inPasteboard );
    err = badPasteboardSyncErr;
    
    err = PasteboardGetItemCount( inPasteboard, &itemCount );
104
    //require_noerr( err, CantGetPasteboardItemCount );
105 106 107 108 109 110
    
    for( int itemIndex = 1; itemIndex <= itemCount; itemIndex++ ) 
	{
        PasteboardItemID itemID;
        CFDataRef flavorData;
        err = PasteboardGetItemIdentifier( inPasteboard, itemIndex, &itemID );
111
        //require_noerr( err, CantGetPasteboardItemIdentifier );
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        err = PasteboardCopyItemFlavorData( inPasteboard, itemID, CFSTR("public.utf8-plain-text"), &flavorData );
        if(err==noErr)data = (char*)CFDataGetBytePtr(flavorData);
        if( data!=NULL && err==noErr )
			clipBoard.append(data);
		else 
			return "Error Pasting";
CantGetPasteboardItemIdentifier:
        ;
    }
CantGetPasteboardItemCount:
    return clipBoard;
    
}
#else
bool setTextToPasteboard(std::string str) {}
std::string getTextFromPasteboard() {return "";}
#endif