[gimp] app: make the LayerAddMaskDialog struct private



commit 41a64b7a634535190accd13fd27d56e9124e3fea
Author: Michael Natterer <mitch gimp org>
Date:   Fri Sep 23 14:35:14 2016 +0200

    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.

 app/actions/layers-commands.c       |  116 +++++++++++++++----------------
 app/dialogs/layer-add-mask-dialog.c |  130 +++++++++++++++++++++++++----------
 app/dialogs/layer-add-mask-dialog.h |   33 ++++-----
 3 files changed, 164 insertions(+), 115 deletions(-)
---
diff --git a/app/actions/layers-commands.c b/app/actions/layers-commands.c
index 14318c1..e1f1b30 100644
--- a/app/actions/layers-commands.c
+++ b/app/actions/layers-commands.c
@@ -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;
+  GimpImage *image;
+  GimpLayer *layer;
+  GtkWidget *widget;
+  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 = layer_add_mask_dialog_new (layer, action_data_get_context (data),
-                                      widget,
-                                      config->layer_add_mask_type,
-                                      config->layer_add_mask_invert);
+  dialog = dialogs_get_dialog (G_OBJECT (layer), ADD_MASK_DIALOG_KEY);
 
-  g_signal_connect (dialog->dialog, "response",
-                    G_CALLBACK (layers_add_mask_response),
-                    dialog);
+  if (! 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
@@ -1174,55 +1182,43 @@ 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));
-      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);
+  GimpImage        *image  = gimp_item_get_image (GIMP_ITEM (layer));
+  GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
+  GimpLayerMask    *mask;
+  GError           *error = NULL;
 
-      mask = gimp_layer_create_mask (layer,
-                                     config->layer_add_mask_type,
-                                     dialog->channel);
+  g_object_set (config,
+                "layer-add-mask-type",   add_mask_type,
+                "layer-add-mask-invert", invert,
+                NULL);
 
-      if (config->layer_add_mask_invert)
-        gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
+  mask = gimp_layer_create_mask (layer,
+                                 config->layer_add_mask_type,
+                                 channel);
 
-      if (! gimp_layer_add_mask (layer, mask, TRUE, &error))
-        {
-          gimp_message_literal (image->gimp,
-                                G_OBJECT (widget), GIMP_MESSAGE_WARNING,
-                                error->message);
-          g_object_unref (mask);
-          g_clear_error (&error);
-          return;
-        }
+  if (config->layer_add_mask_invert)
+    gimp_channel_invert (GIMP_CHANNEL (mask), FALSE);
 
-      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
diff --git a/app/dialogs/layer-add-mask-dialog.c b/app/dialogs/layer-add-mask-dialog.c
index d73134f..41eb34e 100644
--- a/app/dialogs/layer-add-mask-dialog.c
+++ b/app/dialogs/layer-add-mask-dialog.c
@@ -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 *
-layer_add_mask_dialog_new (GimpLayer       *layer,
-                           GimpContext     *context,
-                           GtkWidget       *parent,
-                           GimpAddMaskType  add_mask_type,
-                           gboolean         invert)
+GtkWidget *
+layer_add_mask_dialog_new (GimpLayer           *layer,
+                           GimpContext         *context,
+                           GtkWidget           *parent,
+                           GimpAddMaskType      add_mask_type,
+                           gboolean             invert,
+                           GimpAddMaskCallback  callback,
+                           gpointer             user_data)
 {
-  LayerAddMaskDialog *dialog;
+  LayerAddMaskDialog *private;
+  GtkWidget          *dialog;
   GtkWidget          *vbox;
   GtkWidget          *frame;
   GtkWidget          *combo;
@@ -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 (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,
-                              _("Add Layer Mask"), "gimp-layer-add-mask",
-                              GIMP_STOCK_LAYER_MASK,
-                              _("Add a Mask to the Layer"),
-                              parent,
-                              gimp_standard_help_func,
-                              GIMP_HELP_LAYER_MASK_ADD,
+  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"),
+                                     parent,
+                                     gimp_standard_help_func,
+                                     GIMP_HELP_LAYER_MASK_ADD,
 
-                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-                              GTK_STOCK_ADD,    GTK_RESPONSE_OK,
+                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                     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),
-                     (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);
 }
diff --git a/app/dialogs/layer-add-mask-dialog.h b/app/dialogs/layer-add-mask-dialog.h
index 40911d9..fe0b549 100644
--- a/app/dialogs/layer-add-mask-dialog.h
+++ b/app/dialogs/layer-add-mask-dialog.h
@@ -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;
-};
-
-
-LayerAddMaskDialog * layer_add_mask_dialog_new (GimpLayer       *layer,
-                                                GimpContext     *context,
-                                                GtkWidget       *parent,
-                                                GimpAddMaskType  add_mask_type,
-                                                gboolean         invert);
+typedef void (* GimpAddMaskCallback) (GtkWidget       *dialog,
+                                      GimpLayer       *layer,
+                                      GimpAddMaskType  add_mask_type,
+                                      GimpChannel     *channel,
+                                      gboolean         invert,
+                                      gpointer         user_data);
+
+
+GtkWidget * layer_add_mask_dialog_new (GimpLayer           *layer,
+                                       GimpContext         *context,
+                                       GtkWidget           *parent,
+                                       GimpAddMaskType      add_mask_type,
+                                       gboolean             invert,
+                                       GimpAddMaskCallback  callback,
+                                       gpointer             user_data);
 
 
 #endif /* __LAYER_ADD_MASK_DIALOG_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]