]> git.street.me.uk Git - andy/viking.git/blob - src/vikviewport.c
Add the storage and retrieval of any kind of track or waypoint selected item (either...
[andy/viking.git] / src / vikviewport.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * Lat/Lon plotting functions calcxy* are from GPSDrive
7  * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
8  *
9  * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
31
32 #include <gtk/gtk.h>
33 #ifdef HAVE_MATH_H
34 #include <math.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39
40 #include "coords.h"
41 #include "vikcoord.h"
42 #include "vikwindow.h"
43 #include "vikviewport.h"
44
45 #include "mapcoord.h"
46
47 /* for ALTI_TO_MPP */
48 #include "globals.h"
49
50 static gdouble EASTING_OFFSET = 500000.0;
51
52 static gint PAD = 10;
53
54 static void viewport_class_init ( VikViewportClass *klass );
55 static void viewport_init ( VikViewport *vvp );
56 static void viewport_finalize ( GObject *gob );
57 static void viewport_utm_zone_check ( VikViewport *vvp );
58
59 static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 );
60 static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 );
61 double calcR (double lat);
62
63 static double Radius[181];
64 static void viewport_init_ra();
65
66 static GObjectClass *parent_class;
67
68
69 struct _VikViewport {
70   GtkDrawingArea drawing_area;
71   GdkPixmap *scr_buffer;
72   gint width, height;
73   VikCoord center;
74   VikCoordMode coord_mode;
75   gdouble xmpp, ympp;
76
77   GdkPixbuf *alpha_pixbuf;
78   guint8 alpha_pixbuf_width;
79   guint8 alpha_pixbuf_height;
80
81   gdouble utm_zone_width;
82   gboolean one_utm_zone;
83
84   GdkGC *background_gc;
85   GdkColor background_color;
86   GdkGC *scale_bg_gc;
87
88   GSList *copyrights;
89   GSList *logos;
90
91   /* Wether or not display OSD info */
92   gboolean draw_scale;
93   gboolean draw_centermark;
94   gboolean draw_highlight;
95
96   /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
97   VikViewportDrawMode drawmode;
98
99   /* handy conversion factors which make google plotting extremely fast */
100   gdouble google_calcx_fact;
101   gdouble google_calcy_fact;
102   gdouble google_calcx_rev_fact;
103   gdouble google_calcy_rev_fact;
104
105   /* trigger stuff */
106   gpointer trigger;
107   GdkPixmap *snapshot_buffer;
108   gboolean half_drawn;
109 };
110
111 static gdouble
112 viewport_utm_zone_width ( VikViewport *vvp )
113 {
114   if ( vvp->coord_mode == VIK_COORD_UTM ) {
115     struct LatLon ll;
116
117     /* get latitude of screen bottom */
118     struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
119     utm.northing -= vvp -> height * vvp -> ympp / 2;
120     a_coords_utm_to_latlon ( &utm, &ll );
121
122     /* boundary */
123     ll.lon = (utm.zone - 1) * 6 - 180 ;
124     a_coords_latlon_to_utm ( &ll, &utm);
125     return fabs ( utm.easting - EASTING_OFFSET ) * 2;
126   } else
127     return 0.0;
128 }
129
130
131 GType vik_viewport_get_type (void)
132 {
133   static GType vvp_type = 0;
134
135   if (!vvp_type)
136   {
137     static const GTypeInfo vvp_info = 
138     {
139       sizeof (VikViewportClass),
140       NULL, /* base_init */
141       NULL, /* base_finalize */
142       (GClassInitFunc) viewport_class_init,
143       NULL, /* class_finalize */
144       NULL, /* class_data */
145       sizeof (VikViewport),
146       0,
147       (GInstanceInitFunc) viewport_init,
148     };
149     vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
150   }
151   return vvp_type;
152 }
153
154 static void viewport_class_init ( VikViewportClass *klass )
155 {
156   /* Destructor */
157   GObjectClass *object_class;
158
159   object_class = G_OBJECT_CLASS (klass);
160
161   object_class->finalize = viewport_finalize;
162
163   parent_class = g_type_class_peek_parent (klass);
164 }
165
166 VikViewport *vik_viewport_new ()
167 {
168   VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
169   return vv;
170 }
171
172 static void viewport_init ( VikViewport *vvp )
173 {
174   viewport_init_ra();
175
176   struct UTM utm;
177   struct LatLon ll;
178   ll.lat = a_vik_get_default_lat();
179   ll.lon = a_vik_get_default_long();
180   a_coords_latlon_to_utm ( &ll, &utm );
181
182   /* TODO: not static */
183   vvp->xmpp = 4.0;
184   vvp->ympp = 4.0;
185   vvp->coord_mode = VIK_COORD_LATLON;
186   vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
187   vvp->center.mode = VIK_COORD_LATLON;
188   vvp->center.north_south = ll.lat;
189   vvp->center.east_west = ll.lon;
190   vvp->center.utm_zone = (int)utm.zone;
191   vvp->center.utm_letter = utm.letter;
192   vvp->scr_buffer = NULL;
193   vvp->alpha_pixbuf = NULL;
194   vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
195   vvp->utm_zone_width = 0.0;
196   vvp->background_gc = NULL;
197   vvp->scale_bg_gc = NULL;
198
199   vvp->copyrights = NULL;
200
201   vvp->draw_scale = TRUE;
202   vvp->draw_centermark = TRUE;
203   vvp->draw_highlight = TRUE;
204
205   vvp->trigger = NULL;
206   vvp->snapshot_buffer = NULL;
207   vvp->half_drawn = FALSE;
208
209   g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
210
211   GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
212 }
213
214 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
215 {
216   GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
217   *rv = vvp->background_color;
218   return rv;
219 }
220
221 /* returns pointer to internal static storage, changes next time function called, use quickly */
222 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
223 {
224   static gchar color[8];
225   g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->background_color.red/256),(int)(vvp->background_color.green/256),(int)(vvp->background_color.blue/256));
226   return color;
227 }
228
229 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
230 {
231   g_assert ( vvp && vvp->background_gc );
232   if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
233     gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
234   else
235     g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
236 }
237
238 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
239 {
240   g_assert ( vvp && vvp->background_gc );
241   vvp->background_color = *color;
242   gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
243 }
244
245
246 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
247 {
248   GdkGC *rv = NULL;
249   GdkColor color;
250
251   rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
252   if ( gdk_color_parse ( colorname, &color ) )
253     gdk_gc_set_rgb_fg_color ( rv, &color );
254   else
255     g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
256   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
257   return rv;
258 }
259
260 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
261 {
262   GdkGC *rv;
263
264   rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
265   gdk_gc_set_rgb_fg_color ( rv, color );
266   gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
267   return rv;
268 }
269
270 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
271 {
272   vvp->width = width;
273   vvp->height = height;
274   if ( vvp->scr_buffer )
275     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
276   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
277
278   /* TODO trigger: only if this is enabled !!! */
279   if ( vvp->snapshot_buffer )
280     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
281   vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
282 }
283
284
285 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
286 {
287   return vvp->scr_buffer;
288 }
289
290 gboolean vik_viewport_configure ( VikViewport *vvp )
291 {
292   g_return_val_if_fail ( vvp != NULL, TRUE );
293
294   vvp->width = GTK_WIDGET(vvp)->allocation.width;
295   vvp->height = GTK_WIDGET(vvp)->allocation.height;
296
297   if ( vvp->scr_buffer )
298     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
299
300   vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
301
302   /* TODO trigger: only if enabled! */
303   if ( vvp->snapshot_buffer )
304     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
305
306   vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
307   /* TODO trigger */
308
309   /* this is down here so it can get a GC (necessary?) */
310   if ( !vvp->background_gc )
311   {
312     vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
313     vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
314   }
315   if ( !vvp->scale_bg_gc) {
316     vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
317   }
318
319   return FALSE; 
320 }
321
322 static void viewport_finalize ( GObject *gob )
323 {
324   VikViewport *vvp = VIK_VIEWPORT(gob);
325
326   g_return_if_fail ( vvp != NULL );
327
328   if ( vvp->scr_buffer )
329     g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
330
331   if ( vvp->snapshot_buffer )
332     g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
333
334   if ( vvp->alpha_pixbuf )
335     g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
336
337   if ( vvp->background_gc )
338     g_object_unref ( G_OBJECT ( vvp->background_gc ) );
339
340   if ( vvp->scale_bg_gc ) {
341     g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
342     vvp->scale_bg_gc = NULL;
343   }
344
345   G_OBJECT_CLASS(parent_class)->finalize(gob);
346 }
347
348 /**
349  * vik_viewport_clear:
350  * @vvp: self object
351  * 
352  * Clear the whole viewport.
353  */
354 void vik_viewport_clear ( VikViewport *vvp )
355 {
356   g_return_if_fail ( vvp != NULL );
357   gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
358   vik_viewport_reset_copyrights ( vvp );
359   vik_viewport_reset_logos ( vvp );
360 }
361
362 /**
363  * vik_viewport_set_draw_scale:
364  * @vvp: self
365  * @draw_scale: new value
366  * 
367  * Enable/Disable display of scale.
368  */
369 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
370 {
371   vvp->draw_scale = draw_scale;
372 }
373
374 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
375 {
376   return vvp->draw_scale;
377 }
378
379 void vik_viewport_draw_scale ( VikViewport *vvp )
380 {
381   g_return_if_fail ( vvp != NULL );
382
383   if ( vvp->draw_scale ) {
384     VikCoord left, right;
385     gdouble unit, base, diff, old_unit, old_diff, ratio;
386     gint odd, len, SCSIZE = 5, HEIGHT=10;
387     PangoFontDescription *pfd;
388     PangoLayout *pl;
389     gchar s[128];
390
391     vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
392     vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
393
394     vik_units_distance_t dist_units = a_vik_get_units_distance ();
395     switch (dist_units) {
396     case VIK_UNITS_DISTANCE_KILOMETRES:
397       base = vik_coord_diff ( &left, &right ); // in meters
398       break;
399     case VIK_UNITS_DISTANCE_MILES:
400       // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
401       base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
402       break;
403     default:
404       base = 1; // Keep the compiler happy
405       g_critical("Houston, we've had a problem. distance=%d", dist_units);
406     }
407     ratio = (vvp->width/SCSIZE)/base;
408
409     unit = 1;
410     diff = fabs(base-unit);
411     old_unit = unit;
412     old_diff = diff;
413     odd = 1;
414     while (diff <= old_diff) {
415       old_unit = unit;
416       old_diff = diff;
417       unit = unit * (odd%2 ? 5 : 2);
418       diff = fabs(base-unit);
419       odd++;
420     }
421     unit = old_unit;
422     len = unit * ratio;
423
424     /* white background */
425     vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
426                          PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
427     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
428                          PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
429     vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
430                          PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
431     /* black scale */
432     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
433                          PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
434     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
435                          PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
436     vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
437                          PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
438     if (odd%2) {
439       int i;
440       for (i=1; i<5; i++) {
441         vik_viewport_draw_line(vvp, vvp->scale_bg_gc, 
442                              PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
443         vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
444                              PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
445       }
446     } else {
447       int i;
448       for (i=1; i<10; i++) {
449         vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
450                              PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
451         vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, 
452                              PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
453       }
454     }
455     pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); 
456     pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
457     pango_layout_set_font_description (pl, pfd);
458     pango_font_description_free (pfd);
459
460     switch (dist_units) {
461     case VIK_UNITS_DISTANCE_KILOMETRES:
462       if (unit >= 1000) {
463         sprintf(s, "%d km", (int)unit/1000);
464       } else {
465         sprintf(s, "%d m", (int)unit);
466       }
467       break;
468     case VIK_UNITS_DISTANCE_MILES:
469       // Handle units in 0.1 miles
470       if (unit < 10.0) {
471         sprintf(s, "%0.1f miles", unit/10.0);
472       }
473       else if ((int)unit == 10.0) {
474         sprintf(s, "1 mile");
475       }
476       else {
477         sprintf(s, "%d miles", (int)(unit/10.0));
478       }
479       break;
480     default:
481       g_critical("Houston, we've had a problem. distance=%d", dist_units);
482     }
483     pango_layout_set_text(pl, s, -1);
484     vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
485                            PAD + len + PAD, vvp->height - PAD - 10, pl);
486     g_object_unref(pl);
487     pl = NULL;
488   }
489 }
490
491 void vik_viewport_draw_copyright ( VikViewport *vvp )
492 {
493   g_return_if_fail ( vvp != NULL );
494
495   PangoFontDescription *pfd;
496   PangoLayout *pl;
497   PangoRectangle ink_rect, logical_rect;
498   gchar s[128] = "";
499
500   /* compute copyrights string */
501   guint len = g_slist_length ( vvp->copyrights );
502   int i;
503   for (i = 0 ; i < len ; i++)
504   {
505     gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
506     strcat ( s, copyright );
507     strcat ( s, " " );
508   }
509
510   /* create pango layout */
511   pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); 
512   pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
513   pango_layout_set_font_description (pl, pfd);
514   pango_font_description_free (pfd);
515   pfd = NULL;
516   pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
517
518   /* Set the text */
519   pango_layout_set_text(pl, s, -1);
520
521   /* Use maximum of half the viewport width */
522   pango_layout_set_width ( pl, ( vvp->width / 2 - PAD ) * PANGO_SCALE );
523   pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
524   vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
525                            vvp->width / 2, vvp->height - PAD - logical_rect.height, pl);
526
527   /* Free memory */
528   g_object_unref(pl);
529   pl = NULL;            
530 }
531
532 /**
533  * vik_viewport_set_draw_centermark:
534  * @vvp: self object
535  * @draw_centermark: new value
536  * 
537  * Enable/Disable display of center mark.
538  */
539 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
540 {
541   vvp->draw_centermark = draw_centermark;
542 }
543
544 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
545 {
546   return vvp->draw_centermark;
547 }
548
549 void vik_viewport_draw_centermark ( VikViewport *vvp )
550 {
551   g_return_if_fail ( vvp != NULL );
552
553   if ( !vvp->draw_centermark )
554     return;
555
556   const int len = 30;
557   const int gap = 4;
558   int center_x = vvp->width/2;
559   int center_y = vvp->height/2;
560   GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
561
562   /* white back ground */
563   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
564   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
565   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
566   vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
567   /* black fore ground */
568   vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
569   vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
570   vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
571   vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
572   
573 }
574
575 void vik_viewport_draw_logo ( VikViewport *vvp )
576 {
577   g_return_if_fail ( vvp != NULL );
578
579   /* compute copyrights string */
580   guint len = g_slist_length ( vvp->logos );
581   gint x = vvp->width - PAD;
582   gint y = PAD;
583   int i;
584   for (i = 0 ; i < len ; i++)
585   {
586     GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
587     gint width = gdk_pixbuf_get_width ( logo );
588     gint height = gdk_pixbuf_get_height ( logo );
589     vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
590     x = x - width - PAD;
591   }
592 }
593
594 void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
595 {
596   vvp->draw_highlight = draw_highlight;
597 }
598
599 gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
600 {
601   return vvp->draw_highlight;
602 }
603
604 void vik_viewport_sync ( VikViewport *vvp )
605 {
606   g_return_if_fail ( vvp != NULL );
607   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);
608 }
609
610 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
611 {
612   gint x, y, wid, hei;
613
614   g_return_if_fail ( vvp != NULL );
615   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);
616
617   if (x_off >= 0) {
618     x = 0;
619     wid = x_off;
620   } else {
621     x = vvp->width+x_off; 
622     wid = -x_off;
623   }
624   if (y_off >= 0) {
625     y = 0;
626     hei = y_off;
627   } else {
628     y = vvp->height+y_off; 
629     hei = -y_off;
630   }
631   gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
632   gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
633 }
634
635 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
636 {
637   g_return_if_fail ( vvp != NULL );
638   if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
639     vvp->xmpp = vvp->ympp = xympp;
640
641   if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
642     viewport_utm_zone_check(vvp);
643 }
644
645 /* or could do factor */
646 void vik_viewport_zoom_in ( VikViewport *vvp )
647 {
648   g_return_if_fail ( vvp != NULL );
649   if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
650   {
651     vvp->xmpp /= 2;
652     vvp->ympp /= 2;
653
654     viewport_utm_zone_check(vvp);
655   }
656 }
657
658 void vik_viewport_zoom_out ( VikViewport *vvp )
659 {
660   g_return_if_fail ( vvp != NULL );
661   if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
662   {
663     vvp->xmpp *= 2;
664     vvp->ympp *= 2;
665
666     viewport_utm_zone_check(vvp);
667   }
668 }
669
670 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
671 {
672   if ( vvp->xmpp == vvp->ympp )
673     return vvp->xmpp;
674   return 0.0;
675 }
676
677 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
678 {
679   return vvp->xmpp;
680 }
681
682 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
683 {
684   return vvp->ympp;
685 }
686
687 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
688 {
689   if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
690     vvp->xmpp = xmpp;
691     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
692       viewport_utm_zone_check(vvp);
693   }
694 }
695
696 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
697 {
698   if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
699     vvp->ympp = ympp;
700     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
701       viewport_utm_zone_check(vvp);
702   }
703 }
704
705
706 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
707 {
708   g_return_val_if_fail ( vvp != NULL, NULL );
709   return &(vvp->center);
710 }
711
712 /* called every time we update coordinates/zoom */
713 static void viewport_utm_zone_check ( VikViewport *vvp )
714 {
715   if ( vvp->coord_mode == VIK_COORD_UTM )
716   {
717     struct UTM utm;
718     struct LatLon ll;
719     a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
720     a_coords_latlon_to_utm ( &ll, &utm );
721     if ( utm.zone != vvp->center.utm_zone )
722       *((struct UTM *)(&vvp->center)) = utm;
723
724     /* misc. stuff so we don't have to check later */
725     vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
726     vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
727   }
728 }
729
730 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
731 {
732   vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
733   if ( vvp->coord_mode == VIK_COORD_UTM )
734     viewport_utm_zone_check ( vvp );
735 }
736
737 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
738 {
739   vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
740   if ( vvp->coord_mode == VIK_COORD_UTM )
741     viewport_utm_zone_check ( vvp );
742 }
743
744 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
745 {
746   vvp->center = *coord;
747   if ( vvp->coord_mode == VIK_COORD_UTM )
748     viewport_utm_zone_check ( vvp );
749 }
750
751 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
752 {
753   g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
754
755   /* get center, then just offset */
756   vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
757   ul->mode = VIK_COORD_UTM;
758   *br = *ul;
759
760   ul->north_south += (vvp->ympp * vvp->height / 2);
761   ul->east_west -= (vvp->xmpp * vvp->width / 2);
762   br->north_south -= (vvp->ympp * vvp->height / 2);
763   br->east_west += (vvp->xmpp * vvp->width / 2);
764 }
765
766 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
767 {
768   if ( vvp->coord_mode == VIK_COORD_UTM ) {
769     *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
770     center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
771     center->zone = zone;
772   }
773 }
774
775 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
776 {
777   if ( vvp->coord_mode == VIK_COORD_UTM ) {
778     VikCoord coord;
779     g_assert ( vvp != NULL );
780     vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
781     return coord.utm_zone;
782   }
783   return '\0';
784 }
785
786 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
787 {
788   if ( vvp->coord_mode == VIK_COORD_UTM ) {
789     VikCoord coord;
790     g_assert ( vvp != NULL );
791     vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
792     return coord.utm_zone;
793   }
794   return '\0';
795 }
796
797
798 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
799 {
800   g_return_if_fail ( vvp != NULL );
801   if ( vvp->coord_mode == VIK_COORD_UTM ) {
802     /* slightly optimized */
803     vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
804     vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
805     viewport_utm_zone_check ( vvp );
806   } else {
807     VikCoord tmp;
808     vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
809     vik_viewport_set_center_coord ( vvp, &tmp );
810   }
811 }
812
813 gint vik_viewport_get_width( VikViewport *vvp )
814 {
815   g_return_val_if_fail ( vvp != NULL, 0 );
816   return vvp->width;
817 }
818
819 gint vik_viewport_get_height( VikViewport *vvp )
820 {
821   g_return_val_if_fail ( vvp != NULL, 0 );
822   return vvp->height;
823 }
824
825 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
826 {
827   g_return_if_fail ( vvp != NULL );
828
829   if ( vvp->coord_mode == VIK_COORD_UTM ) {
830     int zone_delta;
831     struct UTM *utm = (struct UTM *) coord;
832     coord->mode = VIK_COORD_UTM;
833
834     utm->zone = vvp->center.utm_zone;
835     utm->letter = vvp->center.utm_letter;
836     utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
837     zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
838     utm->zone += zone_delta;
839     utm->easting -= zone_delta * vvp->utm_zone_width;
840     utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
841   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
842     coord->mode = VIK_COORD_LATLON;
843     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
844       coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
845       coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y));
846     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
847       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);
848     else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
849       /* FIXMERCATOR */
850       coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
851       coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
852
853 #if 0
854 -->     THIS IS JUNK HERE.
855       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
856
857       (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
858       DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
859 #endif
860     }
861   }
862 }
863
864 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
865 {
866   static VikCoord tmp;
867   g_return_if_fail ( vvp != NULL );
868
869   if ( coord->mode != vvp->coord_mode )
870   {
871     g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
872     vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
873     coord = &tmp;
874   }
875
876   if ( vvp->coord_mode == VIK_COORD_UTM ) {
877     struct UTM *center = (struct UTM *) &(vvp->center);
878     struct UTM *utm = (struct UTM *) coord;
879     if ( center->zone != utm->zone && vvp->one_utm_zone )
880     {
881       *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
882       return;
883     }
884
885     *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
886           (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
887     *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
888   } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
889     struct LatLon *center = (struct LatLon *) &(vvp->center);
890     struct LatLon *ll = (struct LatLon *) coord;
891     double xx,yy;
892     if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
893       /* FIXMERCATOR: Optimize */
894       *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
895       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0;
896     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
897       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 );
898       *x = xx; *y = yy;
899     } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
900       /* FIXMERCATOR: Optimize */
901       *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
902       *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
903     }
904   }
905 }
906
907 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
908 {
909   if ( *x1 > 20000 || *x1 < -20000 ) {
910     gdouble shrinkfactor = ABS(20000.0 / *x1);
911     *x1 = *x2 + (shrinkfactor * (*x1-*x2));
912     *y1 = *y2 + (shrinkfactor * (*y1-*y2));
913   } else if ( *y1 > 20000 || *y1 < -20000 ) {
914     gdouble shrinkfactor = ABS(20000.0 / *x1);
915     *x1 = *x2 + (shrinkfactor * (*x1-*x2));
916     *y1 = *y2 + (shrinkfactor * (*y1-*y2));
917   } else if ( *x2 > 20000 || *x2 < -20000 ) {
918     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
919     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
920     *y2 = *y1 + (shrinkfactor * (*y2-*y1));
921   } else if ( *y2 > 20000 || *y2 < -20000 ) {
922     gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
923     *x2 = *x1 + (shrinkfactor * (*x2-*x1));
924     *y2 = *y1 + (shrinkfactor * (*y2-*y1));
925   }
926 }
927
928 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
929 {
930   if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
931        ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
932     /*** clipping, yeah! ***/
933     a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
934     gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
935   }
936 }
937
938 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
939 {
940   if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
941     gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
942 }
943
944 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
945 {
946   if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
947     gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
948 }
949
950 /* shouldn't use this -- slow -- change the alpha channel instead. */
951 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
952                                            gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
953 {
954   gint real_dest_x = MAX(dest_x,0);
955   gint real_dest_y = MAX(dest_y,0);
956
957   if ( alpha == 0 )
958     return; /* don't waste your time */
959
960   if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
961   {
962     if ( vvp->alpha_pixbuf )
963       g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
964     vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
965     vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
966     vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
967   }
968
969   w = MIN(w,vvp->width - dest_x);
970   h = MIN(h,vvp->height - dest_y);
971
972   /* check that we are drawing within boundaries. */
973   src_x += (real_dest_x - dest_x);
974   src_y += (real_dest_y - dest_y);
975   w -= (real_dest_x - dest_x);
976   h -= (real_dest_y - dest_y);
977
978   gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
979                                  real_dest_x, real_dest_y, 0, 0, w, h );
980
981   /* do a composite */
982   gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
983
984   /* draw pixbuf_tmp */
985   vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
986 }
987
988 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
989                               gint dest_x, gint dest_y, gint w, gint h )
990 {
991   gdk_draw_pixbuf ( vvp->scr_buffer,
992 // GTK_WIDGET(vvp)->style->black_gc,
993 NULL,
994  pixbuf,
995                     src_x, src_y, dest_x, dest_y, w, h,
996                     GDK_RGB_DITHER_NONE, 0, 0 );
997 }
998
999 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
1000 {
1001   gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
1002 }
1003
1004
1005 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
1006 {
1007   gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
1008 }
1009
1010 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
1011 {
1012   g_assert ( vvp );
1013   return vvp->coord_mode;
1014 }
1015
1016 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
1017 {
1018   g_return_if_fail ( vvp != NULL );
1019   vvp->coord_mode = mode;
1020   vik_coord_convert ( &(vvp->center), mode );
1021 }
1022
1023 /* Thanks GPSDrive */
1024 static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
1025 {
1026   int px, py;
1027   gdouble dif, lat, lon;
1028   double Ra = Radius[90+(gint)zero_lat];
1029
1030   px = (mapSizeX2 - x) * pixelfact_x;
1031   py = (-mapSizeY2 + y) * pixelfact_y;
1032
1033   lat = zero_lat - py / Ra;
1034   lat = zero_lat - py / Ra;
1035   lon =
1036     zero_long -
1037     px / (Ra *
1038          cos (lat * DEG2RAD));
1039
1040   dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
1041   lat = lat - dif / 1.5;
1042   lon =
1043     zero_long -
1044     px / (Ra *
1045               cos (lat * DEG2RAD));
1046
1047   *lt = lat;
1048   *lg = lon;
1049   return (TRUE);
1050 }
1051
1052 /* Thanks GPSDrive */
1053 static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
1054 {
1055     double dif;
1056     double Ra;
1057     gint mapSizeX = 2 * mapSizeX2;
1058     gint mapSizeY = 2 * mapSizeY2;
1059
1060     g_assert ( lt >= -90.0 && lt <= 90.0 );
1061 //    lg *= rad2deg; // FIXME, optimize equations
1062 //    lt *= rad2deg;
1063     Ra = Radius[90+(gint)lt];
1064     *x = Ra *
1065          cos (lt*DEG2RAD) * (lg - zero_long);
1066     *y = Ra * (lt - zero_lat);
1067     dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
1068     *y = *y + dif / 1.85;
1069     *x = *x / pixelfact_x;
1070     *y = *y / pixelfact_y;
1071     *x = mapSizeX2 - *x;
1072     *y += mapSizeY2;
1073     if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
1074         return (FALSE);
1075     return (TRUE);
1076 }
1077
1078 static void viewport_init_ra()
1079 {
1080   static gboolean done_before = FALSE;
1081   if ( !done_before )
1082   {
1083     gint i;
1084     for ( i = -90; i <= 90; i++)
1085       Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
1086     done_before = TRUE;
1087   }
1088 }
1089
1090 double calcR (double lat)
1091 {
1092     double a = 6378.137, r, sc, x, y, z;
1093     double e2 = 0.081082 * 0.081082;
1094     /*
1095      * the radius of curvature of an ellipsoidal Earth in the plane of the
1096      * meridian is given by
1097      *
1098      * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
1099      *
1100      *
1101      * where a is the equatorial radius, b is the polar radius, and e is
1102      * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
1103      *
1104      * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
1105      * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
1106      * = 0.081082 Eccentricity
1107      */
1108
1109     lat = lat * DEG2RAD;
1110     sc = sin (lat);
1111     x = a * (1.0 - e2);
1112     z = 1.0 - e2 * sc * sc;
1113     y = pow (z, 1.5);
1114     r = x / y;
1115     r = r * 1000.0;
1116     return r;
1117 }
1118
1119 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1120 {
1121   return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1122 }
1123
1124 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1125 {
1126   if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1127     gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1128 }
1129
1130 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1131 {
1132   static GdkGCValues values;
1133   gdk_gc_get_values ( gc, &values );
1134   gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1135 }
1136
1137 GdkFunction vik_gc_get_function ( GdkGC *gc )
1138 {
1139   static GdkGCValues values;
1140   gdk_gc_get_values ( gc, &values );
1141   return values.function;
1142 }
1143
1144 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1145 {
1146   vvp->drawmode = drawmode;
1147   if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1148     viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1149   else {
1150     viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1151   }
1152 }
1153
1154 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1155 {
1156   return vvp->drawmode;
1157 }
1158
1159 /******** triggering *******/
1160 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1161 {
1162   vp->trigger = trigger;
1163 }
1164
1165 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1166 {
1167   return vp->trigger;
1168 }
1169
1170 void vik_viewport_snapshot_save ( VikViewport *vp )
1171 {
1172   gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1173 }
1174
1175 void vik_viewport_snapshot_load ( VikViewport *vp )
1176 {
1177   gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1178 }
1179
1180 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1181 {
1182   vp->half_drawn = half_drawn;
1183 }
1184
1185 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1186 {
1187   return vp->half_drawn;
1188 }
1189
1190
1191 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1192  {
1193   const gchar *name = NULL;
1194   VikWindow *vw = NULL;
1195   GtkWidget *mode_button;
1196   GtkWidget *label;
1197   
1198   vw = VIK_WINDOW_FROM_WIDGET(vv);
1199   mode_button = vik_window_get_drawmode_button(vw, mode);
1200   label = gtk_bin_get_child(GTK_BIN(mode_button));
1201
1202   name = gtk_label_get_text ( GTK_LABEL(label) );
1203
1204   return name;
1205
1206 }
1207
1208 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1209 {
1210   VikCoord tleft, tright, bleft, bright;
1211
1212   vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1213   vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1214   vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1215   vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1216
1217   vik_coord_convert(&tleft, VIK_COORD_LATLON);
1218   vik_coord_convert(&tright, VIK_COORD_LATLON);
1219   vik_coord_convert(&bleft, VIK_COORD_LATLON);
1220   vik_coord_convert(&bright, VIK_COORD_LATLON);
1221
1222   *max_lat = MAX(tleft.north_south, tright.north_south);
1223   *min_lat = MIN(bleft.north_south, bright.north_south);
1224   *max_lon = MAX(tright.east_west, bright.east_west);
1225   *min_lon = MIN(tleft.east_west, bleft.east_west);
1226 }
1227
1228 void vik_viewport_reset_copyrights ( VikViewport *vp ) 
1229 {
1230   g_return_if_fail ( vp != NULL );
1231   g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
1232   g_slist_free ( vp->copyrights );
1233   vp->copyrights = NULL;
1234 }
1235
1236 /**
1237  * vik_viewport_add_copyright:
1238  * @vp: self object
1239  * @copyright: new copyright to display
1240  * 
1241  * Add a copyright to display on viewport.
1242  */
1243 void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright ) 
1244 {
1245   g_return_if_fail ( vp != NULL );
1246   if ( copyright )
1247   {
1248     gchar *found = (gchar*)g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
1249     if ( found == NULL )
1250     {
1251       gchar *duple = g_strdup ( copyright );
1252       vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
1253     }
1254   }
1255 }
1256
1257 void vik_viewport_reset_logos ( VikViewport *vp )
1258 {
1259   g_return_if_fail ( vp != NULL );
1260   /* do not free elem */
1261   g_slist_free ( vp->logos );
1262   vp->logos = NULL;
1263 }
1264
1265 void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
1266 {
1267   g_return_if_fail ( vp != NULL );
1268   if ( logo )
1269   {
1270     GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
1271     if ( found == NULL )
1272     {
1273       vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
1274     }
1275   }
1276 }