/* 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
 *
 * pikacombotagentry.c
 * Copyright (C) 2008 Aurimas Juška <aurisj@svn.gnome.org>
 *
 * 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 <string.h>

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

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikatag.h"
#include "core/pikatagged.h"
#include "core/pikataggedcontainer.h"
#include "core/pikaviewable.h"

#include "pikatagentry.h"
#include "pikatagpopup.h"
#include "pikacombotagentry.h"


static void     pika_combo_tag_entry_constructed       (GObject              *object);

static void     pika_combo_tag_entry_icon_press        (GtkWidget            *widget,
                                                        GtkEntryIconPosition  icon_pos,
                                                        GdkEvent             *event,
                                                        gpointer              user_data);

static void     pika_combo_tag_entry_popup_destroy     (GtkWidget            *widget,
                                                        PikaComboTagEntry    *entry);

static void     pika_combo_tag_entry_tag_count_changed (PikaTaggedContainer  *container,
                                                        gint                  tag_count,
                                                        PikaComboTagEntry    *entry);


G_DEFINE_TYPE (PikaComboTagEntry, pika_combo_tag_entry, PIKA_TYPE_TAG_ENTRY);

#define parent_class pika_combo_tag_entry_parent_class


static void
pika_combo_tag_entry_class_init (PikaComboTagEntryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = pika_combo_tag_entry_constructed;
}

static void
pika_combo_tag_entry_init (PikaComboTagEntry *entry)
{
  entry->popup = NULL;

  gtk_widget_add_events (GTK_WIDGET (entry),
                         GDK_BUTTON_PRESS_MASK |
                         GDK_POINTER_MOTION_MASK);

  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_SECONDARY,
                                     PIKA_ICON_GO_DOWN);

  g_signal_connect (entry, "icon-press",
                    G_CALLBACK (pika_combo_tag_entry_icon_press),
                    NULL);
}

static void
pika_combo_tag_entry_constructed (GObject *object)
{
  PikaComboTagEntry *entry = PIKA_COMBO_TAG_ENTRY (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  g_signal_connect_object (PIKA_TAG_ENTRY (entry)->container,
                           "tag-count-changed",
                           G_CALLBACK (pika_combo_tag_entry_tag_count_changed),
                           entry, 0);
}

/**
 * pika_combo_tag_entry_new:
 * @container: a tagged container to be used.
 * @mode:      tag entry mode to work in.
 *
 * Creates a new #PikaComboTagEntry widget which extends #PikaTagEntry by
 * adding ability to pick tags using popup window (similar to combo box).
 *
 * Returns: a new #PikaComboTagEntry widget.
 **/
GtkWidget *
pika_combo_tag_entry_new (PikaTaggedContainer *container,
                          PikaTagEntryMode     mode)
{
  g_return_val_if_fail (PIKA_IS_TAGGED_CONTAINER (container), NULL);

  return g_object_new (PIKA_TYPE_COMBO_TAG_ENTRY,
                       "container", container,
                       "mode",      mode,
                       NULL);
}

static void
pika_combo_tag_entry_icon_press (GtkWidget            *widget,
                                 GtkEntryIconPosition  icon_pos,
                                 GdkEvent             *event,
                                 gpointer              user_data)
{
  PikaComboTagEntry *entry = PIKA_COMBO_TAG_ENTRY (widget);

  if (! entry->popup)
    {
      PikaTaggedContainer *container = PIKA_TAG_ENTRY (entry)->container;
      gint                 tag_count;

      tag_count = pika_tagged_container_get_tag_count (container);

      if (tag_count > 0 && ! PIKA_TAG_ENTRY (entry)->has_invalid_tags)
        {
          entry->popup = pika_tag_popup_new (entry);
          g_signal_connect (entry->popup, "destroy",
                            G_CALLBACK (pika_combo_tag_entry_popup_destroy),
                            entry);
          pika_tag_popup_show (PIKA_TAG_POPUP (entry->popup), event);
        }
    }
  else
    {
      gtk_widget_destroy (entry->popup);
    }
}

static void
pika_combo_tag_entry_popup_destroy (GtkWidget         *widget,
                                    PikaComboTagEntry *entry)
{
  entry->popup = NULL;
  gtk_widget_grab_focus (GTK_WIDGET (entry));
}

static void
pika_combo_tag_entry_tag_count_changed (PikaTaggedContainer *container,
                                        gint                 tag_count,
                                        PikaComboTagEntry   *entry)
{
  gboolean sensitive;

  sensitive = tag_count > 0 && ! PIKA_TAG_ENTRY (entry)->has_invalid_tags;

  gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
                                GTK_ENTRY_ICON_SECONDARY,
                                sensitive);
}
