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