]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikviewport.c
[QA] Simplify vik_track_steal_and_append_trackpoints()
[andy/viking.git] / src / vikviewport.c
index 614e9b9325cfc61ce811f0fa2ee990988f367fd9..375d96f6d0373edb9a794051f87f4839cb63a7ae 100644 (file)
@@ -28,6 +28,8 @@
 #endif
 
 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
+#define DEFAULT_HIGHLIGHT_COLOR "#EEA500"
+/* Default highlight in orange */
 
 #include <gtk/gtk.h>
 #ifdef HAVE_MATH_H
@@ -49,8 +51,8 @@
 
 static gdouble EASTING_OFFSET = 500000.0;
 
-static void viewport_class_init ( VikViewportClass *klass );
-static void viewport_init ( VikViewport *vvp );
+static gint PAD = 10;
+
 static void viewport_finalize ( GObject *gob );
 static void viewport_utm_zone_check ( VikViewport *vvp );
 
@@ -84,20 +86,18 @@ struct _VikViewport {
   GdkGC *scale_bg_gc;
 
   GSList *copyrights;
+  GSList *logos;
 
   /* Wether or not display OSD info */
   gboolean draw_scale;
   gboolean draw_centermark;
+  gboolean draw_highlight;
+  GdkGC *highlight_gc;
+  GdkColor highlight_color;
 
   /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
   VikViewportDrawMode drawmode;
 
-  /* handy conversion factors which make google plotting extremely fast */
-  gdouble google_calcx_fact;
-  gdouble google_calcy_fact;
-  gdouble google_calcx_rev_fact;
-  gdouble google_calcy_rev_fact;
-
   /* trigger stuff */
   gpointer trigger;
   GdkPixmap *snapshot_buffer;
@@ -123,31 +123,10 @@ viewport_utm_zone_width ( VikViewport *vvp )
     return 0.0;
 }
 
+G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA)
 
-GType vik_viewport_get_type (void)
-{
-  static GType vvp_type = 0;
-
-  if (!vvp_type)
-  {
-    static const GTypeInfo vvp_info = 
-    {
-      sizeof (VikViewportClass),
-      NULL, /* base_init */
-      NULL, /* base_finalize */
-      (GClassInitFunc) viewport_class_init,
-      NULL, /* class_finalize */
-      NULL, /* class_data */
-      sizeof (VikViewport),
-      0,
-      (GInstanceInitFunc) viewport_init,
-    };
-    vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
-  }
-  return vvp_type;
-}
-
-static void viewport_class_init ( VikViewportClass *klass )
+static void
+vik_viewport_class_init ( VikViewportClass *klass )
 {
   /* Destructor */
   GObjectClass *object_class;
@@ -165,7 +144,8 @@ VikViewport *vik_viewport_new ()
   return vv;
 }
 
-static void viewport_init ( VikViewport *vvp )
+static void
+vik_viewport_init ( VikViewport *vvp )
 {
   viewport_init_ra();
 
@@ -190,12 +170,14 @@ static void viewport_init ( VikViewport *vvp )
   vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
   vvp->utm_zone_width = 0.0;
   vvp->background_gc = NULL;
+  vvp->highlight_gc = NULL;
   vvp->scale_bg_gc = NULL;
 
   vvp->copyrights = NULL;
 
   vvp->draw_scale = TRUE;
   vvp->draw_centermark = TRUE;
+  vvp->draw_highlight = TRUE;
 
   vvp->trigger = NULL;
   vvp->snapshot_buffer = NULL;
@@ -237,6 +219,45 @@ void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
   gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
 }
 
+GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp )
+{
+  GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
+  *rv = vvp->highlight_color;
+  return rv;
+}
+
+/* returns pointer to internal static storage, changes next time function called, use quickly */
+const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp )
+{
+  static gchar color[8];
+  g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->highlight_color.red/256),(int)(vvp->highlight_color.green/256),(int)(vvp->highlight_color.blue/256));
+  return color;
+}
+
+void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname )
+{
+  g_assert ( vvp->highlight_gc );
+  gdk_color_parse ( colorname, &(vvp->highlight_color) );
+  gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) );
+}
+
+void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color )
+{
+  g_assert ( vvp->highlight_gc );
+  vvp->highlight_color = *color;
+  gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color );
+}
+
+GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp )
+{
+  return vvp->highlight_gc;
+}
+
+void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness )
+{
+  // Otherwise same GDK_* attributes as in vik_viewport_new_gc
+  gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
+}
 
 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
 {
@@ -307,6 +328,11 @@ gboolean vik_viewport_configure ( VikViewport *vvp )
     vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
     vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
   }
+  if ( ! vvp->highlight_gc )
+  {
+    vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 );
+    vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR );
+  }
   if ( !vvp->scale_bg_gc) {
     vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
   }
@@ -332,6 +358,9 @@ static void viewport_finalize ( GObject *gob )
   if ( vvp->background_gc )
     g_object_unref ( G_OBJECT ( vvp->background_gc ) );
 
