]> err.no Git - mapper/blob - src/cb.c
Use help.c wrappers
[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 #include "help.h"
38
39 gboolean 
40 menu_cb_route_download(GtkAction * action)
41 {
42 GtkListStore *store;
43
44 route_download(NULL);
45 store=route_generate_store();
46 if (store!=NULL) {
47         gtk_tree_view_set_model(route_tree_view, store);
48         g_object_unref(G_OBJECT(store));
49 }
50 return TRUE;
51 }
52
53 gboolean 
54 menu_cb_route_open(GtkAction * action)
55 {
56 GtkListStore *store;
57
58 route_open_file();
59 store=route_generate_store();
60 if (store!=NULL) {
61         gtk_tree_view_set_model(route_tree_view, store);
62         g_object_unref(G_OBJECT(store));
63 }
64 return TRUE;
65 }
66
67 gboolean 
68 menu_cb_route_distnext(GtkAction * action)
69 {
70 route_show_distance_to_next();
71 return TRUE;
72 }
73
74 gboolean 
75 menu_cb_route_distlast(GtkAction * action)
76 {
77 route_show_distance_to_last();
78 return TRUE;
79 }
80
81 gboolean 
82 menu_cb_route_reset(GtkAction * action)
83 {
84 route_find_nearest_point();
85 map_render_data();
86 MACRO_QUEUE_DRAW_AREA();
87 return TRUE;
88 }
89
90 gboolean 
91 menu_cb_route_clear(GtkAction * action)
92 {
93 route_clear();
94 gtk_tree_view_set_model(route_tree_view, NULL);
95 return TRUE;
96 }
97
98 gboolean 
99 menu_cb_track_open(GtkAction * action)
100 {
101 track_open();
102 return TRUE;
103 }
104
105 gboolean 
106 menu_cb_track_save(GtkAction * action)
107 {
108 track_save();
109 return TRUE;
110 }
111
112 gboolean 
113 menu_cb_track_insert_break(GtkAction * action)
114 {
115 track_insert_break();
116 return TRUE;
117 }
118
119 gboolean 
120 menu_cb_track_insert_mark(GtkAction * action)
121 {
122 track_insert_mark();
123 return TRUE;
124 }
125
126 gboolean 
127 menu_cb_track_distlast(GtkAction * action)
128 {
129 track_show_distance_from_last();
130 return TRUE;
131 }
132
133 gboolean 
134 menu_cb_track_distfirst(GtkAction * action)
135 {
136 track_show_distance_from_first();
137 return TRUE;
138 }
139
140 gboolean 
141 menu_cb_route_save(GtkAction * action)
142 {
143 route_save();
144 return TRUE;
145 }
146
147 gboolean 
148 menu_cb_track_clear(GtkAction * action)
149 {
150 track_clear();
151 return TRUE;
152 }
153
154 gboolean 
155 menu_cb_track_filter(GtkAction * action)
156 {
157 filter_dialog();
158 return TRUE;
159 }
160
161 gboolean 
162 menu_cb_show_tracks(GtkAction *action)
163 {
164 _show_tracks ^= TRACKS_MASK;
165 if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action))) {
166         _show_tracks |= TRACKS_MASK;
167         map_render_paths();
168         MACRO_QUEUE_DRAW_AREA();
169         MACRO_BANNER_SHOW_INFO(_window, _("Tracks are now shown"));
170 } else {
171         _show_tracks &= ~TRACKS_MASK;
172         map_force_redraw();
173         MACRO_BANNER_SHOW_INFO(_window, _("Tracks are now hidden"));
174 }
175 return TRUE;
176 }
177
178 gboolean 
179 menu_cb_show_scale(GtkAction * action)
180 {
181 _show_scale = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
182 MACRO_QUEUE_DRAW_AREA();
183 return TRUE;
184 }
185
186 gboolean 
187 menu_cb_show_routes(GtkAction * action)
188 {
189 if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action))) {
190         _show_tracks |= ROUTES_MASK;
191         map_render_paths();
192         MACRO_QUEUE_DRAW_AREA();
193         MACRO_BANNER_SHOW_INFO(_window, _("Routes are now shown"));
194 } else {
195         _show_tracks &= ~ROUTES_MASK;
196         map_force_redraw();
197         MACRO_BANNER_SHOW_INFO(_window, _("Routes are now hidden"));
198 }
199 return TRUE;
200 }
201
202 gboolean 
203 menu_cb_show_velvec(GtkAction * action)
204 {
205 _show_velvec = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
206 map_move_mark();
207 return TRUE;
208 }
209
210 gboolean 
211 menu_cb_show_poi(GtkAction * action)
212 {
213 _show_poi = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
214 map_force_redraw();
215 return TRUE;
216 }
217
218 gboolean 
219 menu_cb_gps_show_info(GtkAction * action)
220 {
221 _gps_info = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
222 gps_show_info();
223 return TRUE;
224 }
225
226 gboolean
227 menu_cb_autocenter(GtkAction *action, GtkRadioAction *current)
228 {
229 guint new_center_unitx, new_center_unity;
230 gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
231
232 switch (value) {
233         case CENTER_LEAD:
234                 _center_mode = CENTER_LEAD;
235                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Mode: Lead"));
236         break;
237         case CENTER_LATLON:
238                 _center_mode = CENTER_LATLON;
239                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Mode: Lat/Lon"));
240         break;
241         case CENTER_MANUAL:
242         default:
243                 _center_mode = -_center_mode;
244                 MACRO_BANNER_SHOW_INFO(_window, _("Auto-Center Off"));
245                 return TRUE;
246         break;
247 }
248
249 MACRO_RECALC_CENTER(new_center_unitx, new_center_unity);
250 map_center_unit(new_center_unitx, new_center_unity);
251 return TRUE;
252 }
253
254 gboolean 
255 menu_cb_goto_latlon(GtkAction * action)
256 {
257 GtkWidget *dialog;
258 GtkWidget *table;
259 GtkWidget *label;
260 GtkWidget *txt_lat;
261 GtkWidget *txt_lon;
262
263 dialog = gtk_dialog_new_with_buttons(_("Go to Lat/Lon"),
264                      GTK_WINDOW(_window),
265                      GTK_DIALOG_MODAL, GTK_STOCK_OK,
266                      GTK_RESPONSE_ACCEPT,
267                      GTK_STOCK_CANCEL,
268                      GTK_RESPONSE_REJECT, NULL);
269
270 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 3, FALSE), TRUE, TRUE, 0);
271
272 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Latitude")),0, 1, 0, 1, GTK_FILL, 0, 2, 4);
273 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
274
275 gtk_table_attach(GTK_TABLE(table), txt_lat = gtk_entry_new(), 1, 2, 0, 1, GTK_FILL, 0, 2, 4);
276 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
277
278 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Longitude")), 0, 1, 1, 2, GTK_FILL, 0, 2, 4);
279 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
280
281 gtk_table_attach(GTK_TABLE(table), txt_lon = gtk_entry_new(), 1, 2, 1, 2, GTK_FILL, 0, 2, 4);
282 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
283
284 #if defined (WITH_DEVICE_770) && !defined(WITH_HILDON_NEW)
285 g_object_set(G_OBJECT(txt_lat), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
286 g_object_set(G_OBJECT(txt_lon), HILDON_INPUT_MODE_HINT, HILDON_INPUT_MODE_HINT_NUMERICSPECIAL, NULL);
287 #endif
288
289 /* Initialize with the current center position. */
290 {
291         gchar buffer[32];
292         gdouble lat, lon;
293         unit2latlon(_center.unitx, _center.unity, lat, lon);
294         snprintf(buffer, sizeof(buffer), "%.06f", lat);
295         gtk_label_set_text(GTK_LABEL(txt_lat), buffer);
296         snprintf(buffer, sizeof(buffer), "%.06f", lon);
297         gtk_label_set_text(GTK_LABEL(txt_lon), buffer);
298 }
299
300 gtk_widget_show_all(dialog);
301
302 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
303         const gchar *text;
304         gchar *error_check;
305         gdouble lat, lon;
306         guint unitx, unity;
307
308         text = gtk_entry_get_text(GTK_ENTRY(txt_lat));
309         lat = strtof(text, &error_check);
310         if (text == error_check || lat < -90.f || lat > 90.f) {
311                 popup_error(dialog, _("Invalid Latitude"));
312                 continue;
313         }
314
315         text = gtk_entry_get_text(GTK_ENTRY(txt_lon));
316         lon = strtof(text, &error_check);
317         if (text == error_check || lon < -180.f || lon > 180.f) {
318                 popup_error(dialog, _("Invalid Longitude"));
319                 continue;
320         }
321
322         latlon2unit(lat, lon, unitx, unity);
323         if (_center_mode > 0)
324                 set_action_activate("autocenter_none", TRUE);
325
326         map_center_unit(unitx, unity);
327         break;
328 }
329 gtk_widget_destroy(dialog);
330 return TRUE;
331 }
332
333 gboolean 
334 menu_cb_goto_home(GtkAction *action)
335 {
336 if (map_goto_position(&_home)==FALSE) {
337         MACRO_BANNER_SHOW_INFO(_window, _("Home not set."));
338 } else {
339         MACRO_BANNER_SHOW_INFO(_window, _("At home location"));
340 }
341 return TRUE;
342 }
343
344 gboolean 
345 menu_cb_goto_destination(GtkAction *action)
346 {
347 if (map_goto_position(&_dest)==FALSE) {
348         MACRO_BANNER_SHOW_INFO(_window, _("Destination not set."));
349 } else {
350         MACRO_BANNER_SHOW_INFO(_window, _("At destination"));
351 }
352 return TRUE;
353 }
354
355 gboolean 
356 menu_cb_goto_gps(GtkAction *action)
357 {
358 _center_mode = CENTER_LATLON;
359 map_center_unit(_pos.unitx, _pos.unity);
360 map_update_location_from_center();
361
362 return TRUE;
363 }
364
365 gboolean 
366 menu_cb_goto_nextway(GtkAction * action)
367 {
368 if (_next_way && _next_way->point->unity) {
369         if (_center_mode > 0)
370                 set_action_activate("autocenter_none", TRUE);
371
372         map_center_unit(_next_way->point->unitx, _next_way->point->unity);
373 } else {
374         MACRO_BANNER_SHOW_INFO(_window, _("There is no next waypoint."));
375 }
376
377 return TRUE;
378 }
379
380 gboolean 
381 menu_cb_goto_nearpoi(GtkAction * action)
382 {
383 gdouble lat, lon;
384 poi_info *p;
385
386 if (_center_mode > 0) {
387         /* Auto-Center is enabled - use the GPS position. */
388         unit2latlon(_pos.unitx, _pos.unity, lat, lon);
389 } else {
390         /* Auto-Center is disabled - use the view center. */
391         unit2latlon(_center.unitx, _center.unity, lat, lon);
392 }
393
394 p=poi_find_nearest(lat, lon);
395
396 if (p) {
397         guint unitx, unity;
398         gchar *banner;
399
400         latlon2unit(p->lat, p->lon, unitx, unity);
401         banner = g_strdup_printf("%s (%s)", p->label, p->cat_label);
402         g_printf("%s\n", banner);
403         MACRO_BANNER_SHOW_INFO(_window, banner);
404         g_free(banner);
405         poi_free(p);
406
407         if (_center_mode > 0)
408                 set_action_activate("autocenter_none", TRUE);
409
410         map_center_unit(unitx, unity);
411         map_update_location_from_center();
412 } else {
413         MACRO_BANNER_SHOW_INFO(_window, _("No POIs found."));
414 }
415
416 return TRUE;
417 }
418
419 gboolean 
420 menu_cb_maps_repoman(GtkAction * action)
421 {
422 repoman_dialog();
423 return TRUE;
424 }
425
426 gboolean 
427 menu_cb_maps_select(GtkAction * action, gpointer new_repo)
428 {
429 repo_set_curr(new_repo);
430 map_force_redraw();
431 return TRUE;
432 }
433
434 gboolean 
435 cb_zoom_auto(GtkAction * action)
436 {
437 map_set_autozoom(TRUE);
438 return TRUE;
439 }
440
441 gboolean 
442 cb_zoom_base(GtkAction * action)
443 {
444 map_set_autozoom(FALSE);
445 map_set_zoom(3);
446 return TRUE;
447 }
448
449 gboolean 
450 cb_zoomin(GtkAction * action)
451 {
452 map_set_autozoom(FALSE);
453 map_zoom(-1);
454 return TRUE;
455 }
456
457 gboolean 
458 cb_zoomout(GtkAction * action)
459 {
460 map_set_autozoom(FALSE);
461 map_zoom(1);
462 return TRUE;
463 }
464
465 gboolean 
466 cb_fullscreen(GtkAction * action)
467 {
468 if ((_fullscreen = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)))) {
469         gtk_window_fullscreen(GTK_WINDOW(_window));
470 } else {
471         gtk_window_unfullscreen(GTK_WINDOW(_window));
472 }
473 gtk_idle_add((GSourceFunc) window_present, NULL);
474 return TRUE;
475 }
476
477 gboolean 
478 menu_cb_enable_gps(GtkAction * action)
479 {
480 if ((_enable_gps = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)))) {
481         if (_rcvr_mac) {
482                 set_conn_state(RCVR_DOWN);
483                 rcvr_connect_now();
484         } else {
485                 popup_error(_window, _("Cannot enable GPS until a GPS Receiver MAC is set in the Settings dialog box."));
486                 set_action_activate("gps_enable", FALSE);
487         }
488 } else {
489         if (_conn_state > RCVR_OFF)
490                 set_conn_state(RCVR_OFF);
491         rcvr_disconnect();
492         track_add(0, FALSE);
493         _speed_excess=FALSE;
494 }
495 set_action_sensitive("gps_details", _enable_gps);
496 set_action_sensitive("goto_gps", _enable_gps);
497 set_action_sensitive("autocenter_latlon", _enable_gps);
498 set_action_sensitive("autocenter_lead", _enable_gps);
499
500 map_move_mark();
501 gps_show_info();
502
503 return TRUE;
504 }
505
506 gboolean 
507 menu_cb_auto_download(GtkAction * action)
508 {
509 if ((_auto_download = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) )) {
510         if (_curr_repo->url == REPOTYPE_NONE)
511                 popup_error(_window, _("NOTE: You must set a Map URI in the current repository in order to download maps."));
512         map_force_redraw();
513 }
514
515 return TRUE;
516 }
517
518 gboolean 
519 menu_cb_gps_details(GtkAction * action)
520 {
521 gps_details();
522 return TRUE;
523 }
524
525 gboolean 
526 menu_cb_settings(GtkAction * action)
527 {
528 if (settings_dialog()) {
529         /* Settings have changed - reconnect to receiver. */
530         if (_enable_gps) {
531                 set_conn_state(RCVR_DOWN);
532                 rcvr_disconnect();
533                 rcvr_connect_now();
534         }
535 }
536 MACRO_RECALC_FOCUS_BASE();
537 MACRO_RECALC_FOCUS_SIZE();
538 map_force_redraw();
539 return TRUE;
540 }
541
542 gboolean 
543 menu_cb_help(GtkAction * action)
544 {
545 help_topic_display(HELP_ID_INTRO, 0);
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 (poi_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 }