Contents of /trunk/src/icons.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (hide annotations)
Wed Aug 12 19:18:53 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 6169 byte(s)
GPS accuracy indicator in map working
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     { NULL, CACHE_TYPE_MAX+1 }, /* ICON_CACHE_TYPE */
28     { NULL, CACHE_CONT_MAX+1 }, /* ICON_CACHE_SIZE */
29     { NULL, 9 }, /* ICON_STARS */
30     { NULL, LOG_TYPE_MAX+1 }, /* ICON_LOG */
31     { NULL, 9 }, /* ICON_HEADING */
32     { NULL, WPT_SYM_MAX+1 }, /* ICON_WPT */
33     { NULL, 1 }, /* ICON_TB */
34 harbaum 51 { NULL, 12 }, /* ICON_MISC */
35 harbaum 1 { NULL, 3 }, /* ICON_FILE */
36     { NULL, -1 }
37     };
38    
39     /* ICON_CACHE_TYPE */
40     const char *cache_type_icon_name[] = {
41     "traditional", "multi", "mystery", "virtual", "webcam", "event",
42     "letterbox", "earthcache", "wherigo", "megaevent", "cito"
43     };
44    
45     /* ICON_CACHE_SIZE */
46     const char *cache_size_icon_name[] = {
47     "regular", "small", "micro", "other", "not_chosen", "large",
48     "virtual"
49     };
50    
51     /* ICON_STARS */
52     const char *stars_icon_name[] = {
53     "1", "1_5", "2", "2_5", "3", "3_5", "4", "4_5", "5"
54     };
55    
56     /* ICON_LOG */
57     const char *log_icon_name[] = {
58     "smile", "sad", "maint", "note", "big_smile", "enabled",
59     "greenlight", "rsvp", "attended", "camera", "disabled",
60     "needsmaint", "coord_update",
61     "traffic_cone",
62     "traffic_cone", /* LOG_TYPE_NEEDS_ARCHIVED */
63     "traffic_cone",
64     };
65    
66     /* ICON_HEADING */
67     const char *heading_icon_name[] = {
68     "n", "ne", "e", "se", "s", "sw", "w", "nw", "none"
69     };
70    
71     /* ICON_WPT */
72     const char *wpt_sym_icon_name[] = {
73     "multistage", "parking", "final", "question",
74     "trailhead", "refpoint",
75     };
76    
77     /* ICON_TB */
78     const char *tb_icon_name[] = {
79     "tb_coin"
80     };
81    
82     /* ICON_MISC */
83     const char *misc_icon_name[] = {
84     "maemo-mapper-out", "override", "locked", "unlocked", "found",
85 harbaum 51 "maemo-mapper-in", "note", "delete", "paypal",
86     "user", "zoomin", "zoomout"
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     /* load cache container/size icons */
159     icons_load(ICON_CACHE_SIZE, "cache_size_%s.%s", cache_size_icon_name);
160    
161     /* load cache difficulty/terrain icons */
162     icons_load(ICON_STARS, "stars%s.%s", stars_icon_name);
163    
164     /* load cache difficulty/terrain icons */
165     icons_load(ICON_LOG, "log_icon_%s.%s", log_icon_name);
166    
167     /* load icons to visualize heading */
168     icons_load(ICON_HEADING, "heading_%s.%s", heading_icon_name);
169    
170     /* load icons to visualize heading */
171     icons_load(ICON_WPT, "wpt_%s.%s", wpt_sym_icon_name);
172    
173     /* load travelbug icon */
174     icons_load(ICON_TB, "%s.%s", tb_icon_name);
175    
176     /* load misc icons */
177     icons_load(ICON_MISC, "%s.%s", misc_icon_name);
178    
179     /* load file icons */
180     icons_load(ICON_FILE, "file_%s.%s", file_icon_name);
181     }
182    
183     void icons_free(void) {
184     int i;
185     struct icon_data *icon = icons;
186    
187     while(icon->count >= 0) {
188     if(icon->count) {
189     for(i=0;i<icon->count;i++)
190     if(icon->data[i])
191     gdk_pixbuf_unref(icon->data[i]);
192    
193     free(icon->data);
194     }
195    
196     icon++;
197     }
198     }
199    
200     GdkPixbuf *icon_get(int type, int num) {
201     if(num < 0) return NULL;
202     if(num >= icons[type].count) return NULL;
203    
204     return icons[type].data[num];
205     }
206    
207     GtkWidget *icon_get_widget(int type, int num) {
208     GdkPixbuf *pbuf = icon_get(type, num);
209     if(!pbuf) return NULL;
210    
211     return gtk_image_new_from_pixbuf(pbuf);
212     }
213    
214     GdkPixbuf *icon_bearing(pos_t from, pos_t to) {
215 harbaum 47 if(isnan(from.lat) || isnan(from.lon) ||
216     isnan(to.lat) || isnan(to.lon))
217 harbaum 1 return icon_get(ICON_HEADING, 8);
218    
219     int idx = (gpx_pos_get_bearing(from, to)+22.5)/45.0;
220    
221     /* make sure we stay in icon bounds */
222     if(idx < 0) idx += 8;
223     if(idx > 7) idx -= 8;
224     return icon_get(ICON_HEADING, idx);
225     }
226