error-console-commands.c 4.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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 <gtk/gtk.h>

23
#include "libgimpbase/gimpbase.h"
24 25
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
26
#include "actions-types.h"
27 28

#include "widgets/gimperrorconsole.h"
29 30
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpwidgets-utils.h"
31 32 33

#include "error-console-commands.h"

34 35 36 37 38 39 40 41 42
#include "gimp-intl.h"


/*  local function prototypes  */

static void   error_console_save_response (GtkWidget        *dialog,
                                           gint              response_id,
                                           GimpErrorConsole *console);

43 44 45 46

/*  public functions  */

void
47
error_console_clear_cmd_callback (GtkAction *action,
48
                                  gpointer   data)
49 50
{
  GimpErrorConsole *console = GIMP_ERROR_CONSOLE (data);
51 52
  GtkTextIter       start_iter;
  GtkTextIter       end_iter;
53

54 55
  gtk_text_buffer_get_bounds (console->text_buffer, &start_iter, &end_iter);
  gtk_text_buffer_delete (console->text_buffer, &start_iter, &end_iter);
56 57 58
}

void
59 60 61
error_console_save_cmd_callback (GtkAction *action,
                                 gint       value,
                                 gpointer   data)
62 63
{
  GimpErrorConsole *console = GIMP_ERROR_CONSOLE (data);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  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);

88 89 90 91 92
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (console->file_dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

93 94 95 96 97 98
  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);
99

100 101 102 103 104 105 106
  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",
107 108
                    G_CALLBACK (error_console_save_response),
                    console);
109
  g_signal_connect (chooser, "delete-event",
110 111
                    G_CALLBACK (gtk_true),
                    NULL);
112 113

  gimp_help_connect (GTK_WIDGET (chooser), gimp_standard_help_func,
114
                     GIMP_HELP_ERRORS_DIALOG, NULL);
115 116

  gtk_widget_show (GTK_WIDGET (chooser));
117 118
}

119 120 121 122 123 124 125

/*  private functions  */

static void
error_console_save_response (GtkWidget        *dialog,
                             gint              response_id,
                             GimpErrorConsole *console)
126
{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  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);
    }
146

147
  gtk_widget_destroy (dialog);
148
}