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