OpenClonk
fileicon.c
Go to the documentation of this file.
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 
21 static MapeFileIcon* mape_file_icon_new(GtkWidget* widget,
23 {
24  MapeFileIcon* icon;
25  GdkPixbuf* pixbuf;
26  gint width, height;
27  GdkPixbuf* scaled_pixbuf;
28  GError *error = 0;
29 
30  switch(type)
31  {
33  pixbuf = gtk_widget_render_icon(
34  widget,
35  GTK_STOCK_HARDDISK,
36  GTK_ICON_SIZE_BUTTON,
37  NULL
38  );
39  break;
41  pixbuf = gtk_widget_render_icon(
42  widget,
43  GTK_STOCK_DIRECTORY,
44  GTK_ICON_SIZE_BUTTON,
45  NULL
46  );
47  break;
49  pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocd.ico", &error);
50  break;
52  pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocf.ico", &error);
53  break;
55  pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocg.ico", &error);
56  break;
58  pixbuf = gdk_pixbuf_new_from_resource("/org/openclonk/mape/ocs.ico", &error);
59  break;
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 
103 static void mape_file_icon_destroy(MapeFileIcon* icon)
104 {
105  g_object_unref(G_OBJECT(icon->pixbuf) );
106  free(icon);
107 }
108 
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 
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 
131 {
132  g_assert(type < MAPE_FILE_ICON_COUNT);
133 
134  return set->icons[type];
135 }
136 
138 {
139  return icon->pixbuf;
140 }
MapeFileIcon * mape_file_icon_set_lookup(MapeFileIconSet *set, MapeFileIconType type)
Definition: fileicon.c:129
GdkPixbuf * mape_file_icon_get(MapeFileIcon *icon)
Definition: fileicon.c:137
void mape_file_icon_set_destroy(MapeFileIconSet *set)
Definition: fileicon.c:122
MapeFileIconSet * mape_file_icon_set_new(GtkWidget *widget)
Definition: fileicon.c:109
@ MAPE_FILE_ICON_FOLDER
Definition: fileicon.h:26
@ MAPE_FILE_ICON_C4GROUP
Definition: fileicon.h:27
@ MAPE_FILE_ICON_C4OBJECT
Definition: fileicon.h:29
@ MAPE_FILE_ICON_C4SCENARIO
Definition: fileicon.h:28
@ MAPE_FILE_ICON_COUNT
Definition: fileicon.h:33
@ MAPE_FILE_ICON_C4MATERIAL
Definition: fileicon.h:31
@ MAPE_FILE_ICON_C4FOLDER
Definition: fileicon.h:30
@ MAPE_FILE_ICON_DRIVE
Definition: fileicon.h:25
enum MapeFileIconType_ MapeFileIconType
MapeFileIcon * icons[MAPE_FILE_ICON_COUNT]
Definition: fileicon.h:42
MapeFileIconType type
Definition: fileicon.h:37
GdkPixbuf * pixbuf
Definition: fileicon.h:38