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