asked    Jerry     2018-10-22       c       70 view        2 Answers

[SOLVED] How do I use clipboard in GTK?

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.

Ok, I've got a working example:

#include <gtk/gtk.h>

void clipboard_callback(GtkClipboard *clip, const gchar *text, gpointer data)
        g_print("Now we're in clipboard_callback function.\n");

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);
        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.

  2 Answers  

        answered    Blithe     2018-10-22      

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.

    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
        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.

        return 0;

        answered    Isabel     2018-10-22      

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);

Your Answer

 2018-10-22         Bernice

Azure ARM template deployment

I am trying to create NIC card for VM through Azure ARM template. But my script also creates concats NIC to app gateway. But as of now I am only creating VM and NIC. Is there a condition to skip the Concatenation of App gateway and NIC. Below is the NIC creation template:{ "comments": "NIC_Creation", "tags": { "owner": "[parameters('owner')]" }, "condition": "[equals(parameters('CreateVM'), 'yes')]", "dependsOn": [ // "[resourceId('Microsoft.Network/publicIPAddresses', parameters('VM_PublicIP_Name'))]", // "[resourceId('Microsoft.Network/...
 azure                     2 answers                     50 view
 2018-10-22         Ives

Azure Active Directory with Springboot : how to handle expired oauth token?

I have a springboot app which use Microsoft Azure active directory to allow authentication (oauth2).I have followed the "how to" provided by Microsoft ( is working well except that I have no ideas on how to handle expired token (after 1 hour) in a way that it will not affect the users.I know that it is possible to get new token with the refresh token but if I take a look in NimbusAuthorizationCodeTokenResponseClien...
 spring-boot                     1 answers                     49 view
 2018-10-22         Earl

azure active directory + spring boot

I am trying to run azure-active-directory-spring-boot-backend-sample but it is not working. After opening localhost:8080 it prompts with localhost:8080/login page and Azure link. After clicking on it it goes to Azure Active Directory page. After successful login it comes back to /login page rather than /welcome page. I tried using other example as well available on internet but nothing seems to be working. Please advise. You need to register your application under the Azure Active Directory where your users are stored. Then enter the published homepage URL in the Reply U...
 spring-boot                     1 answers                     50 view