/* 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 "libpikamath/pikamath.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "config/pikadisplayoptions.h"
#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikagrouplayer.h"

#include "widgets/pikaactiongroup.h"
#include "widgets/pikacolordialog.h"
#include "widgets/pikadock.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikatoggleaction.h"
#include "widgets/pikauimanager.h"
#include "widgets/pikawidgets-utils.h"
#include "widgets/pikawindowstrategy.h"

#include "display/pikadisplay.h"
#include "display/pikadisplay-foreach.h"
#include "display/pikadisplayshell.h"
#include "display/pikadisplayshell-appearance.h"
#include "display/pikadisplayshell-filter-dialog.h"
#include "display/pikadisplayshell-rotate.h"
#include "display/pikadisplayshell-rotate-dialog.h"
#include "display/pikadisplayshell-scale.h"
#include "display/pikadisplayshell-scale-dialog.h"
#include "display/pikadisplayshell-scroll.h"
#include "display/pikadisplayshell-close.h"
#include "display/pikaimagewindow.h"

#include "dialogs/color-profile-dialog.h"
#include "dialogs/dialogs.h"

#include "menus/menus.h"

#include "actions.h"
#include "view-commands.h"

#include "pika-intl.h"


#define SET_ACTIVE(manager,action_name,active) \
  { PikaActionGroup *group = \
      pika_ui_manager_get_action_group (manager, "view"); \
    pika_action_group_set_action_active (group, action_name, active); }

#define IS_ACTIVE_DISPLAY(display) \
  ((display) == \
   pika_context_get_display (pika_get_user_context ((display)->pika)))


/*  local function prototypes  */

static void   view_padding_color_dialog_update (PikaColorDialog          *dialog,
                                                const PikaRGB            *color,
                                                PikaColorDialogState      state,
                                                PikaDisplayShell         *shell);


/*  public functions  */

void
view_new_cmd_callback (PikaAction *action,
                       GVariant   *value,
                       gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  pika_create_display (display->pika,
                       pika_display_get_image (display),
                       shell->unit, pika_zoom_model_get_factor (shell->zoom),
                       G_OBJECT (pika_widget_get_monitor (GTK_WIDGET (shell))));
}

void
view_close_cmd_callback (PikaAction *action,
                         GVariant   *value,
                         gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  PikaImage        *image;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);
  image = pika_display_get_image (display);

  /* Check for the image so we don't close the last display. */
  if (image)
    pika_display_shell_close (shell, FALSE);
}

void
view_scroll_center_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaDisplay *display;
  return_if_no_display (display, data);

  pika_display_shell_scroll_center_image (pika_display_get_shell (display),
                                          TRUE, TRUE);
}

void
view_zoom_fit_in_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplay *display;
  return_if_no_display (display, data);

  pika_display_shell_scale_fit_in (pika_display_get_shell (display));
}

void
view_zoom_fill_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaDisplay *display;
  return_if_no_display (display, data);

  pika_display_shell_scale_fill (pika_display_get_shell (display));
}

void
view_zoom_selection_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaDisplay *display;
  PikaImage   *image;
  gint         x, y, width, height;
  return_if_no_display (display, data);
  return_if_no_image (image, data);

  pika_item_bounds (PIKA_ITEM (pika_image_get_mask (image)),
                    &x, &y, &width, &height);

  pika_display_shell_scale_to_rectangle (pika_display_get_shell (display),
                                         PIKA_ZOOM_IN,
                                         x, y, width, height,
                                         FALSE);
}

void
view_zoom_revert_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplay *display;
  return_if_no_display (display, data);

  pika_display_shell_scale_revert (pika_display_get_shell (display));
}

