Document previous patch
[viking.git] / src / viktrwlayer_tpwin.c
blob406d3925c2bbec382736ff7b4d50510e49328ca3
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <gtk/gtk.h>
27 #include <glib/gi18n.h>
28 #include <time.h>
30 #include "coords.h"
31 #include "vikcoord.h"
32 #include "viktrack.h"
33 #include "viktrwlayer_tpwin.h"
34 #include "vikwaypoint.h"
35 #include "dialog.h"
37 #define SET_BUTTON_SENSITIVE(tpwin,num,sens) gtk_widget_set_sensitive ( GTK_WIDGET(g_list_nth_data((tpwin->buttons),(num))), (sens));
39 struct _VikTrwLayerTpwin {
40 GtkDialog parent;
41 GtkSpinButton *lat, *lon, *alt;
42 GtkLabel *track_name, *ts, *localtime, *diff_dist, *diff_time, *diff_speed;
43 GList *buttons;
44 VikTrackpoint *cur_tp;
45 gboolean cur_tp_is_endpoint; /* for join */
47 gboolean sync_to_tp_block;
50 GType vik_trw_layer_tpwin_get_type (void)
52 static GType tpwin_type = 0;
54 if (!tpwin_type)
56 static const GTypeInfo tpwin_info =
58 sizeof (VikTrwLayerTpwinClass),
59 NULL, /* base_init */
60 NULL, /* base_finalize */
61 NULL, /* class init */
62 NULL, /* class_finalize */
63 NULL, /* class_data */
64 sizeof (VikTrwLayerTpwin),
66 NULL /* instance init */
68 tpwin_type = g_type_register_static ( GTK_TYPE_DIALOG, "VikTrwLayerTpwin", &tpwin_info, 0 );
71 return tpwin_type;
74 static void tpwin_sync_ll_to_tp ( VikTrwLayerTpwin *tpwin )
76 if ( tpwin->cur_tp && (!tpwin->sync_to_tp_block) )
78 struct LatLon ll;
79 VikCoord coord;
80 ll.lat = gtk_spin_button_get_value ( tpwin->lat );
81 ll.lon = gtk_spin_button_get_value ( tpwin->lon );
82 vik_coord_load_from_latlon ( &coord, tpwin->cur_tp->coord.mode, &ll );
84 /* don't redraw unless we really have to */
85 if ( vik_coord_diff(&(tpwin->cur_tp->coord), &coord) > 0.05 ) /* may not be exact due to rounding */
87 tpwin->cur_tp->coord = coord;
88 gtk_dialog_response ( GTK_DIALOG(tpwin), VIK_TRW_LAYER_TPWIN_DATA_CHANGED );
93 static void tpwin_sync_alt_to_tp ( VikTrwLayerTpwin *tpwin )
95 if ( tpwin->cur_tp && (!tpwin->sync_to_tp_block) )
96 tpwin->cur_tp->altitude = gtk_spin_button_get_value ( tpwin->alt );
99 VikTrwLayerTpwin *vik_trw_layer_tpwin_new ( GtkWindow *parent )
101 static gchar *left_label_texts[] = { N_("<b>Part of Track:</b>"),
102 N_("<b>Latitude:</b>"),
103 N_("<b>Longitude:</b>"),
104 N_("<b>Altitude:</b>"),
105 N_("<b>Timestamp:</b>"),
106 N_("<b>Time:</b>") };
107 static gchar *right_label_texts[] = { N_("<b>Distance Difference:</b>"),
108 N_("<b>Time Difference:</b>"),
109 N_("<b>\"Speed\" Between:</b>") };
111 VikTrwLayerTpwin *tpwin = VIK_TRW_LAYER_TPWIN ( g_object_new ( VIK_TRW_LAYER_TPWIN_TYPE, NULL ) );
112 GtkWidget *main_hbox, *left_vbox, *right_vbox;
113 GtkWidget *diff_left_vbox, *diff_right_vbox;
116 gtk_window_set_transient_for ( GTK_WINDOW(tpwin), parent );
117 gtk_window_set_title ( GTK_WINDOW(tpwin), _("Trackpoint") );
119 gtk_dialog_add_buttons ( GTK_DIALOG(tpwin),
120 GTK_STOCK_CLOSE, VIK_TRW_LAYER_TPWIN_CLOSE,
121 GTK_STOCK_DELETE, VIK_TRW_LAYER_TPWIN_DELETE,
122 _("Split Here"), VIK_TRW_LAYER_TPWIN_SPLIT,
123 _("Join With Last"), VIK_TRW_LAYER_TPWIN_JOIN,
124 GTK_STOCK_GO_BACK, VIK_TRW_LAYER_TPWIN_BACK,
125 GTK_STOCK_GO_FORWARD, VIK_TRW_LAYER_TPWIN_FORWARD,
126 NULL );
127 tpwin->buttons = gtk_container_get_children(GTK_CONTAINER(GTK_DIALOG(tpwin)->action_area));
129 /* main track info */
130 left_vbox = a_dialog_create_label_vbox ( left_label_texts, sizeof(left_label_texts) / sizeof(left_label_texts[0]) );
132 tpwin->track_name = GTK_LABEL(gtk_label_new(NULL));
133 tpwin->ts = GTK_LABEL(gtk_label_new(NULL));
134 tpwin->localtime = GTK_LABEL(gtk_label_new(NULL));
136 tpwin->lat = GTK_SPIN_BUTTON(gtk_spin_button_new( GTK_ADJUSTMENT(gtk_adjustment_new (
137 0, -90, 90, 0.00005, 0.01, 0 )), 0.00005, 6));
138 tpwin->lon = GTK_SPIN_BUTTON(gtk_spin_button_new( GTK_ADJUSTMENT(gtk_adjustment_new (
139 0, -180, 180, 0.00005, 0.01, 0 )), 0.00005, 6));
141 g_signal_connect_swapped ( G_OBJECT(tpwin->lat), "value-changed", G_CALLBACK(tpwin_sync_ll_to_tp), tpwin );
142 g_signal_connect_swapped ( G_OBJECT(tpwin->lon), "value-changed", G_CALLBACK(tpwin_sync_ll_to_tp), tpwin );
144 tpwin->alt = GTK_SPIN_BUTTON(gtk_spin_button_new( GTK_ADJUSTMENT(gtk_adjustment_new (
145 0, -1000, 25000, 10, 100, 0 )), 10, 2));
147 g_signal_connect_swapped ( G_OBJECT(tpwin->alt), "value-changed", G_CALLBACK(tpwin_sync_alt_to_tp), tpwin );
149 right_vbox = gtk_vbox_new( TRUE, 3 );
150 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->track_name), FALSE, TRUE, 0 );
151 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->lat), FALSE, TRUE, 0 );
152 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->lon), FALSE, TRUE, 0 );
153 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->alt), FALSE, TRUE, 0 );
154 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->ts), FALSE, TRUE, 5 );
155 gtk_box_pack_start ( GTK_BOX(right_vbox), GTK_WIDGET(tpwin->localtime), FALSE, TRUE, 5 );
157 /* diff info */
158 diff_left_vbox = a_dialog_create_label_vbox ( right_label_texts, sizeof(right_label_texts) / sizeof(right_label_texts[0]) );
160 tpwin->diff_dist = GTK_LABEL(gtk_label_new(NULL));
161 tpwin->diff_time = GTK_LABEL(gtk_label_new(NULL));
162 tpwin->diff_speed = GTK_LABEL(gtk_label_new(NULL));
164 diff_right_vbox = gtk_vbox_new ( TRUE, 3 );
165 gtk_box_pack_start ( GTK_BOX(diff_right_vbox), GTK_WIDGET(tpwin->diff_dist), FALSE, TRUE, 5 );
166 gtk_box_pack_start ( GTK_BOX(diff_right_vbox), GTK_WIDGET(tpwin->diff_time), FALSE, TRUE, 5 );
167 gtk_box_pack_start ( GTK_BOX(diff_right_vbox), GTK_WIDGET(tpwin->diff_speed), FALSE, TRUE, 5 );
169 main_hbox = gtk_hbox_new( TRUE, 0 );
170 gtk_box_pack_start ( GTK_BOX(main_hbox), left_vbox, TRUE, TRUE, 0 );
171 gtk_box_pack_start ( GTK_BOX(main_hbox), right_vbox, TRUE, TRUE, 0 );
172 gtk_box_pack_start ( GTK_BOX(main_hbox), diff_left_vbox, TRUE, TRUE, 0 );
173 gtk_box_pack_start ( GTK_BOX(main_hbox), diff_right_vbox, TRUE, TRUE, 0 );
175 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(tpwin)->vbox), main_hbox, FALSE, FALSE, 0 );
177 tpwin->cur_tp = NULL;
179 return tpwin;
182 void vik_trw_layer_tpwin_set_empty ( VikTrwLayerTpwin *tpwin )
184 gtk_label_set_text ( tpwin->track_name, NULL );
185 gtk_label_set_text ( tpwin->ts, NULL );
186 gtk_label_set_text ( tpwin->localtime, NULL );
188 gtk_widget_set_sensitive ( GTK_WIDGET(tpwin->lat), FALSE );
189 gtk_widget_set_sensitive ( GTK_WIDGET(tpwin->lon), FALSE );
190 gtk_widget_set_sensitive ( GTK_WIDGET(tpwin->alt), FALSE );
192 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_SPLIT, FALSE );
193 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_DELETE, FALSE );
194 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_FORWARD, FALSE );
195 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_BACK, FALSE );
196 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_JOIN, FALSE );
197 gtk_label_set_text ( tpwin->diff_dist, NULL );
198 gtk_label_set_text ( tpwin->diff_time, NULL );
199 gtk_label_set_text ( tpwin->diff_speed, NULL );
200 tpwin->cur_tp = NULL;
203 void vik_trw_layer_tpwin_disable_join ( VikTrwLayerTpwin *tpwin )
205 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_JOIN, FALSE );
208 void vik_trw_layer_tpwin_set_tp ( VikTrwLayerTpwin *tpwin, GList *tpl, gchar *track_name )
210 static char tmp_str[25];
211 static struct LatLon ll;
212 VikTrackpoint *tp = VIK_TRACKPOINT(tpl->data);
214 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_DELETE, TRUE );
216 /* We can only split up a track if it's not an endpoint. Makes sense to me. */
217 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_SPLIT, tpl->next && tpl->prev );
219 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_FORWARD, (gboolean) GPOINTER_TO_INT (tpl->next) );
220 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_BACK, (gboolean) GPOINTER_TO_INT (tpl->prev) );
222 /* we can only join tracks if there was a last tp, the last tp was an endpoint, _AND_ this tp is an endpoint */
223 SET_BUTTON_SENSITIVE ( tpwin, VIK_TRW_LAYER_TPWIN_JOIN, tpwin->cur_tp && tpwin->cur_tp_is_endpoint && (!(tpl->next && tpl->prev)) );
225 gtk_widget_set_sensitive ( GTK_WIDGET(tpwin->lat), TRUE );
226 gtk_widget_set_sensitive ( GTK_WIDGET(tpwin->lon), TRUE );
228 gtk_label_set_text ( tpwin->track_name, track_name );
230 tpwin->sync_to_tp_block = TRUE; /* don't update while setting data. */
232 vik_coord_to_latlon ( &(tp->coord), &ll );
233 gtk_spin_button_set_value ( tpwin->lat, ll.lat );
234 gtk_spin_button_set_value ( tpwin->lon, ll.lon );
235 gtk_spin_button_set_value ( tpwin->alt, tp->altitude );
237 tpwin->sync_to_tp_block = FALSE; /* don't update whlie setting data. */
240 if ( tp->has_timestamp )
242 g_snprintf ( tmp_str, sizeof(tmp_str), "%ld", tp->timestamp );
243 gtk_label_set_text ( tpwin->ts, tmp_str );
244 g_snprintf ( tmp_str, MIN(25,sizeof(tmp_str)), "%s", ctime(&(tp->timestamp)) );
245 /* max. len of 25 will snip off newline, which is good since it messes stuff up */
246 gtk_label_set_text ( tpwin->localtime, tmp_str );
248 else
250 gtk_label_set_text (tpwin->ts, NULL );
251 gtk_label_set_text (tpwin->localtime, NULL );
254 if ( tpwin->cur_tp )
256 g_snprintf ( tmp_str, sizeof(tmp_str), "%.3f m", vik_coord_diff(&(tp->coord), &(tpwin->cur_tp->coord)));
257 gtk_label_set_text ( tpwin->diff_dist, tmp_str );
258 if ( tp->has_timestamp && tpwin->cur_tp->has_timestamp )
260 g_snprintf ( tmp_str, sizeof(tmp_str), "%ld s", tp->timestamp - tpwin->cur_tp->timestamp);
261 gtk_label_set_text ( tpwin->diff_time, tmp_str );
262 if ( tp->timestamp == tpwin->cur_tp->timestamp )
263 gtk_label_set_text ( tpwin->diff_speed, "--" );
264 else
266 g_snprintf ( tmp_str, sizeof(tmp_str), "%.2f m/s", vik_coord_diff(&(tp->coord), &(tpwin->cur_tp->coord)) / ABS(tp->timestamp - tpwin->cur_tp->timestamp) );
267 gtk_label_set_text ( tpwin->diff_speed, tmp_str );
270 else
272 gtk_label_set_text ( tpwin->diff_time, NULL );
273 gtk_label_set_text ( tpwin->diff_speed, NULL );
278 tpwin->cur_tp = tp;
279 tpwin->cur_tp_is_endpoint = ! (tpl->next && tpl->prev);
282 void vik_trw_layer_tpwin_set_track_name ( VikTrwLayerTpwin *tpwin, const gchar *track_name )
284 gtk_label_set_text ( tpwin->track_name, track_name );