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,
static void layers_edit_layer_response (GtkWidget *widget,
gint response_id,
LayerOptionsDialog *dialog);
static void layers_add_mask_response (GtkWidget *widget,
gint response_id,
LayerAddMaskDialog *dialog);
static void layers_add_mask_callback (GtkWidget *dialog,
GimpLayer *layer,
GimpAddMaskType add_mask_type,
GimpChannel *channel,
gboolean invert,
gpointer user_data);
static void layers_scale_layer_callback (GtkWidget *dialog,
GimpViewable *viewable,
gint width,
......@@ -778,29 +780,35 @@ void
layers_mask_add_cmd_callback (GtkAction *action,
gpointer data)
{
LayerAddMaskDialog *dialog;
GimpImage *image;
GimpLayer *layer;
GtkWidget *widget;
GimpDialogConfig *config;
GtkWidget *dialog;
return_if_no_layer (image, layer, data);
return_if_no_widget (widget, data);
if (gimp_layer_get_mask (layer))
return;
config = GIMP_DIALOG_CONFIG (image->gimp->config);
#define ADD_MASK_DIALOG_KEY "gimp-add-mask-dialog"
dialog = dialogs_get_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY);
if (! dialog)
{
GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
dialog = layer_add_mask_dialog_new (layer, action_data_get_context (data),
widget,
config->layer_add_mask_type,
config->layer_add_mask_invert);
config->layer_add_mask_invert,
layers_add_mask_callback,
NULL);
g_signal_connect (dialog->dialog, "response",
G_CALLBACK (layers_add_mask_response),
dialog);
dialogs_attach_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY, dialog);
}
gtk_widget_show (dialog->dialog);
gtk_window_present (GTK_WINDOW (dialog));
}
void
......@@ -1174,37 +1182,26 @@ layers_edit_layer_response (GtkWidget *widget,
}
static void
layers_add_mask_response (GtkWidget *widget,
gint response_id,
LayerAddMaskDialog *dialog)
layers_add_mask_callback (GtkWidget *dialog,
GimpLayer *layer,
GimpAddMaskType add_mask_type,
GimpChannel *channel,
gboolean invert,
gpointer user_data)
{
if (response_id == GTK_RESPONSE_OK)
{
GimpLayer *layer = dialog->layer;
GimpImage *image = gimp_item_get_image (GIMP_ITEM (layer));
GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
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,
"layer-add-mask-type", add_mask_type,
"layer-add-mask-invert", invert,
NULL);
mask = gimp_layer_create_mask (layer,
config->layer_add_mask_type,
dialog->channel);
channel);
if (config->layer_add_mask_invert)
gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
......@@ -1212,7 +1209,7 @@ layers_add_mask_response (GtkWidget *widget,
if (! gimp_layer_add_mask (layer, mask, TRUE, &error))
{
gimp_message_literal (image->gimp,
G_OBJECT (widget), GIMP_MESSAGE_WARNING,
G_OBJECT (dialog), GIMP_MESSAGE_WARNING,
error->message);
g_object_unref (mask);
g_clear_error (&error);
......@@ -1220,9 +1217,8 @@ layers_add_mask_response (GtkWidget *widget,
}
gimp_image_flush (image);
}
gtk_widget_destroy (dialog->dialog);
gtk_widget_destroy (dialog);
}
static void
......
......@@ -24,6 +24,7 @@
#include "dialogs-types.h"
#include "core/gimp.h"
#include "core/gimpchannel.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
......@@ -41,25 +42,44 @@
#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 */
static gboolean layer_add_mask_dialog_channel_selected (GimpContainerView *view,
GimpViewable *viewable,
gpointer insert_data,
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 */
LayerAddMaskDialog *
GtkWidget *
layer_add_mask_dialog_new (GimpLayer *layer,
GimpContext *context,
GtkWidget *parent,
GimpAddMaskType add_mask_type,
gboolean invert)
gboolean invert,
GimpAddMaskCallback callback,
gpointer user_data)
{
LayerAddMaskDialog *dialog;
LayerAddMaskDialog *private;
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *combo;
......@@ -71,14 +91,15 @@ layer_add_mask_dialog_new (GimpLayer *layer,
g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
dialog = g_slice_new0 (LayerAddMaskDialog);
private = g_slice_new0 (LayerAddMaskDialog);
dialog->layer = layer;
dialog->add_mask_type = add_mask_type;
dialog->invert = invert;
private->layer = layer;
private->add_mask_type = add_mask_type;
private->invert = invert;
private->callback = callback;
private->user_data = user_data;
dialog->dialog =
gimp_viewable_dialog_new (GIMP_VIEWABLE (layer), context,
dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (layer), context,
_("Add Layer Mask"), "gimp-layer-add-mask",
GIMP_STOCK_LAYER_MASK,
_("Add a Mask to the Layer"),
......@@ -91,19 +112,23 @@ layer_add_mask_dialog_new (GimpLayer *layer,
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),
(GWeakNotify) layer_add_mask_dialog_free, dialog);
g_object_weak_ref (G_OBJECT (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_CANCEL,
-1);
g_signal_connect (dialog, "response",
G_CALLBACK (layer_add_mask_dialog_response),
private);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 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);
gtk_widget_show (vbox);
......@@ -111,10 +136,10 @@ layer_add_mask_dialog_new (GimpLayer *layer,
gimp_enum_radio_frame_new (GIMP_TYPE_ADD_MASK_TYPE,
gtk_label_new (_("Initialize Layer Mask to:")),
G_CALLBACK (gimp_radio_button_update),
&dialog->add_mask_type,
&private->add_mask_type,
&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_widget_show (frame);
......@@ -130,7 +155,7 @@ layer_add_mask_dialog_new (GimpLayer *layer,
g_signal_connect (combo, "select-item",
G_CALLBACK (layer_add_mask_dialog_channel_selected),
dialog);
private);
channel = gimp_image_get_active_channel (image);
......@@ -141,13 +166,13 @@ layer_add_mask_dialog_new (GimpLayer *layer,
GIMP_VIEWABLE (channel));
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_widget_show (button);
g_signal_connect (button, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&dialog->invert);
&private->invert);
return dialog;
}
......@@ -159,15 +184,46 @@ static gboolean
layer_add_mask_dialog_channel_selected (GimpContainerView *view,
GimpViewable *viewable,
gpointer insert_data,
LayerAddMaskDialog *dialog)
LayerAddMaskDialog *private)
{
dialog->channel = GIMP_CHANNEL (viewable);
private->channel = GIMP_CHANNEL (viewable);
return TRUE;
}
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 @@
#define __LAYER_ADD_MASK_DIALOG_H__
typedef struct _LayerAddMaskDialog LayerAddMaskDialog;
struct _LayerAddMaskDialog
{
GtkWidget *dialog;
GimpLayer *layer;
GimpAddMaskType add_mask_type;
GimpChannel *channel;
gboolean invert;
};
typedef void (* GimpAddMaskCallback) (GtkWidget *dialog,
GimpLayer *layer,
GimpAddMaskType add_mask_type,
GimpChannel *channel,
gboolean invert,
gpointer user_data);
LayerAddMaskDialog * layer_add_mask_dialog_new (GimpLayer *layer,
GtkWidget * layer_add_mask_dialog_new (GimpLayer *layer,
GimpContext *context,
GtkWidget *parent,
GimpAddMaskType add_mask_type,
gboolean invert);
gboolean invert,
GimpAddMaskCallback callback,
gpointer user_data);
#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