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