/* 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
 *
 * PikaForegroundSelectTool
 * Copyright (C) 2005  Sven Neumann <sven@gimp.org>
 *
 * 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 <gdk/gdkkeysyms.h>

#include "libpikamath/pikamath.h"
#include "libpikabase/pikabase.h"
#include "libpikacolor/pikacolor.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "config/pikaguiconfig.h"

#include "gegl/pika-gegl-loops.h"
#include "gegl/pika-gegl-mask.h"
#include "gegl/pika-gegl-utils.h"

#include "core/pika.h"
#include "core/pikachannel-select.h"
#include "core/pikadrawable-foreground-extract.h"
#include "core/pikaerror.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikalayermask.h"
#include "core/pikaprogress.h"
#include "core/pikascanconvert.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikacanvasitem.h"
#include "display/pikacanvasbufferpreview.h"
#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"
#include "display/pikatoolgui.h"

#include "pikaforegroundselecttool.h"
#include "pikaforegroundselectoptions.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


#define FAR_OUTSIDE -10000


typedef struct _StrokeUndo StrokeUndo;

struct _StrokeUndo
{
  GeglBuffer          *saved_trimap;
  gint                 trimap_x;
  gint                 trimap_y;
  PikaMattingDrawMode  draw_mode;
  gint                 stroke_width;
};


static void   pika_foreground_select_tool_finalize       (GObject          *object);

static gboolean  pika_foreground_select_tool_initialize  (PikaTool         *tool,
                                                          PikaDisplay      *display,
                                                          GError          **error);
static void   pika_foreground_select_tool_control        (PikaTool         *tool,
                                                          PikaToolAction    action,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_button_press   (PikaTool         *tool,
                                                          const PikaCoords *coords,
                                                          guint32           time,
                                                          GdkModifierType   state,
                                                          PikaButtonPressType press_type,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_button_release (PikaTool         *tool,
                                                          const PikaCoords *coords,
                                                          guint32           time,
                                                          GdkModifierType   state,
                                                          PikaButtonReleaseType release_type,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_motion         (PikaTool         *tool,
                                                          const PikaCoords *coords,
                                                          guint32           time,
                                                          GdkModifierType   state,
                                                          PikaDisplay      *display);
static gboolean  pika_foreground_select_tool_key_press   (PikaTool         *tool,
                                                          GdkEventKey      *kevent,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_modifier_key   (PikaTool         *tool,
                                                          GdkModifierType   key,
                                                          gboolean          press,
                                                          GdkModifierType   state,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_active_modifier_key
                                                         (PikaTool         *tool,
                                                          GdkModifierType   key,
                                                          gboolean          press,
                                                          GdkModifierType   state,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_oper_update    (PikaTool         *tool,
                                                          const PikaCoords *coords,
                                                          GdkModifierType   state,
                                                          gboolean          proximity,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_cursor_update  (PikaTool         *tool,
                                                          const PikaCoords *coords,
                                                          GdkModifierType   state,
                                                          PikaDisplay      *display);
static const gchar * pika_foreground_select_tool_can_undo
                                                         (PikaTool         *tool,
                                                          PikaDisplay      *display);
static const gchar * pika_foreground_select_tool_can_redo
                                                         (PikaTool         *tool,
                                                          PikaDisplay      *display);
static gboolean   pika_foreground_select_tool_undo       (PikaTool         *tool,
                                                          PikaDisplay      *display);
static gboolean   pika_foreground_select_tool_redo       (PikaTool         *tool,
                                                          PikaDisplay      *display);
static void   pika_foreground_select_tool_options_notify (PikaTool         *tool,
                                                          PikaToolOptions  *options,
                                                          const GParamSpec *pspec);

static void   pika_foreground_select_tool_draw           (PikaDrawTool     *draw_tool);

static void   pika_foreground_select_tool_confirm        (PikaPolygonSelectTool *poly_sel,
                                                          PikaDisplay           *display);

static void   pika_foreground_select_tool_halt           (PikaForegroundSelectTool *fg_select);
static void   pika_foreground_select_tool_commit         (PikaForegroundSelectTool *fg_select);

static void   pika_foreground_select_tool_set_trimap     (PikaForegroundSelectTool *fg_select);
static void   pika_foreground_select_tool_set_preview    (PikaForegroundSelectTool *fg_select);
static void   pika_foreground_select_tool_preview        (PikaForegroundSelectTool *fg_select);

static void   pika_foreground_select_tool_stroke_paint   (PikaForegroundSelectTool *fg_select);
static void   pika_foreground_select_tool_cancel_paint   (PikaForegroundSelectTool *fg_select);

static void   pika_foreground_select_tool_response       (PikaToolGui              *gui,
                                                          gint                      response_id,
                                                          PikaForegroundSelectTool *fg_select);
static void   pika_foreground_select_tool_preview_toggled(GtkToggleButton          *button,
                                                          PikaForegroundSelectTool *fg_select);

static void   pika_foreground_select_tool_update_gui     (PikaForegroundSelectTool *fg_select);

static StrokeUndo * pika_foreground_select_undo_new      (GeglBuffer               *trimap,
                                                          GArray                   *stroke,
                                                          PikaMattingDrawMode       draw_mode,
                                                          gint                      stroke_width);
static void         pika_foreground_select_undo_pop      (StrokeUndo               *undo,
                                                          GeglBuffer               *trimap);
static void         pika_foreground_select_undo_free     (StrokeUndo               *undo);


G_DEFINE_TYPE (PikaForegroundSelectTool, pika_foreground_select_tool,
               PIKA_TYPE_POLYGON_SELECT_TOOL)

#define parent_class pika_foreground_select_tool_parent_class


void
pika_foreground_select_tool_register (PikaToolRegisterCallback  callback,
                                      gpointer                  data)
{
  (* callback) (PIKA_TYPE_FOREGROUND_SELECT_TOOL,
                PIKA_TYPE_FOREGROUND_SELECT_OPTIONS,
                pika_foreground_select_options_gui,
                PIKA_CONTEXT_PROP_MASK_FOREGROUND |
                PIKA_CONTEXT_PROP_MASK_BACKGROUND,
                "pika-foreground-select-tool",
                _("Foreground Select"),
                _("Foreground Select Tool: Select a region containing foreground objects"),
                N_("F_oreground Select"), NULL,
                NULL, PIKA_HELP_TOOL_FOREGROUND_SELECT,
                PIKA_ICON_TOOL_FOREGROUND_SELECT,
                data);
}

static void
pika_foreground_select_tool_class_init (PikaForegroundSelectToolClass *klass)
{
  GObjectClass               *object_class    = G_OBJECT_CLASS (klass);
  PikaToolClass              *tool_class      = PIKA_TOOL_CLASS (klass);
  PikaDrawToolClass          *draw_tool_class = PIKA_DRAW_TOOL_CLASS (klass);
  PikaPolygonSelectToolClass *polygon_select_tool_class;

  polygon_select_tool_class = PIKA_POLYGON_SELECT_TOOL_CLASS (klass);

  object_class->finalize             = pika_foreground_select_tool_finalize;

  tool_class->initialize             = pika_foreground_select_tool_initialize;
  tool_class->control                = pika_foreground_select_tool_control;
  tool_class->button_press           = pika_foreground_select_tool_button_press;
  tool_class->button_release         = pika_foreground_select_tool_button_release;
  tool_class->motion                 = pika_foreground_select_tool_motion;
  tool_class->key_press              = pika_foreground_select_tool_key_press;
  tool_class->modifier_key           = pika_foreground_select_tool_modifier_key;
  tool_class->active_modifier_key    = pika_foreground_select_tool_active_modifier_key;
  tool_class->oper_update            = pika_foreground_select_tool_oper_update;
  tool_class->cursor_update          = pika_foreground_select_tool_cursor_update;
  tool_class->can_undo               = pika_foreground_select_tool_can_undo;
  tool_class->can_redo               = pika_foreground_select_tool_can_redo;
  tool_class->undo                   = pika_foreground_select_tool_undo;
  tool_class->redo                   = pika_foreground_select_tool_redo;
  tool_class->options_notify         = pika_foreground_select_tool_options_notify;

  draw_tool_class->draw              = pika_foreground_select_tool_draw;

  polygon_select_tool_class->confirm = pika_foreground_select_tool_confirm;
}

static void
pika_foreground_select_tool_init (PikaForegroundSelectTool *fg_select)
{
  PikaTool *tool = PIKA_TOOL (fg_select);

  pika_tool_control_set_motion_mode (tool->control, PIKA_MOTION_MODE_EXACT);
  pika_tool_control_set_scroll_lock (tool->control, FALSE);
  pika_tool_control_set_preserve    (tool->control, FALSE);
  pika_tool_control_set_dirty_mask  (tool->control,
                                     PIKA_DIRTY_IMAGE_SIZE |
                                     PIKA_DIRTY_ACTIVE_DRAWABLE);
  pika_tool_control_set_precision   (tool->control,
                                     PIKA_CURSOR_PRECISION_SUBPIXEL);
  pika_tool_control_set_tool_cursor (tool->control,
                                     PIKA_TOOL_CURSOR_FREE_SELECT);

  pika_tool_control_set_action_size (tool->control,
                                     "tools-foreground-select-brush-size-set");

  fg_select->state = MATTING_STATE_FREE_SELECT;
  fg_select->grayscale_preview = NULL;
}

static void
pika_foreground_select_tool_finalize (GObject *object)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (object);

  g_clear_object (&fg_select->gui);
  fg_select->preview_toggle = NULL;

  if (fg_select->stroke)
    g_warning ("%s: stroke should be NULL at this point", G_STRLOC);

  if (fg_select->mask)
    g_warning ("%s: mask should be NULL at this point", G_STRLOC);

  if (fg_select->trimap)
    g_warning ("%s: mask should be NULL at this point", G_STRLOC);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
pika_foreground_select_tool_initialize (PikaTool     *tool,
                                        PikaDisplay  *display,
                                        GError      **error)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  PikaGuiConfig            *config    = PIKA_GUI_CONFIG (display->pika->config);
  PikaImage                *image     = pika_display_get_image (display);
  PikaDisplayShell         *shell     = pika_display_get_shell (display);
  GList                    *drawables = pika_image_get_selected_drawables (image);
  PikaDrawable             *drawable;

  if (g_list_length (drawables) != 1)
    {
      if (g_list_length (drawables) > 1)
        g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                             _("Cannot select from multiple layers."));
      else
        g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED, _("No selected drawables."));

      g_list_free (drawables);
      return FALSE;
    }
  drawable = drawables->data;
  g_list_free (drawables);

  if (! pika_item_is_visible (PIKA_ITEM (drawable)) &&
      ! config->edit_non_visible)
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("The active layer is not visible."));
      return FALSE;
    }

  tool->display = display;

  /*  enable double click for the FreeSelectTool, because it may have been
   *  disabled if the tool has switched to MATTING_STATE_PAINT_TRIMAP,
   *  in pika_foreground_select_tool_set_trimap().
   */
  pika_tool_control_set_wants_double_click (tool->control, TRUE);

  fg_select->state = MATTING_STATE_FREE_SELECT;

  if (! fg_select->gui)
    {
      fg_select->gui =
        pika_tool_gui_new (tool->tool_info,
                           NULL,
                           _("Dialog for foreground select"),
                           NULL, NULL,
                           pika_widget_get_monitor (GTK_WIDGET (shell)),
                           TRUE,

                           _("_Cancel"), GTK_RESPONSE_CANCEL,
                           _("_Select"), GTK_RESPONSE_APPLY,

                           NULL);

      pika_tool_gui_set_auto_overlay (fg_select->gui, TRUE);

      g_signal_connect (fg_select->gui, "response",
                        G_CALLBACK (pika_foreground_select_tool_response),
                        fg_select);

      fg_select->preview_toggle =
        gtk_check_button_new_with_mnemonic (_("_Preview mask"));
      gtk_box_pack_start (GTK_BOX (pika_tool_gui_get_vbox (fg_select->gui)),
                          fg_select->preview_toggle, FALSE, FALSE, 0);
      gtk_widget_show (fg_select->preview_toggle);

      g_signal_connect (fg_select->preview_toggle, "toggled",
                        G_CALLBACK (pika_foreground_select_tool_preview_toggled),
                        fg_select);
    }

  pika_tool_gui_set_description (fg_select->gui,
                                 _("Select foreground pixels"));

  pika_tool_gui_set_response_sensitive (fg_select->gui, GTK_RESPONSE_APPLY,
                                        FALSE);
  gtk_widget_set_sensitive (fg_select->preview_toggle, FALSE);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fg_select->preview_toggle),
                                FALSE);

  pika_tool_gui_set_shell (fg_select->gui, shell);
  pika_tool_gui_set_viewable (fg_select->gui, PIKA_VIEWABLE (drawable));

  pika_tool_gui_show (fg_select->gui);

  return TRUE;
}

