Clear the system clipboard using the GTK lib, in C
I'm calling the following function to开发者_Python百科 try to clear the system clipboard:
GtkClipboard *clipboard;
clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_clear(clipboard);
however it's not cleaning anything. I've searched the Gnome and GTK+ documentations and countless sample code snippets and I've no idea how to do this.
so my question, how do you clear the system (linux, gnome) clipboard by code? Thanks!
I believe you need to actually set it with a zero-length text to clear it completely, I'm unsure myself why this is necessary but this code seems to work :
clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gtk_clipboard_clear(clipboard);
gtk_clipboard_set_text(clipboard, "", 0);
clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_clear(clipboard);
gtk_clipboard_set_text(clipboard, "", 0);
Note that when GTK+ is running under X11, there are actually two 'clipboards', the GTK (GDK_SELECTION_CLIPBOARD) one and the X11 one (GDK_SELECTION_PRIMARY). Under Windows, operations on GDK_SELECTION_PRIMARY I think do nothing.
精彩评论