+  if ( vvp->highlight_gc )
+    g_object_unref ( G_OBJECT ( vvp->highlight_gc ) );
+
   if ( vvp->scale_bg_gc ) {
     g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
     vvp->scale_bg_gc = NULL;
@@ -340,13 +369,27 @@ static void viewport_finalize ( GObject *gob )
   G_OBJECT_CLASS(parent_class)->finalize(gob);
 }
 
+/**
+ * vik_viewport_clear:
+ * @vvp: self object
+ * 
+ * Clear the whole viewport.
+ */
 void vik_viewport_clear ( VikViewport *vvp )
 {
   g_return_if_fail ( vvp != NULL );
   gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
   vik_viewport_reset_copyrights ( vvp );
+  vik_viewport_reset_logos ( vvp );
 }
 
+/**
+ * vik_viewport_set_draw_scale:
+ * @vvp: self
+ * @draw_scale: new value
+ * 
+ * Enable/Disable display of scale.
+ */
 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
 {
   vvp->draw_scale = draw_scale;
@@ -364,8 +407,7 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
   if ( vvp->draw_scale ) {
     VikCoord left, right;
     gdouble unit, base, diff, old_unit, old_diff, ratio;
-    gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
-    PangoFontDescription *pfd;
+    gint odd, len, SCSIZE = 5, HEIGHT=10;
     PangoLayout *pl;
     gchar s[128];
 
@@ -379,7 +421,7 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
       break;
     case VIK_UNITS_DISTANCE_MILES:
       // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
-      base = vik_coord_diff ( &left, &right ) * 0.00621371192;
+      base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
       break;
     default:
       base = 1; // Keep the compiler happy
@@ -410,18 +452,18 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
                         PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
     /* black scale */
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
-    vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+    vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                         PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
     if (odd%2) {
       int i;
       for (i=1; i<5; i++) {
         vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
                             PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
-        vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+        vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                             PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
       }
     } else {
@@ -429,14 +471,12 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
       for (i=1; i<10; i++) {
         vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
                             PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
-        vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
+        vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                             PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
       }
     }
     pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), 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_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
 
     switch (dist_units) {
     case VIK_UNITS_DISTANCE_KILOMETRES:
@@ -462,7 +502,7 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
       g_critical("Houston, we've had a problem. distance=%d", dist_units);
     }
     pango_layout_set_text(pl, s, -1);
-    vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+    vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
                           PAD + len + PAD, vvp->height - PAD - 10, pl);
     g_object_unref(pl);
     pl = NULL;
@@ -473,44 +513,60 @@ void vik_viewport_draw_copyright ( VikViewport *vvp )
 {
   g_return_if_fail ( vvp != NULL );
 
-  gint PAD = 10;
-  PangoFontDescription *pfd;
   PangoLayout *pl;
   PangoRectangle ink_rect, logical_rect;
   gchar s[128] = "";
 
   /* compute copyrights string */
   guint len = g_slist_length ( vvp->copyrights );
+
   int i;
   for (i = 0 ; i < len ; i++)
   {
+    // Stop when buffer is full
+    int slen = strlen ( s );
+    if ( slen >= 127 )
+      break;
+
     gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
-    strcat ( s, copyright );
+
+    // Only use part of this copyright that fits in the available space left
+    //  remembering 1 character is left available for the appended space
+    int clen = strlen ( copyright );
+    if ( slen + clen > 126 ) {
+      clen = 126 - slen;
+    }
+
+    strncat ( s, copyright, clen );
     strcat ( s, " " );
   }
 
   /* create pango layout */
   pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), 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);
-  pfd = NULL;
+  pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
   pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
 
   /* Set the text */
   pango_layout_set_text(pl, s, -1);
 
   /* Use maximum of half the viewport width */
-  pango_layout_set_width ( pl, ( vvp->width / 2 - PAD ) * PANGO_SCALE );
+  pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
   pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
-  vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
-                          vvp->width / 2, vvp->height - PAD - logical_rect.height, pl);
+  vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
+                          vvp->width / 2, vvp->height - logical_rect.height, pl);
 
   /* Free memory */
   g_object_unref(pl);
   pl = NULL;           
 }
 
+/**
+ * vik_viewport_set_draw_centermark:
+ * @vvp: self object
+ * @draw_centermark: new value
+ * 
+ * Enable/Disable display of center mark.
+ */
 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
 {
   vvp->draw_centermark = draw_centermark;
@@ -532,7 +588,7 @@ void vik_viewport_draw_centermark ( VikViewport *vvp )
   const int gap = 4;
   int center_x = vvp->width/2;
   int center_y = vvp->height/2;
-  GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
+  GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
 
   /* white back ground */
   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
@@ -547,10 +603,38 @@ void vik_viewport_draw_centermark ( VikViewport *vvp )
   
 }
 
