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