]> Pileus Git - ~andy/gtk/blob - tests/testclipboard.c
update Punjabi Translation
[~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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21
22 GtkClipboard *clipboard;
23 GtkWidget *image;
24 GtkWidget *label;
25
26 #define SIZE 256.
27
28 static void
29 image_request_cb (GtkClipboard *clipboard,
30                   GdkPixbuf *pixbuf,
31                   gpointer data)
32 {
33   GdkPixbuf *copy;
34   int height;
35   int width;
36   gdouble factor;
37   char *str;
38
39   if (pixbuf != NULL)
40     {
41       height = gdk_pixbuf_get_height (pixbuf);
42       width = gdk_pixbuf_get_width (pixbuf);
43
44       factor = MAX ((SIZE / height), (SIZE / width));
45
46       copy = gdk_pixbuf_scale_simple (pixbuf, width * factor, height * factor, GDK_INTERP_BILINEAR);
47       gtk_image_set_from_pixbuf (GTK_IMAGE (image), copy);
48       g_object_unref (copy);
49       str = g_strdup_printf ("<b>Image</b> %d \342\234\225 %d", width, height);
50     }
51   else
52     {
53       str = g_strdup ("<b>No image data</b>");
54     }
55   gtk_label_set_markup (GTK_LABEL (label), str);
56   g_free (str);
57 }
58
59 static void
60 update_display (void)
61 {
62   gtk_clipboard_request_image (clipboard, image_request_cb, NULL);
63 }
64
65 static void
66 on_owner_change (GtkClipboard *clipboard,
67                  GdkEvent     *event,
68                  gpointer      user_data)
69 {
70   update_display ();
71 }
72
73 static void
74 on_response (GtkDialog *dialog,
75              gint       response_id,
76              gpointer   user_data)
77 {
78   switch (response_id)
79     {
80     case 0:
81       /* copy large */
82       {
83         GtkIconTheme *theme;
84         GdkPixbuf *pixbuf;
85         theme = gtk_icon_theme_get_default ();
86         pixbuf = gtk_icon_theme_load_icon (theme, "terminal", 1600, 0, NULL);
87         gtk_clipboard_set_image (clipboard, pixbuf);
88       }
89       break;
90     case 1:
91       /* copy small */
92       {
93         GtkIconTheme *theme;
94         GdkPixbuf *pixbuf;
95         theme = gtk_icon_theme_get_default ();
96         pixbuf = gtk_icon_theme_load_icon (theme, "terminal", 48, 0, NULL);
97         gtk_clipboard_set_image (clipboard, pixbuf);
98       }
99       break;
100     case GTK_RESPONSE_CLOSE:
101     default:
102       gtk_main_quit ();
103       break;
104     }
105 }
106
107 int
108 main (int argc, char **argv)
109 {
110   GtkWidget *window;
111
112   gtk_init (&argc, &argv);
113
114   window = gtk_dialog_new_with_buttons ("Clipboard",
115                                         NULL,
116                                         0,
117                                         "Copy Large", 0,
118                                         "Copy Small", 1,
119                                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
120                                         NULL);
121
122   image = gtk_image_new ();
123   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), image, FALSE, FALSE, 0);
124   label = gtk_label_new ("No data found");
125   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), label, FALSE, FALSE, 0);
126
127   g_signal_connect (window, "response", G_CALLBACK (on_response), NULL);
128
129   clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (window),
130                                              GDK_SELECTION_CLIPBOARD);
131   g_signal_connect (clipboard, "owner-change", G_CALLBACK (on_owner_change), NULL);
132
133   update_display ();
134
135   gtk_widget_show_all (window);
136
137   gtk_main ();
138
139   return 0;
140 }