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