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 "dialogs-types.h"
26 
27 #include "core/gimp.h"
28 #include "core/gimpchannel.h"
29 #include "core/gimpcontainer.h"
30 #include "core/gimpcontext.h"
31 #include "core/gimpimage.h"
32 #include "core/gimplayer.h"
33 
34 #include "widgets/gimpcontainercombobox.h"
35 #include "widgets/gimpcontainerview.h"
36 #include "widgets/gimphelp-ids.h"
37 #include "widgets/gimpviewabledialog.h"
38 #include "widgets/gimpwidgets-utils.h"
39 
40 #include "layer-add-mask-dialog.h"
41 
42 #include "gimp-intl.h"
43 
44 
45 typedef struct _LayerAddMaskDialog LayerAddMaskDialog;
46 
47 struct _LayerAddMaskDialog
48 {
49   GimpLayer           *layer;
50   GimpAddMaskType      add_mask_type;
51   GimpChannel         *channel;
52   gboolean             invert;
53   GimpAddMaskCallback  callback;
54   gpointer             user_data;
55 };
56 
57 
58 /*  local function prototypes  */
59 
60 static void       layer_add_mask_dialog_free             (LayerAddMaskDialog *private);
61 static void       layer_add_mask_dialog_response         (GtkWidget          *dialog,
62                                                           gint                response_id,
63                                                           LayerAddMaskDialog *private);
64 static gboolean   layer_add_mask_dialog_channel_selected (GimpContainerView  *view,
65                                                           GimpViewable       *viewable,
66                                                           gpointer            insert_data,
67                                                           LayerAddMaskDialog *dialog);
68 
69 
70 /*  public functions  */
71 
72 GtkWidget *
layer_add_mask_dialog_new(GimpLayer * layer,GimpContext * context,GtkWidget * parent,GimpAddMaskType add_mask_type,gboolean invert,GimpAddMaskCallback callback,gpointer user_data)73 layer_add_mask_dialog_new (GimpLayer           *layer,
74                            GimpContext         *context,
75                            GtkWidget           *parent,
76                            GimpAddMaskType      add_mask_type,
77                            gboolean             invert,
78                            GimpAddMaskCallback  callback,
79                            gpointer             user_data)
80 {
81   LayerAddMaskDialog *private;
82   GtkWidget          *dialog;
83   GtkWidget          *vbox;
84   GtkWidget          *frame;
85   GtkWidget          *combo;
86   GtkWidget          *button;
87   GimpImage          *image;
88   GimpChannel        *channel;
89 
90   g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
91   g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL);
92   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
93 
94   private = g_slice_new0 (LayerAddMaskDialog);
95 
96   private->layer         = layer;
97   private->add_mask_type = add_mask_type;
98   private->invert        = invert;
99   private->callback      = callback;
100   private->user_data     = user_data;
101 
102   dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (layer), context,
103                                      _("Add Layer Mask"), "gimp-layer-add-mask",
104                                      GIMP_ICON_LAYER_MASK,
105                                      _("Add a Mask to the Layer"),
106                                      parent,
107                                      gimp_standard_help_func,
108                                      GIMP_HELP_LAYER_MASK_ADD,
109 
110                                      _("_Cancel"), GTK_RESPONSE_CANCEL,
111                                      _("_Add"),    GTK_RESPONSE_OK,
112 
113                                      NULL);
114 
115   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
116                                            GTK_RESPONSE_OK,
117                                            GTK_RESPONSE_CANCEL,
118                                            -1);
119 
120   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
121 
122   g_object_weak_ref (G_OBJECT (dialog),
123                      (GWeakNotify) layer_add_mask_dialog_free, private);
124 
125   g_signal_connect (dialog, "response",
126                     G_CALLBACK (layer_add_mask_dialog_response),
127                     private);
128 
129   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
130   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
131   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
132                       vbox, TRUE, TRUE, 0);
133   gtk_widget_show (vbox);
134 
135   frame =
136     gimp_enum_radio_frame_new (GIMP_TYPE_ADD_MASK_TYPE,
137                                gtk_label_new (_("Initialize Layer Mask to:")),
138                                G_CALLBACK (gimp_radio_button_update),
139                                &private->add_mask_type,
140                                &button);
141   gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (button),
142                                    private->add_mask_type);
143 
144   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
145   gtk_widget_show (frame);
146 
147   image = gimp_item_get_image (GIMP_ITEM (layer));
148 
149   combo = gimp_container_combo_box_new (gimp_image_get_channels (image),
150                                         context,
151                                         GIMP_VIEW_SIZE_SMALL, 1);
152   gimp_enum_radio_frame_add (GTK_FRAME (frame), combo,
153                              GIMP_ADD_MASK_CHANNEL, TRUE);
154   gtk_widget_show (combo);
155 
156   g_signal_connect (combo, "select-item",
157                     G_CALLBACK (layer_add_mask_dialog_channel_selected),
158                     private);
159 
160   channel = gimp_image_get_active_channel (image);
161 
162   if (! channel)
163     channel = GIMP_CHANNEL (gimp_container_get_first_child (gimp_image_get_channels (image)));
164 
165   gimp_container_view_select_item (GIMP_CONTAINER_VIEW (combo),
166                                    GIMP_VIEWABLE (channel));
167 
168   button = gtk_check_button_new_with_mnemonic (_("In_vert mask"));
169   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), private->invert);
170   gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
171   gtk_widget_show (button);
172 
173   g_signal_connect (button, "toggled",
174                     G_CALLBACK (gimp_toggle_button_update),
175                     &private->invert);
176 
177   return dialog;
178 }
179 
180 
181 /*  private functions  */
182 
183 static void
layer_add_mask_dialog_free(LayerAddMaskDialog * private)184 layer_add_mask_dialog_free (LayerAddMaskDialog *private)
185 {
186   g_slice_free (LayerAddMaskDialog, private);
187 }
188 
189 static void
layer_add_mask_dialog_response(GtkWidget * dialog,gint response_id,LayerAddMaskDialog * private)190 layer_add_mask_dialog_response (GtkWidget          *dialog,
191                                 gint                response_id,
192                                 LayerAddMaskDialog *private)
193 {
194   if (response_id == GTK_RESPONSE_OK)
195     {
196       GimpImage *image = gimp_item_get_image (GIMP_ITEM (private->layer));
197 
198       if (private->add_mask_type == GIMP_ADD_MASK_CHANNEL &&
199           ! private->channel)
200         {
201           gimp_message_literal (image->gimp,
202                                 G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
203                                 _("Please select a channel first"));
204           return;
205         }
206 
207       private->callback (dialog,
208                          private->layer,
209                          private->add_mask_type,
210                          private->channel,
211                          private->invert,
212                          private->user_data);
213     }
214   else
215     {
216       gtk_widget_destroy (dialog);
217     }
218 }
219 
220 static gboolean
layer_add_mask_dialog_channel_selected(GimpContainerView * view,GimpViewable * viewable,gpointer insert_data,LayerAddMaskDialog * private)221 layer_add_mask_dialog_channel_selected (GimpContainerView  *view,
222                                         GimpViewable       *viewable,
223                                         gpointer            insert_data,
224                                         LayerAddMaskDialog *private)
225 {
226   private->channel = GIMP_CHANNEL (viewable);
227 
228   return TRUE;
229 }
230