]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikviewport.c
Fix SF#3408170: Selected Track Thickness is Always 1px
[andy/viking.git] / src / vikviewport.c
index 949104c4941f8c38faf51697d5987ae70a1ab0e0..68584ab666d36673d83447a97997619c8ef0c83e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
- * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
  *
  * Lat/Lon plotting functions calcxy* are from GPSDrive
  * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
+#define DEFAULT_HIGHLIGHT_COLOR "#EEA500"
+/* Default highlight in orange */
 
 #include <gtk/gtk.h>
+#ifdef HAVE_MATH_H
 #include <math.h>
-#include <stdio.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
 #include "coords.h"
 #include "vikcoord.h"
+#include "vikwindow.h"
 #include "vikviewport.h"
 
 #include "mapcoord.h"
 
 /* for ALTI_TO_MPP */
 #include "globals.h"
-#include "googlemaps.h"
-#include "khmaps.h"
 
 static gdouble EASTING_OFFSET = 500000.0;
 
+static gint PAD = 10;
+
 static void viewport_class_init ( VikViewportClass *klass );
 static void viewport_init ( VikViewport *vvp );
 static void viewport_finalize ( GObject *gob );
@@ -57,8 +67,6 @@ static void viewport_init_ra();
 
 static GObjectClass *parent_class;
 
