/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * pikacolordisplay.c
 * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"
#include "libpikacolor/pikacolor.h"
#include "libpikaconfig/pikaconfig.h"

#include "pikawidgetstypes.h"

#include "pikacolordisplay.h"
#include "pikaicons.h"


/**
 * SECTION: pikacolordisplay
 * @title: PikaColorDisplay
 * @short_description: Pluggable PIKA display color correction modules.
 * @see_also: #GModule, #GTypeModule, #PikaModule
 *
 * Functions and definitions for creating pluggable PIKA
 * display color correction modules.
 **/


enum
{
  PROP_0,
  PROP_ENABLED,
  PROP_COLOR_CONFIG,
  PROP_COLOR_MANAGED
};

enum
{
  CHANGED,
  LAST_SIGNAL
};


struct _PikaColorDisplayPrivate
{
  gboolean          enabled;
  PikaColorConfig  *config;
  PikaColorManaged *managed;
};

#define GET_PRIVATE(obj) (((PikaColorDisplay *) (obj))->priv)



static void       pika_color_display_constructed (GObject       *object);
static void       pika_color_display_dispose      (GObject      *object);
static void       pika_color_display_set_property (GObject      *object,
                                                   guint         property_id,
                                                   const GValue *value,
                                                  GParamSpec    *pspec);
static void       pika_color_display_get_property (GObject      *object,
                                                   guint         property_id,
                                                   GValue       *value,
                                                   GParamSpec   *pspec);

static void  pika_color_display_set_color_config  (PikaColorDisplay *display,
                                                   PikaColorConfig  *config);
static void  pika_color_display_set_color_managed (PikaColorDisplay *display,
                                                   PikaColorManaged *managed);


G_DEFINE_TYPE_WITH_CODE (PikaColorDisplay, pika_color_display, G_TYPE_OBJECT,
                         G_ADD_PRIVATE (PikaColorDisplay)
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONFIG, NULL))

#define parent_class pika_color_display_parent_class

static guint display_signals[LAST_SIGNAL] = { 0 };


