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