void
view_zoom_cmd_callback (PikaAction *action,
                        GVariant   *value,
                        gpointer    data)
{
  PikaDisplayShell     *shell;
  PikaActionSelectType  select_type;
  return_if_no_shell (shell, data);

  select_type = (PikaActionSelectType) g_variant_get_int32 (value);

  switch (select_type)
    {
    case PIKA_ACTION_SELECT_FIRST:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_OUT_MAX,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    case PIKA_ACTION_SELECT_LAST:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_IN_MAX,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    case PIKA_ACTION_SELECT_PREVIOUS:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_OUT,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    case PIKA_ACTION_SELECT_NEXT:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_IN,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    case PIKA_ACTION_SELECT_SKIP_PREVIOUS:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_OUT_MORE,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    case PIKA_ACTION_SELECT_SKIP_NEXT:
      pika_display_shell_scale (shell,
                                PIKA_ZOOM_IN_MORE,
                                0.0,
                                PIKA_ZOOM_FOCUS_BEST_GUESS);
      break;

    default:
      {
        gdouble scale = pika_zoom_model_get_factor (shell->zoom);

        scale = action_select_value (select_type,
                                     scale,
                                     0.0, 512.0, 1.0,
                                     1.0 / 8.0, 1.0, 16.0, 0.0,
                                     FALSE);

        /* min = 1.0 / 256,  max = 256.0                */
        /* scale = min *  (max / min)**(i/n), i = 0..n  */
        scale = pow (65536.0, scale / 512.0) / 256.0;

        pika_display_shell_scale (shell,
                                  PIKA_ZOOM_TO,
                                  scale,
                                  PIKA_ZOOM_FOCUS_BEST_GUESS);
        break;
      }
    }
}

void
view_zoom_explicit_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaDisplayShell *shell;
  gint              factor;
  return_if_no_shell (shell, data);

  factor = g_variant_get_int32 (value);

  if (factor != 0 /* not Other... */)
    {
      if (fabs (factor - pika_zoom_model_get_factor (shell->zoom)) > 0.0001)
        pika_display_shell_scale (shell,
                                  PIKA_ZOOM_TO,
                                  (gdouble) factor / 10000,
                                  PIKA_ZOOM_FOCUS_RETAIN_CENTERING_ELSE_BEST_GUESS);
    }
  else
    {
      pika_display_shell_scale_dialog (shell);
    }
}

void
view_show_all_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  active = g_variant_get_boolean (value);

  if (active != shell->show_all)
    {
      PikaImageWindow *window = pika_display_shell_get_window (shell);

      pika_display_shell_set_show_all (shell, active);

      if (window)
        SET_ACTIVE (menus_get_image_manager_singleton (display->pika),
                    "view-show-all", shell->show_all);

      if (IS_ACTIVE_DISPLAY (display))
        SET_ACTIVE (shell->popup_manager, "view-show-all",
                    shell->show_all);
    }
}

void
view_dot_for_dot_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  active = g_variant_get_boolean (value);

  if (active != shell->dot_for_dot)
    {
      PikaImageWindow *window = pika_display_shell_get_window (shell);

      pika_display_shell_scale_set_dot_for_dot (shell, active);

      if (window)
        SET_ACTIVE (menus_get_image_manager_singleton (display->pika),
                    "view-dot-for-dot", shell->dot_for_dot);

      if (IS_ACTIVE_DISPLAY (display))
        SET_ACTIVE (shell->popup_manager, "view-dot-for-dot",
                    shell->dot_for_dot);
    }
}

void
view_flip_horizontally_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  active = g_variant_get_boolean (value);

  if (active != shell->flip_horizontally)
    {
      pika_display_shell_flip (shell, active, shell->flip_vertically);
    }
}

void
view_flip_vertically_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  active = g_variant_get_boolean (value);

  if (active != shell->flip_vertically)
    {
      pika_display_shell_flip (shell, shell->flip_horizontally, active);
    }
}

void
view_flip_reset_cmd_callback (PikaAction *action,
                              GVariant   *value,
                              gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;

  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);
  pika_display_shell_flip (shell, FALSE, FALSE);
}

void
view_rotate_absolute_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplay          *display;
  PikaDisplayShell     *shell;
  PikaActionSelectType  select_type;
  gdouble               angle = 0.0;
  return_if_no_display (display, data);

  select_type = (PikaActionSelectType) g_variant_get_int32 (value);

  shell = pika_display_get_shell (display);

  angle = action_select_value (select_type,
                               0.0,
                               -180.0, 180.0, 0.0,
                               1.0, 15.0, 90.0, 0.0,
                               TRUE);

  pika_display_shell_rotate_to (shell, angle);
}

