]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikwindow.c
Alex's GPSBabel acquire code
[andy/viking.git] / src / vikwindow.c
index 7ddb95a667bd3a1bb56c1c5400296c0af3ddf1ca..a9fe0dfd6cb73765044b4e549d15bab7140b5228 100644 (file)
  */
 #include "viking.h"
 #include "background.h"
+#include "acquire.h"
 
 #define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
 
+#include <glib/gprintf.h>
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
 #include <ctype.h>
-
+#include <glib/gprintf.h>
 #ifdef WINDOWS
 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
 #define make_dir(dir) mkdir(dir)
@@ -61,7 +63,6 @@ static void draw_scroll  ( VikWindow *vw, GdkEvent *event );
 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
 static void draw_release ( VikWindow *vw, GdkEventButton *event );
 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
-static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw );
 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
 
@@ -93,8 +94,11 @@ struct _VikWindow {
 
   GtkItemFactory *item_factory;
 
+  /* ruler variables */
   VikCoord oldcoord;
   gboolean has_oldcoord;
+  
+
   guint current_tool;
 
   guint16 tool_layer_id;
@@ -211,8 +215,8 @@ static void window_init ( VikWindow *vw )
 
   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
-  gtk_toolbar_set_icon_size(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_ICON_SIZE_SMALL_TOOLBAR);
-  gtk_toolbar_set_style (gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_TOOLBAR_ICONS);
+  gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
+  gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
 
   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
 
@@ -300,12 +304,190 @@ static void draw_status ( VikWindow *vw )
 static void draw_redraw ( VikWindow *vw )
 {
   vik_viewport_clear ( vw->viking_vvp);
-
   vik_layers_panel_draw_all ( vw->viking_vlp );
+  vik_viewport_draw_scale ( vw->viking_vvp );
+}
+
+static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
+{
+  PangoFontDescription *pfd;
+  PangoLayout *pl;
+  gchar str[128];
+  GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
+  GdkGC *thickgc = gdk_gc_new(d);
+  
+  gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
+  gdouble dx = (x2-x1)/len*10; 
+  gdouble dy = (y2-y1)/len*10;
+  gdouble c = cos(15.0 * M_PI/180.0);
+  gdouble s = sin(15.0 * M_PI/180.0);
+  gdouble angle;
+  gdouble baseangle = 0;
+  gint i;
+
+  /* draw line with arrow ends */
+  {
+    gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
+    a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
+    gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
+  }
+
+  a_viewport_clip_line(&x1, &y1, &x2, &y2);
+  gdk_draw_line(d, gc, x1, y1, x2, y2);
+
+  gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
+  gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
+  gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
+  gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
+  gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
+  gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
+
+  /* draw compass */
+#define CR 80
+#define CW 4
+  angle = atan2(dy, dx) + M_PI_2;
+
+  if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
+    VikCoord test;
+    struct LatLon ll;
+    struct UTM u;
+    gint tx, ty;
+
+    vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
+    vik_coord_to_latlon ( &test, &ll );
+    ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
+    a_coords_latlon_to_utm ( &ll, &u );
+    vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
+    vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
+
+    baseangle = M_PI - atan2(tx-x1, ty-y1);
+    angle -= baseangle;
+  }
+
+  if (angle<0) 
+    angle+=2*M_PI;
+  if (angle>2*M_PI)
+    angle-=2*M_PI;
+
+  {
+    GdkColor color;
+    gdk_gc_copy(thickgc, gc);
+    gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
+    gdk_color_parse("#2255cc", &color);
+    gdk_gc_set_rgb_fg_color(thickgc, &color);
+  }
+  gdk_draw_arc (d, thickgc, FALSE, x1-CR+CW/2, y1-CR+CW/2, 2*CR-CW, 2*CR-CW, (90 - baseangle*180/M_PI)*64, -angle*180/M_PI*64);
+
+
+  gdk_gc_copy(thickgc, gc);
+  gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
+  for (i=0; i<180; i++) {
+    c = cos(i*M_PI/90.0 + baseangle);
+    s = sin(i*M_PI/90.0 + baseangle);
+
+    if (i%5) {
+      gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
+    } else {
+      gdouble ticksize = 2*CW;
+      gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
+    }
+  }
+
+  gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
+  gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
+  gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
+  c = (CR+CW*2)*cos(baseangle);
+  s = (CR+CW*2)*sin(baseangle);
+  gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
+  gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
+
+  /* draw labels */
+#define LABEL(x, y, w, h) { \
+    gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
+    gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
+    gdk_draw_layout(d, gc, (x), (y), pl); } 
+  {
+    gint wd, hd, xd, yd;
+    gint wb, hb, xb, yb;
+
+    pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
+
+    pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
+    pango_layout_set_font_description (pl, pfd);
+    pango_font_description_free (pfd);
+
+    pango_layout_set_text(pl, "N", -1);
+    gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
+
+    /* draw label with distance */
+    if (distance >= 1000 && distance < 100000) {
+      g_sprintf(str, "%3.2f km", distance/1000.0);
+    } else if (distance < 1000) {
+      g_sprintf(str, "%d m", (int)distance);
+    } else {
+      g_sprintf(str, "%d km", (int)distance/1000);
+    }
+    pango_layout_set_text(pl, str, -1);
+
+    pango_layout_get_pixel_size ( pl, &wd, &hd );
+    if (dy>0) {
+      xd = (x1+x2)/2 + dy;
+      yd = (y1+y2)/2 - hd/2 - dx;
+    } else {
+      xd = (x1+x2)/2 - dy;
+      yd = (y1+y2)/2 - hd/2 + dx;
+    }
+
+    if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
+      xd = x2 + 10;
+      yd = y2 - 5;
+    }
+
+    LABEL(xd, yd, wd, hd);
+
+    /* draw label with bearing */
+    g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
+    pango_layout_set_text(pl, str, -1);
+    pango_layout_get_pixel_size ( pl, &wb, &hb );
+    xb = x1 + CR*cos(angle-M_PI_2);
+    yb = y1 + CR*sin(angle-M_PI_2);
+
+    if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
+      xb = x2 + 10;
+      yb = y2 + 10;
+    }
+
+    {
+      GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
+      if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
+       xb = xd + wd + 5;
+      }
+    }
+    LABEL(xb, yb, wb, hb);
+  }
+#undef LABEL
+
+  g_object_unref ( G_OBJECT ( pl ) );
+  g_object_unref ( G_OBJECT ( labgc ) );
+  g_object_unref ( G_OBJECT ( thickgc ) );
+}
+
+gboolean draw_buf_done = TRUE;
+
+static gboolean draw_buf(gpointer data)
+{
+  gpointer *pass_along = data;
+  gdk_threads_enter();
+  gdk_draw_drawable (pass_along[0], pass_along[1],
+                    pass_along[2], 0, 0, 0, 0, -1, -1);
+  gdk_threads_leave();
+  draw_buf_done = TRUE;
+  return FALSE;
 }
 
 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
 {
+  VikViewport *vvp = vw->viking_vvp;
   static VikCoord coord;
   static struct UTM utm;
   static struct LatLon ll;
@@ -315,10 +497,53 @@ static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
   vik_coord_to_utm ( &coord, &utm );
   a_coords_utm_to_latlon ( &utm, &ll );
 
+  if ( vw->current_tool == TOOL_RULER )
+  {
+    struct LatLon ll;
+    VikCoord coord;
+    gchar *temp;
+
+    if ( vw->has_oldcoord ) {
+      int oldx, oldy, w1, h1, w2, h2;
+      static GdkPixmap *buf = NULL;
+      w1 = vik_viewport_get_width(vvp); 
+      h1 = vik_viewport_get_height(vvp);
+      if (!buf) {
+       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
+      }
+      gdk_drawable_get_size(buf, &w2, &h2);
+      if (w1 != w2 || h1 != h2) {
+       g_object_unref ( G_OBJECT ( buf ) );
+       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
+      }
+
+      vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
+      vik_coord_to_latlon ( &coord, &ll );
+      vik_viewport_coord_to_screen ( vvp, &vw->oldcoord, &oldx, &oldy );
+
+      gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc, 
+                        vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
+      draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(vw->oldcoord)) );
+      if (draw_buf_done) {
+       static gpointer pass_along[3];
+       pass_along[0] = GTK_WIDGET(vvp)->window;
+       pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
+       pass_along[2] = buf;
+       g_idle_add (draw_buf, pass_along);
+       draw_buf_done = FALSE;
+      }
+
+      temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
+      vik_statusbar_set_message ( vw->viking_vs, 3, temp );
+      g_free ( temp );
+    }
+  }
+
   g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
 
