/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * pikaaspectpreview.c
 *
 * 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "pikauitypes.h"

#include "pika.h"
#include "pikapdb-private.h"

#include "libpika-intl.h"

#include "pikaaspectpreview.h"


/**
 * SECTION: pikaaspectpreview
 * @title: PikaAspectPreview
 * @short_description: A widget providing a preview with fixed aspect ratio.
 *
 * A widget providing a preview with fixed aspect ratio.
 **/


enum
{
  PROP_0,
  PROP_DRAWABLE
};

struct _PikaAspectPreviewPrivate
{
  PikaDrawable *drawable;
};

typedef struct
{
  gboolean  update;
} PreviewSettings;

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


static void  pika_aspect_preview_constructed   (GObject         *object);
static void  pika_aspect_preview_dispose       (GObject         *object);
static void  pika_aspect_preview_get_property  (GObject         *object,
                                                guint            property_id,
                                                GValue          *value,
                                                GParamSpec      *pspec);
static void  pika_aspect_preview_set_property  (GObject         *object,
                                                guint            property_id,
                                                const GValue    *value,
                                                GParamSpec      *pspec);

static void  pika_aspect_preview_style_updated (GtkWidget       *widget);
static void  pika_aspect_preview_draw          (PikaPreview     *preview);
static void  pika_aspect_preview_draw_buffer   (PikaPreview     *preview,
                                                const guchar    *buffer,
                                                gint             rowstride);
static void  pika_aspect_preview_transform     (PikaPreview     *preview,
                                                gint             src_x,
                                                gint             src_y,
                                                gint            *dest_x,
                                                gint            *dest_y);
static void  pika_aspect_preview_untransform   (PikaPreview     *preview,
                                                gint             src_x,
                                                gint             src_y,
                                                gint            *dest_x,
                                                gint            *dest_y);

static void  pika_aspect_preview_set_drawable  (PikaAspectPreview *preview,
                                                PikaDrawable      *drawable);


G_DEFINE_TYPE_WITH_PRIVATE (PikaAspectPreview, pika_aspect_preview,
                            PIKA_TYPE_PREVIEW)

#define parent_class pika_aspect_preview_parent_class

static gint pika_aspect_preview_counter = 0;


