separate rebuild stuff from gosmore and convert to C
[viking/gosmore.git] / src / vikviewport.c
blob91d1bcb475a955bd9c4dbba52f7350cf5fa0d7f5
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * Lat/Lon plotting functions calcxy* are from GPSDrive
7 * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
9 * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
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.
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.
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
27 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
29 #include <gtk/gtk.h>
30 #include <math.h>
31 #include <stdio.h>
33 #include "coords.h"
34 #include "vikcoord.h"
35 #include "vikwindow.h"
36 #include "vikviewport.h"
38 #include "mapcoord.h"
40 /* for ALTI_TO_MPP */
41 #include "globals.h"
42 #include "googlemaps.h"
43 #include "khmaps.h"
45 static gdouble EASTING_OFFSET = 500000.0;
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 );
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);
56 static double Radius[181];
57 static void viewport_init_ra();
59 static GObjectClass *parent_class;
61 static void viewport_google_rezoom ( VikViewport *vvp );
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;
72 GdkPixbuf *alpha_pixbuf;
73 guint8 alpha_pixbuf_width;
74 guint8 alpha_pixbuf_height;
76 gdouble utm_zone_width;
77 gboolean one_utm_zone;
79 GdkGC *background_gc;
80 GdkColor background_color;
81 GdkGC *scale_bg_gc;
82 gboolean draw_scale;
83 gboolean draw_centermark;
85 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
86 VikViewportDrawMode drawmode;
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;
94 /* trigger stuff */
95 gpointer trigger;
96 GdkPixmap *snapshot_buffer;
97 gboolean half_drawn;
100 static gdouble
101 viewport_utm_zone_width ( VikViewport *vvp )
103 if ( vvp->coord_mode == VIK_COORD_UTM ) {
104 struct LatLon ll;
106 /* get latitude of screen bottom */
107 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
108 utm.northing -= vvp -> height * vvp -> ympp / 2;
109 a_coords_utm_to_latlon ( &utm, &ll );
111 /* boundary */
112 ll.lon = (utm.zone - 1) * 6 - 180 ;
113 a_coords_latlon_to_utm ( &ll, &utm);
114 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
115 } else
116 return 0.0;
120 GType vik_viewport_get_type (void)
122 static GType vvp_type = 0;
124 if (!vvp_type)
126 static const GTypeInfo vvp_info =
128 sizeof (VikViewportClass),
129 NULL, /* base_init */
130 NULL, /* base_finalize */
131 (GClassInitFunc) viewport_class_init,
132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (VikViewport),
136 (GInstanceInitFunc) viewport_init,
138 vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 );
140 return vvp_type;
143 static void viewport_class_init ( VikViewportClass *klass )
145 /* Destructor */
146 GObjectClass *object_class;
148 object_class = G_OBJECT_CLASS (klass);
150 object_class->finalize = viewport_finalize;
152 parent_class = g_type_class_peek_parent (klass);
155 VikViewport *vik_viewport_new ()
157 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
158 return vv;
161 static void viewport_init ( VikViewport *vvp )
163 viewport_init_ra();
165 /* TODO: not static */
166 vvp->xmpp = 4.0;
167 vvp->ympp = 4.0;
168 vvp->coord_mode = VIK_COORD_LATLON;
169 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
170 vvp->center.mode = VIK_COORD_LATLON;
171 vvp->center.north_south = 40.714490;
172 vvp->center.east_west = -74.007130;
173 vvp->center.utm_zone = 31;
174 vvp->center.utm_letter = 'N';
175 vvp->scr_buffer = NULL;
176 vvp->alpha_pixbuf = NULL;
177 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
178 vvp->utm_zone_width = 0.0;
179 vvp->background_gc = NULL;
180 vvp->scale_bg_gc = NULL;
181 vvp->draw_scale = TRUE;
182 vvp->draw_centermark = TRUE;
184 vvp->trigger = NULL;
185 vvp->snapshot_buffer = NULL;
186 vvp->half_drawn = FALSE;
188 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
191 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
193 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
194 *rv = vvp->background_color;
195 return rv;
198 /* returns pointer to internal static storage, changes next time function called, use quickly */
199 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
201 static gchar color[8];
202 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));
203 return color;
206 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
208 g_assert ( vvp->background_gc );
209 gdk_color_parse ( colorname, &(vvp->background_color) );
210 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
213 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
215 g_assert ( vvp->background_gc );
216 vvp->background_color = *color;
217 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
221 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
223 GdkGC *rv;
224 GdkColor color;
226 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
227 gdk_color_parse ( colorname, &color );
228 gdk_gc_set_rgb_fg_color ( rv, &color );
229 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
230 return rv;
233 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
235 GdkGC *rv;
237 rv = gdk_gc_new ( GTK_WIDGET(vvp)->window );
238 gdk_gc_set_rgb_fg_color ( rv, color );
239 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
240 return rv;
243 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
245 vvp->width = width;
246 vvp->height = height;
247 if ( vvp->scr_buffer )
248 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
249 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
251 /* TODO trigger: only if this is enabled !!! */
252 if ( vvp->snapshot_buffer )
253 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
254 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
258 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
260 return vvp->scr_buffer;
263 gboolean vik_viewport_configure ( VikViewport *vvp )
265 g_return_val_if_fail ( vvp != NULL, TRUE );
267 vvp->width = GTK_WIDGET(vvp)->allocation.width;
268 vvp->height = GTK_WIDGET(vvp)->allocation.height;
270 if ( vvp->scr_buffer )
271 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
273 vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
275 /* TODO trigger: only if enabled! */
276 if ( vvp->snapshot_buffer )
277 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
279 vvp->snapshot_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 );
280 /* TODO trigger */
282 /* this is down here so it can get a GC (necessary?) */
283 if ( ! vvp->background_gc )
285 vvp->background_gc = vik_viewport_new_gc ( vvp, "", 1 );
286 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */
288 if ( !vvp->scale_bg_gc) {
289 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
292 return FALSE;
295 static void viewport_finalize ( GObject *gob )
297 VikViewport *vvp = VIK_VIEWPORT(gob);
299 g_return_if_fail ( vvp != NULL );
301 if ( vvp->scr_buffer )
302 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
304 if ( vvp->snapshot_buffer )
305 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
307 if ( vvp->alpha_pixbuf )
308 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
310 if ( vvp->background_gc )
311 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
313 if ( vvp->scale_bg_gc ) {
314 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
315 vvp->scale_bg_gc = NULL;
318 G_OBJECT_CLASS(parent_class)->finalize(gob);
321 void vik_viewport_clear ( VikViewport *vvp )
323 g_return_if_fail ( vvp != NULL );
324 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
327 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
329 vvp->draw_scale = draw_scale;
332 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
334 return vvp->draw_scale;
337 void vik_viewport_draw_scale ( VikViewport *vvp )
339 if ( vvp->draw_scale ) {
340 VikCoord left, right;
341 gdouble unit, base, diff, old_unit, old_diff, ratio;
342 gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
343 PangoFontDescription *pfd;
344 PangoLayout *pl;
345 gchar s[128];
347 g_return_if_fail ( vvp != NULL );
349 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
350 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
352 base = vik_coord_diff ( &left, &right ); // in meters
353 ratio = (vvp->width/SCSIZE)/base;
355 unit = 1;
356 diff = fabs(base-unit);
357 old_unit = unit;
358 old_diff = diff;
359 odd = 1;
360 while (diff <= old_diff) {
361 old_unit = unit;
362 old_diff = diff;
363 unit = unit * (odd%2 ? 5 : 2);
364 diff = fabs(base-unit);
365 odd++;
367 unit = old_unit;
368 len = unit * ratio;
370 /* white background */
371 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
372 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
373 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
374 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
375 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
376 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
377 /* black scale */
378 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
379 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
380 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
381 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
382 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
383 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
384 if (odd%2) {
385 int i;
386 for (i=1; i<5; i++) {
387 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
388 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
389 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
390 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
392 } else {
393 int i;
394 for (i=1; i<10; i++) {
395 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
396 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
397 vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
398 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
401 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
402 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
403 pango_layout_set_font_description (pl, pfd);
404 pango_font_description_free (pfd);
406 if (unit >= 1000) {
407 sprintf(s, "%d km", (int)unit/1000);
408 } else {
409 sprintf(s, "%d m", (int)unit);
411 pango_layout_set_text(pl, s, -1);
412 vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
413 PAD + len + PAD, vvp->height - PAD - 10, pl);
417 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
419 vvp->draw_centermark = draw_centermark;
422 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
424 return vvp->draw_centermark;
427 void vik_viewport_draw_centermark ( VikViewport *vvp )
429 if ( !vvp->draw_centermark )
430 return;
432 const int len = 30;
433 const int gap = 4;
434 int center_x = vvp->width/2;
435 int center_y = vvp->height/2;
436 GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
438 /* white back ground */
439 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
440 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
441 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
442 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
443 /* black fore ground */
444 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
445 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
446 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
447 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
451 void vik_viewport_sync ( VikViewport *vvp )
453 g_return_if_fail ( vvp != NULL );
454 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);
457 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
459 gint x, y, wid, hei;
461 g_return_if_fail ( vvp != NULL );
462 gdk_draw_drawable(GTK_WIDGET(vvp)->window, GTK_WIDGET(vvp)->style->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
464 if (x_off >= 0) {
465 x = 0;
466 wid = x_off;
467 } else {
468 x = vvp->width+x_off;
469 wid = -x_off;
471 if (y_off >= 0) {
472 y = 0;
473 hei = y_off;
474 } else {
475 y = vvp->height+y_off;
476 hei = -y_off;
478 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
479 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
482 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
484 g_return_if_fail ( vvp != NULL );
485 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM )
486 vvp->xmpp = vvp->ympp = xympp;
488 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
489 viewport_utm_zone_check(vvp);
490 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
491 viewport_google_rezoom ( vvp );
494 /* or could do factor */
495 void vik_viewport_zoom_in ( VikViewport *vvp )
497 g_return_if_fail ( vvp != NULL );
498 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
500 vvp->xmpp /= 2;
501 vvp->ympp /= 2;
503 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
504 viewport_google_rezoom ( vvp );
506 viewport_utm_zone_check(vvp);
510 void vik_viewport_zoom_out ( VikViewport *vvp )
512 g_return_if_fail ( vvp != NULL );
513 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
515 vvp->xmpp *= 2;
516 vvp->ympp *= 2;
518 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
519 viewport_google_rezoom ( vvp );
521 viewport_utm_zone_check(vvp);
525 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
527 if ( vvp->xmpp == vvp->ympp )
528 return vvp->xmpp;
529 return 0.0;
532 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
534 return vvp->xmpp;
537 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
539 return vvp->ympp;
542 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
544 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
545 vvp->xmpp = xmpp;
546 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
547 viewport_utm_zone_check(vvp);
548 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
549 viewport_google_rezoom ( vvp );
553 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
555 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
556 vvp->ympp = ympp;
557 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
558 viewport_utm_zone_check(vvp);
559 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
560 viewport_google_rezoom ( vvp );
565 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
567 g_return_val_if_fail ( vvp != NULL, NULL );
568 return &(vvp->center);
571 /* called every time we update coordinates/zoom */
572 static void viewport_utm_zone_check ( VikViewport *vvp )
574 if ( vvp->coord_mode == VIK_COORD_UTM )
576 struct UTM utm;
577 struct LatLon ll;
578 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
579 a_coords_latlon_to_utm ( &ll, &utm );
580 if ( utm.zone != vvp->center.utm_zone )
581 *((struct UTM *)(&vvp->center)) = utm;
583 /* misc. stuff so we don't have to check later */
584 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
585 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
589 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
591 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
592 if ( vvp->coord_mode == VIK_COORD_UTM )
593 viewport_utm_zone_check ( vvp );
596 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
598 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
599 if ( vvp->coord_mode == VIK_COORD_UTM )
600 viewport_utm_zone_check ( vvp );
603 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
605 vvp->center = *coord;
606 if ( vvp->coord_mode == VIK_COORD_UTM )
607 viewport_utm_zone_check ( vvp );
610 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
612 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
614 /* get center, then just offset */
615 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
616 ul->mode = VIK_COORD_UTM;
617 *br = *ul;
619 ul->north_south += (vvp->ympp * vvp->height / 2);
620 ul->east_west -= (vvp->xmpp * vvp->width / 2);
621 br->north_south -= (vvp->ympp * vvp->height / 2);
622 br->east_west += (vvp->xmpp * vvp->width / 2);
625 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
627 if ( vvp->coord_mode == VIK_COORD_UTM ) {
628 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
629 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
630 center->zone = zone;
634 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
636 if ( vvp->coord_mode == VIK_COORD_UTM ) {
637 VikCoord coord;
638 g_assert ( vvp != NULL );
639 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
640 return coord.utm_zone;
642 return '\0';
645 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
647 if ( vvp->coord_mode == VIK_COORD_UTM ) {
648 VikCoord coord;
649 g_assert ( vvp != NULL );
650 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
651 return coord.utm_zone;
653 return '\0';
657 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
659 g_return_if_fail ( vvp != NULL );
660 if ( vvp->coord_mode == VIK_COORD_UTM ) {
661 /* slightly optimized */
662 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
663 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
664 viewport_utm_zone_check ( vvp );
665 } else {
666 VikCoord tmp;
667 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
668 vik_viewport_set_center_coord ( vvp, &tmp );
672 gint vik_viewport_get_width( VikViewport *vvp )
674 g_return_val_if_fail ( vvp != NULL, 0 );
675 return vvp->width;
678 gint vik_viewport_get_height( VikViewport *vvp )
680 g_return_val_if_fail ( vvp != NULL, 0 );
681 return vvp->height;
684 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
686 if ( vvp->coord_mode == VIK_COORD_UTM ) {
687 int zone_delta;
688 struct UTM *utm = (struct UTM *) coord;
689 coord->mode = VIK_COORD_UTM;
691 g_return_if_fail ( vvp != NULL );
693 utm->zone = vvp->center.utm_zone;
694 utm->letter = vvp->center.utm_letter;
695 utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west;
696 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
697 utm->zone += zone_delta;
698 utm->easting -= zone_delta * vvp->utm_zone_width;
699 utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south;
700 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
701 coord->mode = VIK_COORD_LATLON;
702 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
703 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);
704 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) {
705 /* google */
706 coord->east_west = (x - (vvp->width/2)) * vvp->google_calcx_rev_fact + vvp->center.east_west;
707 coord->north_south = ((vvp->height/2) - y) * vvp->google_calcy_rev_fact + vvp->center.north_south;
708 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) {
709 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
710 coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y));
711 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
712 /* FIXMERCATOR */
713 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2));
714 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) );
716 #if 0
717 --> THIS IS JUNK HERE.
718 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
720 (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat);
721 DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll
722 #endif
727 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
729 static VikCoord tmp;
730 g_return_if_fail ( vvp != NULL );
732 if ( coord->mode != vvp->coord_mode )
734 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
735 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
736 coord = &tmp;
739 if ( vvp->coord_mode == VIK_COORD_UTM ) {
740 struct UTM *center = (struct UTM *) &(vvp->center);
741 struct UTM *utm = (struct UTM *) coord;
742 if ( center->zone != utm->zone && vvp->one_utm_zone )
744 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
745 return;
748 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) -
749 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
750 *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp );
751 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
752 struct LatLon *center = (struct LatLon *) &(vvp->center);
753 struct LatLon *ll = (struct LatLon *) coord;
754 double xx,yy;
755 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
756 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 );
757 *x = xx; *y = yy;
758 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) {
759 /* google */
760 *x = vvp->google_calcx_fact * (ll->lon - center->lon) + (vvp->width/2);
761 *y = vvp->google_calcy_fact * (center->lat - ll->lat) + (vvp->height/2);
762 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) {
763 /* subtract, convert to KH coords; blow it up by 256 */
764 *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
765 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0;
766 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
767 /* FIXMERCATOR: Optimize */
768 *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0;
769 *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0;
774 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
776 if ( *x1 > 20000 || *x1 < -20000 ) {
777 gdouble shrinkfactor = ABS(20000.0 / *x1);
778 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
779 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
780 } else if ( *y1 > 20000 || *y1 < -20000 ) {
781 gdouble shrinkfactor = ABS(20000.0 / *x1);
782 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
783 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
784 } else if ( *x2 > 20000 || *x2 < -20000 ) {
785 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
786 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
787 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
788 g_print("%f, %d, %d\n", shrinkfactor, *x2, *y2);
789 } else if ( *y2 > 20000 || *y2 < -20000 ) {
790 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
791 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
792 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
796 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
798 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
799 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
800 /*** clipping, yeah! ***/
801 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
802 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
806 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
808 if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 )
809 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
812 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
814 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
815 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
818 /* shouldn't use this -- slow -- change the alpha channel instead. */
819 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
820 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
822 gint real_dest_x = MAX(dest_x,0);
823 gint real_dest_y = MAX(dest_y,0);
825 if ( alpha == 0 )
826 return; /* don't waste your time */
828 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
830 if ( vvp->alpha_pixbuf )
831 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
832 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
833 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
834 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
837 w = MIN(w,vvp->width - dest_x);
838 h = MIN(h,vvp->height - dest_y);
840 /* check that we are drawing within boundaries. */
841 src_x += (real_dest_x - dest_x);
842 src_y += (real_dest_y - dest_y);
843 w -= (real_dest_x - dest_x);
844 h -= (real_dest_y - dest_y);
846 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
847 real_dest_x, real_dest_y, 0, 0, w, h );
849 /* do a composite */
850 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
852 /* draw pixbuf_tmp */
853 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
856 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
857 gint dest_x, gint dest_y, gint w, gint h )
859 gdk_draw_pixbuf ( vvp->scr_buffer,
860 // GTK_WIDGET(vvp)->style->black_gc,
861 NULL,
862 pixbuf,
863 src_x, src_y, dest_x, dest_y, w, h,
864 GDK_RGB_DITHER_NONE, 0, 0 );
867 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
869 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
873 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
875 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
878 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
880 g_assert ( vvp );
881 return vvp->coord_mode;
884 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
886 g_return_if_fail ( vvp != NULL );
887 vvp->coord_mode = mode;
888 vik_coord_convert ( &(vvp->center), mode );
891 /* Thanks GPSDrive */
892 static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
894 int px, py;
895 gdouble dif, lat, lon;
896 double Ra = Radius[90+(gint)zero_lat];
898 px = (mapSizeX2 - x) * pixelfact_x;
899 py = (-mapSizeY2 + y) * pixelfact_y;
901 lat = zero_lat - py / Ra;
902 lat = zero_lat - py / Ra;
903 lon =
904 zero_long -
905 px / (Ra *
906 cos (lat * DEG2RAD));
908 dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD)));
909 lat = lat - dif / 1.5;
910 lon =
911 zero_long -
912 px / (Ra *
913 cos (lat * DEG2RAD));
915 *lt = lat;
916 *lg = lon;
917 return (TRUE);
920 /* Thanks GPSDrive */
921 static gboolean calcxy(double *x, double *y, double lg, double lt, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
923 double dif;
924 double Ra;
925 gint mapSizeX = 2 * mapSizeX2;
926 gint mapSizeY = 2 * mapSizeY2;
928 g_assert ( lt >= -90.0 && lt <= 90.0 );
929 // lg *= rad2deg; // FIXME, optimize equations
930 // lt *= rad2deg;
931 Ra = Radius[90+(gint)lt];
932 *x = Ra *
933 cos (lt*DEG2RAD) * (lg - zero_long);
934 *y = Ra * (lt - zero_lat);
935 dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long)))));
936 *y = *y + dif / 1.85;
937 *x = *x / pixelfact_x;
938 *y = *y / pixelfact_y;
939 *x = mapSizeX2 - *x;
940 *y += mapSizeY2;
941 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
942 return (FALSE);
943 return (TRUE);
946 static void viewport_init_ra()
948 static gboolean done_before = FALSE;
949 if ( !done_before )
951 gint i;
952 for ( i = -90; i <= 90; i++)
953 Radius[i+90] = calcR ( (double)i ) * DEG2RAD;
954 done_before = TRUE;
958 double calcR (double lat)
960 double a = 6378.137, r, sc, x, y, z;
961 double e2 = 0.081082 * 0.081082;
963 * the radius of curvature of an ellipsoidal Earth in the plane of the
964 * meridian is given by
966 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
969 * where a is the equatorial radius, b is the polar radius, and e is
970 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
972 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
973 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
974 * = 0.081082 Eccentricity
977 lat = lat * DEG2RAD;
978 sc = sin (lat);
979 x = a * (1.0 - e2);
980 z = 1.0 - e2 * sc * sc;
981 y = pow (z, 1.5);
982 r = x / y;
983 r = r * 1000.0;
984 return r;
987 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
989 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
992 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
994 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
995 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
998 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1000 static GdkGCValues values;
1001 gdk_gc_get_values ( gc, &values );
1002 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1005 GdkFunction vik_gc_get_function ( GdkGC *gc )
1007 static GdkGCValues values;
1008 gdk_gc_get_values ( gc, &values );
1009 return values.function;
1012 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1014 vvp->drawmode = drawmode;
1015 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1016 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1017 else {
1018 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1019 if ( drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE )
1020 viewport_google_rezoom ( vvp );
1024 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1026 return vvp->drawmode;
1029 static void viewport_google_rezoom ( VikViewport *vvp )
1031 vvp->google_calcx_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 * 0.7716245833877 / vvp->xmpp);
1032 vvp->google_calcy_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 / vvp->ympp);
1033 vvp->google_calcx_rev_fact = 1 / vvp->google_calcx_fact;
1034 vvp->google_calcy_rev_fact = 1 / vvp->google_calcy_fact;
1037 /******** triggering *******/
1038 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1040 vp->trigger = trigger;
1043 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1045 return vp->trigger;
1048 void vik_viewport_snapshot_save ( VikViewport *vp )
1050 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1053 void vik_viewport_snapshot_load ( VikViewport *vp )
1055 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1058 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1060 vp->half_drawn = half_drawn;
1063 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1065 return vp->half_drawn;
1069 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1071 const gchar *name = NULL;
1072 VikWindow *vw = NULL;
1073 GtkWidget *mode_button;
1074 GtkWidget *label;
1076 vw = VIK_WINDOW_FROM_WIDGET(vv);
1077 mode_button = vik_window_get_drawmode_button(vw, mode);
1078 label = gtk_bin_get_child(GTK_BIN(mode_button));
1080 name = gtk_label_get_text ( GTK_LABEL(label) );
1082 return name;
1086 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1088 VikCoord tleft, tright, bleft, bright;
1090 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1091 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1092 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1093 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1095 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1096 vik_coord_convert(&tright, VIK_COORD_LATLON);
1097 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1098 vik_coord_convert(&bright, VIK_COORD_LATLON);
1100 *max_lat = MAX(tleft.north_south, tright.north_south);
1101 *min_lat = MIN(bleft.north_south, bright.north_south);
1102 *max_lon = MAX(tright.east_west, bright.east_west);
1103 *min_lon = MIN(tleft.east_west, bleft.east_west);