1
/***************************************************************************
4
* Thu Jun 11 13:19:47 2009
5
* Copyright 2009 Rogério Ferro
6
* <rogerioferro@gmail.com>
7
****************************************************************************/
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU Library General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
27
#include "pixbuf-file-chooser.h"
28
#include "cv_drawing.h"
31
#include <glib/gi18n.h>
33
#include <gdk-pixbuf/gdk-pixbuf.h>
36
static void file_print_title (void);
37
static void file_set_name (const char *name);
38
static void file_set_type (const char *type);
39
static void file_set_title (const char *title);
40
static void file_set_untitle (void);
41
static gboolean file_save (const gchar *filename, const gchar *type);
42
static void file_error_dlg (const gchar *message, const GError *error);
45
GtkWindow *parent_window = NULL;
46
gchar *file_name = NULL;
47
gchar *file_title = NULL;
48
gchar *file_type = NULL;
49
gboolean b_saved = TRUE;
50
gboolean b_untitle = TRUE;
58
file_set_parent_window ( GtkWindow * wnd )
72
file_set_unsave ( void )
79
file_set_save ( void )
86
file_save_dialog ( void )
88
gboolean cancel = FALSE;
93
dlg = gtk_message_dialog_new(
98
_("Do you want to save the changes you made to \"%s\"?"),
100
gtk_message_dialog_format_secondary_text(
101
GTK_MESSAGE_DIALOG (dlg),
102
_("Your changes will be lost if you don't save them.") );
104
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_DISCARD, GTK_RESPONSE_NO);
105
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
106
gtk_dialog_add_button (GTK_DIALOG(dlg), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
107
gtk_dialog_set_default_response (GTK_DIALOG(dlg), GTK_RESPONSE_YES);
109
result = gtk_dialog_run (GTK_DIALOG (dlg));
110
gtk_widget_destroy (dlg);
113
case GTK_RESPONSE_YES:
114
on_menu_save_activate (NULL, NULL);
124
case GTK_RESPONSE_CANCEL:
136
file_open ( const gchar * filename )
139
GError *error = NULL;
140
GdkPixbuf *pixbuf = NULL;
141
pixbuf = gdk_pixbuf_new_from_file (filename, &error);
144
if (!file_save_dialog () )
146
GdkPixbufFormat *format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
147
GdkPixbuf *orientation_changed_pixbuf;
148
orientation_changed_pixbuf = gdk_pixbuf_apply_embedded_orientation (pixbuf);
149
cv_set_pixbuf ( orientation_changed_pixbuf );
150
g_object_unref ( orientation_changed_pixbuf );
153
if (gdk_pixbuf_format_is_writable (format))
155
gchar *title = g_filename_display_basename (filename);
156
gchar *type = gdk_pixbuf_format_get_name (format);
159
file_set_type (type);
160
file_set_name (filename);
161
file_set_title (title);
174
gchar *basename = g_path_get_basename (filename);
175
gchar *message = g_strdup_printf (_("Error loading file \"%s\""), basename);
176
file_error_dlg ( message, error);
177
g_error_free (error);
181
g_object_unref (pixbuf);
190
on_menu_open_activate ( GtkMenuItem *menuitem, gpointer user_data)
195
dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_OPEN);
197
response = gtk_dialog_run (GTK_DIALOG (dialog));
198
gtk_widget_hide (dialog);
200
if (response == GTK_RESPONSE_OK)
202
gchar * name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
206
gtk_widget_destroy (dialog);
210
on_menu_save_activate ( GtkMenuItem *menuitem, gpointer user_data)
214
on_menu_save_as_activate (menuitem, user_data);
218
if (file_save (file_name, file_type))
227
on_menu_save_as_activate ( GtkMenuItem *menuitem, gpointer user_data)
232
dialog = pixbuf_file_chooser_new (parent_window, GTK_FILE_CHOOSER_ACTION_SAVE);
233
pixbuf_file_chooser_set_current_name ( PIXBUF_FILE_CHOOSER (dialog), file_title);
234
pixbuf_file_chooser_set_current_filter( PIXBUF_FILE_CHOOSER (dialog), "png");
236
response = gtk_dialog_run (GTK_DIALOG (dialog));
237
gtk_widget_hide (dialog);
239
if (response == GTK_RESPONSE_OK)
241
gchar * name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
242
gchar * title = pixbuf_file_chooser_get_name (PIXBUF_FILE_CHOOSER(dialog));
243
GdkPixbufFormat * format = pixbuf_file_chooser_get_format (PIXBUF_FILE_CHOOSER(dialog));
244
gchar * type = gdk_pixbuf_format_get_name (format);
247
if (file_save (name, type))
252
file_set_name (name);
253
file_set_type (type);
254
file_set_title (title);
261
gtk_widget_destroy (dialog);
268
file_print_title (void)
272
str = g_strdup_printf (_("%s - gnome-paint"), file_title);
277
copy = g_strdup_printf ("%c%s", '*', str);
282
gtk_window_set_title ( parent_window, str);
288
file_set_name (const char *name)
291
file_name = g_strdup (name);
295
file_set_type (const char *type)
298
file_type = g_strdup (type);
302
file_set_title (const char *title)
304
g_free( file_title );
305
file_title = g_strdup (title);
310
file_set_untitle (void)
313
file_set_title (_("untitled"));
317
file_save (const gchar *filename, const gchar *type)
320
GdkPixbuf * pixbuf = cv_get_pixbuf ();
321
GError * error = NULL;
323
if ( !gdk_pixbuf_save ( pixbuf, filename, type, &error, NULL) )
325
gchar *basename = g_path_get_basename (filename);
326
gchar *message = g_strdup_printf (_("Error saving file \"%s\""), basename);
327
file_error_dlg ( message, error);
328
g_error_free (error);
333
g_object_unref (pixbuf);
338
file_error_dlg (const gchar *message, const GError *error)
340
gchar *error_message = _("unknown error");
344
error_message = error->message;
346
g_warning ("%s: %s\n", message, error_message);
347
dlg = gtk_message_dialog_new ( parent_window,
348
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
352
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG (dlg), "%s",error_message);
353
gtk_dialog_run (GTK_DIALOG (dlg));
354
gtk_widget_destroy (dlg);