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