PIKApp/app/tools/pikaoffsettool.c

801 lines
28 KiB
C

/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"
#include "tools-types.h"
#include "core/pikachannel.h"
#include "core/pikadrawable.h"
#include "core/pikadrawablefilter.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikalayermask.h"
#include "widgets/pikahelp-ids.h"
#include "display/pikadisplay.h"
#include "display/pikatoolgui.h"
#include "pikaoffsettool.h"
#include "pikafilteroptions.h"
#include "pikatoolcontrol.h"
#include "pika-intl.h"
static gboolean pika_offset_tool_initialize (PikaTool *tool,
PikaDisplay *display,
GError **error);
static void pika_offset_tool_control (PikaTool *tool,
PikaToolAction action,
PikaDisplay *display);
static void pika_offset_tool_button_press (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaButtonPressType press_type,
PikaDisplay *display);
static void pika_offset_tool_button_release (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaButtonReleaseType release_type,
PikaDisplay *display);
static void pika_offset_tool_motion (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaDisplay *display);
static void pika_offset_tool_oper_update (PikaTool *tool,
const PikaCoords *coords,
GdkModifierType state,
gboolean proximity,
PikaDisplay *display);
static void pika_offset_tool_cursor_update (PikaTool *tool,
const PikaCoords *coords,
GdkModifierType state,
PikaDisplay *display);
static gchar * pika_offset_tool_get_operation (PikaFilterTool *filter_tool,
gchar **description);
static void pika_offset_tool_dialog (PikaFilterTool *filter_tool);
static void pika_offset_tool_config_notify (PikaFilterTool *filter_tool,
PikaConfig *config,
const GParamSpec *pspec);
static void pika_offset_tool_region_changed (PikaFilterTool *filter_tool);
static void pika_offset_tool_offset_changed (PikaSizeEntry *se,
PikaOffsetTool *offset_tool);
static void pika_offset_tool_half_xy_clicked (GtkButton *button,
PikaOffsetTool *offset_tool);
static void pika_offset_tool_half_x_clicked (GtkButton *button,
PikaOffsetTool *offset_tool);
static void pika_offset_tool_half_y_clicked (GtkButton *button,
PikaOffsetTool *offset_tool);
static void pika_offset_tool_edge_behavior_toggled (GtkToggleButton *toggle,
PikaOffsetTool *offset_tool);
static void pika_offset_tool_background_changed (PikaContext *context,
const PikaRGB *color,
PikaOffsetTool *offset_tool);
static gint pika_offset_tool_get_width (PikaOffsetTool *offset_tool);
static gint pika_offset_tool_get_height (PikaOffsetTool *offset_tool);
static void pika_offset_tool_update (PikaOffsetTool *offset_tool);
static void pika_offset_tool_halt (PikaOffsetTool *offset_tool);
G_DEFINE_TYPE (PikaOffsetTool, pika_offset_tool,
PIKA_TYPE_FILTER_TOOL)
#define parent_class pika_offset_tool_parent_class
void
pika_offset_tool_register (PikaToolRegisterCallback callback,
gpointer data)
{
(* callback) (PIKA_TYPE_OFFSET_TOOL,
PIKA_TYPE_FILTER_OPTIONS, NULL,
PIKA_CONTEXT_PROP_MASK_BACKGROUND,
"pika-offset-tool",
_("Offset"),
_("Shift the pixels, optionally wrapping them at the borders"),
N_("_Offset..."), NULL,
NULL, PIKA_HELP_TOOL_OFFSET,
PIKA_ICON_TOOL_OFFSET,
data);
}
static void
pika_offset_tool_class_init (PikaOffsetToolClass *klass)
{
PikaToolClass *tool_class = PIKA_TOOL_CLASS (klass);
PikaFilterToolClass *filter_tool_class = PIKA_FILTER_TOOL_CLASS (klass);
tool_class->initialize = pika_offset_tool_initialize;
tool_class->control = pika_offset_tool_control;
tool_class->button_press = pika_offset_tool_button_press;
tool_class->button_release = pika_offset_tool_button_release;
tool_class->motion = pika_offset_tool_motion;
tool_class->oper_update = pika_offset_tool_oper_update;
tool_class->cursor_update = pika_offset_tool_cursor_update;
filter_tool_class->get_operation = pika_offset_tool_get_operation;
filter_tool_class->dialog = pika_offset_tool_dialog;
filter_tool_class->config_notify = pika_offset_tool_config_notify;
filter_tool_class->region_changed = pika_offset_tool_region_changed;
}
static void
pika_offset_tool_init (PikaOffsetTool *offset_tool)
{
PikaTool *tool = PIKA_TOOL (offset_tool);
pika_tool_control_set_scroll_lock (tool->control, TRUE);
pika_tool_control_set_precision (tool->control,
PIKA_CURSOR_PRECISION_PIXEL_CENTER);
}
static gboolean
pika_offset_tool_initialize (PikaTool *tool,
PikaDisplay *display,
GError **error)
{
PikaFilterTool *filter_tool = PIKA_FILTER_TOOL (tool);
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (tool);
PikaContext *context = PIKA_CONTEXT (PIKA_TOOL_GET_OPTIONS (tool));
PikaImage *image;
PikaDrawable *drawable;
gdouble xres;
gdouble yres;
if (! PIKA_TOOL_CLASS (parent_class)->initialize (tool, display, error))
return FALSE;
if (g_list_length (tool->drawables) != 1)
{
if (g_list_length (tool->drawables) > 1)
pika_tool_message_literal (tool, display,
_("Cannot modify multiple drawables. Select only one."));
else
pika_tool_message_literal (tool, display, _("No selected drawables."));
return FALSE;
}
drawable = tool->drawables->data;
image = pika_item_get_image (PIKA_ITEM (drawable));
pika_image_get_resolution (image, &xres, &yres);
g_signal_handlers_block_by_func (offset_tool->offset_se,
pika_offset_tool_offset_changed,
offset_tool);
pika_size_entry_set_resolution (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 0,
xres, FALSE);
pika_size_entry_set_resolution (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 1,
yres, FALSE);
if (PIKA_IS_LAYER (drawable))
pika_tool_gui_set_description (filter_tool->gui, _("Offset Layer"));
else if (PIKA_IS_LAYER_MASK (drawable))
pika_tool_gui_set_description (filter_tool->gui, _("Offset Layer Mask"));
else if (PIKA_IS_CHANNEL (drawable))
pika_tool_gui_set_description (filter_tool->gui, _("Offset Channel"));
else
g_warning ("%s: unexpected drawable type", G_STRFUNC);
gtk_widget_set_sensitive (offset_tool->transparent_radio,
pika_drawable_has_alpha (drawable));
g_signal_handlers_unblock_by_func (offset_tool->offset_se,
pika_offset_tool_offset_changed,
offset_tool);
gegl_node_set (
filter_tool->operation,
"context", context,
NULL);
g_signal_connect (context, "background-changed",
G_CALLBACK (pika_offset_tool_background_changed),
offset_tool);
pika_offset_tool_update (offset_tool);
return TRUE;
}
static void
pika_offset_tool_control (PikaTool *tool,
PikaToolAction action,
PikaDisplay *display)
{
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (tool);
switch (action)
{
case PIKA_TOOL_ACTION_PAUSE:
case PIKA_TOOL_ACTION_RESUME:
break;
case PIKA_TOOL_ACTION_HALT:
pika_offset_tool_halt (offset_tool);
break;
case PIKA_TOOL_ACTION_COMMIT:
break;
}
PIKA_TOOL_CLASS (parent_class)->control (tool, action, display);
}
static gchar *
pika_offset_tool_get_operation (PikaFilterTool *filter_tool,
gchar **description)
{
return g_strdup ("pika:offset");
}
static void
pika_offset_tool_button_press (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaButtonPressType press_type,
PikaDisplay *display)
{
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (tool);
offset_tool->dragging = ! pika_filter_tool_on_guide (PIKA_FILTER_TOOL (tool),
coords, display);
if (! offset_tool->dragging)
{
PIKA_TOOL_CLASS (parent_class)->button_press (tool, coords, time, state,
press_type, display);
}
else
{
offset_tool->x = coords->x;
offset_tool->y = coords->y;
g_object_get (PIKA_FILTER_TOOL (tool)->config,
"x", &offset_tool->offset_x,
"y", &offset_tool->offset_y,
NULL);
tool->display = display;
pika_tool_control_activate (tool->control);
pika_tool_pop_status (tool, display);
pika_tool_push_status_coords (tool, display,
PIKA_CURSOR_PRECISION_PIXEL_CENTER,
_("Offset: "),
0,
", ",
0,
NULL);
}
}
static void
pika_offset_tool_button_release (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaButtonReleaseType release_type,
PikaDisplay *display)
{
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (tool);
if (! offset_tool->dragging)
{
PIKA_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
release_type, display);
}
else
{
pika_tool_control_halt (tool->control);
offset_tool->dragging = FALSE;
if (release_type == PIKA_BUTTON_RELEASE_CANCEL)
{
g_object_set (PIKA_FILTER_TOOL (tool)->config,
"x", offset_tool->offset_x,
"y", offset_tool->offset_y,
NULL);
}
}
}
static void
pika_offset_tool_motion (PikaTool *tool,
const PikaCoords *coords,
guint32 time,
GdkModifierType state,
PikaDisplay *display)
{
PikaFilterTool *filter_tool = PIKA_FILTER_TOOL (tool);
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (tool);
if (! offset_tool->dragging)
{
PIKA_TOOL_CLASS (parent_class)->motion (tool, coords, time, state,
display);
}
else
{
PikaOffsetType type;
gint offset_x;
gint offset_y;
gint x;
gint y;
gint width;
gint height;
g_object_get (filter_tool->config,
"type", &type,
NULL);
offset_x = RINT (coords->x - offset_tool->x);
offset_y = RINT (coords->y - offset_tool->y);
x = offset_tool->offset_x + offset_x;
y = offset_tool->offset_y + offset_y;
width = pika_offset_tool_get_width (offset_tool);
height = pika_offset_tool_get_height (offset_tool);
if (type == PIKA_OFFSET_WRAP_AROUND)
{
x %= MAX (width, 1);
y %= MAX (height, 1);
}
else
{
x = CLAMP (x, -width, +width);
y = CLAMP (y, -height, +height);
}
g_object_set (filter_tool->config,
"x", x,
"y", y,
NULL);
pika_tool_pop_status (tool, display);
pika_tool_push_status_coords (tool, display,
PIKA_CURSOR_PRECISION_PIXEL_CENTER,
_("Offset: "),
offset_x,
", ",
offset_y,
NULL);
}
}
static void
pika_offset_tool_oper_update (PikaTool *tool,
const PikaCoords *coords,
GdkModifierType state,
gboolean proximity,
PikaDisplay *display)
{
if (! tool->drawables ||
pika_filter_tool_on_guide (PIKA_FILTER_TOOL (tool),
coords, display))
{
PIKA_TOOL_CLASS (parent_class)->oper_update (tool, coords, state,
proximity, display);
}
else
{
pika_tool_pop_status (tool, display);
pika_tool_push_status (tool, display, "%s",
_("Click-Drag to offset drawable"));
}
}
static void
pika_offset_tool_cursor_update (PikaTool *tool,
const PikaCoords *coords,
GdkModifierType state,
PikaDisplay *display)
{
if (! tool->drawables ||
pika_filter_tool_on_guide (PIKA_FILTER_TOOL (tool),
coords, display))
{
PIKA_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state,
display);
}
else
{
pika_tool_set_cursor (tool, display,
PIKA_CURSOR_MOUSE,
PIKA_TOOL_CURSOR_MOVE,
PIKA_CURSOR_MODIFIER_NONE);
}
}
static void
pika_offset_tool_dialog (PikaFilterTool *filter_tool)
{
PikaOffsetTool *offset_tool = PIKA_OFFSET_TOOL (filter_tool);
GtkWidget *main_vbox;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *button;
GtkWidget *spinbutton;
GtkWidget *frame;
GtkAdjustment *adjustment;
main_vbox = pika_filter_tool_dialog_get_vbox (filter_tool);
/* The offset frame */
frame = pika_frame_new (_("Offset"));
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
gtk_container_add (GTK_CONTAINER (frame), vbox);
gtk_widget_show (vbox);
adjustment = (GtkAdjustment *)
gtk_adjustment_new (1, 1, 1, 1, 10, 0);
spinbutton = pika_spin_button_new (adjustment, 1.0, 2);
gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 10);
offset_tool->offset_se = pika_size_entry_new (1, PIKA_UNIT_PIXEL, "%a",
TRUE, TRUE, FALSE, 10,
PIKA_SIZE_ENTRY_UPDATE_SIZE);
pika_size_entry_add_field (PIKA_SIZE_ENTRY (offset_tool->offset_se),
GTK_SPIN_BUTTON (spinbutton), NULL);
gtk_grid_attach (GTK_GRID (offset_tool->offset_se), spinbutton, 1, 0, 1, 1);
gtk_widget_show (spinbutton);
pika_size_entry_attach_label (PIKA_SIZE_ENTRY (offset_tool->offset_se),
_("_X:"), 0, 0, 0.0);
pika_size_entry_attach_label (PIKA_SIZE_ENTRY (offset_tool->offset_se),
_("_Y:"), 1, 0, 0.0);
gtk_box_pack_start (GTK_BOX (vbox), offset_tool->offset_se, FALSE, FALSE, 0);
gtk_widget_show (offset_tool->offset_se);
pika_size_entry_set_unit (PIKA_SIZE_ENTRY (offset_tool->offset_se),
PIKA_UNIT_PIXEL);
g_signal_connect (offset_tool->offset_se, "refval-changed",
G_CALLBACK (pika_offset_tool_offset_changed),
offset_tool);
g_signal_connect (offset_tool->offset_se, "value-changed",
G_CALLBACK (pika_offset_tool_offset_changed),
offset_tool);
button = gtk_button_new_with_mnemonic (_("By width/_2, height/2"));
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
gtk_widget_show (button);
g_signal_connect (button, "clicked",
G_CALLBACK (pika_offset_tool_half_xy_clicked),
offset_tool);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
button = gtk_button_new_with_mnemonic (_("By _width/2"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_show (button);
g_signal_connect (button, "clicked",
G_CALLBACK (pika_offset_tool_half_x_clicked),
offset_tool);
button = gtk_button_new_with_mnemonic (_("By _height/2"));
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_widget_show (button);
g_signal_connect (button, "clicked",
G_CALLBACK (pika_offset_tool_half_y_clicked),
offset_tool);
/* The edge behavior frame */
frame = pika_int_radio_group_new (TRUE, _("Edge Behavior"),
G_CALLBACK (pika_offset_tool_edge_behavior_toggled),
offset_tool, NULL,
PIKA_OFFSET_WRAP_AROUND,
_("W_rap around"),
PIKA_OFFSET_WRAP_AROUND, NULL,
_("Fill with _background color"),
PIKA_OFFSET_BACKGROUND, NULL,
_("Make _transparent"),
PIKA_OFFSET_TRANSPARENT,
&offset_tool->transparent_radio,
NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame);
}
static void
pika_offset_tool_config_notify (PikaFilterTool *filter_tool,
PikaConfig *config,
const GParamSpec *pspec)
{
pika_offset_tool_update (PIKA_OFFSET_TOOL (filter_tool));
PIKA_FILTER_TOOL_CLASS (parent_class)->config_notify (filter_tool,
config, pspec);
}
static void
pika_offset_tool_region_changed (PikaFilterTool *filter_tool)
{
pika_offset_tool_update (PIKA_OFFSET_TOOL (filter_tool));
}
static void
pika_offset_tool_offset_changed (PikaSizeEntry *se,
PikaOffsetTool *offset_tool)
{
g_object_set (PIKA_FILTER_TOOL (offset_tool)->config,
"x", (gint) pika_size_entry_get_refval (se, 0),
"y", (gint) pika_size_entry_get_refval (se, 1),
NULL);
}
static void
pika_offset_tool_half_xy_clicked (GtkButton *button,
PikaOffsetTool *offset_tool)
{
g_object_set (PIKA_FILTER_TOOL (offset_tool)->config,
"x", pika_offset_tool_get_width (offset_tool) / 2,
"y", pika_offset_tool_get_height (offset_tool) / 2,
NULL);
}
static void
pika_offset_tool_half_x_clicked (GtkButton *button,
PikaOffsetTool *offset_tool)
{
g_object_set (PIKA_FILTER_TOOL (offset_tool)->config,
"x", pika_offset_tool_get_width (offset_tool) / 2,
NULL);
}
static void
pika_offset_tool_half_y_clicked (GtkButton *button,
PikaOffsetTool *offset_tool)
{
g_object_set (PIKA_FILTER_TOOL (offset_tool)->config,
"y", pika_offset_tool_get_height (offset_tool) / 2,
NULL);
}
static void
pika_offset_tool_edge_behavior_toggled (GtkToggleButton *toggle,
PikaOffsetTool *offset_tool)
{
if (gtk_toggle_button_get_active (toggle))
{
PikaOffsetType type;
type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toggle),
"pika-item-data"));
g_object_set (PIKA_FILTER_TOOL (offset_tool)->config,
"type", type,
NULL);
}
}
static void
pika_offset_tool_background_changed (PikaContext *context,
const PikaRGB *color,
PikaOffsetTool *offset_tool)
{
PikaFilterTool *filter_tool = PIKA_FILTER_TOOL (offset_tool);
PikaOffsetType type;
g_object_get (filter_tool->config,
"type", &type,
NULL);
if (type == PIKA_OFFSET_BACKGROUND)
{
gegl_node_set (filter_tool->operation,
"context", context,
NULL);
pika_drawable_filter_apply (filter_tool->filter, NULL);
}
}
static gint
pika_offset_tool_get_width (PikaOffsetTool *offset_tool)
{
GeglRectangle drawable_area;
gint drawable_offset_x;
gint drawable_offset_y;
if (pika_filter_tool_get_drawable_area (PIKA_FILTER_TOOL (offset_tool),
&drawable_offset_x,
&drawable_offset_y,
&drawable_area) &&
! gegl_rectangle_is_empty (&drawable_area))
{
return drawable_area.width;
}
return 0;
}
static gint
pika_offset_tool_get_height (PikaOffsetTool *offset_tool)
{
GeglRectangle drawable_area;
gint drawable_offset_x;
gint drawable_offset_y;
if (pika_filter_tool_get_drawable_area (PIKA_FILTER_TOOL (offset_tool),
&drawable_offset_x,
&drawable_offset_y,
&drawable_area) &&
! gegl_rectangle_is_empty (&drawable_area))
{
return drawable_area.height;
}
return 0;
}
static void
pika_offset_tool_update (PikaOffsetTool *offset_tool)
{
PikaTool *tool = PIKA_TOOL (offset_tool);
PikaFilterTool *filter_tool = PIKA_FILTER_TOOL (offset_tool);
PikaOffsetType orig_type;
gint orig_x;
gint orig_y;
PikaOffsetType type;
gint x;
gint y;
gint width;
gint height;
g_object_get (filter_tool->config,
"type", &orig_type,
"x", &orig_x,
"y", &orig_y,
NULL);
width = pika_offset_tool_get_width (offset_tool);
height = pika_offset_tool_get_height (offset_tool);
x = CLAMP (orig_x, -width, +width);
y = CLAMP (orig_y, -height, +height);
type = orig_type;
if (tool->drawables &&
! pika_drawable_has_alpha (tool->drawables->data) &&
type == PIKA_OFFSET_TRANSPARENT)
{
type = PIKA_OFFSET_BACKGROUND;
}
if (x != orig_x ||
y != orig_y ||
type != orig_type)
{
g_object_set (filter_tool->config,
"type", type,
"x", x,
"y", y,
NULL);
}
if (offset_tool->offset_se)
{
gint width = pika_offset_tool_get_width (offset_tool);
gint height = pika_offset_tool_get_height (offset_tool);
g_signal_handlers_block_by_func (offset_tool->offset_se,
pika_offset_tool_offset_changed,
offset_tool);
pika_size_entry_set_refval_boundaries (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 0,
-width, +width);
pika_size_entry_set_refval_boundaries (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 1,
-height, +height);
pika_size_entry_set_size (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 0,
0, width);
pika_size_entry_set_size (
PIKA_SIZE_ENTRY (offset_tool->offset_se), 1,
0, height);
pika_size_entry_set_refval (PIKA_SIZE_ENTRY (offset_tool->offset_se), 0,
x);
pika_size_entry_set_refval (PIKA_SIZE_ENTRY (offset_tool->offset_se), 1,
y);
g_signal_handlers_unblock_by_func (offset_tool->offset_se,
pika_offset_tool_offset_changed,
offset_tool);
}
if (offset_tool->transparent_radio)
{
pika_int_radio_group_set_active (
GTK_RADIO_BUTTON (offset_tool->transparent_radio),
type);
}
}
static void
pika_offset_tool_halt (PikaOffsetTool *offset_tool)
{
PikaContext *context = PIKA_CONTEXT (PIKA_TOOL_GET_OPTIONS (offset_tool));
offset_tool->offset_se = NULL;
offset_tool->transparent_radio = NULL;
g_signal_handlers_disconnect_by_func (
context,
pika_offset_tool_background_changed,
offset_tool);
}