]> Pileus Git - ~andy/gtk/blob - tests/testicontheme.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testicontheme.c
1 /* testicontheme.c
2  * Copyright (C) 2002, 2003  Red Hat, Inc.
3  * Authors: Alexander Larsson, Owen Taylor
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <gtk/gtk.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <locale.h>
23
24 static void
25 usage (void)
26 {
27   g_print ("usage: test-icon-theme lookup <theme name> <icon name> [size]]\n"
28            " or\n"
29            "usage: test-icon-theme list <theme name> [context]\n"
30            " or\n"
31            "usage: test-icon-theme display <theme name> <icon name> [size]\n"
32            " or\n"
33            "usage: test-icon-theme contexts <theme name>\n"
34            );
35 }
36
37 static void
38 icon_loaded_cb (GObject *source_object,
39                 GAsyncResult *res,
40                 gpointer user_data)
41 {
42   GdkPixbuf *pixbuf;
43   GError *error;
44
45   error = NULL;
46   pixbuf = gtk_icon_info_load_icon_finish (GTK_ICON_INFO (source_object),
47                                            res, &error);
48
49   if (pixbuf == NULL)
50     {
51       g_print ("%s\n", error->message);
52       exit (1);
53     }
54
55   gtk_image_set_from_pixbuf (GTK_IMAGE (user_data), pixbuf);
56   g_object_unref (pixbuf);
57 }
58
59
60 int
61 main (int argc, char *argv[])
62 {
63   GtkIconTheme *icon_theme;
64   GtkIconInfo *icon_info;
65   GdkRectangle embedded_rect;
66   GdkPoint *attach_points;
67   int n_attach_points;
68   const gchar *display_name;
69   char *context;
70   char *themename;
71   GList *list;
72   int size = 48;
73   int i;
74   
75   gtk_init (&argc, &argv);
76
77   if (argc < 3)
78     {
79       usage ();
80       return 1;
81     }
82
83   themename = argv[2];
84   
85   icon_theme = gtk_icon_theme_new ();
86   
87   gtk_icon_theme_set_custom_theme (icon_theme, themename);
88
89   if (strcmp (argv[1], "display") == 0)
90     {
91       GError *error;
92       GdkPixbuf *pixbuf;
93       GtkWidget *window, *image;
94       GtkIconSize size;
95
96       if (argc < 4)
97         {
98           g_object_unref (icon_theme);
99           usage ();
100           return 1;
101         }
102       
103       if (argc >= 5)
104         size = atoi (argv[4]);
105       else 
106         size = GTK_ICON_SIZE_BUTTON;
107
108       error = NULL;
109       pixbuf = gtk_icon_theme_load_icon (icon_theme, argv[3], size,
110                                          GTK_ICON_LOOKUP_USE_BUILTIN, &error);
111       if (!pixbuf)
112         {
113           g_print ("%s\n", error->message);
114           return 1;
115         }
116
117       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
118       image = gtk_image_new ();
119       gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
120       g_object_unref (pixbuf);
121       gtk_container_add (GTK_CONTAINER (window), image);
122       g_signal_connect (window, "delete-event",
123                         G_CALLBACK (gtk_main_quit), window);
124       gtk_widget_show_all (window);
125       
126       gtk_main ();
127     }
128   else if (strcmp (argv[1], "display-async") == 0)
129     {
130       GtkWidget *window, *image;
131       GtkIconSize size;
132       GtkIconInfo *info;
133
134       if (argc < 4)
135         {
136           g_object_unref (icon_theme);
137           usage ();
138           return 1;
139         }
140
141       if (argc >= 5)
142         size = atoi (argv[4]);
143       else
144         size = GTK_ICON_SIZE_BUTTON;
145
146       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
147       image = gtk_image_new ();
148       gtk_container_add (GTK_CONTAINER (window), image);
149       g_signal_connect (window, "delete-event",
150                         G_CALLBACK (gtk_main_quit), window);
151       gtk_widget_show_all (window);
152
153       info = gtk_icon_theme_lookup_icon (icon_theme, argv[3], size,
154                                          GTK_ICON_LOOKUP_USE_BUILTIN);
155
156       if (info == NULL)
157         {
158           g_print ("Icon not found\n");
159           return 1;
160         }
161
162       gtk_icon_info_load_icon_async (info,
163                                      NULL, icon_loaded_cb, image);
164
165       gtk_main ();
166     }
167   else if (strcmp (argv[1], "list") == 0)
168     {
169       if (argc >= 4)
170         context = argv[3];
171       else
172         context = NULL;
173
174       list = gtk_icon_theme_list_icons (icon_theme,
175                                            context);
176       
177       while (list)
178         {
179           g_print ("%s\n", (char *)list->data);
180           list = list->next;
181         }
182     }
183   else if (strcmp (argv[1], "contexts") == 0)
184     {
185       list = gtk_icon_theme_list_contexts (icon_theme);
186       
187       while (list)
188         {
189           g_print ("%s\n", (char *)list->data);
190           list = list->next;
191         }
192     }
193   else if (strcmp (argv[1], "lookup") == 0)
194     {
195       if (argc < 4)
196         {
197           g_object_unref (icon_theme);
198           usage ();
199           return 1;
200         }
201       
202       if (argc >= 5)
203         size = atoi (argv[4]);
204       
205       icon_info = gtk_icon_theme_lookup_icon (icon_theme, argv[3], size, GTK_ICON_LOOKUP_USE_BUILTIN);
206       g_print ("icon for %s at %dx%d is %s\n", argv[3], size, size,
207                icon_info ? (gtk_icon_info_get_builtin_pixbuf (icon_info) ? "<builtin>" : gtk_icon_info_get_filename (icon_info)) : "<none>");
208
209       if (icon_info) 
210         {
211           if (gtk_icon_info_get_embedded_rect (icon_info, &embedded_rect))
212             {
213               g_print ("Embedded rect: %d,%d %dx%d\n",
214                        embedded_rect.x, embedded_rect.y,
215                        embedded_rect.width, embedded_rect.height);
216             }
217           
218           if (gtk_icon_info_get_attach_points (icon_info, &attach_points, &n_attach_points))
219             {
220               g_print ("Attach Points: ");
221               for (i = 0; i < n_attach_points; i++)
222                 g_print ("%d, %d; ",
223                          attach_points[i].x,
224                          attach_points[i].y);
225               g_free (attach_points);
226               g_print ("\n");
227             }
228           
229           display_name = gtk_icon_info_get_display_name (icon_info);
230           
231           if (display_name)
232             g_print ("Display name: %s\n", display_name);
233           
234           g_object_unref (icon_info);
235         }
236     }
237   else
238     {
239       g_object_unref (icon_theme);
240       usage ();
241       return 1;
242     }
243  
244
245   g_object_unref (icon_theme);
246   
247   return 0;
248 }