/* 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "core/pikadrawable.h"

#include "paint/pikaeraseroptions.h"

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

#include "pikaerasertool.h"
#include "pikapaintoptions-gui.h"
#include "pikatoolcontrol.h"

#include "pika-intl.h"


static void        pika_eraser_tool_modifier_key  (PikaTool         *tool,
                                                   GdkModifierType   key,
                                                   gboolean          press,
                                                   GdkModifierType   state,
                                                   PikaDisplay      *display);
static void        pika_eraser_tool_cursor_update (PikaTool         *tool,
                                                   const PikaCoords *coords,
                                                   GdkModifierType   state,
                                                   PikaDisplay      *display);

static gboolean    pika_eraser_tool_is_alpha_only (PikaPaintTool    *paint_tool,
                                                   PikaDrawable     *drawable);

static GtkWidget * pika_eraser_options_gui        (PikaToolOptions  *tool_options);


G_DEFINE_TYPE (PikaEraserTool, pika_eraser_tool, PIKA_TYPE_BRUSH_TOOL)

#define parent_class pika_eraser_tool_parent_class


void
pika_eraser_tool_register (PikaToolRegisterCallback  callback,
                           gpointer                  data)
{
  (* callback) (PIKA_TYPE_ERASER_TOOL,
                PIKA_TYPE_ERASER_OPTIONS,
                pika_eraser_options_gui,
                PIKA_PAINT_OPTIONS_CONTEXT_MASK,
                "pika-eraser-tool",
                _("Eraser"),
                _("Eraser Tool: Erase to background or transparency using a brush"),
                N_("_Eraser"), "<shift>E",
                NULL, PIKA_HELP_TOOL_ERASER,
                PIKA_ICON_TOOL_ERASER,
                data);
}

static void
pika_eraser_tool_class_init (PikaEraserToolClass *klass)
{
  PikaToolClass      *tool_class       = PIKA_TOOL_CLASS (klass);
  PikaPaintToolClass *paint_tool_class = PIKA_PAINT_TOOL_CLASS (klass);

  tool_class->modifier_key        = pika_eraser_tool_modifier_key;
  tool_class->cursor_update       = pika_eraser_tool_cursor_update;

  paint_tool_class->is_alpha_only = pika_eraser_tool_is_alpha_only;
}

static void
pika_eraser_tool_init (PikaEraserTool *eraser)
{
  PikaTool      *tool       = PIKA_TOOL (eraser);
  PikaPaintTool *paint_tool = PIKA_PAINT_TOOL (eraser);

  pika_tool_control_set_tool_cursor            (tool->control,
                                                PIKA_TOOL_CURSOR_ERASER);
  pika_tool_control_set_toggle_cursor_modifier (tool->control,
                                                PIKA_CURSOR_MODIFIER_MINUS);

  pika_paint_tool_enable_color_picker (paint_tool,
                                       PIKA_COLOR_PICK_TARGET_BACKGROUND);

  paint_tool->status      = _("Click to erase");
  paint_tool->status_line = _("Click to erase the line");
  paint_tool->status_ctrl = _("%s to pick a background color");
}

static void
pika_eraser_tool_modifier_key (PikaTool        *tool,
                               GdkModifierType  key,
                               gboolean         press,
                               GdkModifierType  state,
                               PikaDisplay     *display)
{
  if (key == GDK_MOD1_MASK)
    {
      PikaEraserOptions *options = PIKA_ERASER_TOOL_GET_OPTIONS (tool);

      g_object_set (options,
                    "anti-erase", ! options->anti_erase,
                    NULL);
    }

  PIKA_TOOL_CLASS (parent_class)->modifier_key (tool, key, press, state, display);
}

static void
pika_eraser_tool_cursor_update (PikaTool         *tool,
                                const PikaCoords *coords,
                                GdkModifierType   state,
                                PikaDisplay      *display)
{
  PikaEraserOptions *options = PIKA_ERASER_TOOL_GET_OPTIONS (tool);

  pika_tool_control_set_toggled (tool->control, options->anti_erase);

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

static gboolean
pika_eraser_tool_is_alpha_only (PikaPaintTool *paint_tool,
                                PikaDrawable  *drawable)
{
  PikaEraserOptions *options = PIKA_ERASER_TOOL_GET_OPTIONS (paint_tool);

  if (! options->anti_erase)
    return pika_drawable_has_alpha (drawable);
  else
    return TRUE;
}


/*  tool options stuff  */

static GtkWidget *
pika_eraser_options_gui (PikaToolOptions *tool_options)
{
  GObject   *config = G_OBJECT (tool_options);
  GtkWidget *vbox   = pika_paint_options_gui (tool_options);
  GtkWidget *button;
  gchar     *str;

  /* the anti_erase toggle */
  str = g_strdup_printf (_("Anti erase  (%s)"),
                         pika_get_mod_string (GDK_MOD1_MASK));

  button = pika_prop_check_button_new (config, "anti-erase", str);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

  g_free (str);

  return vbox;
}
