]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikwindow.c
(no commit message)
[andy/viking.git] / src / vikwindow.c
index ef46c08ed3f1d8f67945d94f904fbc0e44d7069a..fd6dd7fd4a6937a49345d43c6c3bc79637ede18b 100644 (file)
  */
 #include "viking.h"
 #include "background.h"
+#include "acquire.h"
+#include "datasources.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)
@@ -57,7 +60,7 @@ static gboolean delete_event( VikWindow *vw );
 
 static void draw_sync ( VikWindow *vw );
 static void draw_redraw ( VikWindow *vw );
-static void draw_scroll  ( VikWindow *vw, GdkEvent *event );
+static void draw_scroll  ( VikWindow *vw, GdkEventScroll *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 );
@@ -71,16 +74,39 @@ static void draw_status ();
 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
+
+/* tool management */
+typedef struct {
+  VikToolInterface ti;
+  gpointer state;
+} toolbox_tool_t;
+
+typedef struct {
+  int                  active_tool;
+  int                  n_tools;
+  toolbox_tool_t       *tools;
+  VikWindow *vw;
+} toolbox_tools_t;
+
 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
+static toolbox_tools_t* toolbox_create(VikWindow *vw);
+static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti);
+static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
+static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
+static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
+static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
+static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
 
+
+/* ui creation */
 static void window_create_ui( VikWindow *window );
+static void register_vik_icons (GtkIconFactory *icon_factory);
 
+/* i/o */
 static void load_file ( GtkAction *a, VikWindow *vw );
 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
 static gboolean save_file ( GtkAction *a, VikWindow *vw );
 static gboolean window_save ( VikWindow *vw );
-static void register_vik_icons (GtkIconFactory *icon_factory);
-
 
 struct _VikWindow {
   GtkWindow gtkwindow;
@@ -92,10 +118,9 @@ struct _VikWindow {
 
   GtkItemFactory *item_factory;
 
-  VikCoord oldcoord;
-  gboolean has_oldcoord;
+  /* tool management state */
   guint current_tool;
-
+  toolbox_tools_t *vt;
   guint16 tool_layer_id;
   guint16 tool_tool_id;
 
@@ -185,20 +210,24 @@ static void window_init ( VikWindow *vw )
   GtkWidget *main_vbox;
   GtkWidget *hpaned;
 
-  window_create_ui(vw);
-  gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
 
   vw->viking_vvp = vik_viewport_new();
   vw->viking_vlp = vik_layers_panel_new();
   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
   vw->viking_vs = vik_statusbar_new();
 
+  vw->vt = toolbox_create(vw);
+  window_create_ui(vw);
+  gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
+  
+  toolbox_activate(vw->vt, "Zoom");
+
   vw->filename = NULL;
   vw->item_factory = NULL;
 
-  vw->has_oldcoord = FALSE;
   vw->modified = FALSE;
   vw->only_updating_coord_mode_ui = FALSE;
+  
 
   vw->pan_x = vw->pan_y = -1;
   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
@@ -303,6 +332,38 @@ static void draw_redraw ( VikWindow *vw )
   vik_viewport_draw_scale ( vw->viking_vvp );
 }
 
+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);
+  draw_buf_done = TRUE;
+  gdk_threads_leave();
+  return FALSE;
+}
+
+
+/* Mouse event handlers ************************************************************************/
+
+static void draw_click (VikWindow *vw, GdkEventButton *event)
+{
+
+  /* middle button pressed.  we reserve all middle button and scroll events
+   * for panning and zooming; tools only get left/right/movement 
+   */
+  if ( event->button == 2) {
+    /* set panning origin */
+    vw->pan_x = (gint) event->x;
+    vw->pan_y = (gint) event->y;
+  } 
+  else {
+    toolbox_click(vw->vt, event);
+  }
+}
+
 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
 {
   static VikCoord coord;
@@ -310,31 +371,22 @@ static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
   static struct LatLon ll;
   static char pointer_buf[36];
 
+  toolbox_move(vw->vt, (GdkEventButton *)event);
+
   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
   vik_coord_to_utm ( &coord, &utm );
   a_coords_utm_to_latlon ( &utm, &ll );
-
   g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
+  vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
 
   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 );
