pixlist.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #define LOGGING_ON 0
00034
00035
00036 #include <gtk/gtk.h>
00037 #include <string.h>
00038
00039
00040 #include <libergtk/ergtk.h>
00041
00042 #include "log.h"
00043 #include "pixlist.h"
00044
00045 static GTree *g_icon_cache = NULL;
00046
00047 gint compareString(gconstpointer a, gconstpointer b) {
00048 return strcmp((const gchar*)a, (const gchar*)b);
00049 }
00050
00051 static GdkPixbuf* load_icon(const gchar* filename)
00052 {
00053 if (g_icon_cache == NULL) g_icon_cache = g_tree_new(compareString);
00054
00055 gpointer cache = g_tree_lookup(g_icon_cache, filename);
00056 if (cache) return (GdkPixbuf *)cache;
00057
00058 GError *err = NULL;
00059 GdkPixbuf *icon = gdk_pixbuf_new_from_file(filename, &err);
00060 if (icon) {
00061 g_tree_insert(g_icon_cache, g_strdup(filename), icon);
00062 } else {
00063 LOGPRINTF("cannot load iconfile [%s] error [%s]", filename, err->message);
00064 g_clear_error(&err);
00065 }
00066 return icon;
00067 }
00068
00069
00070 GdkPixbuf *pixlist_icon_state(const char *icon, const char *state)
00071 {
00072 LOGPRINTF("entry %s, %s", icon, state);
00073
00074 if ((icon == NULL) || (icon[0] == '\0'))
00075 {
00076
00077 return NULL;
00078 }
00079
00080 gchar *iconfile = NULL;
00081 gchar *fileext = g_utf8_strrchr(icon, strlen(icon), '.');
00082 if (fileext == NULL)
00083 {
00084
00085
00086 if (state && (strcmp(state, "normal") == 0))
00087 {
00088 iconfile = g_strdup_printf("%s%s.png", PATH_IMG, icon);
00089 }
00090 else
00091 {
00092 iconfile = g_strdup_printf("%s%s_%s.png", PATH_IMG, icon, state);
00093 }
00094 }
00095 else
00096 {
00097 gchar *strcopy = g_strdup(icon);
00098 gchar *filepath = g_utf8_strrchr(strcopy, strlen(strcopy), '.');
00099 *filepath = '\0';
00100
00101
00102
00103 if (strcmp(state, "normal") ==0)
00104 {
00105 iconfile = g_strdup_printf("%s%s", strcopy, fileext);
00106 }
00107 else
00108 {
00109 iconfile = g_strdup_printf("%s_%s%s", strcopy, state, fileext);
00110 }
00111 g_free(strcopy);
00112 }
00113
00114 GdkPixbuf* pixbuf = load_icon(iconfile);
00115 g_free(iconfile);
00116 return pixbuf;
00117 }
00118
00119
00120 GdkPixbuf *pixlist_toolbar_icon(const char *name, const char *state)
00121 {
00122 GdkPixbuf* icon = NULL;
00123 if (name != NULL && name[0] != 0) {
00124 static char filename[256];
00125 snprintf(filename, sizeof(filename), "%stoolbar_%s_%s.png", PATH_IMG, name, state);
00126 icon = load_icon(filename);
00127 if (icon == NULL) {
00128 WARNPRINTF("cannot find icon %s", filename);
00129 }
00130 }
00131 if (icon == NULL) {
00132 icon = load_icon(PATH_IMG"toolbar_blank.png");
00133 }
00134 return icon;
00135 }
00136