static void
pika_foreground_select_tool_control (PikaTool       *tool,
                                     PikaToolAction  action,
                                     PikaDisplay    *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  switch (action)
    {
    case PIKA_TOOL_ACTION_PAUSE:
    case PIKA_TOOL_ACTION_RESUME:
      break;

    case PIKA_TOOL_ACTION_HALT:
      pika_foreground_select_tool_halt (fg_select);
      break;

    case PIKA_TOOL_ACTION_COMMIT:
      pika_foreground_select_tool_commit (fg_select);
      break;
    }

  PIKA_TOOL_CLASS (parent_class)->control (tool, action, display);
}

static void
pika_foreground_select_tool_button_press (PikaTool            *tool,
                                          const PikaCoords    *coords,
                                          guint32              time,
                                          GdkModifierType      state,
                                          PikaButtonPressType  press_type,
                                          PikaDisplay         *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  PikaDrawTool             *draw_tool = PIKA_DRAW_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_TOOL_CLASS (parent_class)->button_press (tool, coords, time, state,
                                                    press_type, display);
    }
  else
    {
      PikaVector2 point = pika_vector2_new (coords->x, coords->y);

      pika_draw_tool_pause (draw_tool);

      if (pika_draw_tool_is_active (draw_tool) && draw_tool->display != display)
        pika_draw_tool_stop (draw_tool);

      pika_tool_control_activate (tool->control);

      fg_select->last_coords = *coords;

      g_return_if_fail (fg_select->stroke == NULL);
      fg_select->stroke = g_array_new (FALSE, FALSE, sizeof (PikaVector2));

      g_array_append_val (fg_select->stroke, point);

      if (! pika_draw_tool_is_active (draw_tool))
        pika_draw_tool_start (draw_tool, display);

      pika_draw_tool_resume (draw_tool);
    }
}

