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