]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikviewport.c
Fix: enforce defensive code
[andy/viking.git] / src / vikviewport.c
index 162309087e715c824b8af35ee752d6a04d447125..6158a4606e7c00dd2f042dd71b47d4e7230af7e2 100644 (file)
@@ -162,16 +162,22 @@ 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.mode = VIK_COORD_LATLON;
-  vvp->center.north_south = 40.714490;
-  vvp->center.east_west = -74.007130;
-  vvp->center.utm_zone = 31;
-  vvp->center.utm_letter = 'N';
+  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;
@@ -207,14 +213,16 @@ 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 );
 }
@@ -222,12 +230,14 @@ void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
 
 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;
 }
@@ -284,8 +294,7 @@ gboolean vik_viewport_configure ( VikViewport *vvp )
   /* this is down here so it can get a GC (necessary?) */
   if ( ! vvp->background_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->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
   }
   if ( !vvp->scale_bg_gc) {
     vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
@@ -338,6 +347,8 @@ 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;
@@ -346,12 +357,22 @@ void vik_viewport_draw_scale ( VikViewport *vvp )
     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_coord_diff ( &left, &right ) * 0.00621371192;
+      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;
@@ -405,15 +426,34 @@ 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;
   }
 }
 
@@ -429,6 +469,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;
 
@@ -674,13 +716,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;