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