/* 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
 *
 * pikadocumentview.c
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimagefile.h"

#include "pikacontainerview.h"
#include "pikadocumentview.h"
#include "pikadnd.h"
#include "pikaeditor.h"
#include "pikamenufactory.h"
#include "pikauimanager.h"
#include "pikaviewrenderer.h"
#include "pikawidgets-utils.h"

#include "pika-intl.h"


static void    pika_document_view_activate_item (PikaContainerEditor *editor,
                                                 PikaViewable        *viewable);
static GList * pika_document_view_drag_uri_list (GtkWidget           *widget,
                                                 gpointer             data);


G_DEFINE_TYPE (PikaDocumentView, pika_document_view,
               PIKA_TYPE_CONTAINER_EDITOR)

#define parent_class pika_document_view_parent_class


static void
pika_document_view_class_init (PikaDocumentViewClass *klass)
{
  PikaContainerEditorClass *editor_class = PIKA_CONTAINER_EDITOR_CLASS (klass);

  editor_class->activate_item = pika_document_view_activate_item;
}

static void
pika_document_view_init (PikaDocumentView *view)
{
  view->open_button    = NULL;
  view->remove_button  = NULL;
  view->refresh_button = NULL;
}

GtkWidget *
pika_document_view_new (PikaViewType     view_type,
                        PikaContainer   *container,
                        PikaContext     *context,
                        gint             view_size,
                        gint             view_border_width,
                        PikaMenuFactory *menu_factory)
{
  PikaDocumentView    *document_view;
  PikaContainerEditor *editor;

  g_return_val_if_fail (PIKA_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (view_size > 0 &&
                        view_size <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, FALSE);
  g_return_val_if_fail (view_border_width >= 0 &&
                        view_border_width <= PIKA_VIEW_MAX_BORDER_WIDTH,
                        FALSE);
  g_return_val_if_fail (menu_factory == NULL ||
                        PIKA_IS_MENU_FACTORY (menu_factory), NULL);

  document_view = g_object_new (PIKA_TYPE_DOCUMENT_VIEW,
                                "view-type",         view_type,
                                "container",         container,
                                "context",           context,
                                "view-size",         view_size,
                                "view-border-width", view_border_width,
                                "menu-factory",      menu_factory,
                                "menu-identifier",   "<Documents>",
                                "ui-path",           "/documents-popup",
                                NULL);

  editor = PIKA_CONTAINER_EDITOR (document_view);

  document_view->open_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "documents",
                                   "documents-open",
                                   "documents-raise-or-open",
                                   GDK_SHIFT_MASK,
                                   "documents-file-open-dialog",
                                   pika_get_toggle_behavior_mask (),
                                   NULL);
  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (document_view->open_button),
                                  PIKA_TYPE_IMAGEFILE);

  document_view->remove_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "documents",
                                   "documents-remove", NULL);
  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (document_view->remove_button),
                                  PIKA_TYPE_IMAGEFILE);

  pika_editor_add_action_button (PIKA_EDITOR (editor->view), "documents",
                                 "documents-clear", NULL);

  document_view->refresh_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "documents",
                                   "documents-recreate-preview",
                                   "documents-reload-previews",
                                   GDK_SHIFT_MASK,
                                   "documents-remove-dangling",
                                   pika_get_toggle_behavior_mask (),
                                   NULL);

  if (view_type == PIKA_VIEW_TYPE_LIST)
    {
      GtkWidget *dnd_widget;

      dnd_widget = pika_container_view_get_dnd_widget (editor->view);

      pika_dnd_uri_list_source_add (dnd_widget,
                                    pika_document_view_drag_uri_list,
                                    editor);
    }

  pika_ui_manager_update (pika_editor_get_ui_manager (PIKA_EDITOR (editor->view)),
                          editor);

  return GTK_WIDGET (document_view);
}

static void
pika_document_view_activate_item (PikaContainerEditor *editor,
                                  PikaViewable        *viewable)
{
  PikaDocumentView *view = PIKA_DOCUMENT_VIEW (editor);
  PikaContainer    *container;

  if (PIKA_CONTAINER_EDITOR_CLASS (parent_class)->activate_item)
    PIKA_CONTAINER_EDITOR_CLASS (parent_class)->activate_item (editor, viewable);

  container = pika_container_view_get_container (editor->view);

  if (viewable && pika_container_have (container, PIKA_OBJECT (viewable)))
    {
      gtk_button_clicked (GTK_BUTTON (view->open_button));
    }
}

static GList *
pika_document_view_drag_uri_list (GtkWidget *widget,
                                  gpointer   data)
{
  PikaViewable *viewable = pika_dnd_get_drag_viewable (widget);

  if (viewable)
    {
      const gchar *uri = pika_object_get_name (viewable);

      return g_list_append (NULL, g_strdup (uri));
    }

  return NULL;
}
