/* 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-2003 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/>.
 */

/* NOTE: This file is auto-generated by pdbgen.pl. */

#include "config.h"

#include "stamp-pdbgen.h"

#include <gegl.h>

#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pikadrawable.h"
#include "core/pikaimage.h"
#include "core/pikalayer-floating-selection.h"
#include "core/pikalayer.h"
#include "core/pikaparamspecs.h"

#include "pikapdb.h"
#include "pikapdberror.h"
#include "pikapdb-utils.h"
#include "pikaprocedure.h"
#include "internal-procs.h"

#include "pika-intl.h"


static PikaValueArray *
floating_sel_remove_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaLayer *floating_sel;

  floating_sel = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      if (pika_layer_is_floating_sel (floating_sel))
        {
          pika_image_remove_layer (pika_item_get_image (PIKA_ITEM (floating_sel)),
                                   floating_sel, TRUE, NULL);
        }
      else
        {
          g_set_error_literal (error, PIKA_PDB_ERROR,
                               PIKA_PDB_ERROR_INVALID_ARGUMENT,
                               _("Cannot remove this layer because "
                                 "it is not a floating selection."));
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
floating_sel_anchor_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaLayer *floating_sel;

  floating_sel = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      if (pika_layer_is_floating_sel (floating_sel))
        {
          floating_sel_anchor (floating_sel);
        }
      else
        {
          g_set_error_literal (error, PIKA_PDB_ERROR,
                               PIKA_PDB_ERROR_INVALID_ARGUMENT,
                               _("Cannot anchor this layer because "
                                 "it is not a floating selection."));
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
floating_sel_to_layer_invoker (PikaProcedure         *procedure,
                               Pika                  *pika,
                               PikaContext           *context,
                               PikaProgress          *progress,
                               const PikaValueArray  *args,
                               GError               **error)
{
  gboolean success = TRUE;
  PikaLayer *floating_sel;

  floating_sel = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      if (pika_layer_is_floating_sel (floating_sel))
        {
          success = floating_sel_to_layer (floating_sel, error);
        }
      else
        {
          g_set_error_literal (error, PIKA_PDB_ERROR,
                               PIKA_PDB_ERROR_INVALID_ARGUMENT,
                               _("Cannot convert this layer to a normal layer "
                                 "because it is not a floating selection."));
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
floating_sel_attach_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaLayer *layer;
  PikaDrawable *drawable;

  layer = g_value_get_object (pika_value_array_index (args, 0));
  drawable = g_value_get_object (pika_value_array_index (args, 1));

  if (success)
    {
      if (pika_pdb_item_is_attached (PIKA_ITEM (drawable), NULL,
                                     PIKA_PDB_ITEM_CONTENT, error) &&
          pika_pdb_item_is_not_group (PIKA_ITEM (drawable), error))
        {
          /* see layer-new */
          if (pika_drawable_is_gray (PIKA_DRAWABLE (layer)) &&
              PIKA_IS_LAYER (drawable))
            pika_layer_fix_format_space (layer, TRUE, FALSE);

          floating_sel_attach (layer, drawable);
        }
      else
        success = FALSE;
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

void
register_floating_sel_procs (PikaPDB *pdb)
{
  PikaProcedure *procedure;

  /*
   * pika-floating-sel-remove
   */
  procedure = pika_procedure_new (floating_sel_remove_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-floating-sel-remove");
  pika_procedure_set_static_help (procedure,
                                  "Remove the specified floating selection from its associated drawable.",
                                  "This procedure removes the floating selection completely, without any side effects. The associated drawable is then set to active.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_layer ("floating-sel",
                                                      "floating sel",
                                                      "The floating selection",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-floating-sel-anchor
   */
  procedure = pika_procedure_new (floating_sel_anchor_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-floating-sel-anchor");
  pika_procedure_set_static_help (procedure,
                                  "Anchor the specified floating selection to its associated drawable.",
                                  "This procedure anchors the floating selection to its associated drawable. This is similar to merging with a merge type of ClipToBottomLayer. The floating selection layer is no longer valid after this operation.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_layer ("floating-sel",
                                                      "floating sel",
                                                      "The floating selection",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-floating-sel-to-layer
   */
  procedure = pika_procedure_new (floating_sel_to_layer_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-floating-sel-to-layer");
  pika_procedure_set_static_help (procedure,
                                  "Transforms the specified floating selection into a layer.",
                                  "This procedure transforms the specified floating selection into a layer with the same offsets and extents. The composited image will look precisely the same, but the floating selection layer will no longer be clipped to the extents of the drawable it was attached to. The floating selection will become the active layer. This procedure will not work if the floating selection has a different base type from the underlying image. This might be the case if the floating selection is above an auxiliary channel or a layer mask.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_layer ("floating-sel",
                                                      "floating sel",
                                                      "The floating selection",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-floating-sel-attach
   */
  procedure = pika_procedure_new (floating_sel_attach_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-floating-sel-attach");
  pika_procedure_set_static_help (procedure,
                                  "Attach the specified layer as floating to the specified drawable.",
                                  "This procedure attaches the layer as floating selection to the drawable.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_layer ("layer",
                                                      "layer",
                                                      "The layer (is attached as floating selection)",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_drawable ("drawable",
                                                         "drawable",
                                                         "The drawable (where to attach the floating selection)",
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
