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