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