-}
-
-static void draw_scroll (VikWindow *vw, GdkEvent *event)
-{
-  if ( event->scroll.direction == GDK_SCROLL_UP )
-    vik_viewport_zoom_in (vw->viking_vvp);
-  else
-    vik_viewport_zoom_out(vw->viking_vvp);
-  draw_update(vw);
 }
 
 static void draw_release ( VikWindow *vw, GdkEventButton *event )
 {
-  if ( event->button == 2 ) { /* move / pan */
+  if ( event->button == 2 ) {  /* move / pan */
     if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
         vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
       else
@@ -343,67 +395,351 @@ static void draw_release ( VikWindow *vw, GdkEventButton *event )
       draw_update ( vw );
       vw->pan_x = vw->pan_y = -1;
   }
-  if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
-      vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
-  {
-    vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
-        vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
-        event, vw->viking_vvp );
+  else {
+    toolbox_release(vw->vt, event);
   }
 }
 
-static void draw_click (VikWindow *vw, GdkEventButton *event)
+static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
 {
-  if ( event->button == 2) {
-    vw->pan_x = (gint) event->x;
-    vw->pan_y = (gint) event->y;
-  } else if ( vw->current_tool == TOOL_ZOOM )
+  if ( event->direction == GDK_SCROLL_UP )
+    vik_viewport_zoom_in (vw->viking_vvp);
+  else
+    vik_viewport_zoom_out(vw->viking_vvp);
+  draw_update(vw);
+}
+
+
+
+/********************************************************************************
+ ** Ruler tool code
+ ********************************************************************************/
+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 */
   {
-    vw->modified = TRUE;
-    vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
-    if ( event->button == 1 )
-      vik_viewport_zoom_in (vw->viking_vvp);
-    else if ( event->button == 3 )
-      vik_viewport_zoom_out (vw->viking_vvp);
-    draw_update ( vw );
+    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);
   }
-  else if ( vw->current_tool == TOOL_RULER )
-  {
+
+  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;
-    VikCoord coord;
-    gchar *temp;
-    if ( event->button == 1 || event->button == 3 )
-    {
-      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 )
-         temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
-      else
-        temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
+    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);
+
 
-      vik_statusbar_set_message ( vw->viking_vs, 3, temp );
-      g_free ( temp );
+  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);
 
-      vw->oldcoord = coord;
-      /* we don't use anything else so far */
-      vw->has_oldcoord = TRUE;
+    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);
     }
-    else
+  }
+
+  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;
+    }
+
     {
-      vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
-      draw_update ( vw );
+      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);
   }
