Re: GTK Selection non-sense, need clarification



On Sun, 8 Oct 2000 learfox furry ao net wrote:

I've been reading up on the tutorial for GTK selections

      http://gtk.org/tutorial/gtk_tut-19.html

The tutorial never shows how to fetch the actual data on the `clipboard',
why is that and how do I do that?

Selections can be converted to different "targets".  From the tutorial:

"In the following example, we retrieve the special target "TARGETS", which
is a list of all targets into which the selection can be converted."

At the moment, this will show us what sort of things the selection can be
converted into.  (gdk/gdktypes.h has a list of them under the GdkTarget
enum, I think)

"TARGETS", however, is probably not what you're looking for.  What you're
looking for is the "STRING" target instead, which will give us the
contents of the primary clipboard as a string.


Let's change the example so that it does return the literal contents of
the clipboard.  First, let's make changes to get_targets().  I'll rename
it as 'trigger_selection_received()':

void trigger_selection_received(GdkWidget* widget, gpointer data) {
    static GdkAtom string_atom = GDK_NONE:
    if (string_atom == GDK_NONE)
        string_atom = gdk_atom_intern("STRING", FALSE);
    gtk_selection_convert(widget, GDK_SELECTION_PRIMARY,
                          string_atom, GDK_CURRENT_TIME);
}


In main(), we'd then connect the button to trigger_selection_received like
this:
    /* in main */
    gtk_signal_connect (GTK_OBJECT(button), "clicked",
                        GTK_SIGNAL_FUNC (trigger_selection_received),
                        NULL);
 

Finally, we'd define our selection_received function to just print out the
selection, since we know it's in the form of a string:

void selection_received(GtkWidget *widget,
                        GtkSelectionData *selection_data,
                        gpointer data) {
    g_print("Our string is %d chars long\n", selection_data->length);
    g_print("Here it is: %s\n", selection_data->data);
}


Note: I haven't compiled this at all, so I might be forgetting something.  
Also, I'm a GTK+ newbie, so I might wrong on certain things.  Still, I
hope this is some help to you.  Good luck!





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]