summaryrefslogtreecommitdiff
path: root/tests/testicontheme.c
blob: 0413135da5364b93785f135d1940e5ea9651ed26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* testicontheme.c
 * Copyright (C) 2002, 2003  Red Hat, Inc.
 * Authors: Alexander Larsson, Owen Taylor
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

static void
usage (void)
{
  g_print ("usage: test-icon-theme lookup <theme name> <icon name> [size] [scale]\n"
	   " or\n"
	   "usage: test-icon-theme list <theme name> [context]\n"
	   " or\n"
	   "usage: test-icon-theme display <theme name> <icon name> [size] [scale]\n"
	   " or\n"
	   "usage: test-icon-theme contexts <theme name>\n"
	   );
}

static void
icon_loaded_cb (GObject *source_object,
		GAsyncResult *res,
		gpointer user_data)
{
  GdkPixbuf *pixbuf;
  GError *error;

  error = NULL;
  pixbuf = gtk_icon_info_load_icon_finish (GTK_ICON_INFO (source_object),
					   res, &error);

  if (pixbuf == NULL)
    {
      g_print ("%s\n", error->message);
      exit (1);
    }

  gtk_image_set_from_pixbuf (GTK_IMAGE (user_data), pixbuf);
  g_object_unref (pixbuf);
}


int
main (int argc, char *argv[])
{
  GtkIconTheme *icon_theme;
  GtkIconInfo *icon_info;
  char *context;
  char *themename;
  GList *list;
  int size = 48;
  int scale = 1;
  GtkIconLookupFlags flags;
  
  gtk_init (&argc, &argv);

  if (argc < 3)
    {
      usage ();
      return 1;
    }

  flags = GTK_ICON_LOOKUP_USE_BUILTIN;

  if (g_getenv ("RTL"))
    flags |= GTK_ICON_LOOKUP_DIR_RTL;
  else
    flags |= GTK_ICON_LOOKUP_DIR_LTR;

  themename = argv[2];
  
  icon_theme = gtk_icon_theme_new ();
  
  gtk_icon_theme_set_custom_theme (icon_theme, themename);

  if (strcmp (argv[1], "display") == 0)
    {
      GError *error;
      GdkPixbuf *pixbuf;
      GtkWidget *window, *image;

      if (argc < 4)
	{
	  g_object_unref (icon_theme);
	  usage ();
	  return 1;
	}
      
      if (argc >= 5)
	size = atoi (argv[4]);

      if (argc >= 6)
	scale = atoi (argv[5]);

      error = NULL;
      pixbuf = gtk_icon_theme_load_icon_for_scale (icon_theme, argv[3], size, scale, flags, &error);
      if (!pixbuf)
        {
          g_print ("%s\n", error->message);
          return 1;
        }

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      image = gtk_image_new ();
      gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
      g_object_unref (pixbuf);
      gtk_container_add (GTK_CONTAINER (window), image);
      g_signal_connect (window, "delete-event",
                        G_CALLBACK (gtk_main_quit), window);
      gtk_widget_show_all (window);
      
      gtk_main ();
    }
  else if (strcmp (argv[1], "display-async") == 0)
    {
      GtkWidget *window, *image;
      GtkIconInfo *info;

      if (argc < 4)
	{
	  g_object_unref (icon_theme);
	  usage ();
	  return 1;
	}

      if (argc >= 5)
	size = atoi (argv[4]);

      if (argc >= 6)
	scale = atoi (argv[5]);

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      image = gtk_image_new ();
      gtk_container_add (GTK_CONTAINER (window), image);
      g_signal_connect (window, "delete-event",
                        G_CALLBACK (gtk_main_quit), window);
      gtk_widget_show_all (window);

      info = gtk_icon_theme_lookup_icon_for_scale (icon_theme, argv[3], size, scale, flags);

      if (info == NULL)
	{
          g_print ("Icon not found\n");
          return 1;
	}

      gtk_icon_info_load_icon_async (info,
				     NULL, icon_loaded_cb, image);

      gtk_main ();
    }
  else if (strcmp (argv[1], "list") == 0)
    {
      if (argc >= 4)
	context = argv[3];
      else
	context = NULL;

      list = gtk_icon_theme_list_icons (icon_theme,
					   context);
      
      while (list)
	{
	  g_print ("%s\n", (char *)list->data);
	  list = list->next;
	}
    }
  else if (strcmp (argv[1], "contexts") == 0)
    {
      list = gtk_icon_theme_list_contexts (icon_theme);
      
      while (list)
	{
	  g_print ("%s\n", (char *)list->data);
	  list = list->next;
	}
    }
  else if (strcmp (argv[1], "lookup") == 0)
    {
      if (argc < 4)
	{
	  g_object_unref (icon_theme);
	  usage ();
	  return 1;
	}

      if (argc >= 5)
	size = atoi (argv[4]);

      if (argc >= 6)
	scale = atoi (argv[5]);

      icon_info = gtk_icon_theme_lookup_icon_for_scale (icon_theme, argv[3], size, scale, flags);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      g_print ("icon for %s at %dx%d@%dx is %s\n", argv[3], size, size, scale,
	       icon_info ? (gtk_icon_info_get_builtin_pixbuf (icon_info) ? "<builtin>" : gtk_icon_info_get_filename (icon_info)) : "<none>");
G_GNUC_END_IGNORE_DEPRECATIONS

      if (icon_info)
	{
          GdkPixbuf *pixbuf;

          g_print ("Base size: %d, Scale: %d\n", gtk_icon_info_get_base_size (icon_info), gtk_icon_info_get_base_scale (icon_info));

          pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
          if (pixbuf != NULL)
            {
              g_print ("Pixbuf size: %dx%d\n", gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf));
              g_object_unref (pixbuf);
            }

	  g_object_unref (icon_info);
	}
    }
  else
    {
      g_object_unref (icon_theme);
      usage ();
      return 1;
    }
 

  g_object_unref (icon_theme);
  
  return 0;
}