-  else if ( vw->current_tool == TOOL_LAYER )
-  {
-    vw->modified = TRUE;
-    if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
-        vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
-        event, vw->viking_vvp ) )
-      a_dialog_info_msg_extra ( GTK_WINDOW(vw), "A %s Layer must exist and be either selected or visible before you can use this function.", vik_layer_get_interface ( vw->tool_layer_id )->name );
+#undef LABEL
+
+  g_object_unref ( G_OBJECT ( pl ) );
+  g_object_unref ( G_OBJECT ( labgc ) );
+  g_object_unref ( G_OBJECT ( thickgc ) );
+}
+
+typedef struct {
+  VikWindow *vw;
+  VikViewport *vvp;
+  gboolean has_oldcoord;
+  VikCoord oldcoord;
+} ruler_tool_state_t;
+
+static gpointer ruler_create (VikWindow *vw, VikViewport *vvp) 
+{
+  ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
+  s->vw = vw;
+  s->vvp = vvp;
+  s->has_oldcoord = FALSE;
+  return s;
+}
+
+static void ruler_destroy (ruler_tool_state_t *s)
+{
+  g_free(s);
+}
+
+static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
+{
+  struct LatLon ll;
+  VikCoord coord;
+  gchar *temp;
+  if ( event->button == 1 ) {
+    vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
+    vik_coord_to_latlon ( &coord, &ll );
+    if ( s->has_oldcoord ) {
+      temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
+      s->has_oldcoord = FALSE;
+    }
+    else {
+      temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
+      s->has_oldcoord = TRUE;
+    }
+
+    vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
+    g_free ( temp );
+
+    s->oldcoord = coord;
+  }
+  else {
+    vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
+    draw_update ( s->vw );
   }
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
+{
+  VikViewport *vvp = s->vvp;
+  VikWindow *vw = s->vw;
+
+  struct LatLon ll;
+  VikCoord coord;
+  gchar *temp;
+
+  if ( s->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, &s->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, &(s->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_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
+      draw_buf_done = FALSE;
+    }
+
+    temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
+    vik_statusbar_set_message ( vw->viking_vs, 3, temp );
+    g_free ( temp );
+  }
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
+{
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
+{
+  draw_update ( s->vw );
+}
+
+static VikToolInterface ruler_tool = 
+  { "Ruler", 
+    (VikToolConstructorFunc) ruler_create,
+    (VikToolDestructorFunc) ruler_destroy,
+    (VikToolActivationFunc) NULL,
+    (VikToolActivationFunc) ruler_deactivate, 
+    (VikToolMouseFunc) ruler_click, 
+    (VikToolMouseFunc) ruler_move, 
+    (VikToolMouseFunc) ruler_release };
+/*** end ruler code ********************************************************/
+
+
+
+/********************************************************************************
+ ** Zoom tool code
+ ********************************************************************************/
+static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
+{
+  return vw;
 }
 
+static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
+{
+  vw->modified = TRUE;
+  vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
+  if ( event->button == 1 )
+    vik_viewport_zoom_in (vw->viking_vvp);
+  else if ( event->button == 3 )
+    vik_viewport_zoom_out (vw->viking_vvp);
+  draw_update ( vw );
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
+{
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
+{
+  return VIK_LAYER_TOOL_ACK;
+}
+
+static VikToolInterface zoom_tool = 
+  { "Zoom", 
+    (VikToolConstructorFunc) zoomtool_create,
+    (VikToolDestructorFunc) NULL,
+    (VikToolActivationFunc) NULL,
+    (VikToolActivationFunc) NULL,
+    (VikToolMouseFunc) zoomtool_click, 
+    (VikToolMouseFunc) zoomtool_move,
+    (VikToolMouseFunc) zoomtool_release };
+/*** end ruler code ********************************************************/
+
+
+
 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
 {
   guint what = 128;
@@ -515,11 +851,110 @@ static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
 }
 
+/***************************************
+ ** tool management routines
+ **
+ ***************************************/
+
+static toolbox_tools_t* toolbox_create(VikWindow *vw)
+{
+  toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
+  vt->tools = NULL;
+  vt->n_tools = 0;
+  vt->active_tool = -1;
+  vt->vw = vw;
+  if (!vw->viking_vvp) {
+    printf("Error: no viewport found.\n");
+    exit(1);
+  }
+  return vt;
+}
+
+static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti)
+{
+  vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
+  vt->tools[vt->n_tools].ti = *vti;
+  if (vti->create) {
+    vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
+  } 
+  else {
+    vt->tools[vt->n_tools].state = NULL;
+  }
+  vt->n_tools++;
+}
+
+static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
+{
+  int i;
+  for (i=0; i<vt->n_tools; i++) {
+    if (!strcmp(tool_name, vt->tools[i].ti.name)) {
+      break;
+    }
+  }
+  return i;
+}
+
+static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
+{
+  int tool = toolbox_get_tool(vt, tool_name);
+  toolbox_tool_t *t = &vt->tools[tool];
+  VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
+
+  if (tool == vt->n_tools) {
+    printf("panic: trying to activate a non-existent tool... \n");
+    exit(1);
+  }
+  /* is the tool already active? */
+  if (vt->active_tool == tool) {
+    return;
+  }
+
+  if (vt->active_tool != -1) {
+    if (vt->tools[vt->active_tool].ti.deactivate) {
+      vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
+    }
+  }
+  if (t->ti.activate) {
+    t->ti.activate(vl, t->state);
+  }
+  vt->active_tool = tool;
+}
+
+static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
+{
+  VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
+  if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
+    vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
+  }
+}
+
+static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
+{
+  VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
+  if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
+    vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state);
+  }
+}
+
+static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
+{
+  VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
+  if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release) {
+    vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
+  }
+}
+/** End tool management ************************************/
+
+
+
+/* this function gets called whenever a toolbar tool is clicked */
 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
 {
   /* White Magic, my friends ... White Magic... */
   int i, j;
 
+  toolbox_activate(vw->vt, gtk_action_get_name(a));
+
   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
     vw->current_tool = TOOL_ZOOM;
   } 
@@ -677,6 +1112,21 @@ static gboolean save_file ( GtkAction *a, VikWindow *vw )
   }
 }
 
