Common Save Confirmation Dialogue

From: Marc O'Morain <marc omorain gmail com>

To: gtk-devel-list gnome org

Subject: Common Save Confirmation Dialogue

Date: Tue, 10 May 2005 23:58:10 +0100

Hi all,
I originally sent this idea to desktop-devel-list, but it was decided
that this was a better list to post to.
Attached is some code to create a HIG-compliant save confirmation
message box. This message box would be shown when a user attempts to
exit a program before they have saved changes they have made. At the
moment each application must implement this message box themselves,
which leads to repetition of code and wasted time.
If the attached code was added to GTK, it would help speed up GTK
application development, promote a familiar desktop with common
dialogue boxes, and help ensure HIG compliance.
The addition of this feature has been discussed before, it was
originally mentioned by Murray Cumming in December 2004.
http://mail.gnome.org/archives/usability/2004-December/msg00084.htmlhttp://mail.gnome.org/archives/gnumeric-list/2003-July/msg00033.htmlhttp://actsofvolition.com/include/savealerts/
I am currently studying for my finals, and do not have access to a
GNOME system to compile this code on, so there may be a typo or two.
Note that this is my first ever piece of GTK code, so please be
gentle.
Best Regards,
--
Marc O'Morain

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
/* GTK - The GIMP Toolkit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2005. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_SAVE_CONFIRMATION_ALERT_H__
#define __GTK_SAVE_CONFIRMATION_ALERT_H__
#include "gtkwindow.h"
gint gtk_save_confirmation_alert (GtkWindow *parent,
const GTimeVal *last_save,
const gchar *document_name);
#endif

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
/* GTK - The GIMP Toolkit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2005. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "gtksaveconfirmationalert.h"
#include "gtkmessagedialog.h"
/**
* gtk_save_confirmation_alert:
* @parent: transient parent, or NULL for none
* @last_save: The time that the last save took place
* or the time that the document was created if it has
* not been saved before.
* @document_name: The name of the document that is
* unsaved.
*
* Opens a message box to ask the user if they want to
* save a document that has been changed since the last
* save. This will usually be called when the user exits
* a program while documents are still open.
*
* Return value: The users response. GTK_RESPONSE_NO to
* discard changes, GTK_RESPONSE_CANCEL to return to
* the program, or GTK_RESPONSE_YES to save the changes.
*
* Since: 2.X?
**/
gint
gtk_save_confirmation_alert (GtkWindow *parent,
const GTimeVal *last_save,
const gchar *document_name)
{
GTimeVal now;
glong seconds;
GtkWidget* dialog;
gint result;
g_get_current_time(&now);
seconds = now.tv_sec - last_save.tv_sec;
dialog = gtk_message_dialog_new_with_markup (parent,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_NONE,
"<b>"_("Save changes to %s before closing?")"</b>",
document_name);
gtk_dialog_add_button (dialog, _("Close _Without Saving"),
GTK_RESPONSE_NO);
gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL);
gtk_dialog_add_button (dialog, GTK_STOCK_SAVE,
GTK_RESPONSE_YES);
if (seconds < 55)
{
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"%ld seconds will be permanently lost."),
seconds);
}
else if (seconds < 75)
{
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"minute will be permanently lost."));
}
else if (seconds < 110)
{
seconds -= 60;
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"minute and %ld seconds will be permanently lost."),
seconds);
}
else if (seconds < 3600)
{
glong minutes = seconds / 60;
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"%ld minutes will be permanently lost."),
minutes);
}
else if (seconds < 7200)
{
seconds -= 3600;
glong minutes = seconds / 60;
if (minutes < 5)
{
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"hour will be permanently lost."));
}
else
{
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"hour and %ld minutes will be permanently lost."),
minutes);
}
}
else
{
glong hours = seconds / 3600;
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG(dialog),
_("If you don't save, changes from the last "
"%ld hours will be permanently lost."),
hours);
}
result = gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return result;
}