Contents of /trunk/src/icons.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 184 - (show annotations)
Sat Nov 14 12:28:54 2009 UTC (14 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 6808 byte(s)
Waypoint icons
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+1 }, /* ICON_CACHE_TYPE */
28 { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE_SEMI */
29 { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE_1_5X */
30 { NULL, CACHE_TYPE_MAX+1 }, /* 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, 9 }, /* 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
48 /* ICON_CACHE_SIZE */
49 const char *cache_size_icon_name[] = {
50 "regular", "small", "micro", "other", "not_chosen", "large",
51 "virtual"
52 };
53
54 /* ICON_STARS */
55 const char *stars_icon_name[] = {
56 "1", "1_5", "2", "2_5", "3", "3_5", "4", "4_5", "5"
57 };
58
59 /* ICON_LOG */
60 const char *log_icon_name[] = {
61 "smile", "sad", "maint", "note", "big_smile", "enabled",
62 "greenlight", "rsvp", "attended", "camera", "disabled",
63 "needsmaint", "coord_update",
64 "traffic_cone",
65 "traffic_cone", /* LOG_TYPE_NEEDS_ARCHIVED */
66 "traffic_cone",
67 };
68
69 /* ICON_HEADING */
70 const char *heading_icon_name[] = {
71 "n", "ne", "e", "se", "s", "sw", "w", "nw", "none"
72 };
73
74 /* ICON_WPT */
75 const char *wpt_sym_icon_name[] = {
76 "multistage", "parking", "final", "question",
77 "trailhead", "refpoint",
78 };
79
80 /* ICON_TB */
81 const char *tb_icon_name[] = {
82 "tb", "coin"
83 };
84
85 /* ICON_MISC */
86 const char *misc_icon_name[] = {
87 "maemo-mapper-out", "override", "locked", "unlocked", "found",
88 "maemo-mapper-in", "note", "delete", "paypal"
89 };
90
91 /* ICON_FILE */
92 const char *file_icon_name[] = {
93 "gc", "folder", "zip"
94 };
95
96 static void icons_load(int type, char *format, const char *names[]) {
97 int i;
98
99 if(!icons[type].count) {
100 icons[type].data = NULL;
101 return;
102 }
103
104 icons[type].data = malloc(sizeof(GdkPixbuf *) * icons[type].count);
105
106 for(i=0;i<icons[type].count;i++) {
107 if(names[i]) {
108 GError *error = NULL;
109 char filename[128];
110 strcpy(filename, ICONPATH);
111 snprintf(filename+strlen(filename),
112 sizeof(filename)-strlen(filename), format, names[i], "png");
113 icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error);
114
115 if(error) {
116 /* try gif */
117 error = NULL;
118 strcpy(filename, ICONPATH);
119 snprintf(filename+strlen(filename),
120 sizeof(filename)-strlen(filename), format, names[i], "gif");
121 icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error);
122
123 if(error) {
124 error = NULL;
125 /* try again in local dir */
126 strcpy(filename, "./data/icons/");
127 snprintf(filename+strlen(filename),
128 sizeof(filename)-strlen(filename), format, names[i], "png");
129 icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error);
130
131 if(error) {
132 error = NULL;
133 /* try gif */
134 strcpy(filename, "./data/icons/");
135 snprintf(filename+strlen(filename),
136 sizeof(filename)-strlen(filename), format, names[i], "gif");
137 icons[type].data[i] = gdk_pixbuf_new_from_file(filename, &error);
138
139
140 if(error) {
141 icons[type].data[i] = NULL;
142 g_warning("Could not load cache type icon %s: %s\n",
143 names[i], error->message);
144 g_error_free(error);
145 error = NULL;
146 }
147 }
148 }
149 }
150 } else
151 icons[type].data[i] = NULL;
152 }
153 }
154
155 void icons_init(void) {
156
157 /* load cache type icons */
158 icons_load(ICON_CACHE_TYPE, "32x32/cache_type_%s.%s", cache_type_icon_name);
159
160 /* load semitransparent cache type icons */
161 icons_load(ICON_CACHE_TYPE_SEMI, "32x32/cache_type_%s_semi.%s", cache_type_icon_name);
162
163 /* load 150% sized cache type icons */
164 icons_load(ICON_CACHE_TYPE_1_5X, "48x48/cache_type_%s.%s", cache_type_icon_name);
165
166 /* load double sized cache type icons */
167 icons_load(ICON_CACHE_TYPE_2X, "64x64/cache_type_%s.%s", cache_type_icon_name);
168
169 /* load cache container/size icons */
170 icons_load(ICON_CACHE_SIZE, "cache_size_%s.%s", cache_size_icon_name);
171
172 /* load cache difficulty/terrain/quality icons */
173 icons_load(ICON_STARS, "stars%s.%s", stars_icon_name);
174
175 /* load cache log icons */
176 icons_load(ICON_LOG, "32x32/log_icon_%s.%s", log_icon_name);
177
178 /* load icons to visualize heading */
179 icons_load(ICON_HEADING, "heading_%s.%s", heading_icon_name);
180
181 /* load icons to visualize heading */
182 icons_load(ICON_WPT, "32x32/wpt_%s.%s", wpt_sym_icon_name);
183
184 /* load travelbug icon */
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