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