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 #include "core/gimptoolinfo.h"
30 
31 #include "widgets/gimphelp-ids.h"
32 #include "widgets/gimptextbuffer.h"
33 #include "widgets/gimpuimanager.h"
34 #include "widgets/gimpwidgets-utils.h"
35 
36 #include "display/gimpdisplay.h"
37 
38 #include "tools/gimptexttool.h"
39 
40 #include "dialogs/dialogs.h"
41 
42 #include "text-tool-commands.h"
43 
44 #include "gimp-intl.h"
45 
46 
47 /*  local function prototypes  */
48 
49 static void   text_tool_load_dialog_response (GtkWidget    *dialog,
50                                               gint          response_id,
51                                               GimpTextTool *tool);
52 
53 
54 /*  public functions  */
55 
56 void
text_tool_cut_cmd_callback(GimpAction * action,GVariant * value,gpointer data)57 text_tool_cut_cmd_callback (GimpAction *action,
58                             GVariant   *value,
59                             gpointer    data)
60 {
61   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
62 
63   gimp_text_tool_cut_clipboard (text_tool);
64 }
65 
66 void
text_tool_copy_cmd_callback(GimpAction * action,GVariant * value,gpointer data)67 text_tool_copy_cmd_callback (GimpAction *action,
68                              GVariant   *value,
69                              gpointer    data)
70 {
71   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
72 
73   gimp_text_tool_copy_clipboard (text_tool);
74 }
75 
76 void
text_tool_paste_cmd_callback(GimpAction * action,GVariant * value,gpointer data)77 text_tool_paste_cmd_callback (GimpAction *action,
78                               GVariant   *value,
79                               gpointer    data)
80 {
81   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
82 
83   gimp_text_tool_paste_clipboard (text_tool);
84 }
85 
86 void
text_tool_delete_cmd_callback(GimpAction * action,GVariant * value,gpointer data)87 text_tool_delete_cmd_callback (GimpAction *action,
88                                GVariant   *value,
89                                gpointer    data)
90 {
91   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
92 
93   gimp_text_tool_delete_selection (text_tool);
94 }
95 
96 void
text_tool_load_cmd_callback(GimpAction * action,GVariant * value,gpointer data)97 text_tool_load_cmd_callback (GimpAction *action,
98                              GVariant   *value,
99                              gpointer    data)
100 {
101   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
102   GtkWidget    *dialog;
103 
104   dialog = dialogs_get_dialog (G_OBJECT (text_tool), "gimp-text-file-dialog");
105 
106   if (! dialog)
107     {
108       GtkWidget *parent = NULL;
109 
110       if (GIMP_TOOL (text_tool)->display)
111         {
112           GimpDisplayShell *shell;
113 
114           shell = gimp_display_get_shell (GIMP_TOOL (text_tool)->display);
115 
116           parent = gtk_widget_get_toplevel (GTK_WIDGET (shell));
117         }
118 
119       dialog = gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
120                                             parent ? GTK_WINDOW (parent) : NULL,
121                                             GTK_FILE_CHOOSER_ACTION_OPEN,
122 
123                                             _("_Cancel"), GTK_RESPONSE_CANCEL,
124                                             _("_Open"),   GTK_RESPONSE_OK,
125 
126                                             NULL);
127 
128       gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
129       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
130                                                GTK_RESPONSE_OK,
131                                                GTK_RESPONSE_CANCEL,
132                                                -1);
133 
134       gtk_window_set_role (GTK_WINDOW (dialog), "gimp-text-load-file");
135       gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
136 
137       g_signal_connect (dialog, "response",
138                         G_CALLBACK (text_tool_load_dialog_response),
139                         text_tool);
140       g_signal_connect (dialog, "delete-event",
141                         G_CALLBACK (gtk_true),
142                         NULL);
143 
144       dialogs_attach_dialog (G_OBJECT (text_tool),
145                              "gimp-text-file-dialog", dialog);
146     }
147 
148   gtk_window_present (GTK_WINDOW (dialog));
149 }
150 
151 void
text_tool_clear_cmd_callback(GimpAction * action,GVariant * value,gpointer data)152 text_tool_clear_cmd_callback (GimpAction *action,
153                               GVariant   *value,
154                               gpointer    data)
155 {
156   GimpTextTool  *text_tool = GIMP_TEXT_TOOL (data);
157   GtkTextBuffer *buffer    = GTK_TEXT_BUFFER (text_tool->buffer);
158   GtkTextIter    start, end;
159 
160   gtk_text_buffer_get_bounds (buffer, &start, &end);
161   gtk_text_buffer_select_range (buffer, &start, &end);
162   gimp_text_tool_delete_selection (text_tool);
163 }
164 
165 void
text_tool_text_to_path_cmd_callback(GimpAction * action,GVariant * value,gpointer data)166 text_tool_text_to_path_cmd_callback (GimpAction *action,
167                                      GVariant   *value,
168                                      gpointer    data)
169 {
170   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
171 
172   gimp_text_tool_create_vectors (text_tool);
173 }
174 
175 void
text_tool_text_along_path_cmd_callback(GimpAction * action,GVariant * value,gpointer data)176 text_tool_text_along_path_cmd_callback (GimpAction *action,
177                                         GVariant   *value,
178                                         gpointer    data)
179 {
180   GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
181 
182   gimp_text_tool_create_vectors_warped (text_tool);
183 }
184 
185 void
text_tool_direction_cmd_callback(GimpAction * action,GVariant * value,gpointer data)186 text_tool_direction_cmd_callback (GimpAction *action,
187                                   GVariant   *value,
188                                   gpointer    data)
189 {
190   GimpTextTool      *text_tool = GIMP_TEXT_TOOL (data);
191   GimpTextDirection  direction;
192 
193   direction = (GimpTextDirection) g_variant_get_int32 (value);
194 
195   g_object_set (text_tool->proxy,
196                 "base-direction", direction,
197                 NULL);
198 }
199 
200 
201 /*  private functions  */
202 
203 static void
text_tool_load_dialog_response(GtkWidget * dialog,gint response_id,GimpTextTool * tool)204 text_tool_load_dialog_response (GtkWidget    *dialog,
205                                 gint          response_id,
206                                 GimpTextTool *tool)
207 {
208   if (response_id == GTK_RESPONSE_OK)
209     {
210       GFile  *file  = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
211       GError *error = NULL;
212 
213       if (! gimp_text_buffer_load (tool->buffer, file, &error))
214         {
215           gimp_message (GIMP_TOOL (tool)->tool_info->gimp, G_OBJECT (dialog),
216                         GIMP_MESSAGE_ERROR,
217                         _("Could not open '%s' for reading: %s"),
218                         gimp_file_get_utf8_name (file),
219                         error->message);
220           g_clear_error (&error);
221           g_object_unref (file);
222           return;
223         }
224 
225       g_object_unref (file);
226     }
227 
228   gtk_widget_hide (dialog);
229 }
230