2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2007, 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
30 #define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
31 #define DEFAULT_HIGHLIGHT_COLOR "#EEA500"
32 /* Default highlight in orange */
44 #include "vikwindow.h"
45 #include "vikviewport.h"
52 #define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 )
54 static gdouble EASTING_OFFSET = 500000.0;
58 static void viewport_finalize ( GObject *gob );
59 static void viewport_utm_zone_check ( VikViewport *vvp );
61 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 );
62 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 );
63 double calcR (double lat);
65 static double Radius[181];
66 static void viewport_init_ra();
68 static GObjectClass *parent_class;
72 GtkDrawingArea drawing_area;
73 GdkPixmap *scr_buffer;
75 gint width_2, height_2; // Half of the normal width and height
77 VikCoordMode coord_mode;
79 gdouble xmfactor, ymfactor;
81 GdkPixbuf *alpha_pixbuf;
82 guint8 alpha_pixbuf_width;
83 guint8 alpha_pixbuf_height;
85 gdouble utm_zone_width;
86 gboolean one_utm_zone;
89 GdkColor background_color;
95 /* Wether or not display OSD info */
97 gboolean draw_centermark;
98 gboolean draw_highlight;
100 GdkColor highlight_color;
102 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
103 VikViewportDrawMode drawmode;
107 GdkPixmap *snapshot_buffer;
112 viewport_utm_zone_width ( VikViewport *vvp )
114 if ( vvp->coord_mode == VIK_COORD_UTM ) {
117 /* get latitude of screen bottom */
118 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
119 utm.northing -= vvp -> height * vvp -> ympp / 2;
120 a_coords_utm_to_latlon ( &utm, &ll );
123 ll.lon = (utm.zone - 1) * 6 - 180 ;
124 a_coords_latlon_to_utm ( &ll, &utm);
125 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
130 G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA)
133 vik_viewport_class_init ( VikViewportClass *klass )
136 GObjectClass *object_class;
138 object_class = G_OBJECT_CLASS (klass);
140 object_class->finalize = viewport_finalize;
142 parent_class = g_type_class_peek_parent (klass);
145 VikViewport *vik_viewport_new ()
147 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
152 vik_viewport_init ( VikViewport *vvp )
158 ll.lat = a_vik_get_default_lat();
159 ll.lon = a_vik_get_default_long();
160 a_coords_latlon_to_utm ( &ll, &utm );
162 /* TODO: not static */
165 vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp);
166 vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp);
167 vvp->coord_mode = VIK_COORD_LATLON;
168 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
169 vvp->center.mode = VIK_COORD_LATLON;
170 vvp->center.north_south = ll.lat;
171 vvp->center.east_west = ll.lon;
172 vvp->center.utm_zone = (int)utm.zone;
173 vvp->center.utm_letter = utm.letter;
174 vvp->scr_buffer = NULL;
175 vvp->alpha_pixbuf = NULL;
176 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
177 vvp->utm_zone_width = 0.0;
178 vvp->background_gc = NULL;
179 vvp->highlight_gc = NULL;
180 vvp->scale_bg_gc = NULL;
182 vvp->copyrights = NULL;
184 vvp->draw_scale = TRUE;
185 vvp->draw_centermark = TRUE;
186 vvp->draw_highlight = TRUE;
189 vvp->snapshot_buffer = NULL;
190 vvp->half_drawn = FALSE;
192 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
194 #if GTK_CHECK_VERSION (2,18,0)
195 gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE );
197 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
201 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
203 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
204 *rv = vvp->background_color;
208 /* returns pointer to internal static storage, changes next time function called, use quickly */
209 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
211 static gchar color[8];
212 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));
216 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
218 g_assert ( vvp && vvp->background_gc );
219 if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
220 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
222 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
225 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
227 g_assert ( vvp && vvp->background_gc );
228 vvp->background_color = *color;
229 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
232 GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp )
234 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
235 *rv = vvp->highlight_color;
239 /* returns pointer to internal static storage, changes next time function called, use quickly */
240 const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp )
242 static gchar color[8];
243 g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->highlight_color.red/256),(int)(vvp->highlight_color.green/256),(int)(vvp->highlight_color.blue/256));
247 void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname )
249 g_assert ( vvp->highlight_gc );
250 gdk_color_parse ( colorname, &(vvp->highlight_color) );
251 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) );
254 void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color )
256 g_assert ( vvp->highlight_gc );
257 vvp->highlight_color = *color;
258 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color );
261 GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp )
263 return vvp->highlight_gc;
266 void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness )
268 // Otherwise same GDK_* attributes as in vik_viewport_new_gc
269 gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
272 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
277 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
278 if ( gdk_color_parse ( colorname, &color ) )
279 gdk_gc_set_rgb_fg_color ( rv, &color );
281 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
282 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
286 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
290 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
291 gdk_gc_set_rgb_fg_color ( rv, color );
292 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
296 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
299 vvp->height = height;
301 vvp->width_2 = vvp->width/2;
302 vvp->height_2 = vvp->height/2;
304 if ( vvp->scr_buffer )
305 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
306 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
308 /* TODO trigger: only if this is enabled !!! */
309 if ( vvp->snapshot_buffer )
310 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
311 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
315 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
317 return vvp->scr_buffer;
320 gboolean vik_viewport_configure ( VikViewport *vvp )
322 g_return_val_if_fail ( vvp != NULL, TRUE );
324 vvp->width = GTK_WIDGET(vvp)->allocation.width;
325 vvp->height = GTK_WIDGET(vvp)->allocation.height;
327 vvp->width_2 = vvp->width/2;
328 vvp->height_2 = vvp->height/2;
330 if ( vvp->scr_buffer )
331 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
333 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
335 /* TODO trigger: only if enabled! */
336 if ( vvp->snapshot_buffer )
337 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
339 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
342 /* this is down here so it can get a GC (necessary?) */
343 if ( !vvp->background_gc )
345 vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
346 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
348 if ( ! vvp->highlight_gc )
350 vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 );
351 vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR );
353 if ( !vvp->scale_bg_gc) {
354 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
360 static void viewport_finalize ( GObject *gob )
362 VikViewport *vvp = VIK_VIEWPORT(gob);
364 g_return_if_fail ( vvp != NULL );
366 if ( vvp->scr_buffer )
367 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
369 if ( vvp->snapshot_buffer )
370 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
372 if ( vvp->alpha_pixbuf )
373 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
375 if ( vvp->background_gc )
376 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
378 if ( vvp->highlight_gc )
379 g_object_unref ( G_OBJECT ( vvp->highlight_gc ) );
381 if ( vvp->scale_bg_gc ) {
382 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
383 vvp->scale_bg_gc = NULL;
386 G_OBJECT_CLASS(parent_class)->finalize(gob);
390 * vik_viewport_clear:
393 * Clear the whole viewport.
395 void vik_viewport_clear ( VikViewport *vvp )
397 g_return_if_fail ( vvp != NULL );
398 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
399 vik_viewport_reset_copyrights ( vvp );
400 vik_viewport_reset_logos ( vvp );
404 * vik_viewport_set_draw_scale:
406 * @draw_scale: new value
408 * Enable/Disable display of scale.
410 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
412 vvp->draw_scale = draw_scale;
415 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
417 return vvp->draw_scale;
420 void vik_viewport_draw_scale ( VikViewport *vvp )
422 g_return_if_fail ( vvp != NULL );
424 if ( vvp->draw_scale ) {
425 VikCoord left, right;
426 gdouble unit, base, diff, old_unit, old_diff, ratio;
427 gint odd, len, SCSIZE = 5, HEIGHT=10;
431 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
432 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
434 vik_units_distance_t dist_units = a_vik_get_units_distance ();
435 switch (dist_units) {
436 case VIK_UNITS_DISTANCE_KILOMETRES:
437 base = vik_coord_diff ( &left, &right ); // in meters
439 case VIK_UNITS_DISTANCE_MILES:
440 // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
441 base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
444 base = 1; // Keep the compiler happy
445 g_critical("Houston, we've had a problem. distance=%d", dist_units);
447 ratio = (vvp->width/SCSIZE)/base;
450 diff = fabs(base-unit);
454 while (diff <= old_diff) {
457 unit = unit * (odd%2 ? 5 : 2);
458 diff = fabs(base-unit);
464 /* white background */
465 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
466 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
467 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
468 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
469 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
470 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
472 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
473 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
474 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
475 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
476 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
477 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
480 for (i=1; i<5; i++) {
481 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
482 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
483 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
484 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
488 for (i=1; i<10; i++) {
489 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
490 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
491 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
492 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
495 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
496 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
498 switch (dist_units) {
499 case VIK_UNITS_DISTANCE_KILOMETRES:
501 sprintf(s, "%d km", (int)unit/1000);
503 sprintf(s, "%d m", (int)unit);
506 case VIK_UNITS_DISTANCE_MILES:
507 // Handle units in 0.1 miles
509 sprintf(s, "%0.1f miles", unit/10.0);
511 else if ((int)unit == 10.0) {
512 sprintf(s, "1 mile");
515 sprintf(s, "%d miles", (int)(unit/10.0));
519 g_critical("Houston, we've had a problem. distance=%d", dist_units);
521 pango_layout_set_text(pl, s, -1);
522 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
523 PAD + len + PAD, vvp->height - PAD - 10, pl);
529 void vik_viewport_draw_copyright ( VikViewport *vvp )
531 g_return_if_fail ( vvp != NULL );
534 PangoRectangle ink_rect, logical_rect;
537 /* compute copyrights string */
538 guint len = g_slist_length ( vvp->copyrights );
541 for (i = 0 ; i < len ; i++)
543 // Stop when buffer is full
544 int slen = strlen ( s );
548 gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
550 // Only use part of this copyright that fits in the available space left
551 // remembering 1 character is left available for the appended space
552 int clen = strlen ( copyright );
553 if ( slen + clen > 126 ) {
557 strncat ( s, copyright, clen );
561 /* create pango layout */
562 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
563 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
564 pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
567 pango_layout_set_text(pl, s, -1);
569 /* Use maximum of half the viewport width */
570 pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
571 pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
572 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
573 vvp->width / 2, vvp->height - logical_rect.height, pl);
581 * vik_viewport_set_draw_centermark:
583 * @draw_centermark: new value
585 * Enable/Disable display of center mark.
587 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
589 vvp->draw_centermark = draw_centermark;
592 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
594 return vvp->draw_centermark;
597 void vik_viewport_draw_centermark ( VikViewport *vvp )
599 g_return_if_fail ( vvp != NULL );
601 if ( !vvp->draw_centermark )
606 int center_x = vvp->width/2;
607 int center_y = vvp->height/2;
608 GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
610 /* white back ground */
611 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
612 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
613 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
614 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
615 /* black fore ground */
616 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
617 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
618 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
619 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
623 void vik_viewport_draw_logo ( VikViewport *vvp )
625 g_return_if_fail ( vvp != NULL );
627 guint len = g_slist_length ( vvp->logos );
628 gint x = vvp->width - PAD;
631 for (i = 0 ; i < len ; i++)
633 GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
634 gint width = gdk_pixbuf_get_width ( logo );
635 gint height = gdk_pixbuf_get_height ( logo );
636 vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
641 void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
643 vvp->draw_highlight = draw_highlight;
646 gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
648 return vvp->draw_highlight;
651 void vik_viewport_sync ( VikViewport *vvp )
653 g_return_if_fail ( vvp != NULL );
654 gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height);
657 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
661 g_return_if_fail ( vvp != NULL );
662 gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height);
668 x = vvp->width+x_off;
675 y = vvp->height+y_off;
678 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
679 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
682 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
684 g_return_if_fail ( vvp != NULL );
685 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) {
686 vvp->xmpp = vvp->ympp = xympp;
687 // Since xmpp & ympp are the same it doesn't matter which one is used here
688 vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp);
691 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
692 viewport_utm_zone_check(vvp);
695 /* or could do factor */
696 void vik_viewport_zoom_in ( VikViewport *vvp )
698 g_return_if_fail ( vvp != NULL );
699 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
704 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
705 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
707 viewport_utm_zone_check(vvp);
711 void vik_viewport_zoom_out ( VikViewport *vvp )
713 g_return_if_fail ( vvp != NULL );
714 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
719 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
720 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
722 viewport_utm_zone_check(vvp);
726 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
728 if ( vvp->xmpp == vvp->ympp )
733 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
738 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
743 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
745 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
747 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
748 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
749 viewport_utm_zone_check(vvp);
753 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
755 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
757 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
758 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
759 viewport_utm_zone_check(vvp);
764 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
766 g_return_val_if_fail ( vvp != NULL, NULL );
767 return &(vvp->center);
770 /* called every time we update coordinates/zoom */
771 static void viewport_utm_zone_check ( VikViewport *vvp )
773 if ( vvp->coord_mode == VIK_COORD_UTM )
777 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
778 a_coords_latlon_to_utm ( &ll, &utm );
779 if ( utm.zone != vvp->center.utm_zone )
780 *((struct UTM *)(&vvp->center)) = utm;
782 /* misc. stuff so we don't have to check later */
783 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
784 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
788 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
790 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
791 if ( vvp->coord_mode == VIK_COORD_UTM )
792 viewport_utm_zone_check ( vvp );
795 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
797 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
798 if ( vvp->coord_mode == VIK_COORD_UTM )
799 viewport_utm_zone_check ( vvp );
802 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
804 vvp->center = *coord;
805 if ( vvp->coord_mode == VIK_COORD_UTM )
806 viewport_utm_zone_check ( vvp );
809 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
811 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
813 /* get center, then just offset */
814 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
815 ul->mode = VIK_COORD_UTM;
818 ul->north_south += (vvp->ympp * vvp->height / 2);
819 ul->east_west -= (vvp->xmpp * vvp->width / 2);
820 br->north_south -= (vvp->ympp * vvp->height / 2);
821 br->east_west += (vvp->xmpp * vvp->width / 2);
824 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
826 if ( vvp->coord_mode == VIK_COORD_UTM ) {
827 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
828 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
833 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
835 if ( vvp->coord_mode == VIK_COORD_UTM ) {
837 g_assert ( vvp != NULL );
838 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
839 return coord.utm_zone;
844 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
846 if ( vvp->coord_mode == VIK_COORD_UTM ) {
848 g_assert ( vvp != NULL );
849 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
850 return coord.utm_zone;
856 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
858 g_return_if_fail ( vvp != NULL );
859 if ( vvp->coord_mode == VIK_COORD_UTM ) {
860 /* slightly optimized */
861 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
862 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
863 viewport_utm_zone_check ( vvp );
866 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
867 vik_viewport_set_center_coord ( vvp, &tmp );
871 gint vik_viewport_get_width( VikViewport *vvp )
873 g_return_val_if_fail ( vvp != NULL, 0 );
877 gint vik_viewport_get_height( VikViewport *vvp )
879 g_return_val_if_fail ( vvp != NULL, 0 );
883 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
885 g_return_if_fail ( vvp != NULL );
887 if ( vvp->coord_mode == VIK_COORD_UTM ) {
889 struct UTM *utm = (struct UTM *) coord;
890 coord->mode = VIK_COORD_UTM;
892 utm->zone = vvp->center.utm_zone;
893 utm->letter = vvp->center.utm_letter;
894 utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west;
895 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
896 utm->zone += zone_delta;
897 utm->easting -= zone_delta * vvp->utm_zone_width;
898 utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south;
899 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
900 coord->mode = VIK_COORD_LATLON;
901 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
902 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
903 coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y));
904 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
905 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);
906 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
907 /* This isn't called with a high frequently so less need to optimize */
908 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
909 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) );
915 * Since this function is used for every drawn trackpoint - it can get called alot
916 * Thus x & y position factors are calculated once on zoom changes,
917 * avoiding the need to do it here all the time.
918 * For good measure the half width and height values are also pre calculated too.
920 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
923 g_return_if_fail ( vvp != NULL );
925 if ( coord->mode != vvp->coord_mode )
927 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
928 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
932 if ( vvp->coord_mode == VIK_COORD_UTM ) {
933 struct UTM *center = (struct UTM *) &(vvp->center);
934 struct UTM *utm = (struct UTM *) coord;
935 if ( center->zone != utm->zone && vvp->one_utm_zone )
937 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
941 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) -
942 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
943 *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp );
944 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
945 struct LatLon *center = (struct LatLon *) &(vvp->center);
946 struct LatLon *ll = (struct LatLon *) coord;
948 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
949 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
950 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) );
951 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
952 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 );
954 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
955 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
956 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) );
961 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
963 if ( *x1 > 20000 || *x1 < -20000 ) {
964 gdouble shrinkfactor = ABS(20000.0 / *x1);
965 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
966 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
967 } else if ( *y1 > 20000 || *y1 < -20000 ) {
968 gdouble shrinkfactor = ABS(20000.0 / *x1);
969 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
970 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
971 } else if ( *x2 > 20000 || *x2 < -20000 ) {
972 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
973 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
974 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
975 } else if ( *y2 > 20000 || *y2 < -20000 ) {
976 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
977 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
978 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
982 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
984 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
985 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
986 /*** clipping, yeah! ***/
987 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
988 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
992 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
994 // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
995 if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 )
996 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
999 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
1001 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
1002 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
1005 /* shouldn't use this -- slow -- change the alpha channel instead. */
1006 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
1007 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
1009 gint real_dest_x = MAX(dest_x,0);
1010 gint real_dest_y = MAX(dest_y,0);
1013 return; /* don't waste your time */
1015 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
1017 if ( vvp->alpha_pixbuf )
1018 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
1019 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
1020 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
1021 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
1024 w = MIN(w,vvp->width - dest_x);
1025 h = MIN(h,vvp->height - dest_y);
1027 /* check that we are drawing within boundaries. */
1028 src_x += (real_dest_x - dest_x);
1029 src_y += (real_dest_y - dest_y);
1030 w -= (real_dest_x - dest_x);
1031 h -= (real_dest_y - dest_y);
1033 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
1034 real_dest_x, real_dest_y, 0, 0, w, h );
1036 /* do a composite */
1037 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
1039 /* draw pixbuf_tmp */
1040 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
1043 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
1044 gint dest_x, gint dest_y, gint w, gint h )
1046 gdk_draw_pixbuf ( vvp->scr_buffer,
1049 src_x, src_y, dest_x, dest_y, w, h,
1050 GDK_RGB_DITHER_NONE, 0, 0 );
1053 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
1055 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
1059 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
1061 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
1064 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
1067 return vvp->coord_mode;
1070 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
1072 g_return_if_fail ( vvp != NULL );
1073 vvp->coord_mode = mode;
1074 vik_coord_convert ( &(vvp->center), mode );
1077 /* Thanks GPSDrive */
1078 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 )
1081 gdouble dif, lat, lon;
1082 double Ra = Radius[90+(gint)zero_lat];
1084 px = (mapSizeX2 - x) * pixelfact_x;
1085 py = (-mapSizeY2 + y) * pixelfact_y;
1087 lat = zero_lat - py / Ra;
1088 lat = zero_lat - py / Ra;
1092 cos (DEG2RAD(lat)));
1094 dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
1095 lat = lat - dif / 1.5;
1099 cos (DEG2RAD(lat)));
1106 /* Thanks GPSDrive */
1107 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 )
1111 gint mapSizeX = 2 * mapSizeX2;
1112 gint mapSizeY = 2 * mapSizeY2;
1114 g_assert ( lt >= -90.0 && lt <= 90.0 );
1115 // lg *= rad2deg; // FIXME, optimize equations
1117 Ra = Radius[90+(gint)lt];
1119 cos (DEG2RAD(lt)) * (lg - zero_long);
1120 *y = Ra * (lt - zero_lat);
1121 dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
1122 *y = *y + dif / 1.85;
1123 *x = *x / pixelfact_x;
1124 *y = *y / pixelfact_y;
1125 *x = mapSizeX2 - *x;
1127 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
1132 static void viewport_init_ra()
1134 static gboolean done_before = FALSE;
1138 for ( i = -90; i <= 90; i++)
1139 Radius[i+90] = calcR ( DEG2RAD((double)i) );
1144 double calcR (double lat)
1146 double a = 6378.137, r, sc, x, y, z;
1147 double e2 = 0.081082 * 0.081082;
1149 * the radius of curvature of an ellipsoidal Earth in the plane of the
1150 * meridian is given by
1152 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
1155 * where a is the equatorial radius, b is the polar radius, and e is
1156 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
1158 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
1159 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
1160 * = 0.081082 Eccentricity
1166 z = 1.0 - e2 * sc * sc;
1173 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1175 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1178 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1180 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1181 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1184 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1186 static GdkGCValues values;
1187 gdk_gc_get_values ( gc, &values );
1188 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1191 GdkFunction vik_gc_get_function ( GdkGC *gc )
1193 static GdkGCValues values;
1194 gdk_gc_get_values ( gc, &values );
1195 return values.function;
1198 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1200 vvp->drawmode = drawmode;
1201 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1202 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1204 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1208 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1210 return vvp->drawmode;
1213 /******** triggering *******/
1214 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1216 vp->trigger = trigger;
1219 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1224 void vik_viewport_snapshot_save ( VikViewport *vp )
1226 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1229 void vik_viewport_snapshot_load ( VikViewport *vp )
1231 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1234 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1236 vp->half_drawn = half_drawn;
1239 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1241 return vp->half_drawn;
1245 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1247 const gchar *name = NULL;
1248 VikWindow *vw = NULL;
1249 GtkWidget *mode_button;
1252 vw = VIK_WINDOW_FROM_WIDGET(vv);
1253 mode_button = vik_window_get_drawmode_button(vw, mode);
1254 label = gtk_bin_get_child(GTK_BIN(mode_button));
1256 name = gtk_label_get_text ( GTK_LABEL(label) );
1262 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1264 VikCoord tleft, tright, bleft, bright;
1266 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1267 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1268 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1269 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1271 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1272 vik_coord_convert(&tright, VIK_COORD_LATLON);
1273 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1274 vik_coord_convert(&bright, VIK_COORD_LATLON);
1276 *max_lat = MAX(tleft.north_south, tright.north_south);
1277 *min_lat = MIN(bleft.north_south, bright.north_south);
1278 *max_lon = MAX(tright.east_west, bright.east_west);
1279 *min_lon = MIN(tleft.east_west, bleft.east_west);
1282 void vik_viewport_reset_copyrights ( VikViewport *vp )
1284 g_return_if_fail ( vp != NULL );
1285 g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
1286 g_slist_free ( vp->copyrights );
1287 vp->copyrights = NULL;
1291 * vik_viewport_add_copyright:
1293 * @copyright: new copyright to display
1295 * Add a copyright to display on viewport.
1297 void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright )
1299 g_return_if_fail ( vp != NULL );
1302 GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
1303 if ( found == NULL )
1305 gchar *duple = g_strdup ( copyright );
1306 vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
1311 void vik_viewport_reset_logos ( VikViewport *vp )
1313 g_return_if_fail ( vp != NULL );
1314 /* do not free elem */
1315 g_slist_free ( vp->logos );
1319 void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
1321 g_return_if_fail ( vp != NULL );
1324 GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
1325 if ( found == NULL )
1327 vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
1333 * vik_viewport_compute_bearing:
1339 * @angle: bearing in Radian (output)
1340 * @baseangle: UTM base angle in Radian (output)
1344 void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
1346 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
1347 gdouble dx = (x2-x1)/len*10;
1348 gdouble dy = (y2-y1)/len*10;
1350 *angle = atan2(dy, dx) + M_PI_2;
1352 if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
1358 vik_viewport_screen_to_coord ( vp, x1, y1, &test );
1359 vik_coord_to_latlon ( &test, &ll );
1360 ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude
1361 a_coords_latlon_to_utm ( &ll, &u );
1362 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
1363 vik_viewport_coord_to_screen ( vp, &test, &tx, &ty );
1365 *baseangle = M_PI - atan2(tx-x1, ty-y1);
1366 *angle -= *baseangle;
1371 if (*angle > 2*M_PI)