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