[gimp] app: factor out widget creation to start() functions



commit 4b103eb966e8b3ffb22a703799a25c81cf351834
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jun 27 20:34:31 2017 +0200

    app: factor out widget creation to start() functions
    
    in crop, free select and rectangle select.

 app/tools/gimpcroptool.c            |  124 +++++++++++++++++---------------
 app/tools/gimpfreeselecttool.c      |   62 +++++++++-------
 app/tools/gimprectangleselecttool.c |  136 +++++++++++++++++++----------------
 3 files changed, 176 insertions(+), 146 deletions(-)
---
diff --git a/app/tools/gimpcroptool.c b/app/tools/gimpcroptool.c
index 1650ecb..d2cab37 100644
--- a/app/tools/gimpcroptool.c
+++ b/app/tools/gimpcroptool.c
@@ -82,6 +82,8 @@ static void      gimp_crop_tool_rectangle_response        (GimpToolWidget
 static void      gimp_crop_tool_rectangle_change_complete (GimpToolRectangle    *rectangle,
                                                            GimpCropTool         *crop_tool);
 
+static void      gimp_crop_tool_start                     (GimpCropTool         *crop_tool,
+                                                           GimpDisplay          *display);
 static void      gimp_crop_tool_commit                    (GimpCropTool         *crop_tool);
 static void      gimp_crop_tool_halt                      (GimpCropTool         *crop_tool);
 
@@ -215,61 +217,7 @@ gimp_crop_tool_button_press (GimpTool            *tool,
 
   if (! tool->display)
     {
-      static const gchar *properties[] =
-      {
-        "highlight",
-        "guide",
-        "x",
-        "y",
-        "width",
-        "height",
-        "fixed-rule-active",
-        "fixed-rule",
-        "desired-fixed-width",
-        "desired-fixed-height",
-        "desired-fixed-size-width",
-        "desired-fixed-size-height",
-        "aspect-numerator",
-        "aspect-denominator",
-        "fixed-center"
-      };
-
-      GimpDisplayShell *shell   = gimp_display_get_shell (display);
-      GimpCropOptions  *options = GIMP_CROP_TOOL_GET_OPTIONS (crop_tool);
-      GimpToolWidget   *widget;
-      gint              i;
-
-      tool->display = display;
-
-      crop_tool->rectangle = widget = gimp_tool_rectangle_new (shell);
-
-      gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), widget);
-
-      for (i = 0; i < G_N_ELEMENTS (properties); i++)
-        g_object_bind_property (G_OBJECT (options), properties[i],
-                                G_OBJECT (widget),  properties[i],
-                                G_BINDING_SYNC_CREATE |
-                                G_BINDING_BIDIRECTIONAL);
-
-      g_signal_connect (widget, "changed",
-                        G_CALLBACK (gimp_crop_tool_rectangle_changed),
-                        crop_tool);
-      g_signal_connect (widget, "response",
-                        G_CALLBACK (gimp_crop_tool_rectangle_response),
-                        crop_tool);
-      g_signal_connect (widget, "change-complete",
-                        G_CALLBACK (gimp_crop_tool_rectangle_change_complete),
-                        crop_tool);
-
-      gimp_rectangle_options_connect (GIMP_RECTANGLE_OPTIONS (options),
-                                      gimp_display_get_image (shell->display),
-                                      G_CALLBACK (gimp_crop_tool_auto_shrink),
-                                      crop_tool);
-
-      gimp_tool_rectangle_set_constraint (GIMP_TOOL_RECTANGLE (widget),
-                                          gimp_crop_tool_get_constraint (crop_tool));
-
-      gimp_crop_tool_update_option_defaults (crop_tool, FALSE);
+      gimp_crop_tool_start (crop_tool, display);
 
       gimp_tool_widget_hover (crop_tool->rectangle, coords, state, TRUE);
 
@@ -279,8 +227,6 @@ gimp_crop_tool_button_press (GimpTool            *tool,
        */
       gimp_tool_rectangle_set_function (GIMP_TOOL_RECTANGLE (crop_tool->rectangle),
                                         GIMP_TOOL_RECTANGLE_CREATING);
-
-      gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
     }
 
   if (gimp_tool_widget_button_press (crop_tool->rectangle, coords, time, state,
@@ -402,6 +348,70 @@ gimp_crop_tool_rectangle_change_complete (GimpToolRectangle *rectangle,
 }
 
 static void
+gimp_crop_tool_start (GimpCropTool *crop_tool,
+                      GimpDisplay  *display)
+{
+  static const gchar *properties[] =
+  {
+    "highlight",
+    "guide",
+    "x",
+    "y",
+    "width",
+    "height",
+    "fixed-rule-active",
+    "fixed-rule",
+    "desired-fixed-width",
+    "desired-fixed-height",
+    "desired-fixed-size-width",
+    "desired-fixed-size-height",
+    "aspect-numerator",
+    "aspect-denominator",
+    "fixed-center"
+  };
+
+  GimpTool         *tool    = GIMP_TOOL (crop_tool);
+  GimpDisplayShell *shell   = gimp_display_get_shell (display);
+  GimpCropOptions  *options = GIMP_CROP_TOOL_GET_OPTIONS (crop_tool);
+  GimpToolWidget   *widget;
+  gint              i;
+
+  tool->display = display;
+
+  crop_tool->rectangle = widget = gimp_tool_rectangle_new (shell);
+
+  gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), widget);
+
+  for (i = 0; i < G_N_ELEMENTS (properties); i++)
+    g_object_bind_property (G_OBJECT (options), properties[i],
+                            G_OBJECT (widget),  properties[i],
+                            G_BINDING_SYNC_CREATE |
+                            G_BINDING_BIDIRECTIONAL);
+
+  g_signal_connect (widget, "changed",
+                    G_CALLBACK (gimp_crop_tool_rectangle_changed),
+                    crop_tool);
+  g_signal_connect (widget, "response",
+                    G_CALLBACK (gimp_crop_tool_rectangle_response),
+                    crop_tool);
+  g_signal_connect (widget, "change-complete",
+                    G_CALLBACK (gimp_crop_tool_rectangle_change_complete),
+                    crop_tool);
+
+  gimp_rectangle_options_connect (GIMP_RECTANGLE_OPTIONS (options),
+                                  gimp_display_get_image (shell->display),
+                                  G_CALLBACK (gimp_crop_tool_auto_shrink),
+                                  crop_tool);
+
+  gimp_tool_rectangle_set_constraint (GIMP_TOOL_RECTANGLE (widget),
+                                      gimp_crop_tool_get_constraint (crop_tool));
+
+  gimp_crop_tool_update_option_defaults (crop_tool, FALSE);
+
+  gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
+}
+
+static void
 gimp_crop_tool_commit (GimpCropTool *crop_tool)
 {
   GimpTool        *tool    = GIMP_TOOL (crop_tool);
diff --git a/app/tools/gimpfreeselecttool.c b/app/tools/gimpfreeselecttool.c
index 212bce5..daaf1e0 100644
--- a/app/tools/gimpfreeselecttool.c
+++ b/app/tools/gimpfreeselecttool.c
@@ -186,6 +186,36 @@ gimp_free_select_tool_finalize (GObject *object)
 }
 
 static void
+gimp_free_select_tool_start (GimpFreeSelectTool *fst,
+                             GimpDisplay        *display)
+{
+  GimpTool                  *tool    = GIMP_TOOL (fst);
+  GimpFreeSelectToolPrivate *private = fst->private;
+  GimpSelectionOptions      *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
+  GimpDisplayShell          *shell   = gimp_display_get_shell (display);
+
+  tool->display = display;
+
+  /* We want to apply the selection operation that was current when
+   * the tool was started, so we save this information
+   */
+  private->operation_at_start = options->operation;
+
+  private->polygon = gimp_tool_polygon_new (shell);
+
+  gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), private->polygon);
+
+  g_signal_connect (private->polygon, "changed",
+                    G_CALLBACK (gimp_free_select_tool_polygon_changed),
+                    fst);
+  g_signal_connect (private->polygon, "response",
+                    G_CALLBACK (gimp_free_select_tool_polygon_response),
+                    fst);
+
+  gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
+}
+
+static void
 gimp_free_select_tool_commit (GimpFreeSelectTool *fst,
                               GimpDisplay        *display)
 {
@@ -302,18 +332,14 @@ gimp_free_select_tool_button_press (GimpTool            *tool,
                                     GimpButtonPressType  press_type,
                                     GimpDisplay         *display)
 {
-  GimpDrawTool              *draw_tool = GIMP_DRAW_TOOL (tool);
-  GimpFreeSelectTool        *fst       = GIMP_FREE_SELECT_TOOL (tool);
-  GimpFreeSelectToolPrivate *private   = fst->private;
-  GimpSelectionOptions      *options   = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
+  GimpFreeSelectTool        *fst     = GIMP_FREE_SELECT_TOOL (tool);
+  GimpFreeSelectToolPrivate *private = fst->private;
 
   if (tool->display && tool->display != display)
     gimp_tool_control (tool, GIMP_TOOL_ACTION_COMMIT, tool->display);
 
   if (! private->polygon) /* not tool->display, we have a subclass */
     {
-      GimpDisplayShell *shell = gimp_display_get_shell (display);
-
       /* First of all handle delegation to the selection mask edit logic
        * if appropriate.
        */
@@ -323,36 +349,18 @@ gimp_free_select_tool_button_press (GimpTool            *tool,
           return;
         }
 
-      tool->display = display;
-
-      /* We want to apply the selection operation that was current when
-       * the tool was started, so we save this information
-       */
-      private->operation_at_start = options->operation;
-
-      private->polygon = gimp_tool_polygon_new (shell);
-
-      gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), private->polygon);
-
-      g_signal_connect (private->polygon, "changed",
-                        G_CALLBACK (gimp_free_select_tool_polygon_changed),
-                        fst);
-      g_signal_connect (private->polygon, "response",
-                        G_CALLBACK (gimp_free_select_tool_polygon_response),
-                        fst);
-
-      gimp_draw_tool_start (draw_tool, display);
+      gimp_free_select_tool_start (fst, display);
 
       gimp_tool_widget_hover (private->polygon, coords, state, TRUE);
     }
 
