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