]> Pileus Git - ~andy/gtk/blob - tests/testicontheme.c
Bug 347230 – testicontheme shortcomings
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gtk/gtk.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <locale.h>
25
26 static void
27 usage (void)
28 {
29   g_print ("usage: test-icon-theme lookup <theme name> <icon name> [size]]\n"
30            " or\n"
31            "usage: test-icon-theme list <theme name> [context]\n"
32            " or\n"
33            "usage: test-icon-theme display <theme name> <icon name> [size]\n"
34            " or\n"
35            "usage: test-icon-theme contexts <theme name>\n"
36            );
37 }
38
39
40 int
41 main (int argc, char *argv[])
42 {
43   GtkIconTheme *icon_theme;
44   GtkIconInfo *icon_info;
45   GdkRectangle embedded_rect;
46   GdkPoint *attach_points;
47   int n_attach_points;
48   const gchar *display_name;
49   char *context;
50   char *themename;
51   GList *list;
52   int size = 48;
53   int i;
54   
55   gtk_init (&argc, &argv);
56
57   if (argc < 3)
58     {
59       usage ();
60       return 1;
61     }
62
63   themename = argv[2];
64   
65   icon_theme = gtk_icon_theme_new ();
66   
67   gtk_icon_theme_set_custom_theme (icon_theme, themename);
68
69   if (strcmp (argv[1], "display") == 0)
70     {
71       GError *error;
72       GdkPixbuf *pixbuf;
73       GtkWidget *window, *image;
74       GtkIconSize size;
75
76       if (argc < 4)
77         {
78           g_object_unref (icon_theme);
79           usage ();
80           return 1;
81         }
82       
83       if (argc >= 5)
84         size = atoi (argv[4]);
85       else 
86         size = GTK_ICON_SIZE_BUTTON;
87
88       error = NULL;
89       pixbuf = gtk_icon_theme_load_icon (icon_theme, argv[3], size,
90                                          GTK_ICON_LOOKUP_USE_BUILTIN, &error);
91       if (!pixbuf)
92         {
93           g_print ("%s\n", error->message);
94           return 1;
95         }
96
97       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98       image = gtk_image_new ();
99       gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
100       g_object_unref (pixbuf);
101       gtk_container_add (GTK_CONTAINER (window), image);
102       g_signal_connect (window, "delete-event",
103                         G_CALLBACK (gtk_main_quit), window);
104       gtk_widget_show_all (window);
105       
106       gtk_main ();
107     }
108   else if (strcmp (argv[1], "list") == 0)
109     {
110       if (argc >= 4)
111         context = argv[3];
112       else
113         context = NULL;
114
115       list = gtk_icon_theme_list_icons (icon_theme,
116                                            context);
117       
118       while (list)
119         {
120           g_print ("%s\n", (char *)list->data);
121           list = list->next;
122         }
123     }
124   else if (strcmp (argv[1], "contexts") == 0)
125     {
126       list = gtk_icon_theme_list_contexts (icon_theme);
127       
128       while (list)
129         {
130           g_print ("%s\n", (char *)list->data);
131           list = list->next;
132         }
133     }
134   else if (strcmp (argv[1], "lookup") == 0)
135     {
136       if (argc < 4)
137         {
138           g_object_unref (icon_theme);
139           usage ();
140           return 1;
141         }
142       
143       if (argc >= 5)
144         size = atoi (argv[4]);
145       
146       icon_info = gtk_icon_theme_lookup_icon (icon_theme, argv[3], size, GTK_ICON_LOOKUP_USE_BUILTIN);
147       g_print ("icon for %s at %dx%d is %s\n", argv[3], size, size,
148                icon_info ? (gtk_icon_info_get_builtin_pixbuf (icon_info) ? "<builtin>" : gtk_icon_info_get_filename (icon_info)) : "<none>");
149
150       if (icon_info) 
151         {
152           if (gtk_icon_info_get_embedded_rect (icon_info, &embedded_rect))
153             {
154               g_print ("Embedded rect: %d,%d %dx%d\n",
155                        embedded_rect.x, embedded_rect.y,
156                        embedded_rect.width, embedded_rect.height);
157             }
158           
159           if (gtk_icon_info_get_attach_points (icon_info, &attach_points, &n_attach_points))
160             {
161               g_print ("Attach Points: ");
162               for (i = 0; i < n_attach_points; i++)
163                 g_print ("%d, %d; ",
164                          attach_points[i].x,
165                          attach_points[i].y);
166               g_free (attach_points);
167               g_print ("\n");
168             }
169           
170           display_name = gtk_icon_info_get_display_name (icon_info);
171           
172           if (display_name)
173             g_print ("Display name: %s\n", display_name);
174           
175           gtk_icon_info_free (icon_info);
176         }
177     }
178   else
179     {
180       g_object_unref (icon_theme);
181       usage ();
182       return 1;
183     }
184  
185
186   g_object_unref (icon_theme);
187   
188   return 0;
189 }