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