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