[gimp] app: add a callback to the stroke dialog



commit 0932386d1b9184c70e7b45a0946742cb7dda5092
Author: Michael Natterer <mitch gimp org>
Date:   Tue Sep 27 11:15:52 2016 +0200

    app: add a callback to the stroke dialog
    
    and move the actual stroking code to select-commands.c,
    vectors-commands.c and gimpvectorstool.c. Remember the active drawable
    so the stroke always happens on the drawable the dialog was invoked
    with.

 app/actions/select-commands.c  |   90 ++++++++++++++++------
 app/actions/vectors-commands.c |   47 +++++++++++-
 app/dialogs/fill-dialog.c      |    1 +
 app/dialogs/stroke-dialog.c    |  165 +++++++++++++++++++---------------------
 app/dialogs/stroke-dialog.h    |   24 +++++--
 app/tools/gimpvectortool.c     |   60 +++++++++++++--
 6 files changed, 264 insertions(+), 123 deletions(-)
---
diff --git a/app/actions/select-commands.c b/app/actions/select-commands.c
index b7e3af2..d0a5793 100644
--- a/app/actions/select-commands.c
+++ b/app/actions/select-commands.c
@@ -54,28 +54,34 @@
 
 /*  local function prototypes  */
 
-static void   select_feather_callback (GtkWidget       *widget,
-                                       gdouble          size,
-                                       GimpUnit         unit,
-                                       gpointer         data);
-static void   select_border_callback  (GtkWidget       *widget,
-                                       gdouble          size,
-                                       GimpUnit         unit,
-                                       gpointer         data);
-static void   select_grow_callback    (GtkWidget       *widget,
-                                       gdouble          size,
-                                       GimpUnit         unit,
-                                       gpointer         data);
-static void   select_shrink_callback  (GtkWidget       *widget,
-                                       gdouble          size,
-                                       GimpUnit         unit,
-                                       gpointer         data);
-static void   select_fill_callback    (GtkWidget       *dialog,
-                                       GimpItem        *item,
-                                       GimpDrawable    *drawable,
-                                       GimpContext     *context,
-                                       GimpFillOptions *options,
-                                       gpointer         data);
+static void   select_feather_callback (GtkWidget         *widget,
+                                       gdouble            size,
+                                       GimpUnit           unit,
+                                       gpointer           data);
+static void   select_border_callback  (GtkWidget         *widget,
+                                       gdouble            size,
+                                       GimpUnit           unit,
+                                       gpointer           data);
+static void   select_grow_callback    (GtkWidget         *widget,
+                                       gdouble            size,
+                                       GimpUnit           unit,
+                                       gpointer           data);
+static void   select_shrink_callback  (GtkWidget         *widget,
+                                       gdouble            size,
+                                       GimpUnit           unit,
+                                       gpointer           data);
+static void   select_fill_callback    (GtkWidget         *dialog,
+                                       GimpItem          *item,
+                                       GimpDrawable      *drawable,
+                                       GimpContext       *context,
+                                       GimpFillOptions   *options,
+                                       gpointer           data);
+static void   select_stroke_callback  (GtkWidget         *dialog,
+                                       GimpItem          *item,
+                                       GimpDrawable      *drawable,
+                                       GimpContext       *context,
+                                       GimpStrokeOptions *options,
+                                       gpointer           data);
 
 
 /*  public functions  */
@@ -498,12 +504,18 @@ select_stroke_cmd_callback (GtkAction *action,
 
   if (! dialog)
     {
+      GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
+
       dialog = stroke_dialog_new (GIMP_ITEM (gimp_image_get_mask (image)),
+                                  drawable,
                                   action_data_get_context (data),
                                   _("Stroke Selection"),
                                   GIMP_STOCK_SELECTION_STROKE,
                                   GIMP_HELP_SELECTION_STROKE,
-                                  widget);
+                                  widget,
+                                  config->stroke_options,
+                                  select_stroke_callback,
+                                  NULL);
 
       dialogs_attach_dialog (G_OBJECT (drawable), STROKE_DIALOG_KEY, dialog);
     }
