From 76298c64e374b65711734ae42ed455fb18aab702 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Tue, 17 Jul 2012 16:05:40 +0200 Subject: When the file has been opened rely on it to save. When a file is opened, we keep it in memory and when you try to quit while the data has been changed, propose to save back to this same file. Signed-off-by: Pierre-Yves Chibon --- gtk-gui.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'gtk-gui.c') diff --git a/gtk-gui.c b/gtk-gui.c index 7d449dd40..f28dde484 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -173,26 +173,28 @@ static void file_open(GtkWidget *w, gpointer data) static void file_save(GtkWidget *w, gpointer data) { GtkWidget *dialog; - dialog = gtk_file_chooser_dialog_new("Save File", + char *filename; + if (!existing_filename) { + dialog = gtk_file_chooser_dialog_new("Save File", GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); - gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); - if (!existing_filename) { - gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document"); - } else - gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), existing_filename); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); - if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { - char *filename; - filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document"); + if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + } + gtk_widget_destroy(dialog); + } else { + filename = existing_filename; + } + if (filename){ save_dives(filename); - g_free(filename); mark_divelist_changed(FALSE); } - gtk_widget_destroy(dialog); } static void ask_save_changes() @@ -204,7 +206,18 @@ static void ask_save_changes() GTK_STOCK_NO, GTK_RESPONSE_NO, NULL); content = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); - label = gtk_label_new ("You have unsaved changes\nWould you like to save those before exiting the program?"); + + if (!existing_filename){ + label = gtk_label_new ( + "You have unsaved changes\nWould you like to save those before exiting the program?"); + } else { + char *label_text = (char*) malloc(sizeof(char) * (92 + strlen(existing_filename))); + sprintf(label_text, + "You have unsaved changes to file: %s \nWould you like to save those before exiting the program?", + existing_filename); + label = gtk_label_new (label_text); + g_free(label_text); + } gtk_container_add (GTK_CONTAINER (content), label); gtk_widget_show_all (dialog); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); -- cgit v1.2.3-70-g09d2 From 1a7695115964887cd19bf1d7e2cef60a2c939482 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Tue, 17 Jul 2012 16:09:29 +0200 Subject: Allow to cancel while trying to quit and the data was changed. So far, when trying to quit while the data was changed the offer was "Save" or "Don't save". Now, you can also "Cancel" which will bring you back to the main window. This allows you to re-save the data in another file. Signed-off-by: Pierre-Yves Chibon --- gtk-gui.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'gtk-gui.c') diff --git a/gtk-gui.c b/gtk-gui.c index f28dde484..a969e9552 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -197,13 +197,15 @@ static void file_save(GtkWidget *w, gpointer data) } } -static void ask_save_changes() +static gboolean ask_save_changes() { GtkWidget *dialog, *label, *content; + gboolean quit = TRUE; 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_NO, GTK_RESPONSE_NO, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL); content = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); @@ -221,10 +223,14 @@ static void ask_save_changes() gtk_container_add (GTK_CONTAINER (content), label); gtk_widget_show_all (dialog); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); - if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + gint *outcode = gtk_dialog_run(GTK_DIALOG(dialog)); + if (outcode == GTK_RESPONSE_ACCEPT) { file_save(NULL,NULL); + } else if (outcode == GTK_RESPONSE_CANCEL) { + quit = FALSE; } gtk_widget_destroy(dialog); + return quit; } static gboolean on_delete(GtkWidget* w, gpointer data) @@ -232,10 +238,15 @@ static gboolean on_delete(GtkWidget* w, gpointer data) /* Make sure to flush any modified dive data */ update_dive(NULL); + gboolean quit = TRUE; if (unsaved_changes()) - ask_save_changes(); + quit = ask_save_changes(); - return FALSE; /* go ahead, kill the program, we're good now */ + if (quit){ + return FALSE; /* go ahead, kill the program, we're good now */ + } else { + return TRUE; /* We are not leaving */ + } } static void on_destroy(GtkWidget* w, gpointer data) @@ -248,9 +259,13 @@ static void quit(GtkWidget *w, gpointer data) /* Make sure to flush any modified dive data */ update_dive(NULL); + gboolean quit = TRUE; if (unsaved_changes()) - ask_save_changes(); - gtk_main_quit(); + quit = ask_save_changes(); + + if (quit){ + gtk_main_quit(); + } } GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title, -- cgit v1.2.3-70-g09d2 From 9cb36850303f8ce6c031926512aad3fc2d800889 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Tue, 17 Jul 2012 16:49:27 +0200 Subject: Add a 'Save As' entry in the menu. Add a "Save As" entry in the "File" menu allowing the user to specify the file in which to save the data. This is useful as we no longer offer this option through the "Save" entry while the data had been opened from an existing file. Signed-off-by: Pierre-Yves Chibon --- display-gtk.h | 2 ++ gtk-gui.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'gtk-gui.c') diff --git a/display-gtk.h b/display-gtk.h index 059c6aa23..1f143077e 100644 --- a/display-gtk.h +++ b/display-gtk.h @@ -31,9 +31,11 @@ typedef enum { #if defined __APPLE__ #define CTRLCHAR "" +#define SHIFTCHAR "" #define PREFERENCE_ACCEL "comma" #else #define CTRLCHAR "" +#define SHIFTCHAR "" #define PREFERENCE_ACCEL NULL #endif diff --git a/gtk-gui.c b/gtk-gui.c index a969e9552..02463d91e 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -197,6 +197,30 @@ static void file_save(GtkWidget *w, gpointer data) } } +static void file_save_as(GtkWidget *w, gpointer data) +{ + GtkWidget *dialog; + char *filename; + dialog = gtk_file_chooser_dialog_new("Save File As", + GTK_WINDOW(main_window), + GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, + NULL); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); + + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), existing_filename); + if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + } + gtk_widget_destroy(dialog); + + if (filename){ + save_dives(filename); + mark_divelist_changed(FALSE); + } +} + static gboolean ask_save_changes() { GtkWidget *dialog, *label, *content; @@ -666,6 +690,7 @@ static GtkActionEntry menu_items[] = { { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL}, { "OpenFile", GTK_STOCK_OPEN, NULL, CTRLCHAR "O", NULL, G_CALLBACK(file_open) }, { "SaveFile", GTK_STOCK_SAVE, NULL, CTRLCHAR "S", NULL, G_CALLBACK(file_save) }, + { "SaveAsFile", GTK_STOCK_SAVE_AS, NULL, SHIFTCHAR CTRLCHAR "S", NULL, G_CALLBACK(file_save_as) }, { "Print", GTK_STOCK_PRINT, NULL, CTRLCHAR "P", NULL, G_CALLBACK(do_print) }, { "Import", NULL, "Import", NULL, NULL, G_CALLBACK(import_dialog) }, { "AddDive", NULL, "Add Dive", NULL, NULL, G_CALLBACK(add_dive_cb) }, @@ -687,6 +712,7 @@ static const gchar* ui_string = " \ \ \ \ + \ \ \ \ -- cgit v1.2.3-70-g09d2