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