Search code examples
c++clipboardgtkmmgtkmm3

Gtkmm clipboard text incorrect on Windows


I have a gtkmm application which shares a clipboard from a VNC client (gtk-vnc) to/from the host. For reference, here is the relevant code:

signal_vnc_server_cut_text().connect([this](const Glib::ustring &text) {
    auto clipboard = Gtk::Clipboard::get();
    m_clipboard_text = text;
    /* Works correctly on Windows and Linux */
    clipboard->set_text(text);
    clipboard->store();
});
Gtk::Clipboard::get()->signal_owner_change().connect([this](GdkEventOwnerChange *) {
    auto clipboard = Gtk::Clipboard::get();
    auto text = clipboard->wait_for_text();
    /* text is correct on Linux, but see below for Windows */
    std::cout << "Clipboard got: " << text << std::endl;
    if (!text.empty() && text != m_clipboard_text)
        client_cut_text(text);
});

The current code works as expected on a Linux host (can copy and paste both directions). However, on Windows, any time I attempt to copy from the host, wait_for_text() returns whatever text was in the copy buffer when the app started. Copying from the client on Windows does get the correct text (which I can then paste on the host fine), but once I copy from the host again, the clipboard data is reset to whatever was there from the start.

Am I missing something, or is this a Gtk bug?


Solution

  • I did some digging, and this does appear to be a GTK bug, specifically related to this bugfix: https://bugzilla.gnome.org/show_bug.cgi?id=781814

    Reverting the change or replacing wait_for_text with a custom version that emulates the behavior prior to this patch resolves the clipboard oddities.