From 94c0d3d25ef4883fecfccdf3f364dd44027dfbec Mon Sep 17 00:00:00 2001 From: Reuben Green Date: Wed, 2 Oct 2019 16:04:47 +0100 Subject: [PATCH] Add a dialog to confirm closure of multiple tabs (bug #15873) Adds a new function xfce_dialog_confirm_close_tabs, which runs a dialog box to ask the user if they wish to close the current tab, close the whole window, or cancel. The function has the option of adding a check box to the dialog to let the user choose whether they wish to see this dialog in future. --- libxfce4ui/libxfce4ui.symbols | 1 + libxfce4ui/xfce-dialogs.c | 83 +++++++++++++++++++++++++++++++++++ libxfce4ui/xfce-dialogs.h | 5 +++ 3 files changed, 89 insertions(+) diff --git a/libxfce4ui/libxfce4ui.symbols b/libxfce4ui/libxfce4ui.symbols index df86081..7e4965b 100644 --- a/libxfce4ui/libxfce4ui.symbols +++ b/libxfce4ui/libxfce4ui.symbols @@ -63,6 +63,7 @@ xfce_dialog_show_info G_GNUC_PRINTF (3, 4) xfce_dialog_show_warning G_GNUC_PRINTF (3, 4) xfce_dialog_show_error G_GNUC_PRINTF (3, 4) xfce_dialog_confirm G_GNUC_PRINTF (5, 6) +xfce_dialog_confirm_close_tabs xfce_message_dialog_new G_GNUC_NULL_TERMINATED G_GNUC_MALLOC xfce_message_dialog_new_valist G_GNUC_MALLOC xfce_message_dialog G_GNUC_NULL_TERMINATED diff --git a/libxfce4ui/xfce-dialogs.c b/libxfce4ui/xfce-dialogs.c index f264ef1..4c2bb54 100644 --- a/libxfce4ui/xfce-dialogs.c +++ b/libxfce4ui/xfce-dialogs.c @@ -532,6 +532,89 @@ xfce_dialog_confirm (GtkWindow *parent, +/** + * xfce_dialog_confirm_close_tabs: + * @parent : (allow-none): transient parent of the dialog, or %NULL. + * @num_tabs : the number of open tabs for display to user + * @show_confirm_box : whether to ask user if they want this confirmation in future + * @confirm_box_checked : (allow-none): state of confirmation checkbox + * + * Runs a dialog to ask the user whether they want to close the whole window, + * close the current tab, or cancel. + * + * If @num_tabs is non-negative, the message to the user will state that there + * are @num_tabs open tabs. If @num_tabs is negative, then the message to the + * user will state simply that there are "multiple open tabs". + * + * If @show_confirm_box is %TRUE, then a checkbox is added to the dialog to allow + * the user to set whether they wish to see this dialog in future. The initial + * state of the checkbox is determined by the value stored at @confirm_box_checked, + * and the value at @confirm_box_checked after returning records the state of the + * checkbox. If @show_confirm_box is %FALSE, @confirm_box_checked is ignored, and + * may be %NULL. + * + * Return value: #GTK_RESPONSE_CANCEL if cancelled, #GTK_RESPONSE_YES if the user + * wants to close the window, #GTK_RESPONSE_CLOSE if the user wants to close the tab, + * and #GTK_RESPONSE_NONE for an error. + */ +gint +xfce_dialog_confirm_close_tabs (GtkWindow *parent, + gint num_tabs, + gboolean show_confirm_box, + gboolean* confirm_box_checked) +{ + GtkWidget *dialog, *checkbutton, *vbox; + const gchar *primary_text, *warning_icon; + gchar *secondary_text; + gint response; + + g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), GTK_RESPONSE_NONE); + g_return_val_if_fail (!show_confirm_box || confirm_box_checked != NULL, GTK_RESPONSE_NONE); + + primary_text = _("Close all tabs?"); + if (num_tabs < 0) + secondary_text = g_strdup (_("This window has multiple tabs open. Closing this window\n" + "will also close all its tabs.")); + else + secondary_text = g_strdup_printf (_("This window has %d tabs open. Closing this window\n" + "will also close all its tabs."), num_tabs); + +#if GTK_CHECK_VERSION (3, 0, 0) + warning_icon = "dialog-warning"; +#else + warning_icon = GTK_STOCK_DIALOG_WARNING; +#endif + + dialog = xfce_message_dialog_new (parent, _("Warning"), + warning_icon, + primary_text, + secondary_text, + XFCE_BUTTON_TYPE_MIXED, "gtk-cancel", _("_Cancel"), GTK_RESPONSE_CANCEL, + XFCE_BUTTON_TYPE_MIXED, "application-exit", _("Close _Window"), GTK_RESPONSE_YES, + XFCE_BUTTON_TYPE_MIXED, "window-close", _("Close T_ab"), GTK_RESPONSE_CLOSE, + NULL); + + if (show_confirm_box) + { + checkbutton = gtk_check_button_new_with_mnemonic (_("Do _not ask me again")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), *confirm_box_checked); + vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); + gtk_box_pack_start (GTK_BOX (vbox), checkbutton, FALSE, FALSE, 5); + } + + gtk_widget_show_all (dialog); + response = gtk_dialog_run (GTK_DIALOG (dialog)); + + if (show_confirm_box) + *confirm_box_checked = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbutton)); + + gtk_widget_destroy (dialog); + g_free (secondary_text); + return response; +} + + + /** * xfce_message_dialog_new_valist: * @parent : (allow-none): transient parent of the dialog, or %NULL. diff --git a/libxfce4ui/xfce-dialogs.h b/libxfce4ui/xfce-dialogs.h index 88c5234..2346277 100644 --- a/libxfce4ui/xfce-dialogs.h +++ b/libxfce4ui/xfce-dialogs.h @@ -81,6 +81,11 @@ gboolean xfce_dialog_confirm (GtkWindow *parent, const gchar *primary_format, ...) G_GNUC_PRINTF (5, 6); +gint xfce_dialog_confirm_close_tabs (GtkWindow *parent, + gint num_tabs, + gboolean show_confirm_box, + gboolean* confirm_box_checked); + GtkWidget *xfce_message_dialog_new_valist (GtkWindow *parent, const gchar *title, const gchar *icon_stock_id, -- 2.23.0