-  if ( vw->pan_x != -1 )
+  if ( vw->pan_x != -1 ) {
     vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
+  }
 
   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
 }
@@ -376,17 +601,20 @@ static void draw_click (VikWindow *vw, GdkEventButton *event)
     {
       vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
       vik_coord_to_latlon ( &coord, &ll );
-      if ( vw->has_oldcoord )
+      if ( vw->has_oldcoord ) {
          temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
-      else
+        vw->has_oldcoord = FALSE;
+      }
+      else {
         temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
+       vw->has_oldcoord = TRUE;
+      }
 
       vik_statusbar_set_message ( vw->viking_vs, 3, temp );
       g_free ( temp );
 
       vw->oldcoord = coord;
       /* we don't use anything else so far */
-      vw->has_oldcoord = TRUE;
     }
     else
     {
@@ -404,17 +632,6 @@ static void draw_click (VikWindow *vw, GdkEventButton *event)
   }
 }
 
-static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw )
-{
-  if (!strcmp(gtk_action_get_name(a), "Zoom")) {
-    vw->current_tool = TOOL_ZOOM;
-  }
-  if (!strcmp(gtk_action_get_name(a), "Ruler")) {
-    vw->current_tool = TOOL_RULER;
-  }
-  draw_status ( vw );
-}
-
 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
 {
   guint what = 128;
@@ -688,6 +905,11 @@ static gboolean save_file ( GtkAction *a, VikWindow *vw )
   }
 }
 
