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