+void vik_viewport_draw_logo ( VikViewport *vvp )
+{
+  g_return_if_fail ( vvp != NULL );
+
+  guint len = g_slist_length ( vvp->logos );
+  gint x = vvp->width - PAD;
+  gint y = PAD;
+  int i;
+  for (i = 0 ; i < len ; i++)
+  {
+    GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
+    gint width = gdk_pixbuf_get_width ( logo );
+    gint height = gdk_pixbuf_get_height ( logo );
+    vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
+    x = x - width - PAD;
+  }
+}
+
+void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
+{
+  vvp->draw_highlight = draw_highlight;
+}
+
+gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
+{
+  return vvp->draw_highlight;
+}
+
 void vik_viewport_sync ( VikViewport *vvp )
 {
   g_return_if_fail ( vvp != NULL );
-  gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
+  gdk_draw_drawable(GTK_WIDGET(vvp)->window, gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
 }
 
 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
@@ -558,7 +642,7 @@ void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
   gint x, y, wid, hei;
 
   g_return_if_fail ( vvp != NULL );
-  gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
+  gdk_draw_drawable(GTK_WIDGET(vvp)->window, gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
 
   if (x_off >= 0) {
     x = 0;
@@ -883,7 +967,8 @@ void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gin
 
 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
 {
-  if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
+  // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
+  if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 )
     gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
 }
 
@@ -935,9 +1020,8 @@ void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x,
                               gint dest_x, gint dest_y, gint w, gint h )
 {
   gdk_draw_pixbuf ( vvp->scr_buffer,
-// GTK_WIDGET(vvp)->style->black_gc,
-NULL,
- pixbuf,
+                    NULL,
+                    pixbuf,
                     src_x, src_y, dest_x, dest_y, w, h,
                     GDK_RGB_DITHER_NONE, 0, 0 );
 }
@@ -981,14 +1065,14 @@ static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_l
   lon =
     zero_long -
     px / (Ra *
-         cos (lat * DEG2RAD));
+         cos (DEG2RAD(lat)));
 
-  dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
+  dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
   lat = lat - dif / 1.5;
   lon =
     zero_long -
     px / (Ra *
-              cos (lat * DEG2RAD));
+              cos (DEG2RAD(lat)));
 
   *lt = lat;
   *lg = lon;
@@ -1008,9 +1092,9 @@ static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_l
 //    lt *= rad2deg;
     Ra = Radius[90+(gint)lt];
     *x = Ra *
-         cos (lt*DEG2RAD) * (lg - zero_long);
+         cos (DEG2RAD(lt)) * (lg - zero_long);
     *y = Ra * (lt - zero_lat);
-    dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
+    dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
     *y = *y + dif / 1.85;
     *x = *x / pixelfact_x;
     *y = *y / pixelfact_y;
@@ -1028,7 +1112,7 @@ static void viewport_init_ra()
   {
     gint i;
     for ( i = -90; i <= 90; i++)
-      Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
+      Radius[i+90] = calcR ( DEG2RAD((double)i) );
     done_before = TRUE;
   }
 }
@@ -1052,7 +1136,7 @@ double calcR (double lat)
      * = 0.081082 Eccentricity
      */
 
-    lat = lat * DEG2RAD;
+    lat = DEG2RAD(lat);
     sc = sin (lat);
     x = a * (1.0 - e2);
     z = 1.0 - e2 * sc * sc;
@@ -1179,12 +1263,19 @@ void vik_viewport_reset_copyrights ( VikViewport *vp )
   vp->copyrights = NULL;
 }
 
+/**
+ * vik_viewport_add_copyright:
+ * @vp: self object
+ * @copyright: new copyright to display
+ * 
+ * Add a copyright to display on viewport.
+ */
 void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright ) 
 {
   g_return_if_fail ( vp != NULL );
   if ( copyright )
   {
-    gchar *found = (gchar*)g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
+    GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
     if ( found == NULL )
     {
       gchar *duple = g_strdup ( copyright );
@@ -1192,3 +1283,67 @@ void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright )
     }
   }
 }
+
+void vik_viewport_reset_logos ( VikViewport *vp )
+{
+  g_return_if_fail ( vp != NULL );
+  /* do not free elem */
+  g_slist_free ( vp->logos );
+  vp->logos = NULL;
+}
+
+void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
+{
+  g_return_if_fail ( vp != NULL );
+  if ( logo )
+  {
+    GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
+    if ( found == NULL )
+    {
+      vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
+    }
+  }
+}
+
+/**
+ * vik_viewport_compute_bearing:
+ * @vp: self object
+ * @x1: screen coord
+ * @y1: screen coord
+ * @x2: screen coord
+ * @y2: screen coord
+ * @angle: bearing in Radian (output)
+ * @baseangle: UTM base angle in Radian (output)
+ * 
+ * Compute bearing.
+ */
+void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
+{
+  gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
+  gdouble dx = (x2-x1)/len*10;
+  gdouble dy = (y2-y1)/len*10;
+
+  *angle = atan2(dy, dx) + M_PI_2;
+
+  if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
+    VikCoord test;
+    struct LatLon ll;
+    struct UTM u;
+    gint tx, ty;
+
+    vik_viewport_screen_to_coord ( vp, x1, y1, &test );
+    vik_coord_to_latlon ( &test, &ll );
+    ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 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 ( vp, &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;
+}