/* 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 "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "operations/layer-modes/pika-layer-modes.h"

#include "core/pikachannel.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikalayer-floating-selection.h"

#include "text/pikatextlayer.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikaactiongroup.h"
#include "widgets/pikawidgets-utils.h"

#include "actions.h"
#include "image-commands.h"
#include "items-actions.h"
#include "layers-actions.h"
#include "layers-commands.h"

#include "pika-intl.h"


static const PikaActionEntry layers_actions[] =
{
  { "layers-edit", PIKA_ICON_EDIT,
    NC_("layers-action", "Default Edit Action"), NULL, { NULL },
    NC_("layers-action", "Activate the default edit action for this type of layer"),
    layers_edit_cmd_callback,
    PIKA_HELP_LAYER_EDIT },

  { "layers-edit-text", PIKA_ICON_EDIT,
    NC_("layers-action", "Edit Te_xt on canvas"), NULL, { NULL },
    NC_("layers-action", "Edit this text layer content on canvas"),
    layers_edit_text_cmd_callback,
    PIKA_HELP_LAYER_EDIT },

  { "layers-edit-attributes", PIKA_ICON_EDIT,
    NC_("layers-action", "_Edit Layer Attributes..."), NULL, { NULL },
    NC_("layers-action", "Edit the layer's name"),
    layers_edit_attributes_cmd_callback,
    PIKA_HELP_LAYER_EDIT },

  { "layers-new", PIKA_ICON_DOCUMENT_NEW,
    NC_("layers-action", "_New Layer..."), NULL, { "<primary><shift>N", NULL },
    NC_("layers-action", "Create a new layer and add it to the image"),
    layers_new_cmd_callback,
    PIKA_HELP_LAYER_NEW },

  { "layers-new-last-values", PIKA_ICON_DOCUMENT_NEW,
    NC_("layers-action", "_New Layer"), NULL, { NULL },
    NC_("layers-action", "Create new layers with last used values"),
    layers_new_last_vals_cmd_callback,
    PIKA_HELP_LAYER_NEW },

  { "layers-new-from-visible", NULL,
    NC_("layers-action", "New from _Visible"), NULL, { NULL },
    NC_("layers-action",
        "Create a new layer from what is visible in this image"),
    layers_new_from_visible_cmd_callback,
    PIKA_HELP_LAYER_NEW_FROM_VISIBLE },

  { "layers-new-group", PIKA_ICON_FOLDER_NEW,
    NC_("layers-action", "New Layer _Group"), NULL, { NULL },
    NC_("layers-action", "Create a new layer group and add it to the image"),
    layers_new_group_cmd_callback,
    PIKA_HELP_LAYER_NEW },

  { "layers-duplicate", PIKA_ICON_OBJECT_DUPLICATE,
    NC_("layers-action", "D_uplicate Layer"), NULL, { "<primary><shift>D", NULL },
    NC_("layers-action",
        "Create duplicates of selected layers and add them to the image"),
    layers_duplicate_cmd_callback,
    PIKA_HELP_LAYER_DUPLICATE },

  { "layers-delete", PIKA_ICON_EDIT_DELETE,
    NC_("layers-action", "_Delete Layers"), NULL, { NULL },
    NC_("layers-action", "Delete selected layers"),
    layers_delete_cmd_callback,
    PIKA_HELP_LAYER_DELETE },

  { "layers-raise", PIKA_ICON_GO_UP,
    NC_("layers-action", "_Raise Layer"), NULL, { NULL },
    NC_("layers-action", "Raise this layer one step in the layer stack"),
    layers_raise_cmd_callback,
    PIKA_HELP_LAYER_RAISE },

  { "layers-raise-to-top", PIKA_ICON_GO_TOP,
    NC_("layers-action", "Layer to _Top"), NULL, { NULL },
    NC_("layers-action", "Move this layer to the top of the layer stack"),
    layers_raise_to_top_cmd_callback,
    PIKA_HELP_LAYER_RAISE_TO_TOP },

  { "layers-lower", PIKA_ICON_GO_DOWN,
    NC_("layers-action", "_Lower Layer"), NULL, { NULL },
    NC_("layers-action", "Lower this layer one step in the layer stack"),
    layers_lower_cmd_callback,
    PIKA_HELP_LAYER_LOWER },

  { "layers-lower-to-bottom", PIKA_ICON_GO_BOTTOM,
    NC_("layers-action", "Layer to _Bottom"), NULL, { NULL },
    NC_("layers-action", "Move this layer to the bottom of the layer stack"),
    layers_lower_to_bottom_cmd_callback,
    PIKA_HELP_LAYER_LOWER_TO_BOTTOM },

  { "layers-anchor", PIKA_ICON_LAYER_ANCHOR,
    NC_("layers-action", "_Anchor Floating Layer or Mask"), NULL, { "<primary>H", NULL },
    NC_("layers-action", "Anchor the floating layer or mask"),
    layers_anchor_cmd_callback,
    PIKA_HELP_LAYER_ANCHOR },

  { "layers-merge-down", PIKA_ICON_LAYER_MERGE_DOWN,
    NC_("layers-action", "Merge Do_wn"), NULL, { NULL },
    NC_("layers-action", "Merge these layers with the first visible layer below each"),
    layers_merge_down_cmd_callback,
    PIKA_HELP_LAYER_MERGE_DOWN },

  /* this is the same as layers-merge-down, except it's sensitive even if
   * the layer can't be merged down
   */
  { "layers-merge-down-button", PIKA_ICON_LAYER_MERGE_DOWN,
    NC_("layers-action", "Merge Do_wn"), NULL, { NULL },
    NC_("layers-action", "Merge these layers with the first visible layer below each"),
    layers_merge_down_cmd_callback,
    PIKA_HELP_LAYER_MERGE_DOWN },

  { "layers-merge-group", NULL,
    NC_("layers-action", "Merge Layer Groups"), NULL, { NULL },
    NC_("layers-action", "Merge the layer groups' layers into one normal layer"),
    layers_merge_group_cmd_callback,
    PIKA_HELP_LAYER_MERGE_GROUP },

  { "layers-merge-layers", NULL,
    NC_("layers-action", "Merge _Visible Layers..."), NULL, { NULL },
    NC_("layers-action", "Merge all visible layers into one layer"),
    image_merge_layers_cmd_callback,
    PIKA_HELP_IMAGE_MERGE_LAYERS },

  { "layers-merge-layers-last-values", NULL,
    NC_("layers-action", "Merge _Visible Layers"), NULL, { NULL },
    NC_("layers-action", "Merge all visible layers with last used values"),
    image_merge_layers_last_vals_cmd_callback,
    PIKA_HELP_IMAGE_MERGE_LAYERS },

  { "layers-flatten-image", NULL,
    NC_("layers-action", "_Flatten Image"), NULL, { NULL },
    NC_("layers-action", "Merge all layers into one and remove transparency"),
    image_flatten_image_cmd_callback,
    PIKA_HELP_IMAGE_FLATTEN },

  { "layers-text-discard", PIKA_ICON_TOOL_TEXT,
    NC_("layers-action", "_Discard Text Information"), NULL, { NULL },
    NC_("layers-action", "Turn these text layers into normal layers"),
    layers_text_discard_cmd_callback,
    PIKA_HELP_LAYER_TEXT_DISCARD },

  { "layers-text-to-vectors", PIKA_ICON_TOOL_TEXT,
    NC_("layers-action", "Text to _Path"), NULL, { NULL },
    NC_("layers-action", "Create paths from text layers"),
    layers_text_to_vectors_cmd_callback,
    PIKA_HELP_LAYER_TEXT_TO_PATH },

  { "layers-text-along-vectors", PIKA_ICON_TOOL_TEXT,
    NC_("layers-action", "Text alon_g Path"), NULL, { NULL },
    NC_("layers-action", "Warp this layer's text along the current path"),
    layers_text_along_vectors_cmd_callback,
    PIKA_HELP_LAYER_TEXT_ALONG_PATH },

  { "layers-resize", PIKA_ICON_OBJECT_RESIZE,
    NC_("layers-action", "Layer B_oundary Size..."), NULL, { NULL },
    NC_("layers-action", "Adjust the layer dimensions"),
    layers_resize_cmd_callback,
    PIKA_HELP_LAYER_RESIZE },

  { "layers-resize-to-image", PIKA_ICON_LAYER_TO_IMAGESIZE,
    NC_("layers-action", "Layers to _Image Size"), NULL, { NULL },
    NC_("layers-action", "Resize the layers to the size of the image"),
    layers_resize_to_image_cmd_callback,
    PIKA_HELP_LAYER_RESIZE_TO_IMAGE },

  { "layers-scale", PIKA_ICON_OBJECT_SCALE,
    NC_("layers-action", "_Scale Layer..."), NULL, { NULL },
    NC_("layers-action", "Change the size of the layer content"),
    layers_scale_cmd_callback,
    PIKA_HELP_LAYER_SCALE },

  { "layers-crop-to-selection", PIKA_ICON_TOOL_CROP,
    NC_("layers-action", "_Resize Layers to Selection"), NULL, { NULL },
    NC_("layers-action", "Resize the layers to the extents of the selection"),
    layers_crop_to_selection_cmd_callback,
    PIKA_HELP_LAYER_RESIZE_TO_SELECTION },

  { "layers-crop-to-content", PIKA_ICON_TOOL_CROP,
    NC_("layers-action", "Crop Layers to C_ontent"), NULL, { NULL },
    NC_("layers-action", "Crop the layers to the extents of their content (remove empty borders from the layer)"),
    layers_crop_to_content_cmd_callback,
    PIKA_HELP_LAYER_CROP_TO_CONTENT },

  { "layers-mask-add", PIKA_ICON_LAYER_MASK,
    NC_("layers-action", "Add La_yer Masks..."), NULL, { NULL },
    NC_("layers-action",
        "Add masks to selected layers that allows non-destructive editing of transparency"),
    layers_mask_add_cmd_callback,
    PIKA_HELP_LAYER_MASK_ADD },

  /* this is the same as layers-mask-add, except it's sensitive even if
   * there is a mask on the layer
   */
  { "layers-mask-add-button", PIKA_ICON_LAYER_MASK,
    NC_("layers-action", "Add La_yer Masks..."), NULL, { NULL },
    NC_("layers-action",
        "Add masks to selected layers that allows non-destructive editing of transparency"),
    layers_mask_add_cmd_callback,
    PIKA_HELP_LAYER_MASK_ADD },

  { "layers-mask-add-last-values", PIKA_ICON_LAYER_MASK,
    NC_("layers-action", "Add La_yer Masks with Last Values"), NULL, { NULL },
    NC_("layers-action",
        "Add mask to selected layers with last used values"),
    layers_mask_add_last_vals_cmd_callback,
    PIKA_HELP_LAYER_MASK_ADD },

  { "layers-alpha-add", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Add Alpha C_hannel"), NULL, { NULL },
    NC_("layers-action", "Add transparency information to the layer"),
    layers_alpha_add_cmd_callback,
    PIKA_HELP_LAYER_ALPHA_ADD },

  { "layers-alpha-remove", NULL,
    NC_("layers-action", "_Remove Alpha Channel"), NULL, { NULL },
    NC_("layers-action", "Remove transparency information from the layer"),
    layers_alpha_remove_cmd_callback,
    PIKA_HELP_LAYER_ALPHA_REMOVE }
};