void
view_rotate_relative_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplay          *display;
  PikaDisplayShell     *shell;
  PikaActionSelectType  select_type;
  gdouble               delta = 0.0;
  return_if_no_display (display, data);

  select_type = (PikaActionSelectType) g_variant_get_int32 (value);

  shell = pika_display_get_shell (display);

  delta = action_select_value (select_type,
                               0.0,
                               -180.0, 180.0, 0.0,
                               1.0, 15.0, 90.0, 0.0,
                               TRUE);

  pika_display_shell_rotate (shell, delta);
}

void
view_rotate_other_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);

  pika_display_shell_rotate_dialog (shell);
}

void
view_reset_cmd_callback (PikaAction *action,
                         GVariant   *value,
                         gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;

  return_if_no_display (display, data);

  shell = pika_display_get_shell (display);
  pika_display_shell_rotate_to (shell, 0.0);
  pika_display_shell_flip (shell, FALSE, FALSE);
}

void
view_scroll_horizontal_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  PikaDisplayShell     *shell;
  GtkAdjustment        *adj;
  PikaActionSelectType  select_type;
  gdouble               offset;
  return_if_no_shell (shell, data);

  select_type = (PikaActionSelectType) g_variant_get_int32 (value);

  adj = shell->hsbdata;

  offset = action_select_value (select_type,
                                gtk_adjustment_get_value (adj),
                                gtk_adjustment_get_lower (adj),
                                gtk_adjustment_get_upper (adj) -
                                gtk_adjustment_get_page_size (adj),
                                gtk_adjustment_get_lower (adj),
                                1,
                                gtk_adjustment_get_step_increment (adj),
                                gtk_adjustment_get_page_increment (adj),
                                0,
                                FALSE);

  gtk_adjustment_set_value (shell->hsbdata, offset);
}

void
view_scroll_vertical_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplayShell     *shell;
  GtkAdjustment        *adj;
  PikaActionSelectType  select_type;
  gdouble               offset;
  return_if_no_shell (shell, data);

  select_type = (PikaActionSelectType) g_variant_get_int32 (value);

  adj = shell->vsbdata;

  offset = action_select_value (select_type,
                                gtk_adjustment_get_value (adj),
                                gtk_adjustment_get_lower (adj),
                                gtk_adjustment_get_upper (adj) -
                                gtk_adjustment_get_page_size (adj),
                                gtk_adjustment_get_lower (adj),
                                1,
                                gtk_adjustment_get_step_increment (adj),
                                gtk_adjustment_get_page_increment (adj),
                                0,
                                FALSE);

  gtk_adjustment_set_value (shell->vsbdata, offset);
}

void
view_navigation_window_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  Pika             *pika;
  PikaDisplayShell *shell;
  return_if_no_pika (pika, data);
  return_if_no_shell (shell, data);

  pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (pika)),
                                             pika,
                                             pika_dialog_factory_get_singleton (),
                                             pika_widget_get_monitor (GTK_WIDGET (shell)),
                                             "pika-navigation-view");
}

void
view_display_filters_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplayShell *shell;
  GtkWidget        *dialog;
  return_if_no_shell (shell, data);