-  gimp_tool_control_activate (tool->control);
-
   if (gimp_tool_widget_button_press (private->polygon, coords, time, state,
                                      press_type))
     {
       private->grab_widget = private->polygon;
     }
+
+  gimp_tool_control_activate (tool->control);
 }
 
 static void
diff --git a/app/tools/gimprectangleselecttool.c b/app/tools/gimprectangleselecttool.c
index 72916b7..3537d3c 100644
--- a/app/tools/gimprectangleselecttool.c
+++ b/app/tools/gimprectangleselecttool.c
@@ -119,6 +119,8 @@ static void     gimp_rectangle_select_tool_rectangle_change_complete
                                                           (GimpToolWidget          *widget,
                                                            GimpRectangleSelectTool *rect_tool);
 
+static void     gimp_rectangle_select_tool_start          (GimpRectangleSelectTool *rect_tool,
+                                                           GimpDisplay             *display);
 static void     gimp_rectangle_select_tool_commit         (GimpRectangleSelectTool *rect_tool);
 static void     gimp_rectangle_select_tool_halt           (GimpRectangleSelectTool *rect_tool);
 
@@ -246,74 +248,16 @@ gimp_rectangle_select_tool_button_press (GimpTool            *tool,
 
   if (! tool->display)
     {
-      static const gchar *properties[] =
-      {
-        "highlight",
-        "guide",
-        "round-corners",
-        "corner-radius",
-        "x",
-        "y",
-        "width",
-        "height",
-        "fixed-rule-active",
-        "fixed-rule",
-        "desired-fixed-width",
-        "desired-fixed-height",
-        "desired-fixed-size-width",
-        "desired-fixed-size-height",
-        "aspect-numerator",
-        "aspect-denominator",
-        "fixed-center"
-      };
-
-      GimpDisplayShell           *shell = gimp_display_get_shell (display);
-      GimpRectangleSelectOptions *options;
-      GimpToolWidget             *widget;
-      gint                        i;
-
-      options = GIMP_RECTANGLE_SELECT_TOOL_GET_OPTIONS (rect_tool);
-
-      tool->display = display;
-
-      private->rectangle = widget = gimp_tool_rectangle_new (shell);
-
-      g_object_set (widget,
-                    "draw-ellipse", GIMP_RECTANGLE_SELECT_TOOL_GET_CLASS (rect_tool)->draw_ellipse,
-                    NULL);
-
-      gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), widget);
-
-      for (i = 0; i < G_N_ELEMENTS (properties); i++)
-        g_object_bind_property (G_OBJECT (options), properties[i],
-                                G_OBJECT (widget),  properties[i],
-                                G_BINDING_SYNC_CREATE |
-                                G_BINDING_BIDIRECTIONAL);
-
-      g_signal_connect (widget, "response",
-                        G_CALLBACK (gimp_rectangle_select_tool_rectangle_response),
-                        rect_tool);
-      g_signal_connect (widget, "change-complete",
-                        G_CALLBACK (gimp_rectangle_select_tool_rectangle_change_complete),
-                        rect_tool);
-
-      gimp_rectangle_options_connect (GIMP_RECTANGLE_OPTIONS (options),
-                                      gimp_display_get_image (shell->display),
-                                      G_CALLBACK (gimp_rectangle_select_tool_auto_shrink),
-                                      rect_tool);
-
-      gimp_rectangle_select_tool_update_option_defaults (rect_tool, FALSE);
+      gimp_rectangle_select_tool_start (rect_tool, display);
 