+static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
+{
+  a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
+}
+
+static void acquire_from_google ( GtkAction *a, VikWindow *vw )
+{
+  a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
+}
+
+static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
+{
+  a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
+}
+
 static void clear_cb ( GtkAction *a, VikWindow *vw )
 {
   vik_layers_panel_clear ( vw->viking_vlp );
@@ -1038,6 +1488,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");
@@ -1054,8 +1510,15 @@ static void set_bg_color ( GtkAction *a, VikWindow *vw )
   gtk_widget_destroy ( colorsd );
 }
 
+
+
+/***********************************************************************************************
+ ** GUI Creation
+ ***********************************************************************************************/
+
 static GtkActionEntry entries[] = {
   { "File", NULL, "_File", 0, 0, 0 },
+  { "Edit", NULL, "_Edit", 0, 0, 0 },
   { "View", NULL, "_View", 0, 0, 0 },
   { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
   { "Layers", NULL, "_Layers", 0, 0, 0 },
@@ -1064,6 +1527,10 @@ 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", 0, 0, 0 },
+  { "AcquireGPS",   NULL,                "From _GPS",                    NULL,         "Transfer data from a GPS device",              (GCallback)acquire_from_gps      },
+  { "AcquireGoogle",   NULL,             "Google _Directions",           NULL,         "Get driving directions from Google",           (GCallback)acquire_from_google   },
+  { "AcquireGC",   NULL,                 "Geo_caches",                   NULL,         "Get Geocaches from geocaching.com",            (GCallback)acquire_from_gc       },
   { "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 },
@@ -1100,9 +1567,9 @@ static GtkActionEntry entries[] = {
 static GtkRadioActionEntry mode_entries[] = {
   { "ModeUTM",         NULL,         "_UTM Mode",               "<control>u", NULL, 0 },
   { "ModeExpedia",     NULL,         "_Expedia Mode",           "<control>e", NULL, 1 },
-  { "ModeGoogle",      NULL,         "_Google Mode",            "<control>g", NULL, 2 },
-  { "ModeKH",          NULL,         "_KH\\/Flat LatLon Mode",  "<control>k", NULL, 3 },
-  { "ModeMercator",    NULL,         "_Mercator (New Google)",  "<control>m", NULL, 4 }
+  { "ModeGoogle",      NULL,         "_Old Google Mode",        "<control>o", NULL, 2 },
+  { "ModeKH",          NULL,         "Old _KH Mode",            "<control>k", NULL, 3 },
+  { "ModeMercator",    NULL,         "_Google Mode",            "<control>g", NULL, 4 }
 };
 
 static GtkRadioActionEntry tool_entries[] = {
@@ -1110,6 +1577,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 )
 {
@@ -1126,7 +1597,9 @@ static void window_create_ui( VikWindow *window )
   uim = gtk_ui_manager_new ();
   window->uim = uim;
 
-  
+  toolbox_add_tool(window->vt, &ruler_tool);
+  toolbox_add_tool(window->vt, &zoom_tool);
+
   error = NULL;
   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
     g_error_free (error);
@@ -1135,9 +1608,11 @@ 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 ();
+  gtk_icon_factory_add_default (icon_factory); 
 
   register_vik_icons(icon_factory);
 
@@ -1188,6 +1663,8 @@ static void window_create_ui( VikWindow *window )
                            vik_layer_get_interface(i)->tools[j].name,
                            GTK_UI_MANAGER_TOOLITEM, FALSE);
 
+      toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]));
+
       radio->name = vik_layer_get_interface(i)->tools[j].name;
       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
       radio->label = vik_layer_get_interface(i)->tools[j].name;
@@ -1196,6 +1673,7 @@ static void window_create_ui( VikWindow *window )
       radio->value = ntools;
     }
   }
+  g_object_unref (icon_factory);
 
   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
   g_free(tools);
@@ -1205,12 +1683,11 @@ static void window_create_ui( VikWindow *window )
   accel_group = gtk_ui_manager_get_accel_group (uim);
   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
   gtk_ui_manager_ensure_update (uim);
-  gtk_icon_factory_add_default (icon_factory); 
-  g_object_unref (icon_factory);
 }
 
 
-#include "icons.h"
+
+#include "icons/icons.h"
 static struct { 
   const GdkPixdata *data;
   gchar *stock_id;
@@ -1227,7 +1704,6 @@ static struct {
   { &showpic_18,       "Show Picture"      },
 };
  
-
 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
 
 static void