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