]> Pileus Git - ~andy/gtk/blob - examples/selection/setselection.c
Bug 556527 - The current page property is not passed to GtkPrintUnixDialog
[~andy/gtk] / examples / selection / setselection.c
1
2 #include <stdlib.h>
3 #include <gtk/gtk.h>
4 #include <time.h>
5 #include <string.h>
6
7 GtkWidget *selection_button;
8 GtkWidget *selection_widget;
9
10 /* Callback when the user toggles the selection */
11 static void selection_toggled( GtkWidget *widget,
12                                gint      *have_selection )
13 {
14   if (GTK_TOGGLE_BUTTON (widget)->active)
15     {
16       *have_selection = gtk_selection_owner_set (selection_widget,
17                                                  GDK_SELECTION_PRIMARY,
18                                                  GDK_CURRENT_TIME);
19       /* if claiming the selection failed, we return the button to
20          the out state */
21       if (!*have_selection)
22         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
23     }
24   else
25     {
26       if (*have_selection)
27         {
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,
32                                      GDK_CURRENT_TIME);
33           *have_selection = FALSE;
34         }
35     }
36 }
37
38 /* Called when another application claims the selection */
39 static gboolean selection_clear( GtkWidget         *widget,
40                                  GdkEventSelection *event,
41                                  gint              *have_selection )
42 {
43   *have_selection = FALSE;
44   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selection_button), FALSE);
45
46   return TRUE;
47 }
48
49 /* Supplies the current time as the selection. */
50 static void selection_handle( GtkWidget        *widget,
51                               GtkSelectionData *selection_data,
52                               guint             info,
53                               guint             time_stamp,
54                               gpointer          data )
55 {
56   gchar *timestr;
57   time_t current_time;
58
59   current_time = time (NULL);
60   timestr = asctime (localtime (&current_time));
61   /* When we return a single string, it should not be null terminated.
62      That will be done for us */
63
64   gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
65                           8, timestr, strlen (timestr));
66 }
67
68 int main( int   argc,
69           char *argv[] )
70 {
71   GtkWidget *window;
72
73   static int have_selection = FALSE;
74
75   gtk_init (&argc, &argv);
76
77   /* Create the toplevel window */
78
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);
82
83   g_signal_connect (window, "destroy",
84                     G_CALLBACK (exit), NULL);
85
86   /* Create a toggle button to act as the selection */
87
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);
92
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);
97
98   gtk_selection_add_target (selection_widget,
99                             GDK_SELECTION_PRIMARY,
100                             GDK_SELECTION_TYPE_STRING,
101                             1);
102   g_signal_connect (selection_widget, "selection-get",
103                     G_CALLBACK (selection_handle), &have_selection);
104
105   gtk_widget_show (selection_button);
106   gtk_widget_show (window);
107
108   gtk_main ();
109
110   return 0;
111 }