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