From c8de818349f4de18a94964410daec125c205b137 Mon Sep 17 00:00:00 2001 From: Jehan Date: Mon, 9 Oct 2023 01:07:01 +0200 Subject: [PATCH] app, menus: Colormap dockable now shows a delete button. This comes with a "colormap-delete-color" into the "colormap" action group. The action/button will be insensitive when the selected color is used in the image, since it is only possible to delete unused colors. --- app/actions/colormap-actions.c | 25 +++++--- app/actions/colormap-commands.c | 10 +++ app/actions/colormap-commands.h | 3 + app/widgets/gimpcolormapeditor.c | 96 +++++++++++++++++++++++------ app/widgets/gimpcolormapeditor.h | 20 +++--- app/widgets/gimpcolormapselection.c | 11 +++- app/widgets/gimpeditor.c | 25 ++++++++ app/widgets/gimpeditor.h | 5 ++ menus/colormap-menu.ui | 1 + 9 files changed, 161 insertions(+), 35 deletions(-) diff --git a/app/actions/colormap-actions.c b/app/actions/colormap-actions.c index 54c752e306..a73c57a623 100644 --- a/app/actions/colormap-actions.c +++ b/app/actions/colormap-actions.c @@ -30,6 +30,7 @@ #include "core/gimpimage-colormap.h" #include "widgets/gimpactiongroup.h" +#include "widgets/gimpcolormapeditor.h" #include "widgets/gimphelp-ids.h" #include "actions.h" @@ -45,6 +46,12 @@ static const GimpActionEntry colormap_actions[] = NC_("colormap-action", "_Edit Color..."), NULL, { NULL }, NC_("colormap-action", "Edit this color"), colormap_edit_color_cmd_callback, + GIMP_HELP_INDEXED_PALETTE_EDIT }, + + { "colormap-delete-color", GIMP_ICON_EDIT_DELETE, + NC_("colormap-action", "_Delete Color..."), NULL, { NULL }, + NC_("colormap-action", "Delete this color"), + colormap_delete_color_cmd_callback, GIMP_HELP_INDEXED_PALETTE_EDIT } }; @@ -112,13 +119,14 @@ void colormap_actions_update (GimpActionGroup *group, gpointer data) { - GimpImage *image = action_data_get_image (data); - GimpContext *context = action_data_get_context (data); - gboolean indexed = FALSE; - gboolean drawable_indexed = FALSE; - gint num_colors = 0; - GimpRGB fg; - GimpRGB bg; + GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (data); + GimpImage *image = action_data_get_image (data); + GimpContext *context = action_data_get_context (data); + gboolean indexed = FALSE; + gboolean drawable_indexed = FALSE; + gint num_colors = 0; + GimpRGB fg; + GimpRGB bg; if (image) { @@ -150,6 +158,9 @@ colormap_actions_update (GimpActionGroup *group, SET_SENSITIVE ("colormap-edit-color", indexed && num_colors > 0); + SET_SENSITIVE ("colormap-delete-color", + indexed && num_colors > 0 && + gimp_colormap_editor_is_color_deletable (editor)); SET_SENSITIVE ("colormap-add-color-from-fg", indexed && num_colors < 256); diff --git a/app/actions/colormap-commands.c b/app/actions/colormap-commands.c index 43bbc0da8a..bf62aacdd6 100644 --- a/app/actions/colormap-commands.c +++ b/app/actions/colormap-commands.c @@ -46,6 +46,16 @@ colormap_edit_color_cmd_callback (GimpAction *action, gimp_colormap_editor_edit_color (editor); } +void +colormap_delete_color_cmd_callback (GimpAction *action, + GVariant *value, + gpointer data) +{ + GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (data); + + gimp_colormap_editor_delete_color (editor); +} + void colormap_add_color_cmd_callback (GimpAction *action, GVariant *value, diff --git a/app/actions/colormap-commands.h b/app/actions/colormap-commands.h index 8aa4e525c8..fd29274da5 100644 --- a/app/actions/colormap-commands.h +++ b/app/actions/colormap-commands.h @@ -22,6 +22,9 @@ void colormap_edit_color_cmd_callback (GimpAction *action, GVariant *value, gpointer data); +void colormap_delete_color_cmd_callback (GimpAction *action, + GVariant *value, + gpointer data); void colormap_add_color_cmd_callback (GimpAction *action, GVariant *value, gpointer data); diff --git a/app/widgets/gimpcolormapeditor.c b/app/widgets/gimpcolormapeditor.c index a6070aea16..3ea4d71868 100644 --- a/app/widgets/gimpcolormapeditor.c +++ b/app/widgets/gimpcolormapeditor.c @@ -43,29 +43,33 @@ #include "gimp-intl.h" -static void gimp_colormap_editor_docked_iface_init (GimpDockedInterface *face); +static void gimp_colormap_editor_docked_iface_init (GimpDockedInterface *face); -static void gimp_colormap_editor_constructed (GObject *object); -static void gimp_colormap_editor_dispose (GObject *object); +static void gimp_colormap_editor_constructed (GObject *object); +static void gimp_colormap_editor_dispose (GObject *object); -static void gimp_colormap_editor_unmap (GtkWidget *widget); +static void gimp_colormap_editor_unmap (GtkWidget *widget); -static void gimp_colormap_editor_set_context (GimpDocked *docked, - GimpContext *context); +static void gimp_colormap_editor_set_context (GimpDocked *docked, + GimpContext *context); -static void gimp_colormap_editor_color_update (GimpColorDialog *dialog, - const GimpRGB *color, - GimpColorDialogState state, - GimpColormapEditor *editor); +static void gimp_colormap_editor_color_update (GimpColorDialog *dialog, + const GimpRGB *color, + GimpColorDialogState state, + GimpColormapEditor *editor); + +static gboolean gimp_colormap_editor_entry_button_press (GtkWidget *widget, + GdkEvent *event, + gpointer user_data); +static gboolean gimp_colormap_editor_entry_popup (GtkWidget *widget, + gpointer user_data); +static void gimp_colormap_editor_color_clicked (GimpColormapEditor *editor, + GimpPaletteEntry *entry, + GdkModifierType state); +static void gimp_colormap_editor_notify_index (GimpColormapSelection *selection, + const GParamSpec *pspec, + GimpColormapEditor *editor); -static gboolean gimp_colormap_editor_entry_button_press (GtkWidget *widget, - GdkEvent *event, - gpointer user_data); -static gboolean gimp_colormap_editor_entry_popup (GtkWidget *widget, - gpointer user_data); -static void gimp_colormap_editor_color_clicked (GimpColormapEditor *editor, - GimpPaletteEntry *entry, - GdkModifierType state); G_DEFINE_TYPE_WITH_CODE (GimpColormapEditor, gimp_colormap_editor, GIMP_TYPE_IMAGE_EDITOR, @@ -123,6 +127,9 @@ gimp_colormap_editor_constructed (GObject *object) gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap", "colormap-edit-color", NULL); + gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap", + "colormap-delete-color", + NULL); gimp_editor_add_action_button (GIMP_EDITOR (editor), "colormap", "colormap-add-color-from-fg", @@ -193,6 +200,9 @@ gimp_colormap_editor_set_context (GimpDocked *docked, g_signal_connect (editor->selection, "popup-menu", G_CALLBACK (gimp_colormap_editor_entry_popup), editor); + g_signal_connect (editor->selection, "notify::index", + G_CALLBACK (gimp_colormap_editor_notify_index), + editor); } } @@ -277,6 +287,43 @@ gimp_colormap_editor_edit_color (GimpColormapEditor *editor) gtk_window_present (GTK_WINDOW (editor->color_dialog)); } +void +gimp_colormap_editor_delete_color (GimpColormapEditor *editor) +{ + GimpColormapSelection *selection; + GimpImage *image; + gint index; + + g_return_if_fail (GIMP_IS_COLORMAP_EDITOR (editor)); + g_return_if_fail (gimp_colormap_editor_is_color_deletable (editor)); + + image = GIMP_IMAGE_EDITOR (editor)->image; + selection = GIMP_COLORMAP_SELECTION (editor->selection); + index = gimp_colormap_selection_get_index (selection, NULL); + + gimp_image_delete_colormap_entry (image, index, TRUE); +} + +gboolean +gimp_colormap_editor_is_color_deletable (GimpColormapEditor *editor) +{ + GimpColormapSelection *selection; + GimpImage *image; + gint index; + + g_return_val_if_fail (GIMP_IS_COLORMAP_EDITOR (editor), FALSE); + + image = GIMP_IMAGE_EDITOR (editor)->image; + selection = GIMP_COLORMAP_SELECTION (editor->selection); + index = gimp_colormap_selection_get_index (selection, NULL); + + if (index == -1) + /* No colormap. */ + return FALSE; + else + return ! gimp_image_colormap_is_index_used (image, index); +} + gint gimp_colormap_editor_get_index (GimpColormapEditor *editor, const GimpRGB *search) @@ -407,3 +454,16 @@ gimp_colormap_editor_color_clicked (GimpColormapEditor *editor, else gimp_context_set_foreground (image_editor->context, &entry->color); } + +static void +gimp_colormap_editor_notify_index (GimpColormapSelection *selection, + const GParamSpec *pspec, + GimpColormapEditor *editor) +{ + g_return_if_fail (GIMP_IS_COLORMAP_EDITOR (editor)); + + gimp_editor_set_action_sensitive (GIMP_EDITOR (editor), "colormap", + "colormap-delete-color", + gimp_colormap_editor_is_color_deletable (editor), + _("The color is used in this indexed image")); +} diff --git a/app/widgets/gimpcolormapeditor.h b/app/widgets/gimpcolormapeditor.h index 040f2833e6..bacf5df2d5 100644 --- a/app/widgets/gimpcolormapeditor.h +++ b/app/widgets/gimpcolormapeditor.h @@ -46,19 +46,21 @@ struct _GimpColormapEditorClass }; -GType gimp_colormap_editor_get_type (void) G_GNUC_CONST; +GType gimp_colormap_editor_get_type (void) G_GNUC_CONST; -GtkWidget * gimp_colormap_editor_new (GimpMenuFactory *menu_factory); +GtkWidget * gimp_colormap_editor_new (GimpMenuFactory *menu_factory); -void gimp_colormap_editor_edit_color (GimpColormapEditor *editor); +void gimp_colormap_editor_edit_color (GimpColormapEditor *editor); +void gimp_colormap_editor_delete_color (GimpColormapEditor *editor); +gboolean gimp_colormap_editor_is_color_deletable (GimpColormapEditor *editor); -gint gimp_colormap_editor_get_index (GimpColormapEditor *editor, - const GimpRGB *search); -gboolean gimp_colormap_editor_set_index (GimpColormapEditor *editor, - gint index, - GimpRGB *color); +gint gimp_colormap_editor_get_index (GimpColormapEditor *editor, + const GimpRGB *search); +gboolean gimp_colormap_editor_set_index (GimpColormapEditor *editor, + gint index, + GimpRGB *color); -gint gimp_colormap_editor_max_index (GimpColormapEditor *editor); +gint gimp_colormap_editor_max_index (GimpColormapEditor *editor); #endif /* __GIMP_COLORMAP_EDITOR_H__ */ diff --git a/app/widgets/gimpcolormapselection.c b/app/widgets/gimpcolormapselection.c index 90bae4f68a..e7a653f369 100644 --- a/app/widgets/gimpcolormapselection.c +++ b/app/widgets/gimpcolormapselection.c @@ -53,7 +53,8 @@ enum { PROP_0, - PROP_CONTEXT + PROP_CONTEXT, + PROP_INDEX }; enum @@ -165,6 +166,10 @@ gimp_colormap_selection_class_init (GimpColormapSelectionClass* klass) GIMP_TYPE_CONTEXT, GIMP_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, PROP_INDEX, + g_param_spec_int ("index", NULL, NULL, + 0, G_MAXINT, 0, + GIMP_PARAM_READABLE)); } static void @@ -281,6 +286,9 @@ gimp_colormap_selection_get_property (GObject *object, case PROP_CONTEXT: g_value_set_object (value, selection->context); break; + case PROP_INDEX: + g_value_set_int (value, selection->col_index); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -429,6 +437,7 @@ gimp_colormap_selection_set_index (GimpColormapSelection *selection, selection->col_index = index; + g_object_notify (G_OBJECT (selection), "index"); gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (selection->view), gimp_palette_get_entry (palette, index)); diff --git a/app/widgets/gimpeditor.c b/app/widgets/gimpeditor.c index cf61ec15e6..c9fed5d112 100644 --- a/app/widgets/gimpeditor.c +++ b/app/widgets/gimpeditor.c @@ -774,6 +774,31 @@ gimp_editor_add_action_button (GimpEditor *editor, return button; } +void +gimp_editor_set_action_sensitive (GimpEditor *editor, + const gchar *group_name, + const gchar *action_name, + gboolean sensitive, + const gchar *reason) +{ + GimpActionGroup *group; + GimpAction *action; + + g_return_val_if_fail (GIMP_IS_EDITOR (editor), NULL); + g_return_val_if_fail (action_name != NULL, NULL); + g_return_val_if_fail (editor->priv->ui_manager != NULL, NULL); + + group = gimp_ui_manager_get_action_group (editor->priv->ui_manager, + group_name); + + g_return_val_if_fail (group != NULL, NULL); + + action = gimp_action_group_get_action (group, action_name); + g_return_val_if_fail (action != NULL, NULL); + + gimp_action_set_sensitive (action, sensitive, reason); +} + void gimp_editor_set_show_name (GimpEditor *editor, gboolean show) diff --git a/app/widgets/gimpeditor.h b/app/widgets/gimpeditor.h index b135a42d9e..03d91aee52 100644 --- a/app/widgets/gimpeditor.h +++ b/app/widgets/gimpeditor.h @@ -85,6 +85,11 @@ GtkWidget * gimp_editor_add_action_button (GimpEditor *editor, const gchar *group_name, const gchar *action_name, ...) G_GNUC_NULL_TERMINATED; +void gimp_editor_set_action_sensitive (GimpEditor *editor, + const gchar *group_name, + const gchar *action_name, + gboolean sensitive, + const gchar *reason); void gimp_editor_set_show_name (GimpEditor *editor, gboolean show); diff --git a/menus/colormap-menu.ui b/menus/colormap-menu.ui index 5376c1dc07..190c5611a9 100644 --- a/menus/colormap-menu.ui +++ b/menus/colormap-menu.ui @@ -3,6 +3,7 @@ colormap.colormap-edit-color + colormap.colormap-delete-color colormap.colormap-add-color-from-fg colormap.colormap-add-color-from-bg