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 ) );
151 #define VIK_SETTINGS_VIEW_LAST_LATITUDE "viewport_last_latitude"
152 #define VIK_SETTINGS_VIEW_LAST_LONGITUDE "viewport_last_longitude"
153 #define VIK_SETTINGS_VIEW_LAST_ZOOM_X "viewport_last_zoom_xpp"
154 #define VIK_SETTINGS_VIEW_LAST_ZOOM_Y "viewport_last_zoom_ypp"
157 vik_viewport_init ( VikViewport *vvp )
163 ll.lat = a_vik_get_default_lat();
164 ll.lon = a_vik_get_default_long();
165 gdouble zoom_x = 4.0;
166 gdouble zoom_y = 4.0;
168 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
169 gdouble lat, lon, dzoom;
170 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, &lat ) )
172 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, &lon ) )
174 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, &dzoom ) )
176 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, &dzoom ) )
180 a_coords_latlon_to_utm ( &ll, &utm );
184 vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp);
185 vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp);
186 vvp->coord_mode = VIK_COORD_LATLON;
187 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
188 vvp->center.mode = VIK_COORD_LATLON;
189 vvp->center.north_south = ll.lat;
190 vvp->center.east_west = ll.lon;
191 vvp->center.utm_zone = (int)utm.zone;
192 vvp->center.utm_letter = utm.letter;
193 vvp->scr_buffer = NULL;
194 vvp->alpha_pixbuf = NULL;
195 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
196 vvp->utm_zone_width = 0.0;
197 vvp->background_gc = NULL;
198 vvp->highlight_gc = NULL;
199 vvp->scale_bg_gc = NULL;
201 vvp->copyrights = NULL;
203 vvp->draw_scale = TRUE;
204 vvp->draw_centermark = TRUE;
205 vvp->draw_highlight = TRUE;
208 vvp->snapshot_buffer = NULL;
209 vvp->half_drawn = FALSE;
211 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
213 #if GTK_CHECK_VERSION (2,18,0)
214 gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE );
216 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
220 GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
222 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
223 *rv = vvp->background_color;
227 /* returns pointer to internal static storage, changes next time function called, use quickly */
228 const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
230 static gchar color[8];
231 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));
235 void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
237 g_assert ( vvp && vvp->background_gc );
238 if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
239 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
241 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
244 void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
246 g_assert ( vvp && vvp->background_gc );
247 vvp->background_color = *color;
248 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
251 GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp )
253 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
254 *rv = vvp->highlight_color;
258 /* returns pointer to internal static storage, changes next time function called, use quickly */
259 const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp )
261 static gchar color[8];
262 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));
266 void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname )
268 g_assert ( vvp->highlight_gc );
269 gdk_color_parse ( colorname, &(vvp->highlight_color) );
270 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) );
273 void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color )
275 g_assert ( vvp->highlight_gc );
276 vvp->highlight_color = *color;
277 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color );
280 GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp )
282 return vvp->highlight_gc;
285 void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness )
287 // Otherwise same GDK_* attributes as in vik_viewport_new_gc
288 gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
291 GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
296 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
297 if ( gdk_color_parse ( colorname, &color ) )
298 gdk_gc_set_rgb_fg_color ( rv, &color );
300 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
301 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
305 GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
309 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
310 gdk_gc_set_rgb_fg_color ( rv, color );
311 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
315 void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
318 vvp->height = height;
320 vvp->width_2 = vvp->width/2;
321 vvp->height_2 = vvp->height/2;
323 if ( vvp->scr_buffer )
324 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
325 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
327 /* TODO trigger: only if this is enabled !!! */
328 if ( vvp->snapshot_buffer )
329 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
330 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
334 GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
336 return vvp->scr_buffer;
339 gboolean vik_viewport_configure ( VikViewport *vvp )
341 g_return_val_if_fail ( vvp != NULL, TRUE );
343 vvp->width = GTK_WIDGET(vvp)->allocation.width;
344 vvp->height = GTK_WIDGET(vvp)->allocation.height;
346 vvp->width_2 = vvp->width/2;
347 vvp->height_2 = vvp->height/2;
349 if ( vvp->scr_buffer )
350 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
352 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
354 /* TODO trigger: only if enabled! */
355 if ( vvp->snapshot_buffer )
356 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
358 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
361 /* this is down here so it can get a GC (necessary?) */
362 if ( !vvp->background_gc )
364 vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
365 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
367 if ( ! vvp->highlight_gc )
369 vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 );
370 vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR );
372 if ( !vvp->scale_bg_gc) {
373 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
379 static void viewport_finalize ( GObject *gob )
381 VikViewport *vvp = VIK_VIEWPORT(gob);
383 g_return_if_fail ( vvp != NULL );
385 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
387 vik_coord_to_latlon ( &(vvp->center), &ll );
388 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, ll.lat );
389 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, ll.lon );
390 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, vvp->xmpp );
391 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, vvp->ympp );
394 if ( vvp->scr_buffer )
395 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
397 if ( vvp->snapshot_buffer )
398 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
400 if ( vvp->alpha_pixbuf )
401 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
403 if ( vvp->background_gc )
404 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
406 if ( vvp->highlight_gc )
407 g_object_unref ( G_OBJECT ( vvp->highlight_gc ) );
409 if ( vvp->scale_bg_gc ) {
410 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
411 vvp->scale_bg_gc = NULL;
414 G_OBJECT_CLASS(parent_class)->finalize(gob);
418 * vik_viewport_clear:
421 * Clear the whole viewport.
423 void vik_viewport_clear ( VikViewport *vvp )
425 g_return_if_fail ( vvp != NULL );
426 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
427 vik_viewport_reset_copyrights ( vvp );
428 vik_viewport_reset_logos ( vvp );
432 * vik_viewport_set_draw_scale:
434 * @draw_scale: new value
436 * Enable/Disable display of scale.
438 void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
440 vvp->draw_scale = draw_scale;
443 gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
445 return vvp->draw_scale;
448 void vik_viewport_draw_scale ( VikViewport *vvp )
450 g_return_if_fail ( vvp != NULL );
452 if ( vvp->draw_scale ) {
453 VikCoord left, right;
454 gdouble unit, base, diff, old_unit, old_diff, ratio;
455 gint odd, len, SCSIZE = 5, HEIGHT=10;
459 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
460 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
462 vik_units_distance_t dist_units = a_vik_get_units_distance ();
463 switch (dist_units) {
464 case VIK_UNITS_DISTANCE_KILOMETRES:
465 base = vik_coord_diff ( &left, &right ); // in meters
467 case VIK_UNITS_DISTANCE_MILES:
468 // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
469 base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
472 base = 1; // Keep the compiler happy
473 g_critical("Houston, we've had a problem. distance=%d", dist_units);
475 ratio = (vvp->width/SCSIZE)/base;
478 diff = fabs(base-unit);
482 while (diff <= old_diff) {
485 unit = unit * (odd%2 ? 5 : 2);
486 diff = fabs(base-unit);
492 /* white background */
493 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
494 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
495 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
496 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
497 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
498 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
500 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
501 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
502 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
503 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
504 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
505 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
508 for (i=1; i<5; i++) {
509 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
510 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
511 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
512 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
516 for (i=1; i<10; i++) {
517 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
518 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
519 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
520 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
523 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
524 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
526 switch (dist_units) {
527 case VIK_UNITS_DISTANCE_KILOMETRES:
529 sprintf(s, "%d km", (int)unit/1000);
531 sprintf(s, "%d m", (int)unit);
534 case VIK_UNITS_DISTANCE_MILES:
535 // Handle units in 0.1 miles
537 sprintf(s, "%0.1f miles", unit/10.0);
539 else if ((int)unit == 10.0) {
540 sprintf(s, "1 mile");
543 sprintf(s, "%d miles", (int)(unit/10.0));
547 g_critical("Houston, we've had a problem. distance=%d", dist_units);
549 pango_layout_set_text(pl, s, -1);
550 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
551 PAD + len + PAD, vvp->height - PAD - 10, pl);
557 void vik_viewport_draw_copyright ( VikViewport *vvp )
559 g_return_if_fail ( vvp != NULL );
562 PangoRectangle ink_rect, logical_rect;
565 /* compute copyrights string */
566 guint len = g_slist_length ( vvp->copyrights );
569 for (i = 0 ; i < len ; i++)
571 // Stop when buffer is full
572 int slen = strlen ( s );
576 gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
578 // Only use part of this copyright that fits in the available space left
579 // remembering 1 character is left available for the appended space
580 int clen = strlen ( copyright );
581 if ( slen + clen > 126 ) {
585 strncat ( s, copyright, clen );
589 /* create pango layout */
590 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
591 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
592 pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
595 pango_layout_set_text(pl, s, -1);
597 /* Use maximum of half the viewport width */
598 pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
599 pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
600 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
601 vvp->width / 2, vvp->height - logical_rect.height, pl);
609 * vik_viewport_set_draw_centermark:
611 * @draw_centermark: new value
613 * Enable/Disable display of center mark.
615 void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
617 vvp->draw_centermark = draw_centermark;
620 gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
622 return vvp->draw_centermark;
625 void vik_viewport_draw_centermark ( VikViewport *vvp )
627 g_return_if_fail ( vvp != NULL );
629 if ( !vvp->draw_centermark )
634 int center_x = vvp->width/2;
635 int center_y = vvp->height/2;
636 GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
638 /* white back ground */
639 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
640 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
641 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
642 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
643 /* black fore ground */
644 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
645 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
646 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
647 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
651 void vik_viewport_draw_logo ( VikViewport *vvp )
653 g_return_if_fail ( vvp != NULL );
655 guint len = g_slist_length ( vvp->logos );
656 gint x = vvp->width - PAD;
659 for (i = 0 ; i < len ; i++)
661 GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
662 gint width = gdk_pixbuf_get_width ( logo );
663 gint height = gdk_pixbuf_get_height ( logo );
664 vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
669 void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
671 vvp->draw_highlight = draw_highlight;
674 gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
676 return vvp->draw_highlight;
679 void vik_viewport_sync ( VikViewport *vvp )
681 g_return_if_fail ( vvp != NULL );
682 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);
685 void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
689 g_return_if_fail ( vvp != NULL );
690 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);
696 x = vvp->width+x_off;
703 y = vvp->height+y_off;
706 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
707 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
710 void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
712 g_return_if_fail ( vvp != NULL );
713 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) {
714 vvp->xmpp = vvp->ympp = xympp;
715 // Since xmpp & ympp are the same it doesn't matter which one is used here
716 vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp);
719 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
720 viewport_utm_zone_check(vvp);
723 /* or could do factor */
724 void vik_viewport_zoom_in ( VikViewport *vvp )
726 g_return_if_fail ( vvp != NULL );
727 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
732 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
733 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
735 viewport_utm_zone_check(vvp);
739 void vik_viewport_zoom_out ( VikViewport *vvp )
741 g_return_if_fail ( vvp != NULL );
742 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
747 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
748 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
750 viewport_utm_zone_check(vvp);
754 gdouble vik_viewport_get_zoom ( VikViewport *vvp )
756 if ( vvp->xmpp == vvp->ympp )
761 gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
766 gdouble vik_viewport_get_ympp ( VikViewport *vvp )
771 void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
773 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
775 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
776 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
777 viewport_utm_zone_check(vvp);
781 void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
783 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
785 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
786 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
787 viewport_utm_zone_check(vvp);
792 const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
794 g_return_val_if_fail ( vvp != NULL, NULL );
795 return &(vvp->center);
798 /* called every time we update coordinates/zoom */
799 static void viewport_utm_zone_check ( VikViewport *vvp )
801 if ( vvp->coord_mode == VIK_COORD_UTM )
805 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
806 a_coords_latlon_to_utm ( &ll, &utm );
807 if ( utm.zone != vvp->center.utm_zone )
808 *((struct UTM *)(&vvp->center)) = utm;
810 /* misc. stuff so we don't have to check later */
811 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
812 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
816 void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
818 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
819 if ( vvp->coord_mode == VIK_COORD_UTM )
820 viewport_utm_zone_check ( vvp );
823 void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
825 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
826 if ( vvp->coord_mode == VIK_COORD_UTM )
827 viewport_utm_zone_check ( vvp );
830 void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
832 vvp->center = *coord;
833 if ( vvp->coord_mode == VIK_COORD_UTM )
834 viewport_utm_zone_check ( vvp );
837 void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
839 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
841 /* get center, then just offset */
842 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
843 ul->mode = VIK_COORD_UTM;
846 ul->north_south += (vvp->ympp * vvp->height / 2);
847 ul->east_west -= (vvp->xmpp * vvp->width / 2);
848 br->north_south -= (vvp->ympp * vvp->height / 2);
849 br->east_west += (vvp->xmpp * vvp->width / 2);
852 void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
854 if ( vvp->coord_mode == VIK_COORD_UTM ) {
855 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
856 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
861 gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
863 if ( vvp->coord_mode == VIK_COORD_UTM ) {
865 g_assert ( vvp != NULL );
866 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
867 return coord.utm_zone;
872 gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
874 if ( vvp->coord_mode == VIK_COORD_UTM ) {
876 g_assert ( vvp != NULL );
877 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
878 return coord.utm_zone;
884 void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
886 g_return_if_fail ( vvp != NULL );
887 if ( vvp->coord_mode == VIK_COORD_UTM ) {
888 /* slightly optimized */
889 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
890 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
891 viewport_utm_zone_check ( vvp );
894 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
895 vik_viewport_set_center_coord ( vvp, &tmp );
899 gint vik_viewport_get_width( VikViewport *vvp )
901 g_return_val_if_fail ( vvp != NULL, 0 );
905 gint vik_viewport_get_height( VikViewport *vvp )
907 g_return_val_if_fail ( vvp != NULL, 0 );
911 void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
913 g_return_if_fail ( vvp != NULL );
915 if ( vvp->coord_mode == VIK_COORD_UTM ) {
917 struct UTM *utm = (struct UTM *) coord;
918 coord->mode = VIK_COORD_UTM;
920 utm->zone = vvp->center.utm_zone;
921 utm->letter = vvp->center.utm_letter;
922 utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west;
923 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
924 utm->zone += zone_delta;
925 utm->easting -= zone_delta * vvp->utm_zone_width;
926 utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south;
927 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
928 coord->mode = VIK_COORD_LATLON;
929 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
930 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
931 coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y));
932 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
933 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);
934 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
935 /* This isn't called with a high frequently so less need to optimize */
936 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
937 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) );
943 * Since this function is used for every drawn trackpoint - it can get called alot
944 * Thus x & y position factors are calculated once on zoom changes,
945 * avoiding the need to do it here all the time.
946 * For good measure the half width and height values are also pre calculated too.
948 void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
951 g_return_if_fail ( vvp != NULL );
953 if ( coord->mode != vvp->coord_mode )
955 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
956 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
960 if ( vvp->coord_mode == VIK_COORD_UTM ) {
961 struct UTM *center = (struct UTM *) &(vvp->center);
962 struct UTM *utm = (struct UTM *) coord;
963 if ( center->zone != utm->zone && vvp->one_utm_zone )
965 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
969 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) -
970 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
971 *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp );
972 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
973 struct LatLon *center = (struct LatLon *) &(vvp->center);
974 struct LatLon *ll = (struct LatLon *) coord;
976 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
977 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
978 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) );
979 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
980 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 );
982 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
983 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
984 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) );
989 void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
991 if ( *x1 > 20000 || *x1 < -20000 ) {
992 gdouble shrinkfactor = ABS(20000.0 / *x1);
993 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
994 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
995 } else if ( *y1 > 20000 || *y1 < -20000 ) {
996 gdouble shrinkfactor = ABS(20000.0 / *x1);
997 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
998 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
999 } else if ( *x2 > 20000 || *x2 < -20000 ) {
1000 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
1001 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
1002 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
1003 } else if ( *y2 > 20000 || *y2 < -20000 ) {
1004 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
1005 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
1006 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
1010 void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
1012 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
1013 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
1014 /*** clipping, yeah! ***/
1015 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
1016 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
1020 void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
1022 // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
1023 if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 )
1024 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
1027 void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
1029 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
1030 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
1033 /* shouldn't use this -- slow -- change the alpha channel instead. */
1034 void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
1035 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
1037 gint real_dest_x = MAX(dest_x,0);
1038 gint real_dest_y = MAX(dest_y,0);
1041 return; /* don't waste your time */
1043 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
1045 if ( vvp->alpha_pixbuf )
1046 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
1047 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
1048 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
1049 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
1052 w = MIN(w,vvp->width - dest_x);
1053 h = MIN(h,vvp->height - dest_y);
1055 /* check that we are drawing within boundaries. */
1056 src_x += (real_dest_x - dest_x);
1057 src_y += (real_dest_y - dest_y);
1058 w -= (real_dest_x - dest_x);
1059 h -= (real_dest_y - dest_y);
1061 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
1062 real_dest_x, real_dest_y, 0, 0, w, h );
1064 /* do a composite */
1065 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
1067 /* draw pixbuf_tmp */
1068 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
1071 void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
1072 gint dest_x, gint dest_y, gint w, gint h )
1074 gdk_draw_pixbuf ( vvp->scr_buffer,
1077 src_x, src_y, dest_x, dest_y, w, h,
1078 GDK_RGB_DITHER_NONE, 0, 0 );
1081 void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
1083 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
1087 void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
1089 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
1092 VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
1095 return vvp->coord_mode;
1098 static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
1100 g_return_if_fail ( vvp != NULL );
1101 vvp->coord_mode = mode;
1102 vik_coord_convert ( &(vvp->center), mode );
1105 /* Thanks GPSDrive */
1106 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 )
1109 gdouble dif, lat, lon;
1110 double Ra = Radius[90+(gint)zero_lat];
1112 px = (mapSizeX2 - x) * pixelfact_x;
1113 py = (-mapSizeY2 + y) * pixelfact_y;
1115 lat = zero_lat - py / Ra;
1116 lat = zero_lat - py / Ra;
1120 cos (DEG2RAD(lat)));
1122 dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
1123 lat = lat - dif / 1.5;
1127 cos (DEG2RAD(lat)));
1134 /* Thanks GPSDrive */
1135 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 )
1139 gint mapSizeX = 2 * mapSizeX2;
1140 gint mapSizeY = 2 * mapSizeY2;
1142 g_assert ( lt >= -90.0 && lt <= 90.0 );
1143 // lg *= rad2deg; // FIXME, optimize equations
1145 Ra = Radius[90+(gint)lt];
1147 cos (DEG2RAD(lt)) * (lg - zero_long);
1148 *y = Ra * (lt - zero_lat);
1149 dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
1150 *y = *y + dif / 1.85;
1151 *x = *x / pixelfact_x;
1152 *y = *y / pixelfact_y;
1153 *x = mapSizeX2 - *x;
1155 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
1160 static void viewport_init_ra()
1162 static gboolean done_before = FALSE;
1166 for ( i = -90; i <= 90; i++)
1167 Radius[i+90] = calcR ( DEG2RAD((double)i) );
1172 double calcR (double lat)
1174 double a = 6378.137, r, sc, x, y, z;
1175 double e2 = 0.081082 * 0.081082;
1177 * the radius of curvature of an ellipsoidal Earth in the plane of the
1178 * meridian is given by
1180 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
1183 * where a is the equatorial radius, b is the polar radius, and e is
1184 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
1186 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
1187 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
1188 * = 0.081082 Eccentricity
1194 z = 1.0 - e2 * sc * sc;
1201 gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1203 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1206 void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1208 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1209 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1212 void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1214 static GdkGCValues values;
1215 gdk_gc_get_values ( gc, &values );
1216 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1219 GdkFunction vik_gc_get_function ( GdkGC *gc )
1221 static GdkGCValues values;
1222 gdk_gc_get_values ( gc, &values );
1223 return values.function;
1226 void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1228 vvp->drawmode = drawmode;
1229 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1230 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1232 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
1236 VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1238 return vvp->drawmode;
1241 /******** triggering *******/
1242 void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1244 vp->trigger = trigger;
1247 gpointer vik_viewport_get_trigger ( VikViewport *vp )
1252 void vik_viewport_snapshot_save ( VikViewport *vp )
1254 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1257 void vik_viewport_snapshot_load ( VikViewport *vp )
1259 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1262 void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1264 vp->half_drawn = half_drawn;
1267 gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1269 return vp->half_drawn;
1273 const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
1275 const gchar *name = NULL;
1276 VikWindow *vw = NULL;
1277 GtkWidget *mode_button;
1280 vw = VIK_WINDOW_FROM_WIDGET(vv);
1281 mode_button = vik_window_get_drawmode_button(vw, mode);
1282 label = gtk_bin_get_child(GTK_BIN(mode_button));
1284 name = gtk_label_get_text ( GTK_LABEL(label) );
1290 void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1292 VikCoord tleft, tright, bleft, bright;
1294 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1295 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1296 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1297 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
1299 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1300 vik_coord_convert(&tright, VIK_COORD_LATLON);
1301 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1302 vik_coord_convert(&bright, VIK_COORD_LATLON);
1304 *max_lat = MAX(tleft.north_south, tright.north_south);
1305 *min_lat = MIN(bleft.north_south, bright.north_south);
1306 *max_lon = MAX(tright.east_west, bright.east_west);
1307 *min_lon = MIN(tleft.east_west, bleft.east_west);
1310 void vik_viewport_reset_copyrights ( VikViewport *vp )
1312 g_return_if_fail ( vp != NULL );
1313 g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
1314 g_slist_free ( vp->copyrights );
1315 vp->copyrights = NULL;
1319 * vik_viewport_add_copyright:
1321 * @copyright: new copyright to display
1323 * Add a copyright to display on viewport.
1325 void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright )
1327 g_return_if_fail ( vp != NULL );
1330 GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
1331 if ( found == NULL )
1333 gchar *duple = g_strdup ( copyright );
1334 vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
1339 void vik_viewport_reset_logos ( VikViewport *vp )
1341 g_return_if_fail ( vp != NULL );
1342 /* do not free elem */
1343 g_slist_free ( vp->logos );
1347 void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
1349 g_return_if_fail ( vp != NULL );
1352 GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
1353 if ( found == NULL )
1355 vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
1361 * vik_viewport_compute_bearing:
1367 * @angle: bearing in Radian (output)
1368 * @baseangle: UTM base angle in Radian (output)
1372 void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
1374 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
1375 gdouble dx = (x2-x1)/len*10;
1376 gdouble dy = (y2-y1)/len*10;
1378 *angle = atan2(dy, dx) + M_PI_2;
1380 if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
1386 vik_viewport_screen_to_coord ( vp, x1, y1, &test );
1387 vik_coord_to_latlon ( &test, &ll );
1388 ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude
1389 a_coords_latlon_to_utm ( &ll, &u );
1390 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
1391 vik_viewport_coord_to_screen ( vp, &test, &tx, &ty );
1393 *baseangle = M_PI - atan2(tx-x1, ty-y1);
1394 *angle -= *baseangle;
1399 if (*angle > 2*M_PI)