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