1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <time.h>
#include <gtk/gtk.h>
#include "libgimpwidgets/gimpwidgets.h"
#include "display-types.h"
#include "config/gimpdisplayconfig.h"
#include "core/gimp.h"
#include "core/gimpimage.h"
#include "file/file-utils.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpmessagebox.h"
#include "widgets/gimpmessagedialog.h"
#include "widgets/gimpuimanager.h"
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-close.h"
#include "gimp-intl.h"
/* local function prototypes */
static void gimp_display_shell_close_dialog (GimpDisplayShell *shell,
GimpImage *gimage);
static void gimp_display_shell_close_name_changed (GimpImage *image,
GimpMessageBox *box);
static gboolean gimp_display_shell_close_time_changed (GimpMessageBox *box);
static void gimp_display_shell_close_response (GtkWidget *widget,
gboolean close,
GimpDisplayShell *shell);
static gchar * gimp_time_since (guint then);
/* public functions */
void
gimp_display_shell_close (GimpDisplayShell *shell,
gboolean kill_it)
{
GimpImage *gimage;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
gimage = shell->gdisp->gimage;
/* FIXME: gimp_busy HACK not really appropriate here because we only
* want to prevent the busy image and display to be closed. --Mitch
*/
if (gimage->gimp->busy)
return;
/* If the image has been modified, give the user a chance to save
* it before nuking it--this only applies if its the last view
* to an image canvas. (a gimage with disp_count = 1)
*/
if (! kill_it &&
gimage->disp_count == 1 &&
gimage->dirty &&
GIMP_DISPLAY_CONFIG (gimage->gimp->config)->confirm_on_close)
{
gimp_display_shell_close_dialog (shell, gimage);
}
else
{
gimp_display_delete (shell->gdisp);
}
}
/* private functions */
#define RESPONSE_SAVE 1
static void
gimp_display_shell_close_dialog (GimpDisplayShell *shell,
GimpImage *gimage)
{
GtkWidget *dialog;
GimpMessageBox *box;
GClosure *closure;
GSource *source;
gchar *name;
gchar *title;
if (shell->close_dialog)
{
gtk_window_present (GTK_WINDOW (shell->close_dialog));
return;
}
name = file_utils_uri_to_utf8_basename (gimp_image_get_uri (gimage));
title = g_strdup_printf (_("Close %s"), name);
g_free (name);
shell->close_dialog =
dialog = gimp_message_dialog_new (title, GIMP_STOCK_WARNING,
GTK_WIDGET (shell),
GTK_DIALOG_DESTROY_WITH_PARENT,
gimp_standard_help_func, NULL,
_("Do_n't save"), GTK_RESPONSE_CLOSE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, RESPONSE_SAVE,
NULL);
g_free (title);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
g_signal_connect (dialog, "destroy",
G_CALLBACK (gtk_widget_destroyed),
&shell->close_dialog);
g_signal_connect (dialog, "response",
G_CALLBACK (gimp_display_shell_close_response),
shell);
box = GIMP_MESSAGE_DIALOG (dialog)->box;
g_signal_connect_object (gimage, "name_changed",
G_CALLBACK (gimp_display_shell_close_name_changed),
box, 0);
gimp_display_shell_close_name_changed (gimage, box);
closure =
g_cclosure_new_object (G_CALLBACK (gimp_display_shell_close_time_changed),
G_OBJECT (box));
source = g_timeout_source_new (1000);
g_source_set_closure (source, closure);
g_source_attach (source, NULL);
/* The dialog is destroyed with the shell, so it should be safe
* to hold an image pointer for the lifetime of the dialog.
*/
g_object_set_data (G_OBJECT (box), "gimp-image", gimage);
gimp_display_shell_close_time_changed (box);
gtk_widget_show (dialog);
}
static void
gimp_display_shell_close_name_changed (GimpImage *image,
GimpMessageBox *box)
{
GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET (box));
gchar *name;
name = file_utils_uri_to_utf8_basename (gimp_image_get_uri (image));
if (window)
{
gchar *title = g_strdup_printf (_("Close %s"), name);
gtk_window_set_title (GTK_WINDOW (window), title);
g_free (title);
}
gimp_message_box_set_primary_text (box,
_("Save the changes to image '%s' "
"before closing?"),
name);
g_free (name);
}
static gboolean
gimp_display_shell_close_time_changed (GimpMessageBox *box)
{
GimpImage *image = g_object_get_data (G_OBJECT (box), "gimp-image");
if (image->dirty_time)
{
gchar *period = gimp_time_since (image->dirty_time);
gimp_message_box_set_text (box,
_("If you don't save the image, "
"changes from the last %s will be lost."),
period);
g_free (period);
}
else
{
gimp_message_box_set_text (box, NULL);
}
return TRUE;
}
static void
gimp_display_shell_close_response (GtkWidget *widget,
gint response_id,
GimpDisplayShell *shell)
{
gtk_widget_destroy (widget);
switch (response_id)
{
case GTK_RESPONSE_CLOSE:
gimp_display_delete (shell->gdisp);
break;
case RESPONSE_SAVE:
{
GtkAction *action;
action = gimp_ui_manager_find_action (shell->menubar_manager,
"file", "file-save");
g_return_if_fail (action != NULL);
gtk_action_activate (action);
if (! shell->gdisp->gimage->dirty)
gimp_display_delete (shell->gdisp);
}
break;
default:
break;
}
}
static gchar *
gimp_time_since (guint then)
{
guint now = time (NULL);
guint diff = 1 + now - then;
g_return_val_if_fail (now >= then, NULL);
if (diff == 1)
/* one second, the time period */
return g_strdup (_("second"));
if (diff < 60)
return g_strdup_printf (_("%d seconds"), diff);
/* round to the nearest minute */
diff = (diff + 30) / 60;
if (diff == 1)
return g_strdup (_("minute"));
return g_strdup_printf (_("%d minutes"), diff);
}
|