[gimp] app: add gimp_layer_mode_get_context_array()



commit 89b27867e19a7eb87091fb576eff1cbed33e4b9b
Author: Michael Natterer <mitch gimp org>
Date:   Mon Feb 20 23:51:32 2017 +0100

    app: add gimp_layer_mode_get_context_array()
    
    which returns an array of modes in the order they would appear in a
    GimpLayerModeContext's UI (like tool options or the layers dialog),
    without the separators.
    
    Use it in context-commands.c and layers-commands.c instead of static
    (and outdated) arrays for the actions that cycle through modes.

 app/actions/context-commands.c                |   61 +++++++++----------------
 app/actions/layers-commands.c                 |   57 ++++++++---------------
 app/operations/layer-modes/gimp-layer-modes.c |   31 +++++++++++++
 app/operations/layer-modes/gimp-layer-modes.h |    4 ++
 4 files changed, 76 insertions(+), 77 deletions(-)
---
diff --git a/app/actions/context-commands.c b/app/actions/context-commands.c
index 277cba5..8c21b2b 100644
--- a/app/actions/context-commands.c
+++ b/app/actions/context-commands.c
@@ -28,6 +28,8 @@
 
 #include "actions-types.h"
 
+#include "operations/layer-modes/gimp-layer-modes.h"
+
 #include "core/gimp.h"
 #include "core/gimpbrushgenerated.h"
 #include "core/gimpcontext.h"
@@ -48,45 +50,14 @@
 #include "gimp-intl.h"
 
 
-static const GimpLayerMode paint_modes[] =
-{
-  GIMP_LAYER_MODE_NORMAL,
-  GIMP_LAYER_MODE_DISSOLVE,
-  GIMP_LAYER_MODE_BEHIND_LEGACY,
-  GIMP_LAYER_MODE_COLOR_ERASE,
-  GIMP_LAYER_MODE_MULTIPLY_LEGACY,
-  GIMP_LAYER_MODE_DIVIDE_LEGACY,
-  GIMP_LAYER_MODE_SCREEN_LEGACY,
-  GIMP_LAYER_MODE_OVERLAY,
-  GIMP_LAYER_MODE_DODGE_LEGACY,
-  GIMP_LAYER_MODE_BURN_LEGACY,
-  GIMP_LAYER_MODE_HARDLIGHT_LEGACY,
-  GIMP_LAYER_MODE_SOFTLIGHT_LEGACY,
-  GIMP_LAYER_MODE_GRAIN_EXTRACT_LEGACY,
-  GIMP_LAYER_MODE_GRAIN_MERGE_LEGACY,
-  GIMP_LAYER_MODE_DIFFERENCE_LEGACY,
-  GIMP_LAYER_MODE_ADDITION_LEGACY,
-  GIMP_LAYER_MODE_SUBTRACT_LEGACY,
-  GIMP_LAYER_MODE_DARKEN_ONLY_LEGACY,
-  GIMP_LAYER_MODE_LIGHTEN_ONLY_LEGACY,
-  GIMP_LAYER_MODE_HSV_HUE_LEGACY,
-  GIMP_LAYER_MODE_HSV_SATURATION_LEGACY,
-  GIMP_LAYER_MODE_HSV_COLOR_LEGACY,
-  GIMP_LAYER_MODE_HSV_VALUE_LEGACY,
-  GIMP_LAYER_MODE_LCH_HUE,
-  GIMP_LAYER_MODE_LCH_CHROMA,
-  GIMP_LAYER_MODE_LCH_COLOR,
-  GIMP_LAYER_MODE_LCH_LIGHTNESS,
-  GIMP_LAYER_MODE_LUMINANCE
-};
-
-
 /*  local function prototypes  */
 
 static void     context_select_object    (GimpActionSelectType  select_type,
                                           GimpContext          *context,
                                           GimpContainer        *container);
