]> git.street.me.uk Git - andy/viking.git/blame - src/vikviewport.c
[WINDOWS] Fix reading zip files on Windows due to compiler alignment defaults changing.
[andy/viking.git] / src / vikviewport.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a482007a 4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
be5554c5 5 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
50a14534
EB
6 *
7 * Lat/Lon plotting functions calcxy* are from GPSDrive
8 * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
9 *
10 * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
8c00358d
GB
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
50a14534
EB
30
31#define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
480fb7e1
RN
32#define DEFAULT_HIGHLIGHT_COLOR "#EEA500"
33/* Default highlight in orange */
50a14534
EB
34
35#include <gtk/gtk.h>
8c00358d 36#ifdef HAVE_MATH_H
50a14534 37#include <math.h>
8c00358d 38#endif
82aa018d
GB
39#ifdef HAVE_STRING_H
40#include <string.h>
41#endif
50a14534
EB
42
43#include "coords.h"
44#include "vikcoord.h"
a59e6eb9 45#include "vikwindow.h"
50a14534 46#include "vikviewport.h"
50a14534
EB
47#include "mapcoord.h"
48
49/* for ALTI_TO_MPP */
50#include "globals.h"
7143d1e4 51#include "settings.h"
fca1f3ad 52#include "dialog.h"
50a14534 53
a3603298
RN
54#define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 )
55
50a14534
EB
56static gdouble EASTING_OFFSET = 500000.0;
57
26336cf0
GB
58static gint PAD = 10;
59
50a14534
EB
60static void viewport_finalize ( GObject *gob );
61static void viewport_utm_zone_check ( VikViewport *vvp );
be5554c5
RN
62static void update_centers ( VikViewport *vvp );
63static void free_centers ( VikViewport *vvp, guint start );
50a14534
EB
64
65static 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 );
66static 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 );
67double calcR (double lat);
68
69static double Radius[181];
70static void viewport_init_ra();
71
72static GObjectClass *parent_class;
73
50a14534
EB
74struct _VikViewport {
75 GtkDrawingArea drawing_area;
76 GdkPixmap *scr_buffer;
77 gint width, height;
a3603298 78 gint width_2, height_2; // Half of the normal width and height
50a14534
EB
79 VikCoord center;
80 VikCoordMode coord_mode;
81 gdouble xmpp, ympp;
a3603298 82 gdouble xmfactor, ymfactor;
fca1f3ad 83 GList *centers; // The history of requested positions (of VikCoord type)
be5554c5
RN
84 guint centers_index; // current position within the history list
85 guint centers_max; // configurable maximum size of the history list
86 guint centers_radius; // Metres
50a14534
EB
87
88 GdkPixbuf *alpha_pixbuf;
89 guint8 alpha_pixbuf_width;
90 guint8 alpha_pixbuf_height;
91
92 gdouble utm_zone_width;
93 gboolean one_utm_zone;
94
95 GdkGC *background_gc;
96 GdkColor background_color;
4e485bf3 97 GdkGC *scale_bg_gc;
82aa018d
GB
98
99 GSList *copyrights;
26336cf0 100 GSList *logos;
82aa018d
GB
101
102 /* Wether or not display OSD info */
35c7c0ba 103 gboolean draw_scale;
c933487f 104 gboolean draw_centermark;
2afcef36 105 gboolean draw_highlight;
480fb7e1
RN
106 GdkGC *highlight_gc;
107 GdkColor highlight_color;
50a14534
EB
108
109 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
110 VikViewportDrawMode drawmode;
111
0df66d57
EB
112 /* trigger stuff */
113 gpointer trigger;
114 GdkPixmap *snapshot_buffer;
115 gboolean half_drawn;
50a14534
EB
116};
117
118static gdouble
119viewport_utm_zone_width ( VikViewport *vvp )
120{
121 if ( vvp->coord_mode == VIK_COORD_UTM ) {
122 struct LatLon ll;
123
124 /* get latitude of screen bottom */
125 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
126 utm.northing -= vvp -> height * vvp -> ympp / 2;
127 a_coords_utm_to_latlon ( &utm, &ll );
128
129 /* boundary */
130 ll.lon = (utm.zone - 1) * 6 - 180 ;
131 a_coords_latlon_to_utm ( &ll, &utm);
132 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
133 } else
134 return 0.0;
135}
136
be5554c5
RN
137enum {
138 VW_UPDATED_CENTER_SIGNAL = 0,
139 VW_LAST_SIGNAL,
140};
141static guint viewport_signals[VW_LAST_SIGNAL] = { 0 };
142
eb560cd6 143G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA)
50a14534 144
eb560cd6
GB
145static void
146vik_viewport_class_init ( VikViewportClass *klass )
50a14534
EB
147{
148 /* Destructor */
149 GObjectClass *object_class;
150
151 object_class = G_OBJECT_CLASS (klass);
152
153 object_class->finalize = viewport_finalize;
154
155 parent_class = g_type_class_peek_parent (klass);
be5554c5
RN
156
157 viewport_signals[VW_UPDATED_CENTER_SIGNAL] = g_signal_new ( "updated_center", G_TYPE_FROM_CLASS (klass),
158 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikViewportClass, updated_center), NULL, NULL,
159 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
50a14534
EB
160}
161
24277274 162VikViewport *vik_viewport_new ()
50a14534 163{
a59e6eb9 164 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
a59e6eb9 165 return vv;
50a14534
EB
166}
167
7143d1e4
RN
168#define VIK_SETTINGS_VIEW_LAST_LATITUDE "viewport_last_latitude"
169#define VIK_SETTINGS_VIEW_LAST_LONGITUDE "viewport_last_longitude"
170#define VIK_SETTINGS_VIEW_LAST_ZOOM_X "viewport_last_zoom_xpp"
171#define VIK_SETTINGS_VIEW_LAST_ZOOM_Y "viewport_last_zoom_ypp"
be5554c5
RN
172#define VIK_SETTINGS_VIEW_HISTORY_SIZE "viewport_history_size"
173#define VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST "viewport_history_diff_dist"
7143d1e4 174
eb560cd6
GB
175static void
176vik_viewport_init ( VikViewport *vvp )
50a14534
EB
177{
178 viewport_init_ra();
179
5210c3d3
GB
180 struct UTM utm;
181 struct LatLon ll;
182 ll.lat = a_vik_get_default_lat();
183 ll.lon = a_vik_get_default_long();
7143d1e4
RN
184 gdouble zoom_x = 4.0;
185 gdouble zoom_y = 4.0;
186
187 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
188 gdouble lat, lon, dzoom;
189 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, &lat ) )
190 ll.lat = lat;
191 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, &lon ) )
192 ll.lon = lon;
193 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, &dzoom ) )
194 zoom_x = dzoom;
195 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, &dzoom ) )
196 zoom_y = dzoom;
197 }
198
5210c3d3
GB
199 a_coords_latlon_to_utm ( &ll, &utm );
200
7143d1e4
RN
201 vvp->xmpp = zoom_x;
202 vvp->ympp = zoom_y;
a3603298
RN
203 vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp);
204 vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp);
6a9ff0ee
QT
205 vvp->coord_mode = VIK_COORD_LATLON;
206 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
ee180665 207 vvp->center.mode = VIK_COORD_LATLON;
5210c3d3
GB
208 vvp->center.north_south = ll.lat;
209 vvp->center.east_west = ll.lon;
210 vvp->center.utm_zone = (int)utm.zone;
211 vvp->center.utm_letter = utm.letter;
50a14534
EB
212 vvp->scr_buffer = NULL;
213 vvp->alpha_pixbuf = NULL;
214 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
215 vvp->utm_zone_width = 0.0;
216 vvp->background_gc = NULL;
480fb7e1 217 vvp->highlight_gc = NULL;
4e485bf3 218 vvp->scale_bg_gc = NULL;
82aa018d
GB
219
220 vvp->copyrights = NULL;
be5554c5
RN
221 vvp->centers = NULL;
222 vvp->centers_index = 0;
223 vvp->centers_max = 20;
224 gint tmp = vvp->centers_max;
225 if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_SIZE, &tmp ) )
226 vvp->centers_max = tmp;
227 vvp->centers_radius = 500;
228 if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST, &tmp ) )
229 vvp->centers_radius = tmp;
82aa018d 230
35c7c0ba 231 vvp->draw_scale = TRUE;
c933487f 232 vvp->draw_centermark = TRUE;
2afcef36 233 vvp->draw_highlight = TRUE;
0df66d57
EB
234
235 vvp->trigger = NULL;
236 vvp->snapshot_buffer = NULL;
237 vvp->half_drawn = FALSE;
238
be5554c5
RN
239 // Initiate center history
240 update_centers ( vvp );
241
50a14534 242 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
165d30aa 243
2fd7c35e
RN
244#if GTK_CHECK_VERSION (2,18,0)
245 gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE );
246#else
165d30aa 247 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
2fd7c35e 248#endif
50a14534
EB
249}
250
251GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
252{
253 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
254 *rv = vvp->background_color;
255 return rv;
256}
257
258/* returns pointer to internal static storage, changes next time function called, use quickly */
259const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
260{
261 static gchar color[8];
262 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));
263 return color;
264}
265
266void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
267{
52aa15a7 268 g_assert ( vvp && vvp->background_gc );
b244bc6a
GB
269 if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
270 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
271 else
272 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
50a14534
EB
273}
274
275void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
276{
52aa15a7 277 g_assert ( vvp && vvp->background_gc );
50a14534
EB
278 vvp->background_color = *color;
279 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
280}
281
480fb7e1
RN
282GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp )
283{
284 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
285 *rv = vvp->highlight_color;
286 return rv;
287}
288
289/* returns pointer to internal static storage, changes next time function called, use quickly */
290const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp )
291{
292 static gchar color[8];
293 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));
294 return color;
295}
296
297void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname )
298{
299 g_assert ( vvp->highlight_gc );
300 gdk_color_parse ( colorname, &(vvp->highlight_color) );
301 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) );
302}
303
304void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color )
305{
306 g_assert ( vvp->highlight_gc );
307 vvp->highlight_color = *color;
308 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color );
309}
310
311GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp )
312{
313 return vvp->highlight_gc;
314}
50a14534 315
04f36d92
RN
316void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness )
317{
318 // Otherwise same GDK_* attributes as in vik_viewport_new_gc
319 gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
320}
321
50a14534
EB
322GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
323{
b244bc6a 324 GdkGC *rv = NULL;
50a14534
EB
325 GdkColor color;
326
9b082b39 327 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
b244bc6a
GB
328 if ( gdk_color_parse ( colorname, &color ) )
329 gdk_gc_set_rgb_fg_color ( rv, &color );
330 else
331 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
50a14534
EB
332 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
333 return rv;
334}
335
336GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
337{
338 GdkGC *rv;
339
9b082b39 340 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
50a14534
EB
341 gdk_gc_set_rgb_fg_color ( rv, color );
342 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
343 return rv;
344}
345
346void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
347{
348 vvp->width = width;
349 vvp->height = height;
a3603298
RN
350
351 vvp->width_2 = vvp->width/2;
352 vvp->height_2 = vvp->height/2;
353
50a14534
EB
354 if ( vvp->scr_buffer )
355 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
9b082b39 356 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
0df66d57
EB
357
358 /* TODO trigger: only if this is enabled !!! */
359 if ( vvp->snapshot_buffer )
360 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
9b082b39 361 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
50a14534
EB
362}
363
364
365GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
366{
367 return vvp->scr_buffer;
368}
369
370gboolean vik_viewport_configure ( VikViewport *vvp )
371{
372 g_return_val_if_fail ( vvp != NULL, TRUE );
373
90ba111c
RN
374 GtkAllocation allocation;
375 gtk_widget_get_allocation ( GTK_WIDGET(vvp), &allocation );
376 vvp->width = allocation.width;
377 vvp->height = allocation.height;
50a14534 378
a3603298
RN
379 vvp->width_2 = vvp->width/2;
380 vvp->height_2 = vvp->height/2;
381
50a14534
EB
382 if ( vvp->scr_buffer )
383 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
384
9b082b39 385 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
50a14534 386
0df66d57
EB
387 /* TODO trigger: only if enabled! */
388 if ( vvp->snapshot_buffer )
389 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
390
9b082b39 391 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
0df66d57
EB
392 /* TODO trigger */
393
50a14534 394 /* this is down here so it can get a GC (necessary?) */
e8e82387 395 if ( !vvp->background_gc )
50a14534 396 {
348e8666 397 vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
e8e82387 398 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
50a14534 399 }
480fb7e1
RN
400 if ( ! vvp->highlight_gc )
401 {
402 vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 );
403 vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR );
404 }
4e485bf3
QT
405 if ( !vvp->scale_bg_gc) {
406 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
407 }
50a14534
EB
408
409 return FALSE;
410}
411
412static void viewport_finalize ( GObject *gob )
413{
414 VikViewport *vvp = VIK_VIEWPORT(gob);
415
416 g_return_if_fail ( vvp != NULL );
417
7143d1e4
RN
418 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
419 struct LatLon ll;
420 vik_coord_to_latlon ( &(vvp->center), &ll );
421 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, ll.lat );
422 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, ll.lon );
423 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, vvp->xmpp );
424 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, vvp->ympp );
425 }
426
be5554c5
RN
427 if ( vvp->centers )
428 free_centers ( vvp, 0 );
429
50a14534
EB
430 if ( vvp->scr_buffer )
431 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
432
0df66d57
EB
433 if ( vvp->snapshot_buffer )
434 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
435
50a14534
EB
436 if ( vvp->alpha_pixbuf )
437 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
438
439 if ( vvp->background_gc )
440 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
441
480fb7e1
RN
442 if ( vvp->highlight_gc )
443 g_object_unref ( G_OBJECT ( vvp->highlight_gc ) );
444
4e485bf3
QT
445 if ( vvp->scale_bg_gc ) {
446 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
447 vvp->scale_bg_gc = NULL;
448 }
449
50a14534
EB
450 G_OBJECT_CLASS(parent_class)->finalize(gob);
451}
452
33e0e500
GB
453/**
454 * vik_viewport_clear:
455 * @vvp: self object
456 *
457 * Clear the whole viewport.
458 */
50a14534
EB
459void vik_viewport_clear ( VikViewport *vvp )
460{
461 g_return_if_fail ( vvp != NULL );
462 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
82aa018d 463 vik_viewport_reset_copyrights ( vvp );
26336cf0 464 vik_viewport_reset_logos ( vvp );
50a14534
EB
465}
466
33e0e500
GB
467/**
468 * vik_viewport_set_draw_scale:
469 * @vvp: self
470 * @draw_scale: new value
471 *
472 * Enable/Disable display of scale.
473 */
35c7c0ba
EB
474void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
475{
476 vvp->draw_scale = draw_scale;
477}
478
479gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
480{
481 return vvp->draw_scale;
482}
483
acaf7113
AF
484void vik_viewport_draw_scale ( VikViewport *vvp )
485{
c53eda9a
GB
486 g_return_if_fail ( vvp != NULL );
487
35c7c0ba
EB
488 if ( vvp->draw_scale ) {
489 VikCoord left, right;
490 gdouble unit, base, diff, old_unit, old_diff, ratio;
26336cf0 491 gint odd, len, SCSIZE = 5, HEIGHT=10;
35c7c0ba
EB
492 PangoLayout *pl;
493 gchar s[128];
acaf7113 494
35c7c0ba
EB
495 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
496 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
acaf7113 497
6f9336aa
RN
498 vik_units_distance_t dist_units = a_vik_get_units_distance ();
499 switch (dist_units) {
500 case VIK_UNITS_DISTANCE_KILOMETRES:
501 base = vik_coord_diff ( &left, &right ); // in meters
502 break;
503 case VIK_UNITS_DISTANCE_MILES:
8d772d8a 504 // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
433b3f7f 505 base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
6f9336aa 506 break;
b22233bd
RN
507 case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
508 // in 0.1 NM (copes better when zoomed in as 1 NM can be too big)
509 base = VIK_METERS_TO_NAUTICAL_MILES(vik_coord_diff ( &left, &right )) * 10.0;
510 break;
6f9336aa
RN
511 default:
512 base = 1; // Keep the compiler happy
513 g_critical("Houston, we've had a problem. distance=%d", dist_units);
514 }
35c7c0ba 515 ratio = (vvp->width/SCSIZE)/base;
acaf7113 516
35c7c0ba
EB
517 unit = 1;
518 diff = fabs(base-unit);
acaf7113
AF
519 old_unit = unit;
520 old_diff = diff;
35c7c0ba
EB
521 odd = 1;
522 while (diff <= old_diff) {
523 old_unit = unit;
524 old_diff = diff;
525 unit = unit * (odd%2 ? 5 : 2);
526 diff = fabs(base-unit);
527 odd++;
528 }
529 unit = old_unit;
530 len = unit * ratio;
acaf7113 531
4e485bf3
QT
532 /* white background */
533 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
534 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
535 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
536 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
537 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
538 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
539 /* black scale */
ff37db21 540 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 541 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
ff37db21 542 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 543 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
ff37db21 544 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 545 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
35c7c0ba
EB
546 if (odd%2) {
547 int i;
548 for (i=1; i<5; i++) {
4e485bf3
QT
549 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
550 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
ff37db21 551 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 552 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
35c7c0ba
EB
553 }
554 } else {
555 int i;
556 for (i=1; i<10; i++) {
4e485bf3
QT
557 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
558 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
ff37db21 559 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
35c7c0ba
EB
560 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
561 }
acaf7113 562 }
35c7c0ba 563 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
ff37db21 564 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
35c7c0ba 565
6f9336aa
RN
566 switch (dist_units) {
567 case VIK_UNITS_DISTANCE_KILOMETRES:
568 if (unit >= 1000) {
b22233bd 569 sprintf(s, "%d km", (int)unit/1000);
6f9336aa 570 } else {
b22233bd 571 sprintf(s, "%d m", (int)unit);
6f9336aa
RN
572 }
573 break;
574 case VIK_UNITS_DISTANCE_MILES:
8d772d8a
RN
575 // Handle units in 0.1 miles
576 if (unit < 10.0) {
b22233bd
RN
577 sprintf(s, "%0.1f miles", unit/10.0);
578 }
579 else if ((int)unit == 10.0) {
580 sprintf(s, "1 mile");
581 }
582 else {
583 sprintf(s, "%d miles", (int)(unit/10.0));
584 }
585 break;
586 case VIK_UNITS_DISTANCE_NAUTICAL_MILES:
587 // Handle units in 0.1 NM
588 if (unit < 10.0) {
589 sprintf(s, "%0.1f NM", unit/10.0);
8d772d8a
RN
590 }
591 else if ((int)unit == 10.0) {
b22233bd 592 sprintf(s, "1 NM");
6f9336aa
RN
593 }
594 else {
b22233bd 595 sprintf(s, "%d NMs", (int)(unit/10.0));
6f9336aa
RN
596 }
597 break;
598 default:
599 g_critical("Houston, we've had a problem. distance=%d", dist_units);
acaf7113 600 }
35c7c0ba 601 pango_layout_set_text(pl, s, -1);
ff37db21 602 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 603 PAD + len + PAD, vvp->height - PAD - 10, pl);
6278ccc6 604 g_object_unref(pl);
e6869c62 605 pl = NULL;
35c7c0ba 606 }
acaf7113
AF
607}
608
82aa018d
GB
609void vik_viewport_draw_copyright ( VikViewport *vvp )
610{
611 g_return_if_fail ( vvp != NULL );
612
82aa018d
GB
613 PangoLayout *pl;
614 PangoRectangle ink_rect, logical_rect;
615 gchar s[128] = "";
616
617 /* compute copyrights string */
618 guint len = g_slist_length ( vvp->copyrights );
15a0bd14 619
82aa018d
GB
620 int i;
621 for (i = 0 ; i < len ; i++)
622 {
15a0bd14
RN
623 // Stop when buffer is full
624 int slen = strlen ( s );
625 if ( slen >= 127 )
626 break;
627
82aa018d 628 gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
15a0bd14
RN
629
630 // Only use part of this copyright that fits in the available space left
631 // remembering 1 character is left available for the appended space
632 int clen = strlen ( copyright );
633 if ( slen + clen > 126 ) {
634 clen = 126 - slen;
635 }
636
637 strncat ( s, copyright, clen );
82aa018d
GB
638 strcat ( s, " " );
639 }
640
641 /* create pango layout */
642 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
ff37db21 643 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
82aa018d
GB
644 pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
645
646 /* Set the text */
647 pango_layout_set_text(pl, s, -1);
648
649 /* Use maximum of half the viewport width */
56cb1807 650 pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
82aa018d 651 pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
ff37db21 652 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
56cb1807 653 vvp->width / 2, vvp->height - logical_rect.height, pl);
82aa018d
GB
654
655 /* Free memory */
656 g_object_unref(pl);
657 pl = NULL;
658}
659
33e0e500
GB
660/**
661 * vik_viewport_set_draw_centermark:
662 * @vvp: self object
663 * @draw_centermark: new value
664 *
665 * Enable/Disable display of center mark.
666 */
c933487f
QT
667void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
668{
669 vvp->draw_centermark = draw_centermark;
670}
671
672gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
673{
674 return vvp->draw_centermark;
675}
676
677void vik_viewport_draw_centermark ( VikViewport *vvp )
678{
c53eda9a
GB
679 g_return_if_fail ( vvp != NULL );
680
c933487f
QT
681 if ( !vvp->draw_centermark )
682 return;
683
3df69a6b
QT
684 const int len = 30;
685 const int gap = 4;
c933487f
QT
686 int center_x = vvp->width/2;
687 int center_y = vvp->height/2;
ff37db21 688 GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
c933487f
QT
689
690 /* white back ground */
3df69a6b
QT
691 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
692 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
693 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
694 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
c933487f 695 /* black fore ground */
3df69a6b
QT
696 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
697 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
698 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
699 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
c933487f
QT
700
701}
702
26336cf0
GB
703void vik_viewport_draw_logo ( VikViewport *vvp )
704{
705 g_return_if_fail ( vvp != NULL );
706
26336cf0
GB
707 guint len = g_slist_length ( vvp->logos );
708 gint x = vvp->width - PAD;
709 gint y = PAD;
710 int i;
711 for (i = 0 ; i < len ; i++)
712 {
713 GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
714 gint width = gdk_pixbuf_get_width ( logo );
715 gint height = gdk_pixbuf_get_height ( logo );
716 vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
717 x = x - width - PAD;
718 }
2afcef36 719}
26336cf0 720
2afcef36
RN
721void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
722{
723 vvp->draw_highlight = draw_highlight;
724}
725
726gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
727{
728 return vvp->draw_highlight;
26336cf0
GB
729}
730
50a14534
EB
731void vik_viewport_sync ( VikViewport *vvp )
732{
733 g_return_if_fail ( vvp != NULL );
9b082b39 734 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);
50a14534
EB
735}
736
737void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
738{
acaf7113
AF
739 gint x, y, wid, hei;
740
50a14534 741 g_return_if_fail ( vvp != NULL );
9b082b39 742 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);
acaf7113
AF
743
744 if (x_off >= 0) {
745 x = 0;
746 wid = x_off;
747 } else {
748 x = vvp->width+x_off;
749 wid = -x_off;
750 }
751 if (y_off >= 0) {
752 y = 0;
753 hei = y_off;
754 } else {
755 y = vvp->height+y_off;
756 hei = -y_off;
757 }
758 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
759 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
50a14534
EB
760}
761
762void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
763{
764 g_return_if_fail ( vvp != NULL );
a3603298 765 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) {
50a14534 766 vvp->xmpp = vvp->ympp = xympp;
a3603298
RN
767 // Since xmpp & ympp are the same it doesn't matter which one is used here
768 vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp);
769 }
50a14534
EB
770
771 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
772 viewport_utm_zone_check(vvp);
50a14534
EB
773}
774
775/* or could do factor */
776void vik_viewport_zoom_in ( VikViewport *vvp )
777{
778 g_return_if_fail ( vvp != NULL );
779 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
780 {
781 vvp->xmpp /= 2;
782 vvp->ympp /= 2;
783
a3603298
RN
784 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
785 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
786
50a14534
EB
787 viewport_utm_zone_check(vvp);
788 }
789}
790
791void vik_viewport_zoom_out ( VikViewport *vvp )
792{
793 g_return_if_fail ( vvp != NULL );
794 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
795 {
796 vvp->xmpp *= 2;
797 vvp->ympp *= 2;
798
a3603298
RN
799 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
800 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
801
50a14534
EB
802 viewport_utm_zone_check(vvp);
803 }
804}
805
806gdouble vik_viewport_get_zoom ( VikViewport *vvp )
807{
808 if ( vvp->xmpp == vvp->ympp )
809 return vvp->xmpp;
810 return 0.0;
811}
812
813gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
814{
815 return vvp->xmpp;
816}
817
818gdouble vik_viewport_get_ympp ( VikViewport *vvp )
819{
820 return vvp->ympp;
821}
822
823void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
824{
825 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
826 vvp->xmpp = xmpp;
a3603298 827 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
50a14534
EB
828 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
829 viewport_utm_zone_check(vvp);
50a14534
EB
830 }
831}
832
833void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
834{
835 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
836 vvp->ympp = ympp;
a3603298 837 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
50a14534
EB
838 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
839 viewport_utm_zone_check(vvp);
50a14534
EB
840 }
841}
842
843
844const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
845{
846 g_return_val_if_fail ( vvp != NULL, NULL );
847 return &(vvp->center);
848}
849
850/* called every time we update coordinates/zoom */
851static void viewport_utm_zone_check ( VikViewport *vvp )
852{
853 if ( vvp->coord_mode == VIK_COORD_UTM )
854 {
855 struct UTM utm;
856 struct LatLon ll;
857 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
858 a_coords_latlon_to_utm ( &ll, &utm );
859 if ( utm.zone != vvp->center.utm_zone )
860 *((struct UTM *)(&vvp->center)) = utm;
861
862 /* misc. stuff so we don't have to check later */
863 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
864 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
865 }
866}
867
be5554c5
RN
868/**
869 * Free an individual center position in the history list
870 */
871static void free_center ( VikViewport *vvp, guint index )
872{
873 VikCoord *coord = g_list_nth_data ( vvp->centers, index );
874 if ( coord )
875 g_free ( coord );
876 GList *gl = g_list_nth ( vvp->centers, index );
877 if ( gl )
878 vvp->centers = g_list_delete_link ( vvp->centers, gl );
879}
880
881/**
882 * Free a set of center positions in the history list,
883 * from the indicated start index to the end of the list
884 */
885static void free_centers ( VikViewport *vvp, guint start )
886{
887 // Have to work backward since we delete items referenced by the '_nth()' values,
888 // otherwise if processed forward - removing the lower nth index entries would change the subsequent indexing
889 for ( guint i = g_list_length(vvp->centers)-1; i > start; i-- )
890 free_center ( vvp, i );
891}
892
893/**
894 * Store the current center position into the history list
895 * and emit a signal to notify clients the list has been updated
896 */
897static void update_centers ( VikViewport *vvp )
898{
899 VikCoord *new_center = g_malloc(sizeof (VikCoord));
900 *new_center = vvp->center;
901
902 if ( vvp->centers_index ) {
903
904 if ( vvp->centers_index == vvp->centers_max-1 ) {
905 // List is full, so drop the oldest value to make room for the new one
906 free_center ( vvp, 0 );
907 vvp->centers_index--;
908 }
909 else {
910 // Reset the now unused section of the list
911 // Free from the index to the end
912 free_centers ( vvp, vvp->centers_index+1 );
913 }
914
915 }
916
917 // Store new position
918 // NB ATM this can be the same location as the last one in the list
919 vvp->centers = g_list_append ( vvp->centers, new_center );
920
921 // Reset to the end (NB should be same as centers_index++)
922 vvp->centers_index = g_list_length ( vvp->centers ) - 1;
923
924 // Inform interested subscribers that this change has occurred
925 g_signal_emit ( G_OBJECT(vvp), viewport_signals[VW_UPDATED_CENTER_SIGNAL], 0 );
926}
927
fca1f3ad
RN
928/**
929 * Show the list of forward/backward positions
930 * ATM only for debug usage
931 */
932void vik_viewport_show_centers ( VikViewport *vvp, GtkWindow *parent )
933{
934 GList* node = NULL;
935 GList* texts = NULL;
936 gint index = 0;
937 for (node = vvp->centers; node != NULL; node = g_list_next(node)) {
938 gchar *lat = NULL, *lon = NULL;
939 struct LatLon ll;
940 vik_coord_to_latlon (node->data, &ll);
941 a_coords_latlon_to_string ( &ll, &lat, &lon );
942 gchar *extra = NULL;
943 if ( index == vvp->centers_index-1 )
944 extra = g_strdup ( " [Back]" );
945 else if ( index == vvp->centers_index+1 )
946 extra = g_strdup ( " [Forward]" );
947 else
948 extra = g_strdup ( "" );
949 texts = g_list_prepend ( texts , g_strdup_printf ( "%s %s%s", lat, lon, extra ) );
950 g_free ( lat );
951 g_free ( lon );
952 g_free ( extra );
953 index++;
954 }
955
956 // NB: No i18n as this is just for debug
957 // Using this function the dialog allows sorting of the list which isn't appropriate here
958 // but this doesn't matter much for debug purposes of showing stuff...
959 GList *ans = a_dialog_select_from_list(parent,
960 texts,
961 FALSE,
962 "Back/Forward Locations",
963 "Back/Forward Locations");
964 for (node = ans; node != NULL; node = g_list_next(node))
965 g_free(node->data);
966 g_list_free(ans);
967 for (node = texts; node != NULL; node = g_list_next(node))
968 g_free(node->data);
969 g_list_free(texts);
970}
971
be5554c5
RN
972/**
973 * vik_viewport_go_back:
974 *
975 * Move back in the position history
976 *
977 * Returns: %TRUE one success
978 */
979gboolean vik_viewport_go_back ( VikViewport *vvp )
980{
981 // see if the current position is different from the last saved center position within a certain radius
982 VikCoord *center = g_list_nth_data ( vvp->centers, vvp->centers_index );
983 if ( center ) {
984 // Consider an exclusion size (should it zoom level dependent, rather than a fixed value?)
985 // When still near to the last saved position we'll jump over it to the one before
986 if ( vik_coord_diff ( center, &vvp->center ) > vvp->centers_radius ) {
987
988 if ( vvp->centers_index == g_list_length(vvp->centers)-1 ) {
989 // Only when we haven't already moved back in the list
990 // Remember where this request came from
991 // (alternatively we could insert in the list on every back attempt)
992 update_centers ( vvp );
993 }
994
995 }
996 // 'Go back' if possible
997 // NB if we inserted a position above, then this will then move to the last saved position
998 // otherwise this will skip to the previous saved position, as it's probably somewhere else.
999 if ( vvp->centers_index > 0 )
1000 vvp->centers_index--;
1001 }
1002 else {
1003 return FALSE;
1004 }
1005
1006 VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index );
1007 if ( new_center ) {
1008 vik_viewport_set_center_coord ( vvp, new_center, FALSE );
1009 return TRUE;
1010 }
1011 return FALSE;
1012}
1013
1014/**
1015 * vik_viewport_go_forward:
1016 *
1017 * Move forward in the position history
1018 *
1019 * Returns: %TRUE one success
1020 */
1021gboolean vik_viewport_go_forward ( VikViewport *vvp )
1022{
1023 if ( vvp->centers_index == vvp->centers_max-1 )
1024 return FALSE;
1025
1026 vvp->centers_index++;
1027 VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index );
1028 if ( new_center ) {
1029 vik_viewport_set_center_coord ( vvp, new_center, FALSE );
1030 return TRUE;
1031 }
1032 else
1033 // Set to end of list
1034 vvp->centers_index = g_list_length(vvp->centers) - 1;
1035
1036 return FALSE;
1037}
1038
1039/**
1040 * vik_viewport_back_available:
1041 *
1042 * Returns: %TRUE when a previous position in the history is available
1043 */
1044gboolean vik_viewport_back_available ( const VikViewport *vvp )
1045{
1046 return ( vvp->centers_index > 0 );
1047}
1048
1049/**
1050 * vik_viewport_forward_available:
1051 *
1052 * Returns: %TRUE when a next position in the history is available
1053 */
1054gboolean vik_viewport_forward_available ( const VikViewport *vvp )
1055{
1056 return ( vvp->centers_index < g_list_length(vvp->centers)-1 );
1057}
1058
1059/**
1060 * vik_viewport_set_center_latlon:
1061 * @vvp: The viewport to reposition.
1062 * @ll: The new center position in Lat/Lon format
1063 * @save_position: Whether this new position should be saved into the history of positions
1064 * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions)
1065 */
1066void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll, gboolean save_position )
50a14534
EB
1067{
1068 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
be5554c5
RN
1069 if ( save_position )
1070 update_centers ( vvp );
50a14534
EB
1071 if ( vvp->coord_mode == VIK_COORD_UTM )
1072 viewport_utm_zone_check ( vvp );
1073}
1074
be5554c5
RN
1075/**
1076 * vik_viewport_set_center_utm:
1077 * @vvp: The viewport to reposition.
1078 * @utm: The new center position in UTM format
1079 * @save_position: Whether this new position should be saved into the history of positions
1080 * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions)
1081 */
1082void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm, gboolean save_position )
50a14534
EB
1083{
1084 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
be5554c5
RN
1085 if ( save_position )
1086 update_centers ( vvp );
50a14534
EB
1087 if ( vvp->coord_mode == VIK_COORD_UTM )
1088 viewport_utm_zone_check ( vvp );
1089}
1090
be5554c5
RN
1091/**
1092 * vik_viewport_set_center_coord:
1093 * @vvp: The viewport to reposition.
1094 * @coord: The new center position in a VikCoord type
1095 * @save_position: Whether this new position should be saved into the history of positions
1096 * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions)
1097 */
1098void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord, gboolean save_position )
50a14534
EB
1099{
1100 vvp->center = *coord;
be5554c5
RN
1101 if ( save_position )
1102 update_centers ( vvp );
50a14534
EB
1103 if ( vvp->coord_mode == VIK_COORD_UTM )
1104 viewport_utm_zone_check ( vvp );
1105}
1106
1107void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
1108{
1109 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
1110
1111 /* get center, then just offset */
1112 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
1113 ul->mode = VIK_COORD_UTM;
1114 *br = *ul;
1115
1116 ul->north_south += (vvp->ympp * vvp->height / 2);
1117 ul->east_west -= (vvp->xmpp * vvp->width / 2);
1118 br->north_south -= (vvp->ympp * vvp->height / 2);
1119 br->east_west += (vvp->xmpp * vvp->width / 2);
1120}
1121
1122void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
1123{
1124 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1125 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
1126 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
1127 center->zone = zone;
1128 }
1129}
1130
1131gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
1132{
1133 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1134 VikCoord coord;
1135 g_assert ( vvp != NULL );
1136 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
1137 return coord.utm_zone;
1138 }
1139 return '\0';
1140}
1141
1142gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
1143{
1144 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1145 VikCoord coord;
1146 g_assert ( vvp != NULL );
1147 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
1148 return coord.utm_zone;
1149 }
1150 return '\0';
1151}
1152
1153
1154void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
1155{
1156 g_return_if_fail ( vvp != NULL );
1157 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1158 /* slightly optimized */
1159 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
1160 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
1161 viewport_utm_zone_check ( vvp );
1162 } else {
1163 VikCoord tmp;
1164 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
be5554c5 1165 vik_viewport_set_center_coord ( vvp, &tmp, FALSE );
50a14534
EB
1166 }
1167}
1168
1169gint vik_viewport_get_width( VikViewport *vvp )
1170{
1171 g_return_val_if_fail ( vvp != NULL, 0 );
1172 return vvp->width;
1173}
1174
1175gint vik_viewport_get_height( VikViewport *vvp )
1176{
1177 g_return_val_if_fail ( vvp != NULL, 0 );
1178 return vvp->height;
1179}
1180
1181void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
1182{
c53eda9a
GB
1183 g_return_if_fail ( vvp != NULL );
1184
50a14534
EB
1185 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1186 int zone_delta;
1187 struct UTM *utm = (struct UTM *) coord;
1188 coord->mode = VIK_COORD_UTM;
1189
50a14534
EB
1190 utm->zone = vvp->center.utm_zone;
1191 utm->letter = vvp->center.utm_letter;
a3603298 1192 utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west;
50a14534
EB
1193 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
1194 utm->zone += zone_delta;
1195 utm->easting -= zone_delta * vvp->utm_zone_width;
a3603298 1196 utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south;
50a14534
EB
1197 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
1198 coord->mode = VIK_COORD_LATLON;
d587678a 1199 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
a3603298
RN
1200 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
1201 coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y));
d587678a 1202 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
a3603298 1203 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);
fd98b156 1204 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
a3603298
RN
1205 /* This isn't called with a high frequently so less need to optimize */
1206 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
1207 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) );
50a14534
EB
1208 }
1209 }
1210}
1211
a3603298
RN
1212/*
1213 * Since this function is used for every drawn trackpoint - it can get called alot
1214 * Thus x & y position factors are calculated once on zoom changes,
1215 * avoiding the need to do it here all the time.
1216 * For good measure the half width and height values are also pre calculated too.
1217 */
50a14534
EB
1218void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
1219{
1220 static VikCoord tmp;
1221 g_return_if_fail ( vvp != NULL );
1222
1223 if ( coord->mode != vvp->coord_mode )
1224 {
1225 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
1226 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
1227 coord = &tmp;
1228 }
1229
1230 if ( vvp->coord_mode == VIK_COORD_UTM ) {
1231 struct UTM *center = (struct UTM *) &(vvp->center);
1232 struct UTM *utm = (struct UTM *) coord;
1233 if ( center->zone != utm->zone && vvp->one_utm_zone )
1234 {
1235 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
1236 return;
1237 }
1238
a3603298 1239 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) -
50a14534 1240 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
a3603298 1241 *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp );
50a14534
EB
1242 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
1243 struct LatLon *center = (struct LatLon *) &(vvp->center);
1244 struct LatLon *ll = (struct LatLon *) coord;
1245 double xx,yy;
d587678a 1246 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
a3603298
RN
1247 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
1248 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) );
d587678a 1249 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
a3603298 1250 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 );
50a14534 1251 *x = xx; *y = yy;
50a14534 1252 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
a3603298
RN
1253 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
1254 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) );
50a14534
EB
1255 }
1256 }
1257}
1258
5d325845
RN
1259// Clip functions continually reduce the value by a factor until it is in the acceptable range
1260// whilst also scaling the other coordinate value.
1261static void clip_x ( gint *x1, gint *y1, gint *x2, gint *y2 )
d9ffd267 1262{
5d325845
RN
1263 while ( ABS(*x1) > 32768 ) {
1264 *x1 = *x2 + (0.5 * (*x1-*x2));
1265 *y1 = *y2 + (0.5 * (*y1-*y2));
d9ffd267
EB
1266 }
1267}
1268
5d325845
RN
1269static void clip_y ( gint *x1, gint *y1, gint *x2, gint *y2 )
1270{
1271 while ( ABS(*y1) > 32767 ) {
1272 *x1 = *x2 + (0.5 * (*x1-*x2));
1273 *y1 = *y2 + (0.5 * (*y1-*y2));
1274 }
1275}
1276
1277/**
1278 * a_viewport_clip_line:
1279 * @x1: screen coord
1280 * @y1: screen coord
1281 * @x2: screen coord
1282 * @y2: screen coord
1283 *
1284 * Due to the seemingly undocumented behaviour of gdk_draw_line(), we need to restrict the range of values passed in.
1285 * So despite it accepting gints, the effective range seems to be the actually the minimum C int range (2^16).
1286 * This seems to be limitations coming from the X Window System.
1287 *
1288 * See http://www.rahul.net/kenton/40errs.html
1289 * ERROR 7. Boundary conditions.
1290 * "The X coordinate space is not infinite.
1291 * Most drawing functions limit position, width, and height to 16 bit integers (sometimes signed, sometimes unsigned) of accuracy.
1292 * Because most C compilers use 32 bit integers, Xlib will not complain if you exceed the 16 bit limit, but your results will usually not be what you expected.
1293 * You should be especially careful of this if you are implementing higher level scalable graphics packages."
1294 *
1295 * This function should be called before calling gdk_draw_line().
1296 */
1297void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
1298{
1299 if ( *x1 > 32768 || *x1 < -32767 )
1300 clip_x ( x1, y1, x2, y2 );
1301 if ( *y1 > 32768 || *y1 < -32767 )
1302 clip_y ( x1, y1, x2, y2 );
1303 if ( *x2 > 32768 || *x2 < -32767 )
1304 clip_x ( x2, y2, x1, y1 );
1305 if ( *y2 > 32768 || *y2 < -32767 )
1306 clip_y ( x2, y2, x1, y1 );
1307}
1308
50a14534
EB
1309void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
1310{
1311 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
d9ffd267
EB
1312 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
1313 /*** clipping, yeah! ***/
1314 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
1315 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
1316 }
50a14534
EB
1317}
1318
1319void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
1320{
add30ebe
RN
1321 // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
1322 if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 )
50a14534
EB
1323 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
1324}
1325
1326void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
1327{
1328 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
1329 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
1330}
1331
1332/* shouldn't use this -- slow -- change the alpha channel instead. */
1333void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
1334 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
1335{
1336 gint real_dest_x = MAX(dest_x,0);
1337 gint real_dest_y = MAX(dest_y,0);
1338
1339 if ( alpha == 0 )
1340 return; /* don't waste your time */
1341
1342 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
1343 {
1344 if ( vvp->alpha_pixbuf )
1345 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
1346 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
1347 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
1348 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
1349 }
1350
1351 w = MIN(w,vvp->width - dest_x);
1352 h = MIN(h,vvp->height - dest_y);
1353
1354 /* check that we are drawing within boundaries. */
1355 src_x += (real_dest_x - dest_x);
1356 src_y += (real_dest_y - dest_y);
1357 w -= (real_dest_x - dest_x);
1358 h -= (real_dest_y - dest_y);
1359
1360 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
1361 real_dest_x, real_dest_y, 0, 0, w, h );
1362
1363 /* do a composite */
1364 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
1365
1366 /* draw pixbuf_tmp */
1367 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
1368}
1369
1370void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
1371 gint dest_x, gint dest_y, gint w, gint h )
1372{
1373 gdk_draw_pixbuf ( vvp->scr_buffer,
ff37db21
RN
1374 NULL,
1375 pixbuf,
50a14534
EB
1376 src_x, src_y, dest_x, dest_y, w, h,
1377 GDK_RGB_DITHER_NONE, 0, 0 );
1378}
1379
1380void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
1381{
1382 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
1383}
1384
1385
1386void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
1387{
1388 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
1389}
1390
1391VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
1392{
1393 g_assert ( vvp );
1394 return vvp->coord_mode;
1395}
1396
1397static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
1398{
1399 g_return_if_fail ( vvp != NULL );
1400 vvp->coord_mode = mode;
1401 vik_coord_convert ( &(vvp->center), mode );
1402}
1403
1404/* Thanks GPSDrive */
1405static 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 )
1406{
1407 int px, py;
1408 gdouble dif, lat, lon;
1409 double Ra = Radius[90+(gint)zero_lat];
1410
1411 px = (mapSizeX2 - x) * pixelfact_x;
1412 py = (-mapSizeY2 + y) * pixelfact_y;
1413
1414 lat = zero_lat - py / Ra;
1415 lat = zero_lat - py / Ra;
1416 lon =
1417 zero_long -
1418 px / (Ra *
81765f5e 1419 cos (DEG2RAD(lat)));
50a14534 1420
81765f5e 1421 dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
50a14534
EB
1422 lat = lat - dif / 1.5;
1423 lon =
1424 zero_long -
1425 px / (Ra *
81765f5e 1426 cos (DEG2RAD(lat)));
50a14534
EB
1427
1428 *lt = lat;
1429 *lg = lon;
1430 return (TRUE);
1431}
1432
1433/* Thanks GPSDrive */
1434static 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 )
1435{
1436 double dif;
1437 double Ra;
1438 gint mapSizeX = 2 * mapSizeX2;
1439 gint mapSizeY = 2 * mapSizeY2;
1440
1441 g_assert ( lt >= -90.0 && lt <= 90.0 );
1442// lg *= rad2deg; // FIXME, optimize equations
1443// lt *= rad2deg;
1444 Ra = Radius[90+(gint)lt];
1445 *x = Ra *
81765f5e 1446 cos (DEG2RAD(lt)) * (lg - zero_long);
50a14534 1447 *y = Ra * (lt - zero_lat);
81765f5e 1448 dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
50a14534
EB
1449 *y = *y + dif / 1.85;
1450 *x = *x / pixelfact_x;
1451 *y = *y / pixelfact_y;
1452 *x = mapSizeX2 - *x;
1453 *y += mapSizeY2;
1454 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
1455 return (FALSE);
1456 return (TRUE);
1457}
1458
1459static void viewport_init_ra()
1460{
1461 static gboolean done_before = FALSE;
1462 if ( !done_before )
1463 {
1464 gint i;
1465 for ( i = -90; i <= 90; i++)
81765f5e 1466 Radius[i+90] = calcR ( DEG2RAD((double)i) );
50a14534
EB
1467 done_before = TRUE;
1468 }
1469}
1470
1471double calcR (double lat)
1472{
1473 double a = 6378.137, r, sc, x, y, z;
1474 double e2 = 0.081082 * 0.081082;
1475 /*
1476 * the radius of curvature of an ellipsoidal Earth in the plane of the
1477 * meridian is given by
1478 *
1479 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
1480 *
1481 *
1482 * where a is the equatorial radius, b is the polar radius, and e is
1483 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
1484 *
1485 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
1486 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
1487 * = 0.081082 Eccentricity
1488 */
1489
81765f5e 1490 lat = DEG2RAD(lat);
50a14534
EB
1491 sc = sin (lat);
1492 x = a * (1.0 - e2);
1493 z = 1.0 - e2 * sc * sc;
1494 y = pow (z, 1.5);
1495 r = x / y;
1496 r = r * 1000.0;
1497 return r;
1498}
1499
1500gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1501{
1502 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1503}
1504
1505void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1506{
1507 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1508 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1509}
1510
1511void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1512{
1513 static GdkGCValues values;
1514 gdk_gc_get_values ( gc, &values );
1515 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1516}
1517
1518GdkFunction vik_gc_get_function ( GdkGC *gc )
1519{
1520 static GdkGCValues values;
1521 gdk_gc_get_values ( gc, &values );
1522 return values.function;
1523}
1524
1525void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1526{
1527 vvp->drawmode = drawmode;
1528 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1529 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1530 else {
1531 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
50a14534
EB
1532 }
1533}
1534
1535VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1536{
1537 return vvp->drawmode;
1538}
1539
0df66d57
EB
1540/******** triggering *******/
1541void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1542{
1543 vp->trigger = trigger;
1544}
1545
1546gpointer vik_viewport_get_trigger ( VikViewport *vp )
1547{
1548 return vp->trigger;
1549}
1550
1551void vik_viewport_snapshot_save ( VikViewport *vp )
1552{
1553 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1554}
1555
1556void vik_viewport_snapshot_load ( VikViewport *vp )
1557{
1558 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1559}
1560
1561void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1562{
1563 vp->half_drawn = half_drawn;
1564}
1565
1566gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1567{
1568 return vp->half_drawn;
1569}
1570
7bc965c0
GB
1571
1572const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
0c1044e9 1573 {
7bc965c0 1574 const gchar *name = NULL;
24277274 1575 VikWindow *vw = NULL;
7bc965c0
GB
1576 GtkWidget *mode_button;
1577 GtkWidget *label;
24277274
GB
1578
1579 vw = VIK_WINDOW_FROM_WIDGET(vv);
1580 mode_button = vik_window_get_drawmode_button(vw, mode);
7bc965c0
GB
1581 label = gtk_bin_get_child(GTK_BIN(mode_button));
1582
1583 name = gtk_label_get_text ( GTK_LABEL(label) );
314c1ccc 1584
7bc965c0 1585 return name;
314c1ccc
QT
1586
1587}
7bc965c0 1588
0c1044e9
EB
1589void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1590{
1591 VikCoord tleft, tright, bleft, bright;
1592
1593 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1594 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1595 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1596 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
7bc965c0 1597
0c1044e9
EB
1598 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1599 vik_coord_convert(&tright, VIK_COORD_LATLON);
1600 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1601 vik_coord_convert(&bright, VIK_COORD_LATLON);
1602
1603 *max_lat = MAX(tleft.north_south, tright.north_south);
1604 *min_lat = MIN(bleft.north_south, bright.north_south);
1605 *max_lon = MAX(tright.east_west, bright.east_west);
1606 *min_lon = MIN(tleft.east_west, bleft.east_west);
1607}
82aa018d
GB
1608
1609void vik_viewport_reset_copyrights ( VikViewport *vp )
1610{
1611 g_return_if_fail ( vp != NULL );
1612 g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
1613 g_slist_free ( vp->copyrights );
1614 vp->copyrights = NULL;
1615}
1616
33e0e500
GB
1617/**
1618 * vik_viewport_add_copyright:
1619 * @vp: self object
1620 * @copyright: new copyright to display
1621 *
1622 * Add a copyright to display on viewport.
1623 */
82aa018d
GB
1624void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright )
1625{
1626 g_return_if_fail ( vp != NULL );
1627 if ( copyright )
1628 {
bbbd639f 1629 GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
82aa018d
GB
1630 if ( found == NULL )
1631 {
1632 gchar *duple = g_strdup ( copyright );
1633 vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
1634 }
1635 }
1636}
26336cf0
GB
1637
1638void vik_viewport_reset_logos ( VikViewport *vp )
1639{
1640 g_return_if_fail ( vp != NULL );
1641 /* do not free elem */
1642 g_slist_free ( vp->logos );
1643 vp->logos = NULL;
1644}
1645
1646void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
1647{
1648 g_return_if_fail ( vp != NULL );
1649 if ( logo )
1650 {
1651 GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
1652 if ( found == NULL )
1653 {
1654 vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
1655 }
1656 }
1657}
9a3538f5
GB
1658
1659/**
1660 * vik_viewport_compute_bearing:
1661 * @vp: self object
1662 * @x1: screen coord
1663 * @y1: screen coord
1664 * @x2: screen coord
1665 * @y2: screen coord
1666 * @angle: bearing in Radian (output)
1667 * @baseangle: UTM base angle in Radian (output)
1668 *
1669 * Compute bearing.
1670 */
1671void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
1672{
1673 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
1674 gdouble dx = (x2-x1)/len*10;
1675 gdouble dy = (y2-y1)/len*10;
1676
1677 *angle = atan2(dy, dx) + M_PI_2;
1678
1679 if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
1680 VikCoord test;
1681 struct LatLon ll;
1682 struct UTM u;
1683 gint tx, ty;
1684
1685 vik_viewport_screen_to_coord ( vp, x1, y1, &test );
1686 vik_coord_to_latlon ( &test, &ll );
1687 ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude
1688 a_coords_latlon_to_utm ( &ll, &u );
1689 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
1690 vik_viewport_coord_to_screen ( vp, &test, &tx, &ty );
1691
1692 *baseangle = M_PI - atan2(tx-x1, ty-y1);
1693 *angle -= *baseangle;
1694 }
1695
1696 if (*angle < 0)
1697 *angle += 2*M_PI;
1698 if (*angle > 2*M_PI)
1699 *angle -= 2*M_PI;
1700}