Commit df343547 authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

added gimp_text_buffer_save() which saves a GtkTextBuffer's contents to a

2004-10-25  Michael Natterer  <mitch@gimp.org>

	* app/widgets/gimpwidgets-utils.[ch]: added gimp_text_buffer_save()
	which saves a GtkTextBuffer's contents to a file.

	* app/widgets/gimperrorconsole.c: use
	gimp_editor_add_action_button() and removed all "clicked"
	callbacks, including all file saving code.

	* app/actions/error-console-actions.c
	* app/actions/error-console-commands.[ch]: added the code removed
	above to the action callbacks. Use gimp_text_buffer_save().
parent 714771d4
2004-10-25 Michael Natterer <mitch@gimp.org>
* app/widgets/gimpwidgets-utils.[ch]: added gimp_text_buffer_save()
which saves a GtkTextBuffer's contents to a file.
* app/widgets/gimperrorconsole.c: use
gimp_editor_add_action_button() and removed all "clicked"
callbacks, including all file saving code.
* app/actions/error-console-actions.c
* app/actions/error-console-commands.[ch]: added the code removed
above to the action callbacks. Use gimp_text_buffer_save().
2004-10-24 Michael Natterer <mitch@gimp.org>
 
* app/widgets/gimpgradienteditor.[ch]
......
......@@ -41,18 +41,24 @@ static GimpActionEntry error_console_actions[] =
GIMP_HELP_ERRORS_DIALOG },
{ "error-console-clear", GTK_STOCK_CLEAR,
N_("_Clear Errors"), "", NULL,
N_("_Clear Errors"), "",
N_("Clear errors"),
G_CALLBACK (error_console_clear_cmd_callback),
GIMP_HELP_ERRORS_CLEAR },
GIMP_HELP_ERRORS_CLEAR }
};
static GimpEnumActionEntry error_console_save_actions[] =
{
{ "error-console-save-all", GTK_STOCK_SAVE_AS,
N_("Save _All Errors to File..."), "", NULL,
G_CALLBACK (error_console_save_all_cmd_callback),
N_("Save _All Errors to File..."), "",
N_("Save all errors"),
FALSE,
GIMP_HELP_ERRORS_SAVE },
{ "error-console-save-selection", GTK_STOCK_SAVE_AS,
N_("Save _Selection to File..."), "", NULL,
G_CALLBACK (error_console_save_selection_cmd_callback),
N_("Save _Selection to File..."), "",
N_("Save selection"),
TRUE,
GIMP_HELP_ERRORS_SAVE }
};
......@@ -63,6 +69,11 @@ error_console_actions_setup (GimpActionGroup *group)
gimp_action_group_add_actions (group,
error_console_actions,
G_N_ELEMENTS (error_console_actions));
gimp_action_group_add_enum_actions (group,
error_console_save_actions,
G_N_ELEMENTS (error_console_save_actions),
G_CALLBACK (error_console_save_cmd_callback));
}
void
......
......@@ -20,14 +20,26 @@
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "actions-types.h"
#include "widgets/gimperrorconsole.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpwidgets-utils.h"
#include "error-console-commands.h"
#include "gimp-intl.h"
/* local function prototypes */
static void error_console_save_response (GtkWidget *dialog,
gint response_id,
GimpErrorConsole *console);
/* public functions */
......@@ -36,28 +48,96 @@ error_console_clear_cmd_callback (GtkAction *action,
gpointer data)
{
GimpErrorConsole *console = GIMP_ERROR_CONSOLE (data);
GtkTextIter start_iter;
GtkTextIter end_iter;
if (GTK_WIDGET_IS_SENSITIVE (console->clear_button))
gtk_button_clicked (GTK_BUTTON (console->clear_button));
gtk_text_buffer_get_bounds (console->text_buffer, &start_iter, &end_iter);
gtk_text_buffer_delete (console->text_buffer, &start_iter, &end_iter);
}
void
error_console_save_all_cmd_callback (GtkAction *action,
gpointer data)
error_console_save_cmd_callback (GtkAction *action,
gint value,
gpointer data)
{
GimpErrorConsole *console = GIMP_ERROR_CONSOLE (data);
GtkFileChooser *chooser;
if (value && ! gtk_text_buffer_get_selection_bounds (console->text_buffer,
NULL, NULL))
{
g_message (_("Cannot save. Nothing is selected."));
return;
}
if (console->file_dialog)
{
gtk_window_present (GTK_WINDOW (console->file_dialog));
return;
}
console->file_dialog =
gtk_file_chooser_dialog_new (_("Save Error Log to File"), NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
NULL);
console->save_selection = value;
g_object_add_weak_pointer (G_OBJECT (console->file_dialog),
(gpointer *) &console->file_dialog);
chooser = GTK_FILE_CHOOSER (console->file_dialog);
if (GTK_WIDGET_IS_SENSITIVE (console->save_button))
gtk_button_clicked (GTK_BUTTON (console->save_button));
gtk_window_set_screen (GTK_WINDOW (chooser),
gtk_widget_get_screen (GTK_WIDGET (console)));
gtk_window_set_position (GTK_WINDOW (chooser), GTK_WIN_POS_MOUSE);
gtk_window_set_role (GTK_WINDOW (chooser), "gimp-save-errors");
g_signal_connect (chooser, "response",
G_CALLBACK (error_console_save_response),
console);
g_signal_connect (chooser, "delete_event",
G_CALLBACK (gtk_true),
NULL);
gimp_help_connect (GTK_WIDGET (chooser), gimp_standard_help_func,
GIMP_HELP_ERRORS_DIALOG, NULL);
gtk_widget_show (GTK_WIDGET (chooser));
}
void
error_console_save_selection_cmd_callback (GtkAction *action,
gpointer data)
/* private functions */
static void
error_console_save_response (GtkWidget *dialog,
gint response_id,
GimpErrorConsole *console)
{
GimpErrorConsole *console = GIMP_ERROR_CONSOLE (data);
if (response_id == GTK_RESPONSE_OK)
{
GError *error = NULL;
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (! gimp_text_buffer_save (console->text_buffer, filename,
console->save_selection, &error))
{
g_message (_("Error writing file '%s':\n%s"),
gimp_filename_to_utf8 (filename), error->message);
g_clear_error (&error);
g_free (filename);
return;
}
g_free (filename);
}
if (GTK_WIDGET_IS_SENSITIVE (console->save_button))
gimp_button_extended_clicked (GIMP_BUTTON (console->save_button),
GDK_SHIFT_MASK);
gtk_widget_destroy (dialog);
}
......@@ -20,12 +20,11 @@
#define __ERROR_CONSOLE_COMMANDS_H__
void error_console_clear_cmd_callback (GtkAction *action,
gpointer data);
void error_console_save_all_cmd_callback (GtkAction *action,
gpointer data);
void error_console_save_selection_cmd_callback (GtkAction *action,
gpointer data);
void error_console_clear_cmd_callback (GtkAction *action,
gpointer data);
void error_console_save_cmd_callback (GtkAction *action,
gint value,
gpointer data);
#endif /* __ERROR_CONSOLE_COMMANDS_H__ */
......@@ -24,32 +24,8 @@
#include "config.h"
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef G_OS_WIN32
#include <io.h>
#ifndef S_IRUSR
#define S_IRUSR _S_IREAD
#endif
#ifndef S_IWUSR
#define S_IWUSR _S_IWRITE
#endif
#endif
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "widgets-types.h"
......@@ -57,36 +33,24 @@
#include "core/gimp.h"
#include "gimperrorconsole.h"
#include "gimphelp-ids.h"
#include "gimpmenufactory.h"
#include "gimpwidgets-utils.h"
#include "gimp-intl.h"
static void gimp_error_console_class_init (GimpErrorConsoleClass *klass);
static void gimp_error_console_init (GimpErrorConsole *editor);
static void gimp_error_console_class_init (GimpErrorConsoleClass *klass);
static void gimp_error_console_init (GimpErrorConsole *editor);
static void gimp_error_console_destroy (GtkObject *object);
static void gimp_error_console_unmap (GtkWidget *widget);
static GObject * gimp_error_console_constructor (GType type,
guint n_params,
GObjectConstructParam *params);
static gboolean gimp_error_console_button_press (GtkWidget *widget,
GdkEventButton *event,
GimpErrorConsole *console);
static void gimp_error_console_clear_clicked (GtkWidget *button,
GimpErrorConsole *console);
static void gimp_error_console_save_clicked (GtkWidget *button,
GimpErrorConsole *console);
static void gimp_error_console_save_ext_clicked (GtkWidget *button,
GdkModifierType state,
GimpErrorConsole *console);
static void gimp_error_console_save_response (GtkWidget *dialog,
gint response_id,
GimpErrorConsole *console);
static void gimp_error_console_destroy (GtkObject *object);
static void gimp_error_console_unmap (GtkWidget *widget);
static gboolean gimp_error_console_write_file (GtkTextBuffer *text_buffer,
const gchar *path,
gboolean selection_only);
static gboolean gimp_error_console_button_press (GtkWidget *widget,
GdkEventButton *event,
GimpErrorConsole *console);
static GimpEditorClass *parent_class = NULL;
......@@ -123,23 +87,21 @@ gimp_error_console_get_type (void)
static void
gimp_error_console_class_init (GimpErrorConsoleClass *klass)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = GTK_OBJECT_CLASS (klass);
widget_class = GTK_WIDGET_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->destroy = gimp_error_console_destroy;
widget_class->unmap = gimp_error_console_unmap;
object_class->constructor = gimp_error_console_constructor;
gtk_object_class->destroy = gimp_error_console_destroy;
widget_class->unmap = gimp_error_console_unmap;
}
static void
gimp_error_console_init (GimpErrorConsole *console)
{
GtkWidget *scrolled_window;
gchar *str;
console->text_buffer = gtk_text_buffer_new (NULL);
......@@ -170,29 +132,33 @@ gimp_error_console_init (GimpErrorConsole *console)
G_CALLBACK (gimp_error_console_button_press),
console);
console->clear_button =
gimp_editor_add_button (GIMP_EDITOR (console),
GTK_STOCK_CLEAR, _("Clear errors"),
GIMP_HELP_ERRORS_CLEAR,
G_CALLBACK (gimp_error_console_clear_clicked),
NULL,
console);
console->file_dialog = NULL;
}
str = g_strdup_printf (_("Save all errors\n"
"%s Save selection"),
gimp_get_mod_string (GDK_SHIFT_MASK));
static GObject *
gimp_error_console_constructor (GType type,
guint n_params,
GObjectConstructParam *params)
{
GObject *object;
GimpErrorConsole *console;
console->save_button =
gimp_editor_add_button (GIMP_EDITOR (console),
GTK_STOCK_SAVE_AS, str,
GIMP_HELP_ERRORS_SAVE,
G_CALLBACK (gimp_error_console_save_clicked),
G_CALLBACK (gimp_error_console_save_ext_clicked),
console);
object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
g_free (str);
console = GIMP_ERROR_CONSOLE (object);
console->file_dialog = NULL;
console->clear_button =
gimp_editor_add_action_button (GIMP_EDITOR (console), "error-console",
"error-console-clear", NULL);
console->save_button =
gimp_editor_add_action_button (GIMP_EDITOR (console), "error-console",
"error-console-save-all",
"error-console-save-selection",
GDK_SHIFT_MASK,
NULL);
return object;
}
static void
......@@ -304,149 +270,3 @@ gimp_error_console_button_press (GtkWidget *widget,
return FALSE;
}
static void
gimp_error_console_clear_clicked (GtkWidget *button,
GimpErrorConsole *console)
{
GtkTextIter start_iter;
GtkTextIter end_iter;
gtk_text_buffer_get_start_iter (console->text_buffer, &start_iter);
gtk_text_buffer_get_end_iter (console->text_buffer, &end_iter);
gtk_text_buffer_delete (console->text_buffer, &start_iter, &end_iter);
}
static void
gimp_error_console_save_clicked (GtkWidget *button,
GimpErrorConsole *console)
{
gimp_error_console_save_ext_clicked (button, 0, console);
}
static void
gimp_error_console_save_ext_clicked (GtkWidget *button,
GdkModifierType state,
GimpErrorConsole *console)
{
GtkFileChooser *chooser;
if (! gtk_text_buffer_get_selection_bounds (console->text_buffer,
NULL, NULL) &&
(state & GDK_SHIFT_MASK))
{
g_message (_("Cannot save. Nothing is selected."));
return;
}
if (console->file_dialog)
{
gtk_window_present (GTK_WINDOW (console->file_dialog));
return;
}
console->file_dialog =
gtk_file_chooser_dialog_new (_("Save Error Log to File"), NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
NULL);
console->save_selection = (state & GDK_SHIFT_MASK) ? TRUE : FALSE;
g_object_add_weak_pointer (G_OBJECT (console->file_dialog),
(gpointer *) &console->file_dialog);
chooser = GTK_FILE_CHOOSER (console->file_dialog);
gtk_window_set_screen (GTK_WINDOW (chooser), gtk_widget_get_screen (button));
gtk_window_set_position (GTK_WINDOW (chooser), GTK_WIN_POS_MOUSE);
gtk_window_set_role (GTK_WINDOW (chooser), "gimp-save-errors");
g_signal_connect (chooser, "response",
G_CALLBACK (gimp_error_console_save_response),
console);
g_signal_connect (chooser, "delete_event",
G_CALLBACK (gtk_true),
NULL);
gimp_help_connect (GTK_WIDGET (chooser), gimp_standard_help_func,
GIMP_HELP_ERRORS_DIALOG, NULL);
gtk_widget_show (GTK_WIDGET (chooser));
}
static void
gimp_error_console_save_response (GtkWidget *dialog,
gint response_id,
GimpErrorConsole *console)
{
if (response_id == GTK_RESPONSE_OK)
{
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (! gimp_error_console_write_file (console->text_buffer, filename,
console->save_selection))
{
g_message (_("Error writing file '%s':\n%s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
g_free (filename);
return;
}
g_free (filename);
}
gtk_widget_destroy (dialog);
}
static gboolean
gimp_error_console_write_file (GtkTextBuffer *text_buffer,
const gchar *path,
gboolean selection_only)
{
GtkTextIter start_iter;
GtkTextIter end_iter;
gint fd;
gint text_length;
gint bytes_written;
gchar *text_contents;
fd = open (path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
if (fd == -1)
return FALSE;
if (selection_only)
gtk_text_buffer_get_selection_bounds (text_buffer, &start_iter, &end_iter);
else
gtk_text_buffer_get_bounds (text_buffer, &start_iter, &end_iter);
text_contents = gtk_text_buffer_get_text (text_buffer,
&start_iter, &end_iter, TRUE);
text_length = strlen (text_contents);
if (text_contents && (text_length > 0))
{
bytes_written = write (fd, text_contents, text_length);
g_free (text_contents);
close (fd);
if (bytes_written != text_length)
return FALSE;
else
return TRUE;
}
close (fd);
return TRUE;
}
......@@ -21,7 +21,20 @@
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#ifdef G_OS_WIN32
#include "libbgimpbase/gimpwin32-io.h"
#endif
#include <gtk/gtk.h>
......@@ -730,3 +743,63 @@ gimp_dialog_set_sensitive (GtkDialog *dialog,
gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, sensitive);
}
gboolean
gimp_text_buffer_save (GtkTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,
GError **error)
{
GtkTextIter start_iter;
GtkTextIter end_iter;
gint fd;
gchar *text_contents;
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
if (fd == -1)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
g_strerror (errno));
return FALSE;
}
if (selection_only)
gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter);
else
gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
text_contents = gtk_text_buffer_get_text (buffer,
&start_iter, &end_iter, TRUE);
if (text_contents)
{
gint text_length = strlen (text_contents);
if (text_length > 0)
{
gint bytes_written;
bytes_written = write (fd, text_contents, text_length);
if (bytes_written != text_length)
{
g_free (text_contents);
close (fd);
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
g_strerror (errno));
return FALSE;
}
}
g_free (text_contents);
}
close (fd);
return TRUE;
}
......@@ -72,5 +72,10 @@ void gimp_window_set_hint (GtkWindow *window,
void gimp_dialog_set_sensitive (GtkDialog *dialog,
gboolean sensitive);
gboolean gimp_text_buffer_save (GtkTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,
GError **error);