1 /*
2  * mape - C4 Landscape.txt editor
3  *
4  * Copyright (c) 2005-2009, Armin Burgmeier
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include <stdlib.h>
17 #include <gtk/gtk.h>
18 
19 #include "mape/fileicon.h"
20 
mape_file_icon_new(GtkWidget * widget,MapeFileIconType type)21 static MapeFileIcon* mape_file_icon_new(GtkWidget* widget,
22                                         MapeFileIconType type)
23 {
24 	MapeFileIcon* icon;
25 	GdkPixbuf* pixbuf;
26 	gint width, height;
27 	GdkPixbuf* scaled_pixbuf;
28 	GError *error = 0;
29 
30 	switch(type)
31 	{
32 	case MAPE_FILE_ICON_DRIVE:
33 		pixbuf = gtk_widget_render_icon(
34 			widget,
35 			GTK_STOCK_HARDDISK,
36 			GTK_ICON_SIZE_BUTTON,
37 			NULL
38 		);
39 		break;
40 	case MAPE_FILE_ICON_FOLDER:
41 		pixbuf = gtk_widget_render_icon(
42 			widget,
43 			GTK_STOCK_DIRECTORY,
44 			GTK_ICON_SIZE_BUTTON,
45 			NULL
46 		);
47 		break;
48 	case MAPE_FILE_ICON_C4OBJECT:
49 		pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocd.ico", &error);
50 		break;
51 	case MAPE_FILE_ICON_C4FOLDER:
52 		pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocf.ico", &error);
53 		break;
54 	case MAPE_FILE_ICON_C4GROUP:
55 		pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocg.ico", &error);
56 		break;
57 	case MAPE_FILE_ICON_C4SCENARIO:
58 		pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocs.ico", &error);
59 		break;
60 	case MAPE_FILE_ICON_C4MATERIAL:
61 		pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocm.ico", &error);
62 		break;
63 	default:
64 		g_assert_not_reached();
65 		break;
66 	}
67 	if (error)
68 	{
69 		fprintf (stderr, "Unable to create icon: %s\n", error->message);
70 		g_error_free (error);
71 	}
72 
73 	if(pixbuf == NULL)
74 		return NULL;
75 
76 	gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &width, &height);
77 
78 	/* Scale pixbuf to size of GTK_ICON_SIZE_BUTTON */
79 	if(gdk_pixbuf_get_width(pixbuf) != width ||
80 	   gdk_pixbuf_get_height(pixbuf) != height)
81 	{
82 		scaled_pixbuf = gdk_pixbuf_scale_simple(
83 			pixbuf,
84 			width,
85 			height,
86 			GDK_INTERP_HYPER
87 		);
88 
89 		g_object_unref(pixbuf);
90 		pixbuf = scaled_pixbuf;
91 
92 		if(pixbuf == NULL)
93 			return NULL;
94 	}
95 
96 	icon = malloc(sizeof(MapeFileIcon) );
97 	icon->type = type;
98 	icon->pixbuf = pixbuf;
99 
100 	return icon;
101 }
102 
mape_file_icon_destroy(MapeFileIcon * icon)103 static void mape_file_icon_destroy(MapeFileIcon* icon)
104 {
105 	g_object_unref(G_OBJECT(icon->pixbuf) );
106 	free(icon);
107 }
108 
mape_file_icon_set_new(GtkWidget * widget)109 MapeFileIconSet* mape_file_icon_set_new(GtkWidget* widget)
110 {
111 	MapeFileIconSet* set;
112 	unsigned int i;
113 
114 	set = malloc(sizeof(MapeFileIconSet) );
115 
116 	for(i = 0; i < MAPE_FILE_ICON_COUNT; ++ i)
117 		set->icons[i] = mape_file_icon_new(widget, (MapeFileIconType)i);
118 
119 	return set;
120 }
121 
mape_file_icon_set_destroy(MapeFileIconSet * set)122 void mape_file_icon_set_destroy(MapeFileIconSet* set)
123 {
124 	unsigned int i;
125 	for(i = 0; i < MAPE_FILE_ICON_COUNT; ++ i)
126 		mape_file_icon_destroy(set->icons[i]);
127 }
128 
mape_file_icon_set_lookup(MapeFileIconSet * set,MapeFileIconType type)129 MapeFileIcon* mape_file_icon_set_lookup(MapeFileIconSet* set,
130                                         MapeFileIconType type)
131 {
132 	g_assert(type < MAPE_FILE_ICON_COUNT);
133 
134 	return set->icons[type];
135 }
136 
mape_file_icon_get(MapeFileIcon * icon)137 GdkPixbuf* mape_file_icon_get(MapeFileIcon* icon)
138 {
139 	return icon->pixbuf;
140 }
141