]> Pileus Git - ~andy/gtk/blob - examples/selection/gettargets.c
Use gtk_scrollbar_new() instead gtk_[v|h]scrollbar_new()
[~andy/gtk] / examples / selection / gettargets.c
1
2 #include <stdlib.h>
3 #include <gtk/gtk.h>
4
5 static void selection_received( GtkWidget        *widget,
6                                 GtkSelectionData *selection_data,
7                                 gpointer          data );
8
9 /* Signal handler invoked when user clicks on the "Get Targets" button */
10 static void get_targets( GtkWidget *widget,
11                          gpointer data )
12 {
13   static GdkAtom targets_atom = GDK_NONE;
14   GtkWidget *window = (GtkWidget *)data;
15
16   /* Get the atom corresponding to the string "TARGETS" */
17   if (targets_atom == GDK_NONE)
18     targets_atom = gdk_atom_intern ("TARGETS", FALSE);
19
20   /* And request the "TARGETS" target for the primary selection */
21   gtk_selection_convert (window, GDK_SELECTION_PRIMARY, targets_atom,
22                          GDK_CURRENT_TIME);
23 }
24
25 /* Signal handler called when the selections owner returns the data */
26 static void selection_received( GtkWidget        *widget,
27                                 GtkSelectionData *selection_data,
28                                 gpointer          data )
29 {
30   GdkAtom *atoms;
31   GList *item_list;
32   int i;
33
34   /* **** IMPORTANT **** Check to see if retrieval succeeded  */
35   if (selection_data->length < 0)
36     {
37       g_print ("Selection retrieval failed\n");
38       return;
39     }
40   /* Make sure we got the data in the expected form */
41   if (selection_data->type != GDK_SELECTION_TYPE_ATOM)
42     {
43       g_print ("Selection \"TARGETS\" was not returned as atoms!\n");
44       return;
45     }
46
47   /* Print out the atoms we received */
48   atoms = (GdkAtom *)selection_data->data;
49
50   item_list = NULL;
51   for (i = 0; i < selection_data->length / sizeof(GdkAtom); i++)
52     {
53       char *name;
54       name = gdk_atom_name (atoms[i]);
55       if (name != NULL)
56         g_print ("%s\n",name);
57       else
58         g_print ("(bad atom)\n");
59     }
60
61   return;
62 }
63
64 int main( int   argc,
65           char *argv[] )
66 {
67   GtkWidget *window;
68   GtkWidget *button;
69
70   gtk_init (&argc, &argv);
71
72   /* Create the toplevel window */
73
74   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
75   gtk_window_set_title (GTK_WINDOW (window), "Event Box");
76   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
77
78   g_signal_connect (window, "destroy",
79                     G_CALLBACK (exit), NULL);
80
81   /* Create a button the user can click to get targets */
82
83   button = gtk_button_new_with_label ("Get Targets");
84   gtk_container_add (GTK_CONTAINER (window), button);
85
86   g_signal_connect (button, "clicked",
87                     G_CALLBACK (get_targets), (gpointer) window);
88   g_signal_connect (window, "selection-received",
89                     G_CALLBACK (selection_received), NULL);
90
91   gtk_widget_show (button);
92   gtk_widget_show (window);
93
94   gtk_main ();
95
96   return 0;
97 }