Contents of /trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (hide annotations)
Tue Aug 4 11:18:54 2009 UTC (14 years, 9 months ago) by harbaum
File MIME type: text/plain
File size: 67685 byte(s)
Prevent doubly detected list clicks
1 harbaum 1 /*
2     * This file is part of GPXView.
3     *
4     * GPXView is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     *
9     * GPXView is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
16     */
17    
18     #include <stdio.h>
19     #include <string.h>
20     #include <math.h>
21    
22     #include <time.h>
23     #include <sys/time.h>
24    
25     #include "gpxview.h"
26     #include "custom_size_renderer.h"
27     #include "custom_rating_renderer.h"
28     #include "custom_type_renderer.h"
29    
30     #ifdef USE_MAEMO
31     #include <hildon/hildon-banner.h>
32     #endif
33    
34     #include <locale.h>
35    
36     extern char *strcasestr (__const char *__haystack, __const char *__needle);
37    
38     #ifdef USE_BREAD_CRUMB_TRAIL
39     static void crumb_add(appdata_t *appdata, char *name, int level,
40     gpointer user_data);
41    
42     enum {
43     CRUMB_GPXLIST = 0,
44     CRUMB_CACHELIST,
45     CRUMB_SEARCH_GLOBAL,
46     CRUMB_SEARCH_GPX,
47     CRUMB_CACHE,
48     };
49     #endif
50    
51     /* size of first buffer malloc; start small to exercise grow routines */
52     #define FIRSTSIZE 1
53    
54     void errorf(const char *fmt, ...) {
55     va_list args;
56     char *buf = NULL;
57     size_t bufsize;
58     char *newbuf;
59     size_t nextsize = 0;
60     int outsize;
61    
62     bufsize = 0;
63     for (;;) {
64     if (bufsize == 0) {
65     if ((buf = (char *)malloc(FIRSTSIZE)) == NULL)
66     return;
67    
68     bufsize = 1;
69     } else if ((newbuf = (char *)realloc(buf, nextsize)) != NULL) {
70     buf = newbuf;
71     bufsize = nextsize;
72     } else {
73     free(buf);
74     return;
75     }
76    
77     va_start(args, fmt);
78     outsize = vsnprintf(buf, bufsize, fmt, args);
79     va_end(args);
80    
81     if (outsize == -1) {
82     nextsize = bufsize * 2;
83     } else if (outsize == bufsize) {
84     nextsize = bufsize * 2;
85     } else if (outsize > bufsize) {
86     nextsize = outsize + 2; // Linux!
87     } else if (outsize == bufsize - 1) {
88     nextsize = bufsize * 2;
89     } else {
90     /* Output was not truncated */
91     break;
92     }
93     }
94    
95     GtkWidget *dialog = gtk_message_dialog_new(
96     GTK_WINDOW(NULL),
97     GTK_DIALOG_DESTROY_WITH_PARENT,
98     GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
99     buf);
100    
101     gtk_window_set_title(GTK_WINDOW(dialog), _("ERROR"));
102    
103     gtk_dialog_run(GTK_DIALOG(dialog));
104     gtk_widget_destroy(dialog);
105    
106     free(buf);
107     }
108    
109     gpx_t *choose_file(appdata_t *appdata, gboolean whole_dir) {
110     GtkWidget *dialog;
111     gpx_t *gpx = NULL;
112    
113     #ifdef USE_MAEMO
114     dialog = hildon_file_chooser_dialog_new(GTK_WINDOW(appdata->window),
115     whole_dir?GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER :
116     GTK_FILE_CHOOSER_ACTION_OPEN);
117    
118     #ifdef HILDON_HELP
119     hildon_help_dialog_help_enable(GTK_DIALOG(dialog),
120     HELP_ID_IMPORT, appdata->osso_context);
121     #endif
122     #else
123 harbaum 11 dialog = gtk_file_chooser_dialog_new (whole_dir?_("Import directory"):
124     _("Import file"),
125 harbaum 1 GTK_WINDOW(appdata->window),
126     whole_dir?GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER :
127     GTK_FILE_CHOOSER_ACTION_OPEN,
128     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
129     GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
130     NULL);
131     #endif
132    
133     /* use path if one is present */
134     if(appdata->path)
135     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
136     appdata->path);
137    
138     gtk_widget_show_all (GTK_WIDGET(dialog));
139     if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_FM_OK) {
140     char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
141    
142     gpx_dialog_t *dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
143    
144     if(!whole_dir)
145     gpx = gpx_parse(dialog, filename);
146     else {
147     /* cur trailing '/' if present */
148     if(strlastchr(filename) == '/')
149     filename[strlen(filename)] = 0;
150    
151     gpx = gpx_parse_dir(dialog, filename);
152     }
153    
154     gpx_busy_dialog_destroy(dialog);
155    
156     /* save path if gpx was successfully loaded */
157     if(gpx) {
158     char *r = strrchr(filename, '/');
159    
160     /* there is a delimiter, use everything left of it as path */
161     if(r && !whole_dir) {
162     *r = 0;
163     if(appdata->path) free(appdata->path);
164     appdata->path = strdup(filename);
165     /* restore path ... just in case ... */
166     *r = '/';
167     }
168    
169     if(whole_dir)
170     appdata->path = strdup(filename);
171     }
172    
173     g_free (filename);
174     }
175    
176     gtk_widget_destroy (dialog);
177    
178     return gpx;
179     }
180    
181     /******************** begin of cachelist ********************/
182    
183     enum {
184     CACHELIST_COL_TYPE = 0,
185     CACHELIST_COL_ID,
186     CACHELIST_COL_NAME,
187     CACHELIST_COL_SIZE,
188     CACHELIST_COL_RATING,
189     CACHELIST_COL_BEARING,
190     CACHELIST_COL_DISTANCE,
191     CACHELIST_COL_DATA,
192     CACHELIST_COL_AVAIL,
193     CACHELIST_COL_ARCHIVE,
194     CACHELIST_NUM_COLS
195     } ;
196    
197     void cachelist_view_onRowActivated(GtkTreeView *treeview,
198     GtkTreePath *path,
199     GtkTreeViewColumn *col,
200     gpointer userdata) {
201     appdata_t *appdata = (appdata_t*)userdata;
202     GtkTreeIter iter;
203     GtkTreeModel *model = gtk_tree_view_get_model(treeview);
204    
205 harbaum 43 /* check if a cache is already selected and ignore click if yes */
206     /* (was probably a double click) */
207     if(appdata->cur_cache) return;
208    
209 harbaum 1 if(gtk_tree_model_get_iter(model, &iter, path)) {
210     cache_t *cache;
211     gtk_tree_model_get(model, &iter, CACHELIST_COL_DATA, &cache, -1);
212     #ifndef USE_BREAD_CRUMB_TRAIL
213     cache_dialog(appdata, cache);
214     #else
215     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
216     appdata->cur_view = cache_view(appdata, cache);
217     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
218     gtk_widget_show_all(appdata->vbox);
219    
220     crumb_add(appdata, cache->name, CRUMB_CACHE, cache);
221     #endif
222     }
223     }
224    
225     typedef struct {
226     appdata_t *appdata;
227     GtkTreePath *path;
228     gboolean done;
229     } cachelist_expose_t;
230    
231     static gboolean cachelist_expose(GtkWidget *widget, GdkEventExpose *event,
232     gpointer user_data) {
233     cachelist_expose_t *ce = (cachelist_expose_t*)user_data;
234    
235     if(event->type == GDK_EXPOSE) {
236     if(ce->path && !ce->done) {
237     gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(widget),
238     ce->path, NULL, TRUE, 0.5, 0.5);
239     gtk_tree_path_free(ce->path);
240     ce->done = TRUE;
241     }
242     }
243    
244     return FALSE;
245     }
246    
247     static void cachelist_destroy(GtkWidget *widget, gpointer user_data) {
248     cachelist_expose_t *ce = (cachelist_expose_t*)user_data;
249    
250     printf("cachelist timer removed\n");
251     g_assert(ce->appdata->cachelist_handler_id);
252     gtk_timeout_remove(ce->appdata->cachelist_handler_id);
253     ce->appdata->cachelist_handler_id = 0;
254    
255     free(user_data);
256     }
257    
258     #define CACHELIST_UPDATE_TIMEOUT (30000)
259    
260     static GtkWidget *cachelist_create(appdata_t *appdata, gpx_t *gpx,
261     cache_t *sel_cache);
262    
263     void cachelist_redraw(appdata_t *appdata) {
264     #ifndef USE_MAEMO
265     // gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
266     // appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
267     // gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
268     // gtk_widget_show_all(appdata->vbox);
269     #else
270    
271 harbaum 11 if(!appdata->cur_view) {
272     printf("cachelist redraw: no active view\n");
273     return;
274     }
275    
276 harbaum 1 g_assert(!appdata->cur_cache);
277     int redraw = 0;
278     if(appdata->search_results)
279     redraw = 1;
280     else {
281     if(appdata->cur_gpx)
282     redraw = 2; // redraw cachelist
283     }
284    
285     if(redraw) {
286 harbaum 11 GtkWidget *container = appdata->vbox;
287    
288     printf("redraw %d\n", redraw);
289    
290     #ifdef USE_STACKABLE_WINDOW
291     HildonWindowStack *stack = hildon_window_stack_get_default();
292     container = hildon_window_stack_peek(stack);
293     #endif
294    
295     gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
296 harbaum 1 switch(redraw) {
297     case 1:
298     appdata->cur_view = cachelist_create(appdata,
299     appdata->search_results, NULL);
300     break;
301     case 2:
302     appdata->cur_view = cachelist_create(appdata,
303     appdata->cur_gpx, NULL);
304     break;
305     }
306    
307 harbaum 11 #ifdef USE_STACKABLE_WINDOW
308     if(container != appdata->vbox)
309     gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
310     else
311     #endif
312     gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
313    
314     gtk_widget_show_all(container);
315 harbaum 1 }
316     #endif
317     }
318    
319    
320     static gboolean cachelist_update(gpointer data) {
321    
322     printf("cachelist timer fired!\n");
323    
324     #ifdef USE_MAEMO
325     appdata_t *appdata = (appdata_t*)data;
326    
327 harbaum 11 if(appdata->cur_cache)
328     return TRUE;
329    
330 harbaum 1 if(appdata->cachelist_disable_screensaver)
331     if (osso_display_blanking_pause(appdata->osso_context) != OSSO_OK)
332     fprintf(stderr, "error with display blank\n");
333    
334     if(appdata->cachelist_update)
335     cachelist_redraw(appdata);
336     #endif
337    
338     return TRUE;
339     }
340    
341     static void cachelist_timer_reset(appdata_t *appdata) {
342 harbaum 11 printf("cachelist timer reset\n");
343 harbaum 1 g_assert(appdata->cachelist_handler_id);
344     gtk_timeout_remove(appdata->cachelist_handler_id);
345     appdata->cachelist_handler_id =
346     gtk_timeout_add(CACHELIST_UPDATE_TIMEOUT, cachelist_update, appdata);
347     }
348    
349     static gboolean cachelist_update_reset0(GtkWidget *widget,
350     GdkEventButton *event,
351     gpointer user_data) {
352     cachelist_timer_reset((appdata_t*)user_data);
353     return FALSE;
354     }
355    
356     static void cachelist_update_reset1(GtkAdjustment *adj,
357     gpointer user_data) {
358     cachelist_timer_reset((appdata_t*)user_data);
359     }
360    
361     static GtkWidget *cachelist_create(appdata_t *appdata, gpx_t *gpx,
362     cache_t *sel_cache) {
363     GtkCellRenderer *renderer;
364     GtkWidget *view;
365     GtkListStore *store;
366     GtkTreeIter iter;
367    
368     if(!gpx->notes_loaded) {
369     notes_load_all(appdata, gpx);
370     gpx->notes_loaded = TRUE;
371     }
372    
373     appdata->cur_items = appdata->cachelist_items;
374    
375     /* first sort the caches */
376     pos_t *refpos = get_pos(appdata);
377     gpx_sort(gpx, GPX_SORT_BY_DISTANCE, refpos);
378    
379     view = gtk_tree_view_new();
380    
381     /* --- "Type" column --- */
382     renderer = custom_cell_renderer_type_new();
383     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
384     -1, "Type", renderer, "type", CACHELIST_COL_TYPE, NULL);
385    
386     /* --- "Id" column --- */
387     if(appdata->cachelist_items & CACHELIST_ITEM_ID) {
388     renderer = gtk_cell_renderer_text_new();
389     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
390     -1, "Id", renderer, "text", CACHELIST_COL_ID, NULL);
391     }
392    
393     /* --- "Name" column --- */
394     renderer = gtk_cell_renderer_text_new();
395     g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL );
396    
397     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
398     "Name", renderer, "text", CACHELIST_COL_NAME, NULL);
399     gtk_tree_view_column_set_expand(column, TRUE);
400     gtk_tree_view_insert_column(GTK_TREE_VIEW(view), column, -1);
401    
402     g_object_set(renderer, "foreground", "#ff0000", NULL );
403     gtk_tree_view_column_add_attribute(column, renderer, "strikethrough",
404     CACHELIST_COL_AVAIL);
405     gtk_tree_view_column_add_attribute(column, renderer,
406     "foreground-set", CACHELIST_COL_ARCHIVE);
407    
408     /* --- "Size" column --- */
409     if(appdata->cachelist_items & CACHELIST_ITEM_SIZE) {
410     renderer = custom_cell_renderer_size_new();
411     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
412     -1, "Size", renderer, "size", CACHELIST_COL_SIZE, NULL);
413     }
414    
415     /* --- "Rating" column --- */
416     if(appdata->cachelist_items & CACHELIST_ITEM_RATING) {
417     renderer = custom_cell_renderer_rating_new();
418     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
419     -1, "Rating", renderer, "rating", CACHELIST_COL_RATING, NULL);
420     }
421    
422     /* --- "Bearing" column --- */
423     renderer = gtk_cell_renderer_pixbuf_new();
424     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
425     -1, "Bearing", renderer, "pixbuf", CACHELIST_COL_BEARING, NULL);
426    
427     /* --- "Distance" column --- */
428     renderer = gtk_cell_renderer_text_new();
429     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
430     -1, "Distance", renderer, "text", CACHELIST_COL_DISTANCE, NULL);
431    
432     store = gtk_list_store_new(CACHELIST_NUM_COLS, G_TYPE_INT,
433     G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT,
434     G_TYPE_INT, GDK_TYPE_PIXBUF, G_TYPE_STRING,
435     G_TYPE_POINTER, G_TYPE_BOOLEAN,
436     G_TYPE_BOOLEAN);
437    
438     GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
439    
440     GtkTreeIter sel_iter;
441     gboolean sel_iter_valid = FALSE;
442     GtkTreePath *path = NULL;
443     cache_t *cache = gpx->cache;
444     while(cache) {
445     char str[32];
446     gpx_pos_get_distance_str(str, sizeof(str),
447     *refpos, gpx_cache_pos(cache), appdata->imperial);
448    
449     int dint = (int)(cache->difficulty*2-2);
450     if(dint < 0) dint = 0;
451     if(dint > 8) dint = 8;
452    
453     int tint = (int)(cache->terrain*2-2);
454     if(tint < 0) tint = 0;
455     if(tint > 8) tint = 8;
456    
457     /* cache type includes "solved" flag in lowest bit */
458     int type = (cache->type<<8) +
459     (cache->notes?4:0) +
460     ((cache->notes && cache->notes->override)?1:0) +
461     ((cache->notes && cache->notes->found)?2:0);
462    
463     if((!(type & 2)) || !appdata->cachelist_hide_found) {
464    
465     /* Append a row and fill in some data */
466     gtk_list_store_append (store, &iter);
467    
468     gtk_list_store_set(store, &iter,
469     CACHELIST_COL_TYPE, type,
470     CACHELIST_COL_ID, cache->id,
471     CACHELIST_COL_NAME, cache->name,
472     CACHELIST_COL_SIZE, cache->container,
473     CACHELIST_COL_RATING, 100 * dint + tint,
474     CACHELIST_COL_BEARING,
475     icon_bearing(*refpos, gpx_cache_pos(cache)),
476     CACHELIST_COL_DISTANCE, str,
477     CACHELIST_COL_DATA, cache,
478     CACHELIST_COL_AVAIL, !cache->available ||
479     cache->archived,
480     CACHELIST_COL_ARCHIVE, cache->archived,
481     -1);
482    
483     if(cache == sel_cache) {
484     sel_iter = iter;
485     sel_iter_valid = TRUE;
486     }
487     }
488    
489     cache = cache->next;
490     }
491    
492     gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
493     g_object_unref(store);
494    
495     if(sel_iter_valid) {
496     gtk_tree_selection_select_iter(sel, &sel_iter);
497     path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel_iter);
498     }
499    
500     /* make list react on clicks */
501     g_signal_connect(view, "row-activated",
502     (GCallback)cachelist_view_onRowActivated, appdata);
503    
504     cachelist_expose_t *ce = malloc(sizeof(cachelist_expose_t));
505     ce->appdata = appdata;
506     ce->path = path;
507     ce->done = FALSE;
508    
509     g_signal_connect(view, "expose-event",
510     (GCallback)cachelist_expose, ce);
511     g_signal_connect(view, "destroy",
512     (GCallback)cachelist_destroy, ce);
513    
514     /* put this inside a scrolled view */
515     #ifndef USE_PANNABLE_AREA
516     GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
517     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
518     GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
519     gtk_container_add(GTK_CONTAINER(scrolled_window), view);
520     #else
521     GtkWidget *pannable_area = hildon_pannable_area_new();
522    
523     gtk_container_add(GTK_CONTAINER(pannable_area), view);
524     #endif
525    
526     /* add a timer for automatic update */
527     g_assert(!appdata->cachelist_handler_id);
528     appdata->cachelist_handler_id =
529     gtk_timeout_add(CACHELIST_UPDATE_TIMEOUT, cachelist_update, appdata);
530    
531     /* update timer is being reset if the user scrolls or selects */
532     g_signal_connect(view, "button-press-event",
533     (GCallback)cachelist_update_reset0, appdata);
534    
535     #ifndef USE_PANNABLE_AREA
536     g_signal_connect(gtk_scrolled_window_get_vadjustment(
537     GTK_SCROLLED_WINDOW(scrolled_window)),
538     "value-changed",
539     (GCallback)cachelist_update_reset1, appdata);
540    
541     return scrolled_window;
542     #else
543     g_signal_connect(hildon_pannable_area_get_vadjustment(
544     HILDON_PANNABLE_AREA(pannable_area)),
545     "value-changed",
546     (GCallback)cachelist_update_reset1, appdata);
547    
548    
549     return pannable_area;
550     #endif
551     }
552    
553     #ifndef USE_MAEMO
554     void cachelist_dialog(appdata_t *appdata, gpx_t *gpx) {
555     GtkWidget *dialog =
556     gtk_dialog_new_with_buttons(gpx->name, GTK_WINDOW(appdata->window),
557     GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_MODAL |
558     GTK_DIALOG_DESTROY_WITH_PARENT,
559     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
560     NULL);
561    
562     gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT);
563    
564     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),
565     cachelist_create(appdata, gpx, NULL));
566    
567     gtk_widget_show_all(dialog);
568    
569     gtk_dialog_run(GTK_DIALOG(dialog));
570     gtk_widget_destroy(dialog);
571     }
572     #else
573     #ifdef USE_STACKABLE_WINDOW
574 harbaum 11 static void search_result_free(gpx_t *result);
575    
576     void on_cachelist_destroy(GtkWidget *widget, appdata_t *appdata) {
577     if(appdata->search_results) {
578     search_result_free(appdata->search_results);
579     appdata->search_results = NULL;
580     }
581     appdata->cur_gpx = NULL;
582    
583     /* restore cur_view */
584     appdata->cur_view = g_object_get_data(G_OBJECT(widget), "cur_view");
585     }
586    
587 harbaum 1 void cachelist_dialog(appdata_t *appdata, gpx_t *gpx) {
588     GtkWidget *window = hildon_stackable_window_new();
589    
590 harbaum 11 /* store last "cur_view" in window */
591     g_object_set_data(G_OBJECT(window), "cur_view", appdata->cur_view);
592 harbaum 1
593 harbaum 11 appdata->cur_gpx = gpx;
594 harbaum 34 char *title = g_strdup_printf("%s - GPXView", gpx->name);
595 harbaum 11 gtk_window_set_title(GTK_WINDOW(window), title);
596     g_free(title);
597 harbaum 1
598 harbaum 11 appdata->cur_view = cachelist_create(appdata, gpx, NULL);
599     gtk_container_add(GTK_CONTAINER(window), appdata->cur_view);
600    
601    
602 harbaum 3 hildon_window_set_app_menu(HILDON_WINDOW(window),
603     menu_create(appdata, MENU_CACHELIST));
604    
605 harbaum 11 g_signal_connect(G_OBJECT(window), "destroy",
606     G_CALLBACK(on_cachelist_destroy), appdata);
607    
608 harbaum 1 gtk_widget_show_all(window);
609     }
610     #endif
611     #endif
612    
613     /******************** end of cachelist ********************/
614    
615     /******************** begin of gpxlist ********************/
616    
617     enum {
618     GPXLIST_COL_ICON = 0,
619     GPXLIST_COL_FILENAME,
620     GPXLIST_COL_NAME,
621     GPXLIST_COL_DATE,
622     GPXLIST_COL_CACHES,
623     GPXLIST_COL_OPEN,
624     #ifdef USE_PANNABLE_AREA
625     GPXLIST_COL_DELETE,
626     #endif
627     GPXLIST_COL_DATA,
628     GPXLIST_NUM_COLS
629     } ;
630    
631     static GdkPixbuf *gpx_icon_get(gpx_t *gpx) {
632     if(gpx->filename && g_file_test(gpx->filename, G_FILE_TEST_IS_DIR))
633     return icon_get(ICON_FILE, 1);
634    
635     if(gpx->filename&& !strcasecmp(gpx->filename+strlen(gpx->filename)-4,".zip"))
636     return icon_get(ICON_FILE, 2);
637    
638     return icon_get(ICON_FILE, 0);
639     }
640    
641     static void gpxlist_set(GtkListStore *store, GtkTreeIter *iter, gpx_t *gpx) {
642     char date_str[32], cnum[32];
643    
644     if(gpx->year && gpx->month && gpx->day) {
645     GDate *date = g_date_new_dmy(gpx->day, gpx->month, gpx->year);
646     g_date_strftime(date_str, sizeof(date_str), "%x", date);
647     g_date_free(date);
648     } else
649     strcpy(date_str, "---");
650    
651     char *fname = strrchr(gpx->filename, '/');
652     if(!fname) fname = gpx->filename;
653     else fname++; /* skip '/' */
654    
655     snprintf(cnum, sizeof(cnum), "%d", gpx_total_caches(gpx));
656    
657     /* Append a row and fill in some data */
658     gtk_list_store_set(store, iter,
659     GPXLIST_COL_ICON, gpx_icon_get(gpx),
660     GPXLIST_COL_FILENAME, fname,
661     GPXLIST_COL_NAME, gpx->name,
662     GPXLIST_COL_DATE, gpx->closed?NULL:date_str,
663     GPXLIST_COL_OPEN, !gpx->closed,
664     GPXLIST_COL_CACHES, gpx->closed?NULL:cnum,
665     #ifdef USE_PANNABLE_AREA
666     GPXLIST_COL_DELETE, icon_get(ICON_MISC, 7),
667     #endif
668     GPXLIST_COL_DATA, gpx,
669     -1);
670     }
671    
672     static void gpxlist_remove(appdata_t *appdata,
673     GtkListStore *store, GtkTreeIter *iter,
674     gpx_t *gpx) {
675    
676     printf("removing %s\n", gpx->name);
677    
678     /* de-chain */
679     gpx_t **prev = &appdata->gpx;
680     while(*prev != gpx) prev = &((*prev)->next);
681     *prev = gpx->next;
682    
683     /* remove gconf entry if file was closed */
684     gconf_remove_closed_name(appdata, gpx->filename);
685    
686     /* free gpx itself */
687     gpx_free(gpx);
688    
689     /* and remove from store */
690     gtk_list_store_remove(store, iter);
691     }
692    
693     static void gpxlist_close(appdata_t *appdata,
694     GtkListStore *store, GtkTreeIter *iter,
695     gpx_t *gpx) {
696    
697     printf("closing %s\n", gpx->name);
698    
699     g_assert(!gpx->closed);
700     gpx->closed = TRUE;
701    
702     /* free all associated caches */
703     gpx_free_caches(gpx);
704    
705     /* update entry */
706     gpxlist_set(store, iter, gpx);
707    
708     /* save name in gconf so we know this has been closed */
709     gconf_save_closed_name(appdata, gpx->filename, gpx->name);
710     }
711    
712     static void gpxlist_view_onRowActivated(GtkTreeView *treeview,
713     GtkTreePath *path,
714     GtkTreeViewColumn *col,
715     gpointer userdata) {
716     appdata_t *appdata = (appdata_t*)userdata;
717     GtkTreeIter iter;
718     GtkTreeModel *model = gtk_tree_view_get_model(treeview);
719    
720 harbaum 43 /* check if a cache is already selected and ignore click if yes */
721     /* (was probably a double click) */
722     if(appdata->cur_gpx) return;
723    
724 harbaum 1 if (gtk_tree_model_get_iter(model, &iter, path)) {
725     gpx_t *gpx;
726     gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
727    
728     #ifdef USE_PANNABLE_AREA
729     /* get name of column the user clicked on */
730     const char *col_name = NULL;
731     if(col) col_name = gtk_tree_view_column_get_title(col);
732    
733     if(col_name && !strcmp(col_name, "Del")) {
734     printf("clicked delete\n");
735    
736     /* ask user what he wants */
737     GtkWidget *dialog = gtk_message_dialog_new(
738     GTK_WINDOW(appdata->window),
739     GTK_DIALOG_DESTROY_WITH_PARENT,
740     GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL,
741     _("Do you want to close this entry only or do "
742     "you want to remove it completely from the list?"));
743    
744     gtk_dialog_add_buttons(GTK_DIALOG(dialog),
745     _("Remove"), 1,
746     _("Close"), 2,
747     NULL);
748    
749     if(gpx->closed)
750     gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), 2, FALSE);
751    
752     gtk_window_set_title(GTK_WINDOW(dialog), _("Remove entry?"));
753    
754     /* set the active flag again if the user answered "no" */
755     switch(gtk_dialog_run(GTK_DIALOG(dialog))) {
756    
757     case 1:
758     gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx);
759     break;
760    
761     case 2:
762     gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx);
763     break;
764    
765     default:
766     break;
767     }
768    
769     gtk_widget_destroy(dialog);
770    
771     } else
772     #endif
773     {
774    
775     /* this gpx file may be closed. Since the user definitely wants */
776     /* to use it, we just open it again */
777     if(gpx->closed) {
778     gpx_dialog_t *dialog =
779     gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
780     gpx_t *new = NULL;
781    
782     if(g_file_test(gpx->filename, G_FILE_TEST_IS_DIR))
783     new = gpx_parse_dir(dialog, gpx->filename);
784     else
785     new = gpx_parse(dialog, gpx->filename);
786    
787     if(new) {
788     gpx_t **prev = &(appdata->gpx);
789     while(*prev && *prev != gpx)
790     prev = &(*prev)->next;
791    
792     /* this entry _must_ be in the list */
793     g_assert(*prev);
794    
795     /* replace gpx entry with the new "open" one */
796     (*prev) = new;
797     new->next = gpx->next;
798     gpx->next = NULL;
799    
800     /* free old closed one */
801     gpx_free(gpx);
802    
803     gpx = new;
804    
805     /* finally update the visible list */
806     gpxlist_set(appdata->gpxstore, &iter, gpx);
807    
808     /* and remove gconf entry */
809     gconf_remove_closed_name(appdata, gpx->filename);
810    
811     #ifndef USE_PANNABLE_AREA
812     gtk_widget_set_sensitive(appdata->menu_close, TRUE);
813     #endif
814     } else {
815     printf("unable to reopen file %s\n", gpx->filename);
816     return;
817     }
818    
819     gpx_busy_dialog_destroy(dialog);
820     }
821     #ifndef USE_BREAD_CRUMB_TRAIL
822 harbaum 12 #ifdef USE_STACKABLE_WINDOW
823 harbaum 11 if(!appdata->cur_gpx)
824 harbaum 12 #endif
825 harbaum 11 cachelist_dialog(appdata, gpx);
826 harbaum 12 #ifdef USE_STACKABLE_WINDOW
827 harbaum 11 else
828     printf("selected gpx, but cachelist window already present\n");
829 harbaum 12 #endif
830 harbaum 1 #else
831     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
832     appdata->cur_view = cachelist_create(appdata, gpx, NULL);
833     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
834     gtk_widget_show_all(appdata->vbox);
835    
836     crumb_add(appdata, gpx->name, CRUMB_CACHELIST, gpx);
837     #endif
838     }
839     }
840     }
841    
842     #ifndef USE_PANNABLE_AREA
843     static gboolean
844     view_selection_func(GtkTreeSelection *selection, GtkTreeModel *model,
845     GtkTreePath *path, gboolean path_currently_selected,
846     gpointer userdata) {
847     appdata_t *appdata = (appdata_t*)userdata;
848     GtkTreeIter iter;
849    
850     if(gtk_tree_model_get_iter(model, &iter, path)) {
851     gpx_t *gpx;
852     gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
853    
854     gtk_widget_set_sensitive(appdata->menu_remove, !path_currently_selected);
855    
856     if(!gpx->closed)
857     gtk_widget_set_sensitive(appdata->menu_close, !path_currently_selected);
858     }
859    
860     return TRUE; /* allow selection state to change */
861     }
862     #endif
863    
864     static GtkWidget *gpxlist_create_view_and_model(appdata_t *appdata,
865     gpx_t *sel_gpx) {
866     gpx_t *gpx = appdata->gpx;
867     GtkCellRenderer *renderer;
868    
869     /* saved displayed items */
870     appdata->cur_items = appdata->gpxlist_items;
871    
872     #ifndef USE_PANNABLE_AREA
873     /* nothing selected yet */
874     gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
875     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
876     #endif
877    
878     appdata->gpxview = gtk_tree_view_new ();
879    
880     GtkTreeSelection *selection =
881     gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview));
882     #ifndef USE_PANNABLE_AREA
883     gtk_tree_selection_set_select_function(selection, view_selection_func,
884     appdata, NULL);
885     #endif
886    
887     /* --- "Icon" column --- */
888     renderer = gtk_cell_renderer_pixbuf_new();
889     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview),
890     -1, "Icon", renderer,
891     "pixbuf", GPXLIST_COL_ICON,
892     // "sensitive", GPXLIST_COL_OPEN,
893     NULL);
894    
895     /* --- "FileName" column --- */
896     if(appdata->gpxlist_items & GPXLIST_ITEM_FILENAME) {
897     renderer = gtk_cell_renderer_text_new();
898     gtk_tree_view_insert_column_with_attributes(
899     GTK_TREE_VIEW(appdata->gpxview),
900     -1, "Filename", renderer,
901     "text", GPXLIST_COL_FILENAME,
902     "sensitive", GPXLIST_COL_OPEN,
903     NULL);
904     }
905    
906     /* --- "Name" column --- */
907     renderer = gtk_cell_renderer_text_new();
908     g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL );
909    
910     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
911     "Name", renderer,
912     "text", GPXLIST_COL_NAME,
913     "sensitive", GPXLIST_COL_OPEN,
914     NULL);
915     gtk_tree_view_column_set_expand(column, TRUE);
916     gtk_tree_view_insert_column(GTK_TREE_VIEW(appdata->gpxview), column, -1);
917    
918     /* --- "Date" column --- */
919     if(appdata->gpxlist_items & GPXLIST_ITEM_DATE) {
920     renderer = gtk_cell_renderer_text_new();
921     g_object_set(renderer, "xalign", 1.0, NULL );
922     gtk_tree_view_insert_column_with_attributes(
923     GTK_TREE_VIEW(appdata->gpxview),
924     -1, "Date", renderer,
925     "text", GPXLIST_COL_DATE,
926     "sensitive", GPXLIST_COL_OPEN,
927     NULL);
928     }
929    
930     /* --- "Number of caches" column --- */
931     if(appdata->gpxlist_items & GPXLIST_ITEM_CNUM) {
932     renderer = gtk_cell_renderer_text_new();
933     g_object_set(renderer, "xalign", 1.0, NULL );
934     gtk_tree_view_insert_column_with_attributes(
935     GTK_TREE_VIEW(appdata->gpxview),
936     -1, "#Caches", renderer,
937     "text", GPXLIST_COL_CACHES,
938     "sensitive", GPXLIST_COL_OPEN,
939     NULL);
940     }
941    
942     #ifdef USE_PANNABLE_AREA
943     /* --- "Delete" column --- */
944     renderer = gtk_cell_renderer_pixbuf_new();
945     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(appdata->gpxview),
946     -1, "Del", renderer,
947     "pixbuf", GPXLIST_COL_DELETE,
948     "sensitive", GPXLIST_COL_OPEN,
949     NULL);
950     #endif
951    
952     /* build and fill the store */
953     appdata->gpxstore = gtk_list_store_new(GPXLIST_NUM_COLS, GDK_TYPE_PIXBUF,
954     G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
955     G_TYPE_STRING, G_TYPE_BOOLEAN,
956     #ifdef USE_PANNABLE_AREA
957     GDK_TYPE_PIXBUF,
958     #endif
959     G_TYPE_POINTER);
960    
961     GtkTreePath *path = NULL;
962     GtkTreeIter sel_iter;
963     gboolean sel_iter_valid = FALSE;
964     while(gpx) {
965     GtkTreeIter iter;
966     gtk_list_store_append(appdata->gpxstore, &iter);
967     gpxlist_set(appdata->gpxstore, &iter, gpx);
968    
969     if(gpx == sel_gpx) {
970     sel_iter = iter;
971     sel_iter_valid = TRUE;
972     }
973    
974     gpx = gpx->next;
975     }
976    
977     gtk_tree_view_set_model(GTK_TREE_VIEW(appdata->gpxview),
978     GTK_TREE_MODEL(appdata->gpxstore));
979    
980     g_object_unref(appdata->gpxstore);
981    
982     if(sel_iter_valid) {
983     gtk_tree_selection_select_iter(selection, &sel_iter);
984     path = gtk_tree_model_get_path(GTK_TREE_MODEL(appdata->gpxstore),
985     &sel_iter);
986     gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(appdata->gpxview),
987     path, NULL, TRUE, 0.0, 0.0);
988     gtk_tree_path_free(path);
989     }
990    
991     /* make list react on clicks */
992     g_signal_connect(appdata->gpxview, "row-activated",
993     (GCallback)gpxlist_view_onRowActivated, appdata);
994    
995     /* put this inside a scrolled view */
996     #ifndef USE_PANNABLE_AREA
997     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
998     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
999     GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1000     gtk_container_add(GTK_CONTAINER(scrolled_window), appdata->gpxview);
1001    
1002     return scrolled_window;
1003     #else
1004     GtkWidget *pannable_area = hildon_pannable_area_new();
1005     gtk_container_add(GTK_CONTAINER(pannable_area), appdata->gpxview);
1006    
1007     return pannable_area;
1008     #endif
1009     }
1010    
1011     /* add last entry in gpx list to visual representation */
1012     static void gpxlist_add(appdata_t *appdata, gpx_t *new) {
1013     GtkTreeIter iter;
1014    
1015     gtk_list_store_append(appdata->gpxstore, &iter);
1016     gpxlist_set(appdata->gpxstore, &iter, new);
1017    
1018     /* and attach entry to end of list */
1019     gpx_t **gpx = &appdata->gpx;
1020     while(*gpx) gpx = &((*gpx)->next);
1021     *gpx = new;
1022     }
1023    
1024     /******************** end of gpxlist ********************/
1025    
1026     /******************** begin of menu *********************/
1027    
1028     static void
1029     cb_menu_about(GtkWidget *window, gpointer data) {
1030     GtkAboutDialog *about = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
1031    
1032     gtk_about_dialog_set_name(about, "GPXView");
1033     gtk_about_dialog_set_version(about, VERSION);
1034    
1035 harbaum 14 #ifdef ENABLE_LIBLOCATION
1036     char *uses = "uses liblocation";
1037     #elif defined(ENABLE_GPSBT)
1038     char *uses = "uses gpsbt and gpsd";
1039     #else
1040     char *uses = "uses gpsd";
1041     #endif
1042    
1043 harbaum 15 char *str = g_strdup_printf("%s\n\n(%s)",
1044 harbaum 19 _("GPXView (c) 2008-2009 by\n"
1045     "Till Harbaum <till@harbaum.org>\n"
1046 harbaum 20 "Mailing list: gpxview-users@garage.maemo.org"),
1047 harbaum 19 _(uses)
1048     );
1049 harbaum 14
1050     gtk_about_dialog_set_copyright(about, str);
1051     g_free(str);
1052    
1053 harbaum 1 gtk_about_dialog_set_website(about,
1054     _("http://www.harbaum.org/till/maemo"));
1055    
1056     gtk_about_dialog_set_comments(about,
1057     _("Geocaching.com GPX file viewer"));
1058    
1059     gtk_widget_show_all(GTK_WIDGET(about));
1060     gtk_dialog_run(GTK_DIALOG(about));
1061     gtk_widget_destroy(GTK_WIDGET(about));
1062     }
1063    
1064     #if defined(USE_MAEMO) && defined(HILDON_HELP)
1065     static void
1066     cb_menu_help(GtkWidget *window, gpointer data) {
1067     appdata_t *appdata = (appdata_t*)data;
1068    
1069     hildon_help_show(appdata->osso_context, HELP_ID_INTRO, 0);
1070     }
1071     #endif
1072    
1073     static void
1074     cb_menu_add(GtkWidget *window, gpointer data) {
1075     appdata_t *appdata = (appdata_t *)data;
1076    
1077     gpx_t *new = choose_file(appdata, FALSE);
1078     if(new) gpxlist_add(appdata, new);
1079     }
1080    
1081     static void
1082     cb_menu_adddir(GtkWidget *window, gpointer data) {
1083     appdata_t *appdata = (appdata_t *)data;
1084    
1085     gpx_t *new = choose_file(appdata, TRUE);
1086     if(new) gpxlist_add(appdata, new);
1087     }
1088    
1089     #ifndef USE_PANNABLE_AREA
1090     static void
1091     cb_menu_close(GtkWidget *window, gpointer data) {
1092     appdata_t *appdata = (appdata_t *)data;
1093     GtkTreeSelection *selection;
1094     GtkTreeModel *model;
1095     GtkTreeIter iter;
1096    
1097     printf("selected close\n");
1098    
1099     /* the entry cannot be closed again */
1100     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
1101    
1102     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview));
1103    
1104     printf("gpxlist close\n");
1105    
1106     if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
1107     gpx_t *gpx = NULL;
1108     gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
1109    
1110     if(gpx) gpxlist_close(appdata, GTK_LIST_STORE(model), &iter, gpx);
1111     } else {
1112     g_print ("no row selected.\n");
1113     }
1114     }
1115    
1116     static void
1117     cb_menu_remove(GtkWidget *window, gpointer data) {
1118     appdata_t *appdata = (appdata_t *)data;
1119    
1120     /* disable menu item */
1121     gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
1122     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
1123    
1124     GtkTreeModel *model;
1125     GtkTreeIter iter;
1126     GtkTreeSelection *selection =
1127     gtk_tree_view_get_selection(GTK_TREE_VIEW(appdata->gpxview));
1128    
1129     printf("gpxlist remove\n");
1130    
1131     if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
1132     gpx_t *gpx = NULL;
1133     gtk_tree_model_get(model, &iter, GPXLIST_COL_DATA, &gpx, -1);
1134    
1135     if(gpx) gpxlist_remove(appdata, GTK_LIST_STORE(model), &iter, gpx);
1136     } else {
1137     g_print ("no row selected.\n");
1138     }
1139     }
1140    
1141     #endif // !USE_PANNABLE_AREA
1142    
1143     static void search_result_free(gpx_t *result) {
1144     printf("freeing search results\n");
1145    
1146     /* free found chain */
1147     cache_t *cache = result->cache;
1148     while(cache) {
1149     cache_t *next = cache->next;
1150     free(cache);
1151     cache = next;
1152     }
1153     free(result->name);
1154     free(result);
1155     }
1156    
1157     #define MAX_HITS 50
1158    
1159     static time_t localize_time(time_t in) {
1160     time_t ret;
1161     char *tz;
1162     struct tm *tm = localtime(&in);
1163    
1164     tz = getenv("TZ");
1165     setenv("TZ", "", 1);
1166     tzset();
1167     ret = mktime(tm);
1168     if (tz)
1169     setenv("TZ", tz, 1);
1170     else
1171     unsetenv("TZ");
1172     tzset();
1173     return ret;
1174     }
1175    
1176     static int days_ago(time_t in) {
1177     int day_in = localize_time(in) / (60*60*24);
1178     int day_now = localize_time(time(NULL)) / (60*60*24);
1179    
1180     return day_now - day_in;
1181     }
1182    
1183     gpx_t *search_do(appdata_t *appdata, gpx_t *gpx, char *phrase,
1184     int what, gboolean local) {
1185     /* walk through all caches */
1186    
1187     int hits = 0;
1188     gpx_t *found = malloc(sizeof(gpx_t));
1189     memset(found, 0, sizeof(gpx_t));
1190     cache_t **cacheP = &(found->cache);
1191    
1192     if(what & SEARCH_FINDS) {
1193     time_t loc_now = localize_time(time(NULL));
1194     printf("now: %ld days since 1/1/1970, days hour is %ld\n",
1195     loc_now/(60*60*24), loc_now%(60*60*24)/(60*60));
1196     }
1197    
1198     while(gpx && hits < MAX_HITS) {
1199    
1200     /* we need all notes ... */
1201     if(what & SEARCH_FINDS) {
1202     notes_load_all(appdata, gpx);
1203     gpx->notes_loaded = TRUE;
1204     }
1205    
1206     cache_t *cache = gpx->cache;
1207    
1208     while(cache && hits < MAX_HITS) {
1209     gboolean hit = FALSE;
1210    
1211     if(what & SEARCH_FINDS) {
1212     if(cache->notes && cache->notes->found ) {
1213     int days = days_ago(cache->notes->ftime);
1214    
1215     if(cache->id)
1216     printf("find of %s is %d days ago\n", cache->id, days);
1217    
1218     if(days <= appdata->search_days)
1219     hit = 1;
1220     }
1221     } else if(cache->id && (what & SEARCH_ID) &&
1222     strcasestr(cache->id, phrase))
1223     hit = 1;
1224     else if(cache->name && (what & SEARCH_NAME) &&
1225     strcasestr(cache->name, phrase))
1226     hit = 1;
1227     else if(cache->short_description && (what & SEARCH_DESC) &&
1228     strcasestr(cache->short_description, phrase))
1229     hit = 1;
1230     else if(cache->long_description && (what & SEARCH_DESC) &&
1231     strcasestr(cache->long_description, phrase))
1232     hit = 1;
1233     else if(cache->owner && (what & SEARCH_OWNER) &&
1234     strcasestr(cache->owner, phrase))
1235     hit = 1;
1236    
1237     if(hit) {
1238     /* chain a copy of this cache structure into the found list */
1239     *cacheP = malloc(sizeof(cache_t));
1240     memcpy(*cacheP, cache, sizeof(cache_t));
1241     (*cacheP)->next = NULL;
1242     cacheP = &((*cacheP)->next);
1243     hits++;
1244     }
1245     cache = cache->next;
1246     }
1247    
1248     if(!local) gpx = gpx->next;
1249     else gpx = NULL; /* local search within one gpx only */
1250     }
1251    
1252     found->name = strdup(_("Search results"));
1253    
1254     return found;
1255     }
1256    
1257     typedef struct {
1258     appdata_t *appdata;
1259     GtkWidget *entry, *spinner;
1260     GtkWidget *in_id, *in_name, *in_desc, *in_owner, *in_finds;
1261     } search_context_t;
1262    
1263     static void callback_finds_toggled(GtkWidget *widget, gpointer data ) {
1264     search_context_t *context = (search_context_t*)data;
1265    
1266     gboolean in_finds = gtk_toggle_button_get_active(
1267     GTK_TOGGLE_BUTTON(context->in_finds));
1268    
1269     gtk_widget_set_sensitive(context->entry, !in_finds);
1270     gtk_widget_set_sensitive(context->in_id, !in_finds);
1271     gtk_widget_set_sensitive(context->in_name, !in_finds);
1272     gtk_widget_set_sensitive(context->in_desc, !in_finds);
1273     gtk_widget_set_sensitive(context->in_owner, !in_finds);
1274     gtk_widget_set_sensitive(context->spinner, in_finds);
1275     }
1276    
1277     static void
1278     cb_menu_search(GtkWidget *window, gpointer data) {
1279     appdata_t *appdata = (appdata_t *)data;
1280    
1281     search_context_t context;
1282     memset(&context, 0, sizeof(search_context_t));
1283     context.appdata = appdata;
1284    
1285     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Enter search phrase"),
1286     GTK_WINDOW(appdata->window), GTK_DIALOG_MODAL,
1287     GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1288     GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1289     NULL);
1290    
1291     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1292     gtk_label_new(_("Search in:")));
1293    
1294     GtkWidget *table = gtk_table_new(2, 2, TRUE);
1295     gtk_table_set_col_spacing(GTK_TABLE(table), 0, 8);
1296    
1297     context.in_id = gtk_check_button_new_with_label(_("Waypoint IDs"));
1298     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_id),
1299     appdata->search & SEARCH_ID);
1300     gtk_table_attach_defaults(GTK_TABLE(table), context.in_id, 0, 1, 0, 1);
1301    
1302     context.in_name = gtk_check_button_new_with_label(_("Names"));
1303     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_name),
1304     appdata->search & SEARCH_NAME);
1305     gtk_table_attach_defaults(GTK_TABLE(table), context.in_name, 1, 2, 0, 1);
1306    
1307     context.in_desc = gtk_check_button_new_with_label(_("Descriptions"));
1308     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_desc),
1309     appdata->search & SEARCH_DESC);
1310     gtk_table_attach_defaults(GTK_TABLE(table), context.in_desc, 0, 1, 1, 2);
1311    
1312     context.in_owner = gtk_check_button_new_with_label(_("Owner"));
1313     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_owner),
1314     appdata->search & SEARCH_OWNER);
1315     gtk_table_attach_defaults(GTK_TABLE(table), context.in_owner, 1, 2, 1, 2);
1316    
1317     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1318    
1319     /* -------------------------------------------------------------- */
1320    
1321     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1322     gtk_label_new(_("Search for:")));
1323     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1324     context.entry = gtk_entry_new());
1325     if(appdata->search_str)
1326     gtk_entry_set_text(GTK_ENTRY(context.entry), appdata->search_str);
1327    
1328     /* -------------------------------------------------------------- */
1329    
1330     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1331     gtk_hseparator_new());
1332    
1333     GtkWidget *hbox = gtk_hbox_new(FALSE, 5);
1334    
1335     context.in_finds = gtk_check_button_new_with_label(_("Search finds for"));
1336     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context.in_finds),
1337     appdata->search & SEARCH_FINDS);
1338     gtk_box_pack_start_defaults(GTK_BOX(hbox), context.in_finds);
1339     g_signal_connect(G_OBJECT(context.in_finds), "toggled",
1340     G_CALLBACK(callback_finds_toggled), &context);
1341    
1342     #ifndef USE_MAEMO
1343     GtkObject *adj = gtk_adjustment_new(appdata->search_days, 0, 99, 1, 10, 10);
1344     context.spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 1, 0);
1345     #else
1346     context.spinner = hildon_number_editor_new(0, 99);
1347     hildon_number_editor_set_value(HILDON_NUMBER_EDITOR(context.spinner),
1348     appdata->search_days);
1349     #endif
1350     gtk_box_pack_start_defaults(GTK_BOX(hbox), context.spinner);
1351    
1352     gtk_box_pack_start_defaults(GTK_BOX(hbox), gtk_label_new(_("days")));
1353    
1354     gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
1355    
1356     /* -------------------------------------------------------------- */
1357    
1358     gtk_widget_show_all(dialog);
1359     callback_finds_toggled(NULL, &context);
1360    
1361     if(GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
1362     char *p = strdup(gtk_entry_get_text(GTK_ENTRY(context.entry)));
1363    
1364     /* update saved search string */
1365     if(appdata->search_str) free(appdata->search_str);
1366     if(strlen(p) > 0)
1367     appdata->search_str = strdup(p);
1368    
1369     #ifndef USE_MAEMO
1370     appdata->search_days = gtk_spin_button_get_value_as_int(
1371     GTK_SPIN_BUTTON(context.spinner));
1372     #else
1373     appdata->search_days = hildon_number_editor_get_value(
1374     HILDON_NUMBER_EDITOR(context.spinner));
1375     #endif
1376    
1377     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_finds)))
1378     appdata->search |= SEARCH_FINDS;
1379     else
1380     appdata->search &= ~SEARCH_FINDS;
1381    
1382     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_id)))
1383     appdata->search |= SEARCH_ID;
1384     else
1385     appdata->search &= ~SEARCH_ID;
1386    
1387     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_name)))
1388     appdata->search |= SEARCH_NAME;
1389     else
1390     appdata->search &= ~SEARCH_NAME;
1391    
1392     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_desc)))
1393     appdata->search |= SEARCH_DESC;
1394     else
1395     appdata->search &= ~SEARCH_DESC;
1396    
1397     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context.in_owner)))
1398     appdata->search |= SEARCH_OWNER;
1399     else
1400     appdata->search &= ~SEARCH_OWNER;
1401    
1402     gtk_widget_destroy(dialog);
1403    
1404     /* don't search if we are asked to search for nothing */
1405     if(((appdata->search & (SEARCH_ID|SEARCH_NAME|SEARCH_DESC|SEARCH_OWNER)) &&
1406     strlen(p) > 0) || (appdata->search & SEARCH_FINDS)) {
1407    
1408     printf("Search for %s (flags = %x)...\n", p, appdata->search);
1409    
1410     #ifndef USE_BREAD_CRUMB_TRAIL
1411 harbaum 12 gpx_t *found =
1412 harbaum 11 search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1413 harbaum 1
1414     /* do search result dialog here ... */
1415 harbaum 12 cachelist_dialog(appdata, found);
1416 harbaum 11 #ifndef USE_STACKABLE_WINDOW
1417 harbaum 12 search_result_free(found);
1418     #else
1419     appdata->search_results = found;
1420 harbaum 11 #endif
1421 harbaum 1 #else
1422 harbaum 11 gpx_t *found = NULL;
1423    
1424 harbaum 1 if(appdata->cur_gpx)
1425     found = search_do(appdata, appdata->cur_gpx, p, appdata->search, TRUE);
1426     else
1427     found = search_do(appdata, appdata->gpx, p, appdata->search, FALSE);
1428    
1429     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
1430     appdata->cur_view = cachelist_create(appdata, found, NULL);
1431     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
1432     gtk_widget_show_all(appdata->vbox);
1433     crumb_add(appdata, found->name,
1434     appdata->cur_gpx?CRUMB_SEARCH_GPX:CRUMB_SEARCH_GLOBAL, found);
1435     #endif
1436     } else
1437     printf("No valid search: \"%s\" with flags %x!\n", p, appdata->search);
1438    
1439     free(p);
1440     } else
1441     gtk_widget_destroy(dialog);
1442     }
1443    
1444 harbaum 40 static void on_window_destroy (GtkWidget *widget, gpointer data);
1445 harbaum 1
1446     #ifndef USE_MAEMO
1447     static void
1448     cb_menu_quit(GtkWidget *window, gpointer data) {
1449     on_window_destroy(window, data);
1450     }
1451     #endif
1452    
1453 harbaum 2 #ifndef NO_COPY_N_PASTE
1454 harbaum 1 static void
1455     cb_menu_cut(GtkWidget *widget, gpointer data) {
1456     appdata_t *appdata = (appdata_t*)data;
1457    
1458     if(appdata->active_buffer) {
1459     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1460     gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1461     appdata->clipboard, TRUE);
1462     } else
1463     printf("cut: ERROR, not a text buffer\n");
1464     } else
1465     printf("cut: ERROR, no active buffer\n");
1466     }
1467    
1468     static void
1469     cb_menu_copy(GtkWidget *widget, gpointer data) {
1470     appdata_t *appdata = (appdata_t*)data;
1471    
1472     if(appdata->active_buffer) {
1473     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1474     gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1475     appdata->clipboard);
1476     } else if(GTK_WIDGET_TYPE(appdata->active_buffer) == gtk_html_get_type()) {
1477     printf("copy from html buffer\n");
1478     html_copy_to_clipboard(appdata);
1479     } else
1480     printf("copy: ERROR, not a text nor a html buffer\n");
1481     } else
1482     printf("copy: ERROR, no active buffer\n");
1483     }
1484    
1485     static void
1486     cb_menu_paste(GtkWidget *widget, gpointer data) {
1487     appdata_t *appdata = (appdata_t*)data;
1488    
1489     if(appdata->active_buffer) {
1490     if(GTK_WIDGET_TYPE(appdata->active_buffer) == GTK_TYPE_TEXT_BUFFER) {
1491     gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(appdata->active_buffer),
1492     appdata->clipboard, NULL, TRUE);
1493     } else
1494     printf("paste: ERROR, not a text buffer\n");
1495     } else
1496     printf("paste: ERROR, no active buffer\n");
1497     }
1498 harbaum 2 #endif
1499 harbaum 1
1500 harbaum 6 static void
1501     cb_menu_export_log(GtkWidget *widget, gpointer data) {
1502     appdata_t *appdata = (appdata_t*)data;
1503     notes_log_export(appdata);
1504     }
1505 harbaum 5
1506 harbaum 6 #ifdef USE_MAEMO
1507     static void
1508     cb_menu_export_mmpoi(GtkWidget *widget, gpointer data) {
1509     appdata_t *appdata = (appdata_t*)data;
1510     mmpoi_export(appdata);
1511     }
1512     #endif
1513 harbaum 5
1514 harbaum 6 static void
1515     cb_menu_export_garmin(GtkWidget *widget, gpointer data) {
1516     appdata_t *appdata = (appdata_t*)data;
1517     garmin_export(appdata);
1518 harbaum 1 }
1519    
1520 harbaum 32 #ifdef ENABLE_OSM_GPS_MAP
1521 harbaum 6 static void
1522 harbaum 32 cb_menu_map(GtkWidget *window, gpointer data) {
1523     map((appdata_t *)data);
1524     }
1525     #endif
1526    
1527     static void
1528 harbaum 6 cb_menu_geomath(GtkWidget *window, gpointer data) {
1529     geomath_dialog((appdata_t *)data);
1530     }
1531 harbaum 5
1532 harbaum 6 static void
1533     cb_menu_geotext(GtkWidget *window, gpointer data) {
1534     geotext_dialog((appdata_t *)data);
1535 harbaum 1 }
1536    
1537 harbaum 6 static void
1538     cb_menu_precpos(GtkWidget *window, gpointer data) {
1539     precise_position((appdata_t *)data);
1540 harbaum 1 }
1541    
1542 harbaum 6 #ifdef USE_STACKABLE_WINDOW
1543 harbaum 30 typedef struct {
1544     char *label, *desc;
1545     GtkSignalFunc activate_cb;
1546     } menu_entry_t;
1547 harbaum 6
1548 harbaum 30 typedef struct {
1549     const char *title;
1550     const menu_entry_t *menu;
1551     int len;
1552     } submenu_t;
1553 harbaum 3
1554 harbaum 30 #define COLUMNS 1
1555 harbaum 4
1556 harbaum 30 void on_submenu_entry_clicked(GtkButton *button, GtkWidget *menu) {
1557 harbaum 5
1558 harbaum 30 /* force closing of submenu dialog */
1559     gtk_dialog_response(GTK_DIALOG(menu), GTK_RESPONSE_NONE);
1560     gtk_widget_hide(menu);
1561    
1562     /* let gtk clean up */
1563     while(gtk_events_pending())
1564     gtk_main_iteration();
1565 harbaum 11 }
1566    
1567 harbaum 30 static GtkWidget *app_submenu_create(appdata_t *appdata,
1568     const submenu_t *submenu) {
1569 harbaum 5
1570 harbaum 30 /* create a oridinary dialog box */
1571     GtkWidget *dialog = gtk_dialog_new();
1572     gtk_window_set_title(GTK_WINDOW(dialog), _(submenu->title));
1573     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1574     gtk_window_set_transient_for(GTK_WINDOW(dialog),
1575     GTK_WINDOW(appdata->window));
1576     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
1577 harbaum 5
1578 harbaum 30 GtkWidget *table = gtk_table_new(submenu->len/COLUMNS, COLUMNS, TRUE);
1579     int x = 0, y = 0;
1580 harbaum 11
1581 harbaum 30 const menu_entry_t *menu_entries = submenu->menu;
1582     while(menu_entries->label) {
1583     GtkWidget *button = NULL;
1584 harbaum 21
1585 harbaum 30 button = hildon_button_new_with_text(
1586 harbaum 6 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
1587     HILDON_BUTTON_ARRANGEMENT_VERTICAL,
1588 harbaum 30 _(menu_entries->label), _(menu_entries->desc));
1589 harbaum 5
1590 harbaum 31 /* try to center both texts */
1591     hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1592     hildon_button_set_value_alignment(HILDON_BUTTON(button), 0.5, 0.5);
1593 harbaum 6
1594 harbaum 31 g_signal_connect(button, "clicked",
1595     G_CALLBACK(on_submenu_entry_clicked), dialog);
1596    
1597     g_signal_connect(button, "clicked",
1598     menu_entries->activate_cb, appdata);
1599    
1600 harbaum 30 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
1601    
1602     x++;
1603     if(x == COLUMNS) { x = 0; y++; }
1604 harbaum 3
1605 harbaum 30 menu_entries++;
1606     }
1607 harbaum 20
1608 harbaum 30 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), table);
1609    
1610     return dialog;
1611 harbaum 3 }
1612    
1613 harbaum 30 /* popup the dialog shaped submenu */
1614     static void submenu_popup(GtkWidget *menu) {
1615     gtk_widget_show_all(menu);
1616     gtk_dialog_run(GTK_DIALOG(menu));
1617     gtk_widget_hide(menu);
1618     }
1619    
1620     static void submenu_cleanup(GtkWidget *menu) {
1621     gtk_widget_destroy(menu);
1622     }
1623    
1624     static const menu_entry_t submenu_export_entries[] = {
1625     { "Export to Maemo Mapper" , "Save a Maemo Mapper POI file",
1626     G_CALLBACK(cb_menu_export_mmpoi) },
1627     { "Export Field Notes", "Save a Garmin Field Notes file",
1628     G_CALLBACK(cb_menu_export_log) },
1629     { "Export Garmin GPX", "Save modified waypoints in GPX file",
1630     G_CALLBACK(cb_menu_export_garmin) },
1631     { NULL, NULL, NULL }
1632     };
1633    
1634     static const submenu_t submenu_export = {
1635     "Export", submenu_export_entries,
1636     sizeof(submenu_export_entries)/sizeof(menu_entry_t)-1
1637     };
1638    
1639     /* the export submenu */
1640     void on_export_clicked(GtkButton *button, appdata_t *appdata) {
1641     if(!appdata->export_menu)
1642     appdata->export_menu = app_submenu_create(appdata, &submenu_export);
1643    
1644     submenu_popup(appdata->export_menu);
1645     }
1646    
1647     static const menu_entry_t submenu_tools_entries[] = {
1648 harbaum 32 #ifdef ENABLE_OSM_GPS_MAP
1649     { "Map", "Display an interactive world map",
1650     G_CALLBACK(cb_menu_map) },
1651     #endif
1652 harbaum 30 { "Geomath", "Geocoordinate calculation",
1653     G_CALLBACK(cb_menu_geomath) },
1654     { "Geotext", "Text analysis",
1655     G_CALLBACK(cb_menu_geotext) },
1656     { "Precise Position", "Calculate a precise GPS position",
1657     G_CALLBACK(cb_menu_precpos) },
1658     { NULL, NULL, NULL }
1659     };
1660    
1661     static const submenu_t submenu_tools = {
1662     "Tools", submenu_tools_entries,
1663     sizeof(submenu_tools_entries)/sizeof(menu_entry_t)-1
1664     };
1665    
1666 harbaum 20 /* the tools submenu */
1667     void on_tools_clicked(GtkButton *button, appdata_t *appdata) {
1668     if(!appdata->tools_menu)
1669 harbaum 30 appdata->tools_menu = app_submenu_create(appdata, &submenu_tools);
1670 harbaum 20
1671 harbaum 30 submenu_popup(appdata->tools_menu);
1672 harbaum 20 }
1673    
1674 harbaum 3 HildonAppMenu *menu_create(appdata_t *appdata, int mode) {
1675     GtkWidget *button;
1676     HildonAppMenu *menu = HILDON_APP_MENU(hildon_app_menu_new());
1677    
1678     /* ------- */
1679 harbaum 7 button = gtk_button_new_with_label(_("About"));
1680     g_signal_connect_after(button, "clicked",
1681     G_CALLBACK(cb_menu_about), appdata);
1682     hildon_app_menu_append(menu, GTK_BUTTON(button));
1683    
1684 harbaum 3 button = gtk_button_new_with_label(_("Settings"));
1685     g_signal_connect_after(button, "clicked", G_CALLBACK(cb_menu_settings),
1686     appdata);
1687     hildon_app_menu_append(menu, GTK_BUTTON(button));
1688    
1689     if(mode == MENU_GPXLIST) {
1690     button = gtk_button_new_with_label(_("Import file"));
1691     g_signal_connect_after(button, "clicked",
1692     G_CALLBACK(cb_menu_add), appdata);
1693     hildon_app_menu_append(menu, GTK_BUTTON(button));
1694    
1695 harbaum 7 button = gtk_button_new_with_label(_("Import directory"));
1696 harbaum 3 g_signal_connect_after(button, "clicked",
1697     G_CALLBACK(cb_menu_adddir), appdata);
1698     hildon_app_menu_append(menu, GTK_BUTTON(button));
1699     }
1700    
1701     if(mode == MENU_GPXLIST) {
1702 harbaum 4 button = gtk_button_new_with_label(_("Export"));
1703     g_signal_connect_after(button, "clicked",
1704     G_CALLBACK(on_export_clicked), appdata);
1705     hildon_app_menu_append(menu, GTK_BUTTON(button));
1706    
1707     button = gtk_button_new_with_label(_("Search"));
1708     g_signal_connect_after(button, "clicked",
1709     G_CALLBACK(cb_menu_search), appdata);
1710     hildon_app_menu_append(menu, GTK_BUTTON(button));
1711     }
1712    
1713 harbaum 5 button = gtk_button_new_with_label(_("Tools"));
1714     g_signal_connect_after(button, "clicked",
1715     G_CALLBACK(on_tools_clicked), appdata);
1716     hildon_app_menu_append(menu, GTK_BUTTON(button));
1717 harbaum 4
1718 harbaum 15 #ifdef HILDON_HELP
1719     button = gtk_button_new_with_label(_("Help"));
1720     g_signal_connect_after(button, "clicked",
1721     G_CALLBACK(cb_menu_help), appdata);
1722     hildon_app_menu_append(menu, GTK_BUTTON(button));
1723     #endif
1724 harbaum 3
1725 harbaum 15 gtk_widget_show_all(GTK_WIDGET(menu));
1726    
1727 harbaum 3 return menu;
1728     }
1729     #else
1730 harbaum 5
1731 harbaum 1 void menu_create(appdata_t *appdata) {
1732     GtkWidget *menu, *item;
1733     menu = gtk_menu_new();
1734    
1735 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1736 harbaum 1 appdata->menu_import =
1737     #endif
1738     item = gtk_menu_item_new_with_label(_("Import"));
1739     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1740     GtkWidget *submenu = gtk_menu_new();
1741     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1742    
1743 harbaum 7 item = gtk_menu_item_new_with_label( _("File") );
1744 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1745     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_add), appdata);
1746    
1747 harbaum 7 item = gtk_menu_item_new_with_label( _("Directory") );
1748 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1749     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_adddir), appdata);
1750    
1751     #ifndef USE_PANNABLE_AREA
1752     gtk_menu_append(GTK_MENU_SHELL(submenu), gtk_separator_menu_item_new());
1753    
1754     appdata->menu_close =
1755     item = gtk_menu_item_new_with_label( _("Close") );
1756     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1757     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_close), appdata);
1758    
1759     appdata->menu_remove =
1760     item = gtk_menu_item_new_with_label( _("Remove") );
1761     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1762     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_remove), appdata);
1763     #endif
1764    
1765 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1766 harbaum 1 appdata->menu_export =
1767     #endif
1768     item = gtk_menu_item_new_with_label(_("Export"));
1769     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1770     submenu = gtk_menu_new();
1771     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1772    
1773     #ifdef USE_MAEMO
1774 harbaum 7 item = gtk_menu_item_new_with_label( _("Maemo Mapper POI") );
1775 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1776     g_signal_connect(item, "activate",
1777     GTK_SIGNAL_FUNC(cb_menu_export_mmpoi), appdata);
1778     #endif
1779    
1780 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin Field Notes") );
1781 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1782     g_signal_connect(item, "activate",
1783     GTK_SIGNAL_FUNC(cb_menu_export_log), appdata);
1784    
1785 harbaum 7 item = gtk_menu_item_new_with_label( _("Garmin GPX") );
1786 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1787     g_signal_connect(item, "activate",
1788     GTK_SIGNAL_FUNC(cb_menu_export_garmin), appdata);
1789    
1790 harbaum 3 #ifdef USE_BREAD_CRUMB_TRAIL
1791 harbaum 1 appdata->menu_search =
1792     #endif
1793 harbaum 7 item = gtk_menu_item_new_with_label( _("Search") );
1794 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1795     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_search), appdata);
1796    
1797     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1798 harbaum 2
1799     #ifndef NO_COPY_N_PASTE
1800 harbaum 1 /* ----------- copy'n paste submenu ----------------- */
1801     appdata->clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1802     gtk_clipboard_set_can_store(appdata->clipboard, NULL, 0);
1803    
1804     item = gtk_menu_item_new_with_label(_("Edit"));
1805     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1806     submenu = gtk_menu_new();
1807     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1808    
1809     appdata->menu_cut = item = gtk_menu_item_new_with_label( _("Cut") );
1810     gtk_widget_set_sensitive(item, FALSE);
1811     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1812     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_cut), appdata);
1813     appdata->menu_copy = item = gtk_menu_item_new_with_label( _("Copy") );
1814     gtk_widget_set_sensitive(item, FALSE);
1815     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1816     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_copy), appdata);
1817     appdata->menu_paste = item = gtk_menu_item_new_with_label( _("Paste") );
1818     gtk_widget_set_sensitive(item, FALSE);
1819     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1820     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_paste), appdata);
1821 harbaum 2 #endif
1822 harbaum 1
1823 harbaum 3 item = gtk_menu_item_new_with_label( _("Settings") );
1824 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1825     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_settings),
1826     appdata);
1827    
1828     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1829    
1830     item = gtk_menu_item_new_with_label(_("Tools"));
1831     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1832     submenu = gtk_menu_new();
1833     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1834    
1835 harbaum 32 #ifdef ENABLE_OSM_GPS_MAP
1836     item = gtk_menu_item_new_with_label( _("Map") );
1837     gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1838     g_signal_connect(item, "activate",
1839     GTK_SIGNAL_FUNC(cb_menu_map), appdata);
1840     #endif
1841    
1842 harbaum 7 item = gtk_menu_item_new_with_label( _("Geomath") );
1843 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1844     g_signal_connect(item, "activate",
1845     GTK_SIGNAL_FUNC(cb_menu_geomath), appdata);
1846    
1847 harbaum 7 item = gtk_menu_item_new_with_label( _("Geotext") );
1848 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1849     g_signal_connect(item, "activate",
1850     GTK_SIGNAL_FUNC(cb_menu_geotext), appdata);
1851    
1852 harbaum 7 item = gtk_menu_item_new_with_label( _("Precise Position") );
1853 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(submenu), item);
1854     g_signal_connect(item, "activate",
1855     GTK_SIGNAL_FUNC(cb_menu_precpos), appdata);
1856    
1857     gtk_menu_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
1858    
1859     #if defined(USE_MAEMO) && defined(HILDON_HELP)
1860 harbaum 7 item = gtk_menu_item_new_with_label( _("Help") );
1861 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1862     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_help), appdata);
1863     #endif
1864    
1865 harbaum 7 item = gtk_menu_item_new_with_label( _("About") );
1866 harbaum 1 gtk_menu_append(GTK_MENU_SHELL(menu), item);
1867     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_about), appdata);
1868    
1869     #ifndef USE_MAEMO
1870     item = gtk_menu_item_new_with_label( _("Quit") );
1871     gtk_menu_append(GTK_MENU_SHELL(menu), item);
1872     g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_menu_quit), appdata);
1873     #endif
1874    
1875     #ifdef USE_MAEMO
1876     hildon_window_set_menu(appdata->window, GTK_MENU(menu));
1877     #else
1878     /* attach ordinary gtk menu */
1879     GtkWidget *menu_bar = gtk_menu_bar_new();
1880    
1881     GtkWidget *root_menu = gtk_menu_item_new_with_label (_("Menu"));
1882     gtk_widget_show(root_menu);
1883    
1884     gtk_menu_bar_append(menu_bar, root_menu);
1885     gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
1886    
1887     gtk_widget_show(menu_bar);
1888     gtk_box_pack_start(GTK_BOX(appdata->vbox), menu_bar, 0, 0, 0);
1889     #endif
1890     }
1891 harbaum 3 #endif
1892 harbaum 1
1893     /********************* end of menu **********************/
1894    
1895     void cleanup(appdata_t *appdata) {
1896     gpx_free_all(appdata->gpx);
1897     if(appdata->path) free(appdata->path);
1898     if(appdata->image_path) free(appdata->image_path);
1899     if(appdata->search_str) free(appdata->search_str);
1900    
1901 harbaum 30 #ifdef USE_STACKABLE_WINDOW
1902     if(appdata->export_menu) submenu_cleanup(appdata->export_menu);
1903     if(appdata->tools_menu) submenu_cleanup(appdata->tools_menu);
1904     #endif
1905    
1906 harbaum 1 gnome_vfs_shutdown();
1907     icons_free();
1908     gps_release(appdata);
1909    
1910     #ifdef USE_MAEMO
1911     if(appdata->search_results) {
1912     printf("freeing pending search\n");
1913     search_result_free(appdata->search_results);
1914     }
1915    
1916     if(appdata->osso_context)
1917     osso_deinitialize(appdata->osso_context);
1918    
1919     appdata->program = NULL;
1920     #endif
1921    
1922     /* free chain of locations */
1923     location_t *loc = appdata->location;
1924     while(loc) {
1925     location_t *next = loc->next;
1926     if(loc->name) free(loc->name);
1927     free(loc);
1928     loc = next;
1929     }
1930    
1931     puts("everything is gone");
1932     }
1933    
1934 harbaum 40 static void on_window_destroy (GtkWidget *widget, gpointer data) {
1935 harbaum 1 appdata_t *appdata = (appdata_t*)data;
1936    
1937     gconf_save_state(appdata);
1938     gtk_main_quit();
1939     appdata->window = NULL;
1940     }
1941    
1942     gboolean on_window_key_press(GtkWidget *widget,
1943 harbaum 11 GdkEventKey *event, appdata_t *appdata) {
1944 harbaum 1 int handled = FALSE;
1945    
1946     // printf("key event %d\n", event->keyval);
1947    
1948     switch(event->keyval) {
1949     #ifdef USE_MAEMO
1950    
1951     #ifdef HILDON_HARDKEY_INCREASE
1952     case HILDON_HARDKEY_INCREASE:
1953     html_zoom(appdata, TRUE);
1954     handled = TRUE;
1955     break;
1956     #endif
1957    
1958     #ifdef HILDON_HARDKEY_DECREASE
1959     case HILDON_HARDKEY_DECREASE:
1960     html_zoom(appdata, FALSE);
1961     handled = TRUE;
1962     break;
1963     #endif
1964    
1965     #ifdef HILDON_HARDKEY_FULLSCREEN
1966     case HILDON_HARDKEY_FULLSCREEN:
1967     {
1968     appdata->fullscreen = !appdata->fullscreen;
1969    
1970     if(appdata->fullscreen)
1971     gtk_window_fullscreen(GTK_WINDOW(appdata->window));
1972     else
1973     gtk_window_unfullscreen(GTK_WINDOW(appdata->window));
1974    
1975     handled = TRUE;
1976     }
1977     break;
1978     #endif
1979    
1980     #else
1981     case '+':
1982     printf("zoom+\n");
1983     html_zoom(appdata, TRUE);
1984     handled = TRUE;
1985     break;
1986     case '-':
1987     printf("zoom-\n");
1988     html_zoom(appdata, FALSE);
1989     handled = TRUE;
1990     break;
1991     #endif
1992     }
1993    
1994     return handled;
1995     }
1996    
1997     #ifdef USE_BREAD_CRUMB_TRAIL
1998     typedef struct {
1999     int level;
2000     appdata_t *appdata;
2001     gpointer data;
2002     } crumb_t;
2003    
2004     static void
2005     crumb_back(gpointer data) {
2006     crumb_t *crumb = (crumb_t*)data;
2007     printf("crumb_back called with %d\n", crumb->level);
2008    
2009     /* don't do anything if main window has already been destroyed */
2010     if(!crumb->appdata->window) {
2011     printf("Main window gone ...\n");
2012     return;
2013     }
2014    
2015     /* whatever is being displayed: we don't need it anymore */
2016     gtk_container_remove(GTK_CONTAINER(crumb->appdata->vbox),
2017     crumb->appdata->cur_view);
2018    
2019     /* back from cache to cachelist */
2020     if(crumb->level == CRUMB_CACHE) {
2021     gpx_t *gpx = crumb->appdata->search_results;
2022    
2023     if(!gpx) {
2024     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2025     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2026     printf("no search data found, return to gpx\n");
2027     gpx = crumb->appdata->cur_gpx;
2028     } else
2029     printf("returning to search result\n");
2030    
2031     g_assert(gpx != NULL);
2032    
2033     crumb->appdata->cur_view = cachelist_create(crumb->appdata, gpx,
2034     crumb->appdata->cur_cache);
2035    
2036     /* returning from cache view: invalidate cache reference */
2037     crumb->appdata->cur_cache = NULL;
2038    
2039     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2040     crumb->appdata->cur_view);
2041     }
2042    
2043     if(crumb->level == CRUMB_SEARCH_GPX) {
2044     printf("returning from a local search!\n");
2045    
2046     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2047    
2048     search_result_free((gpx_t*)crumb->data);
2049     crumb->appdata->search_results = NULL;
2050    
2051     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2052    
2053     crumb->appdata->cur_view = cachelist_create(crumb->appdata,
2054     crumb->appdata->cur_gpx, NULL);
2055     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2056     crumb->appdata->cur_view);
2057     }
2058    
2059     /* back from cachelist to gpxlist */
2060     if((crumb->level == CRUMB_CACHELIST) ||
2061     (crumb->level == CRUMB_SEARCH_GLOBAL)) {
2062    
2063     crumb->appdata->cur_view = gpxlist_create_view_and_model(
2064     crumb->appdata, crumb->appdata->cur_gpx);
2065    
2066     /* returning from cachelist/global search view: */
2067     /* invalidate gpx reference */
2068     crumb->appdata->cur_gpx = NULL;
2069    
2070     gtk_box_pack_start_defaults(GTK_BOX(crumb->appdata->vbox),
2071     crumb->appdata->cur_view);
2072    
2073     if((crumb->level == CRUMB_SEARCH_GLOBAL) ||
2074     (crumb->level == CRUMB_SEARCH_GPX)) {
2075     g_assert((gpx_t*)crumb->data == crumb->appdata->search_results);
2076    
2077     search_result_free((gpx_t*)crumb->data);
2078     crumb->appdata->search_results = NULL;
2079     }
2080    
2081     /* enable gpxlist related menu entries */
2082     gtk_widget_set_sensitive(crumb->appdata->menu_import, TRUE);
2083     gtk_widget_set_sensitive(crumb->appdata->menu_search, TRUE);
2084     gtk_widget_set_sensitive(crumb->appdata->menu_export, TRUE);
2085     }
2086    
2087     gtk_widget_show_all(crumb->appdata->vbox);
2088     g_free(data);
2089     }
2090    
2091     static void crumb_add(appdata_t *appdata, char *name, int level,
2092     gpointer user_data) {
2093     crumb_t *crumb = malloc(sizeof(crumb_t));
2094     crumb->level = level;
2095     crumb->appdata = appdata;
2096     crumb->data = user_data;
2097    
2098     printf("crumb_add with level %d\n", level);
2099    
2100     /* save that we are working on search results */
2101     if((level == CRUMB_SEARCH_GLOBAL) ||
2102     (level == CRUMB_SEARCH_GPX)) {
2103     appdata->search_results = (gpx_t*)user_data;
2104    
2105     /* searches cannot be nested */
2106     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2107     }
2108    
2109     /* save "path" pointers in appdata */
2110     if(crumb->level == CRUMB_CACHELIST)
2111     appdata->cur_gpx = (gpx_t*)user_data;
2112    
2113     if(crumb->level == CRUMB_CACHE) {
2114     appdata->cur_cache = (cache_t*)user_data;
2115     /* the cache view cannot be searched */
2116     gtk_widget_set_sensitive(appdata->menu_search, FALSE);
2117     gtk_widget_set_sensitive(appdata->menu_export, FALSE);
2118     }
2119    
2120     if(level != CRUMB_GPXLIST) {
2121     /* disable gpxlist related menu entries */
2122     gtk_widget_set_sensitive(appdata->menu_import, FALSE);
2123     #ifndef USE_PANNABLE_AREA
2124     gtk_widget_set_sensitive(appdata->menu_remove, FALSE);
2125     gtk_widget_set_sensitive(appdata->menu_close, FALSE);
2126     #endif
2127     }
2128    
2129     hildon_bread_crumb_trail_push_text(HILDON_BREAD_CRUMB_TRAIL(appdata->bct),
2130     name, crumb, (GDestroyNotify)crumb_back);
2131     }
2132     #endif // USE_BREAD_CRUMB_TRAIL
2133    
2134     void main_after_settings_redraw(appdata_t *appdata, int flags) {
2135 harbaum 11 printf("main after settings redraw\n");
2136    
2137     if(!appdata->cur_view) {
2138     printf("no active view\n");
2139     return;
2140     }
2141    
2142 harbaum 1 #ifndef USE_MAEMO
2143     // in non-maemo setup this can only affect the main screen as
2144     // the menu is blocked while a dialog is open. also the main
2145     // screen is always present
2146     if(appdata->gpxlist_items != appdata->cur_items) {
2147     /* re-do the main screen */
2148     gtk_container_remove(GTK_CONTAINER(appdata->vbox), appdata->cur_view);
2149     appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2150     gtk_box_pack_start_defaults(GTK_BOX(appdata->vbox), appdata->cur_view);
2151     gtk_widget_show_all(appdata->vbox);
2152     }
2153     #else
2154     /* a cache screen cannot be changed from the settings and thus doesn't */
2155     /* need to be redrawn */
2156     if(appdata->cur_cache) {
2157     printf("No redraw in cache view required\n");
2158     return;
2159     }
2160    
2161     int redraw = 0; // nothing to redraw
2162    
2163     if(appdata->search_results) {
2164     if((appdata->cur_items != appdata->cachelist_items) || flags)
2165     redraw = 1;
2166     } else {
2167     if(!appdata->cur_gpx) {
2168     if(appdata->cur_items != appdata->gpxlist_items)
2169     redraw = 2; // redraw gpxlist
2170     } else {
2171     if((appdata->cur_items != appdata->cachelist_items) || flags)
2172     redraw = 3; // redraw cachelist
2173     }
2174     }
2175    
2176     if(redraw) {
2177 harbaum 11 GtkWidget *container = appdata->vbox;
2178    
2179     #ifdef USE_STACKABLE_WINDOW
2180     HildonWindowStack *stack = hildon_window_stack_get_default();
2181     container = hildon_window_stack_peek(stack);
2182     #endif
2183    
2184     gtk_container_remove(GTK_CONTAINER(container), appdata->cur_view);
2185 harbaum 1 switch(redraw) {
2186     case 1:
2187     appdata->cur_view = cachelist_create(appdata,
2188     appdata->search_results, NULL);
2189     break;
2190     case 2:
2191     appdata->cur_view = gpxlist_create_view_and_model(appdata, NULL);
2192     break;
2193     case 3:
2194     appdata->cur_view = cachelist_create(appdata,
2195     appdata->cur_gpx, NULL);
2196     break;
2197     }
2198    
2199 harbaum 11 #ifdef USE_STACKABLE_WINDOW
2200     if(container != appdata->vbox)
2201     gtk_container_add(GTK_CONTAINER(container), appdata->cur_view);
2202     else
2203     #endif
2204     gtk_box_pack_start_defaults(GTK_BOX(container), appdata->cur_view);
2205    
2206     gtk_widget_show_all(container);
2207 harbaum 1 }
2208 harbaum 11 #endif // USE_MAEMO
2209 harbaum 1 }
2210    
2211     int main(int argc, char *argv[]) {
2212     appdata_t appdata;
2213    
2214     /* init appdata */
2215     memset(&appdata, 0, sizeof(appdata));
2216    
2217     printf("Using locale for %s in %s\n", PACKAGE, LOCALEDIR);
2218    
2219     setlocale(LC_ALL, "");
2220     bindtextdomain(PACKAGE, LOCALEDIR);
2221     bind_textdomain_codeset(PACKAGE, "UTF-8");
2222     textdomain(PACKAGE);
2223    
2224     /* prepare thread system */
2225     g_thread_init(NULL);
2226    
2227     gtk_init (&argc, &argv);
2228    
2229     #ifdef USE_MAEMO
2230     printf("Installing osso context for \"org.harbaum." APP "\"\n");
2231     appdata.osso_context = osso_initialize("org.harbaum."APP,
2232     VERSION, TRUE, NULL);
2233     if(appdata.osso_context == NULL) {
2234     fprintf(stderr, "error initiating osso context\n");
2235     }
2236    
2237     dbus_register(&appdata);
2238     #endif
2239    
2240     icons_init();
2241    
2242     if(!gnome_vfs_init()) {
2243     g_error("Gnome VFS init failed\n");
2244     }
2245    
2246     #ifdef USE_MAEMO
2247     /* Create the hildon program and setup the title */
2248     appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
2249     g_set_application_name("GPXView");
2250    
2251     /* Create HildonWindow and set it to HildonProgram */
2252     #ifdef USE_STACKABLE_WINDOW
2253     appdata.window = HILDON_WINDOW(hildon_stackable_window_new());
2254     #else
2255     appdata.window = HILDON_WINDOW(hildon_window_new());
2256     #endif
2257     hildon_program_add_window(appdata.program, appdata.window);
2258     #else
2259     /* Create a Window. */
2260     appdata.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2261     /* Set a decent default size for the window. */
2262     gtk_window_set_default_size(GTK_WINDOW(appdata.window), 500, 300);
2263     #endif
2264    
2265 harbaum 12 #if MAEMO_VERSION_MAJOR == 5
2266 harbaum 1 gtk_window_set_title(GTK_WINDOW(appdata.window), "GPXView");
2267 harbaum 12 #endif
2268    
2269 harbaum 1 g_signal_connect(G_OBJECT(appdata.window), "destroy",
2270     G_CALLBACK(on_window_destroy), &appdata);
2271    
2272     g_signal_connect(G_OBJECT(appdata.window), "key_press_event",
2273     G_CALLBACK(on_window_key_press), &appdata);
2274    
2275     appdata.vbox = gtk_vbox_new(FALSE, 2);
2276     gtk_container_add(GTK_CONTAINER(appdata.window), appdata.vbox);
2277 harbaum 3 #ifndef USE_STACKABLE_WINDOW
2278 harbaum 1 menu_create(&appdata);
2279 harbaum 3 #else
2280     hildon_window_set_app_menu(HILDON_WINDOW(appdata.window),
2281     menu_create(&appdata, MENU_GPXLIST));
2282     #endif
2283 harbaum 1
2284     #ifdef USE_BREAD_CRUMB_TRAIL
2285     appdata.bct = hildon_bread_crumb_trail_new();
2286    
2287     gtk_box_pack_start(GTK_BOX(appdata.vbox), appdata.bct, FALSE,FALSE,0);
2288    
2289     hildon_bread_crumb_trail_clear(HILDON_BREAD_CRUMB_TRAIL(appdata.bct));
2290     crumb_add(&appdata, "GPX", CRUMB_GPXLIST, NULL);
2291     #endif
2292    
2293     /* wait for main gui to appear */
2294     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2295     while(gtk_events_pending())
2296     gtk_main_iteration();
2297    
2298     appdata.gconf_client = gconf_client_get_default();
2299     gconf_load_state(&appdata);
2300     gps_init(&appdata);
2301    
2302     appdata.cur_view = gpxlist_create_view_and_model(&appdata, NULL);
2303     gtk_box_pack_start_defaults(GTK_BOX(appdata.vbox), appdata.cur_view);
2304    
2305     gtk_widget_show_all(GTK_WIDGET(appdata.window));
2306     gtk_main();
2307    
2308     cleanup(&appdata);
2309    
2310     return 0;
2311     }