]> git.street.me.uk Git - andy/viking.git/blame - src/vikviewport.c
Ensure highlight for a single track or waypoint is always shown on top.
[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>
50a14534
EB
5 *
6 * Lat/Lon plotting functions calcxy* are from GPSDrive
7 * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
8 *
9 * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
8c00358d
GB
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
50a14534
EB
29
30#define DEFAULT_BACKGROUND_COLOR "#CCCCCC"
480fb7e1
RN
31#define DEFAULT_HIGHLIGHT_COLOR "#EEA500"
32/* Default highlight in orange */
50a14534
EB
33
34#include <gtk/gtk.h>
8c00358d 35#ifdef HAVE_MATH_H
50a14534 36#include <math.h>
8c00358d 37#endif
82aa018d
GB
38#ifdef HAVE_STRING_H
39#include <string.h>
40#endif
50a14534
EB
41
42#include "coords.h"
43#include "vikcoord.h"
a59e6eb9 44#include "vikwindow.h"
50a14534 45#include "vikviewport.h"
50a14534
EB
46#include "mapcoord.h"
47
48/* for ALTI_TO_MPP */
49#include "globals.h"
7143d1e4 50#include "settings.h"
50a14534 51
a3603298
RN
52#define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 )
53
50a14534
EB
54static gdouble EASTING_OFFSET = 500000.0;
55
26336cf0
GB
56static gint PAD = 10;
57
50a14534
EB
58static void viewport_finalize ( GObject *gob );
59static void viewport_utm_zone_check ( VikViewport *vvp );
60
61static 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 );
62static 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 );
63double calcR (double lat);
64
65static double Radius[181];
66static void viewport_init_ra();
67
68static GObjectClass *parent_class;
69
50a14534
EB
70
71struct _VikViewport {
72 GtkDrawingArea drawing_area;
73 GdkPixmap *scr_buffer;
74 gint width, height;
a3603298 75 gint width_2, height_2; // Half of the normal width and height
50a14534
EB
76 VikCoord center;
77 VikCoordMode coord_mode;
78 gdouble xmpp, ympp;
a3603298 79 gdouble xmfactor, ymfactor;
50a14534
EB
80
81 GdkPixbuf *alpha_pixbuf;
82 guint8 alpha_pixbuf_width;
83 guint8 alpha_pixbuf_height;
84
85 gdouble utm_zone_width;
86 gboolean one_utm_zone;
87
88 GdkGC *background_gc;
89 GdkColor background_color;
4e485bf3 90 GdkGC *scale_bg_gc;
82aa018d
GB
91
92 GSList *copyrights;
26336cf0 93 GSList *logos;
82aa018d
GB
94
95 /* Wether or not display OSD info */
35c7c0ba 96 gboolean draw_scale;
c933487f 97 gboolean draw_centermark;
2afcef36 98 gboolean draw_highlight;
480fb7e1
RN
99 GdkGC *highlight_gc;
100 GdkColor highlight_color;
50a14534
EB
101
102 /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */
103 VikViewportDrawMode drawmode;
104
0df66d57
EB
105 /* trigger stuff */
106 gpointer trigger;
107 GdkPixmap *snapshot_buffer;
108 gboolean half_drawn;
50a14534
EB
109};
110
111static gdouble
112viewport_utm_zone_width ( VikViewport *vvp )
113{
114 if ( vvp->coord_mode == VIK_COORD_UTM ) {
115 struct LatLon ll;
116
117 /* get latitude of screen bottom */
118 struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp )));
119 utm.northing -= vvp -> height * vvp -> ympp / 2;
120 a_coords_utm_to_latlon ( &utm, &ll );
121
122 /* boundary */
123 ll.lon = (utm.zone - 1) * 6 - 180 ;
124 a_coords_latlon_to_utm ( &ll, &utm);
125 return fabs ( utm.easting - EASTING_OFFSET ) * 2;
126 } else
127 return 0.0;
128}
129
eb560cd6 130G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA)
50a14534 131
eb560cd6
GB
132static void
133vik_viewport_class_init ( VikViewportClass *klass )
50a14534
EB
134{
135 /* Destructor */
136 GObjectClass *object_class;
137
138 object_class = G_OBJECT_CLASS (klass);
139
140 object_class->finalize = viewport_finalize;
141
142 parent_class = g_type_class_peek_parent (klass);
143}
144
24277274 145VikViewport *vik_viewport_new ()
50a14534 146{
a59e6eb9 147 VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) );
a59e6eb9 148 return vv;
50a14534
EB
149}
150
7143d1e4
RN
151#define VIK_SETTINGS_VIEW_LAST_LATITUDE "viewport_last_latitude"
152#define VIK_SETTINGS_VIEW_LAST_LONGITUDE "viewport_last_longitude"
153#define VIK_SETTINGS_VIEW_LAST_ZOOM_X "viewport_last_zoom_xpp"
154#define VIK_SETTINGS_VIEW_LAST_ZOOM_Y "viewport_last_zoom_ypp"
155
eb560cd6
GB
156static void
157vik_viewport_init ( VikViewport *vvp )
50a14534
EB
158{
159 viewport_init_ra();
160
5210c3d3
GB
161 struct UTM utm;
162 struct LatLon ll;
163 ll.lat = a_vik_get_default_lat();
164 ll.lon = a_vik_get_default_long();
7143d1e4
RN
165 gdouble zoom_x = 4.0;
166 gdouble zoom_y = 4.0;
167
168 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
169 gdouble lat, lon, dzoom;
170 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, &lat ) )
171 ll.lat = lat;
172 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, &lon ) )
173 ll.lon = lon;
174 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, &dzoom ) )
175 zoom_x = dzoom;
176 if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, &dzoom ) )
177 zoom_y = dzoom;
178 }
179
5210c3d3
GB
180 a_coords_latlon_to_utm ( &ll, &utm );
181
7143d1e4
RN
182 vvp->xmpp = zoom_x;
183 vvp->ympp = zoom_y;
a3603298
RN
184 vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp);
185 vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp);
6a9ff0ee
QT
186 vvp->coord_mode = VIK_COORD_LATLON;
187 vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
ee180665 188 vvp->center.mode = VIK_COORD_LATLON;
5210c3d3
GB
189 vvp->center.north_south = ll.lat;
190 vvp->center.east_west = ll.lon;
191 vvp->center.utm_zone = (int)utm.zone;
192 vvp->center.utm_letter = utm.letter;
50a14534
EB
193 vvp->scr_buffer = NULL;
194 vvp->alpha_pixbuf = NULL;
195 vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0;
196 vvp->utm_zone_width = 0.0;
197 vvp->background_gc = NULL;
480fb7e1 198 vvp->highlight_gc = NULL;
4e485bf3 199 vvp->scale_bg_gc = NULL;
82aa018d
GB
200
201 vvp->copyrights = NULL;
202
35c7c0ba 203 vvp->draw_scale = TRUE;
c933487f 204 vvp->draw_centermark = TRUE;
2afcef36 205 vvp->draw_highlight = TRUE;
0df66d57
EB
206
207 vvp->trigger = NULL;
208 vvp->snapshot_buffer = NULL;
209 vvp->half_drawn = FALSE;
210
50a14534 211 g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL);
165d30aa 212
2fd7c35e
RN
213#if GTK_CHECK_VERSION (2,18,0)
214 gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE );
215#else
165d30aa 216 GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */
2fd7c35e 217#endif
50a14534
EB
218}
219
220GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp )
221{
222 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
223 *rv = vvp->background_color;
224 return rv;
225}
226
227/* returns pointer to internal static storage, changes next time function called, use quickly */
228const gchar *vik_viewport_get_background_color ( VikViewport *vvp )
229{
230 static gchar color[8];
231 g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->background_color.red/256),(int)(vvp->background_color.green/256),(int)(vvp->background_color.blue/256));
232 return color;
233}
234
235void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname )
236{
52aa15a7 237 g_assert ( vvp && vvp->background_gc );
b244bc6a
GB
238 if ( gdk_color_parse ( colorname, &(vvp->background_color) ) )
239 gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) );
240 else
241 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
50a14534
EB
242}
243
244void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color )
245{
52aa15a7 246 g_assert ( vvp && vvp->background_gc );
50a14534
EB
247 vvp->background_color = *color;
248 gdk_gc_set_rgb_fg_color ( vvp->background_gc, color );
249}
250
480fb7e1
RN
251GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp )
252{
253 GdkColor *rv = g_malloc ( sizeof ( GdkColor ) );
254 *rv = vvp->highlight_color;
255 return rv;
256}
257
258/* returns pointer to internal static storage, changes next time function called, use quickly */
259const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp )
260{
261 static gchar color[8];
262 g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->highlight_color.red/256),(int)(vvp->highlight_color.green/256),(int)(vvp->highlight_color.blue/256));
263 return color;
264}
265
266void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname )
267{
268 g_assert ( vvp->highlight_gc );
269 gdk_color_parse ( colorname, &(vvp->highlight_color) );
270 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) );
271}
272
273void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color )
274{
275 g_assert ( vvp->highlight_gc );
276 vvp->highlight_color = *color;
277 gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color );
278}
279
280GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp )
281{
282 return vvp->highlight_gc;
283}
50a14534 284
04f36d92
RN
285void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness )
286{
287 // Otherwise same GDK_* attributes as in vik_viewport_new_gc
288 gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
289}
290
50a14534
EB
291GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness )
292{
b244bc6a 293 GdkGC *rv = NULL;
50a14534
EB
294 GdkColor color;
295
9b082b39 296 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
b244bc6a
GB
297 if ( gdk_color_parse ( colorname, &color ) )
298 gdk_gc_set_rgb_fg_color ( rv, &color );
299 else
300 g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname);
50a14534
EB
301 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
302 return rv;
303}
304
305GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness )
306{
307 GdkGC *rv;
308
9b082b39 309 rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) );
50a14534
EB
310 gdk_gc_set_rgb_fg_color ( rv, color );
311 gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND );
312 return rv;
313}
314
315void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height )
316{
317 vvp->width = width;
318 vvp->height = height;
a3603298
RN
319
320 vvp->width_2 = vvp->width/2;
321 vvp->height_2 = vvp->height/2;
322
50a14534
EB
323 if ( vvp->scr_buffer )
324 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
9b082b39 325 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
0df66d57
EB
326
327 /* TODO trigger: only if this is enabled !!! */
328 if ( vvp->snapshot_buffer )
329 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
9b082b39 330 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
50a14534
EB
331}
332
333
334GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp )
335{
336 return vvp->scr_buffer;
337}
338
339gboolean vik_viewport_configure ( VikViewport *vvp )
340{
341 g_return_val_if_fail ( vvp != NULL, TRUE );
342
90ba111c
RN
343 GtkAllocation allocation;
344 gtk_widget_get_allocation ( GTK_WIDGET(vvp), &allocation );
345 vvp->width = allocation.width;
346 vvp->height = allocation.height;
50a14534 347
a3603298
RN
348 vvp->width_2 = vvp->width/2;
349 vvp->height_2 = vvp->height/2;
350
50a14534
EB
351 if ( vvp->scr_buffer )
352 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
353
9b082b39 354 vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
50a14534 355
0df66d57
EB
356 /* TODO trigger: only if enabled! */
357 if ( vvp->snapshot_buffer )
358 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
359
9b082b39 360 vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 );
0df66d57
EB
361 /* TODO trigger */
362
50a14534 363 /* this is down here so it can get a GC (necessary?) */
e8e82387 364 if ( !vvp->background_gc )
50a14534 365 {
348e8666 366 vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 );
e8e82387 367 vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR );
50a14534 368 }
480fb7e1
RN
369 if ( ! vvp->highlight_gc )
370 {
371 vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 );
372 vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR );
373 }
4e485bf3
QT
374 if ( !vvp->scale_bg_gc) {
375 vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3);
376 }
50a14534
EB
377
378 return FALSE;
379}
380
381static void viewport_finalize ( GObject *gob )
382{
383 VikViewport *vvp = VIK_VIEWPORT(gob);
384
385 g_return_if_fail ( vvp != NULL );
386
7143d1e4
RN
387 if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) {
388 struct LatLon ll;
389 vik_coord_to_latlon ( &(vvp->center), &ll );
390 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, ll.lat );
391 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, ll.lon );
392 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, vvp->xmpp );
393 a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, vvp->ympp );
394 }
395
50a14534
EB
396 if ( vvp->scr_buffer )
397 g_object_unref ( G_OBJECT ( vvp->scr_buffer ) );
398
0df66d57
EB
399 if ( vvp->snapshot_buffer )
400 g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) );
401
50a14534
EB
402 if ( vvp->alpha_pixbuf )
403 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
404
405 if ( vvp->background_gc )
406 g_object_unref ( G_OBJECT ( vvp->background_gc ) );
407
480fb7e1
RN
408 if ( vvp->highlight_gc )
409 g_object_unref ( G_OBJECT ( vvp->highlight_gc ) );
410
4e485bf3
QT
411 if ( vvp->scale_bg_gc ) {
412 g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) );
413 vvp->scale_bg_gc = NULL;
414 }
415
50a14534
EB
416 G_OBJECT_CLASS(parent_class)->finalize(gob);
417}
418
33e0e500
GB
419/**
420 * vik_viewport_clear:
421 * @vvp: self object
422 *
423 * Clear the whole viewport.
424 */
50a14534
EB
425void vik_viewport_clear ( VikViewport *vvp )
426{
427 g_return_if_fail ( vvp != NULL );
428 gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height);
82aa018d 429 vik_viewport_reset_copyrights ( vvp );
26336cf0 430 vik_viewport_reset_logos ( vvp );
50a14534
EB
431}
432
33e0e500
GB
433/**
434 * vik_viewport_set_draw_scale:
435 * @vvp: self
436 * @draw_scale: new value
437 *
438 * Enable/Disable display of scale.
439 */
35c7c0ba
EB
440void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
441{
442 vvp->draw_scale = draw_scale;
443}
444
445gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
446{
447 return vvp->draw_scale;
448}
449
acaf7113
AF
450void vik_viewport_draw_scale ( VikViewport *vvp )
451{
c53eda9a
GB
452 g_return_if_fail ( vvp != NULL );
453
35c7c0ba
EB
454 if ( vvp->draw_scale ) {
455 VikCoord left, right;
456 gdouble unit, base, diff, old_unit, old_diff, ratio;
26336cf0 457 gint odd, len, SCSIZE = 5, HEIGHT=10;
35c7c0ba
EB
458 PangoLayout *pl;
459 gchar s[128];
acaf7113 460
35c7c0ba
EB
461 vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
462 vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
acaf7113 463
6f9336aa
RN
464 vik_units_distance_t dist_units = a_vik_get_units_distance ();
465 switch (dist_units) {
466 case VIK_UNITS_DISTANCE_KILOMETRES:
467 base = vik_coord_diff ( &left, &right ); // in meters
468 break;
469 case VIK_UNITS_DISTANCE_MILES:
8d772d8a 470 // in 0.1 miles (copes better when zoomed in as 1 mile can be too big)
433b3f7f 471 base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0;
6f9336aa
RN
472 break;
473 default:
474 base = 1; // Keep the compiler happy
475 g_critical("Houston, we've had a problem. distance=%d", dist_units);
476 }
35c7c0ba 477 ratio = (vvp->width/SCSIZE)/base;
acaf7113 478
35c7c0ba
EB
479 unit = 1;
480 diff = fabs(base-unit);
acaf7113
AF
481 old_unit = unit;
482 old_diff = diff;
35c7c0ba
EB
483 odd = 1;
484 while (diff <= old_diff) {
485 old_unit = unit;
486 old_diff = diff;
487 unit = unit * (odd%2 ? 5 : 2);
488 diff = fabs(base-unit);
489 odd++;
490 }
491 unit = old_unit;
492 len = unit * ratio;
acaf7113 493
4e485bf3
QT
494 /* white background */
495 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
496 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
497 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
498 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
499 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
500 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
501 /* black scale */
ff37db21 502 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 503 PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
ff37db21 504 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 505 PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
ff37db21 506 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 507 PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
35c7c0ba
EB
508 if (odd%2) {
509 int i;
510 for (i=1; i<5; i++) {
4e485bf3
QT
511 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
512 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
ff37db21 513 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 514 PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
35c7c0ba
EB
515 }
516 } else {
517 int i;
518 for (i=1; i<10; i++) {
4e485bf3
QT
519 vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
520 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
ff37db21 521 vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
35c7c0ba
EB
522 PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
523 }
acaf7113 524 }
35c7c0ba 525 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
ff37db21 526 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
35c7c0ba 527
6f9336aa
RN
528 switch (dist_units) {
529 case VIK_UNITS_DISTANCE_KILOMETRES:
530 if (unit >= 1000) {
531 sprintf(s, "%d km", (int)unit/1000);
532 } else {
533 sprintf(s, "%d m", (int)unit);
534 }
535 break;
536 case VIK_UNITS_DISTANCE_MILES:
8d772d8a
RN
537 // Handle units in 0.1 miles
538 if (unit < 10.0) {
539 sprintf(s, "%0.1f miles", unit/10.0);
540 }
541 else if ((int)unit == 10.0) {
542 sprintf(s, "1 mile");
6f9336aa
RN
543 }
544 else {
8d772d8a 545 sprintf(s, "%d miles", (int)(unit/10.0));
6f9336aa
RN
546 }
547 break;
548 default:
549 g_critical("Houston, we've had a problem. distance=%d", dist_units);
acaf7113 550 }
35c7c0ba 551 pango_layout_set_text(pl, s, -1);
ff37db21 552 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
acaf7113 553 PAD + len + PAD, vvp->height - PAD - 10, pl);
6278ccc6 554 g_object_unref(pl);
e6869c62 555 pl = NULL;
35c7c0ba 556 }
acaf7113
AF
557}
558
82aa018d
GB
559void vik_viewport_draw_copyright ( VikViewport *vvp )
560{
561 g_return_if_fail ( vvp != NULL );
562
82aa018d
GB
563 PangoLayout *pl;
564 PangoRectangle ink_rect, logical_rect;
565 gchar s[128] = "";
566
567 /* compute copyrights string */
568 guint len = g_slist_length ( vvp->copyrights );
15a0bd14 569
82aa018d
GB
570 int i;
571 for (i = 0 ; i < len ; i++)
572 {
15a0bd14
RN
573 // Stop when buffer is full
574 int slen = strlen ( s );
575 if ( slen >= 127 )
576 break;
577
82aa018d 578 gchar *copyright = g_slist_nth_data ( vvp->copyrights, i );
15a0bd14
RN
579
580 // Only use part of this copyright that fits in the available space left
581 // remembering 1 character is left available for the appended space
582 int clen = strlen ( copyright );
583 if ( slen + clen > 126 ) {
584 clen = 126 - slen;
585 }
586
587 strncat ( s, copyright, clen );
82aa018d
GB
588 strcat ( s, " " );
589 }
590
591 /* create pango layout */
592 pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
ff37db21 593 pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc);
82aa018d
GB
594 pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT );
595
596 /* Set the text */
597 pango_layout_set_text(pl, s, -1);
598
599 /* Use maximum of half the viewport width */
56cb1807 600 pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE );
82aa018d 601 pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect);
ff37db21 602 vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc,
56cb1807 603 vvp->width / 2, vvp->height - logical_rect.height, pl);
82aa018d
GB
604
605 /* Free memory */
606 g_object_unref(pl);
607 pl = NULL;
608}
609
33e0e500
GB
610/**
611 * vik_viewport_set_draw_centermark:
612 * @vvp: self object
613 * @draw_centermark: new value
614 *
615 * Enable/Disable display of center mark.
616 */
c933487f
QT
617void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
618{
619 vvp->draw_centermark = draw_centermark;
620}
621
622gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
623{
624 return vvp->draw_centermark;
625}
626
627void vik_viewport_draw_centermark ( VikViewport *vvp )
628{
c53eda9a
GB
629 g_return_if_fail ( vvp != NULL );
630
c933487f
QT
631 if ( !vvp->draw_centermark )
632 return;
633
3df69a6b
QT
634 const int len = 30;
635 const int gap = 4;
c933487f
QT
636 int center_x = vvp->width/2;
637 int center_y = vvp->height/2;
ff37db21 638 GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc;
c933487f
QT
639
640 /* white back ground */
3df69a6b
QT
641 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
642 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
643 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
644 vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
c933487f 645 /* black fore ground */
3df69a6b
QT
646 vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
647 vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
648 vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
649 vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
c933487f
QT
650
651}
652
26336cf0
GB
653void vik_viewport_draw_logo ( VikViewport *vvp )
654{
655 g_return_if_fail ( vvp != NULL );
656
26336cf0
GB
657 guint len = g_slist_length ( vvp->logos );
658 gint x = vvp->width - PAD;
659 gint y = PAD;
660 int i;
661 for (i = 0 ; i < len ; i++)
662 {
663 GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i );
664 gint width = gdk_pixbuf_get_width ( logo );
665 gint height = gdk_pixbuf_get_height ( logo );
666 vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height );
667 x = x - width - PAD;
668 }
2afcef36 669}
26336cf0 670
2afcef36
RN
671void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight )
672{
673 vvp->draw_highlight = draw_highlight;
674}
675
676gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp )
677{
678 return vvp->draw_highlight;
26336cf0
GB
679}
680
50a14534
EB
681void vik_viewport_sync ( VikViewport *vvp )
682{
683 g_return_if_fail ( vvp != NULL );
9b082b39 684 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
685}
686
687void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off )
688{
acaf7113
AF
689 gint x, y, wid, hei;
690
50a14534 691 g_return_if_fail ( vvp != NULL );
9b082b39 692 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
693
694 if (x_off >= 0) {
695 x = 0;
696 wid = x_off;
697 } else {
698 x = vvp->width+x_off;
699 wid = -x_off;
700 }
701 if (y_off >= 0) {
702 y = 0;
703 hei = y_off;
704 } else {
705 y = vvp->height+y_off;
706 hei = -y_off;
707 }
708 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height);
709 gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei);
50a14534
EB
710}
711
712void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp )
713{
714 g_return_if_fail ( vvp != NULL );
a3603298 715 if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) {
50a14534 716 vvp->xmpp = vvp->ympp = xympp;
a3603298
RN
717 // Since xmpp & ympp are the same it doesn't matter which one is used here
718 vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp);
719 }
50a14534
EB
720
721 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
722 viewport_utm_zone_check(vvp);
50a14534
EB
723}
724
725/* or could do factor */
726void vik_viewport_zoom_in ( VikViewport *vvp )
727{
728 g_return_if_fail ( vvp != NULL );
729 if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) )
730 {
731 vvp->xmpp /= 2;
732 vvp->ympp /= 2;
733
a3603298
RN
734 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
735 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
736
50a14534
EB
737 viewport_utm_zone_check(vvp);
738 }
739}
740
741void vik_viewport_zoom_out ( VikViewport *vvp )
742{
743 g_return_if_fail ( vvp != NULL );
744 if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) )
745 {
746 vvp->xmpp *= 2;
747 vvp->ympp *= 2;
748
a3603298
RN
749 vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp);
750 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
751
50a14534
EB
752 viewport_utm_zone_check(vvp);
753 }
754}
755
756gdouble vik_viewport_get_zoom ( VikViewport *vvp )
757{
758 if ( vvp->xmpp == vvp->ympp )
759 return vvp->xmpp;
760 return 0.0;
761}
762
763gdouble vik_viewport_get_xmpp ( VikViewport *vvp )
764{
765 return vvp->xmpp;
766}
767
768gdouble vik_viewport_get_ympp ( VikViewport *vvp )
769{
770 return vvp->ympp;
771}
772
773void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp )
774{
775 if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) {
776 vvp->xmpp = xmpp;
a3603298 777 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
50a14534
EB
778 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
779 viewport_utm_zone_check(vvp);
50a14534
EB
780 }
781}
782
783void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp )
784{
785 if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) {
786 vvp->ympp = ympp;
a3603298 787 vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp);
50a14534
EB
788 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
789 viewport_utm_zone_check(vvp);
50a14534
EB
790 }
791}
792
793
794const VikCoord *vik_viewport_get_center ( VikViewport *vvp )
795{
796 g_return_val_if_fail ( vvp != NULL, NULL );
797 return &(vvp->center);
798}
799
800/* called every time we update coordinates/zoom */
801static void viewport_utm_zone_check ( VikViewport *vvp )
802{
803 if ( vvp->coord_mode == VIK_COORD_UTM )
804 {
805 struct UTM utm;
806 struct LatLon ll;
807 a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll );
808 a_coords_latlon_to_utm ( &ll, &utm );
809 if ( utm.zone != vvp->center.utm_zone )
810 *((struct UTM *)(&vvp->center)) = utm;
811
812 /* misc. stuff so we don't have to check later */
813 vvp->utm_zone_width = viewport_utm_zone_width ( vvp );
814 vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) );
815 }
816}
817
818void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll )
819{
820 vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll );
821 if ( vvp->coord_mode == VIK_COORD_UTM )
822 viewport_utm_zone_check ( vvp );
823}
824
825void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm )
826{
827 vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm );
828 if ( vvp->coord_mode == VIK_COORD_UTM )
829 viewport_utm_zone_check ( vvp );
830}
831
832void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord )
833{
834 vvp->center = *coord;
835 if ( vvp->coord_mode == VIK_COORD_UTM )
836 viewport_utm_zone_check ( vvp );
837}
838
839void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br )
840{
841 g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM );
842
843 /* get center, then just offset */
844 vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone );
845 ul->mode = VIK_COORD_UTM;
846 *br = *ul;
847
848 ul->north_south += (vvp->ympp * vvp->height / 2);
849 ul->east_west -= (vvp->xmpp * vvp->width / 2);
850 br->north_south -= (vvp->ympp * vvp->height / 2);
851 br->east_west += (vvp->xmpp * vvp->width / 2);
852}
853
854void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone)
855{
856 if ( vvp->coord_mode == VIK_COORD_UTM ) {
857 *center = *((struct UTM *)(vik_viewport_get_center ( vvp )));
858 center->easting -= ( zone - center->zone ) * vvp->utm_zone_width;
859 center->zone = zone;
860 }
861}
862
863gchar vik_viewport_leftmost_zone ( VikViewport *vvp )
864{
865 if ( vvp->coord_mode == VIK_COORD_UTM ) {
866 VikCoord coord;
867 g_assert ( vvp != NULL );
868 vik_viewport_screen_to_coord ( vvp, 0, 0, &coord );
869 return coord.utm_zone;
870 }
871 return '\0';
872}
873
874gchar vik_viewport_rightmost_zone ( VikViewport *vvp )
875{
876 if ( vvp->coord_mode == VIK_COORD_UTM ) {
877 VikCoord coord;
878 g_assert ( vvp != NULL );
879 vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord );
880 return coord.utm_zone;
881 }
882 return '\0';
883}
884
885
886void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y )
887{
888 g_return_if_fail ( vvp != NULL );
889 if ( vvp->coord_mode == VIK_COORD_UTM ) {
890 /* slightly optimized */
891 vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2));
892 vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y);
893 viewport_utm_zone_check ( vvp );
894 } else {
895 VikCoord tmp;
896 vik_viewport_screen_to_coord ( vvp, x, y, &tmp );
897 vik_viewport_set_center_coord ( vvp, &tmp );
898 }
899}
900
901gint vik_viewport_get_width( VikViewport *vvp )
902{
903 g_return_val_if_fail ( vvp != NULL, 0 );
904 return vvp->width;
905}
906
907gint vik_viewport_get_height( VikViewport *vvp )
908{
909 g_return_val_if_fail ( vvp != NULL, 0 );
910 return vvp->height;
911}
912
913void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord )
914{
c53eda9a
GB
915 g_return_if_fail ( vvp != NULL );
916
50a14534
EB
917 if ( vvp->coord_mode == VIK_COORD_UTM ) {
918 int zone_delta;
919 struct UTM *utm = (struct UTM *) coord;
920 coord->mode = VIK_COORD_UTM;
921
50a14534
EB
922 utm->zone = vvp->center.utm_zone;
923 utm->letter = vvp->center.utm_letter;
a3603298 924 utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west;
50a14534
EB
925 zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 );
926 utm->zone += zone_delta;
927 utm->easting -= zone_delta * vvp->utm_zone_width;
a3603298 928 utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south;
50a14534
EB
929 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
930 coord->mode = VIK_COORD_LATLON;
d587678a 931 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
a3603298
RN
932 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
933 coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y));
d587678a 934 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA )
a3603298 935 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 936 else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
a3603298
RN
937 /* This isn't called with a high frequently so less need to optimize */
938 coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2));
939 coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) );
50a14534
EB
940 }
941 }
942}
943
a3603298
RN
944/*
945 * Since this function is used for every drawn trackpoint - it can get called alot
946 * Thus x & y position factors are calculated once on zoom changes,
947 * avoiding the need to do it here all the time.
948 * For good measure the half width and height values are also pre calculated too.
949 */
50a14534
EB
950void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y )
951{
952 static VikCoord tmp;
953 g_return_if_fail ( vvp != NULL );
954
955 if ( coord->mode != vvp->coord_mode )
956 {
957 g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!");
958 vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp );
959 coord = &tmp;
960 }
961
962 if ( vvp->coord_mode == VIK_COORD_UTM ) {
963 struct UTM *center = (struct UTM *) &(vvp->center);
964 struct UTM *utm = (struct UTM *) coord;
965 if ( center->zone != utm->zone && vvp->one_utm_zone )
966 {
967 *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE;
968 return;
969 }
970
a3603298 971 *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) -
50a14534 972 (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp;
a3603298 973 *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp );
50a14534
EB
974 } else if ( vvp->coord_mode == VIK_COORD_LATLON ) {
975 struct LatLon *center = (struct LatLon *) &(vvp->center);
976 struct LatLon *ll = (struct LatLon *) coord;
977 double xx,yy;
d587678a 978 if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) {
a3603298
RN
979 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
980 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) );
d587678a 981 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) {
a3603298 982 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 983 *x = xx; *y = yy;
50a14534 984 } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) {
a3603298
RN
985 *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) );
986 *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) );
50a14534
EB
987 }
988 }
989}
990
d9ffd267
EB
991void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 )
992{
993 if ( *x1 > 20000 || *x1 < -20000 ) {
994 gdouble shrinkfactor = ABS(20000.0 / *x1);
995 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
996 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
997 } else if ( *y1 > 20000 || *y1 < -20000 ) {
998 gdouble shrinkfactor = ABS(20000.0 / *x1);
999 *x1 = *x2 + (shrinkfactor * (*x1-*x2));
1000 *y1 = *y2 + (shrinkfactor * (*y1-*y2));
1001 } else if ( *x2 > 20000 || *x2 < -20000 ) {
1002 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
1003 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
1004 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
d9ffd267
EB
1005 } else if ( *y2 > 20000 || *y2 < -20000 ) {
1006 gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2);
1007 *x2 = *x1 + (shrinkfactor * (*x2-*x1));
1008 *y2 = *y1 + (shrinkfactor * (*y2-*y1));
1009 }
1010}
1011
50a14534
EB
1012void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 )
1013{
1014 if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) ||
d9ffd267
EB
1015 ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) {
1016 /*** clipping, yeah! ***/
1017 a_viewport_clip_line ( &x1, &y1, &x2, &y2 );
1018 gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2);
1019 }
50a14534
EB
1020}
1021
1022void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 )
1023{
add30ebe
RN
1024 // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done
1025 if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 )
50a14534
EB
1026 gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2);
1027}
1028
1029void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string )
1030{
1031 if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 )
1032 gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string );
1033}
1034
1035/* shouldn't use this -- slow -- change the alpha channel instead. */
1036void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha,
1037 gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h )
1038{
1039 gint real_dest_x = MAX(dest_x,0);
1040 gint real_dest_y = MAX(dest_y,0);
1041
1042 if ( alpha == 0 )
1043 return; /* don't waste your time */
1044
1045 if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height )
1046 {
1047 if ( vvp->alpha_pixbuf )
1048 g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) );
1049 vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width);
1050 vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height);
1051 vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height );
1052 }
1053
1054 w = MIN(w,vvp->width - dest_x);
1055 h = MIN(h,vvp->height - dest_y);
1056
1057 /* check that we are drawing within boundaries. */
1058 src_x += (real_dest_x - dest_x);
1059 src_y += (real_dest_y - dest_y);
1060 w -= (real_dest_x - dest_x);
1061 h -= (real_dest_y - dest_y);
1062
1063 gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL,
1064 real_dest_x, real_dest_y, 0, 0, w, h );
1065
1066 /* do a composite */
1067 gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha );
1068
1069 /* draw pixbuf_tmp */
1070 vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h );
1071}
1072
1073void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y,
1074 gint dest_x, gint dest_y, gint w, gint h )
1075{
1076 gdk_draw_pixbuf ( vvp->scr_buffer,
ff37db21
RN
1077 NULL,
1078 pixbuf,
50a14534
EB
1079 src_x, src_y, dest_x, dest_y, w, h,
1080 GDK_RGB_DITHER_NONE, 0, 0 );
1081}
1082
1083void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 )
1084{
1085 gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 );
1086}
1087
1088
1089void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints )
1090{
1091 gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints );
1092}
1093
1094VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp )
1095{
1096 g_assert ( vvp );
1097 return vvp->coord_mode;
1098}
1099
1100static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode )
1101{
1102 g_return_if_fail ( vvp != NULL );
1103 vvp->coord_mode = mode;
1104 vik_coord_convert ( &(vvp->center), mode );
1105}
1106
1107/* Thanks GPSDrive */
1108static gboolean calcxy_rev(double *lg, double *lt, gint x, gint y, double zero_long, double zero_lat, double pixelfact_x, double pixelfact_y, gint mapSizeX2, gint mapSizeY2 )
1109{
1110 int px, py;
1111 gdouble dif, lat, lon;
1112 double Ra = Radius[90+(gint)zero_lat];
1113
1114 px = (mapSizeX2 - x) * pixelfact_x;
1115 py = (-mapSizeY2 + y) * pixelfact_y;
1116
1117 lat = zero_lat - py / Ra;
1118 lat = zero_lat - py / Ra;
1119 lon =
1120 zero_long -
1121 px / (Ra *
81765f5e 1122 cos (DEG2RAD(lat)));
50a14534 1123
81765f5e 1124 dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long)))));
50a14534
EB
1125 lat = lat - dif / 1.5;
1126 lon =
1127 zero_long -
1128 px / (Ra *
81765f5e 1129 cos (DEG2RAD(lat)));
50a14534
EB
1130
1131 *lt = lat;
1132 *lg = lon;
1133 return (TRUE);
1134}
1135
1136/* Thanks GPSDrive */
1137static 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 )
1138{
1139 double dif;
1140 double Ra;
1141 gint mapSizeX = 2 * mapSizeX2;
1142 gint mapSizeY = 2 * mapSizeY2;
1143
1144 g_assert ( lt >= -90.0 && lt <= 90.0 );
1145// lg *= rad2deg; // FIXME, optimize equations
1146// lt *= rad2deg;
1147 Ra = Radius[90+(gint)lt];
1148 *x = Ra *
81765f5e 1149 cos (DEG2RAD(lt)) * (lg - zero_long);
50a14534 1150 *y = Ra * (lt - zero_lat);
81765f5e 1151 dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long)))));
50a14534
EB
1152 *y = *y + dif / 1.85;
1153 *x = *x / pixelfact_x;
1154 *y = *y / pixelfact_y;
1155 *x = mapSizeX2 - *x;
1156 *y += mapSizeY2;
1157 if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY))
1158 return (FALSE);
1159 return (TRUE);
1160}
1161
1162static void viewport_init_ra()
1163{
1164 static gboolean done_before = FALSE;
1165 if ( !done_before )
1166 {
1167 gint i;
1168 for ( i = -90; i <= 90; i++)
81765f5e 1169 Radius[i+90] = calcR ( DEG2RAD((double)i) );
50a14534
EB
1170 done_before = TRUE;
1171 }
1172}
1173
1174double calcR (double lat)
1175{
1176 double a = 6378.137, r, sc, x, y, z;
1177 double e2 = 0.081082 * 0.081082;
1178 /*
1179 * the radius of curvature of an ellipsoidal Earth in the plane of the
1180 * meridian is given by
1181 *
1182 * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
1183 *
1184 *
1185 * where a is the equatorial radius, b is the polar radius, and e is
1186 * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
1187 *
1188 * a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
1189 * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e
1190 * = 0.081082 Eccentricity
1191 */
1192
81765f5e 1193 lat = DEG2RAD(lat);
50a14534
EB
1194 sc = sin (lat);
1195 x = a * (1.0 - e2);
1196 z = 1.0 - e2 * sc * sc;
1197 y = pow (z, 1.5);
1198 r = x / y;
1199 r = r * 1000.0;
1200 return r;
1201}
1202
1203gboolean vik_viewport_is_one_zone ( VikViewport *vvp )
1204{
1205 return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone;
1206}
1207
1208void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout )
1209{
1210 if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 )
1211 gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout );
1212}
1213
1214void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest )
1215{
1216 static GdkGCValues values;
1217 gdk_gc_get_values ( gc, &values );
1218 gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest );
1219}
1220
1221GdkFunction vik_gc_get_function ( GdkGC *gc )
1222{
1223 static GdkGCValues values;
1224 gdk_gc_get_values ( gc, &values );
1225 return values.function;
1226}
1227
1228void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode )
1229{
1230 vvp->drawmode = drawmode;
1231 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM )
1232 viewport_set_coord_mode ( vvp, VIK_COORD_UTM );
1233 else {
1234 viewport_set_coord_mode ( vvp, VIK_COORD_LATLON );
50a14534
EB
1235 }
1236}
1237
1238VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp )
1239{
1240 return vvp->drawmode;
1241}
1242
0df66d57
EB
1243/******** triggering *******/
1244void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger )
1245{
1246 vp->trigger = trigger;
1247}
1248
1249gpointer vik_viewport_get_trigger ( VikViewport *vp )
1250{
1251 return vp->trigger;
1252}
1253
1254void vik_viewport_snapshot_save ( VikViewport *vp )
1255{
1256 gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 );
1257}
1258
1259void vik_viewport_snapshot_load ( VikViewport *vp )
1260{
1261 gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 );
1262}
1263
1264void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn)
1265{
1266 vp->half_drawn = half_drawn;
1267}
1268
1269gboolean vik_viewport_get_half_drawn( VikViewport *vp )
1270{
1271 return vp->half_drawn;
1272}
1273
7bc965c0
GB
1274
1275const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode)
0c1044e9 1276 {
7bc965c0 1277 const gchar *name = NULL;
24277274 1278 VikWindow *vw = NULL;
7bc965c0
GB
1279 GtkWidget *mode_button;
1280 GtkWidget *label;
24277274
GB
1281
1282 vw = VIK_WINDOW_FROM_WIDGET(vv);
1283 mode_button = vik_window_get_drawmode_button(vw, mode);
7bc965c0
GB
1284 label = gtk_bin_get_child(GTK_BIN(mode_button));
1285
1286 name = gtk_label_get_text ( GTK_LABEL(label) );
314c1ccc 1287
7bc965c0 1288 return name;
314c1ccc
QT
1289
1290}
7bc965c0 1291
0c1044e9
EB
1292void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon )
1293{
1294 VikCoord tleft, tright, bleft, bright;
1295
1296 vik_viewport_screen_to_coord ( vp, 0, 0, &tleft );
1297 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright );
1298 vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft );
1299 vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright );
7bc965c0 1300
0c1044e9
EB
1301 vik_coord_convert(&tleft, VIK_COORD_LATLON);
1302 vik_coord_convert(&tright, VIK_COORD_LATLON);
1303 vik_coord_convert(&bleft, VIK_COORD_LATLON);
1304 vik_coord_convert(&bright, VIK_COORD_LATLON);
1305
1306 *max_lat = MAX(tleft.north_south, tright.north_south);
1307 *min_lat = MIN(bleft.north_south, bright.north_south);
1308 *max_lon = MAX(tright.east_west, bright.east_west);
1309 *min_lon = MIN(tleft.east_west, bleft.east_west);
1310}
82aa018d
GB
1311
1312void vik_viewport_reset_copyrights ( VikViewport *vp )
1313{
1314 g_return_if_fail ( vp != NULL );
1315 g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL );
1316 g_slist_free ( vp->copyrights );
1317 vp->copyrights = NULL;
1318}
1319
33e0e500
GB
1320/**
1321 * vik_viewport_add_copyright:
1322 * @vp: self object
1323 * @copyright: new copyright to display
1324 *
1325 * Add a copyright to display on viewport.
1326 */
82aa018d
GB
1327void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright )
1328{
1329 g_return_if_fail ( vp != NULL );
1330 if ( copyright )
1331 {
bbbd639f 1332 GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp );
82aa018d
GB
1333 if ( found == NULL )
1334 {
1335 gchar *duple = g_strdup ( copyright );
1336 vp->copyrights = g_slist_prepend ( vp->copyrights, duple );
1337 }
1338 }
1339}
26336cf0
GB
1340
1341void vik_viewport_reset_logos ( VikViewport *vp )
1342{
1343 g_return_if_fail ( vp != NULL );
1344 /* do not free elem */
1345 g_slist_free ( vp->logos );
1346 vp->logos = NULL;
1347}
1348
1349void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo )
1350{
1351 g_return_if_fail ( vp != NULL );
1352 if ( logo )
1353 {
1354 GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */
1355 if ( found == NULL )
1356 {
1357 vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo );
1358 }
1359 }
1360}
9a3538f5
GB
1361
1362/**
1363 * vik_viewport_compute_bearing:
1364 * @vp: self object
1365 * @x1: screen coord
1366 * @y1: screen coord
1367 * @x2: screen coord
1368 * @y2: screen coord
1369 * @angle: bearing in Radian (output)
1370 * @baseangle: UTM base angle in Radian (output)
1371 *
1372 * Compute bearing.
1373 */
1374void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle )
1375{
1376 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
1377 gdouble dx = (x2-x1)/len*10;
1378 gdouble dy = (y2-y1)/len*10;
1379
1380 *angle = atan2(dy, dx) + M_PI_2;
1381
1382 if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
1383 VikCoord test;
1384 struct LatLon ll;
1385 struct UTM u;
1386 gint tx, ty;
1387
1388 vik_viewport_screen_to_coord ( vp, x1, y1, &test );
1389 vik_coord_to_latlon ( &test, &ll );
1390 ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude
1391 a_coords_latlon_to_utm ( &ll, &u );
1392 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
1393 vik_viewport_coord_to_screen ( vp, &test, &tx, &ty );
1394
1395 *baseangle = M_PI - atan2(tx-x1, ty-y1);
1396 *angle -= *baseangle;
1397 }
1398
1399 if (*angle < 0)
1400 *angle += 2*M_PI;
1401 if (*angle > 2*M_PI)
1402 *angle -= 2*M_PI;
1403}