1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
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  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpbase/gimpbase.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25 
26 #include "actions-types.h"
27 
28 #include "core/gimp.h"
29 
30 #include "widgets/gimphelp-ids.h"
31 #include "widgets/gimptextbuffer.h"
32 #include "widgets/gimptexteditor.h"
33 #include "widgets/gimpuimanager.h"
34 
35 #include "text-editor-commands.h"
36 
37 #include "gimp-intl.h"
38 
39 
40 /*  local function prototypes  */
41 
42 static void   text_editor_load_response (GtkWidget      *dialog,
43                                          gint            response_id,
44                                          GimpTextEditor *editor);
45 
46 
47 /*  public functions  */
48 
49 void
text_editor_load_cmd_callback(GimpAction * action,GVariant * value,gpointer data)50 text_editor_load_cmd_callback (GimpAction *action,
51                                GVariant   *value,
52                                gpointer    data)
53 {
54   GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
55 
56   if (! editor->file_dialog)
57     {
58       GtkWidget *dialog;
59 
60       dialog = editor->file_dialog =
61         gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
62                                      GTK_WINDOW (editor),
63                                      GTK_FILE_CHOOSER_ACTION_OPEN,
64 
65                                      _("_Cancel"), GTK_RESPONSE_CANCEL,
66                                      _("_Open"),   GTK_RESPONSE_OK,
67 
68                                      NULL);
69 
70       gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
71       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
72                                                GTK_RESPONSE_OK,
73                                                GTK_RESPONSE_CANCEL,
74                                                -1);
75 
76       gtk_window_set_role (GTK_WINDOW (dialog), "gimp-text-load-file");
77       gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
78       gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
79 
80       g_object_add_weak_pointer (G_OBJECT (dialog),
81                                  (gpointer) &editor->file_dialog);
82 
83       g_signal_connect (dialog, "response",
84                         G_CALLBACK (text_editor_load_response),
85                         editor);
86       g_signal_connect (dialog, "delete-event",
87                         G_CALLBACK (gtk_true),
88                         NULL);
89     }
90 
91   gtk_window_present (GTK_WINDOW (editor->file_dialog));
92 }
93 
94 void
text_editor_clear_cmd_callback(GimpAction * action,GVariant * value,gpointer data)95 text_editor_clear_cmd_callback (GimpAction *action,
96                                 GVariant   *value,
97                                 gpointer    data)
98 {
99   GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
100   GtkTextBuffer  *buffer;
101 
102   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
103 
104   gtk_text_buffer_set_text (buffer, "", 0);
105 }
106 
107 void
text_editor_direction_cmd_callback(GimpAction * action,GVariant * value,gpointer data)108 text_editor_direction_cmd_callback (GimpAction *action,
109                                     GVariant   *value,
110                                     gpointer    data)
111 {
112   GimpTextEditor    *editor = GIMP_TEXT_EDITOR (data);
113   GimpTextDirection  direction;
114 
115   direction = (GimpTextDirection) g_variant_get_int32 (value);
116 
117   gimp_text_editor_set_direction (editor, direction);
118 }
119 
120 
121 /*  private functions  */
122 
123 static void
text_editor_load_response(GtkWidget * dialog,gint response_id,GimpTextEditor * editor)124 text_editor_load_response (GtkWidget      *dialog,
125                            gint            response_id,
126                            GimpTextEditor *editor)
127 {
128   if (response_id == GTK_RESPONSE_OK)
129     {
130       GtkTextBuffer *buffer;
131       GFile         *file;
132       GError        *error = NULL;
133 
134       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
135       file   = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
136 
137       if (! gimp_text_buffer_load (GIMP_TEXT_BUFFER (buffer), file, &error))
138         {
139           gimp_message (editor->ui_manager->gimp, G_OBJECT (dialog),
140                         GIMP_MESSAGE_ERROR,
141                         _("Could not open '%s' for reading: %s"),
142                         gimp_file_get_utf8_name (file),
143                         error->message);
144           g_clear_error (&error);
145           g_object_unref (file);
146           return;
147         }
148 
149       g_object_unref (file);
150     }
151 
152   gtk_widget_hide (dialog);
153 }
154