]> err.no Git - mapper/blob - src/cb.c
More POI search UI work.
[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 #ifdef WITH_DEVICE_770
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 return TRUE;
547 }
548
549 gboolean 
550 menu_cb_about(GtkAction * action)
551 {
552 gchar *authors[]={
553         "Kaj-Michael Lang",
554         "John Costigan",
555         "Cezary Jackiewicz", NULL
556 };
557
558 gtk_show_about_dialog(GTK_WINDOW(_window), 
559         "name", "Mapper",
560         "version", VERSION, 
561         "copyright", "Kaj-Michael Lang",
562         "license", "GPL",
563         "authors", authors,
564         NULL);
565 return TRUE;
566 }
567
568 gboolean 
569 window_cb_key_press(GtkWidget * widget, GdkEventKey * event)
570 {
571 CustomKey custom_key;
572
573 switch (event->keyval) {
574         case HILDON_HARDKEY_UP:
575                 custom_key = CUSTOM_KEY_UP;
576         break;
577         case HILDON_HARDKEY_DOWN:
578                 custom_key = CUSTOM_KEY_DOWN;
579         break;
580         case HILDON_HARDKEY_LEFT:
581                 custom_key = CUSTOM_KEY_LEFT;
582         break;
583         case HILDON_HARDKEY_RIGHT:
584                 custom_key = CUSTOM_KEY_RIGHT;
585         break;
586         case HILDON_HARDKEY_SELECT:
587                 custom_key = CUSTOM_KEY_SELECT;
588         break;
589         case HILDON_HARDKEY_INCREASE:
590                 custom_key = CUSTOM_KEY_INCREASE;
591         break;
592         case HILDON_HARDKEY_DECREASE:
593                 custom_key = CUSTOM_KEY_DECREASE;
594         break;
595         case HILDON_HARDKEY_FULLSCREEN:
596                 custom_key = CUSTOM_KEY_FULLSCREEN;
597         break;
598         case HILDON_HARDKEY_ESC:
599                 custom_key = CUSTOM_KEY_ESC;
600         break;
601         default:
602                 return FALSE;
603 }
604
605         switch (_action[custom_key]) {
606         case CUSTOM_ACTION_PAN_NORTH:
607                 map_pan(0, -PAN_UNITS);
608         break;
609         case CUSTOM_ACTION_PAN_WEST:
610                 map_pan(-PAN_UNITS, 0);
611         break;
612         case CUSTOM_ACTION_PAN_SOUTH:
613                 map_pan(0, PAN_UNITS);
614         break;
615         case CUSTOM_ACTION_PAN_EAST:
616                 map_pan(PAN_UNITS, 0);
617         break;
618         case CUSTOM_ACTION_TOGGLE_AUTOCENTER:
619                 switch (_center_mode) {
620                 case CENTER_LATLON:
621                 case CENTER_WAS_LEAD:
622                         set_action_activate("autocenter_lead", TRUE);
623                 break;
624                 case CENTER_LEAD:
625                 case CENTER_WAS_LATLON:
626                         set_action_activate("autocenter_latlon", TRUE);
627                 break;
628                 default:
629                         set_action_activate("autocenter_latlon", TRUE);
630                 break;
631                 }
632         break;
633         case CUSTOM_ACTION_ZOOM_IN:
634         case CUSTOM_ACTION_ZOOM_OUT:
635                 if (!_key_zoom_timeout_sid) {
636                         g_printf("Z: %d %d\n", _key_zoom_new, _zoom);
637                         _key_zoom_new = _zoom + (_action[custom_key] == CUSTOM_ACTION_ZOOM_IN
638                                ? -_curr_repo->view_zoom_steps : _curr_repo->view_zoom_steps);
639                         /* Remember, _key_zoom_new is unsigned. */
640                         if (_key_zoom_new < MAX_ZOOM) {
641                                 gchar buffer[80];
642                                 snprintf(buffer, sizeof(buffer), "%s %d", _("Zoom to Level"), _key_zoom_new);
643                                 MACRO_BANNER_SHOW_INFO(_window, buffer);
644                                 _key_zoom_timeout_sid = g_timeout_add(400, map_key_zoom_timeout, NULL);
645                         }
646                 }
647         break;
648         case CUSTOM_ACTION_TOGGLE_FULLSCREEN:
649                 set_action_activate("view_fullscreen", !_fullscreen);
650         break;
651         case CUSTOM_ACTION_TOGGLE_TRACKS:
652                 switch (_show_tracks) {
653                 case 0:
654                         /* Nothing shown, nothing saved; just set both. */
655                         _show_tracks = TRACKS_MASK | ROUTES_MASK;
656                         break;
657                 case TRACKS_MASK << 16:
658                 case ROUTES_MASK << 16:
659                 case (ROUTES_MASK | TRACKS_MASK) << 16:
660                         /* Something was saved and nothing changed since.
661                          * Restore saved. */
662                         _show_tracks = _show_tracks >> 16;
663                         break;
664                 default:
665                         /* There is no history, or they changed something
666                          * since the last historical change. Save and
667                          * clear. */
668                         _show_tracks = _show_tracks << 16;
669                 }
670                 set_action_activate("view_route", _show_tracks & ROUTES_MASK);
671                 set_action_activate("view_track", _show_tracks & TRACKS_MASK);
672         break;
673         case CUSTOM_ACTION_TOGGLE_SCALE:
674                 set_action_activate("view_scale", _show_scale);
675         break;
676         case CUSTOM_ACTION_TOGGLE_POI:
677                 set_action_activate("view_poi", _show_poi);
678         break;
679         case CUSTOM_ACTION_CHANGE_REPO: {
680                         GList *curr = g_list_find(_repo_list, _curr_repo);
681                         if (!curr)
682                                 break;
683
684                         /* Loop until we reach a next-able repo, or until we get
685                          * back to the current repo. */
686                         while ((curr = (curr->next ? curr->next : _repo_list)) && !((RepoData *) curr->data)->nextable && curr->data != _curr_repo) {
687                         }
688
689                         if (curr->data != _curr_repo) {
690                                 repo_set_curr(curr->data);
691                                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(_curr_repo->menu_item), TRUE);
692                         } else {
693                                 popup_error(_window, _("There are no other next-able repositories."));
694                         }
695                         break;
696                 }
697         break;
698         case CUSTOM_ACTION_ROUTE_DISTNEXT:
699                 route_show_distance_to_next();
700         break;
701         case CUSTOM_ACTION_ROUTE_DISTLAST:
702                 route_show_distance_to_last();
703         break;
704         case CUSTOM_ACTION_TRACK_BREAK:
705                 track_insert_break();
706         break;
707         case CUSTOM_ACTION_TRACK_DISTLAST:
708                 track_show_distance_from_last();
709         break;
710         case CUSTOM_ACTION_TRACK_DISTFIRST:
711                 track_show_distance_from_first();
712         break;
713         case CUSTOM_ACTION_TOGGLE_GPS:
714                 set_action_activate("gps_enable", !_enable_gps);
715         break;
716         case CUSTOM_ACTION_TOGGLE_GPSINFO:
717                 set_action_activate("gps_info", !_gps_info);
718         break;
719         case CUSTOM_ACTION_TOGGLE_SPEEDLIMIT:
720                 _speed_limit_on ^= 1;
721         break;
722         default:
723                 return FALSE;
724 }
725 return TRUE;
726 }
727
728 gboolean 
729 window_cb_key_release(GtkWidget * widget, GdkEventKey * event)
730 {
731 switch (event->keyval) {
732         case HILDON_HARDKEY_INCREASE:
733         case HILDON_HARDKEY_DECREASE:
734                 if (_key_zoom_timeout_sid) {
735                         g_source_remove(_key_zoom_timeout_sid);
736                         _key_zoom_timeout_sid = 0;
737                         map_set_zoom(_key_zoom_new);
738                 }
739         return TRUE;
740         break;
741         default:
742                 return FALSE;
743 }
744 }
745
746 void 
747 cmenu_show_latlon(guint unitx, guint unity)
748 {
749 gdouble lat, lon;
750 gchar buffer[80], tmp1[16], tmp2[16];
751
752 unit2latlon(unitx, unity, lat, lon);
753 lat_format(lat, tmp1);
754 lon_format(lon, tmp2);
755
756 snprintf(buffer, sizeof(buffer),
757          "%s: %s\n"
758          "%s: %s", _("Latitude"), tmp1, _("Longitude"), tmp2);
759
760 MACRO_BANNER_SHOW_INFO(_window, buffer);
761 }
762
763 void 
764 cmenu_clip_latlon(guint unitx, guint unity)
765 {
766 gchar buffer[80];
767 gdouble lat, lon;
768
769 unit2latlon(unitx, unity, lat, lon);
770 snprintf(buffer, sizeof(buffer), "%.06f,%.06f", lat, lon);
771
772 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), buffer, -1);
773 }
774
775 void 
776 cmenu_route_to(guint unitx, guint unity)
777 {
778 gchar buffer[80];
779 gchar strlat[32];
780 gchar strlon[32];
781 gdouble lat, lon;
782
783 unit2latlon(unitx, unity, lat, lon);
784
785 g_ascii_formatd(strlat, 32, "%.06f", lat);
786 g_ascii_formatd(strlon, 32, "%.06f", lon);
787 snprintf(buffer, sizeof(buffer), "%s, %s", strlat, strlon);
788
789 route_download(buffer);
790 }
791
792 void 
793 cmenu_distance_to(guint unitx, guint unity)
794 {
795 gchar buffer[80];
796 gdouble lat, lon;
797
798 unit2latlon(unitx, unity, lat, lon);
799
800 snprintf(buffer, sizeof(buffer), "%s: %.02lf %s", _("Distance"),
801          calculate_distance(_gps.lat, _gps.lon, lat, lon) * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
802 MACRO_BANNER_SHOW_INFO(_window, buffer);
803 }
804
805 void 
806 cmenu_add_route(guint unitx, guint unity)
807 {
808 MACRO_PATH_INCREMENT_TAIL(_route);
809 _route.tail->unitx = x2unit(_cmenu_position_x);
810 _route.tail->unity = y2unit(_cmenu_position_y);
811 route_find_nearest_point();
812 map_force_redraw();
813 }
814
815 void cmenu_route_add_way(guint unitx, guint unity)
816 {
817 gdouble lat, lon;
818 gchar tmp1[16], tmp2[16], *p_latlon;
819 GtkWidget *dialog;
820 GtkWidget *table;
821 GtkWidget *label;
822 GtkWidget *txt_scroll;
823 GtkWidget *txt_desc;
824
825 dialog = gtk_dialog_new_with_buttons(_("Add Waypoint"),
826                              GTK_WINDOW(_window),
827                              GTK_DIALOG_MODAL, GTK_STOCK_OK,
828                              GTK_RESPONSE_ACCEPT,
829                              GTK_STOCK_CANCEL,
830                              GTK_RESPONSE_REJECT, NULL);
831
832         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
833                            table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0);
834
835         gtk_table_attach(GTK_TABLE(table),
836                          label = gtk_label_new(_("Lat, Lon")),
837                          0, 1, 0, 1, GTK_FILL, 0, 2, 4);
838         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
839
840         unit2latlon(unitx, unity, lat, lon);
841         lat_format(lat, tmp1);
842         lon_format(lon, tmp2);
843         p_latlon = g_strdup_printf("%s, %s", tmp1, tmp2);
844         gtk_table_attach(GTK_TABLE(table),
845                          label = gtk_label_new(p_latlon),
846                          1, 2, 0, 1, GTK_FILL, 0, 2, 4);
847         gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
848         g_free(p_latlon);
849
850         gtk_table_attach(GTK_TABLE(table),
851                          label = gtk_label_new(_("Description")),
852                          0, 1, 1, 2, GTK_FILL, 0, 2, 4);
853         gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
854
855         txt_scroll = gtk_scrolled_window_new(NULL, NULL);
856         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scroll),
857                                             GTK_SHADOW_IN);
858         gtk_table_attach(GTK_TABLE(table),
859                          txt_scroll,
860                          1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 2, 4);
861
862         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scroll),
863                                        GTK_POLICY_AUTOMATIC,
864                                        GTK_POLICY_AUTOMATIC);
865
866         txt_desc = gtk_text_view_new();
867         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
868
869         gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
870         gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 400, 60);
871
872         gtk_widget_show_all(dialog);
873
874         while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
875                 GtkTextBuffer *tbuf;
876                 GtkTextIter ti1, ti2;
877                 gchar *desc;
878
879                 tbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt_desc));
880                 gtk_text_buffer_get_iter_at_offset(tbuf, &ti1, 0);
881                 gtk_text_buffer_get_end_iter(tbuf, &ti2);
882                 desc = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
883
884                 if (*desc) {
885                         /* There's a description.  Add a waypoint. */
886                         MACRO_PATH_INCREMENT_TAIL(_route);
887                         _route.tail->unitx = unitx;
888                         _route.tail->unity = unity;
889                         _route.tail->time = 0;
890                         _route.tail->altitude = NAN;
891
892                         MACRO_PATH_INCREMENT_WTAIL(_route);
893                         _route.wtail->point = _route.tail;
894                         _route.wtail->desc
895                             = gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE);
896                 } else {
897                         GtkWidget *confirm;
898
899                         g_free(desc);
900
901                         confirm = hildon_note_new_confirmation(GTK_WINDOW(dialog),
902                                                          _("Creating a \"waypoint\" with no description actually "
903                                                           "adds a break point.  Is that what you want?"));
904
905                         if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
906                                 /* There's no description.  Add a break by adding a (0, 0)
907                                  * point (if necessary), and then the ordinary route point. */
908                                 if (_route.tail->unity) {
909                                         MACRO_PATH_INCREMENT_TAIL(_route);
910                                         *_route.tail = _point_null;
911                                 }
912
913                                 MACRO_PATH_INCREMENT_TAIL(_route);
914                                 _route.tail->unitx = unitx;
915                                 _route.tail->unity = unity;
916                                 _route.tail->time = 0;
917                                 _route.tail->altitude = NAN;
918
919                                 gtk_widget_destroy(confirm);
920                         } else {
921                                 gtk_widget_destroy(confirm);
922                                 continue;
923                         }
924                 }
925
926                 route_find_nearest_point();
927                 map_render_paths();
928                 MACRO_QUEUE_DRAW_AREA();
929                 break;
930         }
931 gtk_widget_destroy(dialog);
932 }
933
934 gboolean 
935 cmenu_cb_loc_show_latlon(GtkAction * action)
936 {
937 cmenu_show_latlon(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
938 return TRUE;
939 }
940
941 gboolean 
942 cmenu_cb_loc_clip_latlon(GtkAction * action)
943 {
944 cmenu_clip_latlon(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
945 return TRUE;
946 }
947
948 gboolean 
949 cmenu_cb_loc_route_to(GtkAction * action)
950 {
951 cmenu_route_to(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
952 return TRUE;
953 }
954
955 gboolean 
956 cmenu_cb_loc_distance_to(GtkAction * action)
957 {
958 cmenu_distance_to(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
959 return TRUE;
960 }
961
962 gboolean 
963 cmenu_cb_loc_add_route(GtkAction * action)
964 {
965 cmenu_add_route(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
966 return TRUE;
967 }
968
969 gboolean 
970 cmenu_cb_loc_add_way(GtkAction * action)
971 {
972 cmenu_route_add_way(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
973 return TRUE;
974 }
975
976 gboolean 
977 cmenu_cb_loc_add_poi(GtkAction * action)
978 {
979 poi_dialog(ACTION_ADD_POI, x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
980 return TRUE;
981 }
982
983 gboolean
984 cb_poi_search(GtkAction *action)
985 {
986 poi_info poi;
987
988 poi_search_dialog(NULL, &poi);
989 return TRUE;
990 }
991
992 gboolean 
993 cb_poi_add(GtkAction *action)
994 {
995 guint unitx, unity;
996 const gchar *name = gtk_action_get_name(action);
997
998 if (_center_mode > 0) {
999         latlon2unit(_gps.lat, _gps.lon, unitx, unity);
1000 } else {
1001         unitx=_center.unitx;
1002         unity=_center.unity;
1003 }
1004
1005 if (strcmp(name, "poi_add")==0) 
1006         poi_dialog(ACTION_ADD_POI, unitx, unity);
1007 else if (strcmp(name, "poi_quick_add")==0) 
1008         poi_quick_dialog(unitx, unity);
1009 else
1010         g_assert_not_reached();
1011
1012 return TRUE;
1013 }
1014
1015 gboolean 
1016 cmenu_cb_loc_set_home(GtkAction * action)
1017 {
1018 guint unitx, unity;
1019
1020 unitx = x2unit(_cmenu_position_x);
1021 unity = y2unit(_cmenu_position_y);
1022 unit2latlon(unitx, unity, _home.lat, _home.lon);
1023 _home.valid=TRUE;
1024
1025 config_save_home();
1026 map_render_data();
1027 return TRUE;
1028 }
1029
1030 gboolean 
1031 cmenu_cb_loc_set_destination(GtkAction *action)
1032 {
1033 guint unitx, unity;
1034
1035 unitx = x2unit(_cmenu_position_x);
1036 unity = y2unit(_cmenu_position_y);
1037 unit2latlon(unitx, unity, _dest.lat, _dest.lon);
1038 _dest.valid=TRUE;
1039 map_update_location_from_center();
1040 return TRUE;
1041 }
1042
1043 gboolean 
1044 cmenu_cb_loc_set_gps(GtkAction * action)
1045 {
1046 _pos.unitx = x2unit(_cmenu_position_x);
1047 _pos.unity = y2unit(_cmenu_position_y);
1048 unit2latlon(_pos.unitx, _pos.unity, _gps.lat, _gps.lon);
1049
1050 /* Move mark to new location. */
1051 map_refresh_mark();
1052 track_add(time(NULL), FALSE);
1053
1054 return TRUE;
1055 }
1056
1057 gboolean 
1058 cmenu_cb_way_show_latlon(GtkAction * action)
1059 {
1060 WayPoint *way;
1061
1062 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1063                 cmenu_show_latlon(way->point->unitx, way->point->unity);
1064
1065 return TRUE;
1066 }
1067
1068 gboolean 
1069 cmenu_cb_way_show_desc(GtkAction * action)
1070 {
1071 WayPoint *way;
1072
1073 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y)))) {
1074         MACRO_BANNER_SHOW_INFO(_window, way->desc);
1075 }
1076
1077 return TRUE;
1078 }
1079
1080 gboolean 
1081 cmenu_cb_way_clip_latlon(GtkAction * action)
1082 {
1083 WayPoint *way;
1084
1085 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1086                 cmenu_clip_latlon(way->point->unitx, way->point->unity);
1087 return TRUE;
1088 }
1089
1090 gboolean 
1091 cmenu_cb_way_clip_desc(GtkAction * action)
1092 {
1093 WayPoint *way;
1094
1095 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1096         gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), way->desc, -1);
1097
1098 return TRUE;
1099 }
1100
1101 gboolean 
1102 cmenu_cb_way_route_to(GtkAction * action)
1103 {
1104 WayPoint *way;
1105
1106 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1107         cmenu_route_to(way->point->unitx, way->point->unity);
1108
1109 return TRUE;
1110 }
1111
1112 gboolean 
1113 cmenu_cb_way_distance_to(GtkAction * action)
1114 {
1115 WayPoint *way;
1116
1117 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1118         route_show_distance_to(way->point);
1119
1120 return TRUE;
1121 }
1122
1123 gboolean 
1124 cmenu_cb_way_delete(GtkAction * action)
1125 {
1126 WayPoint *way;
1127
1128 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y)))) {
1129         gchar buffer[BUFFER_SIZE];
1130         GtkWidget *confirm;
1131
1132         snprintf(buffer, sizeof(buffer), "%s:\n%s\n",
1133                  _("Confirm delete of waypoint"), way->desc);
1134         confirm = hildon_note_new_confirmation(GTK_WINDOW(_window), buffer);
1135
1136         if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
1137                 Point *pdel_min, *pdel_max, *pdel_start, *pdel_end;
1138                 guint num_del;
1139
1140                 /* Delete surrounding route data, too. */
1141                 if (way == _route.whead)
1142                         pdel_min = _route.head;
1143                 else
1144                         pdel_min = way[-1].point;
1145
1146                 if (way == _route.wtail)
1147                         pdel_max = _route.tail;
1148                 else
1149                         pdel_max = way[1].point;
1150
1151                 /* Find largest continuous segment around the waypoint, EXCLUDING
1152                  * pdel_min and pdel_max. */
1153                 for (pdel_start = way->point - 1; pdel_start->unity
1154                      && pdel_start > pdel_min; pdel_start--) {
1155                 }
1156                 for (pdel_end = way->point + 1; pdel_end->unity
1157                      && pdel_end < pdel_max; pdel_end++) {
1158                 }
1159
1160                 /* If pdel_end is set to _route.tail, and if _route.tail is a
1161                  * non-zero point, then delete _route.tail. */
1162                 if (pdel_end == _route.tail && pdel_end->unity)
1163                         pdel_end++;     /* delete _route.tail too */
1164                 /* else, if *both* endpoints are zero points, delete one. */
1165                 else if (!pdel_start->unity && !pdel_end->unity)
1166                         pdel_start--;
1167
1168                 /* Delete BETWEEN pdel_start and pdel_end, exclusive. */
1169                 num_del = pdel_end - pdel_start - 1;
1170
1171                 memmove(pdel_start + 1, pdel_end,(_route.tail - pdel_end + 1) * sizeof(Point));
1172                 _route.tail -= num_del;
1173
1174                 /* Remove waypoint and move/adjust subsequent waypoints. */
1175                 g_free(way->desc);
1176                 while (way++ != _route.wtail) {
1177                         way[-1] = *way;
1178                         way[-1].point -= num_del;
1179                 }
1180                 _route.wtail--;
1181
1182                 route_find_nearest_point();
1183                 map_force_redraw();
1184         }
1185         gtk_widget_destroy(confirm);
1186 }
1187
1188 return TRUE;
1189 }
1190
1191 gboolean
1192 menu_cb_category(GtkAction * action)
1193 {
1194 if (category_list())
1195         map_force_redraw();
1196
1197 return TRUE;
1198 }
1199
1200 gboolean 
1201 cmenu_cb_way_add_poi(GtkAction * action)
1202 {
1203 WayPoint *way;
1204
1205 if ((way = find_nearest_waypoint(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y))))
1206         poi_dialog(ACTION_ADD_POI, way->point->unitx, way->point->unity);
1207
1208 return TRUE;
1209 }
1210
1211 gboolean 
1212 cmenu_cb_poi_route_to(GtkAction * action)
1213 {
1214 poi_info poi;
1215
1216 if (poi_select(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1217         guint unitx, unity;
1218         latlon2unit(poi.lat, poi.lon, unitx, unity);
1219         cmenu_route_to(unitx, unity);
1220 }
1221
1222 return TRUE;
1223 }
1224
1225 gboolean 
1226 cmenu_cb_poi_distance_to(GtkAction * action)
1227 {
1228 poi_info poi;
1229
1230 if (poi_select(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1231         guint unitx, unity;
1232         latlon2unit(poi.lat, poi.lon, unitx, unity);
1233         cmenu_distance_to(unitx, unity);
1234 }
1235
1236 return TRUE;
1237 }
1238
1239 gboolean 
1240 cmenu_cb_poi_add_route(GtkAction * action)
1241 {
1242 poi_info poi;
1243
1244 if (poi_select(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1245         guint unitx, unity;
1246         latlon2unit(poi.lat, poi.lon, unitx, unity);
1247         cmenu_add_route(unitx, unity);
1248 }
1249
1250 return TRUE;
1251 }
1252
1253 gboolean 
1254 cmenu_cb_poi_add_way(GtkAction * action)
1255 {
1256 poi_info poi;
1257
1258 if (poi_select(x2unit(_cmenu_position_x), y2unit(_cmenu_position_y), &poi)) {
1259         guint unitx, unity;
1260         latlon2unit(poi.lat, poi.lon, unitx, unity);
1261         cmenu_route_add_way(unitx, unity);
1262 }
1263
1264 return TRUE;
1265 }
1266
1267 gboolean 
1268 cmenu_cb_poi_edit_poi(GtkAction * action)
1269 {
1270 poi_dialog(ACTION_EDIT_POI, x2unit(_cmenu_position_x), y2unit(_cmenu_position_y));
1271 return TRUE;
1272 }