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