]>
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> |
be5554c5 | 5 | * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com> |
50a14534 EB |
6 | * |
7 | * Lat/Lon plotting functions calcxy* are from GPSDrive | |
8 | * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at> | |
9 | * | |
10 | * Multiple UTM zone patch by Kit Transue <notlostyet@didactek.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
25 | * | |
26 | */ | |
8c00358d GB |
27 | #ifdef HAVE_CONFIG_H |
28 | #include "config.h" | |
29 | #endif | |
50a14534 EB |
30 | |
31 | #define DEFAULT_BACKGROUND_COLOR "#CCCCCC" | |
480fb7e1 RN |
32 | #define DEFAULT_HIGHLIGHT_COLOR "#EEA500" |
33 | /* Default highlight in orange */ | |
50a14534 EB |
34 | |
35 | #include <gtk/gtk.h> | |
8c00358d | 36 | #ifdef HAVE_MATH_H |
50a14534 | 37 | #include <math.h> |
8c00358d | 38 | #endif |
82aa018d GB |
39 | #ifdef HAVE_STRING_H |
40 | #include <string.h> | |
41 | #endif | |
50a14534 EB |
42 | |
43 | #include "coords.h" | |
44 | #include "vikcoord.h" | |
a59e6eb9 | 45 | #include "vikwindow.h" |
50a14534 | 46 | #include "vikviewport.h" |
50a14534 EB |
47 | #include "mapcoord.h" |
48 | ||
49 | /* for ALTI_TO_MPP */ | |
50 | #include "globals.h" | |
7143d1e4 | 51 | #include "settings.h" |
50a14534 | 52 | |
a3603298 RN |
53 | #define MERCATOR_FACTOR(x) ( (65536.0 / 180 / (x)) * 256.0 ) |
54 | ||
50a14534 EB |
55 | static gdouble EASTING_OFFSET = 500000.0; |
56 | ||
26336cf0 GB |
57 | static gint PAD = 10; |
58 | ||
50a14534 EB |
59 | static void viewport_finalize ( GObject *gob ); |
60 | static void viewport_utm_zone_check ( VikViewport *vvp ); | |
be5554c5 RN |
61 | static void update_centers ( VikViewport *vvp ); |
62 | static void free_centers ( VikViewport *vvp, guint start ); | |
50a14534 EB |
63 | |
64 | 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 ); | |
65 | 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 ); | |
66 | double calcR (double lat); | |
67 | ||
68 | static double Radius[181]; | |
69 | static void viewport_init_ra(); | |
70 | ||
71 | static GObjectClass *parent_class; | |
72 | ||
50a14534 EB |
73 | struct _VikViewport { |
74 | GtkDrawingArea drawing_area; | |
75 | GdkPixmap *scr_buffer; | |
76 | gint width, height; | |
a3603298 | 77 | gint width_2, height_2; // Half of the normal width and height |
50a14534 EB |
78 | VikCoord center; |
79 | VikCoordMode coord_mode; | |
80 | gdouble xmpp, ympp; | |
a3603298 | 81 | gdouble xmfactor, ymfactor; |
be5554c5 RN |
82 | GList *centers; // The history of requested positions |
83 | guint centers_index; // current position within the history list | |
84 | guint centers_max; // configurable maximum size of the history list | |
85 | guint centers_radius; // Metres | |
50a14534 EB |
86 | |
87 | GdkPixbuf *alpha_pixbuf; | |
88 | guint8 alpha_pixbuf_width; | |
89 | guint8 alpha_pixbuf_height; | |
90 | ||
91 | gdouble utm_zone_width; | |
92 | gboolean one_utm_zone; | |
93 | ||
94 | GdkGC *background_gc; | |
95 | GdkColor background_color; | |
4e485bf3 | 96 | GdkGC *scale_bg_gc; |
82aa018d GB |
97 | |
98 | GSList *copyrights; | |
26336cf0 | 99 | GSList *logos; |
82aa018d GB |
100 | |
101 | /* Wether or not display OSD info */ | |
35c7c0ba | 102 | gboolean draw_scale; |
c933487f | 103 | gboolean draw_centermark; |
2afcef36 | 104 | gboolean draw_highlight; |
480fb7e1 RN |
105 | GdkGC *highlight_gc; |
106 | GdkColor highlight_color; | |
50a14534 EB |
107 | |
108 | /* subset of coord types. lat lon can be plotted in 2 ways, google or exp. */ | |
109 | VikViewportDrawMode drawmode; | |
110 | ||
0df66d57 EB |
111 | /* trigger stuff */ |
112 | gpointer trigger; | |
113 | GdkPixmap *snapshot_buffer; | |
114 | gboolean half_drawn; | |
50a14534 EB |
115 | }; |
116 | ||
117 | static gdouble | |
118 | viewport_utm_zone_width ( VikViewport *vvp ) | |
119 | { | |
120 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
121 | struct LatLon ll; | |
122 | ||
123 | /* get latitude of screen bottom */ | |
124 | struct UTM utm = *((struct UTM *)(vik_viewport_get_center ( vvp ))); | |
125 | utm.northing -= vvp -> height * vvp -> ympp / 2; | |
126 | a_coords_utm_to_latlon ( &utm, &ll ); | |
127 | ||
128 | /* boundary */ | |
129 | ll.lon = (utm.zone - 1) * 6 - 180 ; | |
130 | a_coords_latlon_to_utm ( &ll, &utm); | |
131 | return fabs ( utm.easting - EASTING_OFFSET ) * 2; | |
132 | } else | |
133 | return 0.0; | |
134 | } | |
135 | ||
be5554c5 RN |
136 | enum { |
137 | VW_UPDATED_CENTER_SIGNAL = 0, | |
138 | VW_LAST_SIGNAL, | |
139 | }; | |
140 | static guint viewport_signals[VW_LAST_SIGNAL] = { 0 }; | |
141 | ||
eb560cd6 | 142 | G_DEFINE_TYPE (VikViewport, vik_viewport, GTK_TYPE_DRAWING_AREA) |
50a14534 | 143 | |
eb560cd6 GB |
144 | static void |
145 | vik_viewport_class_init ( VikViewportClass *klass ) | |
50a14534 EB |
146 | { |
147 | /* Destructor */ | |
148 | GObjectClass *object_class; | |
149 | ||
150 | object_class = G_OBJECT_CLASS (klass); | |
151 | ||
152 | object_class->finalize = viewport_finalize; | |
153 | ||
154 | parent_class = g_type_class_peek_parent (klass); | |
be5554c5 RN |
155 | |
156 | viewport_signals[VW_UPDATED_CENTER_SIGNAL] = g_signal_new ( "updated_center", G_TYPE_FROM_CLASS (klass), | |
157 | G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikViewportClass, updated_center), NULL, NULL, | |
158 | g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
50a14534 EB |
159 | } |
160 | ||
24277274 | 161 | VikViewport *vik_viewport_new () |
50a14534 | 162 | { |
a59e6eb9 | 163 | VikViewport *vv = VIK_VIEWPORT ( g_object_new ( VIK_VIEWPORT_TYPE, NULL ) ); |
a59e6eb9 | 164 | return vv; |
50a14534 EB |
165 | } |
166 | ||
7143d1e4 RN |
167 | #define VIK_SETTINGS_VIEW_LAST_LATITUDE "viewport_last_latitude" |
168 | #define VIK_SETTINGS_VIEW_LAST_LONGITUDE "viewport_last_longitude" | |
169 | #define VIK_SETTINGS_VIEW_LAST_ZOOM_X "viewport_last_zoom_xpp" | |
170 | #define VIK_SETTINGS_VIEW_LAST_ZOOM_Y "viewport_last_zoom_ypp" | |
be5554c5 RN |
171 | #define VIK_SETTINGS_VIEW_HISTORY_SIZE "viewport_history_size" |
172 | #define VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST "viewport_history_diff_dist" | |
7143d1e4 | 173 | |
eb560cd6 GB |
174 | static void |
175 | vik_viewport_init ( VikViewport *vvp ) | |
50a14534 EB |
176 | { |
177 | viewport_init_ra(); | |
178 | ||
5210c3d3 GB |
179 | struct UTM utm; |
180 | struct LatLon ll; | |
181 | ll.lat = a_vik_get_default_lat(); | |
182 | ll.lon = a_vik_get_default_long(); | |
7143d1e4 RN |
183 | gdouble zoom_x = 4.0; |
184 | gdouble zoom_y = 4.0; | |
185 | ||
186 | if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) { | |
187 | gdouble lat, lon, dzoom; | |
188 | if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, &lat ) ) | |
189 | ll.lat = lat; | |
190 | if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, &lon ) ) | |
191 | ll.lon = lon; | |
192 | if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, &dzoom ) ) | |
193 | zoom_x = dzoom; | |
194 | if ( a_settings_get_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, &dzoom ) ) | |
195 | zoom_y = dzoom; | |
196 | } | |
197 | ||
5210c3d3 GB |
198 | a_coords_latlon_to_utm ( &ll, &utm ); |
199 | ||
7143d1e4 RN |
200 | vvp->xmpp = zoom_x; |
201 | vvp->ympp = zoom_y; | |
a3603298 RN |
202 | vvp->xmfactor = MERCATOR_FACTOR (vvp->xmpp); |
203 | vvp->ymfactor = MERCATOR_FACTOR (vvp->ympp); | |
6a9ff0ee QT |
204 | vvp->coord_mode = VIK_COORD_LATLON; |
205 | vvp->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR; | |
ee180665 | 206 | vvp->center.mode = VIK_COORD_LATLON; |
5210c3d3 GB |
207 | vvp->center.north_south = ll.lat; |
208 | vvp->center.east_west = ll.lon; | |
209 | vvp->center.utm_zone = (int)utm.zone; | |
210 | vvp->center.utm_letter = utm.letter; | |
50a14534 EB |
211 | vvp->scr_buffer = NULL; |
212 | vvp->alpha_pixbuf = NULL; | |
213 | vvp->alpha_pixbuf_width = vvp->alpha_pixbuf_height = 0; | |
214 | vvp->utm_zone_width = 0.0; | |
215 | vvp->background_gc = NULL; | |
480fb7e1 | 216 | vvp->highlight_gc = NULL; |
4e485bf3 | 217 | vvp->scale_bg_gc = NULL; |
82aa018d GB |
218 | |
219 | vvp->copyrights = NULL; | |
be5554c5 RN |
220 | vvp->centers = NULL; |
221 | vvp->centers_index = 0; | |
222 | vvp->centers_max = 20; | |
223 | gint tmp = vvp->centers_max; | |
224 | if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_SIZE, &tmp ) ) | |
225 | vvp->centers_max = tmp; | |
226 | vvp->centers_radius = 500; | |
227 | if ( a_settings_get_integer ( VIK_SETTINGS_VIEW_HISTORY_DIFF_DIST, &tmp ) ) | |
228 | vvp->centers_radius = tmp; | |
82aa018d | 229 | |
35c7c0ba | 230 | vvp->draw_scale = TRUE; |
c933487f | 231 | vvp->draw_centermark = TRUE; |
2afcef36 | 232 | vvp->draw_highlight = TRUE; |
0df66d57 EB |
233 | |
234 | vvp->trigger = NULL; | |
235 | vvp->snapshot_buffer = NULL; | |
236 | vvp->half_drawn = FALSE; | |
237 | ||
be5554c5 RN |
238 | // Initiate center history |
239 | update_centers ( vvp ); | |
240 | ||
50a14534 | 241 | g_signal_connect (G_OBJECT(vvp), "configure_event", G_CALLBACK(vik_viewport_configure), NULL); |
165d30aa | 242 | |
2fd7c35e RN |
243 | #if GTK_CHECK_VERSION (2,18,0) |
244 | gtk_widget_set_can_focus ( GTK_WIDGET(vvp), TRUE ); | |
245 | #else | |
165d30aa | 246 | GTK_WIDGET_SET_FLAGS(vvp, GTK_CAN_FOCUS); /* allow VVP to have focus -- enabling key events, etc */ |
2fd7c35e | 247 | #endif |
50a14534 EB |
248 | } |
249 | ||
250 | GdkColor *vik_viewport_get_background_gdkcolor ( VikViewport *vvp ) | |
251 | { | |
252 | GdkColor *rv = g_malloc ( sizeof ( GdkColor ) ); | |
253 | *rv = vvp->background_color; | |
254 | return rv; | |
255 | } | |
256 | ||
257 | /* returns pointer to internal static storage, changes next time function called, use quickly */ | |
258 | const gchar *vik_viewport_get_background_color ( VikViewport *vvp ) | |
259 | { | |
260 | static gchar color[8]; | |
261 | 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)); | |
262 | return color; | |
263 | } | |
264 | ||
265 | void vik_viewport_set_background_color ( VikViewport *vvp, const gchar *colorname ) | |
266 | { | |
52aa15a7 | 267 | g_assert ( vvp && vvp->background_gc ); |
b244bc6a GB |
268 | if ( gdk_color_parse ( colorname, &(vvp->background_color) ) ) |
269 | gdk_gc_set_rgb_fg_color ( vvp->background_gc, &(vvp->background_color) ); | |
270 | else | |
271 | g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname); | |
50a14534 EB |
272 | } |
273 | ||
274 | void vik_viewport_set_background_gdkcolor ( VikViewport *vvp, GdkColor *color ) | |
275 | { | |
52aa15a7 | 276 | g_assert ( vvp && vvp->background_gc ); |
50a14534 EB |
277 | vvp->background_color = *color; |
278 | gdk_gc_set_rgb_fg_color ( vvp->background_gc, color ); | |
279 | } | |
280 | ||
480fb7e1 RN |
281 | GdkColor *vik_viewport_get_highlight_gdkcolor ( VikViewport *vvp ) |
282 | { | |
283 | GdkColor *rv = g_malloc ( sizeof ( GdkColor ) ); | |
284 | *rv = vvp->highlight_color; | |
285 | return rv; | |
286 | } | |
287 | ||
288 | /* returns pointer to internal static storage, changes next time function called, use quickly */ | |
289 | const gchar *vik_viewport_get_highlight_color ( VikViewport *vvp ) | |
290 | { | |
291 | static gchar color[8]; | |
292 | g_snprintf(color, sizeof(color), "#%.2x%.2x%.2x", (int)(vvp->highlight_color.red/256),(int)(vvp->highlight_color.green/256),(int)(vvp->highlight_color.blue/256)); | |
293 | return color; | |
294 | } | |
295 | ||
296 | void vik_viewport_set_highlight_color ( VikViewport *vvp, const gchar *colorname ) | |
297 | { | |
298 | g_assert ( vvp->highlight_gc ); | |
299 | gdk_color_parse ( colorname, &(vvp->highlight_color) ); | |
300 | gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, &(vvp->highlight_color) ); | |
301 | } | |
302 | ||
303 | void vik_viewport_set_highlight_gdkcolor ( VikViewport *vvp, GdkColor *color ) | |
304 | { | |
305 | g_assert ( vvp->highlight_gc ); | |
306 | vvp->highlight_color = *color; | |
307 | gdk_gc_set_rgb_fg_color ( vvp->highlight_gc, color ); | |
308 | } | |
309 | ||
310 | GdkGC *vik_viewport_get_gc_highlight ( VikViewport *vvp ) | |
311 | { | |
312 | return vvp->highlight_gc; | |
313 | } | |
50a14534 | 314 | |
04f36d92 RN |
315 | void vik_viewport_set_highlight_thickness ( VikViewport *vvp, gint thickness ) |
316 | { | |
317 | // Otherwise same GDK_* attributes as in vik_viewport_new_gc | |
318 | gdk_gc_set_line_attributes ( vvp->highlight_gc, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); | |
319 | } | |
320 | ||
50a14534 EB |
321 | GdkGC *vik_viewport_new_gc ( VikViewport *vvp, const gchar *colorname, gint thickness ) |
322 | { | |
b244bc6a | 323 | GdkGC *rv = NULL; |
50a14534 EB |
324 | GdkColor color; |
325 | ||
9b082b39 | 326 | rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) ); |
b244bc6a GB |
327 | if ( gdk_color_parse ( colorname, &color ) ) |
328 | gdk_gc_set_rgb_fg_color ( rv, &color ); | |
329 | else | |
330 | g_warning("%s: Failed to parse color '%s'", __FUNCTION__, colorname); | |
50a14534 EB |
331 | gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); |
332 | return rv; | |
333 | } | |
334 | ||
335 | GdkGC *vik_viewport_new_gc_from_color ( VikViewport *vvp, GdkColor *color, gint thickness ) | |
336 | { | |
337 | GdkGC *rv; | |
338 | ||
9b082b39 | 339 | rv = gdk_gc_new ( gtk_widget_get_window(GTK_WIDGET(vvp)) ); |
50a14534 EB |
340 | gdk_gc_set_rgb_fg_color ( rv, color ); |
341 | gdk_gc_set_line_attributes ( rv, thickness, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND ); | |
342 | return rv; | |
343 | } | |
344 | ||
345 | void vik_viewport_configure_manually ( VikViewport *vvp, gint width, guint height ) | |
346 | { | |
347 | vvp->width = width; | |
348 | vvp->height = height; | |
a3603298 RN |
349 | |
350 | vvp->width_2 = vvp->width/2; | |
351 | vvp->height_2 = vvp->height/2; | |
352 | ||
50a14534 EB |
353 | if ( vvp->scr_buffer ) |
354 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
9b082b39 | 355 | vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); |
0df66d57 EB |
356 | |
357 | /* TODO trigger: only if this is enabled !!! */ | |
358 | if ( vvp->snapshot_buffer ) | |
359 | g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) ); | |
9b082b39 | 360 | vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); |
50a14534 EB |
361 | } |
362 | ||
363 | ||
364 | GdkPixmap *vik_viewport_get_pixmap ( VikViewport *vvp ) | |
365 | { | |
366 | return vvp->scr_buffer; | |
367 | } | |
368 | ||
369 | gboolean vik_viewport_configure ( VikViewport *vvp ) | |
370 | { | |
371 | g_return_val_if_fail ( vvp != NULL, TRUE ); | |
372 | ||
90ba111c RN |
373 | GtkAllocation allocation; |
374 | gtk_widget_get_allocation ( GTK_WIDGET(vvp), &allocation ); | |
375 | vvp->width = allocation.width; | |
376 | vvp->height = allocation.height; | |
50a14534 | 377 | |
a3603298 RN |
378 | vvp->width_2 = vvp->width/2; |
379 | vvp->height_2 = vvp->height/2; | |
380 | ||
50a14534 EB |
381 | if ( vvp->scr_buffer ) |
382 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
383 | ||
9b082b39 | 384 | vvp->scr_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); |
50a14534 | 385 | |
0df66d57 EB |
386 | /* TODO trigger: only if enabled! */ |
387 | if ( vvp->snapshot_buffer ) | |
388 | g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) ); | |
389 | ||
9b082b39 | 390 | vvp->snapshot_buffer = gdk_pixmap_new ( gtk_widget_get_window(GTK_WIDGET(vvp)), vvp->width, vvp->height, -1 ); |
0df66d57 EB |
391 | /* TODO trigger */ |
392 | ||
50a14534 | 393 | /* this is down here so it can get a GC (necessary?) */ |
e8e82387 | 394 | if ( !vvp->background_gc ) |
50a14534 | 395 | { |
348e8666 | 396 | vvp->background_gc = vik_viewport_new_gc ( vvp, DEFAULT_BACKGROUND_COLOR, 1 ); |
e8e82387 | 397 | vik_viewport_set_background_color ( vvp, DEFAULT_BACKGROUND_COLOR ); |
50a14534 | 398 | } |
480fb7e1 RN |
399 | if ( ! vvp->highlight_gc ) |
400 | { | |
401 | vvp->highlight_gc = vik_viewport_new_gc ( vvp, DEFAULT_HIGHLIGHT_COLOR, 1 ); | |
402 | vik_viewport_set_highlight_color ( vvp, DEFAULT_HIGHLIGHT_COLOR ); | |
403 | } | |
4e485bf3 QT |
404 | if ( !vvp->scale_bg_gc) { |
405 | vvp->scale_bg_gc = vik_viewport_new_gc(vvp, "grey", 3); | |
406 | } | |
50a14534 EB |
407 | |
408 | return FALSE; | |
409 | } | |
410 | ||
411 | static void viewport_finalize ( GObject *gob ) | |
412 | { | |
413 | VikViewport *vvp = VIK_VIEWPORT(gob); | |
414 | ||
415 | g_return_if_fail ( vvp != NULL ); | |
416 | ||
7143d1e4 RN |
417 | if ( a_vik_get_startup_method ( ) == VIK_STARTUP_METHOD_LAST_LOCATION ) { |
418 | struct LatLon ll; | |
419 | vik_coord_to_latlon ( &(vvp->center), &ll ); | |
420 | a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LATITUDE, ll.lat ); | |
421 | a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_LONGITUDE, ll.lon ); | |
422 | a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_X, vvp->xmpp ); | |
423 | a_settings_set_double ( VIK_SETTINGS_VIEW_LAST_ZOOM_Y, vvp->ympp ); | |
424 | } | |
425 | ||
be5554c5 RN |
426 | if ( vvp->centers ) |
427 | free_centers ( vvp, 0 ); | |
428 | ||
50a14534 EB |
429 | if ( vvp->scr_buffer ) |
430 | g_object_unref ( G_OBJECT ( vvp->scr_buffer ) ); | |
431 | ||
0df66d57 EB |
432 | if ( vvp->snapshot_buffer ) |
433 | g_object_unref ( G_OBJECT ( vvp->snapshot_buffer ) ); | |
434 | ||
50a14534 EB |
435 | if ( vvp->alpha_pixbuf ) |
436 | g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) ); | |
437 | ||
438 | if ( vvp->background_gc ) | |
439 | g_object_unref ( G_OBJECT ( vvp->background_gc ) ); | |
440 | ||
480fb7e1 RN |
441 | if ( vvp->highlight_gc ) |
442 | g_object_unref ( G_OBJECT ( vvp->highlight_gc ) ); | |
443 | ||
4e485bf3 QT |
444 | if ( vvp->scale_bg_gc ) { |
445 | g_object_unref ( G_OBJECT ( vvp->scale_bg_gc ) ); | |
446 | vvp->scale_bg_gc = NULL; | |
447 | } | |
448 | ||
50a14534 EB |
449 | G_OBJECT_CLASS(parent_class)->finalize(gob); |
450 | } | |
451 | ||
33e0e500 GB |
452 | /** |
453 | * vik_viewport_clear: | |
454 | * @vvp: self object | |
455 | * | |
456 | * Clear the whole viewport. | |
457 | */ | |
50a14534 EB |
458 | void vik_viewport_clear ( VikViewport *vvp ) |
459 | { | |
460 | g_return_if_fail ( vvp != NULL ); | |
461 | gdk_draw_rectangle(GDK_DRAWABLE(vvp->scr_buffer), vvp->background_gc, TRUE, 0, 0, vvp->width, vvp->height); | |
82aa018d | 462 | vik_viewport_reset_copyrights ( vvp ); |
26336cf0 | 463 | vik_viewport_reset_logos ( vvp ); |
50a14534 EB |
464 | } |
465 | ||
33e0e500 GB |
466 | /** |
467 | * vik_viewport_set_draw_scale: | |
468 | * @vvp: self | |
469 | * @draw_scale: new value | |
470 | * | |
471 | * Enable/Disable display of scale. | |
472 | */ | |
35c7c0ba EB |
473 | void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale ) |
474 | { | |
475 | vvp->draw_scale = draw_scale; | |
476 | } | |
477 | ||
478 | gboolean vik_viewport_get_draw_scale ( VikViewport *vvp ) | |
479 | { | |
480 | return vvp->draw_scale; | |
481 | } | |
482 | ||
acaf7113 AF |
483 | void vik_viewport_draw_scale ( VikViewport *vvp ) |
484 | { | |
c53eda9a GB |
485 | g_return_if_fail ( vvp != NULL ); |
486 | ||
35c7c0ba EB |
487 | if ( vvp->draw_scale ) { |
488 | VikCoord left, right; | |
489 | gdouble unit, base, diff, old_unit, old_diff, ratio; | |
26336cf0 | 490 | gint odd, len, SCSIZE = 5, HEIGHT=10; |
35c7c0ba EB |
491 | PangoLayout *pl; |
492 | gchar s[128]; | |
acaf7113 | 493 | |
35c7c0ba EB |
494 | vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left ); |
495 | vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right ); | |
acaf7113 | 496 | |
6f9336aa RN |
497 | vik_units_distance_t dist_units = a_vik_get_units_distance (); |
498 | switch (dist_units) { | |
499 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
500 | base = vik_coord_diff ( &left, &right ); // in meters | |
501 | break; | |
502 | case VIK_UNITS_DISTANCE_MILES: | |
8d772d8a | 503 | // in 0.1 miles (copes better when zoomed in as 1 mile can be too big) |
433b3f7f | 504 | base = VIK_METERS_TO_MILES(vik_coord_diff ( &left, &right )) * 10.0; |
6f9336aa RN |
505 | break; |
506 | default: | |
507 | base = 1; // Keep the compiler happy | |
508 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
509 | } | |
35c7c0ba | 510 | ratio = (vvp->width/SCSIZE)/base; |
acaf7113 | 511 | |
35c7c0ba EB |
512 | unit = 1; |
513 | diff = fabs(base-unit); | |
acaf7113 AF |
514 | old_unit = unit; |
515 | old_diff = diff; | |
35c7c0ba EB |
516 | odd = 1; |
517 | while (diff <= old_diff) { | |
518 | old_unit = unit; | |
519 | old_diff = diff; | |
520 | unit = unit * (odd%2 ? 5 : 2); | |
521 | diff = fabs(base-unit); | |
522 | odd++; | |
523 | } | |
524 | unit = old_unit; | |
525 | len = unit * ratio; | |
acaf7113 | 526 | |
4e485bf3 QT |
527 | /* white background */ |
528 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, | |
529 | PAD, vvp->height-PAD, PAD + len, vvp->height-PAD); | |
530 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, | |
531 | PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT); | |
532 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, | |
533 | PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT); | |
534 | /* black scale */ | |
ff37db21 | 535 | vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
acaf7113 | 536 | PAD, vvp->height-PAD, PAD + len, vvp->height-PAD); |
ff37db21 | 537 | vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
acaf7113 | 538 | PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT); |
ff37db21 | 539 | vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
acaf7113 | 540 | PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT); |
35c7c0ba EB |
541 | if (odd%2) { |
542 | int i; | |
543 | for (i=1; i<5; i++) { | |
4e485bf3 QT |
544 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, |
545 | PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); | |
ff37db21 | 546 | vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
acaf7113 | 547 | PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); |
35c7c0ba EB |
548 | } |
549 | } else { | |
550 | int i; | |
551 | for (i=1; i<10; i++) { | |
4e485bf3 QT |
552 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, |
553 | PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); | |
ff37db21 | 554 | vik_viewport_draw_line(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
35c7c0ba EB |
555 | PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2))); |
556 | } | |
acaf7113 | 557 | } |
35c7c0ba | 558 | pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); |
ff37db21 | 559 | pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc); |
35c7c0ba | 560 | |
6f9336aa RN |
561 | switch (dist_units) { |
562 | case VIK_UNITS_DISTANCE_KILOMETRES: | |
563 | if (unit >= 1000) { | |
564 | sprintf(s, "%d km", (int)unit/1000); | |
565 | } else { | |
566 | sprintf(s, "%d m", (int)unit); | |
567 | } | |
568 | break; | |
569 | case VIK_UNITS_DISTANCE_MILES: | |
8d772d8a RN |
570 | // Handle units in 0.1 miles |
571 | if (unit < 10.0) { | |
572 | sprintf(s, "%0.1f miles", unit/10.0); | |
573 | } | |
574 | else if ((int)unit == 10.0) { | |
575 | sprintf(s, "1 mile"); | |
6f9336aa RN |
576 | } |
577 | else { | |
8d772d8a | 578 | sprintf(s, "%d miles", (int)(unit/10.0)); |
6f9336aa RN |
579 | } |
580 | break; | |
581 | default: | |
582 | g_critical("Houston, we've had a problem. distance=%d", dist_units); | |
acaf7113 | 583 | } |
35c7c0ba | 584 | pango_layout_set_text(pl, s, -1); |
ff37db21 | 585 | vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
acaf7113 | 586 | PAD + len + PAD, vvp->height - PAD - 10, pl); |
6278ccc6 | 587 | g_object_unref(pl); |
e6869c62 | 588 | pl = NULL; |
35c7c0ba | 589 | } |
acaf7113 AF |
590 | } |
591 | ||
82aa018d GB |
592 | void vik_viewport_draw_copyright ( VikViewport *vvp ) |
593 | { | |
594 | g_return_if_fail ( vvp != NULL ); | |
595 | ||
82aa018d GB |
596 | PangoLayout *pl; |
597 | PangoRectangle ink_rect, logical_rect; | |
598 | gchar s[128] = ""; | |
599 | ||
600 | /* compute copyrights string */ | |
601 | guint len = g_slist_length ( vvp->copyrights ); | |
15a0bd14 | 602 | |
82aa018d GB |
603 | int i; |
604 | for (i = 0 ; i < len ; i++) | |
605 | { | |
15a0bd14 RN |
606 | // Stop when buffer is full |
607 | int slen = strlen ( s ); | |
608 | if ( slen >= 127 ) | |
609 | break; | |
610 | ||
82aa018d | 611 | gchar *copyright = g_slist_nth_data ( vvp->copyrights, i ); |
15a0bd14 RN |
612 | |
613 | // Only use part of this copyright that fits in the available space left | |
614 | // remembering 1 character is left available for the appended space | |
615 | int clen = strlen ( copyright ); | |
616 | if ( slen + clen > 126 ) { | |
617 | clen = 126 - slen; | |
618 | } | |
619 | ||
620 | strncat ( s, copyright, clen ); | |
82aa018d GB |
621 | strcat ( s, " " ); |
622 | } | |
623 | ||
624 | /* create pango layout */ | |
625 | pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL); | |
ff37db21 | 626 | pango_layout_set_font_description (pl, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->font_desc); |
82aa018d GB |
627 | pango_layout_set_alignment ( pl, PANGO_ALIGN_RIGHT ); |
628 | ||
629 | /* Set the text */ | |
630 | pango_layout_set_text(pl, s, -1); | |
631 | ||
632 | /* Use maximum of half the viewport width */ | |
56cb1807 | 633 | pango_layout_set_width ( pl, ( vvp->width / 2 ) * PANGO_SCALE ); |
82aa018d | 634 | pango_layout_get_pixel_extents(pl, &ink_rect, &logical_rect); |
ff37db21 | 635 | vik_viewport_draw_layout(vvp, gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc, |
56cb1807 | 636 | vvp->width / 2, vvp->height - logical_rect.height, pl); |
82aa018d GB |
637 | |
638 | /* Free memory */ | |
639 | g_object_unref(pl); | |
640 | pl = NULL; | |
641 | } | |
642 | ||
33e0e500 GB |
643 | /** |
644 | * vik_viewport_set_draw_centermark: | |
645 | * @vvp: self object | |
646 | * @draw_centermark: new value | |
647 | * | |
648 | * Enable/Disable display of center mark. | |
649 | */ | |
c933487f QT |
650 | void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark ) |
651 | { | |
652 | vvp->draw_centermark = draw_centermark; | |
653 | } | |
654 | ||
655 | gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp ) | |
656 | { | |
657 | return vvp->draw_centermark; | |
658 | } | |
659 | ||
660 | void vik_viewport_draw_centermark ( VikViewport *vvp ) | |
661 | { | |
c53eda9a GB |
662 | g_return_if_fail ( vvp != NULL ); |
663 | ||
c933487f QT |
664 | if ( !vvp->draw_centermark ) |
665 | return; | |
666 | ||
3df69a6b QT |
667 | const int len = 30; |
668 | const int gap = 4; | |
c933487f QT |
669 | int center_x = vvp->width/2; |
670 | int center_y = vvp->height/2; | |
ff37db21 | 671 | GdkGC * black_gc = gtk_widget_get_style(GTK_WIDGET(&vvp->drawing_area))->black_gc; |
c933487f QT |
672 | |
673 | /* white back ground */ | |
3df69a6b QT |
674 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y); |
675 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y); | |
676 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap); | |
677 | vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len); | |
c933487f | 678 | /* black fore ground */ |
3df69a6b QT |
679 | vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y); |
680 | vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y); | |
681 | vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap); | |
682 | vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len); | |
c933487f QT |
683 | |
684 | } | |
685 | ||
26336cf0 GB |
686 | void vik_viewport_draw_logo ( VikViewport *vvp ) |
687 | { | |
688 | g_return_if_fail ( vvp != NULL ); | |
689 | ||
26336cf0 GB |
690 | guint len = g_slist_length ( vvp->logos ); |
691 | gint x = vvp->width - PAD; | |
692 | gint y = PAD; | |
693 | int i; | |
694 | for (i = 0 ; i < len ; i++) | |
695 | { | |
696 | GdkPixbuf *logo = g_slist_nth_data ( vvp->logos, i ); | |
697 | gint width = gdk_pixbuf_get_width ( logo ); | |
698 | gint height = gdk_pixbuf_get_height ( logo ); | |
699 | vik_viewport_draw_pixbuf ( vvp, logo, 0, 0, x - width, y, width, height ); | |
700 | x = x - width - PAD; | |
701 | } | |
2afcef36 | 702 | } |
26336cf0 | 703 | |
2afcef36 RN |
704 | void vik_viewport_set_draw_highlight ( VikViewport *vvp, gboolean draw_highlight ) |
705 | { | |
706 | vvp->draw_highlight = draw_highlight; | |
707 | } | |
708 | ||
709 | gboolean vik_viewport_get_draw_highlight ( VikViewport *vvp ) | |
710 | { | |
711 | return vvp->draw_highlight; | |
26336cf0 GB |
712 | } |
713 | ||
50a14534 EB |
714 | void vik_viewport_sync ( VikViewport *vvp ) |
715 | { | |
716 | g_return_if_fail ( vvp != NULL ); | |
9b082b39 | 717 | gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, 0, 0, vvp->width, vvp->height); |
50a14534 EB |
718 | } |
719 | ||
720 | void vik_viewport_pan_sync ( VikViewport *vvp, gint x_off, gint y_off ) | |
721 | { | |
acaf7113 AF |
722 | gint x, y, wid, hei; |
723 | ||
50a14534 | 724 | g_return_if_fail ( vvp != NULL ); |
9b082b39 | 725 | gdk_draw_drawable(gtk_widget_get_window(GTK_WIDGET(vvp)), gtk_widget_get_style(GTK_WIDGET(vvp))->bg_gc[0], GDK_DRAWABLE(vvp->scr_buffer), 0, 0, x_off, y_off, vvp->width, vvp->height); |
acaf7113 AF |
726 | |
727 | if (x_off >= 0) { | |
728 | x = 0; | |
729 | wid = x_off; | |
730 | } else { | |
731 | x = vvp->width+x_off; | |
732 | wid = -x_off; | |
733 | } | |
734 | if (y_off >= 0) { | |
735 | y = 0; | |
736 | hei = y_off; | |
737 | } else { | |
738 | y = vvp->height+y_off; | |
739 | hei = -y_off; | |
740 | } | |
741 | gtk_widget_queue_draw_area(GTK_WIDGET(vvp), x, 0, wid, vvp->height); | |
742 | gtk_widget_queue_draw_area(GTK_WIDGET(vvp), 0, y, vvp->width, hei); | |
50a14534 EB |
743 | } |
744 | ||
745 | void vik_viewport_set_zoom ( VikViewport *vvp, gdouble xympp ) | |
746 | { | |
747 | g_return_if_fail ( vvp != NULL ); | |
a3603298 | 748 | if ( xympp >= VIK_VIEWPORT_MIN_ZOOM && xympp <= VIK_VIEWPORT_MAX_ZOOM ) { |
50a14534 | 749 | vvp->xmpp = vvp->ympp = xympp; |
a3603298 RN |
750 | // Since xmpp & ympp are the same it doesn't matter which one is used here |
751 | vvp->xmfactor = vvp->ymfactor = MERCATOR_FACTOR(vvp->xmpp); | |
752 | } | |
50a14534 EB |
753 | |
754 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
755 | viewport_utm_zone_check(vvp); | |
50a14534 EB |
756 | } |
757 | ||
758 | /* or could do factor */ | |
759 | void vik_viewport_zoom_in ( VikViewport *vvp ) | |
760 | { | |
761 | g_return_if_fail ( vvp != NULL ); | |
762 | if ( vvp->xmpp >= (VIK_VIEWPORT_MIN_ZOOM*2) && vvp->ympp >= (VIK_VIEWPORT_MIN_ZOOM*2) ) | |
763 | { | |
764 | vvp->xmpp /= 2; | |
765 | vvp->ympp /= 2; | |
766 | ||
a3603298 RN |
767 | vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp); |
768 | vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); | |
769 | ||
50a14534 EB |
770 | viewport_utm_zone_check(vvp); |
771 | } | |
772 | } | |
773 | ||
774 | void vik_viewport_zoom_out ( VikViewport *vvp ) | |
775 | { | |
776 | g_return_if_fail ( vvp != NULL ); | |
777 | if ( vvp->xmpp <= (VIK_VIEWPORT_MAX_ZOOM/2) && vvp->ympp <= (VIK_VIEWPORT_MAX_ZOOM/2) ) | |
778 | { | |
779 | vvp->xmpp *= 2; | |
780 | vvp->ympp *= 2; | |
781 | ||
a3603298 RN |
782 | vvp->xmfactor = MERCATOR_FACTOR(vvp->xmpp); |
783 | vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); | |
784 | ||
50a14534 EB |
785 | viewport_utm_zone_check(vvp); |
786 | } | |
787 | } | |
788 | ||
789 | gdouble vik_viewport_get_zoom ( VikViewport *vvp ) | |
790 | { | |
791 | if ( vvp->xmpp == vvp->ympp ) | |
792 | return vvp->xmpp; | |
793 | return 0.0; | |
794 | } | |
795 | ||
796 | gdouble vik_viewport_get_xmpp ( VikViewport *vvp ) | |
797 | { | |
798 | return vvp->xmpp; | |
799 | } | |
800 | ||
801 | gdouble vik_viewport_get_ympp ( VikViewport *vvp ) | |
802 | { | |
803 | return vvp->ympp; | |
804 | } | |
805 | ||
806 | void vik_viewport_set_xmpp ( VikViewport *vvp, gdouble xmpp ) | |
807 | { | |
808 | if ( xmpp >= VIK_VIEWPORT_MIN_ZOOM && xmpp <= VIK_VIEWPORT_MAX_ZOOM ) { | |
809 | vvp->xmpp = xmpp; | |
a3603298 | 810 | vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); |
50a14534 EB |
811 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) |
812 | viewport_utm_zone_check(vvp); | |
50a14534 EB |
813 | } |
814 | } | |
815 | ||
816 | void vik_viewport_set_ympp ( VikViewport *vvp, gdouble ympp ) | |
817 | { | |
818 | if ( ympp >= VIK_VIEWPORT_MIN_ZOOM && ympp <= VIK_VIEWPORT_MAX_ZOOM ) { | |
819 | vvp->ympp = ympp; | |
a3603298 | 820 | vvp->ymfactor = MERCATOR_FACTOR(vvp->ympp); |
50a14534 EB |
821 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) |
822 | viewport_utm_zone_check(vvp); | |
50a14534 EB |
823 | } |
824 | } | |
825 | ||
826 | ||
827 | const VikCoord *vik_viewport_get_center ( VikViewport *vvp ) | |
828 | { | |
829 | g_return_val_if_fail ( vvp != NULL, NULL ); | |
830 | return &(vvp->center); | |
831 | } | |
832 | ||
833 | /* called every time we update coordinates/zoom */ | |
834 | static void viewport_utm_zone_check ( VikViewport *vvp ) | |
835 | { | |
836 | if ( vvp->coord_mode == VIK_COORD_UTM ) | |
837 | { | |
838 | struct UTM utm; | |
839 | struct LatLon ll; | |
840 | a_coords_utm_to_latlon ( (struct UTM *) &(vvp->center), &ll ); | |
841 | a_coords_latlon_to_utm ( &ll, &utm ); | |
842 | if ( utm.zone != vvp->center.utm_zone ) | |
843 | *((struct UTM *)(&vvp->center)) = utm; | |
844 | ||
845 | /* misc. stuff so we don't have to check later */ | |
846 | vvp->utm_zone_width = viewport_utm_zone_width ( vvp ); | |
847 | vvp->one_utm_zone = ( vik_viewport_rightmost_zone(vvp) == vik_viewport_leftmost_zone(vvp) ); | |
848 | } | |
849 | } | |
850 | ||
be5554c5 RN |
851 | /** |
852 | * Free an individual center position in the history list | |
853 | */ | |
854 | static void free_center ( VikViewport *vvp, guint index ) | |
855 | { | |
856 | VikCoord *coord = g_list_nth_data ( vvp->centers, index ); | |
857 | if ( coord ) | |
858 | g_free ( coord ); | |
859 | GList *gl = g_list_nth ( vvp->centers, index ); | |
860 | if ( gl ) | |
861 | vvp->centers = g_list_delete_link ( vvp->centers, gl ); | |
862 | } | |
863 | ||
864 | /** | |
865 | * Free a set of center positions in the history list, | |
866 | * from the indicated start index to the end of the list | |
867 | */ | |
868 | static void free_centers ( VikViewport *vvp, guint start ) | |
869 | { | |
870 | // Have to work backward since we delete items referenced by the '_nth()' values, | |
871 | // otherwise if processed forward - removing the lower nth index entries would change the subsequent indexing | |
872 | for ( guint i = g_list_length(vvp->centers)-1; i > start; i-- ) | |
873 | free_center ( vvp, i ); | |
874 | } | |
875 | ||
876 | /** | |
877 | * Store the current center position into the history list | |
878 | * and emit a signal to notify clients the list has been updated | |
879 | */ | |
880 | static void update_centers ( VikViewport *vvp ) | |
881 | { | |
882 | VikCoord *new_center = g_malloc(sizeof (VikCoord)); | |
883 | *new_center = vvp->center; | |
884 | ||
885 | if ( vvp->centers_index ) { | |
886 | ||
887 | if ( vvp->centers_index == vvp->centers_max-1 ) { | |
888 | // List is full, so drop the oldest value to make room for the new one | |
889 | free_center ( vvp, 0 ); | |
890 | vvp->centers_index--; | |
891 | } | |
892 | else { | |
893 | // Reset the now unused section of the list | |
894 | // Free from the index to the end | |
895 | free_centers ( vvp, vvp->centers_index+1 ); | |
896 | } | |
897 | ||
898 | } | |
899 | ||
900 | // Store new position | |
901 | // NB ATM this can be the same location as the last one in the list | |
902 | vvp->centers = g_list_append ( vvp->centers, new_center ); | |
903 | ||
904 | // Reset to the end (NB should be same as centers_index++) | |
905 | vvp->centers_index = g_list_length ( vvp->centers ) - 1; | |
906 | ||
907 | // Inform interested subscribers that this change has occurred | |
908 | g_signal_emit ( G_OBJECT(vvp), viewport_signals[VW_UPDATED_CENTER_SIGNAL], 0 ); | |
909 | } | |
910 | ||
911 | /** | |
912 | * vik_viewport_go_back: | |
913 | * | |
914 | * Move back in the position history | |
915 | * | |
916 | * Returns: %TRUE one success | |
917 | */ | |
918 | gboolean vik_viewport_go_back ( VikViewport *vvp ) | |
919 | { | |
920 | // see if the current position is different from the last saved center position within a certain radius | |
921 | VikCoord *center = g_list_nth_data ( vvp->centers, vvp->centers_index ); | |
922 | if ( center ) { | |
923 | // Consider an exclusion size (should it zoom level dependent, rather than a fixed value?) | |
924 | // When still near to the last saved position we'll jump over it to the one before | |
925 | if ( vik_coord_diff ( center, &vvp->center ) > vvp->centers_radius ) { | |
926 | ||
927 | if ( vvp->centers_index == g_list_length(vvp->centers)-1 ) { | |
928 | // Only when we haven't already moved back in the list | |
929 | // Remember where this request came from | |
930 | // (alternatively we could insert in the list on every back attempt) | |
931 | update_centers ( vvp ); | |
932 | } | |
933 | ||
934 | } | |
935 | // 'Go back' if possible | |
936 | // NB if we inserted a position above, then this will then move to the last saved position | |
937 | // otherwise this will skip to the previous saved position, as it's probably somewhere else. | |
938 | if ( vvp->centers_index > 0 ) | |
939 | vvp->centers_index--; | |
940 | } | |
941 | else { | |
942 | return FALSE; | |
943 | } | |
944 | ||
945 | VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index ); | |
946 | if ( new_center ) { | |
947 | vik_viewport_set_center_coord ( vvp, new_center, FALSE ); | |
948 | return TRUE; | |
949 | } | |
950 | return FALSE; | |
951 | } | |
952 | ||
953 | /** | |
954 | * vik_viewport_go_forward: | |
955 | * | |
956 | * Move forward in the position history | |
957 | * | |
958 | * Returns: %TRUE one success | |
959 | */ | |
960 | gboolean vik_viewport_go_forward ( VikViewport *vvp ) | |
961 | { | |
962 | if ( vvp->centers_index == vvp->centers_max-1 ) | |
963 | return FALSE; | |
964 | ||
965 | vvp->centers_index++; | |
966 | VikCoord *new_center = g_list_nth_data ( vvp->centers, vvp->centers_index ); | |
967 | if ( new_center ) { | |
968 | vik_viewport_set_center_coord ( vvp, new_center, FALSE ); | |
969 | return TRUE; | |
970 | } | |
971 | else | |
972 | // Set to end of list | |
973 | vvp->centers_index = g_list_length(vvp->centers) - 1; | |
974 | ||
975 | return FALSE; | |
976 | } | |
977 | ||
978 | /** | |
979 | * vik_viewport_back_available: | |
980 | * | |
981 | * Returns: %TRUE when a previous position in the history is available | |
982 | */ | |
983 | gboolean vik_viewport_back_available ( const VikViewport *vvp ) | |
984 | { | |
985 | return ( vvp->centers_index > 0 ); | |
986 | } | |
987 | ||
988 | /** | |
989 | * vik_viewport_forward_available: | |
990 | * | |
991 | * Returns: %TRUE when a next position in the history is available | |
992 | */ | |
993 | gboolean vik_viewport_forward_available ( const VikViewport *vvp ) | |
994 | { | |
995 | return ( vvp->centers_index < g_list_length(vvp->centers)-1 ); | |
996 | } | |
997 | ||
998 | /** | |
999 | * vik_viewport_set_center_latlon: | |
1000 | * @vvp: The viewport to reposition. | |
1001 | * @ll: The new center position in Lat/Lon format | |
1002 | * @save_position: Whether this new position should be saved into the history of positions | |
1003 | * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) | |
1004 | */ | |
1005 | void vik_viewport_set_center_latlon ( VikViewport *vvp, const struct LatLon *ll, gboolean save_position ) | |
50a14534 EB |
1006 | { |
1007 | vik_coord_load_from_latlon ( &(vvp->center), vvp->coord_mode, ll ); | |
be5554c5 RN |
1008 | if ( save_position ) |
1009 | update_centers ( vvp ); | |
50a14534 EB |
1010 | if ( vvp->coord_mode == VIK_COORD_UTM ) |
1011 | viewport_utm_zone_check ( vvp ); | |
1012 | } | |
1013 | ||
be5554c5 RN |
1014 | /** |
1015 | * vik_viewport_set_center_utm: | |
1016 | * @vvp: The viewport to reposition. | |
1017 | * @utm: The new center position in UTM format | |
1018 | * @save_position: Whether this new position should be saved into the history of positions | |
1019 | * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) | |
1020 | */ | |
1021 | void vik_viewport_set_center_utm ( VikViewport *vvp, const struct UTM *utm, gboolean save_position ) | |
50a14534 EB |
1022 | { |
1023 | vik_coord_load_from_utm ( &(vvp->center), vvp->coord_mode, utm ); | |
be5554c5 RN |
1024 | if ( save_position ) |
1025 | update_centers ( vvp ); | |
50a14534 EB |
1026 | if ( vvp->coord_mode == VIK_COORD_UTM ) |
1027 | viewport_utm_zone_check ( vvp ); | |
1028 | } | |
1029 | ||
be5554c5 RN |
1030 | /** |
1031 | * vik_viewport_set_center_coord: | |
1032 | * @vvp: The viewport to reposition. | |
1033 | * @coord: The new center position in a VikCoord type | |
1034 | * @save_position: Whether this new position should be saved into the history of positions | |
1035 | * Normally only specific user requests should be saved (i.e. to not include Pan and Zoom repositions) | |
1036 | */ | |
1037 | void vik_viewport_set_center_coord ( VikViewport *vvp, const VikCoord *coord, gboolean save_position ) | |
50a14534 EB |
1038 | { |
1039 | vvp->center = *coord; | |
be5554c5 RN |
1040 | if ( save_position ) |
1041 | update_centers ( vvp ); | |
50a14534 EB |
1042 | if ( vvp->coord_mode == VIK_COORD_UTM ) |
1043 | viewport_utm_zone_check ( vvp ); | |
1044 | } | |
1045 | ||
1046 | void vik_viewport_corners_for_zonen ( VikViewport *vvp, int zone, VikCoord *ul, VikCoord *br ) | |
1047 | { | |
1048 | g_return_if_fail ( vvp->coord_mode == VIK_COORD_UTM ); | |
1049 | ||
1050 | /* get center, then just offset */ | |
1051 | vik_viewport_center_for_zonen ( vvp, VIK_UTM(ul), zone ); | |
1052 | ul->mode = VIK_COORD_UTM; | |
1053 | *br = *ul; | |
1054 | ||
1055 | ul->north_south += (vvp->ympp * vvp->height / 2); | |
1056 | ul->east_west -= (vvp->xmpp * vvp->width / 2); | |
1057 | br->north_south -= (vvp->ympp * vvp->height / 2); | |
1058 | br->east_west += (vvp->xmpp * vvp->width / 2); | |
1059 | } | |
1060 | ||
1061 | void vik_viewport_center_for_zonen ( VikViewport *vvp, struct UTM *center, int zone) | |
1062 | { | |
1063 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
1064 | *center = *((struct UTM *)(vik_viewport_get_center ( vvp ))); | |
1065 | center->easting -= ( zone - center->zone ) * vvp->utm_zone_width; | |
1066 | center->zone = zone; | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | gchar vik_viewport_leftmost_zone ( VikViewport *vvp ) | |
1071 | { | |
1072 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
1073 | VikCoord coord; | |
1074 | g_assert ( vvp != NULL ); | |
1075 | vik_viewport_screen_to_coord ( vvp, 0, 0, &coord ); | |
1076 | return coord.utm_zone; | |
1077 | } | |
1078 | return '\0'; | |
1079 | } | |
1080 | ||
1081 | gchar vik_viewport_rightmost_zone ( VikViewport *vvp ) | |
1082 | { | |
1083 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
1084 | VikCoord coord; | |
1085 | g_assert ( vvp != NULL ); | |
1086 | vik_viewport_screen_to_coord ( vvp, vvp->width, 0, &coord ); | |
1087 | return coord.utm_zone; | |
1088 | } | |
1089 | return '\0'; | |
1090 | } | |
1091 | ||
1092 | ||
1093 | void vik_viewport_set_center_screen ( VikViewport *vvp, int x, int y ) | |
1094 | { | |
1095 | g_return_if_fail ( vvp != NULL ); | |
1096 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
1097 | /* slightly optimized */ | |
1098 | vvp->center.east_west += vvp->xmpp * (x - (vvp->width/2)); | |
1099 | vvp->center.north_south += vvp->ympp * ((vvp->height/2) - y); | |
1100 | viewport_utm_zone_check ( vvp ); | |
1101 | } else { | |
1102 | VikCoord tmp; | |
1103 | vik_viewport_screen_to_coord ( vvp, x, y, &tmp ); | |
be5554c5 | 1104 | vik_viewport_set_center_coord ( vvp, &tmp, FALSE ); |
50a14534 EB |
1105 | } |
1106 | } | |
1107 | ||
1108 | gint vik_viewport_get_width( VikViewport *vvp ) | |
1109 | { | |
1110 | g_return_val_if_fail ( vvp != NULL, 0 ); | |
1111 | return vvp->width; | |
1112 | } | |
1113 | ||
1114 | gint vik_viewport_get_height( VikViewport *vvp ) | |
1115 | { | |
1116 | g_return_val_if_fail ( vvp != NULL, 0 ); | |
1117 | return vvp->height; | |
1118 | } | |
1119 | ||
1120 | void vik_viewport_screen_to_coord ( VikViewport *vvp, int x, int y, VikCoord *coord ) | |
1121 | { | |
c53eda9a GB |
1122 | g_return_if_fail ( vvp != NULL ); |
1123 | ||
50a14534 EB |
1124 | if ( vvp->coord_mode == VIK_COORD_UTM ) { |
1125 | int zone_delta; | |
1126 | struct UTM *utm = (struct UTM *) coord; | |
1127 | coord->mode = VIK_COORD_UTM; | |
1128 | ||
50a14534 EB |
1129 | utm->zone = vvp->center.utm_zone; |
1130 | utm->letter = vvp->center.utm_letter; | |
a3603298 | 1131 | utm->easting = ( ( x - ( vvp->width_2) ) * vvp->xmpp ) + vvp->center.east_west; |
50a14534 EB |
1132 | zone_delta = floor( (utm->easting - EASTING_OFFSET ) / vvp->utm_zone_width + 0.5 ); |
1133 | utm->zone += zone_delta; | |
1134 | utm->easting -= zone_delta * vvp->utm_zone_width; | |
a3603298 | 1135 | utm->northing = ( ( ( vvp->height_2) - y ) * vvp->ympp ) + vvp->center.north_south; |
50a14534 EB |
1136 | } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { |
1137 | coord->mode = VIK_COORD_LATLON; | |
d587678a | 1138 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) { |
a3603298 RN |
1139 | coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2)); |
1140 | coord->north_south = vvp->center.north_south + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)); | |
d587678a | 1141 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) |
a3603298 | 1142 | 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 | 1143 | else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { |
a3603298 RN |
1144 | /* This isn't called with a high frequently so less need to optimize */ |
1145 | coord->east_west = vvp->center.east_west + (180.0 * vvp->xmpp / 65536 / 256 * (x - vvp->width_2)); | |
1146 | coord->north_south = DEMERCLAT ( MERCLAT(vvp->center.north_south) + (180.0 * vvp->ympp / 65536 / 256 * (vvp->height_2 - y)) ); | |
50a14534 EB |
1147 | } |
1148 | } | |
1149 | } | |
1150 | ||
a3603298 RN |
1151 | /* |
1152 | * Since this function is used for every drawn trackpoint - it can get called alot | |
1153 | * Thus x & y position factors are calculated once on zoom changes, | |
1154 | * avoiding the need to do it here all the time. | |
1155 | * For good measure the half width and height values are also pre calculated too. | |
1156 | */ | |
50a14534 EB |
1157 | void vik_viewport_coord_to_screen ( VikViewport *vvp, const VikCoord *coord, int *x, int *y ) |
1158 | { | |
1159 | static VikCoord tmp; | |
1160 | g_return_if_fail ( vvp != NULL ); | |
1161 | ||
1162 | if ( coord->mode != vvp->coord_mode ) | |
1163 | { | |
1164 | g_warning ( "Have to convert in vik_viewport_coord_to_screen! This should never happen!"); | |
1165 | vik_coord_copy_convert ( coord, vvp->coord_mode, &tmp ); | |
1166 | coord = &tmp; | |
1167 | } | |
1168 | ||
1169 | if ( vvp->coord_mode == VIK_COORD_UTM ) { | |
1170 | struct UTM *center = (struct UTM *) &(vvp->center); | |
1171 | struct UTM *utm = (struct UTM *) coord; | |
1172 | if ( center->zone != utm->zone && vvp->one_utm_zone ) | |
1173 | { | |
1174 | *x = *y = VIK_VIEWPORT_UTM_WRONG_ZONE; | |
1175 | return; | |
1176 | } | |
1177 | ||
a3603298 | 1178 | *x = ( (utm->easting - center->easting) / vvp->xmpp ) + (vvp->width_2) - |
50a14534 | 1179 | (center->zone - utm->zone ) * vvp->utm_zone_width / vvp->xmpp; |
a3603298 | 1180 | *y = (vvp->height_2) - ( (utm->northing - center->northing) / vvp->ympp ); |
50a14534 EB |
1181 | } else if ( vvp->coord_mode == VIK_COORD_LATLON ) { |
1182 | struct LatLon *center = (struct LatLon *) &(vvp->center); | |
1183 | struct LatLon *ll = (struct LatLon *) coord; | |
1184 | double xx,yy; | |
d587678a | 1185 | if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_LATLON ) { |
a3603298 RN |
1186 | *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) ); |
1187 | *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * (center->lat - ll->lat) ); | |
d587678a | 1188 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_EXPEDIA ) { |
a3603298 | 1189 | 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 ); |
50a14534 | 1190 | *x = xx; *y = yy; |
50a14534 | 1191 | } else if ( vvp->drawmode == VIK_VIEWPORT_DRAWMODE_MERCATOR ) { |
a3603298 RN |
1192 | *x = vvp->width_2 + ( MERCATOR_FACTOR(vvp->xmpp) * (ll->lon - center->lon) ); |
1193 | *y = vvp->height_2 + ( MERCATOR_FACTOR(vvp->ympp) * ( MERCLAT(center->lat) - MERCLAT(ll->lat) ) ); | |
50a14534 EB |
1194 | } |
1195 | } | |
1196 | } | |
1197 | ||
5d325845 RN |
1198 | // Clip functions continually reduce the value by a factor until it is in the acceptable range |
1199 | // whilst also scaling the other coordinate value. | |
1200 | static void clip_x ( gint *x1, gint *y1, gint *x2, gint *y2 ) | |
d9ffd267 | 1201 | { |
5d325845 RN |
1202 | while ( ABS(*x1) > 32768 ) { |
1203 | *x1 = *x2 + (0.5 * (*x1-*x2)); | |
1204 | *y1 = *y2 + (0.5 * (*y1-*y2)); | |
d9ffd267 EB |
1205 | } |
1206 | } | |
1207 | ||
5d325845 RN |
1208 | static void clip_y ( gint *x1, gint *y1, gint *x2, gint *y2 ) |
1209 | { | |
1210 | while ( ABS(*y1) > 32767 ) { | |
1211 | *x1 = *x2 + (0.5 * (*x1-*x2)); | |
1212 | *y1 = *y2 + (0.5 * (*y1-*y2)); | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | /** | |
1217 | * a_viewport_clip_line: | |
1218 | * @x1: screen coord | |
1219 | * @y1: screen coord | |
1220 | * @x2: screen coord | |
1221 | * @y2: screen coord | |
1222 | * | |
1223 | * Due to the seemingly undocumented behaviour of gdk_draw_line(), we need to restrict the range of values passed in. | |
1224 | * So despite it accepting gints, the effective range seems to be the actually the minimum C int range (2^16). | |
1225 | * This seems to be limitations coming from the X Window System. | |
1226 | * | |
1227 | * See http://www.rahul.net/kenton/40errs.html | |
1228 | * ERROR 7. Boundary conditions. | |
1229 | * "The X coordinate space is not infinite. | |
1230 | * Most drawing functions limit position, width, and height to 16 bit integers (sometimes signed, sometimes unsigned) of accuracy. | |
1231 | * Because most C compilers use 32 bit integers, Xlib will not complain if you exceed the 16 bit limit, but your results will usually not be what you expected. | |
1232 | * You should be especially careful of this if you are implementing higher level scalable graphics packages." | |
1233 | * | |
1234 | * This function should be called before calling gdk_draw_line(). | |
1235 | */ | |
1236 | void a_viewport_clip_line ( gint *x1, gint *y1, gint *x2, gint *y2 ) | |
1237 | { | |
1238 | if ( *x1 > 32768 || *x1 < -32767 ) | |
1239 | clip_x ( x1, y1, x2, y2 ); | |
1240 | if ( *y1 > 32768 || *y1 < -32767 ) | |
1241 | clip_y ( x1, y1, x2, y2 ); | |
1242 | if ( *x2 > 32768 || *x2 < -32767 ) | |
1243 | clip_x ( x2, y2, x1, y1 ); | |
1244 | if ( *y2 > 32768 || *y2 < -32767 ) | |
1245 | clip_y ( x2, y2, x1, y1 ); | |
1246 | } | |
1247 | ||
50a14534 EB |
1248 | void vik_viewport_draw_line ( VikViewport *vvp, GdkGC *gc, gint x1, gint y1, gint x2, gint y2 ) |
1249 | { | |
1250 | if ( ! ( ( x1 < 0 && x2 < 0 ) || ( y1 < 0 && y2 < 0 ) || | |
d9ffd267 EB |
1251 | ( x1 > vvp->width && x2 > vvp->width ) || ( y1 > vvp->height && y2 > vvp->height ) ) ) { |
1252 | /*** clipping, yeah! ***/ | |
1253 | a_viewport_clip_line ( &x1, &y1, &x2, &y2 ); | |
1254 | gdk_draw_line ( vvp->scr_buffer, gc, x1, y1, x2, y2); | |
1255 | } | |
50a14534 EB |
1256 | } |
1257 | ||
1258 | void vik_viewport_draw_rectangle ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x1, gint y1, gint x2, gint y2 ) | |
1259 | { | |
add30ebe RN |
1260 | // Using 32 as half the default waypoint image size, so this draws ensures the highlight gets done |
1261 | if ( x1 > -32 && x1 < vvp->width + 32 && y1 > -32 && y1 < vvp->height + 32 ) | |
50a14534 EB |
1262 | gdk_draw_rectangle ( vvp->scr_buffer, gc, filled, x1, y1, x2, y2); |
1263 | } | |
1264 | ||
1265 | void vik_viewport_draw_string ( VikViewport *vvp, GdkFont *font, GdkGC *gc, gint x1, gint y1, const gchar *string ) | |
1266 | { | |
1267 | if ( x1 > -100 && x1 < vvp->width + 100 && y1 > -100 && y1 < vvp->height + 100 ) | |
1268 | gdk_draw_string ( vvp->scr_buffer, font, gc, x1, y1, string ); | |
1269 | } | |
1270 | ||
1271 | /* shouldn't use this -- slow -- change the alpha channel instead. */ | |
1272 | void vik_viewport_draw_pixbuf_with_alpha ( VikViewport *vvp, GdkPixbuf *pixbuf, gint alpha, | |
1273 | gint src_x, gint src_y, gint dest_x, gint dest_y, gint w, gint h ) | |
1274 | { | |
1275 | gint real_dest_x = MAX(dest_x,0); | |
1276 | gint real_dest_y = MAX(dest_y,0); | |
1277 | ||
1278 | if ( alpha == 0 ) | |
1279 | return; /* don't waste your time */ | |
1280 | ||
1281 | if ( w > vvp->alpha_pixbuf_width || h > vvp->alpha_pixbuf_height ) | |
1282 | { | |
1283 | if ( vvp->alpha_pixbuf ) | |
1284 | g_object_unref ( G_OBJECT ( vvp->alpha_pixbuf ) ); | |
1285 | vvp->alpha_pixbuf_width = MAX(w,vvp->alpha_pixbuf_width); | |
1286 | vvp->alpha_pixbuf_height = MAX(h,vvp->alpha_pixbuf_height); | |
1287 | vvp->alpha_pixbuf = gdk_pixbuf_new ( GDK_COLORSPACE_RGB, FALSE, 8, vvp->alpha_pixbuf_width, vvp->alpha_pixbuf_height ); | |
1288 | } | |
1289 | ||
1290 | w = MIN(w,vvp->width - dest_x); | |
1291 | h = MIN(h,vvp->height - dest_y); | |
1292 | ||
1293 | /* check that we are drawing within boundaries. */ | |
1294 | src_x += (real_dest_x - dest_x); | |
1295 | src_y += (real_dest_y - dest_y); | |
1296 | w -= (real_dest_x - dest_x); | |
1297 | h -= (real_dest_y - dest_y); | |
1298 | ||
1299 | gdk_pixbuf_get_from_drawable ( vvp->alpha_pixbuf, vvp->scr_buffer, NULL, | |
1300 | real_dest_x, real_dest_y, 0, 0, w, h ); | |
1301 | ||
1302 | /* do a composite */ | |
1303 | gdk_pixbuf_composite ( pixbuf, vvp->alpha_pixbuf, 0, 0, w, h, -src_x, -src_y, 1, 1, 0, alpha ); | |
1304 | ||
1305 | /* draw pixbuf_tmp */ | |
1306 | vik_viewport_draw_pixbuf ( vvp, vvp->alpha_pixbuf, 0, 0, real_dest_x, real_dest_y, w, h ); | |
1307 | } | |
1308 | ||
1309 | void vik_viewport_draw_pixbuf ( VikViewport *vvp, GdkPixbuf *pixbuf, gint src_x, gint src_y, | |
1310 | gint dest_x, gint dest_y, gint w, gint h ) | |
1311 | { | |
1312 | gdk_draw_pixbuf ( vvp->scr_buffer, | |
ff37db21 RN |
1313 | NULL, |
1314 | pixbuf, | |
50a14534 EB |
1315 | src_x, src_y, dest_x, dest_y, w, h, |
1316 | GDK_RGB_DITHER_NONE, 0, 0 ); | |
1317 | } | |
1318 | ||
1319 | void vik_viewport_draw_arc ( VikViewport *vvp, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2 ) | |
1320 | { | |
1321 | gdk_draw_arc ( vvp->scr_buffer, gc, filled, x, y, width, height, angle1, angle2 ); | |
1322 | } | |
1323 | ||
1324 | ||
1325 | void vik_viewport_draw_polygon ( VikViewport *vvp, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints ) | |
1326 | { | |
1327 | gdk_draw_polygon ( vvp->scr_buffer, gc, filled, points, npoints ); | |
1328 | } | |
1329 | ||
1330 | VikCoordMode vik_viewport_get_coord_mode ( const VikViewport *vvp ) | |
1331 | { | |
1332 | g_assert ( vvp ); | |
1333 | return vvp->coord_mode; | |
1334 | } | |
1335 | ||
1336 | static void viewport_set_coord_mode ( VikViewport *vvp, VikCoordMode mode ) | |
1337 | { | |
1338 | g_return_if_fail ( vvp != NULL ); | |
1339 | vvp->coord_mode = mode; | |
1340 | vik_coord_convert ( &(vvp->center), mode ); | |
1341 | } | |
1342 | ||
1343 | /* Thanks GPSDrive */ | |
1344 | 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 ) | |
1345 | { | |
1346 | int px, py; | |
1347 | gdouble dif, lat, lon; | |
1348 | double Ra = Radius[90+(gint)zero_lat]; | |
1349 | ||
1350 | px = (mapSizeX2 - x) * pixelfact_x; | |
1351 | py = (-mapSizeY2 + y) * pixelfact_y; | |
1352 | ||
1353 | lat = zero_lat - py / Ra; | |
1354 | lat = zero_lat - py / Ra; | |
1355 | lon = | |
1356 | zero_long - | |
1357 | px / (Ra * | |
81765f5e | 1358 | cos (DEG2RAD(lat))); |
50a14534 | 1359 | |
81765f5e | 1360 | dif = lat * (1 - (cos (DEG2RAD(fabs (lon - zero_long))))); |
50a14534 EB |
1361 | lat = lat - dif / 1.5; |
1362 | lon = | |
1363 | zero_long - | |
1364 | px / (Ra * | |
81765f5e | 1365 | cos (DEG2RAD(lat))); |
50a14534 EB |
1366 | |
1367 | *lt = lat; | |
1368 | *lg = lon; | |
1369 | return (TRUE); | |
1370 | } | |
1371 | ||
1372 | /* Thanks GPSDrive */ | |
1373 | 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 ) | |
1374 | { | |
1375 | double dif; | |
1376 | double Ra; | |
1377 | gint mapSizeX = 2 * mapSizeX2; | |
1378 | gint mapSizeY = 2 * mapSizeY2; | |
1379 | ||
1380 | g_assert ( lt >= -90.0 && lt <= 90.0 ); | |
1381 | // lg *= rad2deg; // FIXME, optimize equations | |
1382 | // lt *= rad2deg; | |
1383 | Ra = Radius[90+(gint)lt]; | |
1384 | *x = Ra * | |
81765f5e | 1385 | cos (DEG2RAD(lt)) * (lg - zero_long); |
50a14534 | 1386 | *y = Ra * (lt - zero_lat); |
81765f5e | 1387 | dif = Ra * RAD2DEG(1 - (cos ((DEG2RAD(lg - zero_long))))); |
50a14534 EB |
1388 | *y = *y + dif / 1.85; |
1389 | *x = *x / pixelfact_x; | |
1390 | *y = *y / pixelfact_y; | |
1391 | *x = mapSizeX2 - *x; | |
1392 | *y += mapSizeY2; | |
1393 | if ((*x < 0)||(*x >= mapSizeX)||(*y < 0)||(*y >= mapSizeY)) | |
1394 | return (FALSE); | |
1395 | return (TRUE); | |
1396 | } | |
1397 | ||
1398 | static void viewport_init_ra() | |
1399 | { | |
1400 | static gboolean done_before = FALSE; | |
1401 | if ( !done_before ) | |
1402 | { | |
1403 | gint i; | |
1404 | for ( i = -90; i <= 90; i++) | |
81765f5e | 1405 | Radius[i+90] = calcR ( DEG2RAD((double)i) ); |
50a14534 EB |
1406 | done_before = TRUE; |
1407 | } | |
1408 | } | |
1409 | ||
1410 | double calcR (double lat) | |
1411 | { | |
1412 | double a = 6378.137, r, sc, x, y, z; | |
1413 | double e2 = 0.081082 * 0.081082; | |
1414 | /* | |
1415 | * the radius of curvature of an ellipsoidal Earth in the plane of the | |
1416 | * meridian is given by | |
1417 | * | |
1418 | * R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2) | |
1419 | * | |
1420 | * | |
1421 | * where a is the equatorial radius, b is the polar radius, and e is | |
1422 | * the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2) | |
1423 | * | |
1424 | * a = 6378 km (3963 mi) Equatorial radius (surface to center distance) | |
1425 | * b = 6356.752 km (3950 mi) Polar radius (surface to center distance) e | |
1426 | * = 0.081082 Eccentricity | |
1427 | */ | |
1428 | ||
81765f5e | 1429 | lat = DEG2RAD(lat); |
50a14534 EB |
1430 | sc = sin (lat); |
1431 | x = a * (1.0 - e2); | |
1432 | z = 1.0 - e2 * sc * sc; | |
1433 | y = pow (z, 1.5); | |
1434 | r = x / y; | |
1435 | r = r * 1000.0; | |
1436 | return r; | |
1437 | } | |
1438 | ||
1439 | gboolean vik_viewport_is_one_zone ( VikViewport *vvp ) | |
1440 | { | |
1441 | return vvp->coord_mode == VIK_COORD_UTM && vvp->one_utm_zone; | |
1442 | } | |
1443 | ||
1444 | void vik_viewport_draw_layout ( VikViewport *vvp, GdkGC *gc, gint x, gint y, PangoLayout *layout ) | |
1445 | { | |
1446 | if ( x > -100 && x < vvp->width + 100 && y > -100 && y < vvp->height + 100 ) | |
1447 | gdk_draw_layout ( vvp->scr_buffer, gc, x, y, layout ); | |
1448 | } | |
1449 | ||
1450 | void vik_gc_get_fg_color ( GdkGC *gc, GdkColor *dest ) | |
1451 | { | |
1452 | static GdkGCValues values; | |
1453 | gdk_gc_get_values ( gc, &values ); | |
1454 | gdk_colormap_query_color ( gdk_colormap_get_system(), values.foreground.pixel, dest ); | |
1455 | } | |
1456 | ||
1457 | GdkFunction vik_gc_get_function ( GdkGC *gc ) | |
1458 | { | |
1459 | static GdkGCValues values; | |
1460 | gdk_gc_get_values ( gc, &values ); | |
1461 | return values.function; | |
1462 | } | |
1463 | ||
1464 | void vik_viewport_set_drawmode ( VikViewport *vvp, VikViewportDrawMode drawmode ) | |
1465 | { | |
1466 | vvp->drawmode = drawmode; | |
1467 | if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) | |
1468 | viewport_set_coord_mode ( vvp, VIK_COORD_UTM ); | |
1469 | else { | |
1470 | viewport_set_coord_mode ( vvp, VIK_COORD_LATLON ); | |
50a14534 EB |
1471 | } |
1472 | } | |
1473 | ||
1474 | VikViewportDrawMode vik_viewport_get_drawmode ( VikViewport *vvp ) | |
1475 | { | |
1476 | return vvp->drawmode; | |
1477 | } | |
1478 | ||
0df66d57 EB |
1479 | /******** triggering *******/ |
1480 | void vik_viewport_set_trigger ( VikViewport *vp, gpointer trigger ) | |
1481 | { | |
1482 | vp->trigger = trigger; | |
1483 | } | |
1484 | ||
1485 | gpointer vik_viewport_get_trigger ( VikViewport *vp ) | |
1486 | { | |
1487 | return vp->trigger; | |
1488 | } | |
1489 | ||
1490 | void vik_viewport_snapshot_save ( VikViewport *vp ) | |
1491 | { | |
1492 | gdk_draw_drawable ( vp->snapshot_buffer, vp->background_gc, vp->scr_buffer, 0, 0, 0, 0, -1, -1 ); | |
1493 | } | |
1494 | ||
1495 | void vik_viewport_snapshot_load ( VikViewport *vp ) | |
1496 | { | |
1497 | gdk_draw_drawable ( vp->scr_buffer, vp->background_gc, vp->snapshot_buffer, 0, 0, 0, 0, -1, -1 ); | |
1498 | } | |
1499 | ||
1500 | void vik_viewport_set_half_drawn(VikViewport *vp, gboolean half_drawn) | |
1501 | { | |
1502 | vp->half_drawn = half_drawn; | |
1503 | } | |
1504 | ||
1505 | gboolean vik_viewport_get_half_drawn( VikViewport *vp ) | |
1506 | { | |
1507 | return vp->half_drawn; | |
1508 | } | |
1509 | ||
7bc965c0 GB |
1510 | |
1511 | const gchar *vik_viewport_get_drawmode_name(VikViewport *vv, VikViewportDrawMode mode) | |
0c1044e9 | 1512 | { |
7bc965c0 | 1513 | const gchar *name = NULL; |
24277274 | 1514 | VikWindow *vw = NULL; |
7bc965c0 GB |
1515 | GtkWidget *mode_button; |
1516 | GtkWidget *label; | |
24277274 GB |
1517 | |
1518 | vw = VIK_WINDOW_FROM_WIDGET(vv); | |
1519 | mode_button = vik_window_get_drawmode_button(vw, mode); | |
7bc965c0 GB |
1520 | label = gtk_bin_get_child(GTK_BIN(mode_button)); |
1521 | ||
1522 | name = gtk_label_get_text ( GTK_LABEL(label) ); | |
314c1ccc | 1523 | |
7bc965c0 | 1524 | return name; |
314c1ccc QT |
1525 | |
1526 | } | |
7bc965c0 | 1527 | |
0c1044e9 EB |
1528 | void vik_viewport_get_min_max_lat_lon ( VikViewport *vp, gdouble *min_lat, gdouble *max_lat, gdouble *min_lon, gdouble *max_lon ) |
1529 | { | |
1530 | VikCoord tleft, tright, bleft, bright; | |
1531 | ||
1532 | vik_viewport_screen_to_coord ( vp, 0, 0, &tleft ); | |
1533 | vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &tright ); | |
1534 | vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &bleft ); | |
1535 | vik_viewport_screen_to_coord ( vp, vp->width, vp->height, &bright ); | |
7bc965c0 | 1536 | |
0c1044e9 EB |
1537 | vik_coord_convert(&tleft, VIK_COORD_LATLON); |
1538 | vik_coord_convert(&tright, VIK_COORD_LATLON); | |
1539 | vik_coord_convert(&bleft, VIK_COORD_LATLON); | |
1540 | vik_coord_convert(&bright, VIK_COORD_LATLON); | |
1541 | ||
1542 | *max_lat = MAX(tleft.north_south, tright.north_south); | |
1543 | *min_lat = MIN(bleft.north_south, bright.north_south); | |
1544 | *max_lon = MAX(tright.east_west, bright.east_west); | |
1545 | *min_lon = MIN(tleft.east_west, bleft.east_west); | |
1546 | } | |
82aa018d GB |
1547 | |
1548 | void vik_viewport_reset_copyrights ( VikViewport *vp ) | |
1549 | { | |
1550 | g_return_if_fail ( vp != NULL ); | |
1551 | g_slist_foreach ( vp->copyrights, (GFunc)g_free, NULL ); | |
1552 | g_slist_free ( vp->copyrights ); | |
1553 | vp->copyrights = NULL; | |
1554 | } | |
1555 | ||
33e0e500 GB |
1556 | /** |
1557 | * vik_viewport_add_copyright: | |
1558 | * @vp: self object | |
1559 | * @copyright: new copyright to display | |
1560 | * | |
1561 | * Add a copyright to display on viewport. | |
1562 | */ | |
82aa018d GB |
1563 | void vik_viewport_add_copyright ( VikViewport *vp, const gchar *copyright ) |
1564 | { | |
1565 | g_return_if_fail ( vp != NULL ); | |
1566 | if ( copyright ) | |
1567 | { | |
bbbd639f | 1568 | GSList *found = g_slist_find_custom ( vp->copyrights, copyright, (GCompareFunc)strcmp ); |
82aa018d GB |
1569 | if ( found == NULL ) |
1570 | { | |
1571 | gchar *duple = g_strdup ( copyright ); | |
1572 | vp->copyrights = g_slist_prepend ( vp->copyrights, duple ); | |
1573 | } | |
1574 | } | |
1575 | } | |
26336cf0 GB |
1576 | |
1577 | void vik_viewport_reset_logos ( VikViewport *vp ) | |
1578 | { | |
1579 | g_return_if_fail ( vp != NULL ); | |
1580 | /* do not free elem */ | |
1581 | g_slist_free ( vp->logos ); | |
1582 | vp->logos = NULL; | |
1583 | } | |
1584 | ||
1585 | void vik_viewport_add_logo ( VikViewport *vp, const GdkPixbuf *logo ) | |
1586 | { | |
1587 | g_return_if_fail ( vp != NULL ); | |
1588 | if ( logo ) | |
1589 | { | |
1590 | GdkPixbuf *found = NULL; /* FIXME (GdkPixbuf*)g_slist_find_custom ( vp->logos, logo, (GCompareFunc)== ); */ | |
1591 | if ( found == NULL ) | |
1592 | { | |
1593 | vp->logos = g_slist_prepend ( vp->logos, (gpointer)logo ); | |
1594 | } | |
1595 | } | |
1596 | } | |
9a3538f5 GB |
1597 | |
1598 | /** | |
1599 | * vik_viewport_compute_bearing: | |
1600 | * @vp: self object | |
1601 | * @x1: screen coord | |
1602 | * @y1: screen coord | |
1603 | * @x2: screen coord | |
1604 | * @y2: screen coord | |
1605 | * @angle: bearing in Radian (output) | |
1606 | * @baseangle: UTM base angle in Radian (output) | |
1607 | * | |
1608 | * Compute bearing. | |
1609 | */ | |
1610 | void vik_viewport_compute_bearing ( VikViewport *vp, gint x1, gint y1, gint x2, gint y2, gdouble *angle, gdouble *baseangle ) | |
1611 | { | |
1612 | gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); | |
1613 | gdouble dx = (x2-x1)/len*10; | |
1614 | gdouble dy = (y2-y1)/len*10; | |
1615 | ||
1616 | *angle = atan2(dy, dx) + M_PI_2; | |
1617 | ||
1618 | if ( vik_viewport_get_drawmode ( vp ) == VIK_VIEWPORT_DRAWMODE_UTM) { | |
1619 | VikCoord test; | |
1620 | struct LatLon ll; | |
1621 | struct UTM u; | |
1622 | gint tx, ty; | |
1623 | ||
1624 | vik_viewport_screen_to_coord ( vp, x1, y1, &test ); | |
1625 | vik_coord_to_latlon ( &test, &ll ); | |
1626 | ll.lat += vik_viewport_get_ympp ( vp ) * vik_viewport_get_height ( vp ) / 11000.0; // about 11km per degree latitude | |
1627 | a_coords_latlon_to_utm ( &ll, &u ); | |
1628 | vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u ); | |
1629 | vik_viewport_coord_to_screen ( vp, &test, &tx, &ty ); | |
1630 | ||
1631 | *baseangle = M_PI - atan2(tx-x1, ty-y1); | |
1632 | *angle -= *baseangle; | |
1633 | } | |
1634 | ||
1635 | if (*angle < 0) | |
1636 | *angle += 2*M_PI; | |
1637 | if (*angle > 2*M_PI) | |
1638 | *angle -= 2*M_PI; | |
1639 | } |