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