+ g_object_unref ( G_OBJECT(dem_alt_gc) );
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+
+}
+
+/**
+ * Draw just the speed (velocity)/time image
+ */
+static void draw_vt ( GtkWidget *image, VikTrack *tr, PropWidgets *widgets)
+{
+ GtkWidget *window;
+ GdkPixmap *pix;
+ gdouble mins, maxs;
+ guint i;
+
+ // Free previous allocation
+ if ( widgets->speeds )
+ g_free ( widgets->speeds );
+
+ widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
+ if ( widgets->speeds == NULL )
+ return;
+
+ GdkGC *gps_speed_gc;
+ GdkColor color;
+
+ window = gtk_widget_get_toplevel (widgets->speed_box);
+
+ pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
+
+ gtk_image_set_from_pixmap ( GTK_IMAGE(image), pix, NULL );
+
+ gps_speed_gc = gdk_gc_new ( window->window );
+ gdk_color_parse ( "red", &color );
+ gdk_gc_set_rgb_fg_color ( gps_speed_gc, &color);
+
+ minmax_array(widgets->speeds, &mins, &maxs, FALSE, widgets->profile_width);
+ if (mins < 0.0)
+ mins = 0; /* splines sometimes give negative speeds */
+ maxs = maxs + ((maxs - mins) * 0.1);
+ if (maxs-mins < MIN_SPEED_DIFF) {
+ maxs = mins + MIN_SPEED_DIFF;
+ }
+
+ /* clear the image */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->bg_gc[0],
+ TRUE, 0, 0, MARGIN, widgets->profile_height);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->mid_gc[0],
+ TRUE, MARGIN, 0, widgets->profile_width, widgets->profile_height);
+
+ /* draw grid */
+ for (i=0; i<=LINES; i++) {
+ PangoFontDescription *pfd;
+ PangoLayout *pl = gtk_widget_create_pango_layout (GTK_WIDGET(image), NULL);
+ gchar s[32];
+ int w, h;
+
+ pango_layout_set_alignment (pl, PANGO_ALIGN_RIGHT);
+ pfd = pango_font_description_from_string (PROPWIN_LABEL_FONT);
+ pango_layout_set_font_description (pl, pfd);
+ pango_font_description_free (pfd);
+ vik_units_speed_t speed_units = a_vik_get_units_speed ();
+ switch (speed_units) {
+ case VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
+ sprintf(s, "%6.1fkm/h", VIK_MPS_TO_KPH(mins + (LINES-i)*(maxs-mins)/LINES));
+ break;
+ case VIK_UNITS_SPEED_MILES_PER_HOUR:
+ sprintf(s, "%6.1fmph", VIK_MPS_TO_MPH(mins + (LINES-i)*(maxs-mins)/LINES));
+ break;
+ case VIK_UNITS_SPEED_METRES_PER_SECOND:
+ sprintf(s, "%8dm/s", (int)(mins + (LINES-i)*(maxs-mins)/LINES));
+ break;
+ case VIK_UNITS_SPEED_KNOTS:
+ sprintf(s, "%6.1fknots", VIK_MPS_TO_KNOTS(mins + (LINES-i)*(maxs-mins)/LINES));
+ break;
+ default:
+ sprintf(s, "--");
+ g_critical("Houston, we've had a problem. speed=%d", speed_units);
+ }
+
+ pango_layout_set_text(pl, s, -1);
+ pango_layout_get_pixel_size (pl, &w, &h);
+ gdk_draw_layout(GDK_DRAWABLE(pix), window->style->fg_gc[0], MARGIN-w-3,
+ CLAMP((int)i*widgets->profile_height/LINES - h/2, 0, widgets->profile_height-h), pl);
+
+ gdk_draw_line (GDK_DRAWABLE(pix), window->style->dark_gc[0],
+ MARGIN, widgets->profile_height/LINES * i, MARGIN + widgets->profile_width, widgets->profile_height/LINES * i);
+ g_object_unref ( G_OBJECT ( pl ) );
+ pl = NULL;
+ }
+
+
+ /* draw speeds */
+ for ( i = 0; i < widgets->profile_width; i++ )
+ gdk_draw_line ( GDK_DRAWABLE(pix), window->style->dark_gc[3],
+ i + MARGIN, widgets->profile_height, i + MARGIN, widgets->profile_height-widgets->profile_height*(widgets->speeds[i]-mins)/(maxs-mins) );
+
+
+ time_t beg_time = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
+ time_t dur = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp - beg_time;
+ GList *iter;
+ for (iter = tr->trackpoints; iter; iter = iter->next) {
+ gdouble gps_speed = VIK_TRACKPOINT(iter->data)->speed;
+ if (isnan(gps_speed))
+ continue;
+ int x = MARGIN + widgets->profile_width * (VIK_TRACKPOINT(iter->data)->timestamp - beg_time) / dur;
+ int y = widgets->profile_height - widgets->profile_height*(gps_speed - mins)/(maxs - mins);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), gps_speed_gc, TRUE, x-2, y-2, 4, 4);
+ }
+
+ /* draw border */
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc, FALSE, MARGIN, 0, widgets->profile_width-1, widgets->profile_height-1);
+
+ g_object_unref ( G_OBJECT(pix) );
+ g_object_unref ( G_OBJECT(gps_speed_gc) );
+
+}
+#undef LINES
+
+/**
+ * Draw all graphs
+ */
+static void draw_all_graphs ( GtkWidget *widget, gpointer *pass_along, gboolean resized )
+{
+ VikTrack *tr = pass_along[0];
+ PropWidgets *widgets = pass_along[2];
+
+ // Draw graphs even if they are not visible
+
+ GList *child = NULL;
+ GtkWidget *image = NULL;
+ GtkWidget *window = gtk_widget_get_toplevel(widget);
+ gdouble pc = NAN;
+
+ // Draw elevations
+ if (widgets->elev_box != NULL) {
+
+ // Saved image no longer any good as we've resized, so we remove it here
+ if (resized && widgets->elev_graph_saved_img.img) {
+ g_object_unref(widgets->elev_graph_saved_img.img);
+ widgets->elev_graph_saved_img.img = NULL;
+ widgets->elev_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->elev_box));
+ draw_elevations (GTK_WIDGET(child->data), tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker is redrawn if necessary
+ if (widgets->is_marker_drawn) {
+
+ pc = tp_percentage_by_distance ( tr, widgets->marker_tp, widgets->track_length );
+ gdouble marker_x = 0.0;
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN + (image->allocation.width/2 - widgets->profile_width/2 - MARGIN/2);
+ save_image_and_draw_graph_mark(image,
+ marker_x,
+ image->allocation.width,
+ window->style->black_gc,
+ &widgets->elev_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn);
+ }
+ }
+ }
+
+ // Draw speeds
+ if (widgets->speed_box != NULL) {
+
+ // Saved image no longer any good as we've resized
+ if (resized && widgets->speed_graph_saved_img.img) {
+ g_object_unref(widgets->speed_graph_saved_img.img);
+ widgets->speed_graph_saved_img.img = NULL;
+ widgets->speed_graph_saved_img.saved = FALSE;
+ }
+
+ child = gtk_container_get_children(GTK_CONTAINER(widgets->speed_box));
+ draw_vt (GTK_WIDGET(child->data), tr, widgets );
+
+ image = GTK_WIDGET(child->data);
+ g_list_free(child);
+
+ // Ensure marker is redrawn if necessary
+ if (widgets->is_marker_drawn) {
+
+ pc = tp_percentage_by_time ( tr, widgets->marker_tp );
+
+ gdouble marker_x = 0.0;
+ if (!isnan(pc)) {
+ marker_x = (pc * widgets->profile_width) + MARGIN + (image->allocation.width/2 - widgets->profile_width/2 - MARGIN/2);
+ save_image_and_draw_graph_mark(image,
+ marker_x,
+ image->allocation.width,
+ window->style->black_gc,
+ &widgets->speed_graph_saved_img,
+ widgets->profile_width,
+ widgets->profile_height,
+ &widgets->is_marker_drawn);
+ }
+ }
+ }
+}
+
+/**
+ * Configure/Resize the profile & speed/time images
+ */
+static gboolean configure_event ( GtkWidget *widget, GdkEventConfigure *event, gpointer *pass_along )
+{
+ PropWidgets *widgets = pass_along[2];
+
+ if (widgets->configure_dialog) {
+ // Determine size offsets between dialog size and size for images
+ // Only on the initialisation of the dialog
+ widgets->profile_width_offset = event->width - widgets->profile_width;
+ widgets->profile_height_offset = event->height - widgets->profile_height;
+ widgets->configure_dialog = FALSE;
+
+ // Without this the settting, the dialog will only grow in vertical size - one can not then make it smaller!
+ gtk_widget_set_size_request ( widget, widgets->profile_width+widgets->profile_width_offset, widgets->profile_height );
+ // In fact this allows one to compress it a bit more vertically as I don't add on the height offset
+ }
+ else {
+ widgets->profile_width_old = widgets->profile_width;
+ widgets->profile_height_old = widgets->profile_height;
+ }
+
+ // Now adjust From Dialog size to get image size
+ widgets->profile_width = event->width - widgets->profile_width_offset;
+ widgets->profile_height = event->height - widgets->profile_height_offset;
+
+ // ATM we receive configure_events when the dialog is moved and so no further action is necessary
+ if ( !widgets->configure_dialog &&
+ (widgets->profile_width_old == widgets->profile_width) && (widgets->profile_height_old == widgets->profile_height) )
+ return FALSE;
+
+ // Draw stuff
+ draw_all_graphs ( widget, pass_along, TRUE );
+
+ return FALSE;
+}
+
+/**
+ * Create height profile widgets including the image and callbacks
+ */
+GtkWidget *vik_trw_layer_create_profile ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets, gdouble *min_alt, gdouble *max_alt)
+{
+ GdkPixmap *pix;
+ GtkWidget *image;
+ GtkWidget *eventbox;
+ gpointer *pass_along;
+
+ // First allocation
+ widgets->altitudes = vik_track_make_elevation_map ( tr, widgets->profile_width );
+
+ if ( widgets->altitudes == NULL ) {
+ *min_alt = *max_alt = VIK_DEFAULT_ALTITUDE;
+ return NULL;
+ }
+
+ minmax_array(widgets->altitudes, min_alt, max_alt, TRUE, widgets->profile_width);
+
+ pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
+ image = gtk_image_new_from_pixmap ( pix, NULL );
+
+ g_object_unref ( G_OBJECT(pix) );
+
+ pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
+ pass_along[0] = tr;
+ pass_along[1] = vlp;
+ pass_along[2] = vvp;
+ pass_along[3] = widgets;
+
+ eventbox = gtk_event_box_new ();
+ g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_profile_click), pass_along );
+ g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_profile_move), pass_along );
+ g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
+ gtk_container_add ( GTK_CONTAINER(eventbox), image );
+ gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK);
+
+ return eventbox;
+}
+
+/**
+ * Create speed/time widgets including the image and callbacks
+ */
+GtkWidget *vik_trw_layer_create_vtdiag ( GtkWidget *window, VikTrack *tr, gpointer vlp, VikViewport *vvp, PropWidgets *widgets)
+{
+ GdkPixmap *pix;
+ GtkWidget *image;
+ GtkWidget *eventbox;
+ gpointer *pass_along;
+
+ // First allocation
+ widgets->speeds = vik_track_make_speed_map ( tr, widgets->profile_width );
+ if ( widgets->speeds == NULL )
+ return NULL;
+
+ pass_along = g_malloc ( sizeof(gpointer) * 4 ); /* FIXME: mem leak -- never be freed */
+ pass_along[0] = tr;
+ pass_along[1] = vlp;
+ pass_along[2] = vvp;
+ pass_along[3] = widgets;
+
+ pix = gdk_pixmap_new( window->window, widgets->profile_width + MARGIN, widgets->profile_height, -1 );
+ image = gtk_image_new_from_pixmap ( pix, NULL );
+
+#if 0
+ /* XXX this can go out, it's just a helpful dev tool */
+ {
+ int j;
+ GdkGC **colors[8] = { window->style->bg_gc,
+ window->style->fg_gc,
+ window->style->light_gc,
+ window->style->dark_gc,
+ window->style->mid_gc,
+ window->style->text_gc,
+ window->style->base_gc,
+ window->style->text_aa_gc };
+ for (i=0; i<5; i++) {
+ for (j=0; j<8; j++) {
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), colors[j][i],
+ TRUE, i*20, j*20, 20, 20);
+ gdk_draw_rectangle(GDK_DRAWABLE(pix), window->style->black_gc,
+ FALSE, i*20, j*20, 20, 20);
+ }
+ }
+ }
+#endif
+
+ g_object_unref ( G_OBJECT(pix) );
+
+ eventbox = gtk_event_box_new ();
+ g_signal_connect ( G_OBJECT(eventbox), "button_press_event", G_CALLBACK(track_vt_click), pass_along );
+ g_signal_connect ( G_OBJECT(eventbox), "motion_notify_event", G_CALLBACK(track_vt_move), pass_along );
+ g_signal_connect_swapped ( G_OBJECT(eventbox), "destroy", G_CALLBACK(g_free), pass_along );
+ gtk_container_add ( GTK_CONTAINER(eventbox), image );
+ gtk_widget_set_events (eventbox, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
+
+ return eventbox;
+}
+#undef MARGIN
+
+static void propwin_response_cb( GtkDialog *dialog, gint resp, PropWidgets *widgets)
+{
+ VikTrack *tr = widgets->tr;
+ VikTrwLayer *vtl = widgets->vtl;
+ gboolean keep_dialog = FALSE;
+
+ /* FIXME: check and make sure the track still exists before doing anything to it */
+ /* Note: destroying diaglog (eg, parent window exit) won't give "response" */
+ switch (resp) {
+ case GTK_RESPONSE_DELETE_EVENT: /* received delete event (not from buttons) */
+ case GTK_RESPONSE_REJECT:
+ break;
+ case GTK_RESPONSE_ACCEPT:
+ vik_track_set_comment(tr, gtk_entry_get_text(GTK_ENTRY(widgets->w_comment)));
+ break;
+ case VIK_TRW_LAYER_PROPWIN_REVERSE:
+ vik_track_reverse(tr);
+ vik_layer_emit_update ( VIK_LAYER(vtl) );
+ break;
+ case VIK_TRW_LAYER_PROPWIN_DEL_DUP:
+ vik_track_remove_dup_points(tr);
+ /* above operation could have deleted current_tp or last_tp */
+ trw_layer_cancel_tps_of_track ( vtl, widgets->track_name );
+ vik_layer_emit_update ( VIK_LAYER(vtl) );
+ break;
+ case VIK_TRW_LAYER_PROPWIN_SPLIT:
+ {
+ /* get new tracks, add them, resolve naming conflicts (free if cancel), and delete old. old can still exist on clipboard. */
+ guint ntracks;
+ VikTrack **tracks = vik_track_split_into_segments(tr, &ntracks);
+ gchar *new_tr_name;
+ guint i;
+ for ( i = 0; i < ntracks; i++ )
+ {
+ g_assert ( tracks[i] );
+ new_tr_name = g_strdup_printf("%s #%d", widgets->track_name, i+1);
+ /* if ( (wp_exists) && (! overwrite) ) */
+ /* don't need to upper case new_tr_name because old tr name was uppercase */
+ if ( vik_trw_layer_get_track(vtl, new_tr_name ) &&
+ ( ! a_dialog_yes_or_no ( VIK_GTK_WINDOW_FROM_LAYER(vtl), "The track \"%s\" exists, do you wish to overwrite it?", new_tr_name ) ) )
+ {
+ gchar *new_new_tr_name = a_dialog_new_track ( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
+ g_free ( new_tr_name );
+ if (new_new_tr_name)
+ new_tr_name = new_new_tr_name;
+ else
+ {
+ new_tr_name = NULL;
+ vik_track_free ( tracks[i] );
+ }
+ }
+ if ( new_tr_name )
+ vik_trw_layer_add_track ( vtl, new_tr_name, tracks[i] );
+ }
+ if ( tracks )
+ {
+ g_free ( tracks );
+ /* Don't let track destroy this dialog */
+ vik_track_clear_property_dialog(tr);
+ vik_trw_layer_delete_track ( vtl, widgets->track_name );
+ vik_layer_emit_update ( VIK_LAYER(vtl) ); /* chase thru the hoops */
+ }
+ }
+ break;
+ case VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER:
+ {
+ GList *iter = tr->trackpoints;
+ while ((iter = iter->next)) {
+ if (widgets->marker_tp == VIK_TRACKPOINT(iter->data))
+ break;
+ }
+ if (iter == NULL) {
+ a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_ERROR,
+ _("Failed spliting track. Track unchanged"), NULL);
+ keep_dialog = TRUE;
+ break;
+ }
+
+ gchar *r_name = g_strdup_printf("%s #R", widgets->track_name);
+ if (vik_trw_layer_get_track(vtl, r_name ) &&
+ ( ! a_dialog_yes_or_no( VIK_GTK_WINDOW_FROM_LAYER(vtl),
+ "The track \"%s\" exists, do you wish to overwrite it?", r_name)))
+ {
+ gchar *new_r_name = a_dialog_new_track( VIK_GTK_WINDOW_FROM_LAYER(vtl), vik_trw_layer_get_tracks(vtl), NULL );
+ if (new_r_name) {
+ g_free( r_name );
+ r_name = new_r_name;
+ }
+ else {
+ a_dialog_msg(VIK_GTK_WINDOW_FROM_LAYER(vtl), GTK_MESSAGE_WARNING,
+ _("Operation Aborted. Track unchanged"), NULL);
+ keep_dialog = TRUE;
+ break;
+ }
+ }
+ iter->prev->next = NULL;
+ iter->prev = NULL;
+ VikTrack *tr_right = vik_track_new();
+ if ( tr->comment )
+ vik_track_set_comment ( tr_right, tr->comment );
+ tr_right->visible = tr->visible;
+ tr_right->trackpoints = iter;
+
+ vik_trw_layer_add_track(vtl, r_name, tr_right);
+ vik_layer_emit_update ( VIK_LAYER(vtl) );
+ }
+ break;
+ default:
+ fprintf(stderr, "DEBUG: unknown response\n");
+ return;
+ }
+
+ /* Keep same behaviour for now: destroy dialog if click on any button */
+ if (!keep_dialog) {
+ prop_widgets_free(widgets);
+ vik_track_clear_property_dialog(tr);
+ gtk_widget_destroy ( GTK_WIDGET(dialog) );
+ }
+}
+
+/**
+ * Create the widgets for the given graph tab
+ */
+static GtkWidget *create_graph_page ( GtkWidget *graph,
+ const gchar *markup,
+ GtkWidget *value,
+ const gchar *markup2,
+ GtkWidget *value2)
+{
+ GtkWidget *hbox = gtk_hbox_new ( FALSE, 10 );
+ GtkWidget *vbox = gtk_vbox_new ( FALSE, 10 );
+ GtkWidget *label = gtk_label_new (NULL);
+ GtkWidget *label2 = gtk_label_new (NULL);
+ gtk_box_pack_start (GTK_BOX(vbox), graph, FALSE, FALSE, 0);
+ gtk_label_set_markup ( GTK_LABEL(label), markup );
+ gtk_label_set_markup ( GTK_LABEL(label2), markup2 );
+ gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(hbox), value, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(hbox), label2, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(hbox), value2, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+ return vbox;
+}
+
+void vik_trw_layer_propwin_run ( GtkWindow *parent, VikTrwLayer *vtl, VikTrack *tr, gpointer vlp, gchar *track_name, VikViewport *vvp )
+{
+ /* FIXME: free widgets when destroy signal received */
+ PropWidgets *widgets = prop_widgets_new();
+ widgets->vtl = vtl;
+ widgets->tr = tr;
+ widgets->vlp = vlp;
+ widgets->profile_width = PROPWIN_PROFILE_WIDTH;
+ widgets->profile_height = PROPWIN_PROFILE_HEIGHT;
+ widgets->track_name = track_name;
+ gchar *title = g_strdup_printf(_("%s - Track Properties"), track_name);
+ GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
+ parent,
+ GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
+ _("Split at _Marker"), VIK_TRW_LAYER_PROPWIN_SPLIT_MARKER,
+ _("Split _Segments"), VIK_TRW_LAYER_PROPWIN_SPLIT,
+ _("_Reverse"), VIK_TRW_LAYER_PROPWIN_REVERSE,
+ _("_Delete Dupl."), VIK_TRW_LAYER_PROPWIN_DEL_DUP,
+ GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+ NULL);
+ widgets->dialog = dialog;
+ g_free(title);
+ g_signal_connect(dialog, "response", G_CALLBACK(propwin_response_cb), widgets);
+ GtkTable *table;