Diff of /trunk/src/notes.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by harbaum, Sat Jun 20 11:08:47 2009 UTC revision 190 by harbaum, Tue Nov 17 10:22:41 2009 UTC
# Line 27  Line 27 
27    
28  #include <math.h>  #include <math.h>
29    
30    #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
31    #include <hildon/hildon-note.h>
32    #include <hildon/hildon-entry.h>
33    #include <hildon/hildon-check-button.h>
34    #endif
35    
36  #if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED)  #if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED)
37  #error "libxml doesn't support required tree or output"  #error "libxml doesn't support required tree or output"
38  #endif  #endif
39    
40  #include "gpxview.h"  #include "gpxview.h"
41    
42    #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
43    #include <hildon/hildon-note.h>
44    #endif
45    
46  void gtk_text_buffer_set_can_paste_rich_text(GtkTextBuffer *buffer, gboolean);  void gtk_text_buffer_set_can_paste_rich_text(GtkTextBuffer *buffer, gboolean);
47  void gtk_text_buffer_set_rich_text_format(GtkTextBuffer *buffer, const gchar *);  void gtk_text_buffer_set_rich_text_format(GtkTextBuffer *buffer, const gchar *);
48    
49  #define TAG_STATE  GTK_STATE_PRELIGHT  #define TAG_STATE  GTK_STATE_PRELIGHT
50    
51    static GtkWidget *check_button_new_with_label(char *label) {
52    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
53      return gtk_check_button_new_with_label(label);
54    #else
55      GtkWidget *cbut =
56        hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
57      gtk_button_set_label(GTK_BUTTON(cbut), label);
58      return cbut;
59    #endif
60    }
61    
62    static void check_button_set_active(GtkWidget *button, gboolean active) {
63    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
64      gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
65    #else
66      hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
67    #endif
68    }
69    
70    static gboolean check_button_get_active(GtkWidget *button) {
71    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
72      return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
73    #else
74      return hildon_check_button_get_active(HILDON_CHECK_BUTTON(button));
75    #endif
76    }
77    
78  static notes_t *notes_load(appdata_t *appdata, cache_t *cache) {  static notes_t *notes_load(appdata_t *appdata, cache_t *cache) {
79    notes_t *notes = NULL;    notes_t *notes = NULL;
80    xmlDoc *doc = NULL;    xmlDoc *doc = NULL;
# Line 157  void notes_load_all(appdata_t *appdata, Line 194  void notes_load_all(appdata_t *appdata,
194    }    }
195  }  }
196    
197  static int notes_save(cache_context_t *context,  static int notes_write_file(cache_context_t *context,
198                        char *text, pos_t pos,                        char *text, pos_t pos,
199                        gboolean override, gboolean found,                        gboolean override, gboolean found,
200                        time_t ftime, gboolean logged) {                        time_t ftime, gboolean logged) {
# Line 230  static int notes_save(cache_context_t *c Line 267  static int notes_save(cache_context_t *c
267    return 0;    return 0;
268  }  }
269    
270  /* this is called from the destroy event of the entire notebook */  static void notes_save(cache_context_t *context) {
 gint notes_destroy_event(GtkWidget *widget, gpointer data ) {  
   cache_context_t *context = (cache_context_t*)data;  
   
   printf("about to destroy notes view\n");  
   
271    /* only save if: there is a text which has been changed or */    /* only save if: there is a text which has been changed or */
272    /* there is a position which differs from the original one */    /* there is a position which differs from the original one */
273    /* or has been changed */    /* or has been changed */
# Line 255  gint notes_destroy_event(GtkWidget *widg Line 287  gint notes_destroy_event(GtkWidget *widg
287      pos.lon = lon_get(context->notes.lonw);      pos.lon = lon_get(context->notes.lonw);
288    
289      gboolean override =      gboolean override =
290        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context->notes.overridew));        check_button_get_active(context->notes.overridew);
291      gboolean found =      gboolean found =
292        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context->notes.foundw));        check_button_get_active(context->notes.foundw);
293      gboolean logged =      gboolean logged =
294        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(context->notes.loggedw));        check_button_get_active(context->notes.loggedw);
295    
296      /* required accuracy is 1/60000 degree */      /* required accuracy is 1/60000 degree */
297      if(((int)(pos.lat * 60000 + .5) !=      if(((int)(pos.lat * 60000 + .5) !=
# Line 333  gint notes_destroy_event(GtkWidget *widg Line 365  gint notes_destroy_event(GtkWidget *widg
365        /* - update the notes entry in the loaded gpx tree */        /* - update the notes entry in the loaded gpx tree */
366    
367        /* update file on disk */        /* update file on disk */
368        notes_save(context, text, pos, override, found,        notes_write_file(context, text, pos, override, found,
369                   context->notes.ftime, logged);                         context->notes.ftime, logged);
370    
371        /* search for matching caches and replace note there */        /* search for matching caches and replace note there */
372        notes_t *note = NULL;        notes_t *note = NULL;
# Line 382  gint notes_destroy_event(GtkWidget *widg Line 414  gint notes_destroy_event(GtkWidget *widg
414    
415      if(text) free(text);      if(text) free(text);
416    }    }
417    }
418    
419    /* this is called from the destroy event of the entire notebook */
420    gint notes_destroy_event(GtkWidget *widget, gpointer data ) {
421      cache_context_t *context = (cache_context_t*)data;
422    
423      printf("about to destroy notes view\n");
424      notes_save(context);
425    
426    return FALSE;    return FALSE;
427  }  }
428    
429    #ifndef NO_COPY_N_PASTE
430  static void on_destroy_textview(GtkWidget *widget, gpointer data) {  static void on_destroy_textview(GtkWidget *widget, gpointer data) {
431    appdata_t *appdata = (appdata_t*)data;    appdata_t *appdata = (appdata_t*)data;
432    
# Line 412  static void on_destroy_textview(GtkWidge Line 453  static void on_destroy_textview(GtkWidge
453      }      }
454    }    }
455  }  }
456    #endif
457    
458  static void ftime_update(GtkWidget *widget, cache_context_t *context,  static void ftime_update(GtkWidget *widget, cache_context_t *context,
459                           gboolean update) {                           gboolean update) {
460    /* check if it has been selected */    /* check if it has been selected */
461    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {    if(check_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
462      printf("set active\n");      printf("set active\n");
463    
464      if(update)      if(update)
# Line 448  static void callback_modified(GtkWidget Line 490  static void callback_modified(GtkWidget
490      printf("was foundw\n");      printf("was foundw\n");
491    
492      /* about to remove "found" flag -> ask for confirmation */      /* about to remove "found" flag -> ask for confirmation */
493      if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {      if(!check_button_get_active(widget)) {
494    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
495        GtkWidget *dialog = gtk_message_dialog_new(        GtkWidget *dialog = gtk_message_dialog_new(
496                   GTK_WINDOW(context->appdata->window),                   GTK_WINDOW(context->appdata->window),
497                   GTK_DIALOG_DESTROY_WITH_PARENT,                   GTK_DIALOG_DESTROY_WITH_PARENT,
# Line 462  static void callback_modified(GtkWidget Line 505  static void callback_modified(GtkWidget
505        if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog)))        if(GTK_RESPONSE_NO == gtk_dialog_run(GTK_DIALOG(dialog)))
506          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
507    
508    #else
509          GtkWidget *dialog =
510            hildon_note_new_confirmation(GTK_WINDOW(context->appdata->window),
511                     _("Do you really want to remove the \"found\" flag? "
512                       "This will void the recorded date of your find!"));
513    
514          /* set the active flag again if the user answered "no" */
515          if(GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(dialog)))
516            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
517    #endif
518    
519        gtk_widget_destroy(dialog);        gtk_widget_destroy(dialog);
520      }      }
521    
# Line 472  static void callback_modified(GtkWidget Line 526  static void callback_modified(GtkWidget
526      printf("was loggedw\n");      printf("was loggedw\n");
527    
528      /* about to remove "found" flag -> ask for confirmation */      /* about to remove "found" flag -> ask for confirmation */
529      if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {      if(!check_button_get_active(widget)) {
530    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
531        GtkWidget *dialog = gtk_message_dialog_new(        GtkWidget *dialog = gtk_message_dialog_new(
532                   GTK_WINDOW(context->appdata->window),                   GTK_WINDOW(context->appdata->window),
533                   GTK_DIALOG_DESTROY_WITH_PARENT,                   GTK_DIALOG_DESTROY_WITH_PARENT,
# Line 491  static void callback_modified(GtkWidget Line 546  static void callback_modified(GtkWidget
546          gtk_widget_set_sensitive(context->notes.foundw, TRUE);          gtk_widget_set_sensitive(context->notes.foundw, TRUE);
547        }        }
548    
549    #else
550          GtkWidget *dialog =
551            hildon_note_new_confirmation(GTK_WINDOW(context->appdata->window),
552                       _("Do you really want to remove the \"logged\" flag? "
553                       "This may cause problems on your next Garmin Field "
554                       "Notes upload!"));
555    
556          /* set the active flag again if the user answered "no" */
557          if(GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(dialog)))
558            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
559          else {
560            gtk_widget_set_sensitive(widget, FALSE);
561            gtk_widget_set_sensitive(context->notes.foundw, TRUE);
562          }
563    
564    #endif
565    
566        gtk_widget_destroy(dialog);        gtk_widget_destroy(dialog);
567      }      }
568    }    }
569  }  }
570    
571    #ifndef NO_COPY_N_PASTE
572  static gboolean focus_in(GtkWidget *widget, GdkEventFocus *event,  static gboolean focus_in(GtkWidget *widget, GdkEventFocus *event,
573                           gpointer data) {                           gpointer data) {
574    appdata_t *appdata = (appdata_t*)data;    appdata_t *appdata = (appdata_t*)data;
# Line 514  static gboolean focus_in(GtkWidget *widg Line 587  static gboolean focus_in(GtkWidget *widg
587    
588    return FALSE;    return FALSE;
589  }  }
590    #endif
591    
592    static gboolean focus_out(GtkWidget *widget, GdkEventFocus *event,
593                             gpointer data) {
594      cache_context_t *context = (cache_context_t*)data;
595    
596      notes_save(context);
597    #if !defined(USE_MAEMO) && defined(ENABLE_OSM_GPS_MAP)
598      map_update(context->appdata);
599    #endif
600    
601      return FALSE;
602    }
603    
604  GtkWidget *cache_notes(cache_context_t *context) {  GtkWidget *cache_notes(cache_context_t *context) {
   appdata_t *appdata = context->appdata;  
605    cache_t *cache = context->cache;    cache_t *cache = context->cache;
606    
607    context->notes.modified = FALSE;    context->notes.modified = FALSE;
# Line 531  GtkWidget *cache_notes(cache_context_t * Line 616  GtkWidget *cache_notes(cache_context_t *
616    /* -------------- custom coordinate ---------------- */    /* -------------- custom coordinate ---------------- */
617    
618    GtkWidget *table = gtk_table_new(2, 4, FALSE);    GtkWidget *table = gtk_table_new(2, 4, FALSE);
619    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
620    gtk_table_set_col_spacing(GTK_TABLE(table), 0, 16);    gtk_table_set_col_spacing(GTK_TABLE(table), 0, 16);
621    gtk_table_set_col_spacing(GTK_TABLE(table), 2, 16);    gtk_table_set_col_spacing(GTK_TABLE(table), 2, 16);
622    #endif
623    
624    gtk_table_attach_defaults(GTK_TABLE(table),    gtk_table_attach_defaults(GTK_TABLE(table),
625                      gtk_label_new(_("New coordinate:")), 0, 1, 0, 1);                      gtk_label_new(_("New coordinate:")), 0, 1, 0, 1);
626    context->notes.overridew = gtk_check_button_new_with_label(_("Override"));    context->notes.overridew = check_button_new_with_label(_("Override"));
627    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context->notes.overridew),    check_button_set_active(context->notes.overridew,
628                                 cache->notes && cache->notes->override);                            cache->notes && cache->notes->override);
629    gtk_table_attach_defaults(GTK_TABLE(table),    gtk_table_attach_defaults(GTK_TABLE(table),
630                              context->notes.overridew, 0, 1, 1, 2);                              context->notes.overridew, 0, 1, 1, 2);
631    
632    GtkWidget *hbox = gtk_hbox_new(FALSE, 2);    GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
633    
634    context->notes.foundw = gtk_check_button_new_with_label(_("Found"));    context->notes.foundw = check_button_new_with_label(_("Found"));
635    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context->notes.foundw),    check_button_set_active(context->notes.foundw,
636                                 cache->notes && cache->notes->found);                            cache->notes && cache->notes->found);
637    gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.foundw);    gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.foundw);
638    
639    context->notes.loggedw = gtk_check_button_new_with_label(_("Logged"));    context->notes.loggedw = check_button_new_with_label(_("Logged"));
640    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(context->notes.loggedw),    check_button_set_active(context->notes.loggedw,
641                                 cache->notes && cache->notes->logged);                            cache->notes && cache->notes->logged);
642    gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.loggedw);    gtk_box_pack_start_defaults(GTK_BOX(hbox), context->notes.loggedw);
643    
644    gtk_table_attach_defaults(GTK_TABLE(table), hbox, 3, 4, 0, 1);    gtk_table_attach_defaults(GTK_TABLE(table), hbox, 3, 4, 0, 1);
# Line 563  GtkWidget *cache_notes(cache_context_t * Line 650  GtkWidget *cache_notes(cache_context_t *
650      gtk_widget_set_sensitive(context->notes.foundw, FALSE);      gtk_widget_set_sensitive(context->notes.foundw, FALSE);
651    
652    context->notes.datew = gtk_label_new("");    context->notes.datew = gtk_label_new("");
653    gtk_misc_set_alignment(GTK_MISC(context->notes.datew), 0.0f, 0.5f);    gtk_misc_set_alignment(GTK_MISC(context->notes.datew), 0.5f, 0.5f);
654    gtk_table_attach_defaults(GTK_TABLE(table),    gtk_table_attach_defaults(GTK_TABLE(table),
655                              context->notes.datew, 3, 4, 1, 2);                              context->notes.datew, 3, 4, 1, 2);
656    ftime_update(context->notes.foundw, context, FALSE);    ftime_update(context->notes.foundw, context, FALSE);
# Line 573  GtkWidget *cache_notes(cache_context_t * Line 660  GtkWidget *cache_notes(cache_context_t *
660    
661    gtk_table_attach_defaults(GTK_TABLE(table),    gtk_table_attach_defaults(GTK_TABLE(table),
662              context->notes.latw = lat_entry_new(pos.lat), 2, 3, 0, 1);              context->notes.latw = lat_entry_new(pos.lat), 2, 3, 0, 1);
663      g_signal_connect(G_OBJECT(context->notes.latw), "focus-out-event",
664                       G_CALLBACK(focus_out), context);
665    
666    gtk_table_attach_defaults(GTK_TABLE(table),    gtk_table_attach_defaults(GTK_TABLE(table),
667              context->notes.lonw = lon_entry_new(pos.lon), 2, 3, 1, 2);              context->notes.lonw = lon_entry_new(pos.lon), 2, 3, 1, 2);
668      g_signal_connect(G_OBJECT(context->notes.lonw), "focus-out-event",
669                       G_CALLBACK(focus_out), context);
670    
671    #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5)
672    hbox = gtk_hbox_new(FALSE, 0);    hbox = gtk_hbox_new(FALSE, 0);
673    gtk_box_pack_start(GTK_BOX(hbox), table, FALSE, FALSE, 0);    gtk_box_pack_start(GTK_BOX(hbox), table, FALSE, FALSE, 0);
674    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
675    #else
676      gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
677    #endif
678    
679  #ifndef USE_PANNABLE_AREA  #ifndef USE_PANNABLE_AREA
680    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);    GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
# Line 594  GtkWidget *cache_notes(cache_context_t * Line 689  GtkWidget *cache_notes(cache_context_t *
689    if(cache->notes && cache->notes->text)    if(cache->notes && cache->notes->text)
690      gtk_text_buffer_set_text(context->notes.buffer, cache->notes->text, -1);      gtk_text_buffer_set_text(context->notes.buffer, cache->notes->text, -1);
691    
692  #ifndef USE_MAEMO  #ifndef USE_HILDON_TEXT_VIEW
693    GtkWidget *view = gtk_text_view_new_with_buffer(context->notes.buffer);    GtkWidget *view = gtk_text_view_new_with_buffer(context->notes.buffer);
694  #else  #else
695    GtkWidget *view = hildon_text_view_new();    GtkWidget *view = hildon_text_view_new();
# Line 612  GtkWidget *cache_notes(cache_context_t * Line 707  GtkWidget *cache_notes(cache_context_t *
707    gtk_text_buffer_set_rich_text_format(context->notes.buffer, "RTF" );    gtk_text_buffer_set_rich_text_format(context->notes.buffer, "RTF" );
708  #endif  #endif
709    
710    #ifndef NO_COPY_N_PASTE
711    g_signal_connect(G_OBJECT(view), "focus-in-event",    g_signal_connect(G_OBJECT(view), "focus-in-event",
712                     G_CALLBACK(focus_in), appdata);                     G_CALLBACK(focus_in), context->appdata);
713    g_signal_connect(G_OBJECT(view), "destroy",    g_signal_connect(G_OBJECT(view), "destroy",
714                     G_CALLBACK(on_destroy_textview), appdata);                     G_CALLBACK(on_destroy_textview), context->appdata);
715    #endif
716    
717  #ifndef USE_PANNABLE_AREA  #ifndef USE_PANNABLE_AREA
718    gtk_container_add(GTK_CONTAINER(scrolled_window), view);    gtk_container_add(GTK_CONTAINER(scrolled_window), view);
# Line 657  void notes_free(notes_t *notes) { Line 754  void notes_free(notes_t *notes) {
754    
755  pos_t notes_get_pos(cache_context_t *context) {  pos_t notes_get_pos(cache_context_t *context) {
756    pos_t pos = context->cache->pos;    pos_t pos = context->cache->pos;
757    if(gtk_toggle_button_get_active(    if(check_button_get_active(context->notes.overridew)) {
                  GTK_TOGGLE_BUTTON(context->notes.overridew))) {  
758      pos.lat = lat_get(context->notes.latw);      pos.lat = lat_get(context->notes.latw);
759      pos.lon = lon_get(context->notes.lonw);      pos.lon = lon_get(context->notes.lonw);
760    }    }
# Line 667  pos_t notes_get_pos(cache_context_t *con Line 763  pos_t notes_get_pos(cache_context_t *con
763    
764  gboolean notes_get_override(cache_context_t *context) {  gboolean notes_get_override(cache_context_t *context) {
765    /* get override value */    /* get override value */
766    return gtk_toggle_button_get_active(    return check_button_get_active(context->notes.overridew);
                       GTK_TOGGLE_BUTTON(context->notes.overridew));  
   
767  }  }
768    
769  typedef struct {  typedef struct {
# Line 821  void notes_log_export(appdata_t *appdata Line 915  void notes_log_export(appdata_t *appdata
915    GtkWidget *label = gtk_label_new(_("Export to:"));    GtkWidget *label = gtk_label_new(_("Export to:"));
916    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE,0);    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE,0);
917    gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f);    gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f);
918    GtkWidget *button = gtk_button_new_with_label(_("Browse..."));    GtkWidget *button = gtk_button_new_with_label(_("Browse"));
919    gtk_signal_connect(GTK_OBJECT(button), "clicked",    gtk_signal_connect(GTK_OBJECT(button), "clicked",
920                       GTK_SIGNAL_FUNC(on_browse), (gpointer)&context);                       GTK_SIGNAL_FUNC(on_browse), (gpointer)&context);
921    gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE,0);    gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE,0);
# Line 941  void notes_log_export(appdata_t *appdata Line 1035  void notes_log_export(appdata_t *appdata
1035          ccontext.appdata = appdata;          ccontext.appdata = appdata;
1036          ccontext.cache = llog->cache;          ccontext.cache = llog->cache;
1037    
1038          notes_save(&ccontext,          notes_write_file(&ccontext,
1039                     llog->cache->notes->text, llog->cache->notes->pos,                     llog->cache->notes->text, llog->cache->notes->pos,
1040                     llog->cache->notes->override, llog->cache->notes->found,                     llog->cache->notes->override, llog->cache->notes->found,
1041                     llog->cache->notes->ftime, llog->cache->notes->logged);                     llog->cache->notes->ftime, llog->cache->notes->logged);

Legend:
Removed from v.1  
changed lines
  Added in v.190