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