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