/* 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) 1999 Manish Singh
 *
 * 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 "display-types.h"

#include "pikadisplayshell.h"
#include "pikadisplayshell-expose.h"
#include "pikadisplayshell-filter.h"
#include "pikadisplayshell-profile.h"
#include "pikadisplayshell-render.h"


/*  local function prototypes  */

static void   pika_display_shell_filter_changed (PikaColorDisplayStack *stack,
                                                 PikaDisplayShell      *shell);


/*  public functions  */

void
pika_display_shell_filter_set (PikaDisplayShell      *shell,
                               PikaColorDisplayStack *stack)
{
  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (stack == NULL || PIKA_IS_COLOR_DISPLAY_STACK (stack));

  if (stack == shell->filter_stack)
    return;

  if (shell->filter_stack)
    {
      g_signal_handlers_disconnect_by_func (shell->filter_stack,
                                            pika_display_shell_filter_changed,
                                            shell);
    }

  g_set_object (&shell->filter_stack, stack);

  if (shell->filter_stack)
    {
      g_signal_connect (shell->filter_stack, "changed",
                        G_CALLBACK (pika_display_shell_filter_changed),
                        shell);
    }

  pika_display_shell_filter_changed (NULL, shell);
}

gboolean
pika_display_shell_has_filter (PikaDisplayShell *shell)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY_SHELL (shell), FALSE);

  if (shell->filter_stack)
    {
      GList *filters;
      GList *iter;

      filters = pika_color_display_stack_get_filters (shell->filter_stack);

      for (iter = filters; iter; iter = g_list_next (iter))
        {
          if (pika_color_display_get_enabled (PIKA_COLOR_DISPLAY (iter->data)))
            return TRUE;
        }
    }

  return FALSE;
}


/*  private functions  */

static gboolean
pika_display_shell_filter_changed_idle (gpointer data)
{
  PikaDisplayShell *shell = data;

  pika_display_shell_profile_update (shell);
  pika_display_shell_expose_full (shell);
  pika_display_shell_render_invalidate_full (shell);

  shell->filter_idle_id = 0;

  return FALSE;
}

static void
pika_display_shell_filter_changed (PikaColorDisplayStack *stack,
                                   PikaDisplayShell      *shell)
{
  if (shell->filter_idle_id)
    g_source_remove (shell->filter_idle_id);

  shell->filter_idle_id =
    g_idle_add_full (G_PRIORITY_LOW,
                     pika_display_shell_filter_changed_idle,
                     shell, NULL);
}
