/* 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

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

#include "display-types.h"

#include "config/pikadisplayoptions.h"

#include "core/pikaimage.h"

#include "widgets/pikadockcolumns.h"
#include "widgets/pikamenumodel.h"
#include "widgets/pikarender.h"
#include "widgets/pikawidgets-utils.h"

#include "pikacanvas.h"
#include "pikacanvasitem.h"
#include "pikadisplay.h"
#include "pikadisplayshell.h"
#include "pikadisplayshell-actions.h"
#include "pikadisplayshell-appearance.h"
#include "pikadisplayshell-expose.h"
#include "pikadisplayshell-selection.h"
#include "pikadisplayshell-scroll.h"
#include "pikadisplayshell-scrollbars.h"
#include "pikaimagewindow.h"
#include "pikastatusbar.h"


/*  local function prototypes  */

static PikaDisplayOptions * appearance_get_options (PikaDisplayShell *shell);


/*  public functions  */

void
pika_display_shell_appearance_update (PikaDisplayShell *shell)
{
  PikaDisplayOptions *options;
  PikaImageWindow    *window;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);
  window  = pika_display_shell_get_window (shell);

  if (window)
    {
      gboolean fullscreen = pika_image_window_get_fullscreen (window);

      pika_display_shell_set_action_active (shell, "view-fullscreen",
                                            fullscreen);
    }

  pika_display_shell_set_show_menubar        (shell,
                                              options->show_menubar);
  pika_display_shell_set_show_statusbar      (shell,
                                              options->show_statusbar);

  pika_display_shell_set_show_rulers         (shell,
                                              options->show_rulers);
  pika_display_shell_set_show_scrollbars     (shell,
                                              options->show_scrollbars);
  pika_display_shell_set_show_selection      (shell,
                                              options->show_selection);
  pika_display_shell_set_show_layer          (shell,
                                              options->show_layer_boundary);
  pika_display_shell_set_show_canvas         (shell,
                                              options->show_canvas_boundary);
  pika_display_shell_set_show_guides         (shell,
                                              options->show_guides);
  pika_display_shell_set_show_grid           (shell,
                                              options->show_grid);
  pika_display_shell_set_show_sample_points  (shell,
                                              options->show_sample_points);
  pika_display_shell_set_padding             (shell,
                                              options->padding_mode,
                                              &options->padding_color);
  pika_display_shell_set_padding_in_show_all (shell,
                                              options->padding_in_show_all);
}

void
pika_display_shell_set_show_menubar (PikaDisplayShell *shell,
                                     gboolean          show)
{
  PikaDisplayOptions *options;
  PikaImageWindow    *window;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);
  window  = pika_display_shell_get_window (shell);

  g_object_set (options, "show-menubar", show, NULL);

  if (window && pika_image_window_get_active_shell (window) == shell)
    {
      pika_image_window_keep_canvas_pos (pika_display_shell_get_window (shell));
      pika_image_window_set_show_menubar (window, show);
    }

  pika_display_shell_set_action_active (shell, "view-show-menubar", show);
}

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

  return appearance_get_options (shell)->show_menubar;
}

void
pika_display_shell_set_show_statusbar (PikaDisplayShell *shell,
                                       gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-statusbar", show, NULL);

  pika_image_window_keep_canvas_pos (pika_display_shell_get_window (shell));
  pika_statusbar_set_visible (PIKA_STATUSBAR (shell->statusbar), show);

  pika_display_shell_set_action_active (shell, "view-show-statusbar", show);
}

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

  return appearance_get_options (shell)->show_statusbar;
}

void
pika_display_shell_set_show_rulers (PikaDisplayShell *shell,
                                    gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-rulers", show, NULL);

  pika_image_window_keep_canvas_pos (pika_display_shell_get_window (shell));
  gtk_widget_set_visible (shell->origin, show);
  gtk_widget_set_visible (shell->hrule, show);
  gtk_widget_set_visible (shell->vrule, show);
  gtk_widget_set_visible (shell->quick_mask_button, show);
  gtk_widget_set_visible (shell->zoom_button, show);

  pika_display_shell_set_action_active (shell, "view-show-rulers", show);
}

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

  return appearance_get_options (shell)->show_rulers;
}

void
pika_display_shell_set_show_scrollbars (PikaDisplayShell *shell,
                                        gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-scrollbars", show, NULL);

  pika_image_window_keep_canvas_pos (pika_display_shell_get_window (shell));
  gtk_widget_set_visible (shell->nav_ebox, show);
  gtk_widget_set_visible (shell->hsb, show);
  gtk_widget_set_visible (shell->vsb, show);
  gtk_widget_set_visible (shell->quick_mask_button, show);
  gtk_widget_set_visible (shell->zoom_button, show);

  pika_display_shell_set_action_active (shell, "view-show-scrollbars", show);
}

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

  return appearance_get_options (shell)->show_scrollbars;
}

void
pika_display_shell_set_show_selection (PikaDisplayShell *shell,
                                       gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-selection", show, NULL);

  pika_display_shell_selection_set_show (shell, show);

  pika_display_shell_set_action_active (shell, "view-show-selection", show);
}

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

  return appearance_get_options (shell)->show_selection;
}

void
pika_display_shell_set_show_layer (PikaDisplayShell *shell,
                                   gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-layer-boundary", show, NULL);

  pika_canvas_item_set_visible (shell->layer_boundary, show);

  pika_display_shell_set_action_active (shell, "view-show-layer-boundary", show);
}

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

  return appearance_get_options (shell)->show_layer_boundary;
}

