FIX:build: Make --disable-nls and --with-included-gettext work.
[navit-package] / navit / gui / gtk / gui_gtk_statusbar.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <math.h>
24 #include <gtk/gtk.h>
25 #include "item.h"
26 #include "coord.h"
27 #include "debug.h"
28 #include "vehicle.h"
29 #include "callback.h"
30 #include "route.h"
31 #include "transform.h"
32 #include "navit.h"
33 #include "map.h"
34 #include "navigation.h"
35 #include "gui_gtk.h"
36 #include "navit_nls.h"
37
38 struct statusbar_priv {
39         struct gui_priv *gui;
40         GtkWidget *hbox;
41         char gps_text[128];
42         GtkWidget *gps;
43         char route_text[128];
44         GtkWidget *route;
45         struct callback *vehicle_cb;
46 };
47
48
49
50
51 static void
52 statusbar_destroy(struct statusbar_priv *this)
53 {
54         g_free(this);
55 }
56
57 static void
58 statusbar_gps_update(struct statusbar_priv *this, int sats, int qual, double lng, double lat, double height, double direction, double speed)
59 {
60         char lat_c='N';
61         char lng_c='E';
62     char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
63         char *dir;
64         int dir_idx;
65
66         if (lng < 0) {
67                 lng=-lng;
68                 lng_c='W';
69         }
70         if (lat < 0) {
71                 lat=-lat;
72                 lat_c='S';
73         }
74         dir_idx=(direction+22.5)/45;
75         dir=dirs[dir_idx];
76         sprintf(this->gps_text, "GPS %2d/%1d %02.0f%07.4f%c %03.0f%07.4f%c %4.0fm %3.0f°%-2s %3.0fkm/h", sats, qual, floor(lat), fmod(lat*60,60), lat_c, floor(lng), fmod(lng*60,60), lng_c, height, direction, dir, speed);
77         gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
78
79 }
80
81 static void
82 statusbar_route_update(struct statusbar_priv *this, struct navit *navit, struct vehicle *v)
83 {
84         struct navigation *nav=NULL;
85         struct map *map=NULL;
86         struct map_rect *mr=NULL;
87         struct item *item=NULL;
88         struct attr attr;
89         double route_len=0;
90         time_t eta;
91         struct tm *eta_tm=NULL;
92         char buffer[128];
93         double lng, lat, direction=0, height=0, speed=0;
94         int sats=0, qual=0;
95         char lat_c='N';
96         char lng_c='E';
97     char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
98         char *dir;
99         int dir_idx;
100
101         if (navit)
102                 nav=navit_get_navigation(navit);
103         if (nav)
104                 map=navigation_get_map(nav);
105         if (map)
106                 mr=map_rect_new(map, NULL);
107         if (mr)
108                 item=map_rect_get_item(mr);
109         if (item) {
110                 if (item_attr_get(item, attr_destination_length, &attr))
111                         route_len=attr.u.num;
112                 if (item_attr_get(item, attr_destination_time, &attr)) {
113                         eta=time(NULL)+attr.u.num/10;
114                         eta_tm=localtime(&eta);
115                 }
116         }
117         if (mr)
118                 map_rect_destroy(mr);
119         sprintf(buffer,_("Route %4.0fkm    %02d:%02d ETA" ),route_len/1000, eta_tm ? eta_tm->tm_hour : 0 , eta_tm ? eta_tm->tm_min : 0);
120         if (strcmp(buffer, this->route_text)) {
121                 strcpy(this->route_text, buffer);
122                 gtk_label_set_text(GTK_LABEL(this->route), this->route_text);
123         }
124         if (!vehicle_get_attr(v, attr_position_coord_geo, &attr, NULL))
125                 return;
126         lng=attr.u.coord_geo->lng;
127         lat=attr.u.coord_geo->lat;
128         if (lng < 0) {
129                 lng=-lng;
130                 lng_c='W';
131         }
132         if (lat < 0) {
133                 lat=-lat;
134                 lat_c='S';
135         }
136         if (vehicle_get_attr(v, attr_position_direction, &attr, NULL))
137                 direction=*(attr.u.numd);
138         direction=fmod(direction,360);
139         if (direction < 0)
140                 direction+=360;
141         dir_idx=(direction+22.5)/45;
142         dir=dirs[dir_idx];
143         if (vehicle_get_attr(v, attr_position_height, &attr, NULL))
144                 height=*(attr.u.numd);
145         if (vehicle_get_attr(v, attr_position_speed, &attr, NULL))
146                 speed=*(attr.u.numd);
147         if (vehicle_get_attr(v, attr_position_sats_used, &attr, NULL))
148                 sats=attr.u.num;
149         if (vehicle_get_attr(v, attr_position_qual, &attr, NULL))
150                 qual=attr.u.num;
151         sprintf(this->gps_text,"GPS %2d/%1d %02.0f%07.4f%c %03.0f%07.4f%c %4.0fm %3.0f°%-2s %3.0fkm/h", sats, qual, floor(lat), fmod(lat*60,60), lat_c, floor(lng), fmod(lng*60,60), lng_c, height, direction, dir, speed);
152         gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
153 }
154
155 struct statusbar_priv *
156 gui_gtk_statusbar_new(struct gui_priv *gui)
157 {
158         struct statusbar_priv *this=g_new0(struct statusbar_priv, 1);
159
160         this->gui=gui;
161         this->hbox=gtk_hbox_new(FALSE, 1);
162         this->gps=gtk_label_new( "GPS 00/0 0000.0000N 00000.0000E 0000m 000°NO 000km/h" );
163         gtk_label_set_justify(GTK_LABEL(this->gps),  GTK_JUSTIFY_LEFT);
164         this->route=gtk_label_new( _( "Route 0000km  0+00:00 ETA" ) );
165         gtk_label_set_justify(GTK_LABEL(this->route),  GTK_JUSTIFY_LEFT);
166         gtk_box_pack_start(GTK_BOX(this->hbox), this->gps, TRUE, TRUE, 2);
167         gtk_box_pack_start(GTK_BOX(this->hbox), gtk_vseparator_new(), TRUE, TRUE, 2);
168         gtk_box_pack_start(GTK_BOX(this->hbox), this->route, TRUE, TRUE, 2);
169         GTK_WIDGET_UNSET_FLAGS (this->hbox, GTK_CAN_FOCUS);
170
171         gtk_box_pack_end(GTK_BOX(gui->vbox), this->hbox, FALSE, FALSE, 0);
172         gtk_widget_show_all(this->hbox);
173         /* add a callback for position updates */
174         this->vehicle_cb=callback_new_attr_1(callback_cast(statusbar_route_update), attr_position_coord_geo, this);
175         navit_add_callback(gui->nav, this->vehicle_cb);
176         return this;
177 }
178