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