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