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