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