text-editor-commands.c 4.63 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gegl.h>
21 22 23 24 25 26 27
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "actions-types.h"

28 29
#include "core/gimp.h"

30
#include "widgets/gimphelp-ids.h"
31 32
#include "widgets/gimptextbuffer.h"
#include "widgets/gimptexteditor.h"
33
#include "widgets/gimpuimanager.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#include "text-editor-commands.h"

#include "gimp-intl.h"


/*  local function prototypes  */

static void   text_editor_load_response (GtkWidget      *dialog,
                                         gint            response_id,
                                         GimpTextEditor *editor);


/*  public functions  */

void
text_editor_load_cmd_callback (GtkAction *action,
                               gpointer   data)
{
  GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);

55
  if (! editor->file_dialog)
56
    {
57
      GtkWidget *dialog;
58

59 60 61 62
      dialog = editor->file_dialog =
        gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
                                     GTK_WINDOW (editor),
                                     GTK_FILE_CHOOSER_ACTION_OPEN,
63

64 65
                                     _("_Cancel"), GTK_RESPONSE_CANCEL,
                                     _("_Open"),   GTK_RESPONSE_OK,
66

67
                                     NULL);
68

69
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
70
      gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
71 72 73
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
74

75 76 77
      gtk_window_set_role (GTK_WINDOW (dialog), "gimp-text-load-file");
      gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
      gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
78

79 80
      g_object_add_weak_pointer (G_OBJECT (dialog),
                                 (gpointer) &editor->file_dialog);
81

82 83 84 85 86 87 88
      g_signal_connect (dialog, "response",
                        G_CALLBACK (text_editor_load_response),
                        editor);
      g_signal_connect (dialog, "delete-event",
                        G_CALLBACK (gtk_true),
                        NULL);
    }
89

90
  gtk_window_present (GTK_WINDOW (editor->file_dialog));
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
}

void
text_editor_clear_cmd_callback (GtkAction *action,
                                gpointer   data)
{
  GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
  GtkTextBuffer  *buffer;

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));

  gtk_text_buffer_set_text (buffer, "", 0);
}

void
text_editor_direction_cmd_callback (GtkAction *action,
                                    GtkAction *current,
                                    gpointer   data)
{
  GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
  gint            value;

  value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));

  gimp_text_editor_set_direction (editor, (GimpTextDirection) value);
}


/*  private functions  */

static void
text_editor_load_response (GtkWidget      *dialog,
                           gint            response_id,
                           GimpTextEditor *editor)
{
  if (response_id == GTK_RESPONSE_OK)
    {
      GtkTextBuffer *buffer;
129
      GFile         *file;
130 131
      GError        *error = NULL;

132 133
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
      file   = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
134

135
      if (! gimp_text_buffer_load (GIMP_TEXT_BUFFER (buffer), file, &error))
136
        {
137 138 139
          gimp_message (editor->ui_manager->gimp, G_OBJECT (dialog),
                        GIMP_MESSAGE_ERROR,
                        _("Could not open '%s' for reading: %s"),
140
                        gimp_file_get_utf8_name (file),
141
                        error->message);
142
          g_clear_error (&error);
143
          g_object_unref (file);
144 145 146
          return;
        }

147
      g_object_unref (file);
148 149
    }

150
  gtk_widget_hide (dialog);
151
}