static void
pika_foreground_select_tool_button_release (PikaTool              *tool,
                                            const PikaCoords      *coords,
                                            guint32                time,
                                            GdkModifierType        state,
                                            PikaButtonReleaseType  release_type,
                                            PikaDisplay           *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                      release_type, display);
    }
  else
    {
      pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));

      pika_tool_control_halt (tool->control);

      if (release_type == PIKA_BUTTON_RELEASE_CANCEL)
        {
          pika_foreground_select_tool_cancel_paint (fg_select);
        }
      else
        {
          pika_foreground_select_tool_stroke_paint (fg_select);

          if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
            pika_foreground_select_tool_preview (fg_select);
          else
            pika_foreground_select_tool_set_trimap (fg_select);
        }

      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }
}

static void
pika_foreground_select_tool_motion (PikaTool         *tool,
                                    const PikaCoords *coords,
                                    guint32           time,
                                    GdkModifierType   state,
                                    PikaDisplay      *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_TOOL_CLASS (parent_class)->motion (tool, coords, time, state,
                                              display);
    }
  else
    {
      PikaVector2 *last = &g_array_index (fg_select->stroke,
                                          PikaVector2,
                                          fg_select->stroke->len - 1);

      pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));

      fg_select->last_coords = *coords;

      if (last->x != (gint) coords->x || last->y != (gint) coords->y)
        {
          PikaVector2 point = pika_vector2_new (coords->x, coords->y);

          g_array_append_val (fg_select->stroke, point);
        }

      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }
}