-static gint     context_paint_mode_index (GimpLayerMode         paint_mode);
+static gint     context_paint_mode_index (GimpLayerMode         paint_mode,
+                                          const GimpLayerMode  *modes,
+                                          gint                  n_modes);
 
 static void     context_select_color     (GimpActionSelectType  select_type,
                                           GimpRGB              *color,
@@ -402,17 +373,25 @@ context_paint_mode_cmd_callback (GtkAction *action,
 {
   GimpContext   *context;
   GimpToolInfo  *tool_info;
+  GimpLayerMode *modes;
+  gint           n_modes;
   GimpLayerMode  paint_mode;
   gint           index;
   return_if_no_context (context, data);
 
   paint_mode = gimp_context_get_paint_mode (context);
 
+  modes = gimp_layer_mode_get_context_array (paint_mode,
+                                             GIMP_LAYER_MODE_CONTEXT_PAINT,
+                                             &n_modes);
+  index = context_paint_mode_index (paint_mode, modes, n_modes);
   index = action_select_value ((GimpActionSelectType) value,
-                               context_paint_mode_index (paint_mode),
-                               0, G_N_ELEMENTS (paint_modes) - 1, 0,
+                               index, 0, n_modes - 1, 0,
                                0.0, 1.0, 1.0, 0.0, FALSE);
-  gimp_context_set_paint_mode (context, paint_modes[index]);
+  paint_mode = modes[index];
+  g_free (modes);
+
+  gimp_context_set_paint_mode (context, paint_mode);
 
   tool_info = gimp_context_get_tool (context);
 
@@ -421,7 +400,7 @@ context_paint_mode_cmd_callback (GtkAction *action,
       GimpDisplay *display;
       const char  *value_desc;
 
-      gimp_enum_get_value (GIMP_TYPE_LAYER_MODE, index,
+      gimp_enum_get_value (GIMP_TYPE_LAYER_MODE, paint_mode,
                            NULL, NULL, &value_desc, NULL);
 
       display = action_data_get_display (data);
@@ -749,11 +728,13 @@ context_select_object (GimpActionSelectType  select_type,
 }
 
 static gint
-context_paint_mode_index (GimpLayerMode paint_mode)
+context_paint_mode_index (GimpLayerMode        paint_mode,
+                          const GimpLayerMode *modes,
+                          gint                 n_modes)
 {
   gint i = 0;
 
-  while (i < (G_N_ELEMENTS (paint_modes) - 1) && paint_modes[i] != paint_mode)
+  while (i < (n_modes - 1) && modes[i] != paint_mode)
     i++;
 
   return i;
diff --git a/app/actions/layers-commands.c b/app/actions/layers-commands.c
index 4a13694..e5466d6 100644
--- a/app/actions/layers-commands.c
+++ b/app/actions/layers-commands.c
@@ -31,6 +31,8 @@
 
 #include "config/gimpdialogconfig.h"
 
+#include "operations/layer-modes/gimp-layer-modes.h"
+
 #include "core/gimp.h"
 #include "core/gimpchannel.h"
 #include "core/gimpcontainer.h"
@@ -82,37 +84,6 @@
 #include "gimp-intl.h"
 
 
-static const GimpLayerMode layer_modes[] =
-{
-  GIMP_LAYER_MODE_NORMAL,
-  GIMP_LAYER_MODE_DISSOLVE,
-  GIMP_LAYER_MODE_MULTIPLY_LEGACY,
-  GIMP_LAYER_MODE_DIVIDE_LEGACY,
-  GIMP_LAYER_MODE_SCREEN_LEGACY,
-  GIMP_LAYER_MODE_OVERLAY,
-  GIMP_LAYER_MODE_DODGE_LEGACY,
-  GIMP_LAYER_MODE_BURN_LEGACY,
-  GIMP_LAYER_MODE_HARDLIGHT_LEGACY,
-  GIMP_LAYER_MODE_SOFTLIGHT_LEGACY,
-  GIMP_LAYER_MODE_GRAIN_EXTRACT_LEGACY,
-  GIMP_LAYER_MODE_GRAIN_MERGE_LEGACY,
-  GIMP_LAYER_MODE_DIFFERENCE_LEGACY,
-  GIMP_LAYER_MODE_ADDITION_LEGACY,
-  GIMP_LAYER_MODE_SUBTRACT_LEGACY,
-  GIMP_LAYER_MODE_DARKEN_ONLY_LEGACY,
-  GIMP_LAYER_MODE_LIGHTEN_ONLY_LEGACY,
-  GIMP_LAYER_MODE_HSV_HUE_LEGACY,
-  GIMP_LAYER_MODE_HSV_SATURATION_LEGACY,
-  GIMP_LAYER_MODE_HSV_COLOR_LEGACY,
-  GIMP_LAYER_MODE_HSV_VALUE_LEGACY,
-  GIMP_LAYER_MODE_LCH_HUE,
-  GIMP_LAYER_MODE_LCH_CHROMA,
-  GIMP_LAYER_MODE_LCH_COLOR,
-  GIMP_LAYER_MODE_LCH_LIGHTNESS,
-  GIMP_LAYER_MODE_LUMINANCE
-};
-
-
 /*  local function prototypes  */
 
 static void   layers_new_callback             (GtkWidget             *dialog,
@@ -190,7 +161,9 @@ static void   layers_resize_callback          (GtkWidget             *dialog,
                                                gboolean               unused2,
                                                gpointer               data);
 
-static gint   layers_mode_index               (GimpLayerMode          layer_mode);
+static gint   layers_mode_index               (GimpLayerMode          layer_mode,
+                                               const GimpLayerMode   *modes,
+                                               gint                   n_modes);
 
 
 /*  private variables  */
@@ -1125,6 +1098,8 @@ layers_mode_cmd_callback (GtkAction *action,
 {
   GimpImage     *image;
   GimpLayer     *layer;
+  GimpLayerMode *modes;
+  gint           n_modes;
   GimpLayerMode  layer_mode;
   gint           index;
   GimpUndo      *undo;
@@ -1139,11 +1114,17 @@ layers_mode_cmd_callback (GtkAction *action,
 
   layer_mode = gimp_layer_get_mode (layer);
 
+  modes = gimp_layer_mode_get_context_array (layer_mode,
+                                             GIMP_LAYER_MODE_CONTEXT_LAYER,
+                                             &n_modes);
+  index = layers_mode_index (layer_mode, modes, n_modes);
   index = action_select_value ((GimpActionSelectType) value,
-                               layers_mode_index (layer_mode),
-                               0, G_N_ELEMENTS (layer_modes) - 1, 0,
+                               index, 0, n_modes - 1, 0,
                                0.0, 1.0, 1.0, 0.0, FALSE);
-  gimp_layer_set_mode (layer, layer_modes[index], push_undo);
+  layer_mode = modes[index];
+  g_free (modes);
+
+  gimp_layer_set_mode (layer, layer_mode, push_undo);
   gimp_image_flush (image);
 }
 
@@ -1650,11 +1631,13 @@ layers_resize_callback (GtkWidget    *dialog,
 }
 
 static gint
-layers_mode_index (GimpLayerMode layer_mode)
+layers_mode_index (GimpLayerMode         layer_mode,
+                   const GimpLayerMode  *modes,
+                   gint                  n_modes)
 {
   gint i = 0;
 
-  while (i < (G_N_ELEMENTS (layer_modes) - 1) && layer_modes[i] != layer_mode)
+  while (i < (n_modes - 1) && modes[i] != layer_mode)
     i++;
 
   return i;
diff --git a/app/operations/layer-modes/gimp-layer-modes.c b/app/operations/layer-modes/gimp-layer-modes.c
index 5e8517d..a1a6774 100644
--- a/app/operations/layer-modes/gimp-layer-modes.c
+++ b/app/operations/layer-modes/gimp-layer-modes.c
@@ -1223,6 +1223,37 @@ gimp_layer_mode_get_context (GimpLayerMode mode)
   return info->context;
 }
 
+GimpLayerMode *
+gimp_layer_mode_get_context_array (GimpLayerMode         mode,
+                                   GimpLayerModeContext  context,
+                                   gint                 *n_modes)
+{
+  GimpLayerModeGroup   group;
+  const GimpLayerMode *group_modes;
+  gint                 n_group_modes;
+  GimpLayerMode       *array;
+  gint                 i;
+
+  group = gimp_layer_mode_get_group (mode);
+
+  group_modes = gimp_layer_mode_get_group_array (group, &n_group_modes);
+
+  array = g_new0 (GimpLayerMode, n_group_modes);
+  *n_modes = 0;
+
+  for (i = 0; i < n_group_modes; i++)
+    {
+      if (group_modes[i] != GIMP_LAYER_MODE_SEPARATOR &&
+          (gimp_layer_mode_get_context (group_modes[i]) & context))
+        {
+          array[*n_modes] = group_modes[i];
+          (*n_modes)++;
+        }
+    }
+
+  return array;
+}
+
 static gboolean
 is_mode_in_array (const GimpLayerMode *modes,
                   gint                 n_modes,
diff --git a/app/operations/layer-modes/gimp-layer-modes.h b/app/operations/layer-modes/gimp-layer-modes.h
index 699e390..c5a79f0 100644
--- a/app/operations/layer-modes/gimp-layer-modes.h
+++ b/app/operations/layer-modes/gimp-layer-modes.h
@@ -43,6 +43,10 @@ GimpLayerModeFunc        gimp_layer_mode_get_function        (GimpLayerMode
 
 GimpLayerModeContext     gimp_layer_mode_get_context         (GimpLayerMode        mode);
 
+GimpLayerMode          * gimp_layer_mode_get_context_array   (GimpLayerMode        mode,
+                                                              GimpLayerModeContext context,
+                                                              gint                *n_modes);
+
 GimpLayerModeGroup       gimp_layer_mode_get_group           (GimpLayerMode        mode);
 
 const GimpLayerMode    * gimp_layer_mode_get_group_array     (GimpLayerModeGroup   group,


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