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