]> git.street.me.uk Git - andy/viking.git/blame - src/vikgeoreflayer.c
Fix: Initalize tv_usec field
[andy/viking.git] / src / vikgeoreflayer.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
4c77d5e0
GB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
50a14534
EB
26#include "viking.h"
27#include "vikgeoreflayer_pixmap.h"
28#include <stdlib.h>
8c060406 29#include <stdio.h>
50a14534 30#include <string.h>
4c77d5e0 31#include <glib/gi18n.h>
50a14534 32
bce3a7b0
EB
33#include "icons/icons.h"
34
50a14534
EB
35VikLayerParam georef_layer_params[] = {
36 { "image", VIK_LAYER_PARAM_STRING, VIK_LAYER_NOT_IN_PROPERTIES },
37 { "corner_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES },
38 { "corner_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES },
39 { "mpp_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES },
40 { "mpp_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES },
41};
42
43enum { PARAM_IMAGE = 0, PARAM_CE, PARAM_CN, PARAM_ME, PARAM_MN, NUM_PARAMS };
44
0a6cab71
AF
45static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len );
46static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
50a14534
EB
47static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp );
48static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id );
49VikGeorefLayer *georef_layer_new ( );
50VikGeorefLayer *georef_layer_create ( VikViewport *vp );
51static void georef_layer_free ( VikGeorefLayer *vgl );
52gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp );
53static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data );
54static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gpointer vlp );
55static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image );
56static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w );
57static void georef_layer_load_image ( VikGeorefLayer *vgl );
941aa6e9
AF
58
59/* tools */
60static gpointer georef_layer_move_create ( VikWindow *vw, VikViewport *vvp);
50a14534
EB
61static gboolean georef_layer_move_release ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp );
62static gboolean georef_layer_move_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp );
941aa6e9 63static gpointer georef_layer_zoom_create ( VikWindow *vw, VikViewport *vvp);
50a14534
EB
64static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp );
65
66static VikToolInterface georef_tools[] = {
4c77d5e0 67 { N_("Georef Move Map"), (VikToolConstructorFunc) georef_layer_move_create, NULL, NULL, NULL,
bce3a7b0 68 (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release,
f2f2f7bf 69 (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geomove },
941aa6e9 70
4c77d5e0 71 { N_("Georef Zoom Tool"), (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL,
bce3a7b0 72 (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL,
f2f2f7bf 73 (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geozoom },
50a14534
EB
74};
75
76VikLayerInterface vik_georef_layer_interface = {
77 "GeoRef Map",
78 &georeflayer_pixbuf, /*icon */
79
80 georef_tools,
81 sizeof(georef_tools) / sizeof(VikToolInterface),
82
83 georef_layer_params,
84 NUM_PARAMS,
85 NULL,
86 0,
87
5a4a28bf
QT
88 VIK_MENU_ITEM_ALL,
89
50a14534
EB
90 (VikLayerFuncCreate) georef_layer_create,
91 (VikLayerFuncRealize) NULL,
92 (VikLayerFuncPostRead) georef_layer_load_image,
93 (VikLayerFuncFree) georef_layer_free,
94
95 (VikLayerFuncProperties) georef_layer_properties,
96 (VikLayerFuncDraw) georef_layer_draw,
97 (VikLayerFuncChangeCoordMode) NULL,
98
20c7a3a0
QT
99 (VikLayerFuncSetMenuItemsSelection) NULL,
100 (VikLayerFuncGetMenuItemsSelection) NULL,
101
50a14534
EB
102 (VikLayerFuncAddMenuItems) georef_layer_add_menu_items,
103 (VikLayerFuncSublayerAddMenuItems) NULL,
104
105 (VikLayerFuncSublayerRenameRequest) NULL,
106 (VikLayerFuncSublayerToggleVisible) NULL,
107
0a6cab71
AF
108 (VikLayerFuncMarshall) georef_layer_marshall,
109 (VikLayerFuncUnmarshall) georef_layer_unmarshall,
50a14534
EB
110
111 (VikLayerFuncSetParam) georef_layer_set_param,
112 (VikLayerFuncGetParam) georef_layer_get_param,
113
114 (VikLayerFuncReadFileData) NULL,
115 (VikLayerFuncWriteFileData) NULL,
116
33534cd8 117 (VikLayerFuncDeleteItem) NULL,
50a14534
EB
118 (VikLayerFuncCopyItem) NULL,
119 (VikLayerFuncPasteItem) NULL,
120 (VikLayerFuncFreeCopiedItem) NULL,
70a23263 121 (VikLayerFuncDragDropRequest) NULL,
50a14534
EB
122};
123
124struct _VikGeorefLayer {
125 VikLayer vl;
126 gchar *image;
127 GdkPixbuf *pixbuf;
128 struct UTM corner;
129 gdouble mpp_easting, mpp_northing;
130 guint width, height;
131
132 gint click_x, click_y;
133};
134
135
136
137GType vik_georef_layer_get_type ()
138{
139 static GType vgl_type = 0;
140
141 if (!vgl_type)
142 {
143 static const GTypeInfo vgl_info =
144 {
145 sizeof (VikGeorefLayerClass),
146 NULL, /* base_init */
147 NULL, /* base_finalize */
148 NULL, /* class init */
149 NULL, /* class_finalize */
150 NULL, /* class_data */
151 sizeof (VikGeorefLayer),
152 0,
153 NULL /* instance init */
154 };
155 vgl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikGeorefLayer", &vgl_info, 0 );
156 }
157
158 return vgl_type;
159}
160
0a6cab71
AF
161static void georef_layer_marshall( VikGeorefLayer *vgl, guint8 **data, gint *len )
162{
163 vik_layer_marshall_params ( VIK_LAYER(vgl), data, len );
164}
165
166static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
167{
168 VikGeorefLayer *rv = georef_layer_new ( vvp );
169 vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp );
170 if (rv->image) {
171 georef_layer_load_image ( rv );
172 }
173 return rv;
174}
175
50a14534
EB
176static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp )
177{
178 switch ( id )
179 {
180 case PARAM_IMAGE: georef_layer_set_image ( vgl, data.s ); break;
181 case PARAM_CN: vgl->corner.northing = data.d; break;
182 case PARAM_CE: vgl->corner.easting = data.d; break;
183 case PARAM_MN: vgl->mpp_northing = data.d; break;
184 case PARAM_ME: vgl->mpp_easting = data.d; break;
185 }
186 return TRUE;
187}
188
189static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id )
190{
191 VikLayerParamData rv;
192 switch ( id )
193 {
194 case PARAM_IMAGE: rv.s = vgl->image ? vgl->image : ""; break;
195 case PARAM_CN: rv.d = vgl->corner.northing; break;
196 case PARAM_CE: rv.d = vgl->corner.easting; break;
197 case PARAM_MN: rv.d = vgl->mpp_northing; break;
198 case PARAM_ME: rv.d = vgl->mpp_easting; break;
199 }
200 return rv;
201}
202
203VikGeorefLayer *georef_layer_new ( )
204{
205 VikGeorefLayer *vgl = VIK_GEOREF_LAYER ( g_object_new ( VIK_GEOREF_LAYER_TYPE, NULL ) );
206 vik_layer_init ( VIK_LAYER(vgl), VIK_LAYER_GEOREF );
207
208 vgl->image = NULL;
209 vgl->pixbuf = NULL;
210 vgl->click_x = -1;
211 vgl->click_y = -1;
212 return vgl;
213}
214
215static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data )
216{
217/* bla, bla */
218 if ( vgl->pixbuf )
219 {
220 VikViewport *vp = VIK_VIEWPORT(data);
221 struct UTM utm_middle;
222 gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp);
223 vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle );
224
225 if ( xmpp == vgl->mpp_easting && ympp == vgl->mpp_northing )
226 {
227 guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp);
228 gint32 x, y;
229 vgl->corner.zone = utm_middle.zone;
230 vgl->corner.letter = utm_middle.letter;
231 VikCoord corner_coord;
232 vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) );
233 vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y );
234 if ( (x < 0 || x < width) && (y < 0 || y < height) && x+vgl->width > 0 && y+vgl->height > 0 )
235 vik_viewport_draw_pixbuf ( vp, vgl->pixbuf, 0, 0, x, y, vgl->width, vgl->height ); /* todo: draw only what we need to. */
236 }
237 }
238}
239
240static void georef_layer_free ( VikGeorefLayer *vgl )
241{
242 if ( vgl->image != NULL )
243 g_free ( vgl->image );
244}
245
246VikGeorefLayer *georef_layer_create ( VikViewport *vp )
247{
248 return georef_layer_new ();
249}
250
251gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp )
252{
253 return georef_layer_dialog ( &vgl, vp, VIK_GTK_WINDOW_FROM_WIDGET(vp) );
254}
255
256static void georef_layer_load_image ( VikGeorefLayer *vgl )
257{
258 GError *gx = NULL;
259 if ( vgl->image == NULL )
260 return;
261
262 if ( vgl->pixbuf )
263 g_object_unref ( G_OBJECT(vgl->pixbuf) );
264
265 vgl->pixbuf = gdk_pixbuf_new_from_file ( vgl->image, &gx );
266
267 if (gx)
268 {
4c77d5e0 269 g_warning ( _("Couldn't open image file: %s"), gx->message );
50a14534
EB
270 g_error_free ( gx );
271 }
272 else
273 {
274 vgl->width = gdk_pixbuf_get_width ( vgl->pixbuf );
275 vgl->height = gdk_pixbuf_get_height ( vgl->pixbuf );
276 }
277
278 /* should find length and width here too */
279}
280
281static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image )
282{
283 if ( vgl->image )
284 g_free ( vgl->image );
285 if ( image == NULL )
286 vgl->image = NULL;
287 vgl->image = g_strdup ( image );
288}
289
290static gboolean world_file_read_line ( gchar *buffer, gint size, FILE *f, GtkWidget *widget, gboolean use_value )
291{
292 if (!fgets ( buffer, 1024, f ))
293 {
4c77d5e0 294 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(widget), _("Unexpected end of file reading World file.") );
50a14534
EB
295 g_free ( buffer );
296 fclose ( f );
8c060406 297 f = NULL;
50a14534
EB
298 return FALSE;
299 }
300 if ( use_value )
301 {
302 gdouble val = strtod ( buffer, NULL );
303 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(widget), val > 0 ? val : -val );
304 }
305 return TRUE;
306}
307
308static void georef_layer_dialog_load ( GtkWidget *pass_along[4] )
309{
6e4a49aa
MA
310 GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"),
311 NULL,
312 GTK_FILE_CHOOSER_ACTION_OPEN,
313 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
314 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
315 NULL);
316
317 if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT )
50a14534 318 {
8c060406 319 FILE *f = g_fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "r" );
50a14534
EB
320 gtk_widget_destroy ( file_selector );
321 if ( !f )
322 {
4c77d5e0 323 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The World file you requested could not be opened for reading.") );
50a14534
EB
324 return;
325 }
326 else
327 {
328 gchar *buffer = g_malloc ( 1024 * sizeof(gchar) );
329 if ( world_file_read_line ( buffer, 1024, f, pass_along[0], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[1], TRUE )
330 && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE ) && world_file_read_line ( buffer, 1024, f, pass_along[0], FALSE )
331 && world_file_read_line ( buffer, 1024, f, pass_along[2], TRUE ) && world_file_read_line ( buffer, 1024, f, pass_along[3], TRUE ) )
332 {
333 g_free ( buffer );
334 fclose ( f );
8c060406 335 f = NULL;
50a14534
EB
336 }
337 }
338 }
339 else
340 gtk_widget_destroy ( file_selector );
341/* do your jazz
342We need a
343 file selection dialog
344 file opener for reading, if NULL, send error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]) )
345 does that catch directories too?
346 read lines -- if not enough lines, give error.
347 if anything outside, give error. define range with #define CONSTANTS
348 put 'em in thar widgets, and that's it.
349*/
350}
351
352static void georef_layer_export_params ( gpointer *pass_along[2] )
353{
354 VikGeorefLayer *vgl = VIK_GEOREF_LAYER(pass_along[0]);
6e4a49aa
MA
355 GtkWidget *file_selector = gtk_file_chooser_dialog_new (_("Choose World file"),
356 NULL,
357 GTK_FILE_CHOOSER_ACTION_SAVE,
358 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
359 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
360 NULL);
361 if ( gtk_dialog_run ( GTK_DIALOG ( file_selector ) ) == GTK_RESPONSE_ACCEPT )
50a14534 362 {
8c060406 363 FILE *f = g_fopen ( gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(file_selector) ), "w" );
6e4a49aa 364
50a14534
EB
365 gtk_widget_destroy ( file_selector );
366 if ( !f )
367 {
4c77d5e0 368 a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_WIDGET(pass_along[0]), _("The file you requested could not be opened for writing.") );
50a14534
EB
369 return;
370 }
371 else
372 {
373 fprintf ( f, "%f\n%f\n%f\n%f\n%f\n%f\n", vgl->mpp_easting, vgl->mpp_northing, 0.0, 0.0, vgl->corner.easting, vgl->corner.northing );
374 fclose ( f );
8c060406 375 f = NULL;
50a14534
EB
376 }
377 }
378 else
379 gtk_widget_destroy ( file_selector );
380}
381
382/* returns TRUE if OK was pressed. */
383static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w )
384{
4c77d5e0 385 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Layer Properties"),
50a14534
EB
386 w,
387 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
388 GTK_STOCK_CANCEL,
389 GTK_RESPONSE_REJECT,
390 GTK_STOCK_OK,
391 GTK_RESPONSE_ACCEPT,
10888930 392 NULL );
50a14534
EB
393 GtkWidget *table, *wfp_hbox, *wfp_label, *wfp_button, *ce_label, *ce_spin, *cn_label, *cn_spin, *xlabel, *xspin, *ylabel, *yspin, *imagelabel, *imageentry;
394
395 GtkWidget *pass_along[4];
396
397 table = gtk_table_new ( 6, 2, FALSE );
398 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0 );
399
400 wfp_hbox = gtk_hbox_new ( FALSE, 0 );
4c77d5e0
GB
401 wfp_label = gtk_label_new ( _("World File Parameters:") );
402 wfp_button = gtk_button_new_with_label ( _("Load From File...") );
50a14534
EB
403
404 gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_label, TRUE, TRUE, 0 );
405 gtk_box_pack_start ( GTK_BOX(wfp_hbox), wfp_button, FALSE, FALSE, 3 );
406
4c77d5e0 407 ce_label = gtk_label_new ( _("Corner pixel easting:") );
50a14534
EB
408 ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 5 ), 1, 4 );
409
4c77d5e0 410 cn_label = gtk_label_new ( _("Corner pixel northing:") );
50a14534
EB
411 cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 5 ), 1, 4 );
412
4c77d5e0
GB
413 xlabel = gtk_label_new ( _("X (easting) scale (mpp): "));
414 ylabel = gtk_label_new ( _("Y (northing) scale (mpp): "));
50a14534
EB
415
416 xspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 );
417 yspin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM, 1, 5, 5 ), 1, 8 );
418
4c77d5e0 419 imagelabel = gtk_label_new ( _("Map Image:") );
50a14534
EB
420 imageentry = vik_file_entry_new ();
421
422 if (*vgl)
423 {
424 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(ce_spin), (*vgl)->corner.easting );
425 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(cn_spin), (*vgl)->corner.northing );
426 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(xspin), (*vgl)->mpp_easting );
427 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(yspin), (*vgl)->mpp_northing );
428 if ( (*vgl)->image )
429 vik_file_entry_set_filename ( VIK_FILE_ENTRY(imageentry), (*vgl)->image );
430 }
431 else
432 {
433 VikCoord corner_coord;
434 struct UTM utm;
435 vik_viewport_screen_to_coord ( VIK_VIEWPORT(vp), 0, 0, &corner_coord );
436 vik_coord_to_utm ( &corner_coord, &utm );
437 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(ce_spin), utm.easting );
438 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(cn_spin), utm.northing );
439 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(xspin), vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ) );
440 gtk_spin_button_set_value ( GTK_SPIN_BUTTON(yspin), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ) );
441 }
442
443 gtk_table_attach_defaults ( GTK_TABLE(table), imagelabel, 0, 1, 0, 1 );
444 gtk_table_attach_defaults ( GTK_TABLE(table), imageentry, 1, 2, 0, 1 );
445 gtk_table_attach_defaults ( GTK_TABLE(table), wfp_hbox, 0, 2, 1, 2 );
446 gtk_table_attach_defaults ( GTK_TABLE(table), xlabel, 0, 1, 2, 3 );
447 gtk_table_attach_defaults ( GTK_TABLE(table), xspin, 1, 2, 2, 3 );
448 gtk_table_attach_defaults ( GTK_TABLE(table), ylabel, 0, 1, 3, 4 );
449 gtk_table_attach_defaults ( GTK_TABLE(table), yspin, 1, 2, 3, 4 );
450 gtk_table_attach_defaults ( GTK_TABLE(table), ce_label, 0, 1, 4, 5 );
451 gtk_table_attach_defaults ( GTK_TABLE(table), ce_spin, 1, 2, 4, 5 );
452 gtk_table_attach_defaults ( GTK_TABLE(table), cn_label, 0, 1, 5, 6 );
453 gtk_table_attach_defaults ( GTK_TABLE(table), cn_spin, 1, 2, 5, 6 );
454
455 pass_along[0] = xspin;
456 pass_along[1] = yspin;
457 pass_along[2] = ce_spin;
458 pass_along[3] = cn_spin;
459 g_signal_connect_swapped ( G_OBJECT(wfp_button), "clicked", G_CALLBACK(georef_layer_dialog_load), pass_along );
460
461 gtk_widget_show_all ( table );
462
463 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
464 {
465 if (! *vgl)
466 {
467 *vgl = georef_layer_new ();
468 vik_layer_rename ( VIK_LAYER(*vgl), vik_georef_layer_interface.name );
469 }
470 (*vgl)->corner.easting = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(ce_spin) );
471 (*vgl)->corner.northing = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(cn_spin) );
472 (*vgl)->mpp_easting = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(xspin) );
473 (*vgl)->mpp_northing = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(yspin) );
474 if ( (!(*vgl)->image) || strcmp( (*vgl)->image, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) ) != 0 )
475 {
476 georef_layer_set_image ( *vgl, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) );
477 georef_layer_load_image ( *vgl );
478 }
479
480 gtk_widget_destroy ( GTK_WIDGET(dialog) );
481 return TRUE;
482 }
483 gtk_widget_destroy ( GTK_WIDGET(dialog) );
484 return FALSE;
485}
486
487static void georef_layer_zoom_to_fit ( gpointer vgl_vlp[2] )
488{
489 vik_viewport_set_xmpp ( vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1])), VIK_GEOREF_LAYER(vgl_vlp[0])->mpp_easting );
490 vik_viewport_set_ympp ( vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1])), VIK_GEOREF_LAYER(vgl_vlp[0])->mpp_northing );
491 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL(vgl_vlp[1]) );
492}
493
494static void georef_layer_goto_center ( gpointer vgl_vlp[2] )
495{
496 VikGeorefLayer *vgl = VIK_GEOREF_LAYER ( vgl_vlp[0] );
497 VikViewport *vp = vik_layers_panel_get_viewport(VIK_LAYERS_PANEL(vgl_vlp[1]));
498 struct UTM utm;
499 VikCoord coord;
500
501 vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm );
502
503 utm.easting = vgl->corner.easting + (vgl->width * vgl->mpp_easting / 2); /* only an approximation */
504 utm.northing = vgl->corner.northing - (vgl->height * vgl->mpp_northing / 2);
505
506 vik_coord_load_from_utm ( &coord, vik_viewport_get_coord_mode ( vp ), &utm );
507 vik_viewport_set_center_coord ( vp, &coord );
508
509 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL(vgl_vlp[1]) );
510}
511
512static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gpointer vlp )
513{
514 static gpointer pass_along[2];
515 GtkWidget *item;
516 pass_along[0] = vgl;
517 pass_along[1] = vlp;
518
519 item = gtk_menu_item_new();
520 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
521 gtk_widget_show ( item );
522
4c77d5e0 523 item = gtk_menu_item_new_with_label ( _("Zoom to Fit Map") );
50a14534
EB
524 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_zoom_to_fit), pass_along );
525 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
526 gtk_widget_show ( item );
527
4c77d5e0 528 item = gtk_menu_item_new_with_label ( _("Goto Map Center") );
50a14534
EB
529 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_goto_center), pass_along );
530 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
531 gtk_widget_show ( item );
532
4c77d5e0 533 item = gtk_menu_item_new_with_label ( _("Export to World File") );
50a14534
EB
534 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(georef_layer_export_params), pass_along );
535 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
536 gtk_widget_show ( item );
537}
538
941aa6e9
AF
539
540static gpointer georef_layer_move_create ( VikWindow *vw, VikViewport *vvp)
541{
542 return vvp;
543}
544
50a14534
EB
545static gboolean georef_layer_move_release ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp )
546{
941aa6e9
AF
547 if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF)
548 return FALSE;
549
50a14534
EB
550 if ( vgl->click_x != -1 )
551 {
552 vgl->corner.easting += (event->x - vgl->click_x) * vik_viewport_get_xmpp (vvp);
553 vgl->corner.northing -= (event->y - vgl->click_y) * vik_viewport_get_ympp (vvp);
554 vik_layer_emit_update ( VIK_LAYER(vgl) );
555 return TRUE;
556 }
557 return FALSE; /* I didn't move anything on this layer! */
558}
559
941aa6e9
AF
560static gpointer georef_layer_zoom_create ( VikWindow *vw, VikViewport *vvp)
561{
562 return vvp;
563}
564
50a14534
EB
565static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp )
566{
941aa6e9
AF
567 if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF)
568 return FALSE;
50a14534
EB
569 if ( event->button == 1 )
570 {
571 if ( vgl->mpp_easting < (VIK_VIEWPORT_MAX_ZOOM / 1.05) && vgl->mpp_northing < (VIK_VIEWPORT_MAX_ZOOM / 1.05) )
572 {
573 vgl->mpp_easting *= 1.01;
574 vgl->mpp_northing *= 1.01;
575 }
576 }
577 else
578 {
579 if ( vgl->mpp_easting > (VIK_VIEWPORT_MIN_ZOOM * 1.05) && vgl->mpp_northing > (VIK_VIEWPORT_MIN_ZOOM * 1.05) )
580 {
581 vgl->mpp_easting /= 1.01;
582 vgl->mpp_northing /= 1.01;
583 }
584 }
585 vik_viewport_set_xmpp ( vvp, vgl->mpp_easting );
586 vik_viewport_set_ympp ( vvp, vgl->mpp_northing );
587 vik_layer_emit_update ( VIK_LAYER(vgl) );
588 return TRUE;
589}
590
591static gboolean georef_layer_move_press ( VikGeorefLayer *vgl, GdkEventButton *event, VikViewport *vvp )
592{
941aa6e9
AF
593 if (!vgl || vgl->vl.type != VIK_LAYER_GEOREF)
594 return FALSE;
50a14534
EB
595 vgl->click_x = event->x;
596 vgl->click_y = event->y;
597 return TRUE;
598}