static gboolean
pika_foreground_select_tool_key_press (PikaTool    *tool,
                                       GdkEventKey *kevent,
                                       PikaDisplay *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      return PIKA_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
    }
  else
    {
      if (display != tool->display)
        return FALSE;

      switch (kevent->keyval)
        {
        case GDK_KEY_Return:
        case GDK_KEY_KP_Enter:
        case GDK_KEY_ISO_Enter:
          if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fg_select->preview_toggle),
                                          TRUE);
          else
            pika_foreground_select_tool_response (fg_select->gui,
                                                  GTK_RESPONSE_APPLY, fg_select);
          return TRUE;

        case GDK_KEY_Escape:
          if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
            pika_foreground_select_tool_response (fg_select->gui,
                                                  GTK_RESPONSE_CANCEL, fg_select);
          else
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fg_select->preview_toggle),
                                          FALSE);
          return TRUE;

        default:
          return FALSE;
        }
    }
}

static void
pika_foreground_select_tool_modifier_key (PikaTool        *tool,
                                          GdkModifierType  key,
                                          gboolean         press,
                                          GdkModifierType  state,
                                          PikaDisplay     *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_TOOL_CLASS (parent_class)->modifier_key (tool, key, press, state,
                                                    display);
    }
  else
    {
#if 0
      if (key == pika_get_toggle_behavior_mask ())
        {
          PikaForegroundSelectOptions *options;

          options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);

          g_object_set (options,
                        "background", ! options->background,
                        NULL);
        }
#endif
    }
}

static void
pika_foreground_select_tool_active_modifier_key (PikaTool        *tool,
                                                 GdkModifierType  key,
                                                 gboolean         press,
                                                 GdkModifierType  state,
                                                 PikaDisplay     *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_TOOL_CLASS (parent_class)->active_modifier_key (tool, key, press,
                                                           state, display);
    }
}

static void
pika_foreground_select_tool_oper_update (PikaTool         *tool,
                                         const PikaCoords *coords,
                                         GdkModifierType   state,
                                         gboolean          proximity,
                                         PikaDisplay      *display)
{
  PikaForegroundSelectTool    *fg_select    = PIKA_FOREGROUND_SELECT_TOOL (tool);
  PikaForegroundSelectOptions *options;
  const gchar                 *status_stage = NULL;
  const gchar                 *status_mode  = NULL;

  options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (fg_select);

  PIKA_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                               display);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      if (PIKA_SELECTION_TOOL (tool)->function == SELECTION_SELECT)
        {
          gint n_points;

          pika_polygon_select_tool_get_points (PIKA_POLYGON_SELECT_TOOL (tool),
                                               NULL, &n_points);

          if (n_points > 2)
            {
              status_mode = _("Roughly outline the object to extract");
              status_stage = _("press Enter to refine.");
            }
          else
            {
              status_stage = _("Roughly outline the object to extract");
            }
        }
    }
  else
    {
      PikaDrawTool *draw_tool = PIKA_DRAW_TOOL (tool);

      pika_draw_tool_pause (draw_tool);

      if (proximity)
        {
          fg_select->last_coords = *coords;
        }
      else
        {
          fg_select->last_coords.x = FAR_OUTSIDE;
          fg_select->last_coords.y = FAR_OUTSIDE;
        }

      pika_draw_tool_resume (draw_tool);

      if (options->draw_mode == PIKA_MATTING_DRAW_MODE_FOREGROUND)
        status_mode = _("Selecting foreground");
      else if (options->draw_mode == PIKA_MATTING_DRAW_MODE_BACKGROUND)
        status_mode = _("Selecting background");
      else
        status_mode = _("Selecting unknown");

      if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
        status_stage = _("press Enter to preview.");
      else
        status_stage = _("press Escape to exit preview or Enter to apply.");
    }

  if (proximity && status_stage)
    {
      if (status_mode)
        pika_tool_replace_status (tool, display, "%s, %s", status_mode, status_stage);
      else
        pika_tool_replace_status (tool, display, "%s", status_stage);
    }
}

static void
pika_foreground_select_tool_cursor_update (PikaTool         *tool,
                                           const PikaCoords *coords,
                                           GdkModifierType   state,
                                           PikaDisplay      *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
    {
      switch (PIKA_SELECTION_TOOL (tool)->function)
        {
        case SELECTION_MOVE_MASK:
        case SELECTION_MOVE:
        case SELECTION_MOVE_COPY:
        case SELECTION_ANCHOR:
          return;
        default:
          break;
        }
    }

  PIKA_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}

static const gchar *
pika_foreground_select_tool_can_undo (PikaTool    *tool,
                                      PikaDisplay *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->undo_stack)
    {
      StrokeUndo  *undo = fg_select->undo_stack->data;
      const gchar *desc;

      if (pika_enum_get_value (PIKA_TYPE_MATTING_DRAW_MODE, undo->draw_mode,
                               NULL, NULL, &desc, NULL))
        {
          return desc;
        }
    }

  return NULL;
}

static const gchar *
pika_foreground_select_tool_can_redo (PikaTool    *tool,
                                      PikaDisplay *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);

  if (fg_select->redo_stack)
    {
      StrokeUndo  *undo = fg_select->redo_stack->data;
      const gchar *desc;

      if (pika_enum_get_value (PIKA_TYPE_MATTING_DRAW_MODE, undo->draw_mode,
                               NULL, NULL, &desc, NULL))
        {
          return desc;
        }
    }

  return NULL;
}

