Commit 41a64b7a authored by Michael Natterer's avatar Michael Natterer 😴

app: make the LayerAddMaskDialog struct private

Instead, provide a custom GimpAddMaskCallback, connect to "response"
internally and call the callback. Takes clutter out of layers-commands.c.
Also attach the dialog to the layer so we don't show multiple add
mask dialogs. Should do the same to all dialogs with public structs,
the custom callback approach keep things more encapsulated.
parent 390b8a3a
...@@ -118,10 +118,12 @@ static void layers_new_layer_response (GtkWidget *widget, ...@@ -118,10 +118,12 @@ static void layers_new_layer_response (GtkWidget *widget,
static void layers_edit_layer_response (GtkWidget *widget, static void layers_edit_layer_response (GtkWidget *widget,
gint response_id, gint response_id,
LayerOptionsDialog *dialog); LayerOptionsDialog *dialog);
static void layers_add_mask_response (GtkWidget *widget, static void layers_add_mask_callback (GtkWidget *dialog,
gint response_id, GimpLayer *layer,
LayerAddMaskDialog *dialog); GimpAddMaskType add_mask_type,
GimpChannel *channel,
gboolean invert,
gpointer user_data);
static void layers_scale_layer_callback (GtkWidget *dialog, static void layers_scale_layer_callback (GtkWidget *dialog,
GimpViewable *viewable, GimpViewable *viewable,
gint width, gint width,
...@@ -778,29 +780,35 @@ void ...@@ -778,29 +780,35 @@ void
layers_mask_add_cmd_callback (GtkAction *action, layers_mask_add_cmd_callback (GtkAction *action,
gpointer data) gpointer data)
{ {
LayerAddMaskDialog *dialog; GimpImage *image;
GimpImage *image; GimpLayer *layer;
GimpLayer *layer; GtkWidget *widget;
GtkWidget *widget; GtkWidget *dialog;
GimpDialogConfig *config;
return_if_no_layer (image, layer, data); return_if_no_layer (image, layer, data);
return_if_no_widget (widget, data); return_if_no_widget (widget, data);
if (gimp_layer_get_mask (layer)) if (gimp_layer_get_mask (layer))
return; return;
config = GIMP_DIALOG_CONFIG (image->gimp->config); #define ADD_MASK_DIALOG_KEY "gimp-add-mask-dialog"
dialog = layer_add_mask_dialog_new (layer, action_data_get_context (data), dialog = dialogs_get_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY);
widget,
config->layer_add_mask_type,
config->layer_add_mask_invert);
g_signal_connect (dialog->dialog, "response", if (! dialog)
G_CALLBACK (layers_add_mask_response), {
dialog); GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
gtk_widget_show (dialog->dialog); dialog = layer_add_mask_dialog_new (layer, action_data_get_context (data),
widget,
config->layer_add_mask_type,
config->layer_add_mask_invert,
layers_add_mask_callback,
NULL);
dialogs_attach_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY, dialog);
}
gtk_window_present (GTK_WINDOW (dialog));
} }
void void
...@@ -1174,55 +1182,43 @@ layers_edit_layer_response (GtkWidget *widget, ...@@ -1174,55 +1182,43 @@ layers_edit_layer_response (GtkWidget *widget,
} }
static void static void
layers_add_mask_response (GtkWidget *widget, layers_add_mask_callback (GtkWidget *dialog,
gint response_id, GimpLayer *layer,
LayerAddMaskDialog *dialog) GimpAddMaskType add_mask_type,
GimpChannel *channel,
gboolean invert,
gpointer user_data)
{ {
if (response_id == GTK_RESPONSE_OK) GimpImage *image = gimp_item_get_image (GIMP_ITEM (layer));
{ GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
GimpLayer *layer = dialog->layer; GimpLayerMask *mask;
GimpImage *image = gimp_item_get_image (GIMP_ITEM (layer)); GError *error = NULL;
GimpLayerMask *mask;
GimpDialogConfig *config;
GError *error = NULL;
config = GIMP_DIALOG_CONFIG (image->gimp->config);
if (dialog->add_mask_type == GIMP_ADD_MASK_CHANNEL &&
! dialog->channel)
{
gimp_message_literal (image->gimp,
G_OBJECT (widget), GIMP_MESSAGE_WARNING,
_("Please select a channel first"));
return;
}
g_object_set (config,
"layer-add-mask-type", dialog->add_mask_type,
"layer-add-mask-invert", dialog->invert,
NULL);
mask = gimp_layer_create_mask (layer, g_object_set (config,
config->layer_add_mask_type, "layer-add-mask-type", add_mask_type,
dialog->channel); "layer-add-mask-invert", invert,
NULL);
if (config->layer_add_mask_invert) mask = gimp_layer_create_mask (layer,
gimp_channel_invert (GIMP_CHANNEL (mask), FALSE); config->layer_add_mask_type,
channel);
if (! gimp_layer_add_mask (layer, mask, TRUE, &error)) if (config->layer_add_mask_invert)
{ gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
gimp_message_literal (image->gimp,
G_OBJECT (widget), GIMP_MESSAGE_WARNING,
error->message);
g_object_unref (mask);
g_clear_error (&error);
return;
}
gimp_image_flush (image); if (! gimp_layer_add_mask (layer, mask, TRUE, &error))
{
gimp_message_literal (image->gimp,
G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
error->message);
g_object_unref (mask);
g_clear_error (&error);
return;
} }
gtk_widget_destroy (dialog->dialog); gimp_image_flush (image);
gtk_widget_destroy (dialog);
} }
static void static void
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "dialogs-types.h" #include "dialogs-types.h"
#include "core/gimp.h"
#include "core/gimpchannel.h" #include "core/gimpchannel.h"
#include "core/gimpcontainer.h" #include "core/gimpcontainer.h"
#include "core/gimpcontext.h" #include "core/gimpcontext.h"
...@@ -41,25 +42,44 @@ ...@@ -41,25 +42,44 @@
#include "gimp-intl.h" #include "gimp-intl.h"
typedef struct _LayerAddMaskDialog LayerAddMaskDialog;
struct _LayerAddMaskDialog
{
GimpLayer *layer;
GimpAddMaskType add_mask_type;
GimpChannel *channel;
gboolean invert;
GimpAddMaskCallback callback;
gpointer user_data;
};
/* local function prototypes */ /* local function prototypes */
static gboolean layer_add_mask_dialog_channel_selected (GimpContainerView *view, static gboolean layer_add_mask_dialog_channel_selected (GimpContainerView *view,
GimpViewable *viewable, GimpViewable *viewable,
gpointer insert_data, gpointer insert_data,
LayerAddMaskDialog *dialog); LayerAddMaskDialog *dialog);
static void layer_add_mask_dialog_free (LayerAddMaskDialog *dialog); static void layer_add_mask_dialog_response (GtkWidget *dialog,
gint response_id,
LayerAddMaskDialog *private);
static void layer_add_mask_dialog_free (LayerAddMaskDialog *private);
/* public functions */ /* public functions */
LayerAddMaskDialog * GtkWidget *
layer_add_mask_dialog_new (GimpLayer *layer, layer_add_mask_dialog_new (GimpLayer *layer,
GimpContext *context, GimpContext *context,
GtkWidget *parent, GtkWidget *parent,
GimpAddMaskType add_mask_type, GimpAddMaskType add_mask_type,
gboolean invert) gboolean invert,
GimpAddMaskCallback callback,
gpointer user_data)
{ {
LayerAddMaskDialog *dialog; LayerAddMaskDialog *private;
GtkWidget *dialog;
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *frame; GtkWidget *frame;
GtkWidget *combo; GtkWidget *combo;
...@@ -71,39 +91,44 @@ layer_add_mask_dialog_new (GimpLayer *layer, ...@@ -71,39 +91,44 @@ layer_add_mask_dialog_new (GimpLayer *layer,
g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL); g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
dialog = g_slice_new0 (LayerAddMaskDialog); private = g_slice_new0 (LayerAddMaskDialog);
dialog->layer = layer; private->layer = layer;
dialog->add_mask_type = add_mask_type; private->add_mask_type = add_mask_type;
dialog->invert = invert; private->invert = invert;
private->callback = callback;
private->user_data = user_data;
dialog->dialog = dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (layer), context,
gimp_viewable_dialog_new (GIMP_VIEWABLE (layer), context, _("Add Layer Mask"), "gimp-layer-add-mask",
_("Add Layer Mask"), "gimp-layer-add-mask", GIMP_STOCK_LAYER_MASK,
GIMP_STOCK_LAYER_MASK, _("Add a Mask to the Layer"),
_("Add a Mask to the Layer"), parent,
parent, gimp_standard_help_func,
gimp_standard_help_func, GIMP_HELP_LAYER_MASK_ADD,
GIMP_HELP_LAYER_MASK_ADD,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_ADD, GTK_RESPONSE_OK, GTK_STOCK_ADD, GTK_RESPONSE_OK,
NULL); NULL);
gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE); gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
g_object_weak_ref (G_OBJECT (dialog->dialog), g_object_weak_ref (G_OBJECT (dialog),
(GWeakNotify) layer_add_mask_dialog_free, dialog); (GWeakNotify) layer_add_mask_dialog_free, private);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog->dialog), gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK, GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL, GTK_RESPONSE_CANCEL,
-1); -1);
g_signal_connect (dialog, "response",
G_CALLBACK (layer_add_mask_dialog_response),
private);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12); gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog->dialog))), gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
vbox, TRUE, TRUE, 0); vbox, TRUE, TRUE, 0);
gtk_widget_show (vbox); gtk_widget_show (vbox);
...@@ -111,10 +136,10 @@ layer_add_mask_dialog_new (GimpLayer *layer, ...@@ -111,10 +136,10 @@ layer_add_mask_dialog_new (GimpLayer *layer,
gimp_enum_radio_frame_new (GIMP_TYPE_ADD_MASK_TYPE, gimp_enum_radio_frame_new (GIMP_TYPE_ADD_MASK_TYPE,
gtk_label_new (_("Initialize Layer Mask to:")), gtk_label_new (_("Initialize Layer Mask to:")),
G_CALLBACK (gimp_radio_button_update), G_CALLBACK (gimp_radio_button_update),
&dialog->add_mask_type, &private->add_mask_type,
&button); &button);
gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (button), gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (button),
dialog->add_mask_type); private->add_mask_type);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
gtk_widget_show (frame); gtk_widget_show (frame);
...@@ -130,7 +155,7 @@ layer_add_mask_dialog_new (GimpLayer *layer, ...@@ -130,7 +155,7 @@ layer_add_mask_dialog_new (GimpLayer *layer,
g_signal_connect (combo, "select-item", g_signal_connect (combo, "select-item",
G_CALLBACK (layer_add_mask_dialog_channel_selected), G_CALLBACK (layer_add_mask_dialog_channel_selected),
dialog); private);
channel = gimp_image_get_active_channel (image); channel = gimp_image_get_active_channel (image);
...@@ -141,13 +166,13 @@ layer_add_mask_dialog_new (GimpLayer *layer, ...@@ -141,13 +166,13 @@ layer_add_mask_dialog_new (GimpLayer *layer,
GIMP_VIEWABLE (channel)); GIMP_VIEWABLE (channel));
button = gtk_check_button_new_with_mnemonic (_("In_vert mask")); button = gtk_check_button_new_with_mnemonic (_("In_vert mask"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), dialog->invert); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), private->invert);
gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0); gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
gtk_widget_show (button); gtk_widget_show (button);
g_signal_connect (button, "toggled", g_signal_connect (button, "toggled",
G_CALLBACK (gimp_toggle_button_update), G_CALLBACK (gimp_toggle_button_update),
&dialog->invert); &private->invert);
return dialog; return dialog;
} }
...@@ -159,15 +184,46 @@ static gboolean ...@@ -159,15 +184,46 @@ static gboolean
layer_add_mask_dialog_channel_selected (GimpContainerView *view, layer_add_mask_dialog_channel_selected (GimpContainerView *view,
GimpViewable *viewable, GimpViewable *viewable,
gpointer insert_data, gpointer insert_data,
LayerAddMaskDialog *dialog) LayerAddMaskDialog *private)
{ {
dialog->channel = GIMP_CHANNEL (viewable); private->channel = GIMP_CHANNEL (viewable);
return TRUE; return TRUE;
} }
static void static void
layer_add_mask_dialog_free (LayerAddMaskDialog *dialog) layer_add_mask_dialog_response (GtkWidget *dialog,
gint response_id,
LayerAddMaskDialog *private)
{
if (response_id == GTK_RESPONSE_OK)
{
GimpImage *image = gimp_item_get_image (GIMP_ITEM (private->layer));
if (private->add_mask_type == GIMP_ADD_MASK_CHANNEL &&
! private->channel)
{
gimp_message_literal (image->gimp,
G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
_("Please select a channel first"));
return;
}
private->callback (dialog,
private->layer,
private->add_mask_type,
private->channel,
private->invert,
private->user_data);
}
else
{
gtk_widget_destroy (dialog);
}
}
static void
layer_add_mask_dialog_free (LayerAddMaskDialog *private)
{ {
g_slice_free (LayerAddMaskDialog, dialog); g_slice_free (LayerAddMaskDialog, private);
} }
...@@ -19,24 +19,21 @@ ...@@ -19,24 +19,21 @@
#define __LAYER_ADD_MASK_DIALOG_H__ #define __LAYER_ADD_MASK_DIALOG_H__
typedef struct _LayerAddMaskDialog LayerAddMaskDialog; typedef void (* GimpAddMaskCallback) (GtkWidget *dialog,
GimpLayer *layer,
struct _LayerAddMaskDialog GimpAddMaskType add_mask_type,
{ GimpChannel *channel,
GtkWidget *dialog; gboolean invert,
gpointer user_data);
GimpLayer *layer;
GimpAddMaskType add_mask_type;
GimpChannel *channel; GtkWidget * layer_add_mask_dialog_new (GimpLayer *layer,
gboolean invert; GimpContext *context,
}; GtkWidget *parent,
GimpAddMaskType add_mask_type,
gboolean invert,
LayerAddMaskDialog * layer_add_mask_dialog_new (GimpLayer *layer, GimpAddMaskCallback callback,
GimpContext *context, gpointer user_data);
GtkWidget *parent,
GimpAddMaskType add_mask_type,
gboolean invert);
#endif /* __LAYER_ADD_MASK_DIALOG_H__ */ #endif /* __LAYER_ADD_MASK_DIALOG_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment