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