void
pika_display_shell_set_show_canvas (PikaDisplayShell *shell,
                                    gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-canvas-boundary", show, NULL);

  pika_canvas_item_set_visible (shell->canvas_boundary,
                                show && shell->show_all);

  pika_display_shell_set_action_active (shell, "view-show-canvas-boundary", show);
}

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

  return appearance_get_options (shell)->show_canvas_boundary;
}

void
pika_display_shell_update_show_canvas (PikaDisplayShell *shell)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  pika_canvas_item_set_visible (shell->canvas_boundary,
                                options->show_canvas_boundary &&
                                shell->show_all);
}

void
pika_display_shell_set_show_guides (PikaDisplayShell *shell,
                                    gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-guides", show, NULL);

  pika_canvas_item_set_visible (shell->guides, show);

  pika_display_shell_set_action_active (shell, "view-show-guides", show);
}

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

  return appearance_get_options (shell)->show_guides;
}

void
pika_display_shell_set_show_grid (PikaDisplayShell *shell,
                                  gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-grid", show, NULL);

  pika_canvas_item_set_visible (shell->grid, show);

  pika_display_shell_set_action_active (shell, "view-show-grid", show);
}

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

  return appearance_get_options (shell)->show_grid;
}

void
pika_display_shell_set_show_sample_points (PikaDisplayShell *shell,
                                           gboolean          show)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "show-sample-points", show, NULL);

  pika_canvas_item_set_visible (shell->sample_points, show);

  pika_display_shell_set_action_active (shell, "view-show-sample-points", show);
}

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

  return appearance_get_options (shell)->show_sample_points;
}

void
pika_display_shell_set_snap_to_grid (PikaDisplayShell *shell,
                                     gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-grid", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_grid;
}

void
pika_display_shell_set_snap_to_guides (PikaDisplayShell *shell,
                                       gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-guides", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_guides;
}

void
pika_display_shell_set_snap_to_canvas (PikaDisplayShell *shell,
                                       gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-canvas", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_canvas;
}

void
pika_display_shell_set_snap_to_vectors (PikaDisplayShell *shell,
                                        gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-path", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_path;
}

void
pika_display_shell_set_snap_to_bbox (PikaDisplayShell *shell,
                                     gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-bbox", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_bbox;
}

void
pika_display_shell_set_snap_to_equidistance (PikaDisplayShell *shell,
                                             gboolean          snap)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  g_object_set (options, "snap-to-equidistance", snap, NULL);
}

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

  return appearance_get_options (shell)->snap_to_equidistance;
}

void
pika_display_shell_set_padding (PikaDisplayShell      *shell,
                                PikaCanvasPaddingMode  padding_mode,
                                const PikaRGB         *padding_color)
{
  PikaImageWindow    *window;
  PikaMenuModel      *model;
  PikaDisplayOptions *options;
  PikaRGB             color;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (padding_color != NULL);

  options = appearance_get_options (shell);
  color   = *padding_color;

  switch (padding_mode)
    {
    case PIKA_CANVAS_PADDING_MODE_DEFAULT:
      break;

    case PIKA_CANVAS_PADDING_MODE_LIGHT_CHECK:
      color = *pika_render_check_color1 ();
      break;

    case PIKA_CANVAS_PADDING_MODE_DARK_CHECK:
      color = *pika_render_check_color2 ();
      break;

    case PIKA_CANVAS_PADDING_MODE_CUSTOM:
    case PIKA_CANVAS_PADDING_MODE_RESET:
      break;
    }

  g_object_set (options,
                "padding-mode",  padding_mode,
                "padding-color", &color,
                NULL);

  pika_canvas_set_padding (PIKA_CANVAS (shell->canvas),
                           padding_mode, &color);

  window = pika_display_shell_get_window (shell);
  model  = pika_image_window_get_menubar_model (window);
  if (padding_mode != PIKA_CANVAS_PADDING_MODE_DEFAULT)
    pika_menu_model_set_color (model, "/View/Padding color",
                               &options->padding_color);
  else
    pika_menu_model_set_color (model, "/View/Padding color",
                               NULL);
}

void
pika_display_shell_get_padding (PikaDisplayShell      *shell,
                                PikaCanvasPaddingMode *padding_mode,
                                PikaRGB               *padding_color)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  if (padding_mode)
    *padding_mode = options->padding_mode;

  if (padding_color)
    *padding_color = options->padding_color;
}

void
pika_display_shell_set_padding_in_show_all (PikaDisplayShell *shell,
                                            gboolean          keep)
{
  PikaDisplayOptions *options;

  g_return_if_fail (PIKA_IS_DISPLAY_SHELL (shell));

  options = appearance_get_options (shell);

  if (options->padding_in_show_all != keep)
    {
      g_object_set (options, "padding-in-show-all", keep, NULL);

      if (shell->display)
        {
          pika_display_shell_scroll_clamp_and_update (shell);
          pika_display_shell_scrollbars_update (shell);

          pika_display_shell_expose_full (shell);
        }

      pika_display_shell_set_action_active (shell,
                                            "view-padding-color-in-show-all",
                                            keep);

      g_object_notify (G_OBJECT (shell), "infinite-canvas");
    }
}

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

  return appearance_get_options (shell)->padding_in_show_all;
}


/*  private functions  */

static PikaDisplayOptions *
appearance_get_options (PikaDisplayShell *shell)
{
  if (pika_display_get_image (shell->display))
    {
      PikaImageWindow *window = pika_display_shell_get_window (shell);

      if (window && pika_image_window_get_fullscreen (window))
        return shell->fullscreen_options;
      else
        return shell->options;
    }

  return shell->no_image_options;
}
