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