]> Pileus Git - ~andy/gtk/blob - tests/testimage.c
Translation updated by Ivar Smolin.
[~andy/gtk] / tests / testimage.c
1 /* testimage.c
2  * Copyright (C) 2004  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 static void
23 drag_begin (GtkWidget      *widget,
24             GdkDragContext *context,
25             gpointer        data)
26 {
27   GtkWidget *image = GTK_WIDGET (data);
28
29   GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
30
31   gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
32 }
33
34 void  
35 drag_data_get  (GtkWidget        *widget,
36                 GdkDragContext   *context,
37                 GtkSelectionData *selection_data,
38                 guint             info,
39                 guint             time,
40                 gpointer          data)
41 {
42   GtkWidget *image = GTK_WIDGET (data);
43
44   GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
45
46   gtk_selection_data_set_pixbuf (selection_data, pixbuf);
47 }
48
49 static void
50 drag_data_received (GtkWidget        *widget,
51                     GdkDragContext   *context,
52                     gint              x,
53                     gint              y,
54                     GtkSelectionData *selection_data,
55                     guint             info,
56                     guint32           time,
57                     gpointer          data)
58 {
59   GtkWidget *image = GTK_WIDGET (data);
60
61   GdkPixbuf *pixbuf;
62
63   if (selection_data->length < 0)
64     return;
65
66   pixbuf = gtk_selection_data_get_pixbuf (selection_data);
67
68   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
69 }
70
71 static gboolean
72 idle_func (gpointer data)
73 {
74   g_print ("keep me busy\n");
75
76   return TRUE;
77 }
78
79 static gboolean
80 anim_image_expose (GtkWidget      *widget,
81                    GdkEventExpose *eevent,
82                    gpointer        data)
83 {
84   g_print ("start busyness\n");
85
86   g_signal_handlers_disconnect_by_func (widget, anim_image_expose, data);
87
88   /* produce high load */
89   g_idle_add_full (G_PRIORITY_DEFAULT,
90                    idle_func, NULL, NULL);
91
92   return FALSE;
93 }
94
95 int
96 main (int argc, char **argv)
97 {
98   GtkWidget *window, *table;
99   GtkWidget *label, *image, *box;
100   GtkIconTheme *theme;
101   GdkPixbuf *pixbuf;
102   GtkIconSet *iconset;
103   GtkIconSource *iconsource;
104   gchar *icon_name = "gnome-terminal";
105   gchar *anim_filename = NULL;
106
107   gtk_init (&argc, &argv);
108
109   if (argc > 1)
110     icon_name = argv[1];
111
112   if (argc > 2)
113     anim_filename = argv[2];
114
115   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
116   table = gtk_table_new (6, 3, FALSE);
117   gtk_container_add (GTK_CONTAINER (window), table);
118
119   label = gtk_label_new ("symbolic size");
120   gtk_table_attach (GTK_TABLE (table), label, 1, 2, 0, 1,
121                     0, 0, 5, 5);
122   label = gtk_label_new ("fixed size");
123   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
124                     0, 0, 5, 5);
125
126   label = gtk_label_new ("GTK_IMAGE_PIXBUF");
127   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
128
129   theme = gtk_icon_theme_get_default ();
130   pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 48, 0, NULL);
131   image = gtk_image_new_from_pixbuf (pixbuf);
132   box = gtk_event_box_new ();
133   gtk_container_add (GTK_CONTAINER (box), image);
134   gtk_table_attach_defaults (GTK_TABLE (table), box, 2, 3, 1, 2);
135
136   gtk_drag_source_set (box, GDK_BUTTON1_MASK, 
137                        NULL, 0,
138                        GDK_ACTION_COPY);
139   gtk_drag_source_add_image_targets (box);
140   g_signal_connect (box, "drag_begin", G_CALLBACK (drag_begin), image);
141   g_signal_connect (box, "drag_data_get", G_CALLBACK (drag_data_get), image);
142
143   gtk_drag_dest_set (box,
144                      GTK_DEST_DEFAULT_MOTION |
145                      GTK_DEST_DEFAULT_HIGHLIGHT |
146                      GTK_DEST_DEFAULT_DROP,
147                      NULL, 0, GDK_ACTION_COPY);
148   gtk_drag_dest_add_image_targets (box);
149   g_signal_connect (box, "drag_data_received", 
150                     G_CALLBACK (drag_data_received), image);
151
152   label = gtk_label_new ("GTK_IMAGE_STOCK");
153   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
154
155   image = gtk_image_new_from_stock (GTK_STOCK_REDO, GTK_ICON_SIZE_DIALOG);
156   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 2, 3);
157
158   label = gtk_label_new ("GTK_IMAGE_ICON_SET");
159   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
160
161   iconsource = gtk_icon_source_new ();
162   gtk_icon_source_set_icon_name (iconsource, icon_name);
163   iconset = gtk_icon_set_new ();
164   gtk_icon_set_add_source (iconset, iconsource);
165   image = gtk_image_new_from_icon_set (iconset, GTK_ICON_SIZE_DIALOG);
166   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 3, 4);
167
168   label = gtk_label_new ("GTK_IMAGE_ICON_NAME");
169   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 4, 5);
170   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
171   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 4, 5);
172   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
173   gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
174   gtk_table_attach_defaults (GTK_TABLE (table), image, 2, 3, 4, 5);
175
176   if (anim_filename)
177     {
178       label = gtk_label_new ("GTK_IMAGE_ANIMATION (from file)");
179       gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6);
180       image = gtk_image_new_from_file (anim_filename);
181       gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
182       gtk_table_attach_defaults (GTK_TABLE (table), image, 2, 3, 5, 6);
183
184       /* produce high load */
185       g_signal_connect_after (image, "expose-event",
186                               G_CALLBACK (anim_image_expose),
187                               NULL);
188     }
189
190   gtk_widget_show_all (window);
191
192   gtk_main ();
193
194   return 0;
195 }