/* 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 .
 */
#include "config.h"
#include 
#include 
#include "libpikawidgets/pikawidgets.h"
#include "dialogs-types.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikaitem.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikasizebox.h"
#include "widgets/pikaviewabledialog.h"
#include "scale-dialog.h"
#include "pika-intl.h"
#define RESPONSE_RESET 1
typedef struct _ScaleDialog ScaleDialog;
struct _ScaleDialog
{
  PikaViewable          *viewable;
  PikaUnit               unit;
  PikaInterpolationType  interpolation;
  GtkWidget             *box;
  GtkWidget             *combo;
  PikaScaleCallback      callback;
  gpointer               user_data;
};
/*  local function prototypes  */
static void   scale_dialog_free     (ScaleDialog *private);
static void   scale_dialog_response (GtkWidget   *dialog,
                                     gint         response_id,
                                     ScaleDialog *private);
static void   scale_dialog_reset    (ScaleDialog *private);
/*  public function  */
GtkWidget *
scale_dialog_new (PikaViewable          *viewable,
                  PikaContext           *context,
                  const gchar           *title,
                  const gchar           *role,
                  GtkWidget             *parent,
                  PikaHelpFunc           help_func,
                  const gchar           *help_id,
                  PikaUnit               unit,
                  PikaInterpolationType  interpolation,
                  PikaScaleCallback      callback,
                  gpointer               user_data)
{
  GtkWidget   *dialog;
  GtkWidget   *vbox;
  GtkWidget   *hbox;
  GtkWidget   *frame;
  GtkWidget   *label;
  ScaleDialog *private;
  PikaImage   *image = NULL;
  const gchar *text  = NULL;
  gint         width, height;
  gdouble      xres, yres;
  g_return_val_if_fail (PIKA_IS_VIEWABLE (viewable), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (callback != NULL, NULL);
  if (PIKA_IS_IMAGE (viewable))
    {
      image = PIKA_IMAGE (viewable);
      width  = pika_image_get_width (image);
      height = pika_image_get_height (image);
      text = _("Image Size");
    }
  else if (PIKA_IS_ITEM (viewable))
    {
      PikaItem *item = PIKA_ITEM (viewable);
      image = pika_item_get_image (item);
      width  = pika_item_get_width  (item);
      height = pika_item_get_height (item);
      text = _("Layer Size");
    }
  else
    {
      g_return_val_if_reached (NULL);
    }
  private = g_slice_new0 (ScaleDialog);
  private->viewable      = viewable;
  private->interpolation = interpolation;
  private->unit          = unit;
  private->callback      = callback;
  private->user_data     = user_data;
  pika_image_get_resolution (image, &xres, &yres);
  dialog = pika_viewable_dialog_new (g_list_prepend (NULL, viewable), context,
                                     title, role, PIKA_ICON_OBJECT_SCALE, title,
                                     parent,
                                     help_func, help_id,
                                     _("_Reset"),  RESPONSE_RESET,
                                     _("_Cancel"), GTK_RESPONSE_CANCEL,
                                     _("_Scale"),  GTK_RESPONSE_OK,
                                     NULL);
  pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           RESPONSE_RESET,
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
  g_object_weak_ref (G_OBJECT (dialog),
                     (GWeakNotify) scale_dialog_free, private);
  g_signal_connect (dialog, "response",
                    G_CALLBACK (scale_dialog_response),
                    private);
  private->box = g_object_new (PIKA_TYPE_SIZE_BOX,
                               "width",           width,
                               "height",          height,
                               "unit",            unit,
                               "xresolution",     xres,
                               "yresolution",     yres,
                               "resolution-unit", pika_image_get_unit (image),
                               "keep-aspect",     TRUE,
                               "edit-resolution", PIKA_IS_IMAGE (viewable),
                               NULL);
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);
  frame = pika_frame_new (text);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);
  gtk_container_add (GTK_CONTAINER (frame), private->box);
  gtk_widget_show (private->box);
  frame = pika_frame_new (_("Quality"));
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
  label = gtk_label_new_with_mnemonic (_("I_nterpolation:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
  gtk_size_group_add_widget (PIKA_SIZE_BOX (private->box)->size_group, label);
  private->combo = pika_enum_combo_box_new (PIKA_TYPE_INTERPOLATION_TYPE);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), private->combo);
  gtk_box_pack_start (GTK_BOX (hbox), private->combo, TRUE, TRUE, 0);
  gtk_widget_show (private->combo);
  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (private->combo),
                                 private->interpolation);
  return dialog;
}
/*  private functions  */
static void
scale_dialog_free (ScaleDialog *private)
{
  g_slice_free (ScaleDialog, private);
}
static void
scale_dialog_response (GtkWidget   *dialog,
                       gint         response_id,
                       ScaleDialog *private)
{
  PikaUnit  unit          = private->unit;
  gint      interpolation = private->interpolation;
  PikaUnit  resolution_unit;
  gint      width, height;
  gdouble   xres, yres;
  switch (response_id)
    {
    case RESPONSE_RESET:
      scale_dialog_reset (private);
      break;
    case GTK_RESPONSE_OK:
      g_object_get (private->box,
                    "width",           &width,
                    "height",          &height,
                    "unit",            &unit,
                    "xresolution",     &xres,
                    "yresolution",     &yres,
                    "resolution-unit", &resolution_unit,
                    NULL);
      pika_int_combo_box_get_active (PIKA_INT_COMBO_BOX (private->combo),
                                     &interpolation);
      private->callback (dialog,
                         private->viewable,
                         width, height, unit, interpolation,
                         xres, yres, resolution_unit,
                         private->user_data);
      break;
    default:
      gtk_widget_destroy (dialog);
      break;
    }
}
static void
scale_dialog_reset (ScaleDialog *private)
{
  PikaImage *image;
  gint       width, height;
  gdouble    xres, yres;
  if (PIKA_IS_IMAGE (private->viewable))
    {
      image = PIKA_IMAGE (private->viewable);
      width  = pika_image_get_width (image);
      height = pika_image_get_height (image);
    }
  else if (PIKA_IS_ITEM (private->viewable))
    {
      PikaItem *item = PIKA_ITEM (private->viewable);
      image = pika_item_get_image (item);
      width  = pika_item_get_width  (item);
      height = pika_item_get_height (item);
    }
  else
    {
      g_return_if_reached ();
    }
  pika_image_get_resolution (image, &xres, &yres);
  g_object_set (private->box,
                "keep-aspect",     FALSE,
                NULL);
  g_object_set (private->box,
                "width",           width,
                "height",          height,
                "unit",            private->unit,
                NULL);
  g_object_set (private->box,
                "keep-aspect",     TRUE,
                "xresolution",     xres,
                "yresolution",     yres,
                "resolution-unit", pika_image_get_unit (image),
                NULL);
  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (private->combo),
                                 private->interpolation);
}