7 GtkWidget *selection_button;
8 GtkWidget *selection_widget;
10 /* Callback when the user toggles the selection */
11 static void selection_toggled( GtkWidget *widget,
12 gint *have_selection )
14 if (GTK_TOGGLE_BUTTON (widget)->active)
16 *have_selection = gtk_selection_owner_set (selection_widget,
17 GDK_SELECTION_PRIMARY,
19 /* if claiming the selection failed, we return the button to
22 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
28 /* Before clearing the selection by setting the owner to NULL,
29 we check if we are the actual owner */
30 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
31 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
33 *have_selection = FALSE;
38 /* Called when another application claims the selection */
39 static gboolean selection_clear( GtkWidget *widget,
40 GdkEventSelection *event,
41 gint *have_selection )
43 *have_selection = FALSE;
44 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selection_button), FALSE);
49 /* Supplies the current time as the selection. */
50 static void selection_handle( GtkWidget *widget,
51 GtkSelectionData *selection_data,
59 current_time = time (NULL);
60 timestr = asctime (localtime (¤t_time));
61 /* When we return a single string, it should not be null terminated.
62 That will be done for us */
64 gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
65 8, timestr, strlen (timestr));
73 static int have_selection = FALSE;
75 gtk_init (&argc, &argv);
77 /* Create the toplevel window */
79 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
80 gtk_window_set_title (GTK_WINDOW (window), "Event Box");
81 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
83 g_signal_connect (window, "destroy",
84 G_CALLBACK (exit), NULL);
86 /* Create a toggle button to act as the selection */
88 selection_widget = gtk_invisible_new ();
89 selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
90 gtk_container_add (GTK_CONTAINER (window), selection_button);
91 gtk_widget_show (selection_button);
93 g_signal_connect (selection_button, "toggled",
94 G_CALLBACK (selection_toggled), &have_selection);
95 g_signal_connect (selection_widget, "selection-clear-event",
96 G_CALLBACK (selection_clear), &have_selection);
98 gtk_selection_add_target (selection_widget,
99 GDK_SELECTION_PRIMARY,
100 GDK_SELECTION_TYPE_STRING,
102 g_signal_connect (selection_widget, "selection-get",
103 G_CALLBACK (selection_handle), &have_selection);
105 gtk_widget_show (selection_button);
106 gtk_widget_show (window);