static const PikaToggleActionEntry layers_toggle_actions[] =
{
  { "layers-mask-edit", PIKA_ICON_EDIT,
    NC_("layers-action", "_Edit Layer Mask"), NULL, { NULL },
    NC_("layers-action", "Work on the layer mask"),
    layers_mask_edit_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_MASK_EDIT },

  { "layers-mask-show", PIKA_ICON_VISIBLE,
    NC_("layers-action", "S_how Layer Masks"), NULL, { NULL }, NULL,
    layers_mask_show_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_MASK_SHOW },

  { "layers-mask-disable", NULL,
    NC_("layers-action", "_Disable Layer Masks"), NULL, { NULL },
    NC_("layers-action", "Dismiss the effect of the layer mask"),
    layers_mask_disable_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_MASK_DISABLE },

  { "layers-visible", PIKA_ICON_VISIBLE,
    NC_("layers-action", "Toggle Layer _Visibility"), NULL, { NULL }, NULL,
    layers_visible_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_VISIBLE },

  { "layers-lock-content", PIKA_ICON_LOCK_CONTENT,
    NC_("layers-action", "L_ock Pixels of Layer"), NULL, { NULL }, NULL,
    layers_lock_content_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_LOCK_PIXELS },

  { "layers-lock-position", PIKA_ICON_LOCK_POSITION,
    NC_("layers-action", "L_ock Position of Layer"), NULL, { NULL }, NULL,
    layers_lock_position_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_LOCK_POSITION },

  { "layers-lock-alpha", PIKA_ICON_LOCK_ALPHA,
    NC_("layers-action", "Lock Alph_a Channel"), NULL, { NULL },
    NC_("layers-action",
        "Keep transparency information on this layer from being modified"),
    layers_lock_alpha_cmd_callback,
    FALSE,
    PIKA_HELP_LAYER_LOCK_ALPHA },
};

