From 94a276973da9dacc651f8b173ee103f948fe5eae Mon Sep 17 00:00:00 2001 From: Alx Sa Date: Fri, 4 Oct 2024 20:18:45 +0000 Subject: [PATCH] app/tools: Add shortcuts for Paint Select modes This patch ports the Add/Subtract mode shortcuts from GimpSelectionTool to GimpPaintSelectionTool, implementing the unfinished gimp_paint_select_tool_modifier_key () function. The UI itself is left unchanged. --- app/tools/gimppaintselectoptions.c | 42 +++++++++++++++++-- app/tools/gimppaintselecttool.c | 65 +++++++++++++++++++++++++++--- app/tools/gimppaintselecttool.h | 2 + 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/app/tools/gimppaintselectoptions.c b/app/tools/gimppaintselectoptions.c index a56287dd88..515e14080b 100644 --- a/app/tools/gimppaintselectoptions.c +++ b/app/tools/gimppaintselectoptions.c @@ -168,7 +168,11 @@ gimp_paint_select_options_gui (GimpToolOptions *tool_options) GtkWidget *hbox; GtkWidget *button; GtkWidget *frame; + GList *children; + GList *radio_children; + GList *list; GtkWidget *scale; + gint i; frame = gimp_prop_enum_radio_frame_new (config, "mode", NULL, 0, 0); @@ -176,7 +180,39 @@ gimp_paint_select_options_gui (GimpToolOptions *tool_options) hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); - gtk_widget_show (hbox); + gtk_widget_set_visible (hbox, TRUE); + + /* add modifier to tooltips */ + children = gtk_container_get_children (GTK_CONTAINER (frame)); + radio_children = gtk_container_get_children (GTK_CONTAINER (children->data)); + for (list = radio_children, i = 0; list; list = list->next, i++) + { + GtkWidget *button = list->data; + const gchar *modifier = NULL; + const gchar *label = NULL; + + if (i == 0) + modifier = gimp_get_mod_string (gimp_get_extend_selection_mask ()); + else if (i == 1) + modifier = gimp_get_mod_string (gimp_get_modify_selection_mask ()); + + if (! modifier) + continue; + + label = gtk_button_get_label (GTK_BUTTON (button)); + + if (label) + { + gchar *tip = g_strdup_printf ("%s %s", label, modifier); + + gimp_help_set_help_data_with_markup (button, tip, NULL); + + g_free (tip); + } + } + g_list_free (children); + g_list_free (radio_children); + g_list_free (list); /* stroke width */ scale = gimp_prop_spin_scale_new (config, "stroke-width", @@ -190,7 +226,7 @@ gimp_paint_select_options_gui (GimpToolOptions *tool_options) gtk_image_set_from_icon_name (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (button))), GIMP_ICON_RESET, GTK_ICON_SIZE_MENU); gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); - gtk_widget_show (button); + gtk_widget_set_visible (button, TRUE); g_signal_connect (button, "clicked", G_CALLBACK (gimp_paint_select_options_reset_stroke_width), @@ -202,7 +238,7 @@ gimp_paint_select_options_gui (GimpToolOptions *tool_options) /* show scribbles */ button = gimp_prop_check_button_new (config, "show-scribbles", "Show scribbles"); gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); - gtk_widget_show (button); + gtk_widget_set_visible (button, TRUE); return vbox; } diff --git a/app/tools/gimppaintselecttool.c b/app/tools/gimppaintselecttool.c index bbc430b82f..37d3482207 100644 --- a/app/tools/gimppaintselecttool.c +++ b/app/tools/gimppaintselecttool.c @@ -420,13 +420,68 @@ gimp_paint_select_tool_key_press (GimpTool *tool, return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display); } +/* Based on gimp_selection_tool_modifier_key () */ static void gimp_paint_select_tool_modifier_key (GimpTool *tool, - GdkModifierType key, - gboolean press, - GdkModifierType state, - GimpDisplay *display) + GdkModifierType key, + gboolean press, + GdkModifierType state, + GimpDisplay *display) { + GimpPaintSelectTool *ps_tool = GIMP_PAINT_SELECT_TOOL (tool); + GimpPaintSelectOptions *options = GIMP_PAINT_SELECT_TOOL_GET_OPTIONS (ps_tool); + GdkModifierType extend_mask; + GdkModifierType modify_mask; + + extend_mask = gimp_get_extend_selection_mask (); + modify_mask = gimp_get_modify_selection_mask (); + + if (key == extend_mask || + key == modify_mask || + key == GDK_MOD1_MASK) + { + GimpPaintSelectMode button_mode = options->mode; + + state &= extend_mask | modify_mask | GDK_MOD1_MASK; + + if (press) + { + if (key == state || ! state) + ps_tool->saved_mode = options->mode; + } + else + { + if (! state) + button_mode = ps_tool->saved_mode; + } + + if (state & GDK_MOD1_MASK) + { + button_mode = ps_tool->saved_mode; + } + else if (state & (extend_mask | modify_mask)) + { + GimpChannelOps op = gimp_modifiers_to_channel_op (state); + + switch (op) + { + case GIMP_CHANNEL_OP_ADD: + button_mode = GIMP_PAINT_SELECT_MODE_ADD; + break; + + case GIMP_CHANNEL_OP_SUBTRACT: + button_mode = GIMP_PAINT_SELECT_MODE_SUBTRACT; + break; + + default: + break; + } + } + + if (button_mode != options->mode) + g_object_set (options, "mode", button_mode, NULL); + } + } static void @@ -809,7 +864,6 @@ gimp_paint_select_tool_create_graph (GimpPaintSelectTool *ps_tool) GeglNode *d; /* drawable */ GeglNode *crop; GeglNode *translate = NULL; - ps_tool->graph = gegl_node_new (); @@ -858,7 +912,6 @@ gimp_paint_select_tool_create_graph (GimpPaintSelectTool *ps_tool) "operation", "gegl:buffer-sink", NULL); - gegl_node_link_many (m, ps_tool->threshold_node, crop, NULL); if (translate) diff --git a/app/tools/gimppaintselecttool.h b/app/tools/gimppaintselecttool.h index 6281e30231..6d62e63f58 100644 --- a/app/tools/gimppaintselecttool.h +++ b/app/tools/gimppaintselecttool.h @@ -39,6 +39,8 @@ struct _GimpPaintSelectTool { GimpDrawTool parent_instance; + GimpPaintSelectMode saved_mode; /* saved tool options state */ + GeglBuffer *trimap; GeglBuffer *image_mask; GeglBuffer *drawable;