Patch for AntTweakBar to support pasting from clipboard on Mac OS X

Alec Jacobson

January 03, 2012

weblog/

I recently noticed I could use pbpaste and a system call to integrate the clipboard's contents into my C++ programs. I use AntTweakBar all the time for quick prototyping UI and though it has a default internal clipboard, I found it frustrating that it doesn't hook into my mac's global clipboard. To make this happen I just modified TwBar.cpp in two places EditInPlaceSetClipboard and EditInPlaceGetClipboard, surrounding each edit with #elif defined ANT_OSX. Now I can copy from my AntTweakBar and paste into some other app or copy from some other app and paste into an AntTweakBar string field. Here's what to place in EditInPlaceSetClipboard
// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012
#elif defined ANT_OSX
    stringstream cmd;
    cmd << "echo \"" << _String << "\" | pbcopy";
    FILE* pipe = popen(cmd.str().c_str(), "r");
    if (!pipe)
    {
        return false;
    }
// PATCH END
And here's what to place in EditInPlaceGetClipboard
// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012                                                                        
#elif defined ANT_OSX
    FILE* pipe = popen("pbpaste", "r");                                                                   
    if (!pipe)
    {   
        return false;
    }                                                                                                     
    char buffer[128];
    string result = "";                                                                                   
    while(!feof(pipe))
    {   
        if(fgets(buffer, 128, pipe) != NULL)                                                              
        {
            result += buffer;                                                                             
        }                                                                                                 
    }   
    pclose(pipe);
    *_OutString = result.c_str();                                                                         
// PATCH END
To ensure your binaries run with this patched version you might consider compiling AntTweakBar as a static library.