Parent Directory | Revision Log
Cache icons SVG
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView 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 | * GPXView 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 GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "gpxview.h" |
21 | #include <math.h> // for isnan() |
22 | |
23 | struct icon_data { |
24 | GdkPixbuf **data; |
25 | int count; |
26 | } icons[] = { |
27 | { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE */ |
28 | { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE_SEMI */ |
29 | { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE_2X */ |
30 | { NULL, CACHE_CONT_MAX+1 }, /* ICON_CACHE_SIZE */ |
31 | { NULL, 9 }, /* ICON_STARS */ |
32 | { NULL, LOG_TYPE_MAX+1 }, /* ICON_LOG */ |
33 | { NULL, 9 }, /* ICON_HEADING */ |
34 | { NULL, WPT_SYM_MAX+1 }, /* ICON_WPT */ |
35 | { NULL, 1 }, /* ICON_TB */ |
36 | { NULL, 9 }, /* ICON_MISC */ |
37 | { NULL, 3 }, /* ICON_FILE */ |
38 | { NULL, -1 } |
39 | }; |
40 | |
41 | /* ICON_CACHE_TYPE / ICON_CACHE_TYPE_SEMI / ICON_CACHE_TYPE_2X */ |
42 | const char *cache_type_icon_name[] = { |
43 | "traditional", "multi", "mystery", "virtual", "webcam", "event", |
44 | "letterbox", "earthcache", "wherigo", "megaevent", "cito" |
45 | }; |
46 | |
47 | /* ICON_CACHE_SIZE */ |
48 | const char *cache_size_icon_name[] = { |
49 | "regular", "small", "micro", "other", "not_chosen", "large", |
50 | "virtual" |
51 | }; |
52 | |
53 | /* ICON_STARS */ |
54 | const char *stars_icon_name[] = { |
55 | "1", "1_5", "2", "2_5", "3", "3_5", "4", "4_5", "5" |
56 | }; |
57 | |
58 | /* ICON_LOG */ |
59 | const char *log_icon_name[] = { |
60 | "smile", "sad", "maint", "note", "big_smile", "enabled", |
61 | "greenlight", "rsvp", "attended", "camera", "disabled", |
62 | "needsmaint", "coord_update", |
63 | "traffic_cone", |
64 | "traffic_cone", /* LOG_TYPE_NEEDS_ARCHIVED */ |
65 | "traffic_cone", |
66 | }; |
67 | |
68 | /* ICON_HEADING */ |
69 | const char *heading_icon_name[] = { |
70 | "n", "ne", "e", "se", "s", "sw", "w", "nw", "none" |
71 | }; |
72 | |
73 | /* ICON_WPT */ |
74 | const char *wpt_sym_icon_name[] = { |
75 | "multistage", "parking", "final", "question", |
76 | "trailhead", "refpoint", |
77 | }; |
78 | |
79 | /* ICON_TB */ |
80 | const char *tb_icon_name[] = { |
81 | "tb_coin" |
82 | }; |
83 | |
84 | /* ICON_MISC */ |
85 | const char *misc_icon_name[] = { |
86 | "maemo-mapper-out", "override", "locked", "unlocked", "found", |
87 | "maemo-mapper-in", "note", "delete", "paypal" |
88 | }; |
89 | |
90 | /* ICON_FILE */ |
91 | const char *file_icon_name[] = { |
92 | "gc", "folder", "zip" |
93 | }; |
94 | |
95 | static void icons_load(int type, char *format, const char *names[]) { |
96 | int i; |
97 | |
98 | if(!icons[type].count) { |
99 | icons[type].data = NULL; |
100 | return; |
101 | } |
102 | |
103 | icons[type].data = malloc(sizeof(GdkPixbuf *) * icons[type].count); |
104 | |
105 | for(i=0;i<icons[type].count;i++) { |
106 | if(names[i]) { |
107 | GError *error = NULL; |
108 | char filename[128]; |
109 | strcpy(filename, ICONPATH); |
110 | snprintf(filename+strlen(filename), |
111 | sizeof(filename)-strlen(filename), format, names[i], "png"); |
112 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
113 | |
114 | if(error) { |
115 | /* try gif */ |
116 | error = NULL; |
117 | strcpy(filename, ICONPATH); |
118 | snprintf(filename+strlen(filename), |
119 | sizeof(filename)-strlen(filename), format, names[i], "gif"); |
120 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
121 | |
122 | if(error) { |
123 | error = NULL; |
124 | /* try again in local dir */ |
125 | strcpy(filename, "./data/icons/"); |
126 | snprintf(filename+strlen(filename), |
127 | sizeof(filename)-strlen(filename), format, names[i], "png"); |
128 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
129 | |
130 | if(error) { |
131 | error = NULL; |
132 | /* try gif */ |
133 | strcpy(filename, "./data/icons/"); |
134 | snprintf(filename+strlen(filename), |
135 | sizeof(filename)-strlen(filename), format, names[i], "gif"); |
136 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
137 | |
138 | |
139 | if(error) { |
140 | icons[type].data[i] = NULL; |
141 | g_warning("Could not load cache type icon %s: %s\n", |
142 | names[i], error->message); |
143 | g_error_free(error); |
144 | error = NULL; |
145 | } |
146 | } |
147 | } |
148 | } |
149 | } else |
150 | icons[type].data[i] = NULL; |
151 | } |
152 | } |
153 | |
154 | void icons_init(void) { |
155 | |
156 | /* load cache type icons */ |
157 | icons_load(ICON_CACHE_TYPE, "cache_type_%s.%s", cache_type_icon_name); |
158 | |
159 | /* load semitransparent cache type icons */ |
160 | icons_load(ICON_CACHE_TYPE_SEMI, "cache_type_%s_semi.%s", cache_type_icon_name); |
161 | |
162 | /* load double sized cache type icons */ |
163 | icons_load(ICON_CACHE_TYPE_2X, "cache_type_%s_2x.%s", cache_type_icon_name); |
164 | |
165 | /* load cache container/size icons */ |
166 | icons_load(ICON_CACHE_SIZE, "cache_size_%s.%s", cache_size_icon_name); |
167 | |
168 | /* load cache difficulty/terrain icons */ |
169 | icons_load(ICON_STARS, "stars%s.%s", stars_icon_name); |
170 | |
171 | /* load cache difficulty/terrain icons */ |
172 | icons_load(ICON_LOG, "log_icon_%s.%s", log_icon_name); |
173 | |
174 | /* load icons to visualize heading */ |
175 | icons_load(ICON_HEADING, "heading_%s.%s", heading_icon_name); |
176 | |
177 | /* load icons to visualize heading */ |
178 | icons_load(ICON_WPT, "wpt_%s.%s", wpt_sym_icon_name); |
179 | |
180 | /* load travelbug icon */ |
181 | icons_load(ICON_TB, "%s.%s", tb_icon_name); |
182 | |
183 | /* load misc icons */ |
184 | icons_load(ICON_MISC, "%s.%s", misc_icon_name); |
185 | |
186 | /* load file icons */ |
187 | icons_load(ICON_FILE, "file_%s.%s", file_icon_name); |
188 | } |
189 | |
190 | void icons_free(void) { |
191 | int i; |
192 | struct icon_data *icon = icons; |
193 | |
194 | while(icon->count >= 0) { |
195 | if(icon->count) { |
196 | for(i=0;i<icon->count;i++) |
197 | if(icon->data[i]) |
198 | gdk_pixbuf_unref(icon->data[i]); |
199 | |
200 | free(icon->data); |
201 | } |
202 | |
203 | icon++; |
204 | } |
205 | } |
206 | |
207 | GdkPixbuf *icon_get(int type, int num) { |
208 | if(num < 0) return NULL; |
209 | if(num >= icons[type].count) return NULL; |
210 | |
211 | return icons[type].data[num]; |
212 | } |
213 | |
214 | GtkWidget *icon_get_widget(int type, int num) { |
215 | GdkPixbuf *pbuf = icon_get(type, num); |
216 | if(!pbuf) return NULL; |
217 | |
218 | return gtk_image_new_from_pixbuf(pbuf); |
219 | } |
220 | |
221 | GdkPixbuf *icon_bearing(pos_t from, pos_t to) { |
222 | if(isnan(from.lat) || isnan(from.lon) || |
223 | isnan(to.lat) || isnan(to.lon)) |
224 | return icon_get(ICON_HEADING, 8); |
225 | |
226 | int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0; |
227 | |
228 | /* make sure we stay in icon bounds */ |
229 | if(idx < 0) idx += 8; |
230 | if(idx > 7) idx -= 8; |
231 | return icon_get(ICON_HEADING, idx); |
232 | } |
233 |