It is easy to set the contents of the normal ctrl-C ctrl-V clipboard in Gtk 3 in a Python 3 app, with something like
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text("It worked!, -1)
It is also easy to monitor the clipboard for changes, using the Clipboard object's owner-change
signal.
However, I would like to ignore owner-change
in my app if it was my app that set the contents of the clipboard, and I don't know how. The owner-change
signal passes an owner
parameter which points to a Gdk.Window
, but I never explicitly create a window in my app; I assume that Gtk quietly creates one for me because there needs to be some X window around to own the contents of the clipboard. I can get that X window's ID when the clipboard owner changes with something like:
def clipboardChanged(clipboard, owner_change):
print("New owner is", owner_change.owner.get_xid())
clipboard.connect('owner-change', clipboardChanged)
This new owner window obviously belongs to my app, but how do I find its xid ahead of time? That is: how can I tell from an owner-change
signal that the new owner is me?
(Note: if I set the contents of the clipboard twice, I get two owner-change
signals, and owner_change.reason
is NEW_OWNER
every time, so this is not reliable: in particular, NEW_OWNER
is also sent for "the same owner set it again", and not just for "a different owner now owns the clipboard".)