/* 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
 *
 * pikaaccellabel.c
 * Copyright (C) 2020 Ell
 *
 * 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/>.
 */

#include "config.h"

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

#include "libpikabase/pikabase.h"

#include "widgets-types.h"

#include "pikaaction.h"
#include "pikaaccellabel.h"


enum
{
  PROP_0,
  PROP_ACTION
};


struct _PikaAccelLabelPrivate
{
  PikaAction *action;
};


/*  local function prototypes  */

static void   pika_accel_label_dispose      (GObject             *object);
static void   pika_accel_label_set_property (GObject             *object,
                                             guint                property_id,
                                             const GValue        *value,
                                             GParamSpec          *pspec);
static void   pika_accel_label_get_property (GObject             *object,
                                             guint                property_id,
                                             GValue              *value,
                                             GParamSpec          *pspec);

static void   pika_accel_label_update       (PikaAccelLabel      *accel_label);


G_DEFINE_TYPE_WITH_PRIVATE (PikaAccelLabel, pika_accel_label, GTK_TYPE_LABEL)

#define parent_class pika_accel_label_parent_class


/*  private functions  */

static void
pika_accel_label_class_init (PikaAccelLabelClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose      = pika_accel_label_dispose;
  object_class->get_property = pika_accel_label_get_property;
  object_class->set_property = pika_accel_label_set_property;

  g_object_class_install_property (object_class, PROP_ACTION,
                                   g_param_spec_object ("action",
                                                        NULL, NULL,
                                                        PIKA_TYPE_ACTION,
                                                        PIKA_PARAM_READWRITE));
}

static void
pika_accel_label_init (PikaAccelLabel *accel_label)
{
  accel_label->priv = pika_accel_label_get_instance_private (accel_label);
}

static void
pika_accel_label_dispose (GObject *object)
{
  PikaAccelLabel *accel_label = PIKA_ACCEL_LABEL (object);

  pika_accel_label_set_action (accel_label, NULL);

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

static void
pika_accel_label_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  PikaAccelLabel *accel_label = PIKA_ACCEL_LABEL (object);

  switch (property_id)
    {
    case PROP_ACTION:
      pika_accel_label_set_action (accel_label, g_value_get_object (value));
      break;

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

static void
pika_accel_label_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  PikaAccelLabel *accel_label = PIKA_ACCEL_LABEL (object);

  switch (property_id)
    {
    case PROP_ACTION:
      g_value_set_object (value, accel_label->priv->action);
      break;

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

static void
pika_accel_label_update (PikaAccelLabel *accel_label)
{
  gchar **accels;

  gtk_label_set_label (GTK_LABEL (accel_label), NULL);

  if (! accel_label->priv->action)
    return;

  accels = pika_action_get_display_accels (accel_label->priv->action);

  if (accels && accels[0])
    gtk_label_set_label (GTK_LABEL (accel_label), accels[0]);

  g_strfreev (accels);
}


/*  public functions  */

GtkWidget *
pika_accel_label_new (PikaAction *action)
{
  g_return_val_if_fail (action == NULL || PIKA_IS_ACTION (action), NULL);

  return g_object_new (PIKA_TYPE_ACCEL_LABEL,
                       "action", action,
                       NULL);
}

void
pika_accel_label_set_action (PikaAccelLabel *accel_label,
                             PikaAction     *action)
{
  g_return_if_fail (PIKA_IS_ACCEL_LABEL (accel_label));
  g_return_if_fail (action == NULL || PIKA_IS_ACTION (action));

  if (action != accel_label->priv->action)
    {
      if (accel_label->priv->action)
        g_signal_handlers_disconnect_by_func (accel_label->priv->action,
                                              pika_accel_label_update,
                                              accel_label);

      g_set_object (&accel_label->priv->action, action);

      if (accel_label->priv->action)
        g_signal_connect_swapped (action, "accels-changed",
                                  G_CALLBACK (pika_accel_label_update),
                                  accel_label);

      pika_accel_label_update (accel_label);

      g_object_notify (G_OBJECT (accel_label), "action");
    }
}

PikaAction *
pika_accel_label_get_action (PikaAccelLabel *accel_label)
{
  g_return_val_if_fail (PIKA_IS_ACCEL_LABEL (accel_label), NULL);

  return accel_label->priv->action;
}
