Fix:Core:Renamed src to navit for cleanup of includes
[navit-package] / navit / gui / gtk / gui_gtk_statusbar.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #include <math.h>
5 #include <gtk/gtk.h>
6 #include <libintl.h>
7 #include "item.h"
8 #include "coord.h"
9 #include "debug.h"
10 #include "vehicle.h"
11 #include "callback.h"
12 #include "route.h"
13 #include "transform.h"
14 #include "navit.h"
15 #include "map.h"
16 #include "navigation.h"
17 #include "gui_gtk.h"
18
19
20 #define _(STRING) gettext(STRING)
21
22 struct statusbar_priv {
23         struct gui_priv *gui;
24         GtkWidget *hbox;
25         char gps_text[128];
26         GtkWidget *gps;
27         char route_text[128];
28         GtkWidget *route;
29         struct callback *vehicle_cb;
30 };
31
32
33
34
35 static void
36 statusbar_destroy(struct statusbar_priv *this)
37 {
38         g_free(this);
39 }
40
41 static void
42 statusbar_gps_update(struct statusbar_priv *this, int sats, int qual, double lng, double lat, double height, double direction, double speed)
43 {
44         char lat_c='N';
45         char lng_c='E';
46     char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
47         char *dir;
48         int dir_idx;
49
50         if (lng < 0) {
51                 lng=-lng;
52                 lng_c='W';
53         }
54         if (lat < 0) {
55                 lat=-lat;
56                 lat_c='S';
57         }
58         dir_idx=(direction+22.5)/45;
59         dir=dirs[dir_idx];
60         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);
61         gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
62
63 }
64
65 static void
66 statusbar_route_update(struct statusbar_priv *this, struct navit *navit, struct vehicle *v)
67 {
68         struct navigation *nav=NULL;
69         struct map *map=NULL;
70         struct map_rect *mr=NULL;
71         struct item *item=NULL;
72         struct attr attr;
73         double route_len=0;
74         time_t eta;
75         struct tm *eta_tm=NULL;
76         char buffer[128];
77         double lng, lat, direction=0, height=0, speed=0;
78         int sats=0, qual=0;
79         char lat_c='N';
80         char lng_c='E';
81     char *dirs[]={_("N"),_("NE"),_("E"),_("SE"),_("S"),_("SW"),_("W"),_("NW"),_("N")};
82         char *dir;
83         int dir_idx;
84
85         if (navit)
86                 nav=navit_get_navigation(navit);
87         if (nav)
88                 map=navigation_get_map(nav);
89         if (map)
90                 mr=map_rect_new(map, NULL);
91         if (mr)
92                 item=map_rect_get_item(mr);
93         if (item) {
94                 if (item_attr_get(item, attr_destination_length, &attr))
95                         route_len=attr.u.num;
96                 if (item_attr_get(item, attr_destination_time, &attr)) {
97                         eta=time(NULL)+attr.u.num/10;
98                         eta_tm=localtime(&eta);
99                 }
100         }
101         if (mr)
102                 map_rect_destroy(mr);
103         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);
104         if (strcmp(buffer, this->route_text)) {
105                 strcpy(this->route_text, buffer);
106                 gtk_label_set_text(GTK_LABEL(this->route), this->route_text);
107         }
108         if (!vehicle_get_attr(v, attr_position_coord_geo, &attr))
109                 return;
110         lng=attr.u.coord_geo->lng;
111         lat=attr.u.coord_geo->lat;
112         if (lng < 0) {
113                 lng=-lng;
114                 lng_c='W';
115         }
116         if (lat < 0) {
117                 lat=-lat;
118                 lat_c='S';
119         }
120         if (vehicle_get_attr(v, attr_position_direction, &attr))
121                 direction=*(attr.u.numd);
122         direction=fmod(direction,360);
123         if (direction < 0)
124                 direction+=360;
125         dir_idx=(direction+22.5)/45;
126         dir=dirs[dir_idx];
127         if (vehicle_get_attr(v, attr_position_height, &attr))
128                 height=*(attr.u.numd);
129         if (vehicle_get_attr(v, attr_position_speed, &attr))
130                 speed=*(attr.u.numd);
131         if (vehicle_get_attr(v, attr_position_sats_used, &attr))
132                 sats=attr.u.num;
133         if (vehicle_get_attr(v, attr_position_qual, &attr))
134                 qual=attr.u.num;
135         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);
136         gtk_label_set_text(GTK_LABEL(this->gps), this->gps_text);
137 }
138
139 struct statusbar_priv *
140 gui_gtk_statusbar_new(struct gui_priv *gui)
141 {
142         struct statusbar_priv *this=g_new0(struct statusbar_priv, 1);
143
144         this->gui=gui;
145         this->hbox=gtk_hbox_new(FALSE, 1);
146         this->gps=gtk_label_new( "GPS 00/0 0000.0000N 00000.0000E 0000m 000°NO 000km/h" );
147         gtk_label_set_justify(GTK_LABEL(this->gps),  GTK_JUSTIFY_LEFT);
148         this->route=gtk_label_new( _( "Route 0000km  0+00:00 ETA" ) );
149         gtk_label_set_justify(GTK_LABEL(this->route),  GTK_JUSTIFY_LEFT);
150         gtk_box_pack_start(GTK_BOX(this->hbox), this->gps, TRUE, TRUE, 2);
151         gtk_box_pack_start(GTK_BOX(this->hbox), gtk_vseparator_new(), TRUE, TRUE, 2);
152         gtk_box_pack_start(GTK_BOX(this->hbox), this->route, TRUE, TRUE, 2);
153         GTK_WIDGET_UNSET_FLAGS (this->hbox, GTK_CAN_FOCUS);
154
155         gtk_box_pack_end(GTK_BOX(gui->vbox), this->hbox, FALSE, FALSE, 0);
156         gtk_widget_show_all(this->hbox);
157         /* add a callback for position updates */
158         this->vehicle_cb=callback_new_attr_1(callback_cast(statusbar_route_update), attr_position_coord_geo, this);
159         navit_add_callback(gui->nav, this->vehicle_cb);
160         return this;
161 }
162