]> Pileus Git - ~andy/gtk/blob - tests/testclipboard.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testclipboard.c
1 /*
2  * Copyright (C) 2011  Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <gtk/gtk.h>
19
20 GtkClipboard *clipboard;
21 GtkWidget *image;
22 GtkWidget *label;
23
24 #define SIZE 256.
25
26 static void
27 image_request_cb (GtkClipboard *clipboard,
28                   GdkPixbuf *pixbuf,
29                   gpointer data)
30 {
31   GdkPixbuf *copy;
32   int height;
33   int width;
34   gdouble factor;
35   char *str;
36
37   if (pixbuf != NULL)
38     {
39       height = gdk_pixbuf_get_height (pixbuf);
40       width = gdk_pixbuf_get_width (pixbuf);
41
42       factor = MAX ((SIZE / height), (SIZE / width));
43
44       copy = gdk_pixbuf_scale_simple (pixbuf, width * factor, height * factor, GDK_INTERP_BILINEAR);
45       gtk_image_set_from_pixbuf (GTK_IMAGE (image), copy);
46       g_object_unref (copy);
47       str = g_strdup_printf ("<b>Image</b> %d \342\234\225 %d", width, height);
48     }
49   else
50     {
51       str = g_strdup ("<b>No image data</b>");
52     }
53   gtk_label_set_markup (GTK_LABEL (label), str);
54   g_free (str);
55 }
56
57 static void
58 update_display (void)
59 {
60   gtk_clipboard_request_image (clipboard, image_request_cb, NULL);
61 }
62
63 static void
64 on_owner_change (GtkClipboard *clipboard,
65                  GdkEvent     *event,
66                  gpointer      user_data)
67 {
68   update_display ();
69 }
70
71 static void
72 on_response (GtkDialog *dialog,
73              gint       response_id,
74              gpointer   user_data)
75 {
76   switch (response_id)
77     {
78     case 0:
79       /* copy large */
80       {
81         GtkIconTheme *theme;
82         GdkPixbuf *pixbuf;
83         theme = gtk_icon_theme_get_default ();
84         pixbuf = gtk_icon_theme_load_icon (theme, "terminal", 1600, 0, NULL);
85         gtk_clipboard_set_image (clipboard, pixbuf);
86       }
87       break;
88     case 1:
89       /* copy small */
90       {
91         GtkIconTheme *theme;
92         GdkPixbuf *pixbuf;
93         theme = gtk_icon_theme_get_default ();
94         pixbuf = gtk_icon_theme_load_icon (theme, "terminal", 48, 0, NULL);
95         gtk_clipboard_set_image (clipboard, pixbuf);
96       }
97       break;
98     case GTK_RESPONSE_CLOSE:
99     default:
100       gtk_main_quit ();
101       break;
102     }
103 }
104
105 int
106 main (int argc, char **argv)
107 {
108   GtkWidget *window;
109
110   gtk_init (&argc, &argv);
111
112   window = gtk_dialog_new_with_buttons ("Clipboard",
113                                         NULL,
114                                         0,
115                                         "Copy Large", 0,
116                                         "Copy Small", 1,
117                                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
118                                         NULL);
119
120   image = gtk_image_new ();
121   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), image, FALSE, FALSE, 0);
122   label = gtk_label_new ("No data found");
123   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), label, FALSE, FALSE, 0);
124
125   g_signal_connect (window, "response", G_CALLBACK (on_response), NULL);
126
127   clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (window),
128                                              GDK_SELECTION_CLIPBOARD);
129   g_signal_connect (clipboard, "owner-change", G_CALLBACK (on_owner_change), NULL);
130
131   update_display ();
132
133   gtk_widget_show_all (window);
134
135   gtk_main ();
136
137   return 0;
138 }