static gboolean
pika_foreground_select_tool_undo (PikaTool    *tool,
                                  PikaDisplay *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  StrokeUndo               *undo      = fg_select->undo_stack->data;

  pika_foreground_select_undo_pop (undo, fg_select->trimap);

  fg_select->undo_stack = g_list_remove (fg_select->undo_stack, undo);
  fg_select->redo_stack = g_list_prepend (fg_select->redo_stack, undo);

  if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
    pika_foreground_select_tool_preview (fg_select);
  else
    pika_foreground_select_tool_set_trimap (fg_select);

  return TRUE;
}

static gboolean
pika_foreground_select_tool_redo (PikaTool    *tool,
                                  PikaDisplay *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  StrokeUndo               *undo      = fg_select->redo_stack->data;

  pika_foreground_select_undo_pop (undo, fg_select->trimap);

  fg_select->redo_stack = g_list_remove (fg_select->redo_stack, undo);
  fg_select->undo_stack = g_list_prepend (fg_select->undo_stack, undo);

  if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
    pika_foreground_select_tool_preview (fg_select);
  else
    pika_foreground_select_tool_set_trimap (fg_select);

  return TRUE;
}

static void
pika_foreground_select_tool_options_notify (PikaTool         *tool,
                                            PikaToolOptions  *options,
                                            const GParamSpec *pspec)
{
  PikaForegroundSelectTool    *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  PikaForegroundSelectOptions *fg_options;

  fg_options = PIKA_FOREGROUND_SELECT_OPTIONS (options);

  if (! tool->display)
    return;

  if (! strcmp (pspec->name, "mask-color") ||
      ! strcmp (pspec->name, "preview-mode"))
    {
      if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
        {
          pika_foreground_select_tool_set_trimap (fg_select);
        }
      else if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
        {
          pika_foreground_select_tool_set_preview (fg_select);
        }
    }
  else if (! strcmp (pspec->name, "engine"))
    {
      if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
        {
          pika_foreground_select_tool_preview (fg_select);
        }
    }
  else if (! strcmp (pspec->name, "iterations"))
    {
      if (fg_options->engine == PIKA_MATTING_ENGINE_GLOBAL &&
          fg_select->state   == MATTING_STATE_PREVIEW_MASK)
        {
          pika_foreground_select_tool_preview (fg_select);
        }
    }
  else if (! strcmp (pspec->name, "levels") ||
           ! strcmp (pspec->name, "active-levels"))
    {
      if (fg_options->engine == PIKA_MATTING_ENGINE_LEVIN &&
          fg_select->state   == MATTING_STATE_PREVIEW_MASK)
        {
          pika_foreground_select_tool_preview (fg_select);
        }
    }
}

static void
pika_foreground_select_tool_get_area (GeglBuffer *mask,
                                      gint       *x1,
                                      gint       *y1,
                                      gint       *x2,
                                      gint       *y2)
{
  gint width;
  gint height;

  pika_gegl_mask_bounds (mask, x1, y1, x2, y2);

  width  = *x2 - *x1;
  height = *y2 - *y1;

  *x1 = MAX (*x1 - width  / 2, 0);
  *y1 = MAX (*y1 - height / 2, 0);
  *x2 = MIN (*x2 + width  / 2, pika_item_get_width  (PIKA_ITEM (mask)));
  *y2 = MIN (*y2 + height / 2, pika_item_get_height (PIKA_ITEM (mask)));
}

static void
pika_foreground_select_tool_draw (PikaDrawTool *draw_tool)
{
  PikaTool                    *tool      = PIKA_TOOL (draw_tool);
  PikaForegroundSelectTool    *fg_select = PIKA_FOREGROUND_SELECT_TOOL (tool);
  PikaForegroundSelectOptions *options;

  options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);

  if (fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PIKA_DRAW_TOOL_CLASS (parent_class)->draw (draw_tool);
      return;
    }
  else
    {
      gint    x      = fg_select->last_coords.x;
      gint    y      = fg_select->last_coords.y;
      gdouble radius = options->stroke_width / 2.0f;

      if (fg_select->stroke)
        {
          PikaDisplayShell *shell = pika_display_get_shell (draw_tool->display);

          pika_draw_tool_add_pen (draw_tool,
                                  (const PikaVector2 *) fg_select->stroke->data,
                                  fg_select->stroke->len,
                                  PIKA_CONTEXT (options),
                                  PIKA_ACTIVE_COLOR_FOREGROUND,
                                  options->stroke_width * shell->scale_y);
        }

      /*  warn if the user is drawing outside of the working area  */
      if (FALSE)
        {
          gint x1, y1;
          gint x2, y2;

          pika_foreground_select_tool_get_area (fg_select->mask,
                                                &x1, &y1, &x2, &y2);

          if (x < x1 + radius || x > x2 - radius ||
              y < y1 + radius || y > y2 - radius)
            {
              pika_draw_tool_add_rectangle (draw_tool, FALSE,
                                            x1, y1,
                                            x2 - x1, y2 - y1);
            }
        }

      if (x > FAR_OUTSIDE && y > FAR_OUTSIDE)
        pika_draw_tool_add_arc (draw_tool, FALSE,
                                x - radius, y - radius,
                                2 * radius, 2 * radius,
                                0.0, 2.0 * G_PI);

      if (fg_select->grayscale_preview)
        pika_draw_tool_add_preview (draw_tool, fg_select->grayscale_preview);
    }
}