+static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
+{
+  a_acquire_from_gps(vw, vw->viking_vlp, vw->viking_vvp );
+}
+
 static void clear_cb ( GtkAction *a, VikWindow *vw )
 {
   vik_layers_panel_clear ( vw->viking_vlp );
@@ -874,7 +1096,7 @@ static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_im
   GtkWidget *total_size_label;
 
   /* only used if (!one_image_only) */
-  GtkWidget *tiles_width_spin, *tiles_height_spin;
+  GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
 
 
   width_label = gtk_label_new ( "Width (pixels):" );
@@ -1049,6 +1271,12 @@ static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWin
   }
 }
 
+static void set_draw_scale ( GtkAction *a, VikWindow *vw )
+{
+  vik_viewport_set_draw_scale ( vw->viking_vvp, !vik_viewport_get_draw_scale(vw->viking_vvp) );
+  draw_update ( vw );
+}
+
 static void set_bg_color ( GtkAction *a, VikWindow *vw )
 {
   GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
@@ -1075,6 +1303,7 @@ static GtkActionEntry entries[] = {
   { "New",       GTK_STOCK_NEW,          "_New",                          "<control>N", "New file",                                     (GCallback)newwindow_cb          },
   { "Open",      GTK_STOCK_OPEN,         "_Open",                         "<control>O", "Open a file",                                  (GCallback)load_file             },
   { "Append",    GTK_STOCK_ADD,          "A_ppend File",                  NULL,         "Append data from a different file",            (GCallback)load_file             },
+  { "Acquire",   NULL,                   "A_cquire from GPS",             NULL,         "Transfer data from a GPS device",              (GCallback)acquire_from_gps      },
   { "Save",      GTK_STOCK_SAVE,         "_Save",                         "<control>S", "Save the file",                                (GCallback)save_file             },
   { "SaveAs",    GTK_STOCK_SAVE_AS,      "Save _As",                      NULL,         "Save the file under different name",           (GCallback)save_file_as          },
   { "GenImg",    GTK_STOCK_CLEAR,        "_Generate Image File",          NULL,         "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
@@ -1121,6 +1350,10 @@ static GtkRadioActionEntry tool_entries[] = {
   { "Ruler",     "vik-icon-ruler",       "_Ruler",                        "<control><shift>R", "Ruler Tool", 1 }
 };
 
+static GtkToggleActionEntry toggle_entries[] = {
+  { "ShowScale", NULL,                   "Show Scale",                    NULL,         NULL,                                           (GCallback)set_draw_scale, TRUE   },
+};
+
 #include "menu.xml.h"
 static void window_create_ui( VikWindow *window )
 {
@@ -1128,11 +1361,9 @@ static void window_create_ui( VikWindow *window )
   GtkActionGroup *action_group;
   GtkAccelGroup *accel_group;
   GError *error;
-  GtkActionEntry add_layer_item;
   guint i, j, mid;
   GtkIconFactory *icon_factory;
   GtkIconSet *icon_set; 
-  GtkIconSource *icon_source;
   GtkRadioActionEntry *tools = NULL, *radio;
   guint ntools;
   
@@ -1148,6 +1379,7 @@ static void window_create_ui( VikWindow *window )
 
   action_group = gtk_action_group_new ("MenuActions");
   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
+  gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 0, (GCallback)window_change_coord_mode_cb, window);
 
   icon_factory = gtk_icon_factory_new ();
@@ -1192,7 +1424,6 @@ static void window_create_ui( VikWindow *window )
       radio = &tools[ntools];
       ntools++;
       
-      fprintf(stderr, "    %s\n",                          vik_layer_get_interface(i)->tools[j].name);
       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
                            vik_layer_get_interface(i)->tools[j].name,
                            vik_layer_get_interface(i)->tools[j].name,
@@ -1224,7 +1455,7 @@ static void window_create_ui( VikWindow *window )
 }
 
 
-#include "icons.h"
+#include "icons/icons.h"
 static struct { 
   const GdkPixdata *data;
   gchar *stock_id;