-      gimp_tool_widget_hover (widget, coords, state, TRUE);
+      gimp_tool_widget_hover (private->rectangle, coords, state, TRUE);
 
       /* HACK: force CREATING on a newly created rectangle; otherwise,
        * the above binding of properties would cause the rectangle to
        * start with the size from tool options.
        */
-      gimp_tool_rectangle_set_function (GIMP_TOOL_RECTANGLE (widget),
+      gimp_tool_rectangle_set_function (GIMP_TOOL_RECTANGLE (private->rectangle),
                                         GIMP_TOOL_RECTANGLE_CREATING);
-
-      gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
     }
 
   private->saved_show_selection = gimp_display_shell_get_show_selection (shell);
@@ -688,6 +632,74 @@ gimp_rectangle_select_tool_rectangle_change_complete (GimpToolWidget          *w
   gimp_rectangle_select_tool_update_option_defaults (rect_tool, FALSE);
 }
 
+static void
+gimp_rectangle_select_tool_start (GimpRectangleSelectTool *rect_tool,
+                                  GimpDisplay             *display)
+{
+  static const gchar *properties[] =
+  {
+    "highlight",
+    "guide",
+    "round-corners",
+    "corner-radius",
+    "x",
+    "y",
+    "width",
+    "height",
+    "fixed-rule-active",
+    "fixed-rule",
+    "desired-fixed-width",
+    "desired-fixed-height",
+    "desired-fixed-size-width",
+    "desired-fixed-size-height",
+    "aspect-numerator",
+    "aspect-denominator",
+    "fixed-center"
+  };
+
+  GimpTool                       *tool    = GIMP_TOOL (rect_tool);
+  GimpRectangleSelectToolPrivate *private = rect_tool->private;
+  GimpDisplayShell               *shell   = gimp_display_get_shell (display);
+  GimpRectangleSelectOptions     *options;
+  GimpToolWidget                 *widget;
+  gint                            i;
+
+  options = GIMP_RECTANGLE_SELECT_TOOL_GET_OPTIONS (rect_tool);
+
+  tool->display = display;
+
+  private->rectangle = widget = gimp_tool_rectangle_new (shell);
+
+  g_object_set (widget,
+                "draw-ellipse",
+                GIMP_RECTANGLE_SELECT_TOOL_GET_CLASS (rect_tool)->draw_ellipse,
+                NULL);
+
+  gimp_draw_tool_set_widget (GIMP_DRAW_TOOL (tool), widget);
+
+  for (i = 0; i < G_N_ELEMENTS (properties); i++)
+    g_object_bind_property (G_OBJECT (options), properties[i],
+                            G_OBJECT (widget),  properties[i],
+                            G_BINDING_SYNC_CREATE |
+                            G_BINDING_BIDIRECTIONAL);
+
+  g_signal_connect (widget, "response",
+                    G_CALLBACK (gimp_rectangle_select_tool_rectangle_response),
+                    rect_tool);
+  g_signal_connect (widget, "change-complete",
+                    G_CALLBACK (gimp_rectangle_select_tool_rectangle_change_complete),
+                    rect_tool);
+
+  gimp_rectangle_options_connect (GIMP_RECTANGLE_OPTIONS (options),
+                                  gimp_display_get_image (shell->display),
+                                  G_CALLBACK (gimp_rectangle_select_tool_auto_shrink),
+                                  rect_tool);
+
+  gimp_rectangle_select_tool_update_option_defaults (rect_tool, FALSE);
+
+  gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
+}
+
 /* This function is called if the user clicks and releases the left
  * button without moving it.  There are the things we might want
  * to do here:


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