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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/***************************************************************************
* file.c
*
* Thu Jun 11 13:19:47 2009
* Copyright 2009 Rogério Ferro
* <rogerioferro@gmail.com>
****************************************************************************/
/*
* 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 Library 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., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
#include "common.h"
#include "file.h"
#include "pixbuf-file-chooser.h"
#include "cv_drawing.h"
#include "undo.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
/*private functions*/
static void file_print_title (void);
static void file_set_name (const char *name);
static void file_set_type (const char *type);
static void file_set_title (const char *title);
static void file_set_untitle (void);
static gboolean file_save (const gchar *filename, const gchar *type);
static void file_error_dlg (const gchar *message, const GError *error);
/*private data*/
GtkWindow *parent_window = NULL;
gchar *file_name = NULL;
gchar *file_title = NULL;
gchar *file_type = NULL;
gboolean b_saved = TRUE;
gboolean b_untitle = TRUE;
/*
* CODE
*/
void
file_set_parent_window ( GtkWindow * wnd )
{
parent_window = wnd;
b_saved = TRUE;
file_set_untitle ();
}
gboolean
file_is_save ( void )
{
return b_saved;
}
void
file_set_unsave ( void )
{
b_saved = FALSE;
file_print_title ();
}
void
file_set_save ( void )
{
b_saved = TRUE;
file_print_title ();
}
gboolean
file_save_dialog ( void )
{
gboolean cancel = FALSE;
if (!b_saved)
{
gint result;
GtkWidget *dlg;
dlg = gtk_message_dialog_new(
parent_window,
GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_NONE,
_("Do you want to save the changes you made to \"%s\"?"),
file_title);
gtk_message_dialog_format_secondary_text(
GTK_MESSAGE_DIALOG (dlg),
_("Your changes will be lost if you don't save them.") );
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_DISCARD, GTK_RESPONSE_NO);
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
gtk_dialog_set_default_response (GTK_DIALOG(dlg), GTK_RESPONSE_YES);
result = gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
switch (result)
{
case GTK_RESPONSE_YES:
on_menu_save_activate (NULL, NULL);
if ( b_saved )
{
cancel = FALSE;
}
else
{
cancel = TRUE;
}
break;
case GTK_RESPONSE_CANCEL:
cancel = TRUE;
break;
}
}
return cancel;
}
gboolean
file_open ( const gchar * fname )
{
gboolean ok = FALSE;
GError *error = NULL;
GdkPixbuf *pixbuf = NULL;
gchar *filename = NULL;
GFile *file = NULL;
filename = g_strdup (fname);
file = g_file_new_for_uri (filename);
if( G_IS_FILE (file) )
{
gchar *p = NULL;
p = g_file_get_path ( file);
if(p)
{
g_free (filename);
filename = p;
}
g_object_unref(file);
}
pixbuf = gdk_pixbuf_new_from_file (filename, &error);
if (pixbuf != NULL)
{
if (!file_save_dialog () )
{
GdkPixbufFormat *format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
GdkPixbuf *orientation_changed_pixbuf;
orientation_changed_pixbuf = gdk_pixbuf_apply_embedded_orientation (pixbuf);
cv_set_pixbuf ( orientation_changed_pixbuf );
g_object_unref ( orientation_changed_pixbuf );
undo_clear ();
ok = TRUE;
if (gdk_pixbuf_format_is_writable (format))
{
gchar *title = g_filename_display_basename (filename);
gchar *type = gdk_pixbuf_format_get_name (format);
b_untitle = FALSE;
b_saved = TRUE;
file_set_type (type);
file_set_name (filename);
file_set_title (title);
g_free (title);
g_free (type);
}
else
{
b_saved = FALSE;
file_set_untitle ();
}
}
}
else
{
gchar *basename = g_path_get_basename (filename);
gchar *message = g_strdup_printf (_("Error loading file \"%s\""), basename);
file_error_dlg ( message, error);
g_error_free (error);
g_free (basename);
g_free (message);
}
if(filename)
{
g_free (filename);
}
g_object_unref (pixbuf);
return ok;
}
/* GUI CallBacks */
void
on_menu_open_activate ( GtkMenuItem *menuitem, gpointer user_data)
{
GtkWidget *dialog;
gint response;
dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_OPEN);
response = gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_hide (dialog);
if (response == GTK_RESPONSE_OK)
{
gchar * name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
file_open (name);
g_free (name);
}
gtk_widget_destroy (dialog);
}
void
on_menu_save_activate ( GtkMenuItem *menuitem, gpointer user_data)
{
if (b_untitle)
{
on_menu_save_as_activate (menuitem, user_data);
}
else
{
if (file_save (file_name, file_type))
{
b_saved = TRUE;
file_print_title ();
}
}
}
void
on_menu_save_as_activate ( GtkMenuItem *menuitem, gpointer user_data)
{
GtkWidget *dialog;
gint response;
dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_SAVE);
pixbuf_file_chooser_set_current_name ( PIXBUF_FILE_CHOOSER (dialog), file_title);
pixbuf_file_chooser_set_current_filter( PIXBUF_FILE_CHOOSER (dialog), "png");
response = gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_hide (dialog);
if (response == GTK_RESPONSE_OK)
{
gchar * name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gchar * title = pixbuf_file_chooser_get_name (PIXBUF_FILE_CHOOSER(dialog));
GdkPixbufFormat * format = pixbuf_file_chooser_get_format (PIXBUF_FILE_CHOOSER(dialog));
gchar * type = gdk_pixbuf_format_get_name (format);
if (file_save (name, type))
{
b_untitle = FALSE;
b_saved = TRUE;
file_set_name (name);
file_set_type (type);
file_set_title (title);
}
g_free (name);
g_free (title);
g_free (type);
}
gtk_widget_destroy (dialog);
}
/*Private*/
static void
file_print_title (void)
{
gchar *str;
str = g_strdup_printf (_("%s - gnome-paint"), file_title);
if (!b_saved)
{
gchar *copy;
copy = g_strdup_printf ("%c%s", '*', str);
g_free (str);
str = copy;
}
gtk_window_set_title ( parent_window, str);
g_free (str);
}
static void
file_set_name (const char *name)
{
g_free (file_name);
file_name = g_strdup (name);
}
static void
file_set_type (const char *type)
{
g_free (file_type);
file_type = g_strdup (type);
}
static void
file_set_title (const char *title)
{
g_free( file_title );
file_title = g_strdup (title);
file_print_title ();
}
static void
file_set_untitle (void)
{
b_untitle = TRUE;
file_set_title (_("untitled"));
}
static gboolean
file_save (const gchar *filename, const gchar *type)
{
gboolean ok = TRUE;
GdkPixbuf * pixbuf = cv_get_pixbuf ();
GError * error = NULL;
if ( !gdk_pixbuf_save ( pixbuf, filename, type, &error, NULL) )
{
gchar *basename = g_path_get_basename (filename);
gchar *message = g_strdup_printf (_("Error saving file \"%s\""), basename);
file_error_dlg ( message, error);
g_error_free (error);
g_free (basename);
g_free (message);
ok = FALSE;
}
g_object_unref (pixbuf);
return ok;
}
static void
file_error_dlg (const gchar *message, const GError *error)
{
gchar *error_message = _("unknown error");
GtkWidget *dlg;
if (error != NULL )
{
error_message = error->message;
}
g_warning ("%s: %s\n", message, error_message);
dlg = gtk_message_dialog_new ( parent_window,
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"%s",message);
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG (dlg), "%s",error_message);
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
}
|