150
104
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...");
106
tr_tool->undo_desc = Q_("command|Scale");
107
tr_tool->progress_text = _("Scaling");
109
tr_tool->use_grid = TRUE;
110
tr_tool->use_handles = TRUE;
111
tr_tool->use_center = TRUE;
112
tr_tool->use_mid_handles = TRUE;
157
116
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
121
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);
123
GimpTransformOptions *options = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
125
gint width = ROUND (tr_tool->trans_info[X1] - tr_tool->trans_info[X0]);
126
gint height = ROUND (tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0]);
128
g_object_set (GIMP_SCALE_TOOL (tr_tool)->box,
131
"keep-aspect", options->constrain,
298
136
gimp_scale_tool_prepare (GimpTransformTool *tr_tool,
137
GimpDisplay *display)
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,
139
GimpScaleTool *scale = GIMP_SCALE_TOOL (tr_tool);
140
GimpTransformOptions *options = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
345
142
tr_tool->trans_info[X0] = (gdouble) tr_tool->x1;
346
143
tr_tool->trans_info[Y0] = (gdouble) tr_tool->y1;
347
144
tr_tool->trans_info[X1] = (gdouble) tr_tool->x2;
348
145
tr_tool->trans_info[Y1] = (gdouble) tr_tool->y2;
149
g_signal_handlers_disconnect_by_func (scale->box,
150
gimp_scale_tool_size_notify,
152
gtk_widget_destroy (scale->box);
155
/* Need to create a new GimpSizeBox widget because the initial
156
* width and height is what counts as 100%.
159
g_object_new (GIMP_TYPE_SIZE_BOX,
160
"width", tr_tool->x2 - tr_tool->x1,
161
"height", tr_tool->y2 - tr_tool->y1,
162
"keep-aspect", options->constrain,
163
"unit", GIMP_DISPLAY_SHELL (display->shell)->unit,
164
"xresolution", display->image->xresolution,
165
"yresolution", display->image->yresolution,
168
gtk_container_set_border_width (GTK_CONTAINER (scale->box), 6);
169
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (tr_tool->dialog)->vbox), scale->box,
171
gtk_widget_show (scale->box);
173
g_signal_connect (scale->box, "notify",
174
G_CALLBACK (gimp_scale_tool_size_notify),
352
179
gimp_scale_tool_motion (GimpTransformTool *tr_tool,
180
GimpDisplay *display)
355
GimpTransformOptions *options;
182
GimpTransformOptions *options = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
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;
189
gdouble diff_x = tr_tool->curx - tr_tool->lastx;
190
gdouble diff_y = tr_tool->cury - tr_tool->lasty;
370
192
switch (tr_tool->function)
372
case TRANSFORM_HANDLE_1:
194
case TRANSFORM_HANDLE_N:
195
diff_x = 0; /* and fall through */
196
case TRANSFORM_HANDLE_NW:
373
197
x1 = &tr_tool->trans_info[X0];
374
198
y1 = &tr_tool->trans_info[Y0];
375
199
x2 = &tr_tool->trans_info[X1];
412
242
tr_tool->trans_info[Y2] += diff_y;
413
243
tr_tool->trans_info[X3] += diff_x;
414
244
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);
254
/* if control is being held, constrain the aspect ratio */
255
if (options->constrain)
257
/* FIXME: improve this */
258
gdouble w = tr_tool->trans_info[X1] - tr_tool->trans_info[X0];
259
gdouble h = tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0];
261
switch (tr_tool->function)
263
case TRANSFORM_HANDLE_NW:
264
case TRANSFORM_HANDLE_SW:
265
tr_tool->trans_info[X0] =
266
tr_tool->trans_info[X1] - tr_tool->aspect * h;
269
case TRANSFORM_HANDLE_NE:
270
case TRANSFORM_HANDLE_SE:
271
tr_tool->trans_info[X1] =
272
tr_tool->trans_info[X0] + tr_tool->aspect * h;
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
304
gimp_scale_tool_recalc (GimpTransformTool *tr_tool,
305
GimpDisplay *display)
478
gimp_transform_matrix_scale (tr_tool->x1,
307
gimp_matrix3_identity (&tr_tool->transform);
308
gimp_transform_matrix_scale (&tr_tool->transform,
480
311
tr_tool->x2 - tr_tool->x1,
481
312
tr_tool->y2 - tr_tool->y1,
482
313
tr_tool->trans_info[X0],
483
314
tr_tool->trans_info[Y0],
484
315
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));
316
tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0]);
320
gimp_scale_tool_size_notify (GtkWidget *box,
322
GimpTransformTool *tr_tool)
324
GimpTransformOptions *options = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
326
if (! strcmp (pspec->name, "width") ||
327
! strcmp (pspec->name, "height"))
339
old_width = ROUND (tr_tool->trans_info[X1] - tr_tool->trans_info[X0]);
340
old_height = ROUND (tr_tool->trans_info[Y1] - tr_tool->trans_info[Y0]);
342
if ((width != old_width) || (height != old_height))
344
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tr_tool));
346
tr_tool->trans_info[X1] = tr_tool->trans_info[X0] + width;
347
tr_tool->trans_info[Y1] = tr_tool->trans_info[Y0] + height;
349
gimp_transform_tool_recalc (tr_tool, GIMP_TOOL (tr_tool)->display);
351
gimp_transform_tool_expose_preview (tr_tool);
353
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tr_tool));
356
else if (! strcmp (pspec->name, "keep-aspect"))
361
"keep-aspect", &constrain,
364
if (constrain != options->constrain)
374
/* Take the aspect ratio from the size box when the user
375
* activates the constraint by pressing the chain button.
377
tr_tool->aspect = (gdouble) width / (gdouble) height;
379
g_object_set (options,
380
"constrain", constrain,