Parent Directory | Revision Log
Initial import
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of OSM2Go. |
5 | * |
6 | * OSM2Go is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * OSM2Go is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <sys/stat.h> |
21 | #include "appdata.h" |
22 | |
23 | static const char *icon_exts[] = { |
24 | "gif", "png", "jpg", "" |
25 | }; |
26 | |
27 | static gchar* |
28 | icon_file_exists(const gchar *file) { |
29 | gchar *filename, *fullname; |
30 | gint idx = 0; |
31 | |
32 | while(icon_exts[idx][0]) { |
33 | filename = g_strdup_printf("icons/%s.%s", file, icon_exts[idx]); |
34 | fullname = find_file(filename); |
35 | g_free(filename); |
36 | |
37 | if(fullname) |
38 | return fullname; |
39 | |
40 | idx++; |
41 | } |
42 | return NULL; |
43 | } |
44 | |
45 | GdkPixbuf *icon_load(icon_t **icon, const char *name) { |
46 | if(!name) return NULL; |
47 | |
48 | /* check if icon list already contains an icon of that name */ |
49 | while(*icon) { |
50 | if(strcmp(name, (*icon)->name) == 0) { |
51 | // printf("reuse existing icon\n"); |
52 | (*icon)->use++; |
53 | return (*icon)->buf; |
54 | } |
55 | |
56 | icon = &((*icon)->next); |
57 | } |
58 | |
59 | gchar *fullname = icon_file_exists(name); |
60 | if(fullname) { |
61 | GdkPixbuf *pix = gdk_pixbuf_new_from_file(fullname, NULL); |
62 | g_free(fullname); |
63 | |
64 | // printf("Successfully loaded icon %s to %p\n", name, pix); |
65 | *icon = g_new0(icon_t, 1); |
66 | (*icon)->name = strdup(name); |
67 | (*icon)->buf = pix; |
68 | (*icon)->use = 1; |
69 | |
70 | return pix; |
71 | } |
72 | |
73 | printf("Icon %s not found\n", name); |
74 | return NULL; |
75 | } |
76 | |
77 | GtkWidget *icon_widget_load(icon_t **icon, const char *name) { |
78 | GdkPixbuf *pix = icon_load(icon, name); |
79 | if(!pix) return NULL; |
80 | |
81 | return gtk_image_new_from_pixbuf(pix); |
82 | } |
83 | |
84 | void icon_free(icon_t **icon, GdkPixbuf *buf) { |
85 | // printf("request to free icon %p\n", buf); |
86 | |
87 | while(*icon) { |
88 | // printf(" -> %s %p\n", (*icon)->name, (*icon)->buf); |
89 | |
90 | if(buf == (*icon)->buf) { |
91 | (*icon)->use--; |
92 | if(!(*icon)->use) { |
93 | printf("freeing unused icon %s\n", (*icon)->name); |
94 | |
95 | g_free((*icon)->name); |
96 | gdk_pixbuf_unref((*icon)->buf); |
97 | icon_t *next = (*icon)->next; |
98 | g_free(*icon); |
99 | *icon = next; |
100 | |
101 | } else |
102 | printf("keeping icon %s still in use by %d\n", |
103 | (*icon)->name, (*icon)->use); |
104 | |
105 | return; |
106 | } |
107 | icon = &((*icon)->next); |
108 | } |
109 | |
110 | printf("ERROR: icon to be freed not found\n"); |
111 | } |
112 | |
113 | void icon_free_all(icon_t **icons) { |
114 | int cnt = 0; |
115 | |
116 | icon_t *icon = *icons; |
117 | while(icon) { |
118 | cnt++; |
119 | g_free(icon->name); |
120 | gdk_pixbuf_unref(icon->buf); |
121 | icon_t *next = icon->next; |
122 | g_free(icon); |
123 | icon = next; |
124 | } |
125 | |
126 | *icons = NULL; |
127 | |
128 | printf("freed %d icons\n", cnt); |
129 | } |