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