@@ -762,3 +774,35 @@ select_fill_callback (GtkWidget       *dialog,
 
   gtk_widget_destroy (dialog);
 }
+
+static void
+select_stroke_callback (GtkWidget         *dialog,
+                        GimpItem          *item,
+                        GimpDrawable      *drawable,
+                        GimpContext       *context,
+                        GimpStrokeOptions *options,
+                        gpointer           data)
+{
+  GimpDialogConfig *config = GIMP_DIALOG_CONFIG (context->gimp->config);
+  GimpImage        *image  = gimp_item_get_image (item);
+  GError           *error  = NULL;
+
+  gimp_config_sync (G_OBJECT (options),
+                    G_OBJECT (config->stroke_options), 0);
+
+  if (! gimp_item_stroke (item, drawable, context, options, NULL,
+                          TRUE, NULL, &error))
+    {
+      gimp_message_literal (context->gimp,
+                            G_OBJECT (dialog),
+                            GIMP_MESSAGE_WARNING,
+                            error ? error->message : "NULL");
+
+      g_clear_error (&error);
+      return;
+    }
+
+  gimp_image_flush (image);
+
+  gtk_widget_destroy (dialog);
+}
diff --git a/app/actions/vectors-commands.c b/app/actions/vectors-commands.c
index f36f659..56f8bed 100644
--- a/app/actions/vectors-commands.c
+++ b/app/actions/vectors-commands.c
@@ -90,6 +90,12 @@ static void   vectors_fill_callback            (GtkWidget            *dialog,
                                                 GimpContext          *context,
                                                 GimpFillOptions      *options,
                                                 gpointer              user_data);
+static void   vectors_stroke_callback          (GtkWidget            *dialog,
+                                                GimpItem             *item,
+                                                GimpDrawable         *drawable,
+                                                GimpContext          *context,
+                                                GimpStrokeOptions    *options,
+                                                gpointer              user_data);
 static void   vectors_import_response          (GtkWidget            *widget,
                                                 gint                  response_id,
                                                 VectorsImportDialog  *dialog);
@@ -503,12 +509,18 @@ vectors_stroke_cmd_callback (GtkAction *action,
 
   if (! dialog)
     {
+      GimpDialogConfig *config = GIMP_DIALOG_CONFIG (image->gimp->config);
+
       dialog = stroke_dialog_new (GIMP_ITEM (vectors),
+                                  drawable,
                                   action_data_get_context (data),
                                   _("Stroke Path"),
                                   GIMP_STOCK_PATH_STROKE,
                                   GIMP_HELP_PATH_STROKE,
-                                  widget);
+                                  widget,
+                                  config->stroke_options,
+                                  vectors_stroke_callback,
+                                  NULL);
 
       dialogs_attach_dialog (G_OBJECT (drawable), STROKE_DIALOG_KEY, dialog);
     }
@@ -868,6 +880,39 @@ vectors_fill_callback (GtkWidget       *dialog,
   gtk_widget_destroy (dialog);
 }
 
