1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpwidgets/gimpwidgets.h"
24 
25 #include "tools-types.h"
26 
27 #include "core/gimpdrawable.h"
28 
29 #include "paint/gimperaseroptions.h"
30 
31 #include "widgets/gimphelp-ids.h"
32 #include "widgets/gimpwidgets-utils.h"
33 
34 #include "gimperasertool.h"
35 #include "gimppaintoptions-gui.h"
36 #include "gimptoolcontrol.h"
37 
38 #include "gimp-intl.h"
39 
40 
41 static void        gimp_eraser_tool_modifier_key  (GimpTool         *tool,
42                                                    GdkModifierType   key,
43                                                    gboolean          press,
44                                                    GdkModifierType   state,
45                                                    GimpDisplay      *display);
46 static void        gimp_eraser_tool_cursor_update (GimpTool         *tool,
47                                                    const GimpCoords *coords,
48                                                    GdkModifierType   state,
49                                                    GimpDisplay      *display);
50 
51 static gboolean    gimp_eraser_tool_is_alpha_only (GimpPaintTool    *paint_tool,
52                                                    GimpDrawable     *drawable);
53 
54 static GtkWidget * gimp_eraser_options_gui        (GimpToolOptions  *tool_options);
55 
56 
G_DEFINE_TYPE(GimpEraserTool,gimp_eraser_tool,GIMP_TYPE_BRUSH_TOOL)57 G_DEFINE_TYPE (GimpEraserTool, gimp_eraser_tool, GIMP_TYPE_BRUSH_TOOL)
58 
59 #define parent_class gimp_eraser_tool_parent_class
60 
61 
62 void
63 gimp_eraser_tool_register (GimpToolRegisterCallback  callback,
64                            gpointer                  data)
65 {
66   (* callback) (GIMP_TYPE_ERASER_TOOL,
67                 GIMP_TYPE_ERASER_OPTIONS,
68                 gimp_eraser_options_gui,
69                 GIMP_PAINT_OPTIONS_CONTEXT_MASK,
70                 "gimp-eraser-tool",
71                 _("Eraser"),
72                 _("Eraser Tool: Erase to background or transparency using a brush"),
73                 N_("_Eraser"), "<shift>E",
74                 NULL, GIMP_HELP_TOOL_ERASER,
75                 GIMP_ICON_TOOL_ERASER,
76                 data);
77 }
78 
79 static void
gimp_eraser_tool_class_init(GimpEraserToolClass * klass)80 gimp_eraser_tool_class_init (GimpEraserToolClass *klass)
81 {
82   GimpToolClass      *tool_class       = GIMP_TOOL_CLASS (klass);
83   GimpPaintToolClass *paint_tool_class = GIMP_PAINT_TOOL_CLASS (klass);
84 
85   tool_class->modifier_key        = gimp_eraser_tool_modifier_key;
86   tool_class->cursor_update       = gimp_eraser_tool_cursor_update;
87 
88   paint_tool_class->is_alpha_only = gimp_eraser_tool_is_alpha_only;
89 }
90 
91 static void
gimp_eraser_tool_init(GimpEraserTool * eraser)92 gimp_eraser_tool_init (GimpEraserTool *eraser)
93 {
94   GimpTool      *tool       = GIMP_TOOL (eraser);
95   GimpPaintTool *paint_tool = GIMP_PAINT_TOOL (eraser);
96 
97   gimp_tool_control_set_tool_cursor            (tool->control,
98                                                 GIMP_TOOL_CURSOR_ERASER);
99   gimp_tool_control_set_toggle_cursor_modifier (tool->control,
100                                                 GIMP_CURSOR_MODIFIER_MINUS);
101 
102   gimp_paint_tool_enable_color_picker (paint_tool,
103                                        GIMP_COLOR_PICK_TARGET_BACKGROUND);
104 
105   paint_tool->status      = _("Click to erase");
106   paint_tool->status_line = _("Click to erase the line");
107   paint_tool->status_ctrl = _("%s to pick a background color");
108 }
109 
110 static void
gimp_eraser_tool_modifier_key(GimpTool * tool,GdkModifierType key,gboolean press,GdkModifierType state,GimpDisplay * display)111 gimp_eraser_tool_modifier_key (GimpTool        *tool,
112                                GdkModifierType  key,
113                                gboolean         press,
114                                GdkModifierType  state,
115                                GimpDisplay     *display)
116 {
117   if (key == GDK_MOD1_MASK)
118     {
119       GimpEraserOptions *options = GIMP_ERASER_TOOL_GET_OPTIONS (tool);
120 
121       g_object_set (options,
122                     "anti-erase", ! options->anti_erase,
123                     NULL);
124     }
125 
126   GIMP_TOOL_CLASS (parent_class)->modifier_key (tool, key, press, state, display);
127 }
128 
129 static void
gimp_eraser_tool_cursor_update(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,GimpDisplay * display)130 gimp_eraser_tool_cursor_update (GimpTool         *tool,
131                                 const GimpCoords *coords,
132                                 GdkModifierType   state,
133                                 GimpDisplay      *display)
134 {
135   GimpEraserOptions *options = GIMP_ERASER_TOOL_GET_OPTIONS (tool);
136 
137   gimp_tool_control_set_toggled (tool->control, options->anti_erase);
138 
139   GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
140 }
141 
142 static gboolean
gimp_eraser_tool_is_alpha_only(GimpPaintTool * paint_tool,GimpDrawable * drawable)143 gimp_eraser_tool_is_alpha_only (GimpPaintTool *paint_tool,
144                                 GimpDrawable  *drawable)
145 {
146   GimpEraserOptions *options = GIMP_ERASER_TOOL_GET_OPTIONS (paint_tool);
147 
148   if (! options->anti_erase)
149     return gimp_drawable_has_alpha (drawable);
150   else
151     return TRUE;
152 }
153 
154 
155 /*  tool options stuff  */
156 
157 static GtkWidget *
gimp_eraser_options_gui(GimpToolOptions * tool_options)158 gimp_eraser_options_gui (GimpToolOptions *tool_options)
159 {
160   GObject   *config = G_OBJECT (tool_options);
161   GtkWidget *vbox   = gimp_paint_options_gui (tool_options);
162   GtkWidget *button;
163   gchar     *str;
164 
165   /* the anti_erase toggle */
166   str = g_strdup_printf (_("Anti erase  (%s)"),
167                          gimp_get_mod_string (GDK_MOD1_MASK));
168 
169   button = gimp_prop_check_button_new (config, "anti-erase", str);
170   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
171   gtk_widget_show (button);
172 
173   g_free (str);
174 
175   return vbox;
176 }
177