Parent Directory | Revision Log
Map/cache icon handling
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 | 205 | { 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 | 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 | 189 | { NULL, 6 }, /* ICON_MISC */ |
38 | harbaum | 151 | { 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 | harbaum | 205 | "ovl_override", "ovl_found", "ovl_note", "ovl_mine", |
49 | harbaum | 189 | |
50 | NULL | ||
51 | harbaum | 1 | }; |
52 | |||
53 | /* ICON_CACHE_SIZE */ | ||
54 | const char *cache_size_icon_name[] = { | ||
55 | "regular", "small", "micro", "other", "not_chosen", "large", | ||
56 | harbaum | 189 | "virtual", NULL |
57 | harbaum | 1 | }; |
58 | |||
59 | /* ICON_STARS */ | ||
60 | const char *stars_icon_name[] = { | ||
61 | harbaum | 189 | "1", "1_5", "2", "2_5", "3", "3_5", "4", "4_5", "5", NULL |
62 | harbaum | 1 | }; |
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 | harbaum | 189 | "traffic_cone", |
72 | NULL | ||
73 | harbaum | 1 | }; |
74 | |||
75 | /* ICON_HEADING */ | ||
76 | const char *heading_icon_name[] = { | ||
77 | harbaum | 189 | "n", "ne", "e", "se", "s", "sw", "w", "nw", "none", NULL |
78 | harbaum | 1 | }; |
79 | |||
80 | /* ICON_WPT */ | ||
81 | const char *wpt_sym_icon_name[] = { | ||
82 | "multistage", "parking", "final", "question", | ||
83 | harbaum | 189 | "trailhead", "refpoint", NULL |
84 | harbaum | 1 | }; |
85 | |||
86 | /* ICON_TB */ | ||
87 | const char *tb_icon_name[] = { | ||
88 | harbaum | 189 | "tb", "coin", NULL |
89 | harbaum | 1 | }; |
90 | |||
91 | /* ICON_MISC */ | ||
92 | const char *misc_icon_name[] = { | ||
93 | harbaum | 189 | "maemo-mapper-out", "locked", "unlocked", |
94 | "maemo-mapper-in", "delete", "paypal", NULL | ||
95 | harbaum | 1 | }; |
96 | |||
97 | /* ICON_FILE */ | ||
98 | const char *file_icon_name[] = { | ||
99 | harbaum | 189 | "gc", "folder", "zip", NULL |
100 | harbaum | 1 | }; |
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 | harbaum | 189 | g_assert(names[i]); |
114 | harbaum | 1 | |
115 | harbaum | 189 | 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 | harbaum | 1 | if(error) { |
131 | harbaum | 189 | 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 | harbaum | 1 | error = NULL; |
136 | harbaum | 189 | } |
137 | } | ||
138 | harbaum | 1 | } |
139 | harbaum | 189 | |
140 | g_assert(!names[i]); | ||
141 | harbaum | 1 | } |
142 | |||
143 | void icons_init(void) { | ||
144 | |||
145 | /* load cache type icons */ | ||
146 | harbaum | 180 | icons_load(ICON_CACHE_TYPE, "32x32/cache_type_%s.%s", cache_type_icon_name); |
147 | harbaum | 1 | |
148 | harbaum | 174 | /* load semitransparent cache type icons */ |
149 | harbaum | 180 | icons_load(ICON_CACHE_TYPE_SEMI, "32x32/cache_type_%s_semi.%s", cache_type_icon_name); |
150 | harbaum | 151 | |
151 | harbaum | 176 | /* load 150% sized cache type icons */ |
152 | harbaum | 180 | icons_load(ICON_CACHE_TYPE_1_5X, "48x48/cache_type_%s.%s", cache_type_icon_name); |
153 | harbaum | 176 | |
154 | harbaum | 174 | /* load double sized cache type icons */ |
155 | harbaum | 180 | icons_load(ICON_CACHE_TYPE_2X, "64x64/cache_type_%s.%s", cache_type_icon_name); |
156 | harbaum | 174 | |
157 | harbaum | 1 | /* load cache container/size icons */ |
158 | harbaum | 187 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
159 | harbaum | 185 | icons_load(ICON_CACHE_SIZE, "45x12/cache_size_%s.%s", cache_size_icon_name); |
160 | harbaum | 187 | #else |
161 | icons_load(ICON_CACHE_SIZE, "90x24/cache_size_%s.%s", cache_size_icon_name); | ||
162 | #endif | ||
163 | harbaum | 1 | |
164 | harbaum | 180 | /* load cache difficulty/terrain/quality icons */ |
165 | harbaum | 187 | #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 | harbaum | 1 | |
171 | harbaum | 180 | /* load cache log icons */ |
172 | icons_load(ICON_LOG, "32x32/log_icon_%s.%s", log_icon_name); | ||
173 | harbaum | 1 | |
174 | /* load icons to visualize heading */ | ||
175 | harbaum | 187 | #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 | harbaum | 1 | |
181 | harbaum | 187 | /* load waypoint icons */ |
182 | harbaum | 184 | icons_load(ICON_WPT, "32x32/wpt_%s.%s", wpt_sym_icon_name); |
183 | harbaum | 1 | |
184 | harbaum | 187 | /* load travelbug/coin icons */ |
185 | harbaum | 180 | icons_load(ICON_TB, "32x32/%s.%s", tb_icon_name); |
186 | harbaum | 1 | |
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 | harbaum | 47 | if(isnan(from.lat) || isnan(from.lon) || |
227 | isnan(to.lat) || isnan(to.lon)) | ||
228 | harbaum | 1 | 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 |