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