#define FILTERS_DIALOG_KEY "pika-display-filters-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (shell), FILTERS_DIALOG_KEY);

  if (! dialog)
    {
      dialog = pika_display_shell_filter_dialog_new (shell);

      dialogs_attach_dialog (G_OBJECT (shell), FILTERS_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
view_color_management_reset_cmd_callback (PikaAction *action,
                                          GVariant   *value,
                                          gpointer    data)
{
  PikaDisplayShell *shell;
  PikaColorConfig  *global_config;
  PikaColorConfig  *shell_config;
  return_if_no_shell (shell, data);

  global_config = PIKA_CORE_CONFIG (shell->display->config)->color_management;
  shell_config  = pika_display_shell_get_color_config (shell);

  pika_config_copy (PIKA_CONFIG (global_config),
                    PIKA_CONFIG (shell_config),
                    0);
  shell->color_config_set = FALSE;
}

void
view_color_management_enable_cmd_callback (PikaAction *action,
                                           GVariant   *value,
                                           gpointer    data)
{
  PikaDisplayShell        *shell;
  PikaColorConfig         *color_config;
  PikaColorManagementMode  mode;
  gboolean                 active;
  return_if_no_shell (shell, data);

  color_config = pika_display_shell_get_color_config (shell);

  active = g_variant_get_boolean (value);

  mode = pika_color_config_get_mode (color_config);

  if (active)
    {
      if (mode != PIKA_COLOR_MANAGEMENT_SOFTPROOF)
        mode = PIKA_COLOR_MANAGEMENT_DISPLAY;
    }
  else
    {
      mode = PIKA_COLOR_MANAGEMENT_OFF;
    }

  if (mode != pika_color_config_get_mode (color_config))
    {
      g_object_set (color_config,
                    "mode", mode,
                    NULL);
      shell->color_config_set = TRUE;
    }
}

void
view_color_management_softproof_cmd_callback (PikaAction *action,
                                              GVariant   *value,
                                              gpointer    data)
{
  PikaDisplayShell        *shell;
  PikaColorConfig         *color_config;
  PikaColorManagementMode  mode;
  gboolean                 active;
  return_if_no_shell (shell, data);

  color_config = pika_display_shell_get_color_config (shell);

  active = g_variant_get_boolean (value);

  mode = pika_color_config_get_mode (color_config);

  if (active)
    {
      mode = PIKA_COLOR_MANAGEMENT_SOFTPROOF;
    }
  else
    {
      if (mode != PIKA_COLOR_MANAGEMENT_OFF)
        mode = PIKA_COLOR_MANAGEMENT_DISPLAY;
    }

  if (mode != pika_color_config_get_mode (color_config))
    {
      g_object_set (color_config,
                    "mode", mode,
                    NULL);
      shell->color_config_set = TRUE;
    }
}

void
view_display_intent_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaDisplayShell          *shell;
  PikaColorConfig           *color_config;
  PikaColorRenderingIntent   intent;
  return_if_no_shell (shell, data);

  intent = (PikaColorRenderingIntent) g_variant_get_int32 (value);

  color_config = pika_display_shell_get_color_config (shell);

  if (intent != pika_color_config_get_display_intent (color_config))
    {
      g_object_set (color_config,
                    "display-rendering-intent", intent,
                    NULL);
      shell->color_config_set = TRUE;
    }
}

void
view_display_bpc_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplayShell *shell;
  PikaColorConfig  *color_config;
  gboolean          active;
  return_if_no_shell (shell, data);

  color_config = pika_display_shell_get_color_config (shell);

  active = g_variant_get_boolean (value);

  if (active != pika_color_config_get_display_bpc (color_config))
    {
      g_object_set (color_config,
                    "display-use-black-point-compensation", active,
                    NULL);
      shell->color_config_set = TRUE;
    }
}

void
view_softproof_gamut_check_cmd_callback (PikaAction *action,
                                         GVariant   *value,
                                         gpointer    data)
{
  PikaDisplayShell *shell;
  PikaColorConfig  *color_config;
  gboolean          active;
  return_if_no_shell (shell, data);

  color_config = pika_display_shell_get_color_config (shell);

  active = g_variant_get_boolean (value);

  if (active != pika_color_config_get_simulation_gamut_check (color_config))
    {
      g_object_set (color_config,
                    "simulation-gamut-check", active,
                    NULL);
      shell->color_config_set = TRUE;
    }
}

void
view_toggle_selection_cmd_callback (PikaAction *action,
                                    GVariant   *value,
                                    gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_selection (shell))
    {
      pika_display_shell_set_show_selection (shell, active);
    }
}

void
view_toggle_layer_boundary_cmd_callback (PikaAction *action,
                                         GVariant   *value,
                                         gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_layer (shell))
    {
      pika_display_shell_set_show_layer (shell, active);
    }
}

void
view_toggle_canvas_boundary_cmd_callback (PikaAction *action,
                                          GVariant   *value,
                                          gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_canvas (shell))
    {
      pika_display_shell_set_show_canvas (shell, active);
    }
}

void
view_toggle_menubar_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_menubar (shell))
    {
      pika_display_shell_set_show_menubar (shell, active);
    }
}

