[gimp] app: implement "Crop Image/Layer to Content" menu items in the core



commit 796a9474d8e10cca53162a6df196636cfbf89e2a
Author: Michael Natterer <mitch gimp org>
Date:   Sat Sep 22 20:35:36 2012 +0200

    app: implement "Crop Image/Layer to Content" menu items in the core
    
    in a few lines using existing functionality, as replacement for the
    autocrop plugin. Rename the existing "crop" actions to
    "crop-to-selection".

 app/actions/image-actions.c   |   13 ++++++++++---
 app/actions/image-commands.c  |   33 +++++++++++++++++++++++++++++++--
 app/actions/image-commands.h  |    4 +++-
 app/actions/layers-actions.c  |   13 ++++++++++---
 app/actions/layers-commands.c |   41 ++++++++++++++++++++++++++++++++++++++---
 app/actions/layers-commands.h |    4 +++-
 menus/image-menu.xml.in       |    6 ++++--
 7 files changed, 99 insertions(+), 15 deletions(-)
---
diff --git a/app/actions/image-actions.c b/app/actions/image-actions.c
index 68d9cad..28b7e7b 100644
--- a/app/actions/image-actions.c
+++ b/app/actions/image-actions.c
@@ -100,10 +100,16 @@ static const GimpActionEntry image_actions[] =
     G_CALLBACK (image_scale_cmd_callback),
     GIMP_HELP_IMAGE_SCALE },
 
