pixlist.c

Go to the documentation of this file.
00001 /*
00002  * File Name: pixlist.c
00003  */
00004 
00005 /*
00006  * This file is part of popupmenu.
00007  *
00008  * popupmenu is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * popupmenu is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021  
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  *
00026  * Note: possibly could have used reference count of gobject instead
00027  */
00028 
00029 //----------------------------------------------------------------------------
00030 // Include Files
00031 //----------------------------------------------------------------------------
00032 
00033 #define LOGGING_ON 0
00034 
00035 // system include files, between < >
00036 #include <gtk/gtk.h>
00037 #include <string.h>
00038 
00039 // ereader include files, between < >
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         // no icon name so don't bother to load/ref image
00077         return NULL;
00078     }
00079 
00080     gchar *iconfile = NULL;
00081     gchar *fileext = g_utf8_strrchr(icon, strlen(icon), '.');
00082     if (fileext == NULL)
00083     {
00084         // name is not a filename or path 
00085         // prefix with path and postfix with state and file extension
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         // icon is a full path
00102         // postfix with state only
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 
Generated by  doxygen 1.6.2-20100208