-static void viewport_google_rezoom ( VikViewport *vvp );
-
 
 struct _VikViewport {
   GtkDrawingArea drawing_area;
@@ -78,8 +86,16 @@ struct _VikViewport {
   GdkGC *background_gc;
   GdkColor background_color;
   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;
@@ -89,6 +105,11 @@ struct _VikViewport {
   gdouble google_calcy_fact;
   gdouble google_calcx_rev_fact;
   gdouble google_calcy_rev_fact;
+
+  /* trigger stuff */
+  gpointer trigger;
+  GdkPixmap *snapshot_buffer;
+  gboolean half_drawn;
 };
 
 static gdouble
@@ -148,31 +169,51 @@ static void viewport_class_init ( VikViewportClass *klass )
 
 VikViewport *vik_viewport_new ()
 {
-  return VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
+  VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
+  return vv;
 }
 
 static void viewport_init ( VikViewport *vvp )
 {
   viewport_init_ra();
 
+  struct UTM utm;
+  struct LatLon ll;
+  ll.lat = a_vik_get_default_lat();
+  ll.lon = a_vik_get_default_long();
+  a_coords_latlon_to_utm ( &ll, &utm );
+
   /* TODO: not static */
   vvp->xmpp = 4.0;
   vvp->ympp = 4.0;
   vvp->coord_mode = VIK_COORD_LATLON;
   vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
-  vvp->center.north_south = 0;
-  vvp->center.east_west = -166021;
-  vvp->center.utm_zone = 31;
-  vvp->center.utm_letter = 'N';
+  vvp->center.mode = VIK_COORD_LATLON;
+  vvp->center.north_south = ll.lat;
+  vvp->center.east_west = ll.lon;
+  vvp->center.utm_zone = (int)utm.zone;
+  vvp->center.utm_letter = utm.letter;
   vvp->scr_buffer = NULL;
   vvp->alpha_pixbuf = NULL;
   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;
+  vvp->half_drawn = FALSE;
+
   g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
+
+  GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
 }
 
 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
@@ -192,27 +233,70 @@ const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
 
 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
 {
-  g_assert ( vvp->background_gc );
-  gdk_color_parse ( colorname, &(vvp->background_color) );
-  gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
+  g_assert ( vvp && vvp->background_gc );
+  if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
+    gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
+  else
+    g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
 }
 
 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
 {
-  g_assert ( vvp->background_gc );
+  g_assert ( vvp && vvp->background_gc );
   vvp->background_color = *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 )
 {
-  GdkGC *rv;
+  GdkGC *rv = NULL;
   GdkColor color;
 
   rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
-  gdk_color_parse ( colorname, &color );
-  gdk_gc_set_rgb_fg_color ( rv, &color );
+  if ( gdk_color_parse ( colorname, &color ) )
+    gdk_gc_set_rgb_fg_color ( rv, &color );
+  else
+    g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
   return rv;
 }
@@ -234,6 +318,11 @@ void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint heigh
   if ( vvp->scr_buffer )
     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+
+  /* TODO trigger: only if this is enabled !!! */
+  if ( vvp->snapshot_buffer )
+    g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
+  vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
 }
 
 
@@ -254,11 +343,23 @@ gboolean vik_viewport_configure ( VikViewport *vvp )
 
   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
 
+  /* TODO trigger: only if enabled! */
+  if ( vvp->snapshot_buffer )
+    g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
+
+  vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
+  /* TODO trigger */
+
   /* this is down here so it can get a GC (necessary?) */
-  if ( ! vvp->background_gc )
+  if ( !vvp->background_gc )
+  {
+    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->background_gc = vik_viewport_new_gc ( vvp, "", 1 );
-    vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */
+    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);
@@ -276,12 +377,18 @@ static void viewport_finalize ( GObject *gob )
   if ( vvp->scr_buffer )
     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
 
+  if ( vvp->snapshot_buffer )
+    g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
+
   if ( vvp->alpha_pixbuf )
     g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
 
   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;
@@ -290,12 +397,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;
@@ -308,20 +430,32 @@ gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
 
 void vik_viewport_draw_scale ( VikViewport *vvp )
 {
+  g_return_if_fail ( vvp != NULL );
+
   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;
+    gint odd, len, SCSIZE = 5, HEIGHT=10;
     PangoFontDescription *pfd;
     PangoLayout *pl;
     gchar s[128];
 
-    g_return_if_fail ( vvp != NULL );
-
     vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
     vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
 
-    base = vik_coord_diff ( &left, &right ); // in meters
+    vik_units_distance_t dist_units = a_vik_get_units_distance ();
+    switch (dist_units) {
+    case VIK_UNITS_DISTANCE_KILOMETRES:
+      base = vik_coord_diff ( &left, &right ); // in meters
+      break;
+    case VIK_UNITS_DISTANCE_MILES:
+      // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
+      base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
+      break;
+    default:
+      base = 1; // Keep the compiler happy
+      g_critical("Houston, we've had a problem. distance=%d", dist_units);
+    }
     ratio = (vvp->width/SCSIZE)/base;
 
     unit = 1;
@@ -375,17 +509,85 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
     pango_layout_set_font_description (pl, pfd);
     pango_font_description_free (pfd);
 
-    if (unit >= 1000) {
-      sprintf(s, "%d km", (int)unit/1000);
-    } else {
-      sprintf(s, "%d m", (int)unit);
+    switch (dist_units) {
+    case VIK_UNITS_DISTANCE_KILOMETRES:
+      if (unit >= 1000) {
+       sprintf(s, "%d km", (int)unit/1000);
+      } else {
+       sprintf(s, "%d m", (int)unit);
+      }
+      break;
+    case VIK_UNITS_DISTANCE_MILES:
+      // Handle units in 0.1 miles
+      if (unit < 10.0) {
+       sprintf(s, "%0.1f miles", unit/10.0);
+      }
+      else if ((int)unit == 10.0) {
+       sprintf(s, "1 mile");
+      }
+      else {
+       sprintf(s, "%d miles", (int)(unit/10.0));
+      }
+      break;
+    default:
+      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,
                           PAD + len + PAD, vvp->height - PAD - 10, pl);
+    g_object_unref(pl);
+    pl = NULL;
   }
 }
 
+void vik_viewport_draw_copyright ( VikViewport *vvp )
+{
+  g_return_if_fail ( vvp != NULL );
+
+  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++)
+  {
+    gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
+    strcat ( s, copyright );
+    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_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_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);
+
+  /* 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;
@@ -398,6 +600,8 @@ gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
 
 void vik_viewport_draw_centermark ( VikViewport *vvp )
 {
+  g_return_if_fail ( vvp != NULL );
+
   if ( !vvp->draw_centermark )
     return;
 
@@ -420,6 +624,35 @@ void vik_viewport_draw_centermark ( VikViewport *vvp )
   
 }
 
+void vik_viewport_draw_logo ( VikViewport *vvp )
+{
+  g_return_if_fail ( vvp != NULL );
+
+  /* compute copyrights string */
+  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 );
@@ -459,8 +692,6 @@ void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
 
   if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
     viewport_utm_zone_check(vvp);