+
+static void
+vectors_stroke_callback (GtkWidget         *dialog,
+                         GimpItem          *item,
+                         GimpDrawable      *drawable,
+                         GimpContext       *context,
+                         GimpStrokeOptions *options,
+                         gpointer           data)
+{
+  GimpDialogConfig *config = GIMP_DIALOG_CONFIG (context->gimp->config);
+  GimpImage        *image  = gimp_item_get_image (item);
+  GError           *error  = NULL;
+
+  gimp_config_sync (G_OBJECT (options),
+                    G_OBJECT (config->stroke_options), 0);
+
+  if (! gimp_item_stroke (item, drawable, context, options, NULL,
+                          TRUE, NULL, &error))
+    {
+      gimp_message_literal (context->gimp,
+                            G_OBJECT (dialog),
+                            GIMP_MESSAGE_WARNING,
+                            error ? error->message : "NULL");
+
+      g_clear_error (&error);
+      return;
+    }
+
+  gimp_image_flush (image);
+
+  gtk_widget_destroy (dialog);
+}
+
 static void
 vectors_import_response (GtkWidget           *widget,
                          gint                 response_id,
diff --git a/app/dialogs/fill-dialog.c b/app/dialogs/fill-dialog.c
index b57ee0e..b75aa93 100644
--- a/app/dialogs/fill-dialog.c
+++ b/app/dialogs/fill-dialog.c
@@ -87,6 +87,7 @@ fill_dialog_new (GimpItem         *item,
   g_return_val_if_fail (icon_name != NULL, NULL);
   g_return_val_if_fail (help_id != NULL, NULL);
   g_return_val_if_fail (parent == NULL || GTK_IS_WIDGET (parent), NULL);
+  g_return_val_if_fail (callback != NULL, NULL);
 
   private = g_slice_new0 (FillDialog);
 
diff --git a/app/dialogs/stroke-dialog.c b/app/dialogs/stroke-dialog.c
index 5d33f43..bb87fd2 100644
--- a/app/dialogs/stroke-dialog.c
+++ b/app/dialogs/stroke-dialog.c
@@ -27,8 +27,6 @@
 
 #include "dialogs-types.h"
 
-#include "config/gimpdialogconfig.h"
-
 #include "core/gimp.h"
 #include "core/gimpdrawable.h"
 #include "core/gimpimage.h"
@@ -49,48 +47,72 @@
 #define RESPONSE_RESET 1
 
 
+typedef struct
+{
+  GimpItem           *item;
+  GimpDrawable       *drawable;
+  GimpContext        *context;
+  GimpStrokeOptions  *options;
+  GimpStrokeCallback  callback;
+  gpointer            user_data;
+
+  GtkWidget          *tool_combo;
+} StrokeDialog;
+
+
 /*  local functions  */
 
-static void  stroke_dialog_response (GtkWidget *widget,
-                                     gint       response_id,
-                                     GtkWidget *dialog);
+static void  stroke_dialog_response (GtkWidget    *dialog,
+                                     gint          response_id,
+                                     StrokeDialog *private);
+static void  stroke_dialog_free     (StrokeDialog *private);
 
 
 /*  public function  */
 
 GtkWidget *
-stroke_dialog_new (GimpItem    *item,
-                   GimpContext *context,
-                   const gchar *title,
-                   const gchar *icon_name,
-                   const gchar *help_id,
-                   GtkWidget   *parent)
+stroke_dialog_new (GimpItem           *item,
+                   GimpDrawable       *drawable,
+                   GimpContext        *context,
+                   const gchar        *title,
+                   const gchar        *icon_name,
+                   const gchar        *help_id,
+                   GtkWidget          *parent,
+                   GimpStrokeOptions  *options,
+                   GimpStrokeCallback  callback,
+                   gpointer            user_data)
 {
-  GimpDialogConfig  *config;
-  GimpStrokeOptions *options;
-  GimpImage         *image;
-  GtkWidget         *dialog;
-  GtkWidget         *main_vbox;
-  GtkWidget         *radio_box;
-  GtkWidget         *cairo_radio;
-  GtkWidget         *paint_radio;
-  GSList            *group;
-  GtkWidget         *frame;
+  StrokeDialog *private;
+  GimpImage    *image;
+  GtkWidget    *dialog;
+  GtkWidget    *main_vbox;
+  GtkWidget    *radio_box;
+  GtkWidget    *cairo_radio;
+  GtkWidget    *paint_radio;
+  GSList       *group;
+  GtkWidget    *frame;
 
   g_return_val_if_fail (GIMP_IS_ITEM (item), NULL);
+  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
   g_return_val_if_fail (icon_name != NULL, NULL);
   g_return_val_if_fail (help_id != NULL, NULL);
   g_return_val_if_fail (parent == NULL || GTK_IS_WIDGET (parent), NULL);
+  g_return_val_if_fail (callback != NULL, NULL);
 
   image = gimp_item_get_image (item);
 
-  config = GIMP_DIALOG_CONFIG (context->gimp->config);
+  private = g_slice_new0 (StrokeDialog);
 
-  options = gimp_stroke_options_new (context->gimp, context, TRUE);
+  private->item      = item;
+  private->drawable  = drawable;
+  private->context   = context;
+  private->options   = gimp_stroke_options_new (context->gimp, context, TRUE);
+  private->callback  = callback;
+  private->user_data = user_data;
 
-  gimp_config_sync (G_OBJECT (config->stroke_options),
-                    G_OBJECT (options), 0);
+  gimp_config_sync (G_OBJECT (options),
+                    G_OBJECT (private->options), 0);
 
   dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (item), context,
                                      title, "gimp-stroke-options",
@@ -114,13 +136,12 @@ stroke_dialog_new (GimpItem    *item,
 
   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
 
+  g_object_weak_ref (G_OBJECT (dialog),
+                     (GWeakNotify) stroke_dialog_free, private);
+
   g_signal_connect (dialog, "response",
                     G_CALLBACK (stroke_dialog_response),
-                    dialog);
-
-  g_object_set_data (G_OBJECT (dialog), "gimp-item", item);
-  g_object_set_data_full (G_OBJECT (dialog), "gimp-stroke-options", options,
-                          (GDestroyNotify) g_object_unref);
+                    private);
 
   main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
@@ -128,8 +149,8 @@ stroke_dialog_new (GimpItem    *item,
                       main_vbox, TRUE, TRUE, 0);
   gtk_widget_show (main_vbox);
 
-  radio_box = gimp_prop_enum_radio_box_new (G_OBJECT (options), "method",
-                                            -1, -1);
+  radio_box = gimp_prop_enum_radio_box_new (G_OBJECT (private->options),
+                                            "method", -1, -1);
 
   group = gtk_radio_button_get_group (g_object_get_data (G_OBJECT (radio_box),
                                                          "radio-button"));
@@ -174,7 +195,7 @@ stroke_dialog_new (GimpItem    *item,
 
     gimp_image_get_resolution (image, &xres, &yres);
 
-    stroke_editor = gimp_stroke_editor_new (options, yres, FALSE);
+    stroke_editor = gimp_stroke_editor_new (private->options, yres, FALSE);
     gtk_container_add (GTK_CONTAINER (frame), stroke_editor);
     gtk_widget_show (stroke_editor);
 
@@ -217,14 +238,14 @@ stroke_dialog_new (GimpItem    *item,
     gtk_widget_show (label);
 
     combo = gimp_container_combo_box_new (image->gimp->paint_info_list,
-                                          GIMP_CONTEXT (options),
+                                          GIMP_CONTEXT (private->options),
                                           16, 0);
     gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
     gtk_widget_show (combo);
 
-    g_object_set_data (G_OBJECT (dialog), "gimp-tool-menu", combo);
+    private->tool_combo = combo;
 
-    button = gimp_prop_check_button_new (G_OBJECT (options),
+    button = gimp_prop_check_button_new (G_OBJECT (private->options),
                                          "emulate-brush-dynamics",
                                          _("_Emulate brush dynamics"));
     gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
@@ -238,73 +259,43 @@ stroke_dialog_new (GimpItem    *item,
 /*  private functions  */
 
 static void
-stroke_dialog_response (GtkWidget  *widget,
-                        gint        response_id,
-                        GtkWidget  *dialog)
+stroke_dialog_response (GtkWidget    *dialog,
+                        gint          response_id,
+                        StrokeDialog *private)
 {
-  GimpStrokeOptions *options;
-  GimpItem          *item;
-  GimpImage         *image;
-  GimpContext       *context;
-  GtkWidget         *combo;
-
-  item    = g_object_get_data (G_OBJECT (dialog), "gimp-item");
-  options = g_object_get_data (G_OBJECT (dialog), "gimp-stroke-options");
-  combo   = g_object_get_data (G_OBJECT (dialog), "gimp-tool-menu");
-
-  image   = gimp_item_get_image (item);
-  context = GIMP_VIEWABLE_DIALOG (dialog)->context;
-
   switch (response_id)
     {
     case RESPONSE_RESET:
       {
-        GimpToolInfo *tool_info = gimp_context_get_tool (context);
+        GimpToolInfo *tool_info = gimp_context_get_tool (private->context);
 
-        gimp_config_reset (GIMP_CONFIG (options));
+        gimp_config_reset (GIMP_CONFIG (private->options));
 
-        gimp_container_view_select_item (GIMP_CONTAINER_VIEW (combo),
+        gimp_container_view_select_item (GIMP_CONTAINER_VIEW (private->tool_combo),
                                          GIMP_VIEWABLE (tool_info->paint_info));
 
       }
       break;
 
     case GTK_RESPONSE_OK:
-      {
-        GimpDialogConfig *config   = GIMP_DIALOG_CONFIG (context->gimp->config);
-        GimpDrawable     *drawable = gimp_image_get_active_drawable (image);
-        GError           *error    = NULL;
-
-        if (! drawable)
-          {
-            gimp_message_literal (context->gimp, G_OBJECT (widget),
-                                  GIMP_MESSAGE_WARNING,
-                                  _("There is no active layer or channel "
-                                    "to stroke to."));
-            return;
-          }
-
-        gimp_config_sync (G_OBJECT (options),
-                          G_OBJECT (config->stroke_options), 0);
-
-        if (! gimp_item_stroke (item, drawable, context, options, NULL,
-                                TRUE, NULL, &error))
-          {
-            gimp_message_literal (context->gimp,
-                                  G_OBJECT (widget),
-                                  GIMP_MESSAGE_WARNING,
-                                  error ? error->message : "NULL");
-
-            g_clear_error (&error);
-            return;
-          }
-
-        gimp_image_flush (image);
-      }
-      /* fallthrough */
+      private->callback (dialog,
+                         private->item,
+                         private->drawable,
+                         private->context,
+                         private->options,
+                         private->user_data);
+      break;
 
     default:
       gtk_widget_destroy (dialog);
       break;
     }
 }
+
+static void
+stroke_dialog_free (StrokeDialog *private)
+{
+  g_object_unref (private->options);
+
+  g_slice_free (StrokeDialog, private);
+}
diff --git a/app/dialogs/stroke-dialog.h b/app/dialogs/stroke-dialog.h
index 24f62fd..9b52d77 100644
--- a/app/dialogs/stroke-dialog.h
+++ b/app/dialogs/stroke-dialog.h
@@ -21,12 +21,24 @@
 #define __STROKE_DIALOG_H__
 
 
-GtkWidget * stroke_dialog_new (GimpItem    *item,
-                               GimpContext *context,
-                               const gchar *title,
-                               const gchar *icon_name,
-                               const gchar *help_id,
-                               GtkWidget   *parent);
+typedef void (* GimpStrokeCallback) (GtkWidget         *dialog,
+                                     GimpItem          *item,
+                                     GimpDrawable      *drawable,
+                                     GimpContext       *context,
+                                     GimpStrokeOptions *options,
+                                     gpointer           user_data);
+
+
+GtkWidget * stroke_dialog_new (GimpItem           *item,
+                               GimpDrawable       *drawable,
+                               GimpContext        *context,
+                               const gchar        *title,
+                               const gchar        *icon_name,
+                               const gchar        *help_id,
+                               GtkWidget          *parent,
+                               GimpStrokeOptions  *options,
+                               GimpStrokeCallback  callback,
+                               gpointer            user_data);
 
 
 #endif  /*  __STROKE_DIALOG_H__  */
diff --git a/app/tools/gimpvectortool.c b/app/tools/gimpvectortool.c
index 93d13cc..4d6160e 100644
--- a/app/tools/gimpvectortool.c
+++ b/app/tools/gimpvectortool.c
@@ -25,10 +25,13 @@
 #include <gdk/gdkkeysyms.h>
 
 #include "libgimpbase/gimpbase.h"
+#include "libgimpconfig/gimpconfig.h"
 #include "libgimpwidgets/gimpwidgets.h"
 
 #include "tools-types.h"
 
+#include "config/gimpdialogconfig.h"
+
 #include "core/gimp.h"
 #include "core/gimpcontext.h"
 #include "core/gimpimage.h"
@@ -139,6 +142,12 @@ static void     gimp_vector_tool_to_selection_extended
                                                   gint                   state);
 static void     gimp_vector_tool_stroke_vectors  (GimpVectorTool        *vector_tool,
                                                   GtkWidget             *button);
+static void     gimp_vector_tool_stroke_callback (GtkWidget             *dialog,
+                                                  GimpItem              *item,
+                                                  GimpDrawable          *drawable,
+                                                  GimpContext           *context,
+                                                  GimpStrokeOptions     *options,
+                                                  gpointer               data);
 
 
 G_DEFINE_TYPE (GimpVectorTool, gimp_vector_tool, GIMP_TYPE_DRAW_TOOL)
@@ -1953,18 +1962,21 @@ static void
 gimp_vector_tool_stroke_vectors (GimpVectorTool *vector_tool,
                                  GtkWidget      *button)
 {
-  GimpImage    *image;
-  GimpDrawable *active_drawable;
-  GtkWidget    *dialog;
+  GimpDialogConfig *config;
+  GimpImage        *image;
+  GimpDrawable     *drawable;
+  GtkWidget        *dialog;
 
   if (! vector_tool->vectors)
     return;
 
   image = gimp_item_get_image (GIMP_ITEM (vector_tool->vectors));
 
-  active_drawable = gimp_image_get_active_drawable (image);
+  config = GIMP_DIALOG_CONFIG (image->gimp->config);
+
+  drawable = gimp_image_get_active_drawable (image);
 
-  if (! active_drawable)
+  if (! drawable)
     {
       gimp_tool_message (GIMP_TOOL (vector_tool),
                          GIMP_TOOL (vector_tool)->display,
@@ -1973,10 +1985,46 @@ gimp_vector_tool_stroke_vectors (GimpVectorTool *vector_tool,
     }
 
   dialog = stroke_dialog_new (GIMP_ITEM (vector_tool->vectors),
+                              drawable,
                               GIMP_CONTEXT (GIMP_TOOL_GET_OPTIONS (vector_tool)),
                               _("Stroke Path"),
                               GIMP_STOCK_PATH_STROKE,
                               GIMP_HELP_PATH_STROKE,
-                              button);
+                              button,
+                              config->stroke_options,
+                              gimp_vector_tool_stroke_callback,
+                              vector_tool);
   gtk_widget_show (dialog);
 }
+
+static void
+gimp_vector_tool_stroke_callback (GtkWidget         *dialog,
+                                  GimpItem          *item,
+                                  GimpDrawable      *drawable,
+                                  GimpContext       *context,
+                                  GimpStrokeOptions *options,
+                                  gpointer           data)
+{
+  GimpDialogConfig *config = GIMP_DIALOG_CONFIG (context->gimp->config);
+  GimpImage        *image  = gimp_item_get_image (item);
+  GError           *error  = NULL;
+
+  gimp_config_sync (G_OBJECT (options),
+                    G_OBJECT (config->stroke_options), 0);
+
+  if (! gimp_item_stroke (item, drawable, context, options, NULL,
+                          TRUE, NULL, &error))
+    {
+      gimp_message_literal (context->gimp,
+                            G_OBJECT (dialog),
+                            GIMP_MESSAGE_WARNING,
+                            error ? error->message : "NULL");
+
+      g_clear_error (&error);
+      return;
+    }
+
+  gimp_image_flush (image);
+
+  gtk_widget_destroy (dialog);
+}


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