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