-  else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-    viewport_google_rezoom ( vvp );
 }
 
 /* or could do factor */
@@ -472,9 +703,6 @@ void vik_viewport_zoom_in ( VikViewport *vvp )
     vvp->xmpp /= 2;
     vvp->ympp /= 2;
 
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-      viewport_google_rezoom ( vvp );
-
     viewport_utm_zone_check(vvp);
   }
 }
@@ -487,9 +715,6 @@ void vik_viewport_zoom_out ( VikViewport *vvp )
     vvp->xmpp *= 2;
     vvp->ympp *= 2;
 
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-      viewport_google_rezoom ( vvp );
-
     viewport_utm_zone_check(vvp);
   }
 }
@@ -517,8 +742,6 @@ void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
     vvp->xmpp = xmpp;
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
       viewport_utm_zone_check(vvp);
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-      viewport_google_rezoom ( vvp );
   }
 }
 
@@ -528,8 +751,6 @@ void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
     vvp->ympp = ympp;
     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
       viewport_utm_zone_check(vvp);
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-      viewport_google_rezoom ( vvp );
   }
 }
 
@@ -655,13 +876,13 @@ gint vik_viewport_get_height( VikViewport *vvp )
 
 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
 {
+  g_return_if_fail ( vvp != NULL );
+
   if ( vvp->coord_mode == VIK_COORD_UTM ) {
     int zone_delta;
     struct UTM *utm = (struct UTM *) coord;
     coord->mode = VIK_COORD_UTM;
 
-    g_return_if_fail ( vvp != NULL );
-
     utm->zone = vvp->center.utm_zone;
     utm->letter = vvp->center.utm_letter;
     utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
@@ -671,16 +892,12 @@ void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *co
     utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
     coord->mode = VIK_COORD_LATLON;
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
-      calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width/2, vvp->height/2);
-    else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) {
-      /* google */
-      coord->east_west = (x - (vvp->width/2)) * vvp->google_calcx_rev_fact + vvp->center.east_west;
-      coord->north_south = ((vvp->height/2) - y) * vvp->google_calcy_rev_fact + vvp->center.north_south;
-    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) {
+    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
       coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
       coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y));
-    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
+    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
+      calcxy_rev(&(coord->east_west), &(coord->north_south), x, y, vvp->center.east_west, vvp->center.north_south, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width/2, vvp->height/2);
+    else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
       /* FIXMERCATOR */
       coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
       coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
@@ -724,17 +941,13 @@ void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int
     struct LatLon *center = (struct LatLon *) &(vvp->center);
     struct LatLon *ll = (struct LatLon *) coord;
     double xx,yy;
-    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
-      calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width / 2, vvp->height / 2 );
-      *x = xx; *y = yy;
-    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) {
-      /* google */
-      *x = vvp->google_calcx_fact * (ll->lon - center->lon) + (vvp->width/2);
-      *y = vvp->google_calcy_fact * (center->lat - ll->lat) + (vvp->height/2);
-    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) {
-      /* subtract, convert to KH coords; blow it up by 256 */
+    if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
+      /* FIXMERCATOR: Optimize */
       *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0;
+    } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
+      calcxy ( &xx, &yy, center->lon, center->lat, ll->lon, ll->lat, vvp->xmpp * ALTI_TO_MPP, vvp->ympp * ALTI_TO_MPP, vvp->width / 2, vvp->height / 2 );
+      *x = xx; *y = yy;
     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
       /* FIXMERCATOR: Optimize */
       *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
