1
/* The GIMP -- an image manipulation program
2
* 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 2 of the License, or
7
* (at your option) any later version.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "libgimpbase/gimpbase.h"
24
#include "libgimpmath/gimpmath.h"
25
#include "libgimpwidgets/gimpwidgets.h"
27
#include "tools-types.h"
29
#include "core/gimp-transform-utils.h"
30
#include "core/gimpimage.h"
31
#include "core/gimpdrawable-transform.h"
32
#include "core/gimptoolinfo.h"
33
#include "core/gimpunit.h"
35
#include "widgets/gimphelp-ids.h"
37
#include "display/gimpdisplay.h"
38
#include "display/gimpdisplayshell.h"
41
#warning FIXME #include "dialogs/dialogs-types.h"
43
#include "dialogs/dialogs-types.h"
44
#include "dialogs/info-dialog.h"
46
#include "gimpscaletool.h"
47
#include "gimptoolcontrol.h"
48
#include "gimptransformoptions.h"
50
#include "gimp-intl.h"
53
/* local function prototypes */
55
static void gimp_scale_tool_class_init (GimpScaleToolClass *klass);
56
static void gimp_scale_tool_init (GimpScaleTool *sc_tool);
58
static void gimp_scale_tool_dialog (GimpTransformTool *tr_tool);
59
static void gimp_scale_tool_dialog_update (GimpTransformTool *tr_tool);
60
static void gimp_scale_tool_prepare (GimpTransformTool *tr_tool,
62
static void gimp_scale_tool_motion (GimpTransformTool *tr_tool,
64
static void gimp_scale_tool_recalc (GimpTransformTool *tr_tool,
67
static void gimp_scale_tool_size_changed (GtkWidget *widget,
68
GimpTransformTool *tr_tool);
69
static void gimp_scale_tool_unit_changed (GtkWidget *widget,
70
GimpTransformTool *tr_tool);
71
static void gimp_scale_tool_aspect_changed (GtkWidget *widget,
72
GimpTransformTool *tr_tool);
75
/* private variables */
77
static GimpTransformToolClass *parent_class = NULL;
80
/* public functions */
83
gimp_scale_tool_register (GimpToolRegisterCallback callback,
86
(* callback) (GIMP_TYPE_SCALE_TOOL,
87
GIMP_TYPE_TRANSFORM_OPTIONS,
88
gimp_transform_options_gui,
92
_("Scale the layer or selection"),
93
N_("_Scale"), "<shift>T",
94
NULL, GIMP_HELP_TOOL_SCALE,
95
GIMP_STOCK_TOOL_SCALE,
100
gimp_scale_tool_get_type (void)
102
static GType tool_type = 0;
106
static const GTypeInfo tool_info =
108
sizeof (GimpScaleToolClass),
109
(GBaseInitFunc) NULL,
110
(GBaseFinalizeFunc) NULL,
111
(GClassInitFunc) gimp_scale_tool_class_init,
112
NULL, /* class_finalize */
113
NULL, /* class_data */
114
sizeof (GimpScaleTool),
116
(GInstanceInitFunc) gimp_scale_tool_init,
119
tool_type = g_type_register_static (GIMP_TYPE_TRANSFORM_TOOL,
128
/* private functions */
131
gimp_scale_tool_class_init (GimpScaleToolClass *klass)
133
GimpTransformToolClass *trans_class = GIMP_TRANSFORM_TOOL_CLASS (klass);
135
parent_class = g_type_class_peek_parent (klass);
137
trans_class->dialog = gimp_scale_tool_dialog;
138
trans_class->dialog_update = gimp_scale_tool_dialog_update;
139
trans_class->prepare = gimp_scale_tool_prepare;
140
trans_class->motion = gimp_scale_tool_motion;
141
trans_class->recalc = gimp_scale_tool_recalc;
145
gimp_scale_tool_init (GimpScaleTool *scale_tool)
147
GimpTool *tool = GIMP_TOOL (scale_tool);
148
GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (scale_tool);
150
gimp_tool_control_set_tool_cursor (tool->control, GIMP_TOOL_CURSOR_RESIZE);
152
tr_tool->shell_desc = _("Scaling information");
153
tr_tool->progress_text = _("Scaling...");
157
gimp_scale_tool_dialog (GimpTransformTool *tr_tool)
159
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
160
GtkWidget *spinbutton;
162
info_dialog_add_label (tr_tool->info_dialog,
163
_("Original Width:"),
164
scale->orig_width_buf);
165
info_dialog_add_label (tr_tool->info_dialog,
167
scale->orig_height_buf);
169
spinbutton = info_dialog_add_spinbutton (tr_tool->info_dialog,
171
NULL, -1, 1, 1, 10, 1, 1, 2,
173
scale->sizeentry = info_dialog_add_sizeentry (tr_tool->info_dialog,
174
_("Current height:"),
176
GIMP_UNIT_PIXEL, "%a",
178
GIMP_SIZE_ENTRY_UPDATE_SIZE,
179
G_CALLBACK (gimp_scale_tool_size_changed),
181
g_signal_connect (scale->sizeentry, "unit_changed",
182
G_CALLBACK (gimp_scale_tool_unit_changed),
185
gimp_size_entry_add_field (GIMP_SIZE_ENTRY (scale->sizeentry),
186
GTK_SPIN_BUTTON (spinbutton), NULL);
188
info_dialog_add_label (tr_tool->info_dialog,
191
info_dialog_add_label (tr_tool->info_dialog,
195
spinbutton = info_dialog_add_spinbutton (tr_tool->info_dialog,
197
&scale->aspect_ratio_val,
198
0, 65536, 0.01, 0.1, 1, 0.5, 2,
199
G_CALLBACK (gimp_scale_tool_aspect_changed),
202
gtk_table_set_row_spacing (GTK_TABLE (tr_tool->info_dialog->info_table),
204
gtk_table_set_row_spacing (GTK_TABLE (tr_tool->info_dialog->info_table),
209
gimp_scale_tool_dialog_update (GimpTransformTool *tr_tool)
211
GimpTool *tool = GIMP_TOOL (tr_tool);
212
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
213
GimpTransformOptions *options;
215
gdouble ratio_x, ratio_y;
216
gint x1, y1, x2, y2, x3, y3, x4, y4;
219
gchar format_buf[16];
221
static GimpUnit label_unit = GIMP_UNIT_PIXEL;
223
options = GIMP_TRANSFORM_OPTIONS (tool->tool_info->tool_options);
225
unit = gimp_size_entry_get_unit (GIMP_SIZE_ENTRY (scale->sizeentry));
227
/* Find original sizes */
233
if (unit != GIMP_UNIT_PERCENT)
236
gimp = tool->tool_info->gimp;
238
unit_factor = _gimp_unit_get_factor (gimp, label_unit);
240
if (label_unit) /* unit != GIMP_UNIT_PIXEL */
242
g_snprintf (format_buf, sizeof (format_buf), "%%.%df %s",
243
_gimp_unit_get_digits (gimp, label_unit) + 1,
244
_gimp_unit_get_symbol (gimp, label_unit));
245
g_snprintf (scale->orig_width_buf, MAX_INFO_BUF, format_buf,
246
(x2 - x1) * unit_factor / tool->gdisp->gimage->xresolution);
247
g_snprintf (scale->orig_height_buf, MAX_INFO_BUF, format_buf,
248
(y2 - y1) * unit_factor / tool->gdisp->gimage->yresolution);
250
else /* unit == GIMP_UNIT_PIXEL */
252
g_snprintf (scale->orig_width_buf, MAX_INFO_BUF, "%d", x2 - x1);
253
g_snprintf (scale->orig_height_buf, MAX_INFO_BUF, "%d", y2 - y1);
256
/* Find current sizes */
257
x3 = (gint) tr_tool->trans_info[X0];
258
y3 = (gint) tr_tool->trans_info[Y0];
259
x4 = (gint) tr_tool->trans_info[X1];
260
y4 = (gint) tr_tool->trans_info[Y1];
262
scale->size_vals[0] = x4 - x3;
263
scale->size_vals[1] = y4 - y3;
265
ratio_x = ratio_y = 0.0;
268
ratio_x = (double) (x4 - x3) / (double) (x2 - x1);
270
ratio_y = (double) (y4 - y3) / (double) (y2 - y1);
272
/* Detecting initial update, aspect_ratio reset */
273
if (ratio_x == 1 && ratio_y == 1)
274
scale->aspect_ratio_val = 0.0;
276
/* Only when one or the two options are disabled, is necessary to
277
* update the value Taking care of the initial update too
279
if (! options->constrain_1 ||
280
! options->constrain_2 ||
281
scale->aspect_ratio_val == 0 )
283
scale->aspect_ratio_val =
284
((tr_tool->trans_info[X1] - tr_tool->trans_info[X0]) /
285
(tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0]));
288
g_snprintf (scale->x_ratio_buf, sizeof (scale->x_ratio_buf),
290
g_snprintf (scale->y_ratio_buf, sizeof (scale->y_ratio_buf),
293
info_dialog_update (tr_tool->info_dialog);
294
info_dialog_show (tr_tool->info_dialog);
298
gimp_scale_tool_prepare (GimpTransformTool *tr_tool,
301
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
303
scale->size_vals[0] = tr_tool->x2 - tr_tool->x1;
304
scale->size_vals[1] = tr_tool->y2 - tr_tool->y1;
306
g_signal_handlers_block_by_func (scale->sizeentry,
307
gimp_scale_tool_size_changed,
309
g_signal_handlers_block_by_func (scale->sizeentry,
310
gimp_scale_tool_unit_changed,
313
gimp_size_entry_set_unit (GIMP_SIZE_ENTRY (scale->sizeentry),
314
GIMP_DISPLAY_SHELL (gdisp->shell)->unit);
316
gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (scale->sizeentry), 0,
317
gdisp->gimage->xresolution, FALSE);
318
gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (scale->sizeentry), 1,
319
gdisp->gimage->yresolution, FALSE);
321
gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (scale->sizeentry), 0,
323
GIMP_MAX_IMAGE_SIZE);
324
gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (scale->sizeentry), 1,
326
GIMP_MAX_IMAGE_SIZE);
328
gimp_size_entry_set_size (GIMP_SIZE_ENTRY (scale->sizeentry), 0,
329
0, scale->size_vals[0]);
330
gimp_size_entry_set_size (GIMP_SIZE_ENTRY (scale->sizeentry), 1,
331
0, scale->size_vals[1]);
333
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (scale->sizeentry), 0,
334
scale->size_vals[0]);
335
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (scale->sizeentry), 1,
336
scale->size_vals[1]);
338
g_signal_handlers_unblock_by_func (scale->sizeentry,
339
gimp_scale_tool_size_changed,
341
g_signal_handlers_unblock_by_func (scale->sizeentry,
342
gimp_scale_tool_unit_changed,
345
tr_tool->trans_info[X0] = (gdouble) tr_tool->x1;
346
tr_tool->trans_info[Y0] = (gdouble) tr_tool->y1;
347
tr_tool->trans_info[X1] = (gdouble) tr_tool->x2;
348
tr_tool->trans_info[Y1] = (gdouble) tr_tool->y2;
352
gimp_scale_tool_motion (GimpTransformTool *tr_tool,
355
GimpTransformOptions *options;
363
gdouble diff_x, diff_y;
365
options = GIMP_TRANSFORM_OPTIONS (GIMP_TOOL (tr_tool)->tool_info->tool_options);
367
diff_x = tr_tool->curx - tr_tool->lastx;
368
diff_y = tr_tool->cury - tr_tool->lasty;
370
switch (tr_tool->function)
372
case TRANSFORM_HANDLE_1:
373
x1 = &tr_tool->trans_info[X0];
374
y1 = &tr_tool->trans_info[Y0];
375
x2 = &tr_tool->trans_info[X1];
376
y2 = &tr_tool->trans_info[Y1];
380
case TRANSFORM_HANDLE_2:
381
x1 = &tr_tool->trans_info[X1];
382
y1 = &tr_tool->trans_info[Y0];
383
x2 = &tr_tool->trans_info[X0];
384
y2 = &tr_tool->trans_info[Y1];
389
case TRANSFORM_HANDLE_3:
390
x1 = &tr_tool->trans_info[X0];
391
y1 = &tr_tool->trans_info[Y1];
392
x2 = &tr_tool->trans_info[X1];
393
y2 = &tr_tool->trans_info[Y0];
398
case TRANSFORM_HANDLE_4:
399
x1 = &tr_tool->trans_info[X1];
400
y1 = &tr_tool->trans_info[Y1];
401
x2 = &tr_tool->trans_info[X0];
402
y2 = &tr_tool->trans_info[Y0];
406
case TRANSFORM_HANDLE_CENTER:
407
tr_tool->trans_info[X0] += diff_x;
408
tr_tool->trans_info[Y0] += diff_y;
409
tr_tool->trans_info[X1] += diff_x;
410
tr_tool->trans_info[Y1] += diff_y;
411
tr_tool->trans_info[X2] += diff_x;
412
tr_tool->trans_info[Y2] += diff_y;
413
tr_tool->trans_info[X3] += diff_x;
414
tr_tool->trans_info[Y3] += diff_y;
422
/* if just the mod1 key is down, affect only the height */
423
if (options->constrain_2 && ! options->constrain_1)
427
/* if just the control key is down, affect only the width */
428
else if (options->constrain_1 && ! options->constrain_2)
432
/* if control and mod1 are both down, constrain the aspect ratio */
433
else if (options->constrain_1 && options->constrain_2)
435
mag = hypot ((gdouble) (tr_tool->x2 - tr_tool->x1),
436
(gdouble) (tr_tool->y2 - tr_tool->y1));
438
dot = (dir_x * diff_x * (tr_tool->x2 - tr_tool->x1) +
439
dir_y * diff_y * (tr_tool->y2 - tr_tool->y1));
443
diff_x = dir_x * (tr_tool->x2 - tr_tool->x1) * dot / (mag * mag);
444
diff_y = dir_y * (tr_tool->y2 - tr_tool->y1) * dot / (mag * mag);
457
if (*x1 >= *x2) *x1 = *x2 - 1;
461
if (*x1 <= *x2) *x1 = *x2 + 1;
466
if (*y1 >= *y2) *y1 = *y2 - 1;
470
if (*y1 <= *y2) *y1 = *y2 + 1;
475
gimp_scale_tool_recalc (GimpTransformTool *tr_tool,
478
gimp_transform_matrix_scale (tr_tool->x1,
480
tr_tool->x2 - tr_tool->x1,
481
tr_tool->y2 - tr_tool->y1,
482
tr_tool->trans_info[X0],
483
tr_tool->trans_info[Y0],
484
tr_tool->trans_info[X1] - tr_tool->trans_info[X0],
485
tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0],
486
&tr_tool->transform);
490
gimp_scale_tool_size_changed (GtkWidget *widget,
491
GimpTransformTool *tr_tool)
493
GimpTool *tool = GIMP_TOOL (tr_tool);
494
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
495
GimpTransformOptions *options;
499
options = GIMP_TRANSFORM_OPTIONS (tool->tool_info->tool_options);
501
width = RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0));
502
height = RINT (gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 1));
504
if ((width != (tr_tool->trans_info[X1] -
505
tr_tool->trans_info[X0])) ||
506
(height != (tr_tool->trans_info[Y1] -
507
tr_tool->trans_info[Y0])))
509
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tr_tool));
511
if (options->constrain_1 && options->constrain_2)
513
gdouble ratio = scale->aspect_ratio_val;
515
/* Calculating height and width taking into account the aspect ratio*/
516
if (width != (tr_tool->trans_info[X1] - tr_tool->trans_info[X0]))
517
height = width / ratio;
519
width = height * ratio;
522
tr_tool->trans_info[X1] = tr_tool->trans_info[X0] + width;
523
tr_tool->trans_info[Y1] = tr_tool->trans_info[Y0] + height;
525
gimp_transform_tool_recalc (tr_tool, GIMP_TOOL (tr_tool)->gdisp);
527
gimp_transform_tool_expose_preview (tr_tool);
529
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tr_tool));
534
gimp_scale_tool_unit_changed (GtkWidget *widget,
535
GimpTransformTool *tr_tool)
537
gimp_scale_tool_dialog_update (tr_tool);
541
gimp_scale_tool_aspect_changed (GtkWidget *widget,
542
GimpTransformTool *tr_tool)
544
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
546
scale->aspect_ratio_val = GTK_ADJUSTMENT (widget)->value;
548
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tr_tool));
550
tr_tool->trans_info[Y1] =
551
((gdouble) (tr_tool->trans_info[X1] - tr_tool->trans_info[X0]) /
552
scale->aspect_ratio_val) +
553
tr_tool->trans_info[Y0];
555
gimp_transform_tool_recalc (tr_tool, GIMP_TOOL (tr_tool)->gdisp);
557
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tr_tool));