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 "libgimpmath/gimpmath.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25
26 #include "tools-types.h"
27
28 #include "core/gimp-transform-utils.h"
29
30 #include "widgets/gimphelp-ids.h"
31 #include "widgets/gimpspinscale.h"
32
33 #include "display/gimpdisplay.h"
34 #include "display/gimptoolgui.h"
35 #include "display/gimptoolsheargrid.h"
36
37 #include "gimpsheartool.h"
38 #include "gimptoolcontrol.h"
39 #include "gimptransformgridoptions.h"
40
41 #include "gimp-intl.h"
42
43
44 /* index into trans_info array */
45 enum
46 {
47 ORIENTATION,
48 SHEAR_X,
49 SHEAR_Y
50 };
51
52
53 #define SB_WIDTH 10
54
55
56 /* local function prototypes */
57
58 static gboolean gimp_shear_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
59 GimpMatrix3 *transform);
60 static gchar * gimp_shear_tool_get_undo_desc (GimpTransformGridTool *tg_tool);
61 static void gimp_shear_tool_dialog (GimpTransformGridTool *tg_tool);
62 static void gimp_shear_tool_dialog_update (GimpTransformGridTool *tg_tool);
63 static void gimp_shear_tool_prepare (GimpTransformGridTool *tg_tool);
64 static GimpToolWidget * gimp_shear_tool_get_widget (GimpTransformGridTool *tg_tool);
65 static void gimp_shear_tool_update_widget (GimpTransformGridTool *tg_tool);
66 static void gimp_shear_tool_widget_changed (GimpTransformGridTool *tg_tool);
67
68 static void shear_x_mag_changed (GtkAdjustment *adj,
69 GimpTransformGridTool *tg_tool);
70 static void shear_y_mag_changed (GtkAdjustment *adj,
71 GimpTransformGridTool *tg_tool);
72
73
G_DEFINE_TYPE(GimpShearTool,gimp_shear_tool,GIMP_TYPE_TRANSFORM_GRID_TOOL)74 G_DEFINE_TYPE (GimpShearTool, gimp_shear_tool, GIMP_TYPE_TRANSFORM_GRID_TOOL)
75
76 #define parent_class gimp_shear_tool_parent_class
77
78
79 void
80 gimp_shear_tool_register (GimpToolRegisterCallback callback,
81 gpointer data)
82 {
83 (* callback) (GIMP_TYPE_SHEAR_TOOL,
84 GIMP_TYPE_TRANSFORM_GRID_OPTIONS,
85 gimp_transform_grid_options_gui,
86 0,
87 "gimp-shear-tool",
88 _("Shear"),
89 _("Shear Tool: Shear the layer, selection or path"),
90 N_("S_hear"), "<shift>H",
91 NULL, GIMP_HELP_TOOL_SHEAR,
92 GIMP_ICON_TOOL_SHEAR,
93 data);
94 }
95
96 static void
gimp_shear_tool_class_init(GimpShearToolClass * klass)97 gimp_shear_tool_class_init (GimpShearToolClass *klass)
98 {
99 GimpTransformToolClass *tr_class = GIMP_TRANSFORM_TOOL_CLASS (klass);
100 GimpTransformGridToolClass *tg_class = GIMP_TRANSFORM_GRID_TOOL_CLASS (klass);
101
102 tg_class->info_to_matrix = gimp_shear_tool_info_to_matrix;
103 tg_class->get_undo_desc = gimp_shear_tool_get_undo_desc;
104 tg_class->dialog = gimp_shear_tool_dialog;
105 tg_class->dialog_update = gimp_shear_tool_dialog_update;
106 tg_class->prepare = gimp_shear_tool_prepare;
107 tg_class->get_widget = gimp_shear_tool_get_widget;
108 tg_class->update_widget = gimp_shear_tool_update_widget;
109 tg_class->widget_changed = gimp_shear_tool_widget_changed;
110
111 tr_class->progress_text = C_("undo-type", "Shear");
112 tr_class->progress_text = _("Shearing");
113 tg_class->ok_button_label = _("_Shear");
114 }
115
116 static void
gimp_shear_tool_init(GimpShearTool * shear_tool)117 gimp_shear_tool_init (GimpShearTool *shear_tool)
118 {
119 GimpTool *tool = GIMP_TOOL (shear_tool);
120
121 gimp_tool_control_set_tool_cursor (tool->control, GIMP_TOOL_CURSOR_SHEAR);
122 }
123
124 static gboolean
gimp_shear_tool_info_to_matrix(GimpTransformGridTool * tg_tool,GimpMatrix3 * transform)125 gimp_shear_tool_info_to_matrix (GimpTransformGridTool *tg_tool,
126 GimpMatrix3 *transform)
127 {
128 GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
129 gdouble amount;
130
131 if (tg_tool->trans_info[SHEAR_X] == 0.0 &&
132 tg_tool->trans_info[SHEAR_Y] == 0.0)
133 {
134 tg_tool->trans_info[ORIENTATION] = GIMP_ORIENTATION_UNKNOWN;
135 }
136
137 if (tg_tool->trans_info[ORIENTATION] == GIMP_ORIENTATION_HORIZONTAL)
138 amount = tg_tool->trans_info[SHEAR_X];
139 else
140 amount = tg_tool->trans_info[SHEAR_Y];
141
142 gimp_matrix3_identity (transform);
143 gimp_transform_matrix_shear (transform,
144 tr_tool->x1,
145 tr_tool->y1,
146 tr_tool->x2 - tr_tool->x1,
147 tr_tool->y2 - tr_tool->y1,
148 tg_tool->trans_info[ORIENTATION],
149 amount);
150
151 return TRUE;
152 }
153
154 static gchar *
gimp_shear_tool_get_undo_desc(GimpTransformGridTool * tg_tool)155 gimp_shear_tool_get_undo_desc (GimpTransformGridTool *tg_tool)
156 {
157 gdouble x = tg_tool->trans_info[SHEAR_X];
158 gdouble y = tg_tool->trans_info[SHEAR_Y];
159
160 switch ((gint) tg_tool->trans_info[ORIENTATION])
161 {
162 case GIMP_ORIENTATION_HORIZONTAL:
163 return g_strdup_printf (C_("undo-type", "Shear horizontally by %-3.3g"),
164 x);
165
166 case GIMP_ORIENTATION_VERTICAL:
167 return g_strdup_printf (C_("undo-type", "Shear vertically by %-3.3g"),
168 y);
169
170 default:
171 /* e.g. user entered numbers but no notification callback */
172 return g_strdup_printf (C_("undo-type", "Shear horizontally by %-3.3g, vertically by %-3.3g"),
173 x, y);
174 }
175 }
176
177 static void
gimp_shear_tool_dialog(GimpTransformGridTool * tg_tool)178 gimp_shear_tool_dialog (GimpTransformGridTool *tg_tool)
179 {
180 GimpShearTool *shear = GIMP_SHEAR_TOOL (tg_tool);
181 GtkWidget *vbox;
182 GtkWidget *scale;
183
184 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
185 gtk_box_pack_start (GTK_BOX (gimp_tool_gui_get_vbox (tg_tool->gui)), vbox,
186 FALSE, FALSE, 0);
187 gtk_widget_show (vbox);
188
189 shear->x_adj = (GtkAdjustment *)
190 gtk_adjustment_new (0, -65536, 65536, 1, 10, 0);
191 scale = gimp_spin_scale_new (shear->x_adj, _("Shear magnitude _X"), 0);
192 gimp_spin_scale_set_scale_limits (GIMP_SPIN_SCALE (scale), -1000, 1000);
193 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
194 gtk_widget_show (scale);
195
196 g_signal_connect (shear->x_adj, "value-changed",
197 G_CALLBACK (shear_x_mag_changed),
198 tg_tool);
199
200 shear->y_adj = (GtkAdjustment *)
201 gtk_adjustment_new (0, -65536, 65536, 1, 10, 0);
202 scale = gimp_spin_scale_new (shear->y_adj, _("Shear magnitude _Y"), 0);
203 gimp_spin_scale_set_scale_limits (GIMP_SPIN_SCALE (scale), -1000, 1000);
204 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
205 gtk_widget_show (scale);
206
207 g_signal_connect (shear->y_adj, "value-changed",
208 G_CALLBACK (shear_y_mag_changed),
209 tg_tool);
210 }
211
212 static void
gimp_shear_tool_dialog_update(GimpTransformGridTool * tg_tool)213 gimp_shear_tool_dialog_update (GimpTransformGridTool *tg_tool)
214 {
215 GimpShearTool *shear = GIMP_SHEAR_TOOL (tg_tool);
216
217 gtk_adjustment_set_value (shear->x_adj, tg_tool->trans_info[SHEAR_X]);
218 gtk_adjustment_set_value (shear->y_adj, tg_tool->trans_info[SHEAR_Y]);
219 }
220
221 static void
gimp_shear_tool_prepare(GimpTransformGridTool * tg_tool)222 gimp_shear_tool_prepare (GimpTransformGridTool *tg_tool)
223 {
224 tg_tool->trans_info[ORIENTATION] = GIMP_ORIENTATION_UNKNOWN;
225 tg_tool->trans_info[SHEAR_X] = 0.0;
226 tg_tool->trans_info[SHEAR_Y] = 0.0;
227 }
228
229 static GimpToolWidget *
gimp_shear_tool_get_widget(GimpTransformGridTool * tg_tool)230 gimp_shear_tool_get_widget (GimpTransformGridTool *tg_tool)
231 {
232 GimpTool *tool = GIMP_TOOL (tg_tool);
233 GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
234 GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
235 GimpToolWidget *widget;
236
237 widget = gimp_tool_shear_grid_new (shell,
238 tr_tool->x1,
239 tr_tool->y1,
240 tr_tool->x2,
241 tr_tool->y2,
242 tg_tool->trans_info[ORIENTATION],
243 tg_tool->trans_info[SHEAR_X],
244 tg_tool->trans_info[SHEAR_Y]);
245
246 g_object_set (widget,
247 "inside-function", GIMP_TRANSFORM_FUNCTION_SHEAR,
248 "outside-function", GIMP_TRANSFORM_FUNCTION_SHEAR,
249 "frompivot-shear", TRUE,
250 NULL);
251
252 return widget;
253 }
254
255 static void
gimp_shear_tool_update_widget(GimpTransformGridTool * tg_tool)256 gimp_shear_tool_update_widget (GimpTransformGridTool *tg_tool)
257 {
258 GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
259
260 GIMP_TRANSFORM_GRID_TOOL_CLASS (parent_class)->update_widget (tg_tool);
261
262 g_object_set (tg_tool->widget,
263 "x1", (gdouble) tr_tool->x1,
264 "y1", (gdouble) tr_tool->y1,
265 "x2", (gdouble) tr_tool->x2,
266 "y2", (gdouble) tr_tool->y2,
267 "orientation", (gint) tg_tool->trans_info[ORIENTATION],
268 "shear-x", tg_tool->trans_info[SHEAR_X],
269 "shear-y", tg_tool->trans_info[SHEAR_Y],
270 NULL);
271 }
272
273 static void
gimp_shear_tool_widget_changed(GimpTransformGridTool * tg_tool)274 gimp_shear_tool_widget_changed (GimpTransformGridTool *tg_tool)
275 {
276 GimpOrientationType orientation;
277
278 g_object_get (tg_tool->widget,
279 "orientation", &orientation,
280 "shear-x", &tg_tool->trans_info[SHEAR_X],
281 "shear-y", &tg_tool->trans_info[SHEAR_Y],
282 NULL);
283
284 tg_tool->trans_info[ORIENTATION] = orientation;
285
286 GIMP_TRANSFORM_GRID_TOOL_CLASS (parent_class)->widget_changed (tg_tool);
287 }
288
289 static void
shear_x_mag_changed(GtkAdjustment * adj,GimpTransformGridTool * tg_tool)290 shear_x_mag_changed (GtkAdjustment *adj,
291 GimpTransformGridTool *tg_tool)
292 {
293 gdouble value = gtk_adjustment_get_value (adj);
294
295 if (value != tg_tool->trans_info[SHEAR_X])
296 {
297 GimpTool *tool = GIMP_TOOL (tg_tool);
298 GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
299
300 tg_tool->trans_info[ORIENTATION] = GIMP_ORIENTATION_HORIZONTAL;
301
302 tg_tool->trans_info[SHEAR_X] = value;
303 tg_tool->trans_info[SHEAR_Y] = 0.0; /* can only shear in one axis */
304
305 gimp_transform_grid_tool_push_internal_undo (tg_tool, TRUE);
306
307 gimp_transform_tool_recalc_matrix (tr_tool, tool->display);
308 }
309 }
310
311 static void
shear_y_mag_changed(GtkAdjustment * adj,GimpTransformGridTool * tg_tool)312 shear_y_mag_changed (GtkAdjustment *adj,
313 GimpTransformGridTool *tg_tool)
314 {
315 gdouble value = gtk_adjustment_get_value (adj);
316
317 if (value != tg_tool->trans_info[SHEAR_Y])
318 {
319 GimpTool *tool = GIMP_TOOL (tg_tool);
320 GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (tg_tool);
321
322 tg_tool->trans_info[ORIENTATION] = GIMP_ORIENTATION_VERTICAL;
323
324 tg_tool->trans_info[SHEAR_Y] = value;
325 tg_tool->trans_info[SHEAR_X] = 0.0; /* can only shear in one axis */
326
327 gimp_transform_grid_tool_push_internal_undo (tg_tool, TRUE);
328
329 gimp_transform_tool_recalc_matrix (tr_tool, tool->display);
330 }
331 }
332