]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, 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 | ||
27 | #define DEFAULT_BACKGROUND_COLOR "#CCCCCC" | |
28 | ||
29 | #include <gtk/gtk.h> | |
30 | #include <math.h> | |
31 | ||
32 | #include "coords.h" | |
33 | #include "vikcoord.h" | |
34 | #include "vikviewport.h" | |
35 | ||
36 | #include "mapcoord.h" | |
37 | ||
38 | /* for ALTI_TO_MPP */ | |
39 | #include "globals.h" | |
40 | #include "googlemaps.h" | |
41 | #include "khmaps.h" | |
42 | ||
43 | static gdouble EASTING_OFFSET = 500000.0; | |
44 | ||
45 | static void viewport_class_init ( VikViewportClass *klass ); | |
46 | static void viewport_init ( VikViewport *vvp ); | |
47 | static void viewport_finalize ( GObject *gob ); | |
48 | static void viewport_utm_zone_check ( VikViewport *vvp ); | |
49 | ||
50 | 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 ); | |
51 | 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 ); | |
52 | double calcR (double lat); | |
53 | ||
54 | static double Radius[181]; | |
55 | static void viewport_init_ra(); | |
56 | ||
57 | static GObjectClass *parent_class; | |
58 | ||
59 | static void viewport_google_rezoom ( VikViewport *vvp ); | |
60 | ||
61 | ||
62 | struct _VikViewport { | |
63 | GtkDrawingArea drawing_area; | |
64 | GdkPixmap *scr_buffer; | |
65 | gint width, height; | |
66 | VikCoord center; | |
67 | VikCoordMode coord_mode; | |
68 | gdouble xmpp, ympp; | |
69 | ||
70 | GdkPixbuf *alpha_pixbuf; | |
71 | guint8 alpha_pixbuf_width; | |
72 | guint8 alpha_pixbuf_height; | |
73 | ||
74 | gdouble utm_zone_width; | |
75 | gboolean one_utm_zone; | |
76 | ||
77 | GdkGC *background_gc; | |
78 | GdkColor background_color; | |
35c7c0ba | 79 | gboolean draw_scale; |
50a14534 EB |
80 | |
81 | /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */ | |
82 | VikViewportDrawMode drawmode; | |
83 | ||
84 | /* handy conversion factors which make google plotting extremely fast */ | |
85 | gdouble google_calcx_fact; | |
86 | gdouble google_calcy_fact; | |
87 | gdouble google_calcx_rev_fact; | |
88 | gdouble google_calcy_rev_fact; | |
89 | }; | |
90 | ||
91 | static gdouble | |
92 | viewport_utm_zone_width ( VikViewport *vvp ) | |
93 | { | |
94 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
95 | struct LatLon ll; | |
96 | ||
97 | /* get latitude of screen bottom */ | |
98 | struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp ))); | |
99 | utm.northing -= vvp -> height * vvp -> ympp / 2; | |
100 | a_coords_utm_to_latlon ( &utm, &ll ); | |
101 | ||
102 | /* boundary */ | |
103 | ll.lon = (utm.zone - 1) * 6 - 180 ; | |
104 | a_coords_latlon_to_utm ( &ll, &utm); | |
105 | return fabs ( utm.easting - EASTING_OFFSET ) * 2; | |
106 | } else | |
107 | return 0.0; | |
108 | } | |
109 | ||
110 | ||
111 | GType vik_viewport_get_type (void) | |
112 | { | |
113 | static GType vvp_type = 0; | |
114 | ||
115 | if (!vvp_type) | |
116 | { | |
117 | static const GTypeInfo vvp_info = | |
118 | { | |
119 | sizeof (VikViewportClass), | |
120 | NULL, /* base_init */ | |
121 | NULL, /* base_finalize */ | |
122 | (GClassInitFunc) viewport_class_init, | |
123 | NULL, /* class_finalize */ | |
124 | NULL, /* class_data */ | |
125 | sizeof (VikViewport), | |
126 | 0, | |
127 | (GInstanceInitFunc) viewport_init, | |
128 | }; | |
129 | vvp_type = g_type_register_static ( GTK_TYPE_DRAWING_AREA, "VikViewport", &vvp_info, 0 ); | |
130 | } | |
131 | return vvp_type; | |
132 | } | |
133 | ||
134 | static void viewport_class_init ( VikViewportClass *klass ) | |
135 | { | |
136 | /* Destructor */ | |
137 | GObjectClass *object_class; | |
138 | ||
139 | object_class = G_OBJECT_CLASS (klass); | |
140 | ||
141 | object_class->finalize = viewport_finalize; | |
142 | ||
143 | parent_class = g_type_class_peek_parent (klass); | |
144 | } | |
145 | ||
146 | VikViewport *vik_viewport_new () | |
147 | { | |
148 | return VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) ); | |
149 | } | |
150 | ||
151 | static void viewport_init ( VikViewport *vvp ) | |
152 | { | |
153 | viewport_init_ra(); | |
154 | ||
155 | /* TODO: not static */ | |
156 | vvp->xmpp = 4.0; | |
157 | vvp->ympp = 4.0; | |
158 | vvp->coord_mode = VIK_COORD_UTM; | |
159 | vvp->drawmode = VIK_VIEWPORT_DRAWMODE_UTM; | |
160 | vvp->center.north_south = 0; | |
161 | vvp->center.east_west = -166021; | |
162 | vvp->center.utm_zone = 31; | |
163 | vvp->center.utm_letter = 'N'; | |
164 | vvp->scr_buffer = NULL; | |
165 | vvp->alpha_pixbuf = NULL; | |
166 | vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0; | |
167 | vvp->utm_zone_width = 0.0; | |
168 | vvp->background_gc = NULL; | |
35c7c0ba | 169 | vvp->draw_scale = TRUE; |
50a14534 EB |
170 | g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL); |
171 | } | |
172 | ||
173 | GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp ) | |
174 | { | |
175 | GdkColor *rv = g_malloc ( sizeof ( GdkColor ) ); | |
176 | *rv = vvp->background_color; | |
177 | return rv; | |
178 | } | |
179 | ||
180 | /* returns pointer to internal static storage, changes next time function called, use quickly */ | |
181 | const gchar *vik_viewport_get_background_color ( VikViewport *vvp ) | |
182 | { | |
183 | static gchar color[8]; | |
184 | 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)); | |
185 | return color; | |
186 | } | |
187 | ||
188 | void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname ) | |
189 | { | |
190 | g_assert ( vvp->background_gc ); | |
191 | gdk_color_parse ( colorname, &(vvp->background_color) ); | |
192 | gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) ); | |
193 | } | |
194 | ||
195 | void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color ) | |
196 | { | |
197 | g_assert ( vvp->background_gc ); | |
198 | vvp->background_color = *color; | |
199 | gdk_gc_set_rgb_fg_color ( vvp->background_gc, color ); | |
200 | } | |
201 | ||
202 | ||
203 | GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness ) | |
204 | { | |
205 | GdkGC *rv; | |
206 | GdkColor color; | |
207 | ||
208 | rv = gdk_gc_new ( GTK_WIDGET(vvp)->window ); | |
209 | gdk_color_parse ( colorname, &color ); | |
210 | gdk_gc_set_rgb_fg_color ( rv, &color ); | |
211 | gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); | |
212 | return rv; | |
213 | } | |
214 | ||
215 | GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness ) | |
216 | { | |
217 | GdkGC *rv; | |
218 | ||
219 | rv = gdk_gc_new ( GTK_WIDGET(vvp)->window ); | |
220 | gdk_gc_set_rgb_fg_color ( rv, color ); | |
221 | gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); | |
222 | return rv; | |
223 | } | |
224 | ||
225 | void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height ) | |
226 | { | |
227 | vvp->width = width; | |
228 | vvp->height = height; | |
229 | if ( vvp->scr_buffer ) | |
230 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
231 | vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); | |
232 | } | |
233 | ||
234 | ||
235 | GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp ) | |
236 | { | |
237 | return vvp->scr_buffer; | |
238 | } | |
239 | ||
240 | gboolean vik_viewport_configure ( VikViewport *vvp ) | |
241 | { | |
242 | g_return_val_if_fail ( vvp != NULL, TRUE ); | |
243 | ||
244 | vvp->width = GTK_WIDGET(vvp)->allocation.width; | |
245 | vvp->height = GTK_WIDGET(vvp)->allocation.height; | |
246 | ||
247 | if ( vvp->scr_buffer ) | |
248 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
249 | ||
250 | vvp->scr_buffer = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, vvp->width, vvp->height, -1 ); | |
251 | ||
252 | /* this is down here so it can get a GC (necessary?) */ | |
253 | if ( ! vvp->background_gc ) | |
254 | { | |
255 | vvp->background_gc = vik_viewport_new_gc ( vvp, "", 1 ); | |
256 | vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); /* set to "backup" color in vvp->background_color */ | |
257 | } | |
258 | ||
259 | return FALSE; | |
260 | } | |
261 | ||
262 | static void viewport_finalize ( GObject *gob ) | |
263 | { | |
264 | VikViewport *vvp = VIK_VIEWPORT(gob); | |
265 | ||
266 | g_return_if_fail ( vvp != NULL ); | |
267 | ||
268 | if ( vvp->scr_buffer ) | |
269 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
270 | ||
271 | if ( vvp->alpha_pixbuf ) | |
272 | g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) ); | |
273 | ||
274 | if ( vvp->background_gc ) | |
275 | g_object_unref ( G_OBJECT ( vvp->background_gc ) ); | |
276 | ||
277 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
278 | } | |
279 | ||
280 | void vik_viewport_clear ( VikViewport *vvp ) | |
281 | { | |
282 | g_return_if_fail ( vvp != NULL ); | |
283 | gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height); | |
284 | } | |
285 | ||
35c7c0ba EB |
286 | void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale ) |
287 | { | |
288 | vvp->draw_scale = draw_scale; | |
289 | } | |
290 | ||
291 | gboolean vik_viewport_get_draw_scale ( VikViewport *vvp ) | |
292 | { | |
293 | return vvp->draw_scale; | |
294 | } | |
295 | ||
acaf7113 AF |
296 | void vik_viewport_draw_scale ( VikViewport *vvp ) |
297 | { | |
35c7c0ba EB |
298 | if ( vvp->draw_scale ) { |
299 | VikCoord left, right; | |
300 | gdouble unit, base, diff, old_unit, old_diff, ratio; | |
301 | gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10; | |
302 | PangoFontDescription *pfd; | |
303 | PangoLayout *pl; | |
304 | gchar s[128]; | |
acaf7113 | 305 | |
35c7c0ba | 306 | g_return_if_fail ( vvp != NULL ); |
acaf7113 | 307 | |
35c7c0ba EB |
308 | vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left ); |
309 | vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right ); | |
acaf7113 | 310 | |
35c7c0ba EB |
311 | base = vik_coord_diff ( &left, &right ); // in meters |
312 | ratio = (vvp->width/SCSIZE)/base; | |
acaf7113 | 313 | |
35c7c0ba EB |
314 | unit = 1; |
315 | diff = fabs(base-unit); | |
acaf7113 AF |
316 | old_unit = unit; |
317 | old_diff = diff; | |
35c7c0ba EB |
318 | odd = 1; |
319 | while (diff <= old_diff) { | |
320 | old_unit = unit; | |
321 | old_diff = diff; | |
322 | unit = unit * (odd%2 ? 5 : 2); | |
323 | diff = fabs(base-unit); | |
324 | odd++; | |
325 | } | |
326 | unit = old_unit; | |
327 | len = unit * ratio; | |
acaf7113 | 328 | |
35c7c0ba | 329 | vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, |
acaf7113 | 330 | PAD, vvp->height-PAD, PAD + len, vvp->height-PAD); |
35c7c0ba | 331 | vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, |
acaf7113 | 332 | PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT); |
35c7c0ba | 333 | vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, |
acaf7113 | 334 | PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT); |
35c7c0ba EB |
335 | if (odd%2) { |
336 | int i; | |
337 | for (i=1; i<5; i++) { | |
338 | vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, | |
acaf7113 | 339 | PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); |
35c7c0ba EB |
340 | } |
341 | } else { | |
342 | int i; | |
343 | for (i=1; i<10; i++) { | |
344 | vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, | |
345 | PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); | |
346 | } | |
acaf7113 | 347 | } |
35c7c0ba EB |
348 | pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); |
349 | pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable? | |
350 | pango_layout_set_font_description (pl, pfd); | |
351 | pango_font_description_free (pfd); | |
352 | ||
353 | if (unit >= 1000) { | |
354 | sprintf(s, "%d km", (int)unit/1000); | |
355 | } else { | |
356 | sprintf(s, "%d m", (int)unit); | |
acaf7113 | 357 | } |
35c7c0ba EB |
358 | pango_layout_set_text(pl, s, -1); |
359 | vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc, | |
acaf7113 | 360 | PAD + len + PAD, vvp->height - PAD - 10, pl); |
35c7c0ba | 361 | } |
acaf7113 AF |
362 | } |
363 | ||
50a14534 EB |
364 | void vik_viewport_sync ( VikViewport *vvp ) |
365 | { | |
366 | g_return_if_fail ( vvp != NULL ); | |
367 | 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); | |
368 | } | |
369 | ||
370 | void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off ) | |
371 | { | |
acaf7113 AF |
372 | gint x, y, wid, hei; |
373 | ||
50a14534 EB |
374 | g_return_if_fail ( vvp != NULL ); |
375 | 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 |
376 | |
377 | if (x_off >= 0) { | |
378 | x = 0; | |
379 | wid = x_off; | |
380 | } else { | |
381 | x = vvp->width+x_off; | |
382 | wid = -x_off; | |
383 | } | |
384 | if (y_off >= 0) { | |
385 | y = 0; | |
386 | hei = y_off; | |
387 | } else { | |
388 | y = vvp->height+y_off; | |
389 | hei = -y_off; | |
390 | } | |
391 | gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height); | |
392 | gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei); | |
50a14534 EB |
393 | } |
394 | ||
395 | void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp ) | |
396 | { | |
397 | g_return_if_fail ( vvp != NULL ); | |
398 | if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) | |
399 | vvp->xmpp = vvp->ympp = xympp; | |
400 | ||
401 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
402 | viewport_utm_zone_check(vvp); | |
403 | else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
404 | viewport_google_rezoom ( vvp ); | |
405 | } | |
406 | ||
407 | /* or could do factor */ | |
408 | void vik_viewport_zoom_in ( VikViewport *vvp ) | |
409 | { | |
410 | g_return_if_fail ( vvp != NULL ); | |
411 | if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) ) | |
412 | { | |
413 | vvp->xmpp /= 2; | |
414 | vvp->ympp /= 2; | |
415 | ||
416 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
417 | viewport_google_rezoom ( vvp ); | |
418 | ||
419 | viewport_utm_zone_check(vvp); | |
420 | } | |
421 | } | |
422 | ||
423 | void vik_viewport_zoom_out ( VikViewport *vvp ) | |
424 | { | |
425 | g_return_if_fail ( vvp != NULL ); | |
426 | if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) ) | |
427 | { | |
428 | vvp->xmpp *= 2; | |
429 | vvp->ympp *= 2; | |
430 | ||
431 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
432 | viewport_google_rezoom ( vvp ); | |
433 | ||
434 | viewport_utm_zone_check(vvp); | |
435 | } | |
436 | } | |
437 | ||
438 | gdouble vik_viewport_get_zoom ( VikViewport *vvp ) | |
439 | { | |
440 | if ( vvp->xmpp == vvp->ympp ) | |
441 | return vvp->xmpp; | |
442 | return 0.0; | |
443 | } | |
444 | ||
445 | gdouble vik_viewport_get_xmpp ( VikViewport *vvp ) | |
446 | { | |
447 | return vvp->xmpp; | |
448 | } | |
449 | ||
450 | gdouble vik_viewport_get_ympp ( VikViewport *vvp ) | |
451 | { | |
452 | return vvp->ympp; | |
453 | } | |
454 | ||
455 | void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp ) | |
456 | { | |
457 | if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) { | |
458 | vvp->xmpp = xmpp; | |
459 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
460 | viewport_utm_zone_check(vvp); | |
461 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
462 | viewport_google_rezoom ( vvp ); | |
463 | } | |
464 | } | |
465 | ||
466 | void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp ) | |
467 | { | |
468 | if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) { | |
469 | vvp->ympp = ympp; | |
470 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
471 | viewport_utm_zone_check(vvp); | |
472 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
473 | viewport_google_rezoom ( vvp ); | |
474 | } | |
475 | } | |
476 | ||
477 | ||
478 | const VikCoord *vik_viewport_get_center ( VikViewport *vvp ) | |
479 | { | |
480 | g_return_val_if_fail ( vvp != NULL, NULL ); | |
481 | return &(vvp->center); | |
482 | } | |
483 | ||
484 | /* called every time we update coordinates/zoom */ | |
485 | static void viewport_utm_zone_check ( VikViewport *vvp ) | |
486 | { | |
487 | if ( vvp->coord_mode == VIK_COORD_UTM ) | |
488 | { | |
489 | struct UTM utm; | |
490 | struct LatLon ll; | |
491 | a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll ); | |
492 | a_coords_latlon_to_utm ( &ll, &utm ); | |
493 | if ( utm.zone != vvp->center.utm_zone ) | |
494 | *((struct UTM *)(&vvp->center)) = utm; | |
495 | ||
496 | /* misc. stuff so we don't have to check later */ | |
497 | vvp->utm_zone_width = viewport_utm_zone_width ( vvp ); | |
498 | vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) ); | |
499 | } | |
500 | } | |
501 | ||
502 | void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll ) | |
503 | { | |
504 | vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll ); | |
505 | if ( vvp->coord_mode == VIK_COORD_UTM ) | |
506 | viewport_utm_zone_check ( vvp ); | |
507 | } | |
508 | ||
509 | void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm ) | |
510 | { | |
511 | vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm ); | |
512 | if ( vvp->coord_mode == VIK_COORD_UTM ) | |
513 | viewport_utm_zone_check ( vvp ); | |
514 | } | |
515 | ||
516 | void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord ) | |
517 | { | |
518 | vvp->center = *coord; | |
519 | if ( vvp->coord_mode == VIK_COORD_UTM ) | |
520 | viewport_utm_zone_check ( vvp ); | |
521 | } | |
522 | ||
523 | void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br ) | |
524 | { | |
525 | g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM ); | |
526 | ||
527 | /* get center, then just offset */ | |
528 | vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone ); | |
529 | ul->mode = VIK_COORD_UTM; | |
530 | *br = *ul; | |
531 | ||
532 | ul->north_south += (vvp->ympp * vvp->height / 2); | |
533 | ul->east_west -= (vvp->xmpp * vvp->width / 2); | |
534 | br->north_south -= (vvp->ympp * vvp->height / 2); | |
535 | br->east_west += (vvp->xmpp * vvp->width / 2); | |
536 | } | |
537 | ||
538 | void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone) | |
539 | { | |
540 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
541 | *center = *((struct UTM *)(vik_viewport_get_center ( vvp ))); | |
542 | center->easting -= ( zone - center->zone ) * vvp->utm_zone_width; | |
543 | center->zone = zone; | |
544 | } | |
545 | } | |
546 | ||
547 | gchar vik_viewport_leftmost_zone ( VikViewport *vvp ) | |
548 | { | |
549 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
550 | VikCoord coord; | |
551 | g_assert ( vvp != NULL ); | |
552 | vik_viewport_screen_to_coord ( vvp, 0, 0, &coord ); | |
553 | return coord.utm_zone; | |
554 | } | |
555 | return '\0'; | |
556 | } | |
557 | ||
558 | gchar vik_viewport_rightmost_zone ( VikViewport *vvp ) | |
559 | { | |
560 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
561 | VikCoord coord; | |
562 | g_assert ( vvp != NULL ); | |
563 | vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord ); | |
564 | return coord.utm_zone; | |
565 | } | |
566 | return '\0'; | |
567 | } | |
568 | ||
569 | ||
570 | void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y ) | |
571 | { | |
572 | g_return_if_fail ( vvp != NULL ); | |
573 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
574 | /* slightly optimized */ | |
575 | vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2)); | |
576 | vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y); | |
577 | viewport_utm_zone_check ( vvp ); | |
578 | } else { | |
579 | VikCoord tmp; | |
580 | vik_viewport_screen_to_coord ( vvp, x, y, &tmp ); | |
581 | vik_viewport_set_center_coord ( vvp, &tmp ); | |
582 | } | |
583 | } | |
584 | ||
585 | gint vik_viewport_get_width( VikViewport *vvp ) | |
586 | { | |
587 | g_return_val_if_fail ( vvp != NULL, 0 ); | |
588 | return vvp->width; | |
589 | } | |
590 | ||
591 | gint vik_viewport_get_height( VikViewport *vvp ) | |
592 | { | |
593 | g_return_val_if_fail ( vvp != NULL, 0 ); | |
594 | return vvp->height; | |
595 | } | |
596 | ||
597 | void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord ) | |
598 | { | |
599 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
600 | int zone_delta; | |
601 | struct UTM *utm = (struct UTM *) coord; | |
602 | coord->mode = VIK_COORD_UTM; | |
603 | ||
604 | g_return_if_fail ( vvp != NULL ); | |
605 | ||
606 | utm->zone = vvp->center.utm_zone; | |
607 | utm->letter = vvp->center.utm_letter; | |
608 | utm->easting = ( ( x - ( vvp->width / 2) ) * vvp->xmpp ) + vvp->center.east_west; | |
609 | zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 ); | |
610 | utm->zone += zone_delta; | |
611 | utm->easting -= zone_delta * vvp->utm_zone_width; | |
612 | utm->northing = ( ( ( vvp->height / 2) - y ) * vvp->ympp ) + vvp->center.north_south; | |
613 | } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { | |
614 | coord->mode = VIK_COORD_LATLON; | |
615 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) | |
616 | 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); | |
617 | else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) { | |
618 | /* google */ | |
619 | coord->east_west = (x - (vvp->width/2)) * vvp->google_calcx_rev_fact + vvp->center.east_west; | |
620 | coord->north_south = ((vvp->height/2) - y) * vvp->google_calcy_rev_fact + vvp->center.north_south; | |
621 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) { | |
622 | coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2)); | |
623 | coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)); | |
624 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { | |
625 | /* FIXMERCATOR */ | |
626 | coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width/2)); | |
627 | coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) ); | |
628 | ||
629 | #if 0 | |
630 | --> THIS IS JUNK HERE. | |
631 | *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0; | |
632 | ||
633 | (*y - vvp->height/2) / 256 / 65536 * 180 * vvp->ympp = (MERCLAT(center->lat) - MERCLAT(ll->lat); | |
634 | DML((180.0 * vvp->ympp / 65536 / 256 * (vvp->height/2 - y)) + ML(cl)) = ll | |
635 | #endif | |
636 | } | |
637 | } | |
638 | } | |
639 | ||
640 | void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y ) | |
641 | { | |
642 | static VikCoord tmp; | |
643 | g_return_if_fail ( vvp != NULL ); | |
644 | ||
645 | if ( coord->mode != vvp->coord_mode ) | |
646 | { | |
647 | g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!"); | |
648 | vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp ); | |
649 | coord = &tmp; | |
650 | } | |
651 | ||
652 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
653 | struct UTM *center = (struct UTM *) &(vvp->center); | |
654 | struct UTM *utm = (struct UTM *) coord; | |
655 | if ( center->zone != utm->zone && vvp->one_utm_zone ) | |
656 | { | |
657 | *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE; | |
658 | return; | |
659 | } | |
660 | ||
661 | *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width / 2) - | |
662 | (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp; | |
663 | *y = (vvp->height / 2) - ( (utm->northing - center->northing) / vvp->ympp ); | |
664 | } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { | |
665 | struct LatLon *center = (struct LatLon *) &(vvp->center); | |
666 | struct LatLon *ll = (struct LatLon *) coord; | |
667 | double xx,yy; | |
668 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) { | |
669 | 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 ); | |
670 | *x = xx; *y = yy; | |
671 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) { | |
672 | /* google */ | |
673 | *x = vvp->google_calcx_fact * (ll->lon - center->lon) + (vvp->width/2); | |
674 | *y = vvp->google_calcy_fact * (center->lat - ll->lat) + (vvp->height/2); | |
675 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_KH ) { | |
676 | /* subtract, convert to KH coords; blow it up by 256 */ | |
677 | *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0; | |
678 | *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (center->lat - ll->lat))*256.0; | |
679 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { | |
680 | /* FIXMERCATOR: Optimize */ | |
681 | *x = vvp->width/2 + (65536.0 / 180 / vvp->xmpp * (ll->lon - center->lon))*256.0; | |
682 | *y = vvp->height/2 + (65536.0 / 180 / vvp->ympp * (MERCLAT(center->lat) - MERCLAT(ll->lat)))*256.0; | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
d9ffd267 EB |
687 | void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 ) |
688 | { | |
689 | if ( *x1 > 20000 || *x1 < -20000 ) { | |
690 | gdouble shrinkfactor = ABS(20000.0 / *x1); | |
691 | *x1 = *x2 + (shrinkfactor * (*x1-*x2)); | |
692 | *y1 = *y2 + (shrinkfactor * (*y1-*y2)); | |
693 | } else if ( *y1 > 20000 || *y1 < -20000 ) { | |
694 | gdouble shrinkfactor = ABS(20000.0 / *x1); | |
695 | *x1 = *x2 + (shrinkfactor * (*x1-*x2)); | |
696 | *y1 = *y2 + (shrinkfactor * (*y1-*y2)); | |
697 | } else if ( *x2 > 20000 || *x2 < -20000 ) { | |
698 | gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2); | |
699 | *x2 = *x1 + (shrinkfactor * (*x2-*x1)); | |
700 | *y2 = *y1 + (shrinkfactor * (*y2-*y1)); | |
701 | printf("%f, %d, %d\n", shrinkfactor, *x2, *y2); | |
702 | } else if ( *y2 > 20000 || *y2 < -20000 ) { | |
703 | gdouble shrinkfactor = ABS(20000.0 / (gdouble)*x2); | |
704 | *x2 = *x1 + (shrinkfactor * (*x2-*x1)); | |
705 | *y2 = *y1 + (shrinkfactor * (*y2-*y1)); | |
706 | } | |
707 | } | |
708 | ||
50a14534 EB |
709 | void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 ) |
710 | { | |
711 | if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) || | |
d9ffd267 EB |
712 | ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) { |
713 | /*** clipping, yeah! ***/ | |
714 | a_viewport_clip_line ( &x1, &y1, &x2, &y2 ); | |
715 | gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2); | |
716 | } | |
50a14534 EB |
717 | } |
718 | ||
719 | void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 ) | |
720 | { | |
721 | if ( x1 > -10 && x1 < vvp->width + 10 && y1 > -10 && y1 < vvp->height + 10 ) | |
722 | gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2); | |
723 | } | |
724 | ||
725 | void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string ) | |
726 | { | |
727 | if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 ) | |
728 | gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string ); | |
729 | } | |
730 | ||
731 | /* shouldn't use this -- slow -- change the alpha channel instead. */ | |
732 | void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha, | |
733 | gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h ) | |
734 | { | |
735 | gint real_dest_x = MAX(dest_x,0); | |
736 | gint real_dest_y = MAX(dest_y,0); | |
737 | ||
738 | if ( alpha == 0 ) | |
739 | return; /* don't waste your time */ | |
740 | ||
741 | if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height ) | |
742 | { | |
743 | if ( vvp->alpha_pixbuf ) | |
744 | g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) ); | |
745 | vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width); | |
746 | vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height); | |
747 | vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height ); | |
748 | } | |
749 | ||
750 | w = MIN(w,vvp->width - dest_x); | |
751 | h = MIN(h,vvp->height - dest_y); | |
752 | ||
753 | /* check that we are drawing within boundaries. */ | |
754 | src_x += (real_dest_x - dest_x); | |
755 | src_y += (real_dest_y - dest_y); | |
756 | w -= (real_dest_x - dest_x); | |
757 | h -= (real_dest_y - dest_y); | |
758 | ||
759 | gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL, | |
760 | real_dest_x, real_dest_y, 0, 0, w, h ); | |
761 | ||
762 | /* do a composite */ | |
763 | gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha ); | |
764 | ||
765 | /* draw pixbuf_tmp */ | |
766 | vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h ); | |
767 | } | |
768 | ||
769 | void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y, | |
770 | gint dest_x, gint dest_y, gint w, gint h ) | |
771 | { | |
772 | gdk_draw_pixbuf ( vvp->scr_buffer, | |
773 | // GTK_WIDGET(vvp)->style->black_gc, | |
774 | NULL, | |
775 | pixbuf, | |
776 | src_x, src_y, dest_x, dest_y, w, h, | |
777 | GDK_RGB_DITHER_NONE, 0, 0 ); | |
778 | } | |
779 | ||
780 | void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 ) | |
781 | { | |
782 | gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 ); | |
783 | } | |
784 | ||
785 | ||
786 | void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints ) | |
787 | { | |
788 | gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints ); | |
789 | } | |
790 | ||
791 | VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp ) | |
792 | { | |
793 | g_assert ( vvp ); | |
794 | return vvp->coord_mode; | |
795 | } | |
796 | ||
797 | static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode ) | |
798 | { | |
799 | g_return_if_fail ( vvp != NULL ); | |
800 | vvp->coord_mode = mode; | |
801 | vik_coord_convert ( &(vvp->center), mode ); | |
802 | } | |
803 | ||
804 | /* Thanks GPSDrive */ | |
805 | 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 ) | |
806 | { | |
807 | int px, py; | |
808 | gdouble dif, lat, lon; | |
809 | double Ra = Radius[90+(gint)zero_lat]; | |
810 | ||
811 | px = (mapSizeX2 - x) * pixelfact_x; | |
812 | py = (-mapSizeY2 + y) * pixelfact_y; | |
813 | ||
814 | lat = zero_lat - py / Ra; | |
815 | lat = zero_lat - py / Ra; | |
816 | lon = | |
817 | zero_long - | |
818 | px / (Ra * | |
819 | cos (lat * DEG2RAD)); | |
820 | ||
821 | dif = lat * (1 - (cos ((fabs (lon - zero_long)) * DEG2RAD))); | |
822 | lat = lat - dif / 1.5; | |
823 | lon = | |
824 | zero_long - | |
825 | px / (Ra * | |
826 | cos (lat * DEG2RAD)); | |
827 | ||
828 | *lt = lat; | |
829 | *lg = lon; | |
830 | return (TRUE); | |
831 | } | |
832 | ||
833 | /* Thanks GPSDrive */ | |
834 | 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 ) | |
835 | { | |
836 | double dif; | |
837 | double Ra; | |
838 | gint mapSizeX = 2 * mapSizeX2; | |
839 | gint mapSizeY = 2 * mapSizeY2; | |
840 | ||
841 | g_assert ( lt >= -90.0 && lt <= 90.0 ); | |
842 | // lg *= rad2deg; // FIXME, optimize equations | |
843 | // lt *= rad2deg; | |
844 | Ra = Radius[90+(gint)lt]; | |
845 | *x = Ra * | |
846 | cos (lt*DEG2RAD) * (lg - zero_long); | |
847 | *y = Ra * (lt - zero_lat); | |
848 | dif = Ra * RAD2DEG * (1 - (cos ((DEG2RAD * (lg - zero_long))))); | |
849 | *y = *y + dif / 1.85; | |
850 | *x = *x / pixelfact_x; | |
851 | *y = *y / pixelfact_y; | |
852 | *x = mapSizeX2 - *x; | |
853 | *y += mapSizeY2; | |
854 | if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY)) | |
855 | return (FALSE); | |
856 | return (TRUE); | |
857 | } | |
858 | ||
859 | static void viewport_init_ra() | |
860 | { | |
861 | static gboolean done_before = FALSE; | |
862 | if ( !done_before ) | |
863 | { | |
864 | gint i; | |
865 | for ( i = -90; i <= 90; i++) | |
866 | Radius[i+90] = calcR ( (double)i ) * DEG2RAD; | |
867 | done_before = TRUE; | |
868 | } | |
869 | } | |
870 | ||
871 | double calcR (double lat) | |
872 | { | |
873 | double a = 6378.137, r, sc, x, y, z; | |
874 | double e2 = 0.081082 * 0.081082; | |
875 | /* | |
876 | * the radius of curvature of an ellipsoidal Earth in the plane of the | |
877 | * meridian is given by | |
878 | * | |
879 | * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2) | |
880 | * | |
881 | * | |
882 | * where a is the equatorial radius, b is the polar radius, and e is | |
883 | * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2) | |
884 | * | |
885 | * a = 6378 km (3963 mi) Equatorial radius (surface to center distance) | |
886 | * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e | |
887 | * = 0.081082 Eccentricity | |
888 | */ | |
889 | ||
890 | lat = lat * DEG2RAD; | |
891 | sc = sin (lat); | |
892 | x = a * (1.0 - e2); | |
893 | z = 1.0 - e2 * sc * sc; | |
894 | y = pow (z, 1.5); | |
895 | r = x / y; | |
896 | r = r * 1000.0; | |
897 | return r; | |
898 | } | |
899 | ||
900 | gboolean vik_viewport_is_one_zone ( VikViewport *vvp ) | |
901 | { | |
902 | return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone; | |
903 | } | |
904 | ||
905 | void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout ) | |
906 | { | |
907 | if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 ) | |
908 | gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout ); | |
909 | } | |
910 | ||
911 | void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest ) | |
912 | { | |
913 | static GdkGCValues values; | |
914 | gdk_gc_get_values ( gc, &values ); | |
915 | gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest ); | |
916 | } | |
917 | ||
918 | GdkFunction vik_gc_get_function ( GdkGC *gc ) | |
919 | { | |
920 | static GdkGCValues values; | |
921 | gdk_gc_get_values ( gc, &values ); | |
922 | return values.function; | |
923 | } | |
924 | ||
925 | void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode ) | |
926 | { | |
927 | vvp->drawmode = drawmode; | |
928 | if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
929 | viewport_set_coord_mode ( vvp, VIK_COORD_UTM ); | |
930 | else { | |
931 | viewport_set_coord_mode ( vvp, VIK_COORD_LATLON ); | |
932 | if ( drawmode == VIK_VIEWPORT_DRAWMODE_GOOGLE ) | |
933 | viewport_google_rezoom ( vvp ); | |
934 | } | |
935 | } | |
936 | ||
937 | VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp ) | |
938 | { | |
939 | return vvp->drawmode; | |
940 | } | |
941 | ||
942 | static void viewport_google_rezoom ( VikViewport *vvp ) | |
943 | { | |
944 | vvp->google_calcx_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 * 0.7716245833877 / vvp->xmpp); | |
945 | vvp->google_calcy_fact = (GOOGLEMAPS_ZOOM_ONE_MPP * 65536.0 / vvp->ympp); | |
946 | vvp->google_calcx_rev_fact = 1 / vvp->google_calcx_fact; | |
947 | vvp->google_calcy_rev_fact = 1 / vvp->google_calcy_fact; | |
948 | } |