void
view_toggle_rulers_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_rulers (shell))
    {
      pika_display_shell_set_show_rulers (shell, active);
    }
}

void
view_toggle_scrollbars_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_scrollbars (shell))
    {
      pika_display_shell_set_show_scrollbars (shell, active);
    }
}

void
view_toggle_statusbar_cmd_callback (PikaAction *action,
                                    GVariant   *value,
                                    gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_statusbar (shell))
    {
      pika_display_shell_set_show_statusbar (shell, active);
    }
}

void
view_toggle_guides_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_guides (shell))
    {
      pika_display_shell_set_show_guides (shell, active);
    }
}

void
view_toggle_grid_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_grid (shell))
    {
      pika_display_shell_set_show_grid (shell, active);
    }
}

void
view_toggle_sample_points_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_show_sample_points (shell))
    {
      pika_display_shell_set_show_sample_points (shell, active);
    }
}

void
view_snap_to_guides_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_guides (shell))
    {
      pika_display_shell_set_snap_to_guides (shell, active);
    }
}

void
view_snap_to_grid_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_grid (shell))
    {
      pika_display_shell_set_snap_to_grid (shell, active);
    }
}

void
view_snap_to_canvas_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_canvas (shell))
    {
      pika_display_shell_set_snap_to_canvas (shell, active);
    }
}

void
view_snap_to_vectors_cmd_callback (PikaAction *action,
                                   GVariant   *value,
                                   gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_vectors (shell))
    {
      pika_display_shell_set_snap_to_vectors (shell, active);
    }
}

void
view_snap_to_bbox_cmd_callback (PikaAction *action,
                                GVariant   *value,
                                gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_bbox (shell))
    {
      pika_display_shell_set_snap_to_bbox (shell, active);
    }
}

void
view_snap_to_equidistance_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_snap_to_equidistance (shell))
    pika_display_shell_set_snap_to_equidistance (shell, active);
}

void
view_padding_color_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaDisplay           *display;
  PikaImageWindow       *window;
  PikaDisplayShell      *shell;
  PikaDisplayOptions    *options;
  PikaCanvasPaddingMode  padding_mode;
  gboolean               fullscreen;
  return_if_no_display (display, data);

  padding_mode = (PikaCanvasPaddingMode) g_variant_get_int32 (value);

  shell  = pika_display_get_shell (display);
  window = pika_display_shell_get_window (shell);

  if (window)
    fullscreen = pika_image_window_get_fullscreen (window);
  else
    fullscreen = FALSE;

  if (fullscreen)
    options = shell->fullscreen_options;
  else
    options = shell->options;

#define PADDING_COLOR_DIALOG_KEY "pika-padding-color-dialog"

  switch (padding_mode)
    {
    case PIKA_CANVAS_PADDING_MODE_DEFAULT:
    case PIKA_CANVAS_PADDING_MODE_LIGHT_CHECK:
    case PIKA_CANVAS_PADDING_MODE_DARK_CHECK:
      dialogs_destroy_dialog (G_OBJECT (shell), PADDING_COLOR_DIALOG_KEY);

      options->padding_mode_set = TRUE;

      pika_display_shell_set_padding (shell, padding_mode,
                                      &options->padding_color);
      break;

    case PIKA_CANVAS_PADDING_MODE_CUSTOM:
      {
        GtkWidget             *dialog;
        PikaRGB               *old_color = g_new (PikaRGB, 1);
        PikaCanvasPaddingMode  old_padding_mode;

        dialog = dialogs_get_dialog (G_OBJECT (shell), PADDING_COLOR_DIALOG_KEY);

        if (! dialog)
          {
            PikaImage        *image = pika_display_get_image (display);
            PikaDisplayShell *shell = pika_display_get_shell (display);

            dialog =
              pika_color_dialog_new (PIKA_VIEWABLE (image),
                                     action_data_get_context (data),
                                     FALSE,
                                     _("Set Canvas Padding Color"),
                                     PIKA_ICON_FONT,
                                     _("Set Custom Canvas Padding Color"),
                                     GTK_WIDGET (shell),
                                     NULL, NULL,
                                     &options->padding_color,
                                     TRUE, FALSE);

            g_signal_connect (dialog, "update",
                              G_CALLBACK (view_padding_color_dialog_update),
                              shell);

            dialogs_attach_dialog (G_OBJECT (shell),
                                   PADDING_COLOR_DIALOG_KEY, dialog);
          }
        *old_color       = options->padding_color;
        old_padding_mode = options->padding_mode;
        g_object_set_data_full (G_OBJECT (dialog), "old-color",
                                old_color, g_free);
        g_object_set_data (G_OBJECT (dialog), "old-padding-mode",
                           GINT_TO_POINTER (old_padding_mode));

        gtk_window_present (GTK_WINDOW (dialog));
      }
      break;

    case PIKA_CANVAS_PADDING_MODE_RESET:
      dialogs_destroy_dialog (G_OBJECT (shell), PADDING_COLOR_DIALOG_KEY);

      {
        PikaDisplayOptions *default_options;

        options->padding_mode_set = FALSE;

        if (fullscreen)
          default_options = display->config->default_fullscreen_view;
        else
          default_options = display->config->default_view;

        pika_display_shell_set_padding (shell,
                                        default_options->padding_mode,
                                        &default_options->padding_color);
        pika_display_shell_set_padding_in_show_all (shell,
                                                    default_options->padding_in_show_all);
      }
      break;
    }
}