@@ -757,7 +970,6 @@ void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
     *y2 = *y1 + (shrinkfactor * (*y2-*y1));
-    g_print("%f, %d, %d\n", shrinkfactor, *x2, *y2);
   } else if ( *y2 > 20000 || *y2 < -20000 ) {
     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
@@ -988,8 +1200,6 @@ void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode
     viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
   else {
     viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
-    if ( drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
-      viewport_google_rezoom ( vvp );
   }
 }
 
@@ -998,26 +1208,121 @@ VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
   return vvp->drawmode;
 }
 
-static void viewport_google_rezoom ( VikViewport *vvp )
+/******** triggering *******/
+void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
 {
-  vvp->google_calcx_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 * 0.7716245833877 / vvp->xmpp);
-  vvp->google_calcy_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 / vvp->ympp);
-  vvp->google_calcx_rev_fact = 1 / vvp->google_calcx_fact;
-  vvp->google_calcy_rev_fact = 1 / vvp->google_calcy_fact;
+  vp->trigger = trigger;
 }
 
-const gchar *vik_viewport_drawmode_name(VikViewportDrawMode mode)
+gpointer vik_viewport_get_trigger ( VikViewport *vp )
 {
-  static gchar *names[] = {
-    "UTM",
-    "Expedia",
-    "Old Google",
-    "Old KH",
-    "Google"
-  };
+  return vp->trigger;
+}
 
-  if (mode < VIK_VIEWPORT_NUM_DRAWMODES)
-    return names[mode];
-  return NULL;
+void vik_viewport_snapshot_save ( VikViewport *vp )
+{
+  gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
+}
 
+void vik_viewport_snapshot_load ( VikViewport *vp )
+{
+  gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
+}
+
+void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
+{
+  vp->half_drawn = half_drawn;
+}
+
+gboolean vik_viewport_get_half_drawn( VikViewport *vp )
+{
+  return vp->half_drawn;
+}
+
+
+const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
+ {
+  const gchar *name = NULL;
+  VikWindow *vw = NULL;
+  GtkWidget *mode_button;
+  GtkWidget *label;
+  
+  vw = VIK_WINDOW_FROM_WIDGET(vv);
+  mode_button = vik_window_get_drawmode_button(vw, mode);
+  label = gtk_bin_get_child(GTK_BIN(mode_button));
+
+  name = gtk_label_get_text ( GTK_LABEL(label) );
+
+  return name;
+
+}
+
+void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
+{
+  VikCoord tleft, tright, bleft, bright;
+
+  vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
+  vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
+  vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
+  vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
+
+  vik_coord_convert(&tleft, VIK_COORD_LATLON);
+  vik_coord_convert(&tright, VIK_COORD_LATLON);
+  vik_coord_convert(&bleft, VIK_COORD_LATLON);
+  vik_coord_convert(&bright, VIK_COORD_LATLON);
+
+  *max_lat = MAX(tleft.north_south, tright.north_south);
+  *min_lat = MIN(bleft.north_south, bright.north_south);
+  *max_lon = MAX(tright.east_west, bright.east_west);
+  *min_lon = MIN(tleft.east_west, bleft.east_west);
+}
+
+void vik_viewport_reset_copyrights ( VikViewport *vp ) 
+{
+  g_return_if_fail ( vp != NULL );
+  g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
+  g_slist_free ( vp->copyrights );
+  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 );
+    if ( found == NULL )
+    {
+      gchar *duple = g_strdup ( copyright );
+      vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
+    }
+  }
+}
+
+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 );
+    }
+  }
 }