Reputation: 442
How can I, using GtkClipboard, read and write to a clipboard? As an example, please show me how to get the current clipboard content and print it to console.
I tried this to get and print what is currently in the clipboard, but it doesn't work:
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gtk_clipboard_request_text(clip, (GtkClipboardTextReceivedFunc)print_clip, NULL);
Everything compiles without any warnings, but print_clip()
function is never reached. Maybe I should use another function, like gtk_clipboard_wait_for_text()
? Please help me, what am I supposed to do?
I use Linux/X11, if it matters. Also, I use GTK+3, not GTK+2 or some other release.
#include <gtk/gtk.h>
void clipboard_callback(GtkClipboard *clip, const gchar *text, gpointer data)
{
g_print("Now we're in clipboard_callback function.\n");
gtk_main_quit();
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_request_text(clip, clipboard_callback, NULL);
gtk_main();
return 0;
}
The only thing that I need now is to somehow exit clipboard_callback()
without calling gtk_main_quit()
, since that closes an application.
Upvotes: 4
Views: 4369
Reputation: 442
One should really use gtk_clipboard_wait_for_text()
instead of gtk_clipboard_request_text()
.
For example, this is how it should be done:
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gchar *text = gtk_clipboard_wait_for_text(clip);
Upvotes: 2
Reputation: 2731
You can use below code from this link.
#include <gdk/gdk.h>
#include <gtk/gtk.h>
// This callback is invoked when the clipboard contents have been
// received. The text contents of the clipboard will be in UTF-8 in
// the `text` parameter.
void text_request_callback(GtkClipboard *clipboard,
const gchar *text,
gpointer data)
{
// To demonstrate setting a new value into the clipboard, we
// choose some text.
const gchar* new_clipboard_text = "Clipboard test 1";
if(text == 0 || g_utf8_collate(text, new_clipboard_text) == 0)
{
// If the program was already run, and the clipboard contains
// our string already, use a different string. This way when
// you run the program multiple times in a row, you'll see the
// string changing.
new_clipboard_text = "Clipboard test 2";
}
// This sets the text. I'm passing -1 because I'm lazy -- the
// function will call strlen on the string to figure out how long
// it is.
gtk_clipboard_set_text(clipboard, new_clipboard_text, -1);
// Display the content and the contents of the variable we passed
// in.
printf("Clipboard text was %s, value is %8X\n",
text, *(int*)data);
// Now that we've monkeyed with the clipboard, our job is done
// here.
gtk_main_quit();
}
int main(int argc, char** argv)
{
// Standard boilerplate: initialize the toolkit.
gtk_init(&argc, &argv);
// Get a handle to the given clipboard. You can also ask for
// GDK_SELECTION_PRIMARY (the X "primary selection") or
// GDK_SELECTION_SECONDARY.
GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
// This is just an arbitrary value to pass through to the
// callback. You could pass a pointer to a local struct or
// something similar if that was useful to you.
int value = 0xDECAFBAD;
// There are more complex things you can place in the clipboard,
// but this demonstrates text. The callback will be invoked when
// the clipboard contents has been received.
//
// For a much simpler method of getting the text in the clipboard,
// see gtk_clipboard_wait_for_text(), which is used in the example
// program clipboard_watch.
gtk_clipboard_request_text(clipboard, text_request_callback, &value);
// We have to run the GTK main loop so that the events required to
// fetch the clipboard contents can be processed.
gtk_main();
return 0;
}
Upvotes: 2