static void
pika_color_display_class_init (PikaColorDisplayClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_color_display_constructed;
  object_class->dispose      = pika_color_display_dispose;
  object_class->set_property = pika_color_display_set_property;
  object_class->get_property = pika_color_display_get_property;

  g_object_class_install_property (object_class, PROP_ENABLED,
                                   g_param_spec_boolean ("enabled",
                                                         "Enabled",
                                                         "Whether this display filter is enabled",
                                                         TRUE,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_COLOR_CONFIG,
                                   g_param_spec_object ("color-config",
                                                        "Color Config",
                                                        "The color config used for this filter",
                                                        PIKA_TYPE_COLOR_CONFIG,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_COLOR_MANAGED,
                                   g_param_spec_object ("color-managed",
                                                        "Color Managed",
                                                        "The color managed pixel source that is filtered",
                                                        PIKA_TYPE_COLOR_MANAGED,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  display_signals[CHANGED] =
    g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaColorDisplayClass, changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  klass->name            = "Unnamed";
  klass->help_id         = NULL;
  klass->icon_name       = PIKA_ICON_DISPLAY_FILTER;

  klass->convert_buffer  = NULL;
  klass->configure       = NULL;

  klass->changed         = NULL;
}

static void
pika_color_display_init (PikaColorDisplay *display)
{
  display->priv = pika_color_display_get_instance_private (display);
}

static void
pika_color_display_constructed (GObject *object)
{
  G_OBJECT_CLASS (parent_class)->constructed (object);

  /* emit an initial "changed" signal after all construct properties are set */
  pika_color_display_changed (PIKA_COLOR_DISPLAY (object));
}

static void
pika_color_display_dispose (GObject *object)
{
  PikaColorDisplayPrivate *private = GET_PRIVATE (object);

  if (private->config)
    {
      g_signal_handlers_disconnect_by_func (private->config,
                                            pika_color_display_changed,
                                            object);
      g_object_unref (private->config);
      private->config = NULL;
    }

  if (private->managed)
    {
      g_signal_handlers_disconnect_by_func (private->managed,
                                            pika_color_display_changed,
                                            object);
      g_object_unref (private->managed);
      private->managed = NULL;
    }

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

static void
pika_color_display_set_property (GObject      *object,
                                 guint         property_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  PikaColorDisplay        *display = PIKA_COLOR_DISPLAY (object);
  PikaColorDisplayPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_ENABLED:
      private->enabled = g_value_get_boolean (value);
      break;

    case PROP_COLOR_CONFIG:
      pika_color_display_set_color_config (display,
                                           g_value_get_object (value));
      break;

    case PROP_COLOR_MANAGED:
      pika_color_display_set_color_managed (display,
                                            g_value_get_object (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_color_display_get_property (GObject    *object,
                                 guint       property_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  PikaColorDisplayPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_ENABLED:
      g_value_set_boolean (value, private->enabled);
      break;

    case PROP_COLOR_CONFIG:
      g_value_set_object (value, private->config);
      break;

    case PROP_COLOR_MANAGED:
      g_value_set_object (value, private->managed);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_color_display_set_color_config (PikaColorDisplay *display,
                                     PikaColorConfig  *config)
{
  PikaColorDisplayPrivate *private = GET_PRIVATE (display);

  g_return_if_fail (private->config == NULL);

  if (config)
    {
      private->config = g_object_ref (config);

      g_signal_connect_swapped (private->config, "notify",
                                G_CALLBACK (pika_color_display_changed),
                                display);
    }
}

static void
pika_color_display_set_color_managed (PikaColorDisplay *display,
                                      PikaColorManaged *managed)
{
  PikaColorDisplayPrivate *private = GET_PRIVATE (display);

  g_return_if_fail (private->managed == NULL);

  if (managed)
    {
      private->managed = g_object_ref (managed);

      g_signal_connect_swapped (private->managed, "profile-changed",
                                G_CALLBACK (pika_color_display_changed),
                                display);
    }
}

/**
 * pika_color_display_clone:
 * @display: a #PikaColorDisplay
 *
 * Creates a copy of @display.
 *
 * Returns: (transfer full): a duplicate of @display.
 *
 * Since: 2.0
 **/
PikaColorDisplay *
pika_color_display_clone (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), NULL);

  return PIKA_COLOR_DISPLAY (pika_config_duplicate (PIKA_CONFIG (display)));
}

/**
 * pika_color_display_convert_buffer:
 * @display: a #PikaColorDisplay
 * @buffer:  a #GeglBuffer
 * @area:    area in @buffer to convert
 *
 * Converts all pixels in @area of @buffer.
 *
 * Since: 2.10
 **/
void
pika_color_display_convert_buffer (PikaColorDisplay *display,
                                   GeglBuffer       *buffer,
                                   GeglRectangle    *area)
{
  PikaColorDisplayPrivate *private;

  g_return_if_fail (PIKA_IS_COLOR_DISPLAY (display));
  g_return_if_fail (GEGL_IS_BUFFER (buffer));

  private = GET_PRIVATE (display);

  if (private->enabled &&
      PIKA_COLOR_DISPLAY_GET_CLASS (display)->convert_buffer)
    {
      PIKA_COLOR_DISPLAY_GET_CLASS (display)->convert_buffer (display, buffer,
                                                              area);
    }
}

/**
 * pika_color_display_load_state:
 * @display: a #PikaColorDisplay
 * @state:   a #PikaParasite
 *
 * Configures @display from the contents of the parasite @state.
 * @state must be a properly serialized configuration for a
 * #PikaColorDisplay, such as saved by pika_color_display_save_state().
 *
 * Since: 2.0
 **/
void
pika_color_display_load_state (PikaColorDisplay *display,
                               PikaParasite     *state)
{
  g_return_if_fail (PIKA_IS_COLOR_DISPLAY (display));
  g_return_if_fail (state != NULL);

  pika_config_deserialize_parasite (PIKA_CONFIG (display),
                                    state,
                                    NULL, NULL);
}

/**
 * pika_color_display_save_state:
 * @display: a #PikaColorDisplay
 *
 * Saves the configuration state of @display as a new parasite.
 *
 * Returns: (transfer full): a #PikaParasite
 *
 * Since: 2.0
 **/
PikaParasite *
pika_color_display_save_state (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), NULL);

  return pika_config_serialize_to_parasite (PIKA_CONFIG (display),
                                            "Display/Proof",
                                            PIKA_PARASITE_PERSISTENT,
                                            NULL);
}

/**
 * pika_color_display_configure:
 * @display: a #PikaColorDisplay
 *
 * Creates a configuration widget for @display which can be added to a
 * container widget.
 *
 * Returns: (transfer full): a new configuration widget for @display, or
 *          %NULL if no specific widget exists.
 *
 * Since: 2.0
 **/
GtkWidget *
pika_color_display_configure (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), NULL);

  if (PIKA_COLOR_DISPLAY_GET_CLASS (display)->configure)
    return PIKA_COLOR_DISPLAY_GET_CLASS (display)->configure (display);

  return NULL;
}

void
pika_color_display_configure_reset (PikaColorDisplay *display)
{
  g_return_if_fail (PIKA_IS_COLOR_DISPLAY (display));

  pika_config_reset (PIKA_CONFIG (display));
}

void
pika_color_display_changed (PikaColorDisplay *display)
{
  g_return_if_fail (PIKA_IS_COLOR_DISPLAY (display));

  g_signal_emit (display, display_signals[CHANGED], 0);
}

void
pika_color_display_set_enabled (PikaColorDisplay *display,
                                gboolean          enabled)
{
  PikaColorDisplayPrivate *private;

  g_return_if_fail (PIKA_IS_COLOR_DISPLAY (display));

  private = GET_PRIVATE (display);

  if (enabled != private->enabled)
    {
      g_object_set (display,
                    "enabled", enabled,
                    NULL);
    }
}

gboolean
pika_color_display_get_enabled (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), FALSE);

  return GET_PRIVATE (display)->enabled;
}

/**
 * pika_color_display_get_config:
 * @display:
 *
 * Returns: (transfer none): a pointer to the #PikaColorConfig
 *               object or %NULL.
 *
 * Since: 2.4
 **/
PikaColorConfig *
pika_color_display_get_config (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), NULL);

  return GET_PRIVATE (display)->config;
}

/**
 * pika_color_display_get_managed:
 * @display:
 *
 * Returns: (transfer none): a pointer to the #PikaColorManaged
 *               object or %NULL.
 *
 * Since: 2.4
 **/
PikaColorManaged *
pika_color_display_get_managed (PikaColorDisplay *display)
{
  g_return_val_if_fail (PIKA_IS_COLOR_DISPLAY (display), NULL);

  return GET_PRIVATE (display)->managed;
}
