/* 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/pika.h"
#include "core/pikacontainer.h"
#include "core/pikadisplay.h"
#include "core/pikaimage.h"
#include "core/pikaparamspecs.h"

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


static PikaValueArray *
display_id_is_valid_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  gint display_id;
  gboolean valid = FALSE;

  display_id = g_value_get_int (pika_value_array_index (args, 0));

  if (success)
    {
      valid = (pika_display_get_by_id (pika, display_id) != NULL);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_boolean (pika_value_array_index (return_vals, 1), valid);

  return return_vals;
}

static PikaValueArray *
display_new_invoker (PikaProcedure         *procedure,
                     Pika                  *pika,
                     PikaContext           *context,
                     PikaProgress          *progress,
                     const PikaValueArray  *args,
                     GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaImage *image;
  PikaDisplay *display = NULL;

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

  if (success)
    {
      pika_image_flush (image);

      display = pika_create_display (pika, image, PIKA_UNIT_PIXEL, 1.0, NULL);

      if (display)
        {
          /* the first display takes ownership of the image */
          if (pika_image_get_display_count (image) == 1)
            g_object_unref (image);
        }
      else
        {
          success = FALSE;
        }
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_object (pika_value_array_index (return_vals, 1), display);

  return return_vals;
}

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

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

  if (success)
    {
      pika_delete_display (pika, display);
    }

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

static PikaValueArray *
display_get_window_handle_invoker (PikaProcedure         *procedure,
                                   Pika                  *pika,
                                   PikaContext           *context,
                                   PikaProgress          *progress,
                                   const PikaValueArray  *args,
                                   GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaDisplay *display;
  GBytes *handle = NULL;

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

  if (success)
    {
      handle = pika_get_display_window_id (pika, display);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_take_boxed (pika_value_array_index (return_vals, 1), handle);

  return return_vals;
}

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

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

  if (success)
    {
      pika_display_present (display);
    }

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

static PikaValueArray *
displays_flush_invoker (PikaProcedure         *procedure,
                        Pika                  *pika,
                        PikaContext           *context,
                        PikaProgress          *progress,
                        const PikaValueArray  *args,
                        GError               **error)
{
  pika_container_foreach (pika->images, (GFunc) pika_image_flush, NULL);

  return pika_procedure_get_return_values (procedure, TRUE, NULL);
}

static PikaValueArray *
displays_reconnect_invoker (PikaProcedure         *procedure,
                            Pika                  *pika,
                            PikaContext           *context,
                            PikaProgress          *progress,
                            const PikaValueArray  *args,
                            GError               **error)
{
  gboolean success = TRUE;
  PikaImage *old_image;
  PikaImage *new_image;

  old_image = g_value_get_object (pika_value_array_index (args, 0));
  new_image = g_value_get_object (pika_value_array_index (args, 1));

  if (success)
    {
      success = (old_image != new_image    &&
                 pika_image_get_display_count (old_image) > 0 &&
                 pika_image_get_display_count (new_image) == 0);

      if (success)
        {
          pika_reconnect_displays (pika, old_image, new_image);

          /* take ownership of the image */
          if (pika_image_get_display_count (new_image) > 0)
            g_object_unref (new_image);
        }
    }

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

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

  /*
   * pika-display-id-is-valid
   */
  procedure = pika_procedure_new (display_id_is_valid_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-display-id-is-valid");
  pika_procedure_set_static_help (procedure,
                                  "Returns TRUE if the display ID is valid.",
                                  "This procedure checks if the given display ID is valid and refers to an existing display.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Sven Neumann <sven@gimp.org>",
                                         "Sven Neumann",
                                         "2007");
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("display-id",
                                                 "display id",
                                                 "The display ID to check",
                                                 G_MININT32, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_boolean ("valid",
                                                         "valid",
                                                         "Whether the display ID is valid",
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-display-new
   */
  procedure = pika_procedure_new (display_new_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-display-new");
  pika_procedure_set_static_help (procedure,
                                  "Create a new display for the specified image.",
                                  "Creates a new display for the specified image. If the image already has a display, another is added. Multiple displays are handled transparently by PIKA. The newly created display is returned and can be subsequently destroyed with a call to 'pika-display-delete'. This procedure only makes sense for use with the PIKA UI, and will result in an execution error if called when PIKA has no UI.",
                                  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_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_display ("display",
                                                            "display",
                                                            "The new display",
                                                            FALSE,
                                                            PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-display-delete
   */
  procedure = pika_procedure_new (display_delete_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-display-delete");
  pika_procedure_set_static_help (procedure,
                                  "Delete the specified display.",
                                  "This procedure removes the specified display. If this is the last remaining display for the underlying image, then the image is deleted also. Note that the display is closed no matter if the image is dirty or not. Better save the image before calling this procedure.",
                                  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_display ("display",
                                                        "display",
                                                        "The display to delete",
                                                        FALSE,
                                                        PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-display-get-window-handle
   */
  procedure = pika_procedure_new (display_get_window_handle_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-display-get-window-handle");
  pika_procedure_set_static_help (procedure,
                                  "Get a handle to the native window for an image display.",
                                  "This procedure returns a handle to the native window for a given image display.\n"
                                  "It can be different types of data depending on the platform you are running on. For example in the X backend of GDK, a native window handle is an Xlib XID whereas on Wayland, it is a string handle. A value of NULL is returned for an invalid display or if this function is unimplemented for the windowing system that is being used.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Sven Neumann <sven@gimp.org>",
                                         "Sven Neumann",
                                         "2005");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_display ("display",
                                                        "display",
                                                        "The display to get the window handle from",
                                                        FALSE,
                                                        PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_boxed ("handle",
                                                       "handle",
                                                       "The native window handle or NULL",
                                                       G_TYPE_BYTES,
                                                       PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-display-present
   */
  procedure = pika_procedure_new (display_present_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-display-present");
  pika_procedure_set_static_help (procedure,
                                  "Present the specified display.",
                                  "This procedure presents the specified display at the top of the display stack.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Jehan",
                                         "Jehan",
                                         "2021");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_display ("display",
                                                        "display",
                                                        "The display to present",
                                                        FALSE,
                                                        PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-displays-flush
   */
  procedure = pika_procedure_new (displays_flush_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-displays-flush");
  pika_procedure_set_static_help (procedure,
                                  "Flush all internal changes to the user interface",
                                  "This procedure takes no arguments and returns nothing except a success status. Its purpose is to flush all pending updates of image manipulations to the user interface. It should be called whenever appropriate.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-displays-reconnect
   */
  procedure = pika_procedure_new (displays_reconnect_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-displays-reconnect");
  pika_procedure_set_static_help (procedure,
                                  "Reconnect displays from one image to another image.",
                                  "This procedure connects all displays of the old_image to the new_image. If the old_image has no display or new_image already has a display the reconnect is not performed and the procedure returns without success. You should rarely need to use this function.",
                                  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_image ("old-image",
                                                      "old image",
                                                      "The old image (must have at least one display)",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("new-image",
                                                      "new image",
                                                      "The new image (must not have a display)",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
