tags:

views:

141

answers:

1

I'm calling the following function to 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!

+1  A: 

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.

matja
Good answer, but one clarification: there aren't really two different clipboards. 'CLIPBOARD' is the clipboard as we know it from applications. The X11 clipboard ('PRIMARY') contains the last selected text, which in most X11 environments can be pasted by clicking the middle mouse button.
ptomato