]> err.no Git - mapper/blob - src/cb.c
Merge branch 'master' of git+ssh://tal.org/home/git/mapper
[mapper] / src / cb.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stddef.h>
10 #include <locale.h>
11 #include <errno.h>
12 #include <sys/wait.h>
13 #include <glib/gstdio.h>
14 #include <gtk/gtk.h>
15 #include <fcntl.h>
16 #include <libintl.h>
17 #include <locale.h>
18
19 #include "hildon-mapper.h"
20
21 #include "utils.h"
22 #include "poi.h"
23 #include "route.h"
24 #include "track.h"
25 #include "path.h"
26 #include "settings.h"
27 #include "gps.h"
28 #include "map.h"
29 #include "mapper-types.h"
30 #include "bt.h"
31 #include "ui-common.h"
32 #include "db.h"
33 #include "latlon.h"
34 #include "cb.h"
35 #include "gps-panels.h"
36
37 gboolean 
38 menu_cb_route_download(GtkAction * action)
39 {
40 route_download(NULL);
41 return TRUE;
42 }
43
44 gboolean 
45 menu_cb_route_open(GtkAction * action)
46 {
47 route_open_file();
48 return TRUE;
49 }
50
51 gboolean 
52 menu_cb_route_distnext(GtkAction * action)
53 {
54 route_show_distance_to_next();
55 return TRUE;
56 }
57
58 gboolean 
59 menu_cb_route_distlast(GtkAction * action)
60 {
61 route_show_distance_to_last();
62 return TRUE;
63 }
64
65 gboolean 
66 menu_cb_route_reset(GtkAction * action)
67 {
68 route_find_nearest_point();
69 map_render_data();
70 MACRO_QUEUE_DRAW_AREA();
71 return TRUE;
72 }
73
74 gboolean 
75 menu_cb_route_clear(GtkAction * action)
76 {
77 route_clear();
78 return TRUE;
79 }
80
81 gboolean 
82 menu_cb_track_open(GtkAction * action)
83 {
84 track_open();
85 return TRUE;
86 }
87
88 gboolean 
89 menu_cb_track_save(GtkAction * action)
90 {
91 track_save();
92 return TRUE;
93 }
94
95 gboolean 
96 menu_cb_track_insert_break(GtkAction * action)
97 {
98 track_insert_break();
99 return TRUE;
100 }
101
102 gboolean 
103 menu_cb_track_insert_mark(GtkAction * action)
104 {
105 track_insert_mark();
106 return TRUE;
107 }
108
109 gboolean 
110 menu_cb_track_distlast(GtkAction * action)
111 {
112 track_show_distance_from_last();
113 return TRUE;
114 }
115
116 gboolean 
117 menu_cb_track_distfirst(GtkAction * action)
118 {
119 track_show_distance_from_first();
120 return TRUE;
121 }
122
123 gboolean 
124 menu_cb_route_save(GtkAction * action)
125 {
126 route_save();
127 return TRUE;
128 }
129
130 gboolean 
131 menu_cb_track_clear(GtkAction * action)
132 {
133 track_clear();
134 return TRUE;
135 }
136
137 gboolean 
138 menu_cb_track_filter(GtkAction * action)
139 {
140 filter_dialog();
141 return TRUE;
142 }
143
144 gboolean 
145 menu_cb_show_tracks(GtkAction *action)
146 {
147 _show_tracks ^= TRACKS_MASK;
148 if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action))) {
149         _show_tracks |= TRACKS_MASK;
150         map_render_paths();
151         MACRO_QUEUE_DRAW_AREA();
152         MACRO_BANNER_SHOW_INFO(_window, _("Tracks are now shown"));
153 } else {
154         _show_tracks &= ~TRACKS_MASK;
155         map_force_redraw();
156         MACRO_BANNER_SHOW_INFO(_window, _("Tracks are now hidden"));
157 }
158 return TRUE;
159 }
160
161 gboolean 
162 menu_cb_show_scale(GtkAction * action)
163 {
164 _show_scale = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
165 MACRO_QUEUE_DRAW_AREA();
166 return TRUE;
167 }
168
169 gboolean 
170 menu_cb_show_routes(GtkAction * action)
171 {
172 if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action))) {
173         _show_tracks |= ROUTES_MASK;
174         map_render_paths();
175         MACRO_QUEUE_DRAW_AREA();
176         MACRO_BANNER_SHOW_INFO(_window, _("Routes are now shown"));
177 } else {
178         _show_tracks &= ~ROUTES_MASK;
179         map_force_redraw();
180         MACRO_BANNER_SHOW_INFO(_window, _("Routes are now hidden"));
181 }
182 return TRUE;
183 }
184
185 gboolean 
186 menu_cb_show_velvec(GtkAction * action)
187 {
188 _show_velvec = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
189 map_move_mark();
190 return TRUE;
191 }
192
193 gboolean 
194 menu_cb_show_poi(GtkAction * action)
195 {
196 _show_poi = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
197 map_force_redraw();
198 return TRUE;
199 }
200
201 gboolean 
202 menu_cb_gps_show_info(GtkAction * action)
203 {
204 _gps_info = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
205 gps_show_info();
206 return TRUE;
207 }
208
209 gboolean
210 menu_cb_autocenter(GtkAction *action, GtkRadioAction *current)
211 {
212 guint new_center_unitx, new_center_unity;
213 gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
214
215 switch (value) {
216         case CENTER_LEAD:
217                 _center_mode = CENTER_LEAD;
218                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Mode: Lead"));
219         break;
220         case CENTER_LATLON:
221                 _center_mode = CENTER_LATLON;
222                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Mode: Lat/Lon"));
223         break;
224         case CENTER_MANUAL:
225         default:
226                 _center_mode = -_center_mode;
227                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Off"));
228                 return TRUE;
229         break;
230 }
231
232 MACRO_RECALC_CENTER(new_center_unitx, new_center_unity);
233 map_center_unit(new_center_unitx, new_center_unity);
234 return TRUE;
235 }
236
237 gboolean 
238 menu_cb_goto_latlon(GtkAction * action)
239 {
240 GtkWidget *dialog;
241 GtkWidget *table;
242 GtkWidget *label;
243 GtkWidget *txt_lat;
244 GtkWidget *txt_lon;
245
246 dialog = gtk_dialog_new_with_buttons(_("Go to Lat/Lon"),
247                      GTK_WINDOW(_window),
248                      GTK_DIALOG_MODAL, GTK_STOCK_OK,
249                      GTK_RESPONSE_ACCEPT,
250                      GTK_STOCK_CANCEL,
251                      GTK_RESPONSE_REJECT, NULL);
252
253 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 3, FALSE), TRUE, TRUE, 0);
254
255 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Latitude")),0, 1, 0, 1, GTK_FILL, 0, 2, 4);
256 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
257
258 gtk_table_attach(GTK_TABLE(table), txt_lat = gtk_entry_new(), 1, 2, 0, 1, GTK_FILL, 0, 2, 4);
259 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
260
261 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Longitude")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
262 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
263
264 gtk_table_attach(GTK_TABLE(table), txt_lon = gtk_entry_new(), 1, 2, 1, 2, GTK_FILL, 0, 2, 4);
265 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
266
267 #ifdef WITH_DEVICE_770
268 g_object_set(G_OBJECT(txt_lat), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
269 g_object_set(G_OBJECT(txt_lon), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
270 #endif
271
272 /* Initialize with the current center position. */
273 {
274         gchar buffer[32];
275         gdouble lat, lon;
276         unit2latlon(_center.unitx, _center.unity, lat, lon);
277         snprintf(buffer, sizeof(buffer), "%.06f", lat);
278         gtk_label_set_text(GTK_LABEL(txt_lat), buffer);
279         snprintf(buffer, sizeof(buffer), "%.06f", lon);
280         gtk_label_set_text(GTK_LABEL(txt_lon), buffer);
281 }
282
283 gtk_widget_show_all(dialog);
284
285 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
286         const gchar *text;
287         gchar *error_check;
288         gdouble lat, lon;
289         guint unitx, unity;
290
291         text = gtk_entry_get_text(GTK_ENTRY(txt_lat));
292         lat = strtof(text, &error_check);
293         if (text == error_check || lat < -90.f || lat > 90.f) {
294                 popup_error(dialog, _("Invalid Latitude"));
295                 continue;
296         }
297
298         text = gtk_entry_get_text(GTK_ENTRY(txt_lon));
299         lon = strtof(text, &error_check);
300         if (text == error_check || lon < -180.f || lon > 180.f) {
301                 popup_error(dialog, _("Invalid Longitude"));
302                 continue;
303         }
304
305         latlon2unit(lat, lon, unitx, unity);
306         if (_center_mode > 0)
307                 set_action_activate("autocenter_none", TRUE);
308
309         map_center_unit(unitx, unity);
310         break;
311 }
312 gtk_widget_destroy(dialog);
313 return TRUE;
314 }
315
316 gboolean 
317 menu_cb_goto_home(GtkAction *action)
318 {
319 if (map_goto_position(&_home)==FALSE) {
320         MACRO_BANNER_SHOW_INFO(_window, _("Home not set."));
321 } else {
322         MACRO_BANNER_SHOW_INFO(_window, _("At home location"));
323 }
324 return TRUE;
325 }
326
327 gboolean 
328 menu_cb_goto_destination(GtkAction *action)
329 {
330 if (map_goto_position(&_dest)==FALSE) {
331         MACRO_BANNER_SHOW_INFO(_window, _("Destination not set."));
332 } else {
333         MACRO_BANNER_SHOW_INFO(_window, _("At destination"));
334 }
335 return TRUE;
336 }
337
338 gboolean 
339 menu_cb_goto_gps(GtkAction *action)
340 {
341 _center_mode = CENTER_LATLON;
342 map_center_unit(_pos.unitx, _pos.unity);
343 map_update_location_from_center();
344
345 return TRUE;
346 }
347
348 gboolean 
349 menu_cb_goto_nextway(GtkAction * action)
350 {
351 if (_next_way && _next_way->point->unity) {
352         if (_center_mode > 0)
353                 set_action_activate("autocenter_none", TRUE);
354
355         map_center_unit(_next_way->point->unitx, _next_way->point->unity);
356 } else {
357         MACRO_BANNER_SHOW_INFO(_window, _("There is no next waypoint."));
358 }
359
360 return TRUE;
361 }
362
363 gboolean 
364 menu_cb_goto_nearpoi(GtkAction * action)
365 {
366 gdouble lat, lon;
367 poi_info *p;
368
369 if (_center_mode > 0) {
370         /* Auto-Center is enabled - use the GPS position. */
371         unit2latlon(_pos.unitx, _pos.unity, lat, lon);
372 } else {
373         /* Auto-Center is disabled - use the view center. */
374         unit2latlon(_center.unitx, _center.unity, lat, lon);
375 }
376
377 p=poi_find_nearest(lat, lon);
378
379 if (p) {
380         guint unitx, unity;
381         gchar *banner;
382
383         latlon2unit(p->lat, p->lon, unitx, unity);
384         banner = g_strdup_printf("%s (%s)", p->label, p->cat_label);
385         g_printf("%s\n", banner);
386         MACRO_BANNER_SHOW_INFO(_window, banner);
387         g_free(banner);
388         poi_free(p);
389
390         if (_center_mode > 0)
391                 set_action_activate("autocenter_none", TRUE);
392
393         map_center_unit(unitx, unity);
394         map_update_location_from_center();
395 } else {
396         MACRO_BANNER_SHOW_INFO(_window, _("No POIs found."));
397 }
398
399 return TRUE;
400 }
401
402 gboolean 
403 menu_cb_maps_repoman(GtkAction * action)
404 {
405 repoman_dialog();
406 return TRUE;
407 }
408
409 gboolean 
410 menu_cb_maps_select(GtkAction * action, gpointer new_repo)
411 {
412 repo_set_curr(new_repo);
413 map_force_redraw();
414 return TRUE;
415 }
416
417 gboolean 
418 cb_zoom_auto(GtkAction * action)
419 {
420 map_set_autozoom(TRUE);
421 return TRUE;
422 }
423
424 gboolean 
425 cb_zoom_base(GtkAction * action)
426 {
427 map_set_autozoom(FALSE);
428 map_set_zoom(3);
429 return TRUE;
430 }
431
432 gboolean 
433 cb_zoomin(GtkAction * action)
434 {
435 map_set_autozoom(FALSE);
436 map_zoom(-1);
437 return TRUE;
438 }
439
440 gboolean 
441 cb_zoomout(GtkAction * action)
442 {
443 map_set_autozoom(FALSE);
444 map_zoom(1);
445 return TRUE;
446 }
447
448 gboolean 
449 cb_fullscreen(GtkAction * action)
450 {
451 if ((_fullscreen = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)))) {
452         gtk_window_fullscreen(GTK_WINDOW(_window));
453 } else {
454         gtk_window_unfullscreen(GTK_WINDOW(_window));
455 }
456 gtk_idle_add((GSourceFunc) window_present, NULL);
457 return TRUE;
458 }
459
460 gboolean 
461 menu_cb_enable_gps(GtkAction * action)
462 {
463 if ((_enable_gps = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)))) {
464         if (_rcvr_mac) {
465                 set_conn_state(RCVR_DOWN);
466                 rcvr_connect_now();
467         } else {
468                 popup_error(_window, _("Cannot enable GPS until a GPS Receiver MAC is set in the Settings dialog box."));
469                 set_action_activate("gps_enable", FALSE);
470         }
471 } else {
472         if (_conn_state > RCVR_OFF)
473                 set_conn_state(RCVR_OFF);
474         rcvr_disconnect();
475         track_add(0, FALSE);
476         _speed_excess=FALSE;
477 }
478 set_action_sensitive("gps_details", _enable_gps);
479 set_action_sensitive("goto_gps", _enable_gps);
480 set_action_sensitive("autocenter_latlon", _enable_gps);
481 set_action_sensitive("autocenter_lead", _enable_gps);
482
483 map_move_mark();
484 gps_show_info();
485
486 return TRUE;
487 }
488
489 gboolean 
490 menu_cb_auto_download(GtkAction * action)
491 {
492 if ((_auto_download = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) )) {
493         if (_curr_repo->url == REPOTYPE_NONE)
494                 popup_error(_window, _("NOTE: You must set a Map URI in the current repository in order to download maps."));
495         map_force_redraw();
496 }
497
498 return TRUE;
499 }
500
501 gboolean 
502 menu_cb_gps_details(GtkAction * action)
503 {
504 gps_details();
505 return TRUE;
506 }
507
508 gboolean 
509 menu_cb_settings(GtkAction * action)
510 {
511 if (settings_dialog()) {
512         /* Settings have changed - reconnect to receiver. */
513         if (_enable_gps) {
514                 set_conn_state(RCVR_DOWN);
515                 rcvr_disconnect();
516                 rcvr_connect_now();
517         }
518 }
519 MACRO_RECALC_FOCUS_BASE();
520 MACRO_RECALC_FOCUS_SIZE();
521 map_force_redraw();
522 return TRUE;
523 }
524
525 gboolean 
526 menu_cb_help(GtkAction * action)
527 {
528 #ifdef WITH_OSSO
529 ossohelp_show(_osso, HELP_ID_INTRO, 0);
530 #endif
531 return TRUE;
532 }
533
534 gboolean 
535 menu_cb_about(GtkAction * action)
536 {
537 gchar *authors[]={
538         "Kaj-Michael Lang",
539         "John Costigan",
540         "Cezary Jackiewicz", NULL
541 };
542
543 gtk_show_about_dialog(GTK_WINDOW(_window), 
544         "name", "Mapper",
545         "version", VERSION, 
546         "copyright", "Kaj-Michael Lang",
547         "license", "GPL",
548         "authors", authors,
549         NULL);
550 return TRUE;
551 }
552
553 gboolean 
554 window_cb_key_press(GtkWidget * widget, GdkEventKey * event)
555 {
556 CustomKey custom_key;
557
558 switch (event->keyval) {
559         case HILDON_HARDKEY_UP:
560                 custom_key = CUSTOM_KEY_UP;
561         break;
562         case HILDON_HARDKEY_DOWN:
563                 custom_key = CUSTOM_KEY_DOWN;
564         break;
565         case HILDON_HARDKEY_LEFT:
566                 custom_key = CUSTOM_KEY_LEFT;
567         break;
568         case HILDON_HARDKEY_RIGHT:
569                 custom_key = CUSTOM_KEY_RIGHT;
570         break;
571         case HILDON_HARDKEY_SELECT:
572                 custom_key = CUSTOM_KEY_SELECT;
573         break;
574         case HILDON_HARDKEY_INCREASE:
575                 custom_key = CUSTOM_KEY_INCREASE;
576         break;
577         case HILDON_HARDKEY_DECREASE:
578                 custom_key = CUSTOM_KEY_DECREASE;
579         break;
580         case HILDON_HARDKEY_FULLSCREEN:
581                 custom_key = CUSTOM_KEY_FULLSCREEN;
582         break;
583         case HILDON_HARDKEY_ESC:
584                 custom_key = CUSTOM_KEY_ESC;
585         break;
586         default:
587                 return FALSE;
588 }
589
590         switch (_action[custom_key]) {
591         case CUSTOM_ACTION_PAN_NORTH:
592                 map_pan(0, -PAN_UNITS);
593         break;
594         case CUSTOM_ACTION_PAN_WEST:
595                 map_pan(-PAN_UNITS, 0);
596         break;
597         case CUSTOM_ACTION_PAN_SOUTH:
598                 map_pan(0, PAN_UNITS);
599         break;
600         case CUSTOM_ACTION_PAN_EAST:
601                 map_pan(PAN_UNITS, 0);
602         break;
603         case CUSTOM_ACTION_TOGGLE_AUTOCENTER:
604                 switch (_center_mode) {
605                 case CENTER_LATLON:
606                 case CENTER_WAS_LEAD:
607                         set_action_activate("autocenter_lead", TRUE);
608                 break;
609                 case CENTER_LEAD:
610                 case CENTER_WAS_LATLON:
611                         set_action_activate("autocenter_latlon", TRUE);
612                 break;
613                 default:
614                         set_action_activate("autocenter_latlon", TRUE);
615                 break;
616                 }
617         break;
618         case CUSTOM_ACTION_ZOOM_IN:
619         case CUSTOM_ACTION_ZOOM_OUT:
620                 if (!_key_zoom_timeout_sid) {
621                         g_printf("Z: %d %d\n", _key_zoom_new, _zoom);
622                         _key_zoom_new = _zoom + (_action[custom_key] == CUSTOM_ACTION_ZOOM_IN
623                                ? -_curr_repo->view_zoom_steps : _curr_repo->view_zoom_steps);
624                         /* Remember, _key_zoom_new is unsigned. */
625                         if (_key_zoom_new < MAX_ZOOM) {
626                                 gchar buffer[80];
627                                 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), _key_zoom_new);
628                                 MACRO_BANNER_SHOW_INFO(_window, buffer);
629                                 _key_zoom_timeout_sid = g_timeout_add(400, map_key_zoom_timeout, NULL);
630                         }
631                 }
632         break;
633         case CUSTOM_ACTION_TOGGLE_FULLSCREEN:
634                 set_action_activate("view_fullscreen", !_fullscreen);
635         break;
636         case CUSTOM_ACTION_TOGGLE_TRACKS:
637                 switch (_show_tracks) {
638                 case 0:
639                         /* Nothing shown, nothing saved; just set both. */
640                         _show_tracks = TRACKS_MASK | ROUTES_MASK;
641                         break;
642                 case TRACKS_MASK << 16:
643                 case ROUTES_MASK << 16:
644                 case (ROUTES_MASK | TRACKS_MASK) << 16:
645                         /* Something was saved and nothing changed since.
646                          * Restore saved. */
647                         _show_tracks = _show_tracks >> 16;
648                         break;
649                 default:
650                         /* There is no history, or they changed something
651                          * since the last historical change. Save and
652                          * clear. */
653                         _show_tracks = _show_tracks << 16;
654                 }
655                 set_action_activate("view_route", _show_tracks & ROUTES_MASK);
656                 set_action_activate("view_track", _show_tracks & TRACKS_MASK);
657         break;
658         case CUSTOM_ACTION_TOGGLE_SCALE:
659                 set_action_activate("view_scale", _show_scale);
660         break;
661         case CUSTOM_ACTION_TOGGLE_POI:
662                 set_action_activate("view_poi", _show_poi);
663         break;
664         case CUSTOM_ACTION_CHANGE_REPO: {
665                         GList *curr = g_list_find(_repo_list, _curr_repo);
666                         if (!curr)
667                                 break;
668
669                         /* Loop until we reach a next-able repo, or until we get
670                          * back to the current repo. */
671                         while ((curr = (curr->next ? curr->next : _repo_list)) && !((RepoData *) curr->data)->nextable && curr->data != _curr_repo) {
672                         }
673
674                         if (curr->data != _curr_repo) {
675                                 repo_set_curr(curr->data);
676                                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(_curr_repo->menu_item), TRUE);
677                         } else {
678                                 popup_error(_window, _("There are no other next-able repositories."));
679                         }
680                         break;
681                 }
682         break;
683         case CUSTOM_ACTION_ROUTE_DISTNEXT:
684                 route_show_distance_to_next();
685         break;
686         case CUSTOM_ACTION_ROUTE_DISTLAST:
687                 route_show_distance_to_last();
688         break;
689         case CUSTOM_ACTION_TRACK_BREAK:
690                 track_insert_break();
691         break;
692         case CUSTOM_ACTION_TRACK_DISTLAST:
693                 track_show_distance_from_last();
694         break;
695         case CUSTOM_ACTION_TRACK_DISTFIRST:
696                 track_show_distance_from_first();
697         break;
698         case CUSTOM_ACTION_TOGGLE_GPS:
699                 set_action_activate("gps_enable", !_enable_gps);
700         break;
701         case CUSTOM_ACTION_TOGGLE_GPSINFO:
702                 set_action_activate("gps_info", !_gps_info);
703         break;
704         case CUSTOM_ACTION_TOGGLE_SPEEDLIMIT:
705                 _speed_limit_on ^= 1;
706         break;
707         default:
708                 return FALSE;
709 }
710 return TRUE;
711 }
712
713 gboolean 
714 window_cb_key_release(GtkWidget * widget, GdkEventKey * event)
715 {
716 switch (event->keyval) {
717         case HILDON_HARDKEY_INCREASE:
718         case HILDON_HARDKEY_DECREASE:
719                 if (_key_zoom_timeout_sid) {
720                         g_source_remove(_key_zoom_timeout_sid);
721                         _key_zoom_timeout_sid = 0;
722                         map_set_zoom(_key_zoom_new);
723                 }
724         return TRUE;
725         break;
726         default:
727                 return FALSE;
728 }
729 }
730
731 void 
732 cmenu_show_latlon(guint unitx, guint unity)
733 {
734 gdouble lat, lon;
735 gchar buffer[80], tmp1[16], tmp2[16];
736
737 unit2latlon(unitx, unity, lat, lon);
738 lat_format(lat, tmp1);
739 lon_format(lon, tmp2);
740
741 snprintf(buffer, sizeof(buffer),
742          "%s: %s\n"
743          "%s: %s", _("Latitude"), tmp1, _("Longitude"), tmp2);
744
745 MACRO_BANNER_SHOW_INFO(_window, buffer);
746 }
747
748 void 
749 cmenu_clip_latlon(guint unitx, guint unity)
750 {
751 gchar buffer[80];
752 gdouble lat, lon;
753
754 unit2latlon(unitx, unity, lat, lon);
755 snprintf(buffer, sizeof(buffer), "%.06f,%.06f", lat, lon);
756
757 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), buffer, -1);
758 }
759
760 void 
761 cmenu_route_to(guint unitx, guint unity)
762 {
763 gchar buffer[80];
764 gchar strlat[32];
765 gchar strlon[32];
766 gdouble lat, lon;
767
768 unit2latlon(unitx, unity, lat, lon);
769
770 g_ascii_formatd(strlat, 32, "%.06f", lat);
771 g_ascii_formatd(strlon, 32, "%.06f", lon);
772 snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
773
774 route_download(buffer);
775 }
776
777 void 
778 cmenu_distance_to(guint unitx, guint unity)
779 {
780 gchar buffer[80];
781 gdouble lat, lon;
782
783 unit2latlon(unitx, unity, lat, lon);
784
785 snprintf(buffer, sizeof(buffer), "%s: %.02lf %s", _("Distance"),
786          calculate_distance(_gps.lat, _gps.lon, lat, lon) * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
787 MACRO_BANNER_SHOW_INFO(_window, buffer);
788 }
789
790 void 
791 cmenu_add_route(guint unitx, guint unity)
792 {
793 MACRO_PATH_INCREMENT_TAIL(_route);
794 _route.tail->unitx = x2unit(_cmenu_position_x);
795 _route.tail->unity = y2unit(_cmenu_position_y);
796 route_find_nearest_point();
797 map_force_redraw();
798 }
799
800 void cmenu_route_add_way(guint unitx, guint unity)
801 {
802 gdouble lat, lon;
803 gchar tmp1[16], tmp2[16], *p_latlon;
804 GtkWidget *dialog;
805 GtkWidget *table;
806 GtkWidget *label;
807 GtkWidget *txt_scroll;
808 GtkWidget *txt_desc;
809
810 dialog = gtk_dialog_new_with_buttons(_("Add Waypoint"),
811                              GTK_WINDOW(_window),
812                              GTK_DIALOG_MODAL, GTK_STOCK_OK,
813                              GTK_RESPONSE_ACCEPT,
814                              GTK_STOCK_CANCEL,
815                              GTK_RESPONSE_REJECT, NULL);
816
817         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
818                            table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0);
819
820         gtk_table_attach(GTK_TABLE(table),
821                          label = gtk_label_new(_("Lat, Lon")),
822                          0, 1, 0, 1, GTK_FILL, 0, 2, 4);
823         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
824
825         unit2latlon(unitx, unity, lat, lon);
826         lat_format(lat, tmp1);
827         lon_format(lon, tmp2);
828         p_latlon = g_strdup_printf("%s, %s", tmp1, tmp2);
829         gtk_table_attach(GTK_TABLE(table),
830                          label = gtk_label_new(p_latlon),
831                          1, 2, 0, 1, GTK_FILL, 0, 2, 4);
832         gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
833         g_free(p_latlon);
834
835         gtk_table_attach(GTK_TABLE(table),
836                          label = gtk_label_new(_("Description")),
837                          0, 1, 1, 2, GTK_FILL, 0, 2, 4);
838         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
839
840         txt_scroll = gtk_scrolled_window_new(NULL, NULL);
841         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
842                                             GTK_SHADOW_IN);
843         gtk_table_attach(GTK_TABLE(table),
844                          txt_scroll,
845                          1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
846
847         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
848                                        GTK_POLICY_AUTOMATIC,
849                                        GTK_POLICY_AUTOMATIC);
850
851         txt_desc = gtk_text_view_new();
852         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
853
854         gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
855         gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 400, 60);
856
857         gtk_widget_show_all(dialog);
858
859         while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
860                 GtkTextBuffer *tbuf;
861                 GtkTextIter ti1, ti2;
862                 gchar *desc;
863
864                 tbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt_desc));
865                 gtk_text_buffer_get_iter_at_offset(tbuf, &ti1, 0);
866                 gtk_text_buffer_get_end_iter(tbuf, &ti2);
867                 desc = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
868
869                 if (*desc) {
870                         /* There's a description.  Add a waypoint. */
871                         MACRO_PATH_INCREMENT_TAIL(_route);
872                         _route.tail->unitx = unitx;
873                         _route.tail->unity = unity;
874                         _route.tail->time = 0;
875                         _route.tail->altitude = NAN;
876
877                         MACRO_PATH_INCREMENT_WTAIL(_route);
878                         _route.wtail->point = _route.tail;
879                         _route.wtail->desc
880                             = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
881                 } else {
882                         GtkWidget *confirm;
883
884                         g_free(desc);
885
886                         confirm = hildon_note_new_confirmation(GTK_WINDOW(dialog),
887                                                          _("Creating a \"waypoint\" with no description actually "
888                                                           "adds a break point.  Is that what you want?"));
889
890                         if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
891                                 /* There's no description.  Add a break by adding a (0, 0)
892                                  * point (if necessary), and then the ordinary route point. */
893                                 if (_route.tail->unity) {
894                                         MACRO_PATH_INCREMENT_TAIL(_route);
895                                         *_route.tail = _point_null;
896                                 }
897
898                                 MACRO_PATH_INCREMENT_TAIL(_route);
899                                 _route.tail->unitx = unitx;
900                                 _route.tail->unity = unity;
901                                 _route.tail->time = 0;
902                                 _route.tail->altitude = NAN;
903
904                                 gtk_widget_destroy(confirm);
905                         } else {
906                                 gtk_widget_destroy(confirm);
907                                 continue;
908                         }
909                 }
910
911                 route_find_nearest_point();
912                 map_render_paths();
913                 MACRO_QUEUE_DRAW_AREA();
914                 break;
915         }
916 gtk_widget_destroy(dialog);
917 }
918
919 gboolean 
920 cmenu_cb_loc_show_latlon(GtkAction * action)
921 {
922 cmenu_show_latlon(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
923 return TRUE;
924 }
925
926 gboolean 
927 cmenu_cb_loc_clip_latlon(GtkAction * action)
928 {
929 cmenu_clip_latlon(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
930 return TRUE;
931 }
932
933 gboolean 
934 cmenu_cb_loc_route_to(GtkAction * action)
935 {
936 cmenu_route_to(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
937 return TRUE;
938 }
939
940 gboolean 
941 cmenu_cb_loc_distance_to(GtkAction * action)
942 {
943 cmenu_distance_to(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
944 return TRUE;
945 }
946
947 gboolean 
948 cmenu_cb_loc_add_route(GtkAction * action)
949 {
950 cmenu_add_route(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
951 return TRUE;
952 }
953
954 gboolean 
955 cmenu_cb_loc_add_way(GtkAction * action)
956 {
957 cmenu_route_add_way(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
958 return TRUE;
959 }
960
961 gboolean 
962 cmenu_cb_loc_add_poi(GtkAction * action)
963 {
964 poi_dialog(ACTION_ADD_POI, x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
965 return TRUE;
966 }
967
968 gboolean 
969 cb_add_poi(GtkAction *action)
970 {
971 guint unitx, unity;
972 const gchar *name = gtk_action_get_name(action);
973
974 if (_center_mode > 0) {
975         latlon2unit(_gps.lat, _gps.lon, unitx, unity);
976 } else {
977         unitx=_center.unitx;
978         unity=_center.unity;
979 }
980
981 if (strcmp(name, "poi_add")==0) 
982         poi_dialog(ACTION_ADD_POI, unitx, unity);
983 else if (strcmp(name, "poi_quick_add")==0) 
984         poi_quick_dialog(unitx, unity);
985 else
986         g_assert_not_reached();
987
988 return TRUE;
989 }
990
991 gboolean 
992 cmenu_cb_loc_set_home(GtkAction * action)
993 {
994 guint unitx, unity;
995
996 unitx = x2unit(_cmenu_position_x);
997 unity = y2unit(_cmenu_position_y);
998 unit2latlon(unitx, unity, _home.lat, _home.lon);
999 _home.valid=TRUE;
1000
1001 config_save_home();
1002 map_render_data();
1003 return TRUE;
1004 }
1005
1006 gboolean 
1007 cmenu_cb_loc_set_destination(GtkAction *action)
1008 {
1009 guint unitx, unity;
1010
1011 unitx = x2unit(_cmenu_position_x);
1012 unity = y2unit(_cmenu_position_y);
1013 unit2latlon(unitx, unity, _dest.lat, _dest.lon);
1014 _dest.valid=TRUE;
1015 map_update_location_from_center();
1016 return TRUE;
1017 }
1018
1019 gboolean 
1020 cmenu_cb_loc_set_gps(GtkAction * action)
1021 {
1022 _pos.unitx = x2unit(_cmenu_position_x);
1023 _pos.unity = y2unit(_cmenu_position_y);
1024 unit2latlon(_pos.unitx, _pos.unity, _gps.lat, _gps.lon);
1025
1026 /* Move mark to new location. */
1027 map_refresh_mark();
1028 track_add(time(NULL), FALSE);
1029
1030 return TRUE;
1031 }
1032
1033 gboolean 
1034 cmenu_cb_way_show_latlon(GtkAction * action)
1035 {
1036 WayPoint *way;
1037
1038 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1039                 cmenu_show_latlon(way->point->unitx, way->point->unity);
1040
1041 return TRUE;
1042 }
1043
1044 gboolean 
1045 cmenu_cb_way_show_desc(GtkAction * action)
1046 {
1047 WayPoint *way;
1048
1049 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y)))) {
1050         MACRO_BANNER_SHOW_INFO(_window, way->desc);
1051 }
1052
1053 return TRUE;
1054 }
1055
1056 gboolean 
1057 cmenu_cb_way_clip_latlon(GtkAction * action)
1058 {
1059 WayPoint *way;
1060
1061 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1062                 cmenu_clip_latlon(way->point->unitx, way->point->unity);
1063 return TRUE;
1064 }
1065
1066 gboolean 
1067 cmenu_cb_way_clip_desc(GtkAction * action)
1068 {
1069 WayPoint *way;
1070
1071 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1072         gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), way->desc, -1);
1073
1074 return TRUE;
1075 }
1076
1077 gboolean 
1078 cmenu_cb_way_route_to(GtkAction * action)
1079 {
1080 WayPoint *way;
1081
1082 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1083         cmenu_route_to(way->point->unitx, way->point->unity);
1084
1085 return TRUE;
1086 }
1087
1088 gboolean 
1089 cmenu_cb_way_distance_to(GtkAction * action)
1090 {
1091 WayPoint *way;
1092
1093 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1094         route_show_distance_to(way->point);
1095
1096 return TRUE;
1097 }
1098
1099 gboolean 
1100 cmenu_cb_way_delete(GtkAction * action)
1101 {
1102 WayPoint *way;
1103
1104 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y)))) {
1105         gchar buffer[BUFFER_SIZE];
1106         GtkWidget *confirm;
1107
1108         snprintf(buffer, sizeof(buffer), "%s:\n%s\n",
1109                  _("Confirm delete of waypoint"), way->desc);
1110         confirm = hildon_note_new_confirmation(GTK_WINDOW(_window), buffer);
1111
1112         if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
1113                 Point *pdel_min, *pdel_max, *pdel_start, *pdel_end;
1114                 guint num_del;
1115
1116                 /* Delete surrounding route data, too. */
1117                 if (way == _route.whead)
1118                         pdel_min = _route.head;
1119                 else
1120                         pdel_min = way[-1].point;
1121
1122                 if (way == _route.wtail)
1123                         pdel_max = _route.tail;
1124                 else
1125                         pdel_max = way[1].point;
1126
1127                 /* Find largest continuous segment around the waypoint, EXCLUDING
1128                  * pdel_min and pdel_max. */
1129                 for (pdel_start = way->point - 1; pdel_start->unity
1130                      && pdel_start > pdel_min; pdel_start--) {
1131                 }
1132                 for (pdel_end = way->point + 1; pdel_end->unity
1133                      && pdel_end < pdel_max; pdel_end++) {
1134                 }
1135
1136                 /* If pdel_end is set to _route.tail, and if _route.tail is a
1137                  * non-zero point, then delete _route.tail. */
1138                 if (pdel_end == _route.tail && pdel_end->unity)
1139                         pdel_end++;     /* delete _route.tail too */
1140                 /* else, if *both* endpoints are zero points, delete one. */
1141                 else if (!pdel_start->unity && !pdel_end->unity)
1142                         pdel_start--;
1143
1144                 /* Delete BETWEEN pdel_start and pdel_end, exclusive. */
1145                 num_del = pdel_end - pdel_start - 1;
1146
1147                 memmove(pdel_start + 1, pdel_end,(_route.tail - pdel_end + 1) * sizeof(Point));
1148                 _route.tail -= num_del;
1149
1150                 /* Remove waypoint and move/adjust subsequent waypoints. */
1151                 g_free(way->desc);
1152                 while (way++ != _route.wtail) {
1153                         way[-1] = *way;
1154                         way[-1].point -= num_del;
1155                 }
1156                 _route.wtail--;
1157
1158                 route_find_nearest_point();
1159                 map_force_redraw();
1160         }
1161         gtk_widget_destroy(confirm);
1162 }
1163
1164 return TRUE;
1165 }
1166
1167 gboolean 
1168 cmenu_cb_way_add_poi(GtkAction * action)
1169 {
1170 WayPoint *way;
1171
1172 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1173         poi_dialog(ACTION_ADD_POI, way->point->unitx, way->point->unity);
1174
1175 return TRUE;
1176 }
1177
1178 gboolean 
1179 cmenu_cb_poi_route_to(GtkAction * action)
1180 {
1181 poi_info poi;
1182
1183 if (select_poi(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1184         guint unitx, unity;
1185         latlon2unit(poi.lat, poi.lon, unitx, unity);
1186         cmenu_route_to(unitx, unity);
1187 }
1188
1189 return TRUE;
1190 }
1191
1192 gboolean 
1193 cmenu_cb_poi_distance_to(GtkAction * action)
1194 {
1195 poi_info poi;
1196
1197 if (select_poi(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1198         guint unitx, unity;
1199         latlon2unit(poi.lat, poi.lon, unitx, unity);
1200         cmenu_distance_to(unitx, unity);
1201 }
1202
1203 return TRUE;
1204 }
1205
1206 gboolean 
1207 cmenu_cb_poi_add_route(GtkAction * action)
1208 {
1209 poi_info poi;
1210
1211 if (select_poi(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1212         guint unitx, unity;
1213         latlon2unit(poi.lat, poi.lon, unitx, unity);
1214         cmenu_add_route(unitx, unity);
1215 }
1216
1217 return TRUE;
1218 }
1219
1220 gboolean 
1221 cmenu_cb_poi_add_way(GtkAction * action)
1222 {
1223 poi_info poi;
1224
1225 if (select_poi(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1226         guint unitx, unity;
1227         latlon2unit(poi.lat, poi.lon, unitx, unity);
1228         cmenu_route_add_way(unitx, unity);
1229 }
1230
1231 return TRUE;
1232 }
1233
1234 gboolean 
1235 cmenu_cb_poi_edit_poi(GtkAction * action)
1236 {
1237 poi_dialog(ACTION_EDIT_POI, x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
1238 return TRUE;
1239 }