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