-  { "image-crop", GIMP_STOCK_TOOL_CROP,
+  { "image-crop-to-selection", GIMP_STOCK_TOOL_CROP,
     NC_("image-action", "_Crop to Selection"), NULL,
     NC_("image-action", "Crop the image to the extents of the selection"),
-    G_CALLBACK (image_crop_cmd_callback),
+    G_CALLBACK (image_crop_to_selection_cmd_callback),
+    GIMP_HELP_IMAGE_CROP },
+
+  { "image-crop-to-content", GIMP_STOCK_TOOL_CROP,
+    NC_("image-action", "Crop to C_ontent"), NULL,
+    NC_("image-action", "Crop the image to the extents of its content (remove empty borders from the image)"),
+    G_CALLBACK (image_crop_to_content_cmd_callback),
     GIMP_HELP_IMAGE_CROP },
 
   { "image-duplicate", GIMP_STOCK_DUPLICATE,
@@ -331,7 +337,8 @@ image_actions_update (GimpActionGroup *group,
   SET_SENSITIVE ("image-resize-to-selection", image && sel);
   SET_SENSITIVE ("image-print-size",          image);
   SET_SENSITIVE ("image-scale",               image);
-  SET_SENSITIVE ("image-crop",                image && sel);
+  SET_SENSITIVE ("image-crop-to-selection",   image && sel);
+  SET_SENSITIVE ("image-crop-to-content",     image);
   SET_SENSITIVE ("image-duplicate",           image);
   SET_SENSITIVE ("image-merge-layers",        image && !aux && lp);
   SET_SENSITIVE ("image-flatten",             image && !aux && lp);
diff --git a/app/actions/image-commands.c b/app/actions/image-commands.c
index 9eedc8c..8191b09 100644
--- a/app/actions/image-commands.c
+++ b/app/actions/image-commands.c
@@ -41,6 +41,8 @@
 #include "core/gimpimage-rotate.h"
 #include "core/gimpimage-scale.h"
 #include "core/gimpimage-undo.h"
+#include "core/gimppickable.h"
+#include "core/gimppickable-auto-shrink.h"
 #include "core/gimpprogress.h"
 
 #include "widgets/gimpdialogfactory.h"
@@ -439,8 +441,8 @@ image_rotate_cmd_callback (GtkAction *action,
 }
 
 void
-image_crop_cmd_callback (GtkAction *action,
-                         gpointer   data)
+image_crop_to_selection_cmd_callback (GtkAction *action,
+                                      gpointer   data)
 {
   GimpImage *image;
   GtkWidget *widget;
@@ -463,6 +465,33 @@ image_crop_cmd_callback (GtkAction *action,
 }
 
 void
+image_crop_to_content_cmd_callback (GtkAction *action,
+                                    gpointer   data)
+{
+  GimpImage *image;
+  GtkWidget *widget;
+  gint       x1, y1, x2, y2;
+  return_if_no_image (image, data);
+  return_if_no_widget (widget, data);
+
+  if (! gimp_pickable_auto_shrink (GIMP_PICKABLE (gimp_image_get_projection (image)),
+                                   0, 0,
+                                   gimp_image_get_width  (image),
+                                   gimp_image_get_height (image),
+                                   &x1, &y1, &x2, &y2))
+    {
+      gimp_message_literal (image->gimp,
+			    G_OBJECT (widget), GIMP_MESSAGE_WARNING,
+			    _("Cannot crop because the image has no content."));
+      return;
+    }
+
+  gimp_image_crop (image, action_data_get_context (data),
+                   x1, y1, x2, y2, TRUE);
+  gimp_image_flush (image);
+}
+
+void
 image_duplicate_cmd_callback (GtkAction *action,
                               gpointer   data)
 {
diff --git a/app/actions/image-commands.h b/app/actions/image-commands.h
index 07fdaec..a74ae07 100644
--- a/app/actions/image-commands.h
+++ b/app/actions/image-commands.h
@@ -45,7 +45,9 @@ void   image_flip_cmd_callback                (GtkAction *action,
 void   image_rotate_cmd_callback              (GtkAction *action,
                                                gint       value,
                                                gpointer   data);
-void   image_crop_cmd_callback                (GtkAction *action,
+void   image_crop_to_selection_cmd_callback   (GtkAction *action,
+                                               gpointer   data);
+void   image_crop_to_content_cmd_callback     (GtkAction *action,
                                                gpointer   data);
 
 void   image_duplicate_cmd_callback           (GtkAction *action,
diff --git a/app/actions/layers-actions.c b/app/actions/layers-actions.c
index 772bde1..bf981a2 100644
--- a/app/actions/layers-actions.c
+++ b/app/actions/layers-actions.c
@@ -206,10 +206,16 @@ static const GimpActionEntry layers_actions[] =
     G_CALLBACK (layers_scale_cmd_callback),
     GIMP_HELP_LAYER_SCALE },
 
-  { "layers-crop", GIMP_STOCK_TOOL_CROP,
+  { "layers-crop-to-selection", GIMP_STOCK_TOOL_CROP,
     NC_("layers-action", "_Crop to Selection"), NULL,
     NC_("layers-action", "Crop the layer to the extents of the selection"),
-    G_CALLBACK (layers_crop_cmd_callback),
+    G_CALLBACK (layers_crop_to_selection_cmd_callback),
+    GIMP_HELP_LAYER_CROP },
+
+  { "layers-crop-to-content", GIMP_STOCK_TOOL_CROP,
+    NC_("layers-action", "Crop to C_ontent"), NULL,
+    NC_("layers-action", "Crop the layer to the extents its content (remove empty borders from the layer)"),
+    G_CALLBACK (layers_crop_to_content_cmd_callback),
     GIMP_HELP_LAYER_CROP },
 
   { "layers-mask-add", GIMP_STOCK_LAYER_MASK,
@@ -643,7 +649,8 @@ layers_actions_update (GimpActionGroup *group,
   SET_SENSITIVE ("layers-resize-to-image", writable && !ac);
   SET_SENSITIVE ("layers-scale",           writable && !ac);
 
-  SET_SENSITIVE ("layers-crop",            writable && sel);
+  SET_SENSITIVE ("layers-crop-to-selection", writable && sel);
+  SET_SENSITIVE ("layers-crop-to-content",   writable);
 
   SET_SENSITIVE ("layers-alpha-add",       writable && !children && !fs && !alpha);
   SET_SENSITIVE ("layers-alpha-remove",    writable && !children && !fs &&  alpha);
diff --git a/app/actions/layers-commands.c b/app/actions/layers-commands.c
index f61a0b9..c494c52 100644
--- a/app/actions/layers-commands.c
+++ b/app/actions/layers-commands.c
@@ -41,6 +41,7 @@
 #include "core/gimpitemundo.h"
 #include "core/gimplayer-floating-sel.h"
 #include "core/gimppickable.h"
+#include "core/gimppickable-auto-shrink.h"
 #include "core/gimptoolinfo.h"
 #include "core/gimpundostack.h"
 #include "core/gimpprogress.h"
@@ -673,8 +674,8 @@ layers_scale_cmd_callback (GtkAction *action,
 }
 
 void
-layers_crop_cmd_callback (GtkAction *action,
-                          gpointer   data)
+layers_crop_to_selection_cmd_callback (GtkAction *action,
+                                       gpointer   data)
 {
   GimpImage *image;
   GimpLayer *layer;
@@ -699,7 +700,7 @@ layers_crop_cmd_callback (GtkAction *action,
   off_y -= y1;
 
   gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                               _("Crop Layer"));
+                               _("Crop Layer to Selection"));
 
   gimp_item_resize (GIMP_ITEM (layer), action_data_get_context (data),
                     x2 - x1, y2 - y1, off_x, off_y);
@@ -710,6 +711,40 @@ layers_crop_cmd_callback (GtkAction *action,
 }
 
 void
+layers_crop_to_content_cmd_callback (GtkAction *action,
+                                     gpointer   data)
+{
+  GimpImage *image;
+  GimpLayer *layer;
+  GtkWidget *widget;
+  gint       x1, y1, x2, y2;
+  return_if_no_layer (image, layer, data);
+  return_if_no_widget (widget, data);
+
+  if (! gimp_pickable_auto_shrink (GIMP_PICKABLE (layer),
+                                   0, 0,
+                                   gimp_item_get_width  (GIMP_ITEM (layer)),
+                                   gimp_item_get_height (GIMP_ITEM (layer)),
+                                   &x1, &y1, &x2, &y2))
+    {
+      gimp_message_literal (image->gimp,
+			    G_OBJECT (widget), GIMP_MESSAGE_WARNING,
+			    _("Cannot crop because the active layer has no content."));
+      return;
+    }
+
+  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                               _("Crop Layer to Content"));
+
+  gimp_item_resize (GIMP_ITEM (layer), action_data_get_context (data),
+                    x2 - x1, y2 - y1, -x1, -y1);
+
+  gimp_image_undo_group_end (image);
+
+  gimp_image_flush (image);
+}
+
+void
 layers_mask_add_cmd_callback (GtkAction *action,
                               gpointer   data)
 {
diff --git a/app/actions/layers-commands.h b/app/actions/layers-commands.h
index 6505684..3a016e6 100644
--- a/app/actions/layers-commands.h
+++ b/app/actions/layers-commands.h
@@ -70,7 +70,9 @@ void   layers_resize_to_image_cmd_callback    (GtkAction   *action,
                                                gpointer     data);
 void   layers_scale_cmd_callback              (GtkAction   *action,
                                                gpointer     data);
-void   layers_crop_cmd_callback               (GtkAction   *action,
+void   layers_crop_to_selection_cmd_callback  (GtkAction   *action,
+                                               gpointer     data);
+void   layers_crop_to_content_cmd_callback    (GtkAction   *action,
                                                gpointer     data);
 
 void   layers_mask_add_cmd_callback           (GtkAction   *action,
diff --git a/menus/image-menu.xml.in b/menus/image-menu.xml.in
index 3910039..e3f4d26 100644
--- a/menus/image-menu.xml.in
+++ b/menus/image-menu.xml.in
@@ -351,7 +351,8 @@
       </placeholder>
       <separator />
       <placeholder name="Crop">
-        <menuitem action="image-crop" />
+        <menuitem action="image-crop-to-selection" />
+        <menuitem action="image-crop-to-content" />
       </placeholder>
       <separator />
       <placeholder name="Structure">
@@ -490,7 +491,8 @@
         <menuitem action="layers-scale" />
       </placeholder>
       <placeholder name="Crop">
-        <menuitem action="layers-crop" />
+        <menuitem action="layers-crop-to-selection" />
+        <menuitem action="layers-crop-to-content" />
       </placeholder>
       <separator />
     </menu>



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