static void
pika_foreground_select_tool_confirm (PikaPolygonSelectTool *poly_sel,
                                     PikaDisplay           *display)
{
  PikaForegroundSelectTool *fg_select = PIKA_FOREGROUND_SELECT_TOOL (poly_sel);
  PikaImage                *image     = pika_display_get_image (display);
  GList                    *drawables = pika_image_get_selected_drawables (image);
  PikaDrawable             *drawable;
  PikaItem                 *item;

  g_return_if_fail (g_list_length (drawables) == 1);

  drawable = drawables->data;
  item     = PIKA_ITEM (drawable);
  g_list_free (drawables);

  if (drawable && fg_select->state == MATTING_STATE_FREE_SELECT)
    {
      PikaScanConvert   *scan_convert = pika_scan_convert_new ();
      const PikaVector2 *points;
      gint               n_points;

      pika_polygon_select_tool_get_points (poly_sel, &points, &n_points);

      pika_scan_convert_add_polyline (scan_convert, n_points, points, TRUE);

      fg_select->trimap =
        gegl_buffer_new (GEGL_RECTANGLE (pika_item_get_offset_x (item),
                                         pika_item_get_offset_y (item),
                                         pika_item_get_width    (item),
                                         pika_item_get_height   (item)),
                         pika_image_get_mask_format (image));

      pika_scan_convert_render_value (scan_convert, fg_select->trimap,
                                      0, 0, 0.5);
      pika_scan_convert_free (scan_convert);

      fg_select->grayscale_preview =
          pika_canvas_buffer_preview_new (pika_display_get_shell (display),
                                          fg_select->trimap);

      pika_foreground_select_tool_set_trimap (fg_select);
    }
}

static void
pika_foreground_select_tool_halt (PikaForegroundSelectTool *fg_select)
{
  PikaTool     *tool = PIKA_TOOL (fg_select);
  PikaDrawTool *draw_tool = PIKA_DRAW_TOOL (fg_select);

  if (draw_tool->preview)
    {
      pika_draw_tool_remove_preview (draw_tool, fg_select->grayscale_preview);
    }

  g_clear_object (&fg_select->grayscale_preview);
  g_clear_object (&fg_select->trimap);
  g_clear_object (&fg_select->mask);

  if (fg_select->undo_stack)
    {
      g_list_free_full (fg_select->undo_stack,
                        (GDestroyNotify) pika_foreground_select_undo_free);
      fg_select->undo_stack = NULL;
    }

  if (fg_select->redo_stack)
    {
      g_list_free_full (fg_select->redo_stack,
                        (GDestroyNotify) pika_foreground_select_undo_free);
      fg_select->redo_stack = NULL;
    }

  if (tool->display)
    pika_display_shell_set_mask (pika_display_get_shell (tool->display),
                                 NULL, 0, 0, NULL, FALSE);

  pika_tool_control_set_tool_cursor        (tool->control,
                                            PIKA_TOOL_CURSOR_FREE_SELECT);
  pika_tool_control_set_toggle_tool_cursor (tool->control,
                                            PIKA_TOOL_CURSOR_FREE_SELECT);

  pika_tool_control_set_toggled (tool->control, FALSE);

  /*  set precision to SUBPIXEL, because it may have been changed to
   *  PIXEL_CENTER if the tool has switched to MATTING_STATE_PAINT_TRIMAP,
   *  in pika_foreground_select_tool_set_trimap().
   */
  pika_tool_control_set_precision (tool->control,
                                   PIKA_CURSOR_PRECISION_SUBPIXEL);

  fg_select->state = MATTING_STATE_FREE_SELECT;

  /*  update the undo actions / menu items  */
  if (tool->display)
    pika_image_flush (pika_display_get_image (tool->display));

  tool->display   = NULL;
  g_list_free (tool->drawables);
  tool->drawables = NULL;

  if (fg_select->gui)
    pika_tool_gui_hide (fg_select->gui);
}

static void
pika_foreground_select_tool_commit (PikaForegroundSelectTool *fg_select)
{
  PikaTool             *tool    = PIKA_TOOL (fg_select);
  PikaSelectionOptions *options = PIKA_SELECTION_TOOL_GET_OPTIONS (fg_select);

  if (tool->display && fg_select->state != MATTING_STATE_FREE_SELECT)
    {
      PikaImage *image = pika_display_get_image (tool->display);

      if (fg_select->state != MATTING_STATE_PREVIEW_MASK)
        pika_foreground_select_tool_preview (fg_select);

      pika_channel_select_buffer (pika_image_get_mask (image),
                                  C_("command", "Foreground Select"),
                                  fg_select->mask,
                                  0, /* x offset */
                                  0, /* y offset */
                                  options->operation,
                                  options->feather,
                                  options->feather_radius,
                                  options->feather_radius);

      pika_image_flush (image);
    }
}

static void
pika_foreground_select_tool_set_trimap (PikaForegroundSelectTool *fg_select)
{
  PikaTool                    *tool = PIKA_TOOL (fg_select);
  PikaForegroundSelectOptions *options;

  g_return_if_fail (fg_select->trimap != NULL);

  options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);

  pika_polygon_select_tool_halt (PIKA_POLYGON_SELECT_TOOL (fg_select));

  if (options->preview_mode == PIKA_MATTING_PREVIEW_MODE_ON_COLOR)
    {
      if (fg_select->grayscale_preview)
        pika_canvas_item_set_visible (fg_select->grayscale_preview, FALSE);

      pika_display_shell_set_mask (pika_display_get_shell (tool->display),
                                   fg_select->trimap, 0, 0,
                                   &options->mask_color, TRUE);
    }
  else
    {
      pika_display_shell_set_mask (pika_display_get_shell (tool->display),
                                   NULL, 0, 0, NULL, FALSE);

      if (fg_select->grayscale_preview)
        {
          g_object_set (fg_select->grayscale_preview, "buffer",
                        fg_select->trimap, NULL);

          pika_canvas_item_set_visible (fg_select->grayscale_preview, TRUE);
        }
    }

  pika_tool_control_set_tool_cursor        (tool->control,
                                            PIKA_TOOL_CURSOR_PAINTBRUSH);
  pika_tool_control_set_toggle_tool_cursor (tool->control,
                                            PIKA_TOOL_CURSOR_PAINTBRUSH);

  pika_tool_control_set_toggled (tool->control, FALSE);

  /* disable double click in paint trimap state */
  pika_tool_control_set_wants_double_click (tool->control, FALSE);

  /* set precision to PIXEL_CENTER in paint trimap state */
  pika_tool_control_set_precision (tool->control,
                                   PIKA_CURSOR_PRECISION_PIXEL_CENTER);

  fg_select->state = MATTING_STATE_PAINT_TRIMAP;

  pika_foreground_select_tool_update_gui (fg_select);
}

