Parent Directory | Revision Log
Invalid positions marked by NANs
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 | { NULL, 8 }, /* ICON_MISC */ |
34 | { 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 | "maemo-mapper-in", "note", "delete" |
85 | }; |
86 | |
87 | /* ICON_FILE */ |
88 | const char *file_icon_name[] = { |
89 | "gc", "folder", "zip" |
90 | }; |
91 | |
92 | static void icons_load(int type, char *format, const char *names[]) { |
93 | int i; |
94 | |
95 | if(!icons[type].count) { |
96 | icons[type].data = NULL; |
97 | return; |
98 | } |
99 | |
100 | icons[type].data = malloc(sizeof(GdkPixbuf *) * icons[type].count); |
101 | |
102 | for(i=0;i<icons[type].count;i++) { |
103 | if(names[i]) { |
104 | GError *error = NULL; |
105 | char filename[128]; |
106 | strcpy(filename, ICONPATH); |
107 | snprintf(filename+strlen(filename), |
108 | sizeof(filename)-strlen(filename), format, names[i], "png"); |
109 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
110 | |
111 | if(error) { |
112 | /* try gif */ |
113 | error = NULL; |
114 | strcpy(filename, ICONPATH); |
115 | snprintf(filename+strlen(filename), |
116 | sizeof(filename)-strlen(filename), format, names[i], "gif"); |
117 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
118 | |
119 | if(error) { |
120 | error = NULL; |
121 | /* try again in local dir */ |
122 | strcpy(filename, "./data/icons/"); |
123 | snprintf(filename+strlen(filename), |
124 | sizeof(filename)-strlen(filename), format, names[i], "png"); |
125 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
126 | |
127 | if(error) { |
128 | error = NULL; |
129 | /* try gif */ |
130 | strcpy(filename, "./data/icons/"); |
131 | snprintf(filename+strlen(filename), |
132 | sizeof(filename)-strlen(filename), format, names[i], "gif"); |
133 | icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error); |
134 | |
135 | |
136 | if(error) { |
137 | icons[type].data[i] = NULL; |
138 | g_warning("Could not load cache type icon %s: %s\n", |
139 | names[i], error->message); |
140 | g_error_free(error); |
141 | error = NULL; |
142 | } |
143 | } |
144 | } |
145 | } |
146 | } else |
147 | icons[type].data[i] = NULL; |
148 | } |
149 | } |
150 | |
151 | void icons_init(void) { |
152 | |
153 | /* load cache type icons */ |
154 | icons_load(ICON_CACHE_TYPE, "cache_type_%s.%s", cache_type_icon_name); |
155 | |
156 | /* load cache container/size icons */ |
157 | icons_load(ICON_CACHE_SIZE, "cache_size_%s.%s", cache_size_icon_name); |
158 | |
159 | /* load cache difficulty/terrain icons */ |
160 | icons_load(ICON_STARS, "stars%s.%s", stars_icon_name); |
161 | |
162 | /* load cache difficulty/terrain icons */ |
163 | icons_load(ICON_LOG, "log_icon_%s.%s", log_icon_name); |
164 | |
165 | /* load icons to visualize heading */ |
166 | icons_load(ICON_HEADING, "heading_%s.%s", heading_icon_name); |
167 | |
168 | /* load icons to visualize heading */ |
169 | icons_load(ICON_WPT, "wpt_%s.%s", wpt_sym_icon_name); |
170 | |
171 | /* load travelbug icon */ |
172 | icons_load(ICON_TB, "%s.%s", tb_icon_name); |
173 | |
174 | /* load misc icons */ |
175 | icons_load(ICON_MISC, "%s.%s", misc_icon_name); |
176 | |
177 | /* load file icons */ |
178 | icons_load(ICON_FILE, "file_%s.%s", file_icon_name); |
179 | } |
180 | |
181 | void icons_free(void) { |
182 | int i; |
183 | struct icon_data *icon = icons; |
184 | |
185 | while(icon->count >= 0) { |
186 | if(icon->count) { |
187 | for(i=0;i<icon->count;i++) |
188 | if(icon->data[i]) |
189 | gdk_pixbuf_unref(icon->data[i]); |
190 | |
191 | free(icon->data); |
192 | } |
193 | |
194 | icon++; |
195 | } |
196 | } |
197 | |
198 | GdkPixbuf *icon_get(int type, int num) { |
199 | if(num < 0) return NULL; |
200 | if(num >= icons[type].count) return NULL; |
201 | |
202 | return icons[type].data[num]; |
203 | } |
204 | |
205 | GtkWidget *icon_get_widget(int type, int num) { |
206 | GdkPixbuf *pbuf = icon_get(type, num); |
207 | if(!pbuf) return NULL; |
208 | |
209 | return gtk_image_new_from_pixbuf(pbuf); |
210 | } |
211 | |
212 | GdkPixbuf *icon_bearing(pos_t from, pos_t to) { |
213 | if(isnan(from.lat) && isnan(from.lon)) |
214 | return icon_get(ICON_HEADING, 8); |
215 | |
216 | int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0; |
217 | |
218 | /* make sure we stay in icon bounds */ |
219 | if(idx < 0) idx += 8; |
220 | if(idx > 7) idx -= 8; |
221 | return icon_get(ICON_HEADING, idx); |
222 | } |
223 |