diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-08-17 10:28:19 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-08-17 10:28:19 -0700 |
commit | 589589c707262920a4c9ffc258704e30fa62b8fa (patch) | |
tree | e2f4c4fcd04259a85404361f06c790db9220ad82 | |
parent | aab94d07ccb890caaa03cf220b442ea5ce228fc1 (diff) | |
parent | 9cb36850303f8ce6c031926512aad3fc2d800889 (diff) | |
download | subsurface-589589c707262920a4c9ffc258704e30fa62b8fa.tar.gz |
Merge branch 'change_quit2' of http://ambre.pingoured.fr/cgit/subsurface
Pull patches to change behavior on exit from Pierre-Yves Chibon.
Pierry-Yves explains:
"When someone opens a file, change something in it and try to quit, the
program asked whether the data should be saved.
If 'Ok' then it shows the save-window and the user can choose to save
the file or rename it.
My habits in such case would be that since I opened a specific file, I
want to save to that specific file, therefore, when I press 'Ok', I
want it to save automatically to the file I opened.
So I have been working on changes that do:
- When a file has been opened by the user, save to this same file if
the user is 'Ok' while closing.
- Add a 'Cancel' option to the pop-up window that offers to save the
file while closing.
- Add a 'Save As' entry in the file menu."
* 'change_quit2' of http://ambre.pingoured.fr/cgit/subsurface:
Add a 'Save As' entry in the menu.
Allow to cancel while trying to quit and the data was changed.
When the file has been opened rely on it to save.
-rw-r--r-- | display-gtk.h | 2 | ||||
-rw-r--r-- | gtk-gui.c | 84 |
2 files changed, 71 insertions, 15 deletions
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 "<Meta>" +#define SHIFTCHAR "<Shift>" #define PREFERENCE_ACCEL "<Meta>comma" #else #define CTRLCHAR "<Control>" +#define SHIFTCHAR "<Shift>" #define PREFERENCE_ACCEL NULL #endif @@ -173,45 +173,88 @@ 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_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document"); - } else - gtk_file_chooser_set_filename(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); + } else { + filename = existing_filename; + } + if (filename){ + save_dives(filename); + mark_divelist_changed(FALSE); + } +} + +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) { - char *filename; filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + } + gtk_widget_destroy(dialog); + + if (filename){ save_dives(filename); - g_free(filename); mark_divelist_changed(FALSE); } - gtk_widget_destroy(dialog); } -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)); - 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); - 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) @@ -219,10 +262,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) @@ -235,9 +283,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, @@ -638,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) }, @@ -659,6 +712,7 @@ static const gchar* ui_string = " \ <menu name=\"FileMenu\" action=\"FileMenuAction\"> \ <menuitem name=\"Open\" action=\"OpenFile\" /> \ <menuitem name=\"Save\" action=\"SaveFile\" /> \ + <menuitem name=\"Save As\" action=\"SaveAsFile\" /> \ <menuitem name=\"Print\" action=\"Print\" /> \ <separator name=\"Separator1\"/> \ <menuitem name=\"Preferences\" action=\"Preferences\" /> \ |