static void
pika_aspect_preview_class_init (PikaAspectPreviewClass *klass)
{
  GObjectClass     *object_class  = G_OBJECT_CLASS (klass);
  GtkWidgetClass   *widget_class  = GTK_WIDGET_CLASS (klass);
  PikaPreviewClass *preview_class = PIKA_PREVIEW_CLASS (klass);

  object_class->constructed   = pika_aspect_preview_constructed;
  object_class->dispose       = pika_aspect_preview_dispose;
  object_class->get_property  = pika_aspect_preview_get_property;
  object_class->set_property  = pika_aspect_preview_set_property;

  widget_class->style_updated = pika_aspect_preview_style_updated;

  preview_class->draw         = pika_aspect_preview_draw;
  preview_class->draw_buffer  = pika_aspect_preview_draw_buffer;
  preview_class->transform    = pika_aspect_preview_transform;
  preview_class->untransform  = pika_aspect_preview_untransform;

  /**
   * PikaAspectPreview:drawable-id:
   *
   * The drawable the #PikaAspectPreview is attached to.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class, PROP_DRAWABLE,
                                   g_param_spec_object ("drawable",
                                                        "Drawable",
                                                        "The drawable this preview is attached to",
                                                        PIKA_TYPE_DRAWABLE,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_aspect_preview_init (PikaAspectPreview *preview)
{
  preview->priv = pika_aspect_preview_get_instance_private (preview);

  g_object_set (pika_preview_get_area (PIKA_PREVIEW (preview)),
                "check-size", pika_check_size (),
                "check-type", pika_check_type (),
                "check-custom-color1", pika_check_custom_color1 (),
                "check-custom-color2", pika_check_custom_color2 (),
                NULL);
}

static void
pika_aspect_preview_constructed (GObject *object)
{
  gchar           *data_name;
  PreviewSettings  settings;
  GBytes          *settings_bytes = NULL;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  data_name = g_strdup_printf ("%s-aspect-preview-%d",
                               g_get_prgname (),
                               pika_aspect_preview_counter++);

  if (pika_pdb_get_data (data_name, &settings_bytes) &&
      g_bytes_get_size (settings_bytes) == sizeof (PreviewSettings))
    {
      settings = *((PreviewSettings *) g_bytes_get_data (settings_bytes, NULL));
      pika_preview_set_update (PIKA_PREVIEW (object), settings.update);
    }
  g_bytes_unref (settings_bytes);

  g_object_set_data_full (object, "pika-aspect-preview-data-name",
                          data_name, (GDestroyNotify) g_free);
}

static void
pika_aspect_preview_dispose (GObject *object)
{
  PikaAspectPreviewPrivate *priv = GET_PRIVATE (object);
  const gchar              *data_name;

  data_name = g_object_get_data (G_OBJECT (object),
                                 "pika-aspect-preview-data-name");
  if (data_name)
    {
      PikaPreview     *preview = PIKA_PREVIEW (object);
      GBytes          *bytes;
      PreviewSettings  settings;

      settings.update = pika_preview_get_update (preview);

      bytes = g_bytes_new_static (&settings, sizeof (PreviewSettings));
      pika_pdb_set_data (data_name, bytes);
      g_bytes_unref (bytes);
    }

  g_clear_object (&priv->drawable);

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

static void
pika_aspect_preview_get_property (GObject    *object,
                                  guint       property_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  PikaAspectPreview        *preview = PIKA_ASPECT_PREVIEW (object);
  PikaAspectPreviewPrivate *priv    = GET_PRIVATE (preview);

  switch (property_id)
    {
    case PROP_DRAWABLE:
      g_value_set_object (value, priv->drawable);
      break;

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

static void
pika_aspect_preview_set_property (GObject      *object,
                                  guint         property_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  PikaAspectPreview *preview = PIKA_ASPECT_PREVIEW (object);

  switch (property_id)
    {
    case PROP_DRAWABLE:
      pika_aspect_preview_set_drawable (preview, g_value_dup_object (value));
      break;

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

static void
pika_aspect_preview_style_updated (GtkWidget *widget)
{
  PikaPreview *preview = PIKA_PREVIEW (widget);
  GtkWidget   *area    = pika_preview_get_area (preview);

  GTK_WIDGET_CLASS (parent_class)->style_updated (widget);

  if (area)
    {
      PikaAspectPreviewPrivate *priv = GET_PRIVATE (preview);
      gint                      width;
      gint                      height;
      gint                      preview_width;
      gint                      preview_height;
      gint                      size;

      width  = pika_drawable_get_width  (priv->drawable);
      height = pika_drawable_get_height (priv->drawable);

      gtk_widget_style_get (widget,
                            "size", &size,
                            NULL);

      if (width > height)
        {
          preview_width  = MIN (width, size);
          preview_height = (height * preview_width) / width;
        }
      else
        {
          preview_height = MIN (height, size);
          preview_width  = (width * preview_height) / height;
        }

      pika_preview_set_size (preview, preview_width, preview_height);
    }
}

static void
pika_aspect_preview_draw (PikaPreview *preview)
{
  GtkWidget *area = pika_preview_get_area (preview);
  gint       width;
  gint       height;

  pika_preview_get_size (preview, &width, &height);

  pika_preview_area_fill (PIKA_PREVIEW_AREA (area),
                          0, 0,
                          width, height,
                          0, 0, 0);
}

static void
pika_aspect_preview_draw_buffer (PikaPreview  *preview,
                                 const guchar *buffer,
                                 gint          rowstride)
{
  PikaAspectPreviewPrivate *priv = GET_PRIVATE (preview);
  GtkWidget                *area = pika_preview_get_area (preview);
  PikaImage                *image;
  gint                      width;
  gint                      height;

  pika_preview_get_size (preview, &width, &height);

  image = pika_item_get_image (PIKA_ITEM (priv->drawable));

  if (pika_selection_is_empty (image))
    {
      pika_preview_area_draw (PIKA_PREVIEW_AREA (area),
                              0, 0,
                              width, height,
                              pika_drawable_type (priv->drawable),
                              buffer,
                              rowstride);
    }
  else
    {
      GBytes        *sel;
      GBytes        *src;
      PikaSelection *selection;
      gint           w, h;
      gint           bpp;

      selection = pika_image_get_selection (image);

      src = pika_drawable_get_thumbnail_data (priv->drawable,
                                              width, height,
                                              &w, &h, &bpp);
      sel = pika_drawable_get_thumbnail_data (PIKA_DRAWABLE (selection),
                                              width, height,
                                              &w, &h, &bpp);

      pika_preview_area_mask (PIKA_PREVIEW_AREA (area),
                              0, 0, width, height,
                              pika_drawable_type (priv->drawable),
                              g_bytes_get_data (src, NULL),
                              width * pika_drawable_get_bpp (priv->drawable),
                              buffer, rowstride,
                              g_bytes_get_data (sel, NULL), width);

      g_bytes_unref (sel);
      g_bytes_unref (src);
    }
}

static void
pika_aspect_preview_transform (PikaPreview *preview,
                               gint         src_x,
                               gint         src_y,
                               gint        *dest_x,
                               gint        *dest_y)
{
  PikaAspectPreviewPrivate *priv = GET_PRIVATE (preview);
  gint                      width;
  gint                      height;

  pika_preview_get_size (preview, &width, &height);

  *dest_x = (gdouble) src_x * width  / pika_drawable_get_width  (priv->drawable);
  *dest_y = (gdouble) src_y * height / pika_drawable_get_height (priv->drawable);
}

static void
pika_aspect_preview_untransform (PikaPreview *preview,
                                 gint         src_x,
                                 gint         src_y,
                                 gint        *dest_x,
                                 gint        *dest_y)
{
  PikaAspectPreviewPrivate *priv = GET_PRIVATE (preview);
  gint                      width;
  gint                      height;

  pika_preview_get_size (preview, &width, &height);

  *dest_x = (gdouble) src_x * pika_drawable_get_width  (priv->drawable) / width;
  *dest_y = (gdouble) src_y * pika_drawable_get_height (priv->drawable) / height;
}

static void
pika_aspect_preview_set_drawable (PikaAspectPreview *preview,
                                  PikaDrawable      *drawable)
{
  PikaAspectPreviewPrivate *priv = GET_PRIVATE (preview);
  gint                      d_width;
  gint                      d_height;
  gint                      width;
  gint                      height;

  g_return_if_fail (priv->drawable == NULL);

  priv->drawable = drawable;

  d_width  = pika_drawable_get_width  (priv->drawable);
  d_height = pika_drawable_get_height (priv->drawable);

  if (d_width > d_height)
    {
      width  = MIN (d_width, 512);
      height = (d_height * width) / d_width;
    }
  else
    {
      height = MIN (d_height, 512);
      width  = (d_width * height) / d_height;
    }

  pika_preview_set_bounds (PIKA_PREVIEW (preview), 0, 0, width, height);

  if (height > 0)
    g_object_set (pika_preview_get_frame (PIKA_PREVIEW (preview)),
                  "ratio",
                  (gdouble) d_width / (gdouble) d_height,
                  NULL);
}

/**
 * pika_aspect_preview_new_from_drawable:
 * @drawable: (transfer none): a drawable
 *
 * Creates a new #PikaAspectPreview widget for @drawable_. See also
 * pika_drawable_preview_new_from_drawable().
 *
 * Since: 2.10
 *
 * Returns: a new #PikaAspectPreview.
 **/
GtkWidget *
pika_aspect_preview_new_from_drawable (PikaDrawable *drawable)
{
  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (pika_item_is_valid (PIKA_ITEM (drawable)), NULL);

  return g_object_new (PIKA_TYPE_ASPECT_PREVIEW,
                       "drawable", drawable,
                       NULL);
}
