* void dive_list_update_dives(void)
* void update_dive_list_units(void)
* void set_divelist_font(const char *font)
+ * void mark_divelist_changed(int changed)
+ * int unsaved_changes()
*/
#include <stdio.h>
#include <stdlib.h>
GtkListStore *model;
GtkTreeViewColumn *date, *depth, *duration, *location;
GtkTreeViewColumn *temperature, *cylinder, *nitrox, *sac;
+ int changed;
};
static struct DiveList dive_list;
DIVELIST_COLUMNS
};
-/* the global dive list that we maintain */
-static struct DiveList dive_list;
-
static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
{
GtkTreeIter iter;
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
+ dive_list.changed = 0;
+
return dive_list.container_widget;
}
+
+void mark_divelist_changed(int changed)
+{
+ dive_list.changed = changed;
+}
+
+int unsaved_changes()
+{
+ return dive_list.changed;
+}
extern void update_dive_list_units(void);
extern void flush_divelist(struct dive *);
+extern void mark_divelist_changed(int);
+extern int unsaved_changes(void);
#endif
#define GCONF_NAME(x) "/apps/subsurface/" #x
-void on_destroy(GtkWidget* w, gpointer data)
-{
- gtk_main_quit();
-}
-
static GtkWidget *dive_profile;
void repaint_dive(void)
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
save_dives(filename);
g_free(filename);
+ mark_divelist_changed(TRUE);
}
gtk_widget_destroy(dialog);
}
+static void ask_save_changes()
+{
+ GtkWidget *dialog;
+ dialog = gtk_dialog_new_with_buttons("Save Changes?",
+ GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+ NULL);
+
+ if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+ file_save(NULL,NULL);
+ }
+ gtk_widget_destroy(dialog);
+}
+
+void on_destroy(GtkWidget* w, gpointer data)
+{
+ if (unsaved_changes())
+ ask_save_changes();
+ gtk_main_quit();
+}
+
static void quit(GtkWidget *w, gpointer data)
{
gtk_main_quit();
return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
}
+/* old is NULL or a valid string, new is a valid string
+ * NOTW: NULL and "" need to be treated as "unchanged" */
+static int text_changed(char *old, char *new)
+{
+ return ((old && strcmp(old,new)) ||
+ (!old && strcmp("",new)));
+}
+
void flush_dive_info_changes(struct dive *dive)
{
+ char *old_text;
+ int changed = 0;
+
if (!dive)
return;
if (location_changed) {
- g_free(dive->location);
+ old_text = dive->location;
dive->location = gtk_editable_get_chars(GTK_EDITABLE(location), 0, -1);
+ if (text_changed(old_text,dive->location))
+ changed = 1;
+ if (old_text)
+ g_free(old_text);
}
if (divemaster_changed) {
- g_free(dive->divemaster);
+ old_text = dive->divemaster;
dive->divemaster = gtk_editable_get_chars(GTK_EDITABLE(divemaster), 0, -1);
+ if (text_changed(old_text,dive->divemaster))
+ changed = 1;
+ if (old_text)
+ g_free(old_text);
}
if (buddy_changed) {
- g_free(dive->buddy);
+ old_text = dive->buddy;
dive->buddy = gtk_editable_get_chars(GTK_EDITABLE(buddy), 0, -1);
+ if (text_changed(old_text,dive->buddy))
+ changed = 1;
+ if (old_text)
+ g_free(old_text);
}
if (notes_changed) {
- g_free(dive->notes);
+ old_text = dive->notes;
dive->notes = get_text(notes);
+ if (text_changed(old_text,dive->notes))
+ changed = 1;
+ if (old_text)
+ g_free(old_text);
}
+ if (changed)
+ mark_divelist_changed(TRUE);
}
#define SET_TEXT_ENTRY(x) \