static void
pika_foreground_select_tool_set_preview (PikaForegroundSelectTool *fg_select)
{

  PikaTool                    *tool = PIKA_TOOL (fg_select);
  PikaForegroundSelectOptions *options;

  g_return_if_fail (fg_select->mask != NULL);

  options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);

  if (options->preview_mode == PIKA_MATTING_PREVIEW_MODE_ON_COLOR)
    {
      if (fg_select->grayscale_preview)
        pika_canvas_item_set_visible (fg_select->grayscale_preview, FALSE);

      pika_display_shell_set_mask (pika_display_get_shell (tool->display),
                                   fg_select->mask, 0, 0,
                                   &options->mask_color, TRUE);
    }
  else
    {
      pika_display_shell_set_mask (pika_display_get_shell (tool->display),
                                   NULL, 0, 0, NULL, FALSE);

      if (fg_select->grayscale_preview)
        {
          g_object_set (fg_select->grayscale_preview, "buffer",
                    fg_select->mask, NULL);
          pika_canvas_item_set_visible (fg_select->grayscale_preview, TRUE);
        }
    }

  pika_tool_control_set_tool_cursor        (tool->control,
                                            PIKA_TOOL_CURSOR_PAINTBRUSH);
  pika_tool_control_set_toggle_tool_cursor (tool->control,
                                            PIKA_TOOL_CURSOR_PAINTBRUSH);

  pika_tool_control_set_toggled (tool->control, FALSE);

  fg_select->state = MATTING_STATE_PREVIEW_MASK;

  pika_foreground_select_tool_update_gui (fg_select);
}

static void
pika_foreground_select_tool_preview (PikaForegroundSelectTool *fg_select)
{
  PikaTool                    *tool      = PIKA_TOOL (fg_select);
  PikaForegroundSelectOptions *options;
  PikaImage                   *image     = pika_display_get_image (tool->display);
  GList                       *drawables = pika_image_get_selected_drawables (image);
  PikaDrawable                *drawable;

  g_return_if_fail (g_list_length (drawables) == 1);

  drawable = drawables->data;
  g_list_free (drawables);

  options  = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);

  g_clear_object (&fg_select->mask);

  fg_select->mask = pika_drawable_foreground_extract (drawable,
                                                      options->engine,
                                                      options->iterations,
                                                      options->levels,
                                                      options->active_levels,
                                                      fg_select->trimap,
                                                      PIKA_PROGRESS (fg_select));

  pika_foreground_select_tool_set_preview (fg_select);
}

static void
pika_foreground_select_tool_stroke_paint (PikaForegroundSelectTool *fg_select)
{
  PikaForegroundSelectOptions *options;
  PikaScanConvert             *scan_convert;
  StrokeUndo                  *undo;
  gint                         width;
  gdouble                      opacity;

  options = PIKA_FOREGROUND_SELECT_TOOL_GET_OPTIONS (fg_select);

  g_return_if_fail (fg_select->stroke != NULL);

  width = ROUND ((gdouble) options->stroke_width);

  if (fg_select->redo_stack)
    {
      g_list_free_full (fg_select->redo_stack,
                        (GDestroyNotify) pika_foreground_select_undo_free);
      fg_select->redo_stack = NULL;
    }

  undo = pika_foreground_select_undo_new (fg_select->trimap,
                                          fg_select->stroke,
                                          options->draw_mode, width);
  if (! undo)
    {
      g_array_free (fg_select->stroke, TRUE);
      fg_select->stroke = NULL;
      return;
    }

  fg_select->undo_stack = g_list_prepend (fg_select->undo_stack, undo);

  scan_convert = pika_scan_convert_new ();

  if (fg_select->stroke->len == 1)
    {
      PikaVector2 points[2];

      points[0] = points[1] = ((PikaVector2 *) fg_select->stroke->data)[0];

      points[1].x += 0.01;
      points[1].y += 0.01;

      pika_scan_convert_add_polyline (scan_convert, 2, points, FALSE);
    }
  else
    {
      pika_scan_convert_add_polyline (scan_convert,
                                      fg_select->stroke->len,
                                      (PikaVector2 *) fg_select->stroke->data,
                                      FALSE);
    }

  pika_scan_convert_stroke (scan_convert,
                            width,
                            PIKA_JOIN_ROUND, PIKA_CAP_ROUND, 10.0,
                            0.0, NULL);

  if (options->draw_mode == PIKA_MATTING_DRAW_MODE_FOREGROUND)
    opacity = 1.0;
  else if (options->draw_mode == PIKA_MATTING_DRAW_MODE_BACKGROUND)
    opacity = 0.0;
  else
    opacity = 0.5;

  pika_scan_convert_compose_value (scan_convert, fg_select->trimap,
                                   0, 0,
                                   opacity);

  pika_scan_convert_free (scan_convert);

  g_array_free (fg_select->stroke, TRUE);
  fg_select->stroke = NULL;

  /*  update the undo actions / menu items  */
  pika_image_flush (pika_display_get_image (PIKA_TOOL (fg_select)->display));
}

