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