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