static void
pika_foreground_select_tool_cancel_paint (PikaForegroundSelectTool *fg_select)
{
  g_return_if_fail (fg_select->stroke != NULL);

  g_array_free (fg_select->stroke, TRUE);
  fg_select->stroke = NULL;
}

static void
pika_foreground_select_tool_response (PikaToolGui              *gui,
                                      gint                      response_id,
                                      PikaForegroundSelectTool *fg_select)
{
  PikaTool *tool = PIKA_TOOL (fg_select);

  switch (response_id)
    {
    case GTK_RESPONSE_APPLY:
      pika_tool_control (tool, PIKA_TOOL_ACTION_COMMIT, tool->display);
      break;

    default:
      pika_tool_control (tool, PIKA_TOOL_ACTION_HALT, tool->display);
      break;
    }
}

static void
pika_foreground_select_tool_preview_toggled (GtkToggleButton          *button,
                                             PikaForegroundSelectTool *fg_select)
{
  if (fg_select->state != MATTING_STATE_FREE_SELECT)
    {
      if (gtk_toggle_button_get_active (button))
        {
          if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
            pika_foreground_select_tool_preview (fg_select);
        }
      else
        {
          if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
            pika_foreground_select_tool_set_trimap (fg_select);
        }
    }
}

static void
pika_foreground_select_tool_update_gui (PikaForegroundSelectTool *fg_select)
{
  if (fg_select->state == MATTING_STATE_PAINT_TRIMAP)
    {
      pika_tool_gui_set_description (fg_select->gui, _("Paint mask"));
    }
  else if (fg_select->state == MATTING_STATE_PREVIEW_MASK)
    {
      pika_tool_gui_set_description (fg_select->gui, _("Preview"));
    }

  pika_tool_gui_set_response_sensitive (fg_select->gui, GTK_RESPONSE_APPLY,
                                        TRUE);
  gtk_widget_set_sensitive (fg_select->preview_toggle, TRUE);
}

static StrokeUndo *
pika_foreground_select_undo_new (GeglBuffer          *trimap,
                                 GArray              *stroke,
                                 PikaMattingDrawMode draw_mode,
                                 gint                stroke_width)

{
  StrokeUndo          *undo;
  const GeglRectangle *extent;
  gint                 x1, y1, x2, y2;
  gint                 width, height;
  gint                 i;

  extent = gegl_buffer_get_extent (trimap);

  x1 = G_MAXINT;
  y1 = G_MAXINT;
  x2 = G_MININT;
  y2 = G_MININT;

  for (i = 0; i < stroke->len; i++)
    {
      PikaVector2 *point = &g_array_index (stroke, PikaVector2, i);

      x1 = MIN (x1, floor (point->x));
      y1 = MIN (y1, floor (point->y));
      x2 = MAX (x2, ceil (point->x));
      y2 = MAX (y2, ceil (point->y));
    }

  x1 -= (stroke_width + 1) / 2;
  y1 -= (stroke_width + 1) / 2;
  x2 += (stroke_width + 1) / 2;
  y2 += (stroke_width + 1) / 2;

  x1 = MAX (x1, extent->x);
  y1 = MAX (y1, extent->y);
  x2 = MIN (x2, extent->x + extent->width);
  y2 = MIN (y2, extent->x + extent->height);

  width  = x2 - x1;
  height = y2 - y1;

  if (width <= 0 || height <= 0)
    return NULL;

  undo = g_slice_new0 (StrokeUndo);
  undo->saved_trimap = gegl_buffer_new (GEGL_RECTANGLE (x1, y1, width, height),
                                        gegl_buffer_get_format (trimap));

  pika_gegl_buffer_copy (
    trimap,             GEGL_RECTANGLE (x1, y1, width, height),
    GEGL_ABYSS_NONE,
    undo->saved_trimap, NULL);

  undo->trimap_x = x1;
  undo->trimap_y = y1;

  undo->draw_mode    = draw_mode;
  undo->stroke_width = stroke_width;

  return undo;
}

static void
pika_foreground_select_undo_pop (StrokeUndo *undo,
                                 GeglBuffer *trimap)
{
  GeglBuffer *buffer;
  gint        width, height;

  buffer = pika_gegl_buffer_dup (undo->saved_trimap);

  width  = gegl_buffer_get_width  (buffer);
  height = gegl_buffer_get_height (buffer);

  pika_gegl_buffer_copy (trimap,
                         GEGL_RECTANGLE (undo->trimap_x, undo->trimap_y,
                                         width, height),
                         GEGL_ABYSS_NONE,
                         undo->saved_trimap, NULL);

  pika_gegl_buffer_copy (buffer,
                         GEGL_RECTANGLE (undo->trimap_x, undo->trimap_y,
                                         width, height),
                         GEGL_ABYSS_NONE,
                         trimap, NULL);

  g_object_unref (buffer);
}

static void
pika_foreground_select_undo_free (StrokeUndo *undo)
{
  g_clear_object (&undo->saved_trimap);

  g_slice_free (StrokeUndo, undo);
}