static const PikaRadioActionEntry layers_blend_space_actions[] =
{
  { "layers-blend-space-auto", NULL,
    NC_("layers-action", "Auto"), NULL, { NULL },
    NC_("layers-action", "Layer Blend Space: Auto"),
    PIKA_LAYER_COLOR_SPACE_AUTO,
    NULL },

  { "layers-blend-space-rgb-linear", NULL,
    NC_("layers-action", "RGB (linear)"), NULL, { NULL },
    NC_("layers-action", "Layer Blend Space: RGB (linear)"),
    PIKA_LAYER_COLOR_SPACE_RGB_LINEAR,
    NULL },

  { "layers-blend-space-rgb-perceptual", NULL,
    NC_("layers-action", "RGB (perceptual)"), NULL, { NULL },
    NC_("layers-action", "Layer Blend Space: RGB (perceptual)"),
    PIKA_LAYER_COLOR_SPACE_RGB_PERCEPTUAL,
    NULL }
};

static const PikaRadioActionEntry layers_composite_space_actions[] =
{
  { "layers-composite-space-auto", NULL,
    NC_("layers-action", "Auto"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Space: Auto"),
    PIKA_LAYER_COLOR_SPACE_AUTO,
    NULL },

  { "layers-composite-space-rgb-linear", NULL,
    NC_("layers-action", "RGB (linear)"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Space: RGB (linear)"),
    PIKA_LAYER_COLOR_SPACE_RGB_LINEAR,
    NULL },

  { "layers-composite-space-rgb-perceptual", NULL,
    NC_("layers-action", "RGB (perceptual)"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Space: RGB (perceptual)"),
    PIKA_LAYER_COLOR_SPACE_RGB_PERCEPTUAL,
    NULL }
};

static const PikaRadioActionEntry layers_composite_mode_actions[] =
{
  { "layers-composite-mode-auto", NULL,
    NC_("layers-action", "Auto"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Mode: Auto"),
    PIKA_LAYER_COMPOSITE_AUTO,
    NULL },

  { "layers-composite-mode-union", NULL,
    NC_("layers-action", "Union"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Mode: Union"),
    PIKA_LAYER_COMPOSITE_UNION,
    NULL },

  { "layers-composite-mode-clip-to-backdrop", NULL,
    NC_("layers-action", "Clip to Backdrop"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Mode: Clip to Backdrop"),
    PIKA_LAYER_COMPOSITE_CLIP_TO_BACKDROP,
    NULL },

  { "layers-composite-mode-clip-to-layer", NULL,
    NC_("layers-action", "Clip to Layer"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Mode: Clip to Layer"),
    PIKA_LAYER_COMPOSITE_CLIP_TO_LAYER,
    NULL },

  { "layers-composite-mode-intersection", NULL,
    NC_("layers-action", "Intersection"), NULL, { NULL },
    NC_("layers-action", "Layer Composite Mode: Intersection"),
    PIKA_LAYER_COMPOSITE_INTERSECTION,
    NULL }
};

static const PikaEnumActionEntry layers_color_tag_actions[] =
{
  { "layers-color-tag-none", PIKA_ICON_EDIT_CLEAR,
    NC_("layers-action", "None"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Clear"),
    PIKA_COLOR_TAG_NONE, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-blue", NULL,
    NC_("layers-action", "Blue"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Blue"),
    PIKA_COLOR_TAG_BLUE, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-green", NULL,
    NC_("layers-action", "Green"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Green"),
    PIKA_COLOR_TAG_GREEN, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-yellow", NULL,
    NC_("layers-action", "Yellow"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Yellow"),
    PIKA_COLOR_TAG_YELLOW, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-orange", NULL,
    NC_("layers-action", "Orange"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Orange"),
    PIKA_COLOR_TAG_ORANGE, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-brown", NULL,
    NC_("layers-action", "Brown"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Brown"),
    PIKA_COLOR_TAG_BROWN, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-red", NULL,
    NC_("layers-action", "Red"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Red"),
    PIKA_COLOR_TAG_RED, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-violet", NULL,
    NC_("layers-action", "Violet"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Violet"),
    PIKA_COLOR_TAG_VIOLET, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG },

  { "layers-color-tag-gray", NULL,
    NC_("layers-action", "Gray"), NULL, { NULL },
    NC_("layers-action", "Layer Color Tag: Set to Gray"),
    PIKA_COLOR_TAG_GRAY, FALSE,
    PIKA_HELP_LAYER_COLOR_TAG }
};

static const PikaEnumActionEntry layers_mask_apply_actions[] =
{
  { "layers-mask-apply", NULL,
    NC_("layers-action", "Apply Layer _Mask"), NULL, { NULL },
    NC_("layers-action", "Apply the effect of the layer mask and remove it"),
    PIKA_MASK_APPLY, FALSE,
    PIKA_HELP_LAYER_MASK_APPLY },

  { "layers-mask-delete", PIKA_ICON_EDIT_DELETE,
    NC_("layers-action", "Delete Layer Mas_k"), NULL, { NULL },
    NC_("layers-action", "Remove layer masks and their effect"),
    PIKA_MASK_DISCARD, FALSE,
    PIKA_HELP_LAYER_MASK_DELETE }
};

static const PikaEnumActionEntry layers_mask_to_selection_actions[] =
{
  { "layers-mask-selection-replace", PIKA_ICON_SELECTION_REPLACE,
    NC_("layers-action", "_Masks to Selection"), NULL, { NULL },
    NC_("layers-action", "Replace the selection with the layer masks"),
    PIKA_CHANNEL_OP_REPLACE, FALSE,
    PIKA_HELP_LAYER_MASK_SELECTION_REPLACE },

  { "layers-mask-selection-add", PIKA_ICON_SELECTION_ADD,
    NC_("layers-action", "_Add Masks to Selection"), NULL, { NULL },
    NC_("layers-action", "Add the layer masks to the current selection"),
    PIKA_CHANNEL_OP_ADD, FALSE,
    PIKA_HELP_LAYER_MASK_SELECTION_ADD },

  { "layers-mask-selection-subtract", PIKA_ICON_SELECTION_SUBTRACT,
    NC_("layers-action", "_Subtract Masks from Selection"), NULL, { NULL },
    NC_("layers-action", "Subtract the layer masks from the current selection"),
    PIKA_CHANNEL_OP_SUBTRACT, FALSE,
    PIKA_HELP_LAYER_MASK_SELECTION_SUBTRACT },

  { "layers-mask-selection-intersect", PIKA_ICON_SELECTION_INTERSECT,
    NC_("layers-action", "_Intersect Masks with Selection"), NULL, { NULL },
    NC_("layers-action", "Intersect the layer masks with the current selection"),
    PIKA_CHANNEL_OP_INTERSECT, FALSE,
    PIKA_HELP_LAYER_MASK_SELECTION_INTERSECT }
};

static const PikaEnumActionEntry layers_alpha_to_selection_actions[] =
{
  { "layers-alpha-selection-replace", PIKA_ICON_SELECTION_REPLACE,
    NC_("layers-action", "Al_pha to Selection"), NULL, { NULL },
    NC_("layers-action",
        "Replace the selection with the layer's alpha channel"),
    PIKA_CHANNEL_OP_REPLACE, FALSE,
    PIKA_HELP_LAYER_ALPHA_SELECTION_REPLACE },

  { "layers-alpha-selection-add", PIKA_ICON_SELECTION_ADD,
    NC_("layers-action", "A_dd Alpha to Selection"), NULL, { NULL },
    NC_("layers-action",
        "Add the layer's alpha channel to the current selection"),
    PIKA_CHANNEL_OP_ADD, FALSE,
    PIKA_HELP_LAYER_ALPHA_SELECTION_ADD },

  { "layers-alpha-selection-subtract", PIKA_ICON_SELECTION_SUBTRACT,
    NC_("layers-action", "_Subtract Alpha from Selection"), NULL, { NULL },
    NC_("layers-action",
        "Subtract the layer's alpha channel from the current selection"),
    PIKA_CHANNEL_OP_SUBTRACT, FALSE,
    PIKA_HELP_LAYER_ALPHA_SELECTION_SUBTRACT },

  { "layers-alpha-selection-intersect", PIKA_ICON_SELECTION_INTERSECT,
    NC_("layers-action", "_Intersect Alpha with Selection"), NULL, { NULL },
    NC_("layers-action",
        "Intersect the layer's alpha channel with the current selection"),
    PIKA_CHANNEL_OP_INTERSECT, FALSE,
    PIKA_HELP_LAYER_ALPHA_SELECTION_INTERSECT }
};

static const PikaEnumActionEntry layers_select_actions[] =
{
  { "layers-select-top", NULL,
    NC_("layers-action", "Select _Top Layer"), NULL, { "Home", NULL },
    NC_("layers-action", "Select the topmost layer"),
    PIKA_ACTION_SELECT_FIRST, FALSE,
    PIKA_HELP_LAYER_TOP },

  { "layers-select-bottom", NULL,
    NC_("layers-action", "Select _Bottom Layer"), NULL, { "End", NULL },
    NC_("layers-action", "Select the bottommost layer"),
    PIKA_ACTION_SELECT_LAST, FALSE,
    PIKA_HELP_LAYER_BOTTOM },

  { "layers-select-previous", NULL,
    NC_("layers-action", "Select _Previous Layers"), NULL, { "Prior", NULL },
    NC_("layers-action", "Select the layers above the current layers"),
    PIKA_ACTION_SELECT_PREVIOUS, FALSE,
    PIKA_HELP_LAYER_PREVIOUS },

  { "layers-select-next", NULL,
    NC_("layers-action", "Select _Next Layers"), NULL, { "Next", NULL },
    NC_("layers-action", "Select the layers below the current layers"),
    PIKA_ACTION_SELECT_NEXT, FALSE,
    PIKA_HELP_LAYER_NEXT }
};

static const PikaEnumActionEntry layers_opacity_actions[] =
{
  { "layers-opacity-set", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Set"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_SET, TRUE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-transparent", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make Completely Transparent"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_FIRST, FALSE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-opaque", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make Completely Opaque"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_LAST, FALSE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-decrease", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make More Transparent"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_PREVIOUS, FALSE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-increase", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make More Opaque"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_NEXT, FALSE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-decrease-skip", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make 10% More Transparent"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_SKIP_PREVIOUS, FALSE,
    PIKA_HELP_LAYER_OPACITY },
  { "layers-opacity-increase-skip", PIKA_ICON_TRANSPARENCY,
    NC_("layers-action", "Layer Opacity: Make 10% More Opaque"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_SKIP_NEXT, FALSE,
    PIKA_HELP_LAYER_OPACITY }
};

static const PikaEnumActionEntry layers_mode_actions[] =
{
  { "layers-mode-first", PIKA_ICON_TOOL_PENCIL,
    NC_("layers-action", "Layer Mode: Select First"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_FIRST, FALSE,
    PIKA_HELP_LAYER_MODE },
  { "layers-mode-last", PIKA_ICON_TOOL_PENCIL,
    NC_("layers-action", "Layer Mode: Select Last"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_LAST, FALSE,
    PIKA_HELP_LAYER_MODE },
  { "layers-mode-previous", PIKA_ICON_TOOL_PENCIL,
    NC_("layers-action", "Layer Mode: Select Previous"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_PREVIOUS, FALSE,
    PIKA_HELP_LAYER_MODE },
  { "layers-mode-next", PIKA_ICON_TOOL_PENCIL,
    NC_("layers-action", "Layer Mode: Select Next"), NULL, { NULL }, NULL,
    PIKA_ACTION_SELECT_NEXT, FALSE,
    PIKA_HELP_LAYER_MODE }
};

/**
 * layers_actions_fix_tooltip:
 * @group:
 * @action:
 * @modifiers:
 *
 * Make layer alpha to selection click-shortcuts discoverable, at
 * least in theory.
 **/
static void
layers_actions_fix_tooltip (PikaActionGroup *group,
                            const gchar     *action,
                            GdkModifierType  modifiers)
{
  const gchar *old_hint;
  gchar       *new_hint;

  old_hint = pika_action_group_get_action_tooltip (group,
                                                   action);
  new_hint = g_strconcat (old_hint,
                          "\n",
                          /* Will be followed with e.g. "Shift-Click
                             on thumbnail"
                           */
                          _("Shortcut: "),
                          pika_get_mod_string (modifiers),
                          /* Will be prepended with a modifier key
                             string, e.g. "Shift"
                           */
                          _("-Click on thumbnail in Layers dockable"),
                          NULL);

  pika_action_group_set_action_tooltip (group, action, new_hint);
  g_free (new_hint);
}

void
layers_actions_setup (PikaActionGroup *group)
{
  GdkDisplay      *display = gdk_display_get_default ();
  GdkModifierType  extend_mask;
  GdkModifierType  modify_mask;

  extend_mask =
    gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
                                  GDK_MODIFIER_INTENT_EXTEND_SELECTION);
  modify_mask =
    gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
                                  GDK_MODIFIER_INTENT_MODIFY_SELECTION);

  pika_action_group_add_actions (group, "layers-action",
                                 layers_actions,
                                 G_N_ELEMENTS (layers_actions));

  pika_action_group_add_toggle_actions (group, "layers-action",
                                        layers_toggle_actions,
                                        G_N_ELEMENTS (layers_toggle_actions));

  pika_action_group_add_radio_actions (group, "layers-action",
                                       layers_blend_space_actions,
                                       G_N_ELEMENTS (layers_blend_space_actions),
                                       NULL, 0,
                                       layers_blend_space_cmd_callback);

  pika_action_group_add_radio_actions (group, "layers-action",
                                       layers_composite_space_actions,
                                       G_N_ELEMENTS (layers_composite_space_actions),
                                       NULL, 0,
                                       layers_composite_space_cmd_callback);

  pika_action_group_add_radio_actions (group, "layers-action",
                                       layers_composite_mode_actions,
                                       G_N_ELEMENTS (layers_composite_mode_actions),
                                       NULL, 0,
                                       layers_composite_mode_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_color_tag_actions,
                                      G_N_ELEMENTS (layers_color_tag_actions),
                                      layers_color_tag_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_mask_apply_actions,
                                      G_N_ELEMENTS (layers_mask_apply_actions),
                                      layers_mask_apply_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_mask_to_selection_actions,
                                      G_N_ELEMENTS (layers_mask_to_selection_actions),
                                      layers_mask_to_selection_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_alpha_to_selection_actions,
                                      G_N_ELEMENTS (layers_alpha_to_selection_actions),
                                      layers_alpha_to_selection_cmd_callback);

  layers_actions_fix_tooltip (group, "layers-alpha-selection-replace",
                              GDK_MOD1_MASK);
  layers_actions_fix_tooltip (group, "layers-alpha-selection-add",
                              extend_mask | GDK_MOD1_MASK);
  layers_actions_fix_tooltip (group, "layers-alpha-selection-subtract",
                              modify_mask | GDK_MOD1_MASK);
  layers_actions_fix_tooltip (group, "layers-alpha-selection-intersect",
                              extend_mask | modify_mask | GDK_MOD1_MASK);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_select_actions,
                                      G_N_ELEMENTS (layers_select_actions),
                                      layers_select_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_opacity_actions,
                                      G_N_ELEMENTS (layers_opacity_actions),
                                      layers_opacity_cmd_callback);

  pika_action_group_add_enum_actions (group, "layers-action",
                                      layers_mode_actions,
                                      G_N_ELEMENTS (layers_mode_actions),
                                      layers_mode_cmd_callback);

  items_actions_setup (group, "layers");
}

void
layers_actions_update (PikaActionGroup *group,
                       gpointer         data)
{
  PikaImage     *image          = action_data_get_image (data);
  GList         *layers         = NULL;
  GList         *iter           = NULL;
  PikaLayer     *layer          = NULL;
  gboolean       fs             = FALSE;    /*  floating sel           */
  gboolean       ac             = FALSE;    /*  Has selected channels  */
  gboolean       sel            = FALSE;
  gboolean       indexed        = FALSE;    /*  is indexed             */
  gboolean       lock_alpha     = TRUE;
  gboolean       can_lock_alpha = FALSE;
  gboolean       text_layer     = FALSE;
  gboolean       bs_mutable     = FALSE; /* At least 1 selected layers' blend space is mutable.     */
  gboolean       cs_mutable     = FALSE; /* At least 1 selected layers' composite space is mutable. */
  gboolean       cm_mutable     = FALSE; /* At least 1 selected layers' composite mode is mutable.  */
  gboolean       next_mode      = TRUE;
  gboolean       prev_mode      = TRUE;
  gboolean       last_mode      = FALSE;
  gboolean       first_mode     = FALSE;

  gboolean       have_masks     = FALSE; /* At least 1 selected layer has a mask.             */
  gboolean       have_no_masks  = FALSE; /* At least 1 selected layer has no mask.            */
  gboolean       have_groups    = FALSE; /* At least 1 selected layer is a group.             */
  gboolean       have_no_groups = FALSE; /* At least 1 selected layer is not a group.         */
  gboolean       have_writable  = FALSE; /* At least 1 selected layer has no contents lock.   */
  gboolean       have_prev      = FALSE; /* At least 1 selected layer has a previous sibling. */
  gboolean       have_next      = FALSE; /* At least 1 selected layer has a next sibling.     */
  gboolean       have_alpha     = FALSE; /* At least 1 selected layer has an alpha channel.   */
  gboolean       have_no_alpha  = FALSE; /* At least 1 selected layer has no alpha channel.   */

  gboolean       all_visible        = TRUE;
  gboolean       all_next_visible   = TRUE;
  gboolean       all_masks_shown    = TRUE;
  gboolean       all_masks_disabled = TRUE;
  gboolean       all_writable       = TRUE;
  gboolean       all_movable        = TRUE;

  gint           n_selected_layers  = 0;
  gint           n_layers           = 0;
  gint           n_text_layers      = 0;

  if (image)
    {
      fs      = (pika_image_get_floating_selection (image) != NULL);
      ac      = (pika_image_get_selected_channels (image) != NULL);
      sel     = ! pika_channel_is_empty (pika_image_get_mask (image));
      indexed = (pika_image_get_base_type (image) == PIKA_INDEXED);

      layers            = pika_image_get_selected_layers (image);
      n_selected_layers = g_list_length (layers);
      n_layers          = pika_image_get_n_layers (image);

      for (iter = layers; iter; iter = iter->next)
        {
          PikaLayerMode *modes;
          PikaLayerMode  mode;
          GList         *layer_list;
          GList         *iter2;
          gint           n_modes;
          gint           i = 0;

          /* have_masks and have_no_masks are not opposite. 3 cases are
           * possible: all layers have masks, none have masks, or some
           * have masks, and some none.
           */
          if (pika_layer_get_mask (iter->data))
            {
              have_masks = TRUE;
              if (! pika_layer_get_show_mask (iter->data))
                all_masks_shown = FALSE;
              if (pika_layer_get_apply_mask (iter->data))
                all_masks_disabled = FALSE;
            }
          else
            {
              have_no_masks = TRUE;
            }

          if (pika_viewable_get_children (PIKA_VIEWABLE (iter->data)))
            have_groups = TRUE;
          else
            have_no_groups = TRUE;

          if (! pika_item_is_content_locked (PIKA_ITEM (iter->data), NULL))
            have_writable = TRUE;
          else
            all_writable  = FALSE;

          if (pika_item_is_position_locked (PIKA_ITEM (iter->data), NULL))
            all_movable = FALSE;

          if (pika_layer_can_lock_alpha (iter->data))
            {
              if (! pika_layer_get_lock_alpha (iter->data))
                lock_alpha = FALSE;
              can_lock_alpha = TRUE;
            }

          mode = pika_layer_get_mode (iter->data);
          modes = pika_layer_mode_get_context_array (mode,
                                                     PIKA_LAYER_MODE_CONTEXT_LAYER,
                                                     &n_modes);
          while (i < (n_modes - 1) && modes[i] != mode)
            i++;
          g_free (modes);
          if (i >= n_modes - 1)
            next_mode = FALSE;
          else
            last_mode = TRUE;
          if (i <= 0)
            prev_mode = FALSE;
          else
            first_mode = TRUE;

          layer_list = pika_item_get_container_iter (PIKA_ITEM (iter->data));
          iter2 = g_list_find (layer_list, iter->data);

          if (iter2)
            {
              GList *next_visible;

              if (g_list_previous (iter2))
                have_prev = TRUE;

              if (g_list_next (iter2))
                have_next = TRUE;

              for (next_visible = g_list_next (iter2);
                   next_visible;
                   next_visible = g_list_next (next_visible))
                {
                  if (pika_item_get_visible (next_visible->data))
                    {
                      /*  "next_visible" is actually "next_visible" and
                       *  "writable" and "not group"
                       */
                      if (pika_item_is_content_locked (next_visible->data, NULL) ||
                          pika_viewable_get_children (next_visible->data))
                        next_visible = NULL;

                      break;
                    }
                }

              if (! next_visible)
                all_next_visible = FALSE;
            }

          if (pika_layer_mode_is_blend_space_mutable (mode))
            bs_mutable = TRUE;
          if (pika_layer_mode_is_composite_space_mutable (mode))
            cs_mutable = TRUE;
          if (pika_layer_mode_is_composite_mode_mutable (mode))
            cm_mutable = TRUE;

          if (! pika_item_get_visible (iter->data))
            all_visible = FALSE;

          if (pika_drawable_has_alpha (iter->data))
            have_alpha    = TRUE;
          else
            have_no_alpha = TRUE;

          if (PIKA_IS_TEXT_LAYER (iter->data))
            n_text_layers++;
        }

      if (n_selected_layers == 1)
        {
          /* Special unique layer case. */
          const gchar *action = NULL;

          layer  = layers->data;
          switch (pika_layer_get_blend_space (layer))
            {
            case PIKA_LAYER_COLOR_SPACE_AUTO:
              action = "layers-blend-space-auto"; break;
            case PIKA_LAYER_COLOR_SPACE_RGB_LINEAR:
              action = "layers-blend-space-rgb-linear"; break;
            case PIKA_LAYER_COLOR_SPACE_RGB_PERCEPTUAL:
              action = "layers-blend-space-rgb-perceptual"; break;
            default:
              action = NULL; break; /* can't happen */
            }

          if (action)
            pika_action_group_set_action_active (group, action, TRUE);

          switch (pika_layer_get_composite_space (layer))
            {
            case PIKA_LAYER_COLOR_SPACE_AUTO:
              action = "layers-composite-space-auto"; break;
            case PIKA_LAYER_COLOR_SPACE_RGB_LINEAR:
              action = "layers-composite-space-rgb-linear"; break;
            case PIKA_LAYER_COLOR_SPACE_RGB_PERCEPTUAL:
              action = "layers-composite-space-rgb-perceptual"; break;
            default:
              action = NULL; break; /* can't happen */
            }

          if (action)
            pika_action_group_set_action_active (group, action, TRUE);

          switch (pika_layer_get_composite_mode (layer))
            {
            case PIKA_LAYER_COMPOSITE_AUTO:
              action = "layers-composite-mode-auto"; break;
            case PIKA_LAYER_COMPOSITE_UNION:
              action = "layers-composite-mode-union"; break;
            case PIKA_LAYER_COMPOSITE_CLIP_TO_BACKDROP:
              action = "layers-composite-mode-clip-to-backdrop"; break;
            case PIKA_LAYER_COMPOSITE_CLIP_TO_LAYER:
              action = "layers-composite-mode-clip-to-layer"; break;
            case PIKA_LAYER_COMPOSITE_INTERSECTION:
              action = "layers-composite-mode-intersection"; break;
            }

          pika_action_group_set_action_active (group, action, TRUE);

          text_layer = pika_item_is_text_layer (PIKA_ITEM (layer));
        }
    }

#define SET_VISIBLE(action,condition) \
        pika_action_group_set_action_visible (group, action, (condition) != 0)
#define SET_SENSITIVE(action,condition) \
        pika_action_group_set_action_sensitive (group, action, (condition) != 0, NULL)
#define SET_ACTIVE(action,condition) \
        pika_action_group_set_action_active (group, action, (condition) != 0)
#define SET_LABEL(action,label) \
        pika_action_group_set_action_label (group, action, label)

  SET_SENSITIVE ("layers-edit",             !ac && ((layer && !fs) || text_layer));
  SET_VISIBLE   ("layers-edit-text",        text_layer && !ac);
  SET_SENSITIVE ("layers-edit-text",        text_layer && !ac);
  SET_SENSITIVE ("layers-edit-attributes",  layer && !fs && !ac);

  if (layer && pika_layer_is_floating_sel (layer))
    {
      SET_LABEL ("layers-new",             C_("layers-action", "To _New Layer"));
      SET_LABEL ("layers-new-last-values", C_("layers-action", "To _New Layer"));
    }
  else
    {
      SET_LABEL ("layers-new",             C_("layers-action", "_New Layer..."));
      SET_LABEL ("layers-new-last-values", C_("layers-action", "_New Layer"));
    }

  SET_SENSITIVE ("layers-new",              image);
  SET_SENSITIVE ("layers-new-last-values",  image);
  SET_SENSITIVE ("layers-new-from-visible", image);
  SET_SENSITIVE ("layers-new-group",        image && !indexed);
  SET_SENSITIVE ("layers-duplicate",        n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-delete",           n_selected_layers > 0 && !ac);

  SET_SENSITIVE ("layers-mode-first",       n_selected_layers > 0 && !ac && first_mode);
  SET_SENSITIVE ("layers-mode-last",        n_selected_layers > 0 && !ac && last_mode);
  SET_SENSITIVE ("layers-mode-previous",    n_selected_layers > 0 && !ac && prev_mode);
  SET_SENSITIVE ("layers-mode-next",        n_selected_layers > 0 && !ac && next_mode);

  SET_SENSITIVE ("layers-select-top",       n_layers > 0 && !fs && (n_selected_layers == 0 || have_prev));
  SET_SENSITIVE ("layers-select-bottom",    n_layers > 0 && !fs && (n_selected_layers == 0 || have_next));
  SET_SENSITIVE ("layers-select-previous",  n_selected_layers > 0 && !fs && !ac && have_prev);
  SET_SENSITIVE ("layers-select-next",      n_selected_layers > 0 && !fs && !ac && have_next);

  SET_SENSITIVE ("layers-raise",            n_selected_layers > 0 && !fs && !ac && have_prev);
  SET_SENSITIVE ("layers-raise-to-top",     n_selected_layers > 0 && !fs && !ac && have_prev);
  SET_SENSITIVE ("layers-lower",            n_selected_layers > 0 && !fs && !ac && have_next);
  SET_SENSITIVE ("layers-lower-to-bottom",  n_selected_layers > 0 && !fs && !ac && have_next);

  SET_VISIBLE   ("layers-anchor",            fs && !ac);
  SET_VISIBLE   ("layers-merge-down",        !fs);
  SET_SENSITIVE ("layers-merge-down",        n_selected_layers > 0 && !fs && !ac && all_visible && all_next_visible);
  SET_VISIBLE   ("layers-merge-down-button", !fs);
  SET_SENSITIVE ("layers-merge-down-button", n_selected_layers > 0 && !fs && !ac);
  SET_VISIBLE   ("layers-merge-group",       have_groups);
  SET_SENSITIVE ("layers-merge-group",       n_selected_layers && !fs && !ac && have_groups);
  SET_SENSITIVE ("layers-merge-layers",      n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-flatten-image",     !fs && !ac);

  SET_VISIBLE   ("layers-text-discard",       n_text_layers > 0 && !ac);
  SET_VISIBLE   ("layers-text-to-vectors",    n_text_layers > 0 && !ac);
  SET_VISIBLE   ("layers-text-along-vectors", text_layer && !ac);

  SET_SENSITIVE ("layers-resize",          n_selected_layers == 1 && all_writable && all_movable && !ac);
  SET_SENSITIVE ("layers-resize-to-image", all_writable && all_movable && !ac);
  SET_SENSITIVE ("layers-scale",           n_selected_layers == 1 && all_writable && all_movable && !ac);

  SET_SENSITIVE ("layers-crop-to-selection", all_writable && all_movable && sel);
  SET_SENSITIVE ("layers-crop-to-content",   all_writable && all_movable);

  SET_SENSITIVE ("layers-alpha-add",    all_writable && have_no_groups && !fs && have_no_alpha);
  SET_SENSITIVE ("layers-alpha-remove", all_writable && have_no_groups && !fs && have_alpha);

  SET_SENSITIVE ("layers-lock-alpha", can_lock_alpha);
  SET_ACTIVE    ("layers-lock-alpha", lock_alpha);

  SET_SENSITIVE ("layers-blend-space-auto",           n_selected_layers && bs_mutable);
  SET_SENSITIVE ("layers-blend-space-rgb-linear",     n_selected_layers && bs_mutable);
  SET_SENSITIVE ("layers-blend-space-rgb-perceptual", n_selected_layers && bs_mutable);

  SET_SENSITIVE ("layers-composite-space-auto",           n_selected_layers && cs_mutable);
  SET_SENSITIVE ("layers-composite-space-rgb-linear",     n_selected_layers && cs_mutable);
  SET_SENSITIVE ("layers-composite-space-rgb-perceptual", n_selected_layers && cs_mutable);

  SET_SENSITIVE ("layers-composite-mode-auto",             n_selected_layers && cm_mutable);
  SET_SENSITIVE ("layers-composite-mode-union",            n_selected_layers && cm_mutable);
  SET_SENSITIVE ("layers-composite-mode-clip-to-backdrop", n_selected_layers && cm_mutable);
  SET_SENSITIVE ("layers-composite-mode-clip-to-layer",    n_selected_layers && cm_mutable);
  SET_SENSITIVE ("layers-composite-mode-intersection",     n_selected_layers && cm_mutable);

  SET_SENSITIVE ("layers-mask-add",             n_selected_layers > 0 && !fs && !ac && have_no_masks);
  SET_SENSITIVE ("layers-mask-add-button",      n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-mask-add-last-values", n_selected_layers > 0 && !fs && !ac && have_no_masks);

  SET_SENSITIVE ("layers-mask-apply",  have_writable && !fs && !ac && have_masks && have_no_groups);
  SET_SENSITIVE ("layers-mask-delete", n_selected_layers > 0 && !fs && !ac && have_masks);

  SET_SENSITIVE ("layers-mask-edit",    n_selected_layers == 1 && !fs && !ac && have_masks);
  SET_SENSITIVE ("layers-mask-show",    n_selected_layers > 0 && !fs && !ac && have_masks);
  SET_SENSITIVE ("layers-mask-disable", n_selected_layers > 0 && !fs && !ac && have_masks);

  SET_ACTIVE ("layers-mask-edit",    n_selected_layers == 1 && have_masks && pika_layer_get_edit_mask (layers->data));
  SET_ACTIVE ("layers-mask-show",    all_masks_shown);
  SET_ACTIVE ("layers-mask-disable", all_masks_disabled);

  SET_SENSITIVE ("layers-mask-selection-replace",   n_selected_layers && !fs && !ac && have_masks);
  SET_SENSITIVE ("layers-mask-selection-add",       n_selected_layers && !fs && !ac && have_masks);
  SET_SENSITIVE ("layers-mask-selection-subtract",  n_selected_layers && !fs && !ac && have_masks);
  SET_SENSITIVE ("layers-mask-selection-intersect", n_selected_layers && !fs && !ac && have_masks);

  SET_SENSITIVE ("layers-alpha-selection-replace",   n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-add",       n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-subtract",  n_selected_layers > 0 && !fs && !ac);
  SET_SENSITIVE ("layers-alpha-selection-intersect", n_selected_layers > 0 && !fs && !ac);

#undef SET_VISIBLE
#undef SET_SENSITIVE
#undef SET_ACTIVE
#undef SET_LABEL

  items_actions_update (group, "layers", layers);
}
