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
image_request_cb(GtkClipboard * clipboard,GdkPixbuf * pixbuf,gpointer data)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
update_display(void)58 update_display (void)
59 {
60   gtk_clipboard_request_image (clipboard, image_request_cb, NULL);
61 }
62 
63 static void
on_owner_change(GtkClipboard * clipboard,GdkEvent * event,gpointer user_data)64 on_owner_change (GtkClipboard *clipboard,
65                  GdkEvent     *event,
66                  gpointer      user_data)
67 {
68   update_display ();
69 }
70 
71 static void
on_response(GtkDialog * dialog,gint response_id,gpointer user_data)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, "utilities-terminal", 1600, 0, NULL);
85         g_assert_nonnull (pixbuf);
86         gtk_clipboard_set_image (clipboard, pixbuf);
87       }
88       break;
89     case 1:
90       /* copy small */
91       {
92         GtkIconTheme *theme;
93         GdkPixbuf *pixbuf;
94         theme = gtk_icon_theme_get_default ();
95         pixbuf = gtk_icon_theme_load_icon (theme, "utilities-terminal", 48, 0, NULL);
96         g_assert_nonnull (pixbuf);
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
main(int argc,char ** argv)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                                         "_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 }
141