void
view_padding_color_in_show_all_cmd_callback (PikaAction *action,
                                             GVariant   *value,
                                             gpointer    data)
{
  PikaDisplayShell *shell;
  gboolean          active;
  return_if_no_shell (shell, data);

  active = g_variant_get_boolean (value);

  if (active != pika_display_shell_get_padding_in_show_all (shell))
    {
      pika_display_shell_set_padding_in_show_all (shell, active);
    }
}

void
view_shrink_wrap_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaDisplayShell *shell;
  return_if_no_shell (shell, data);

  pika_display_shell_scale_shrink_wrap (shell, FALSE);
}

void
view_fullscreen_cmd_callback (PikaAction *action,
                              GVariant   *value,
                              gpointer    data)
{
  PikaDisplay      *display;
  PikaDisplayShell *shell;
  PikaImageWindow  *window;
  return_if_no_display (display, data);

  shell  = pika_display_get_shell (display);
  window = pika_display_shell_get_window (shell);

  if (window)
    {
      gboolean active = g_variant_get_boolean (value);

      pika_image_window_set_fullscreen (window, active);
    }
}


/*  private functions  */

static void
view_padding_color_dialog_update (PikaColorDialog      *dialog,
                                  const PikaRGB        *color,
                                  PikaColorDialogState  state,
                                  PikaDisplayShell     *shell)
{
  PikaImageWindow       *window;
  PikaDisplayOptions    *options;
  PikaRGB               *old_color;
  PikaCanvasPaddingMode  old_padding_mode;
  gboolean               fullscreen;

  window           = pika_display_shell_get_window (shell);
  old_color        = g_object_get_data (G_OBJECT (dialog), "old-color");
  old_padding_mode = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dialog), "old-padding-mode"));

  g_return_if_fail (old_color);

  if (window)
    fullscreen = pika_image_window_get_fullscreen (window);
  else
    fullscreen = FALSE;

  if (fullscreen)
    options = shell->fullscreen_options;
  else
    options = shell->options;

  switch (state)
    {
    case PIKA_COLOR_DIALOG_OK:
      options->padding_mode_set = TRUE;

      pika_display_shell_set_padding (shell, PIKA_CANVAS_PADDING_MODE_CUSTOM,
                                      color);
      gtk_widget_destroy (GTK_WIDGET (dialog));
      break;

    case PIKA_COLOR_DIALOG_CANCEL:
      pika_display_shell_set_padding (shell,
                                      old_padding_mode,
                                      old_color);
      gtk_widget_destroy (GTK_WIDGET (dialog));
      break;

    case PIKA_COLOR_DIALOG_UPDATE:
      pika_display_shell_set_padding (shell, PIKA_CANVAS_PADDING_MODE_CUSTOM,
                                      color);
      break;

    default:
      break;
    }
}
